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 }
72 }
73 }
75 /* Set default attributes */
76 $this->DISKcn = $disk['cn'];
77 $this->DISKdescription = $disk['description'];
78 $this->partitions = $disk['partitions'];
79 $this->is_edit = true;
80 $this->old_cn = $disk['cn'];
81 }
82 }
85 function execute()
86 {
87 /* Call parent execute */
88 plugin::execute();
90 /* Fill templating stuff */
91 $smarty = get_smarty();
92 $s_action = "";
93 $s_entry = "";
94 $display = "";
96 /* Assign values
97 * And Checkbox selection
98 */
99 foreach($this->attributes as $attrs){
100 $smarty->assign($attrs,$this->$attrs);
101 if($this->$attrs){
102 $smarty->assign($attrs."CHK"," ");
103 }else{
104 $smarty->assign($attrs."CHK"," disabled ");
105 }
106 }
108 /* Check all Posts if there is something usefull for us,
109 * For example : Delete is posted as Delete_1
110 * The number specifies the index we want to delete
111 */
112 foreach($_POST as $name => $value){
113 if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable()){
114 $tmp = split("_",$name);
115 $s_action = "remove";
116 $s_entry = $tmp[1];
117 }
118 }
120 /* To remove a partition we unset the index posted.
121 * We must sort the index again, else we possibly got problems
122 * with partitions order.
123 */
124 if($s_action == "remove" && $this->acl_is_removeable()){
125 if($this->partitions[$s_entry]['status'] == "edited"){
126 $this->deletePartitions[]= $this->partitions[$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()){
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){
161 $acl = $this->getacl($name);
162 $sacl .= $acl;
163 if($this->FAIstate == "freezed"){
164 $acl = preg_replace("/w/","",$acl);
165 }
166 $smarty->assign($name."ACL",$acl);
167 }
168 $smarty->assign("allowSave",$sacl);
170 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
172 /* Fetch template and show the result
173 */
174 $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
175 return($display);
176 }
178 function generateParts()
179 {
180 /* Define Arrays with allowed syntax */
181 $PartitionTypes = array("primary"=>_("primary"),"logical"=>_("logical"));
182 $FAIfsTypes = array("ext2","ext3","xfs","swap","reiserfs","dosfat16","winfat32");
184 /* Display Header */
185 $str = "<table summary='' style='width:100%'>";
186 if (count($this->partitions)){
187 $str .= "<tr>";
188 $str .= "<td><b>"._("Type")."</b></td>";
189 $str .= "<td><b>"._("FS type")."</b></td>";
190 $str .= "<td><b>"._("Mount point")."</b></td>";
191 $str .= "<td><b>"._("Size in MB")."</b></td>";
192 $str .= "<td><b>"._("Mount options")."</b></td>";
193 $str .= "<td><b>"._("FS option")."</b></td>";
194 $str .= "<td><b>"._("Preserve")."</b></td>";
195 $str .= "<td> </td>";
196 $str .= "</tr>";
197 }
199 /* Walk through all defined partitions.
200 * Create a new row for each partition and append it to
201 * the header defined above.
202 * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
203 */
204 foreach($this->partitions as $key => $part){
206 $dis = "";
207 if($part['FAIpartitionFlags'] == "preserve"){
208 $dis = " disabled ";
209 }
211 $disableALL = "";
212 if($this->FAIstate =="freeze"){
213 $disableALL = " disabled ";
214 }
216 if($part['status']!="delete"){
218 /* Generate Partition select box
219 */
220 $PartitionType = "";
221 if($this->acl_is_writeable("FAIpartitionType")){
222 $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
223 foreach($PartitionTypes as $type => $PTname){
224 if($part['FAIpartitionType'] == $type){
225 $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
226 }else{
227 $PartitionType .="<option value='".$type."'>".$PTname."</option>";
228 }
229 }
230 $PartitionType.="</select>";
231 }elseif($this->acl_is_readable("FAIpartitionType")){
232 $PartitionType = $part['FAIpartitionType'];
233 }
236 /* Generate fsType select box
237 */
238 $FAIfsType= "";
239 if($this->acl_is_writeable("FAIfsType")){
240 $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
241 foreach($FAIfsTypes as $type){
242 if($part['FAIfsType'] == $type){
243 $FAIfsType .="<option value='".$type."' selected >".$type."</option>";
244 }else{
245 $FAIfsType .="<option value='".$type."'>".$type."</option>";
246 }
247 }
248 $FAIfsType.="</select>";
249 }elseif($this->acl_is_readable("FAIfsType")){
250 $FAIfsType = $part['FAIfsType'];
251 }
253 foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
254 $$attr = "";
255 if($this->acl_is_writeable($attr)){
256 $$attr = "<input name='".$attr."_".$key."' ".$disableALL." ".$dis."
257 value='".$part[$attr]."' id='".$attr."_".$key."'>";
259 }elseif($this->acl_is_readable($attr)){
260 $$attr = $part[$attr];
261 }
262 }
265 $str .= "\n<tr>";
266 $str .= "\n<td>".$PartitionType."</td>";
267 $str .= "\n<td>".$FAIfsType."</td>";
268 $str .= "\n<td>".$FAImountPoint."</td>";
269 $str .= "\n<td>".$FAIpartitionSize."</td>";
270 $str .= "\n<td>".$FAImountOptions."</td>";
271 $str .= "\n<td>".$FAIfsOptions."</td>";
273 $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
274 "changeState('FAIpartitionSize_".$key."') ; ".
275 "changeState('FAImountOptions_".$key."') ; ".
276 "changeState('FAIfsType_".$key."') ; ".
277 "changeState('FAIfsOptions_".$key."') ; \"";
279 if($this->acl_is_writeable("FAIpartitionFlags")){
280 if($part['FAIpartitionFlags']!=false){
281 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
282 }else{
283 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
284 }
285 }else{
286 if($part['FAIpartitionFlags']!=false){
287 $str .= "<td>"._("False")."</td>";
288 }else{
289 $str .= "<td>"._("True")."</td>";
290 }
291 }
292 if($this->acl_is_removeable()){
293 $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
294 }else{
295 $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";
296 }
297 $str .= "\n</tr>";
298 }
299 }
300 $str.="</table>";
301 return($str);
303 }
305 function save()
306 {
307 $tmp = array();
308 $tmp['cn'] = $this->DISKcn;
310 /* Attach partitions */
311 foreach($this->partitions as $key=>$val) {
312 $this->partitions[$key]['FAIpartitionNr']=$key;
313 }
315 /* Attach deleted */
316 foreach($this->deletePartitions as $key=>$val) {
317 $this->partitions[$key."-delete"]=$val;
318 $this->partitions[$key."-delete"]['status']="delete";
319 }
321 $tmp['description'] = $this->DISKdescription;
322 $tmp['partitions'] = $this->partitions;
323 $tmp['status'] = $this->status;
325 /* If hdd name has changed, tell partitionTable to rename it */
326 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
327 $tmp['rename']['from'] = $this->old_cn;
328 $tmp['rename']['to'] = $this->DISKcn;
329 }
330 return($tmp);
331 }
334 /* Save data to object */
335 function save_object()
336 {
339 if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
340 print_a($_POST);
341 plugin::save_object();
343 foreach($this->partitions as $key => $part){
344 foreach($this->UsedAttrs as $attrs){
346 if($this->acl_is_writeable($attrs)){
348 if(isset($_POST[$attrs."_".$key])){
349 $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
350 }else{
351 $this->partitions[$key][$attrs] = false;
352 }
353 }
354 }
355 }
356 }
357 }
360 /* Check supplied data */
361 function check()
362 {
363 /* Call common method to give check the hook */
364 $message= plugin::check();
366 /* Check for an empty disk name */
367 if(empty($this->DISKcn)){
368 $message[] = _("Please specify a valid disc name.");
369 }
371 /* check every partition.
372 * if there is an invalid value defined, append an errorstr to message
373 */
375 /* Array that contain every partitionname mountpoint etc already assigned */
376 $alreadyUsed = array();
377 foreach($this->UsedAttrs as $attrs){
378 $alreadyUsed[$attrs] = array();
379 }
381 foreach($this->partitions as $key => $part){
383 /* Skip all checks, if preserve is set */
384 if($part['FAIpartitionFlags'] == "preserve"){
385 $this->partitions[$key]['FAIfsType'] = "preserve";
386 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
387 continue;
388 }
390 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
391 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
392 }
394 if($part['FAIfsType']!="swap"){
395 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
396 $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
397 }
398 }
399 if($part['FAIfsType'] == "swap"){
400 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
401 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
402 }
403 }
404 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
405 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
406 }
408 $tmp = split("-",$part['FAIpartitionSize']);
409 switch (count($tmp)){
410 case 0:
411 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
412 break;
413 case 1:
414 if (!is_id(is_id($tmp[0]))){
415 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
416 }
417 break;
419 case 2:
420 if((!is_id($tmp[0]))||(!is_id($tmp[1]))){
421 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
422 }elseif($tmp[0]>=$tmp[1]){
423 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
424 }
425 break;
427 default:
428 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
429 }
431 foreach($this->UsedAttrs as $attrs){
432 $alreadyUsed[$attrs][$key] = $part[$attrs];
433 }
434 }
436 $cnt = 0;
437 foreach($this->partitions as $key => $part){
438 if($part['FAIpartitionType'] == "primary"){
439 $cnt ++ ;
440 }
441 }
442 if($cnt > 3){
443 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
444 }
446 return ($message);
447 }
451 /* Return plugin informations for acl handling */
452 function plInfo()
453 {
454 return (array(
455 "plShortName" => _("Partition table entry"),
456 "plDescription" => _("FAI partition table entry"),
457 "plSelfModify" => FALSE,
458 "plDepends" => array(),
459 "plPriority" => 27,
460 "plSection" => array("administration"),
461 "plCategory" => array("fai"),
462 "plProvidedAcls" => array(
463 "DISKcn" => _("Name"),
464 "DISKdescription" => _("Description"),
465 "FAIpartitionType" => _("Partition type"),
466 "FAIpartitionNr" => _("Partition no."),
467 "FAIfsType" => _("File system type"),
468 "FAImountPoint" => _("Mount point"),
469 "FAIpartitionSize" => _("Partition size"),
470 "FAImountOptions" => _("Mount options"),
471 "FAIfsOptions" => _("File system options"),
472 "FAIpartitionFlags" => _("Partition flags"))
473 ));
474 }
477 }
479 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
480 ?>