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[$s_entry]= $this->partitions[$s_entry];
127 $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
128 unset($this->partitions[$s_entry]);
129 }else{
130 unset($this->partitions[$s_entry]);
131 }
132 $tmp= array();
133 foreach($this->partitions as $part){
134 $tmp[count($tmp)+1]=$part;
135 }
136 $this->partitions = $tmp;
137 }
139 /* To add a partitions we only append an empty
140 * array to the already defined partitions.
141 */
142 if(isset($_POST['AddPartition']) && $this->acl_is_createable()){
143 foreach($this->UsedAttrs as $attr){
144 $tmp[$attr] = "";
145 }
146 $tmp["old_cn"] = "";
147 $tmp['status']="new";
148 $this->partitions[count($this->partitions)+1]=$tmp;
149 }
151 /* $setup contains a table with the partitions.
152 */
153 $smarty->assign("setup", $this->generateParts());
154 foreach($this->attributes as $attr){
155 $smarty->assign($attr,$this->$attr);
156 }
158 $tmp = $this->plInfo();
159 $sacl = "";
160 foreach($tmp['plProvidedAcls'] as $name => $translated){
162 $acl = $this->getacl($name);
163 $sacl .= $acl;
164 if($this->FAIstate == "freezed"){
165 $acl = preg_replace("/w/","",$acl);
166 }
167 $smarty->assign($name."ACL",$acl);
168 }
169 $smarty->assign("allowSave",$sacl);
171 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
173 /* Fetch template and show the result
174 */
175 $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.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 = "<table summary='' style='width:100%'>";
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($this->FAIstate =="freeze"){
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 }
301 $str.="</table>";
302 return($str);
304 }
306 function save()
307 {
308 $tmp = array();
309 $tmp['cn'] = $this->DISKcn;
311 /* Attach partitions */
312 foreach($this->partitions as $key=>$val) {
313 $this->partitions[$key]['FAIpartitionNr']=$key;
314 }
316 /* Attach deleted */
317 foreach($this->deletePartitions as $key=>$val) {
318 $this->partitions[$key."-delete"]=$val;
319 $this->partitions[$key."-delete"]['status']="delete";
320 }
322 $tmp['description'] = $this->DISKdescription;
323 $tmp['partitions'] = $this->partitions;
324 $tmp['status'] = $this->status;
326 /* If hdd name has changed, tell partitionTable to rename it */
327 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
328 $tmp['rename']['from'] = $this->old_cn;
329 $tmp['rename']['to'] = $this->DISKcn;
330 }
331 return($tmp);
332 }
335 /* Save data to object */
336 function save_object()
337 {
340 if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
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 ?>