1 <?php
3 class faiDiskEntry extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account= TRUE;
7 var $attributes = array("DISKcn","DISKdescription");
8 var $objectclasses= array();
10 var $DISKcn = "";
11 var $DISKdescription = "";
13 var $partitions = array();
14 var $UsedAttrs = array();
15 var $is_edit = false;
16 var $old_cn = "";
17 var $status = "new";
18 var $deletePartitions = array();
20 var $FAIstate = "";
22 function faiDiskEntry (&$config, $dn= NULL,$usedDiskNames=array(),$disk=false)
23 {
24 plugin::plugin ($config, $dn);
25 $this->UsedAttrs = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
26 "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
28 /* Default status is new */
29 $this->status = "new";
31 /* We want to edit an entry */
32 if($disk){
34 /* Set disk status */
35 $this->status = $disk['status'];
37 /* Walk through partitions */
38 foreach($disk['partitions'] as $name => $values){
40 /* If a partition is already marked as delete, attach it to deletePartitions only. */
41 if($values['status'] == "delete"){
42 unset($disk['partitions'][$name]);
43 $this->deletePartitions[]=$values;
44 }else{
46 /* Set status, to know which partition must be deleted from ldap
47 new : Neu partition entry // save
48 edited : Update partition entry in ldap
49 deleted: Remove partition from ldap
50 */
52 /* If status is not new, set to edit mode.
53 * New means that this partition currently wasn't saved to ldap.
54 */
55 if($disk['partitions'][$name]['status']!="new"){
56 $disk['partitions'][$name]['status']="edited";
57 }
59 $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
61 /* Assign empty attributes, if attribute is missing */
62 foreach($this->UsedAttrs as $attr){
63 if(!isset($values[$attr])){
64 $disk['partitions'][$name][$attr]="";
65 }
66 }
68 if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
69 $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
70 }
71 }
72 }
74 /* Set default attributes */
75 $this->DISKcn = $disk['cn'];
76 $this->DISKdescription = $disk['description'];
77 $this->partitions = $disk['partitions'];
78 $this->is_edit = true;
79 $this->old_cn = $disk['cn'];
80 }
81 }
84 function execute()
85 {
86 /* Call parent execute */
87 plugin::execute();
89 /* Fill templating stuff */
90 $smarty = get_smarty();
91 $s_action = "";
92 $s_entry = "";
93 $display = "";
95 /* Assign values
96 * And Checkbox selection
97 */
98 foreach($this->attributes as $attrs){
99 $smarty->assign($attrs,$this->$attrs);
100 if($this->$attrs){
101 $smarty->assign($attrs."CHK"," ");
102 }else{
103 $smarty->assign($attrs."CHK"," disabled ");
104 }
105 }
107 /* Check all Posts if there is something usefull for us,
108 * For example : Delete is posted as Delete_1
109 * The number specifies the index we want to delete
110 */
111 foreach($_POST as $name => $value){
112 if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
113 $tmp = split("_",$name);
114 $s_action = "remove";
115 $s_entry = $tmp[1];
116 }
117 }
119 /* To remove a partition we unset the index posted.
120 * We must sort the index again, else we possibly got problems
121 * with partitions order.
122 */
123 if($s_action == "remove" && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
124 if($this->partitions[$s_entry]['status'] == "edited"){
125 $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
126 $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
127 unset($this->partitions[$s_entry]);
128 }else{
129 unset($this->partitions[$s_entry]);
130 }
131 $tmp= array();
132 foreach($this->partitions as $part){
133 $tmp[count($tmp)+1]=$part;
134 }
135 $this->partitions = $tmp;
136 }
138 /* To add a partitions we only append an empty
139 * array to the already defined partitions.
140 */
141 if(isset($_POST['AddPartition']) && $this->acl_is_createable() && !preg_match("/freeze/i",$this->FAIstate)){
142 foreach($this->UsedAttrs as $attr){
143 $tmp[$attr] = "";
144 }
145 $tmp["old_cn"] = "";
146 $tmp['status']="new";
147 $this->partitions[count($this->partitions)+1]=$tmp;
148 }
150 /* $setup contains a table with the partitions.
151 */
152 $smarty->assign("setup", $this->generateParts());
153 foreach($this->attributes as $attr){
154 $smarty->assign($attr,$this->$attr);
155 }
157 $tmp = $this->plInfo();
158 $sacl = "";
159 foreach($tmp['plProvidedAcls'] as $name => $translated){
160 $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
161 $smarty->assign($name."ACL",$acl);
162 }
164 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
165 $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
167 // Fill boxes
168 $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
169 $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
170 $smarty->assign("fstabkey", "device");
171 $smarty->assign("disklabel", "msdos");
173 /* Fetch template and show the result
174 */
175 $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
176 return($display);
177 }
179 function generateParts()
180 {
181 /* Define Arrays with allowed syntax */
182 $PartitionTypes = array("primary"=>_("primary"),"logical"=>_("logical"));
183 $FAIfsTypes = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
185 /* Display Header */
186 $str = "";
187 if (count($this->partitions)){
188 $str .= "<tr>";
189 $str .= "<td><b>"._("Type")."</b></td>";
190 $str .= "<td><b>"._("FS type")."</b></td>";
191 $str .= "<td><b>"._("Mount point")."</b></td>";
192 $str .= "<td><b>"._("Size in MB")."</b></td>";
193 $str .= "<td><b>"._("Mount options")."</b></td>";
194 $str .= "<td><b>"._("FS option")."</b></td>";
195 $str .= "<td><b>"._("Preserve")."</b></td>";
196 $str .= "<td> </td>";
197 $str .= "</tr>";
198 }
200 /* Walk through all defined partitions.
201 * Create a new row for each partition and append it to
202 * the header defined above.
203 * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
204 */
205 foreach($this->partitions as $key => $part){
207 $dis = "";
208 if($part['FAIpartitionFlags'] == "preserve"){
209 $dis = " disabled ";
210 }
212 $disableALL = "";
213 if (preg_match("/freeze/", $this->FAIstate)){
214 $disableALL = " disabled ";
215 }
217 if($part['status']!="delete"){
219 /* Generate Partition select box
220 */
221 $PartitionType = "";
222 if($this->acl_is_writeable("FAIpartitionType")){
223 $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
224 foreach($PartitionTypes as $type => $PTname){
225 if($part['FAIpartitionType'] == $type){
226 $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
227 }else{
228 $PartitionType .="<option value='".$type."'>".$PTname."</option>";
229 }
230 }
231 $PartitionType.="</select>";
232 }elseif($this->acl_is_readable("FAIpartitionType")){
233 $PartitionType = $part['FAIpartitionType'];
234 }
237 /* Generate fsType select box
238 */
239 $FAIfsType= "";
240 if($this->acl_is_writeable("FAIfsType")){
241 $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
242 foreach($FAIfsTypes as $type){
243 if($part['FAIfsType'] == $type){
244 $FAIfsType .="<option value='".$type."' selected >".$type."</option>";
245 }else{
246 $FAIfsType .="<option value='".$type."'>".$type."</option>";
247 }
248 }
249 $FAIfsType.="</select>";
250 }elseif($this->acl_is_readable("FAIfsType")){
251 $FAIfsType = $part['FAIfsType'];
252 }
254 foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
255 $$attr = "";
256 if($this->acl_is_writeable($attr)){
257 $$attr = "<input name='".$attr."_".$key."' ".$disableALL." ".$dis."
258 value='".$part[$attr]."' id='".$attr."_".$key."'>";
260 }elseif($this->acl_is_readable($attr)){
261 $$attr = $part[$attr];
262 }
263 }
266 $str .= "\n<tr>";
267 $str .= "\n<td>".$PartitionType."</td>";
268 $str .= "\n<td>".$FAIfsType."</td>";
269 $str .= "\n<td>".$FAImountPoint."</td>";
270 $str .= "\n<td>".$FAIpartitionSize."</td>";
271 $str .= "\n<td>".$FAImountOptions."</td>";
272 $str .= "\n<td>".$FAIfsOptions."</td>";
274 $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
275 "changeState('FAIpartitionSize_".$key."') ; ".
276 "changeState('FAImountOptions_".$key."') ; ".
277 "changeState('FAIfsType_".$key."') ; ".
278 "changeState('FAIfsOptions_".$key."') ; \"";
280 if($this->acl_is_writeable("FAIpartitionFlags")){
281 if($part['FAIpartitionFlags']!=false){
282 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
283 }else{
284 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
285 }
286 }else{
287 if($part['FAIpartitionFlags']!=false){
288 $str .= "<td>"._("False")."</td>";
289 }else{
290 $str .= "<td>"._("True")."</td>";
291 }
292 }
293 if($this->acl_is_removeable()){
294 $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
295 }else{
296 $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";
297 }
298 $str .= "\n</tr>";
299 }
300 }
302 if(!empty($str)){
303 $str = "<table summary='' style='width:100%'>".$str."</table>";
304 }
305 return($str);
307 }
309 function save()
310 {
311 $tmp = array();
312 $tmp['cn'] = $this->DISKcn;
314 /* Attach partitions */
315 foreach($this->partitions as $key=>$val) {
316 $this->partitions[$key]['FAIpartitionNr']=$key;
317 }
319 /* Attach deleted */
320 foreach($this->deletePartitions as $key=>$val) {
321 $this->partitions[$key."-delete"]=$val;
322 $this->partitions[$key."-delete"]['status']="delete";
323 }
325 $tmp['description'] = $this->DISKdescription;
326 $tmp['partitions'] = $this->partitions;
327 $tmp['status'] = $this->status;
329 /* If hdd name has changed, tell partitionTable to rename it */
330 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
331 $tmp['rename']['from'] = $this->old_cn;
332 $tmp['rename']['to'] = $this->DISKcn;
333 }
334 return($tmp);
335 }
338 /* Save data to object */
339 function save_object()
340 {
341 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
342 plugin::save_object();
344 foreach($this->partitions as $key => $part){
345 foreach($this->UsedAttrs as $attrs){
347 if($this->acl_is_writeable($attrs)){
349 if(isset($_POST[$attrs."_".$key])){
350 $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
351 }else{
352 $this->partitions[$key][$attrs] = false;
353 }
354 }
355 }
357 /* Adapt ext3 to have -j option */
358 if ($this->partitions[$key]["FAIfsType"] == "ext3") {
359 $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
360 $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
361 }
362 }
364 }
365 }
368 /* Check supplied data */
369 function check()
370 {
371 /* Call common method to give check the hook */
372 $message= plugin::check();
374 /* Check for an empty disk name */
375 $d = trim($this->DISKcn);
376 if($d == "" ){
377 $message[] = msgPool::required(_("Name"));
378 }
379 if(preg_match("/[^a-z0-9_\-]/i",$d)){
380 $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
381 }
383 /* check every partition.
384 * if there is an invalid value defined, append an errorstr to message
385 */
387 /* Array that contain every partitionname mountpoint etc already assigned */
388 $alreadyUsed = array();
389 foreach($this->UsedAttrs as $attrs){
390 $alreadyUsed[$attrs] = array();
391 }
393 foreach($this->partitions as $key => $part){
395 /* Skip all checks, if preserve is set */
396 if($part['FAIpartitionFlags'] == "preserve"){
397 $this->partitions[$key]['FAIfsType'] = "preserve";
398 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
399 continue;
400 }
402 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
403 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
404 }
406 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
407 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
408 $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
409 }
410 }
411 if($part['FAIfsType'] == "swap"){
412 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
413 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
414 }
415 }
416 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
417 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
418 }
420 $tmp = split("-",$part['FAIpartitionSize']);
421 switch (count($tmp)){
422 case 0:
423 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
424 break;
425 case 1:
426 if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
427 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
428 }
429 break;
431 case 2:
432 if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
433 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
434 }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
435 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
436 }
437 break;
439 default:
440 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
441 }
443 foreach($this->UsedAttrs as $attrs){
444 $alreadyUsed[$attrs][$key] = $part[$attrs];
445 }
446 }
448 $cnt = 0;
449 foreach($this->partitions as $key => $part){
450 if($part['FAIpartitionType'] == "primary"){
451 $cnt ++ ;
452 }
453 }
454 if($cnt > 3){
455 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
456 }
458 return ($message);
459 }
463 /* Return plugin informations for acl handling */
464 static function plInfo()
465 {
466 return (array(
467 "plShortName" => _("Partition table entry"),
468 "plDescription" => _("FAI partition table entry"),
469 "plSelfModify" => FALSE,
470 "plDepends" => array(),
471 "plPriority" => 27,
472 "plSection" => array("administration"),
473 "plCategory" => array("fai"),
474 "plProvidedAcls" => array(
475 "DISKcn" => _("Name"),
476 "DISKdescription" => _("Description"),
477 "DISKFAIdiskOption" => _("Disk options"),
478 "FAIpartitionType" => _("Partition type"),
479 "FAIpartitionNr" => _("Partition no."),
480 "FAIfsType" => _("File system type"),
481 "FAImountPoint" => _("Mount point"),
482 "FAIpartitionSize" => _("Partition size"),
483 "FAImountOptions" => _("Mount options"),
484 "FAIfsOptions" => _("File system options"),
485 "FAIpartitionFlags" => _("Partition flags"))
486 ));
487 }
490 }
492 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
493 ?>