1 <?php
3 class faiPartitionTableEntry extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account= TRUE;
12 var $attributes = array("DISKcn","DISKdescription");
13 var $objectclasses= array();
15 var $DISKcn = "";
16 var $DISKdescription = "";
18 var $partitions = array();
19 var $UsedAttrs = array();
20 var $is_edit = false;
21 var $old_cn = "";
22 var $status = "new";
23 var $deletePartitions = array();
25 var $FAIstate = "";
27 function faiPartitionTableEntry (&$config, $dn= NULL,$usedDiskNames=array(),$disk=false)
28 {
29 plugin::plugin ($config, $dn);
30 $this->UsedAttrs = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
31 "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
33 /* Default status is new */
34 $this->status = "new";
36 /* We want to edit an entry */
37 if($disk){
39 /* Set disk status */
40 $this->status = $disk['status'];
42 /* Walk through partitions */
43 foreach($disk['partitions'] as $name => $values){
45 /* If a partition is already marked as delete, attach it to deletePartitions only. */
46 if($values['status'] == "delete"){
47 unset($disk['partitions'][$name]);
48 $this->deletePartitions[]=$values;
49 }else{
51 /* Set status, to know which partition must be deleted from ldap
52 new : Neu partition entry // save
53 edited : Update partition entry in ldap
54 deleted: Remove partition from ldap
55 */
57 /* If status is not new, set to edit mode.
58 * New means that this partition currently wasn't saved to ldap.
59 */
60 if($disk['partitions'][$name]['status']!="new"){
61 $disk['partitions'][$name]['status']="edited";
62 }
64 $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
66 /* Assign empty attributes, if attribute is missing */
67 foreach($this->UsedAttrs as $attr){
68 if(!isset($values[$attr])){
69 $disk['partitions'][$name][$attr]="";
70 }
71 }
73 if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
74 $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
75 $disk['partitions'][$name]['FAIencrypted']= true;
76 } else {
77 $disk['partitions'][$name]['FAIencrypted']= false;
78 }
79 }
80 }
82 /* Set default attributes */
83 $this->DISKcn = $disk['cn'];
84 $this->DISKdescription = $disk['description'];
85 $this->partitions = $disk['partitions'];
86 $this->is_edit = true;
87 $this->old_cn = $disk['cn'];
88 }
89 }
92 function execute()
93 {
94 /* Call parent execute */
95 plugin::execute();
97 /* Fill templating stuff */
98 $smarty = get_smarty();
99 $s_action = "";
100 $s_entry = "";
101 $display = "";
103 /* Assign values
104 * And Checkbox selection
105 */
106 foreach($this->attributes as $attrs){
107 $smarty->assign($attrs,$this->$attrs);
108 if($this->$attrs){
109 $smarty->assign($attrs."CHK"," ");
110 }else{
111 $smarty->assign($attrs."CHK"," disabled ");
112 }
113 }
115 /* Check all Posts if there is something usefull for us,
116 * For example : Delete is posted as Delete_1
117 * The number specifies the index we want to delete
118 */
119 foreach($_POST as $name => $value){
120 if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable()){
121 $tmp = split("_",$name);
122 $s_action = "remove";
123 $s_entry = $tmp[1];
124 }
125 }
127 /* To remove a partition we unset the index posted.
128 * We must sort the index again, else we possibly got problems
129 * with partitions order.
130 */
131 if($s_action == "remove" && $this->acl_is_removeable()){
132 if($this->partitions[$s_entry]['status'] == "edited"){
133 $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
134 $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
135 unset($this->partitions[$s_entry]);
136 }else{
137 unset($this->partitions[$s_entry]);
138 }
139 $tmp= array();
140 foreach($this->partitions as $part){
141 $tmp[count($tmp)+1]=$part;
142 }
143 $this->partitions = $tmp;
144 }
146 /* To add a partitions we only append an empty
147 * array to the already defined partitions.
148 */
149 if(isset($_POST['AddPartition']) && $this->acl_is_createable()){
150 foreach($this->UsedAttrs as $attr){
151 $tmp[$attr] = "";
152 }
153 $tmp["old_cn"] = "";
154 $tmp["FAIencrypted"] = FALSE;
155 $tmp['status']="new";
156 $this->partitions[count($this->partitions)+1]=$tmp;
157 }
159 /* $setup contains a table with the partitions.
160 */
161 $smarty->assign("setup", $this->generateParts());
162 foreach($this->attributes as $attr){
163 $smarty->assign($attr,$this->$attr);
164 }
166 $tmp = $this->plInfo();
167 $sacl = "";
168 foreach($tmp['plProvidedAcls'] as $name => $translated){
170 $acl = $this->getacl($name);
171 $sacl .= $acl;
172 if($this->FAIstate == "freezed"){
173 $acl = preg_replace("/w/","",$acl);
174 }
175 $smarty->assign($name."ACL",$acl);
176 }
177 $smarty->assign("allowSave",$sacl);
179 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
181 /* Fetch template and show the result
182 */
183 $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
184 return($display);
185 }
187 function generateParts()
188 {
189 /* Define Arrays with allowed syntax */
190 $PartitionTypes = array("primary"=>_("primary"),"logical"=>_("logical"));
191 $FAIfsTypes = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
193 /* Display Header */
194 $str = "";
195 if (count($this->partitions)){
196 $str .= "<tr>";
197 $str .= "<td><b>"._("Type")."</b></td>";
198 $str .= "<td><b>"._("FS type")."</b></td>";
199 $str .= "<td><b>"._("Mount point")."</b></td>";
200 $str .= "<td><b>"._("Size in MB")."</b></td>";
201 $str .= "<td><b>"._("Mount options")."</b></td>";
202 $str .= "<td><b>"._("FS option")."</b></td>";
203 $str .= "<td><b>"._("Preserve")."</b></td>";
204 $str .= "<td><b>"._("Encrypted")."</b></td>";
205 $str .= "<td> </td>";
206 $str .= "</tr>";
207 }
209 /* Walk through all defined partitions.
210 * Create a new row for each partition and append it to
211 * the header defined above.
212 * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
213 */
214 foreach($this->partitions as $key => $part){
216 $dis = "";
217 if($part['FAIpartitionFlags'] == "preserve"){
218 $dis = " disabled ";
219 }
221 $disableALL = "";
222 if($this->FAIstate =="freeze"){
223 $disableALL = " disabled ";
224 }
226 if($part['status']!="delete"){
228 /* Generate Partition select box
229 */
230 $PartitionType = "";
231 if($this->acl_is_writeable("FAIpartitionType")){
232 $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
233 foreach($PartitionTypes as $type => $PTname){
234 if($part['FAIpartitionType'] == $type){
235 $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
236 }else{
237 $PartitionType .="<option value='".$type."'>".$PTname."</option>";
238 }
239 }
240 $PartitionType.="</select>";
241 }elseif($this->acl_is_readable("FAIpartitionType")){
242 $PartitionType = $part['FAIpartitionType'];
243 }
246 /* Generate fsType select box
247 */
248 $FAIfsType= "";
249 if($this->acl_is_writeable("FAIfsType")){
250 $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
251 foreach($FAIfsTypes as $type){
252 if($part['FAIfsType'] == $type){
253 $FAIfsType .="<option value='".$type."' selected >".$type."</option>";
254 }else{
255 $FAIfsType .="<option value='".$type."'>".$type."</option>";
256 }
257 }
258 $FAIfsType.="</select>";
259 }elseif($this->acl_is_readable("FAIfsType")){
260 $FAIfsType = $part['FAIfsType'];
261 }
263 foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
264 $$attr = "";
265 if($this->acl_is_writeable($attr)){
266 $$attr = "<input name='".$attr."_".$key."' ".$disableALL." ".$dis."
267 value='".$part[$attr]."' id='".$attr."_".$key."'>";
269 }elseif($this->acl_is_readable($attr)){
270 $$attr = $part[$attr];
271 }
272 }
275 $str .= "\n<tr>";
276 $str .= "\n<td>".$PartitionType."</td>";
277 $str .= "\n<td>".$FAIfsType."</td>";
278 $str .= "\n<td>".$FAImountPoint."</td>";
279 $str .= "\n<td>".$FAIpartitionSize."</td>";
280 $str .= "\n<td>".$FAImountOptions."</td>";
281 $str .= "\n<td>".$FAIfsOptions."</td>";
283 $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
284 "changeState('FAIpartitionSize_".$key."') ; ".
285 "changeState('FAImountOptions_".$key."') ; ".
286 "changeState('FAIfsType_".$key."') ; ".
287 "changeState('FAIfsOptions_".$key."') ; \"";
289 if($this->acl_is_writeable("FAIpartitionFlags")){
290 if($part['FAIpartitionFlags']!=false){
291 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
292 }else{
293 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
294 }
295 if($part['FAIencrypted']!=false){
296 $str .= "\n<td><input type='checkbox' name='FAIencrypted_".$key."' value='encrypted' checked></td>";
297 }else{
298 $str .= "\n<td><input type='checkbox' name='FAIencrypted_".$key."' value='encrypted'></td>";
299 }
300 }else{
301 if($part['FAIpartitionFlags']!=false){
302 $str .= "<td>"._("False")."</td>";
303 }else{
304 $str .= "<td>"._("True")."</td>";
305 }
306 if($part['FAIencrypted']!=false){
307 $str .= "<td>"._("False")."</td>";
308 }else{
309 $str .= "<td>"._("True")."</td>";
310 }
311 }
312 if($this->acl_is_removeable()){
313 $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
314 }else{
315 $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";
316 }
317 $str .= "\n</tr>";
318 }
319 }
321 if(!empty($str)){
322 $str = "<table summary='' style='width:100%'>".$str."</table>";
323 }
324 return($str);
326 }
328 function save()
329 {
330 $tmp = array();
331 $tmp['cn'] = $this->DISKcn;
333 /* Attach partitions */
334 foreach($this->partitions as $key=>$val) {
335 if (isset($this->partitions[$key]['FAIencrypted']) && $this->partitions[$key]['FAIencrypted']){
336 unset ($this->partitions[$key]['FAIencrypted']);
337 $this->partitions[$key]['FAIfsType']= "_".$this->partitions[$key]['FAIfsType'];
338 }
339 $this->partitions[$key]['FAIpartitionNr']=$key;
340 }
342 /* Attach deleted */
343 foreach($this->deletePartitions as $key=>$val) {
344 $this->partitions[$key."-delete"]=$val;
345 $this->partitions[$key."-delete"]['status']="delete";
346 }
348 $tmp['description'] = $this->DISKdescription;
349 $tmp['partitions'] = $this->partitions;
350 $tmp['status'] = $this->status;
352 /* If hdd name has changed, tell partitionTable to rename it */
353 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
354 $tmp['rename']['from'] = $this->old_cn;
355 $tmp['rename']['to'] = $this->DISKcn;
356 }
357 return($tmp);
358 }
361 /* Save data to object */
362 function save_object()
363 {
364 if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
365 plugin::save_object();
367 foreach($this->partitions as $key => $part){
368 foreach($this->UsedAttrs as $attrs){
370 if($this->acl_is_writeable($attrs)){
372 if(isset($_POST[$attrs."_".$key])){
373 $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
374 }else{
375 $this->partitions[$key][$attrs] = false;
376 }
377 }
378 }
380 if($this->acl_is_writeable("FAIencrypted")){
382 if(isset($_POST["FAIencrypted_".$key])){
383 $this->partitions[$key]["FAIencrypted"] = $_POST["FAIencrypted_".$key];
384 }else{
385 $this->partitions[$key]["FAIencrypted"] = false;
386 }
387 }
389 /* Adapt ext3 to have -j option */
390 if ($this->partitions[$key]["FAIfsType"] == "ext3") {
391 $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
392 $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
393 }
394 }
396 }
397 }
400 /* Check supplied data */
401 function check()
402 {
403 /* Call common method to give check the hook */
404 $message= plugin::check();
406 /* Check for an empty disk name */
407 if(empty($this->DISKcn)){
408 $message[] = _("Please specify a valid disc name.");
409 }
411 /* check every partition.
412 * if there is an invalid value defined, append an errorstr to message
413 */
415 /* Array that contain every partitionname mountpoint etc already assigned */
416 $alreadyUsed = array();
417 foreach($this->UsedAttrs as $attrs){
418 $alreadyUsed[$attrs] = array();
419 }
421 foreach($this->partitions as $key => $part){
423 /* Skip all checks, if preserve is set */
424 if($part['FAIpartitionFlags'] == "preserve"){
425 $this->partitions[$key]['FAIfsType'] = "preserve";
426 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
427 continue;
428 }
430 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
431 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
432 }
434 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
435 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
436 $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
437 }
438 }
439 if($part['FAIfsType'] == "swap"){
440 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
441 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
442 }
443 }
444 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
445 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
446 }
448 $tmp = split("-",$part['FAIpartitionSize']);
449 switch (count($tmp)){
450 case 0:
451 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
452 break;
453 case 1:
454 if (!tests::is_id(is_id($tmp[0])) &&(!empty($tmp[1]))){
455 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
456 }
457 break;
459 case 2:
460 if((!tests::is_id($tmp[0]))&&(!tests::is_id($tmp[1])) &&(!empty($tmp[1]))){
461 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
462 }elseif($tmp[0]>=$tmp[1]){
463 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
464 }
465 break;
467 default:
468 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
469 }
471 foreach($this->UsedAttrs as $attrs){
472 $alreadyUsed[$attrs][$key] = $part[$attrs];
473 }
474 }
476 $cnt = 0;
477 foreach($this->partitions as $key => $part){
478 if($part['FAIpartitionType'] == "primary"){
479 $cnt ++ ;
480 }
481 }
482 if($cnt > 3){
483 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
484 }
486 return ($message);
487 }
491 /* Return plugin informations for acl handling */
492 static function plInfo()
493 {
494 return (array(
495 "plShortName" => _("Partition table entry"),
496 "plDescription" => _("FAI partition table entry"),
497 "plSelfModify" => FALSE,
498 "plDepends" => array(),
499 "plPriority" => 27,
500 "plSection" => array("administration"),
501 "plCategory" => array("fai"),
502 "plProvidedAcls" => array(
503 "DISKcn" => _("Name"),
504 "DISKdescription" => _("Description"),
505 "FAIpartitionType" => _("Partition type"),
506 "FAIpartitionNr" => _("Partition no."),
507 "FAIfsType" => _("File system type"),
508 "FAImountPoint" => _("Mount point"),
509 "FAIpartitionSize" => _("Partition size"),
510 "FAImountOptions" => _("Mount options"),
511 "FAIfsOptions" => _("File system options"),
512 "FAIencrypted" => _("File system encryption"),
513 "FAIpartitionFlags" => _("Partition flags"))
514 ));
515 }
518 }
520 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
521 ?>