1 <?php
3 class faiPartitionTableEntry 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 faiPartitionTableEntry (&$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()){
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()){
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()){
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","reiser","dosfat16","winfat32");
184 /* Display Header */
185 $str = "";
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 (preg_match("/freeze/", $this->FAIstate)){
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 }
301 if(!empty($str)){
302 $str = "<table summary='' style='width:100%'>".$str."</table>";
303 }
304 return($str);
306 }
308 function save()
309 {
310 $tmp = array();
311 $tmp['cn'] = $this->DISKcn;
313 /* Attach partitions */
314 foreach($this->partitions as $key=>$val) {
315 $this->partitions[$key]['FAIpartitionNr']=$key;
316 }
318 /* Attach deleted */
319 foreach($this->deletePartitions as $key=>$val) {
320 $this->partitions[$key."-delete"]=$val;
321 $this->partitions[$key."-delete"]['status']="delete";
322 }
324 $tmp['description'] = $this->DISKdescription;
325 $tmp['partitions'] = $this->partitions;
326 $tmp['status'] = $this->status;
328 /* If hdd name has changed, tell partitionTable to rename it */
329 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
330 $tmp['rename']['from'] = $this->old_cn;
331 $tmp['rename']['to'] = $this->DISKcn;
332 }
333 return($tmp);
334 }
337 /* Save data to object */
338 function save_object()
339 {
340 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
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 }
356 /* Adapt ext3 to have -j option */
357 if ($this->partitions[$key]["FAIfsType"] == "ext3") {
358 $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
359 $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
360 }
361 }
363 }
364 }
367 /* Check supplied data */
368 function check()
369 {
370 /* Call common method to give check the hook */
371 $message= plugin::check();
373 /* Check for an empty disk name */
374 if(empty($this->DISKcn)){
375 $message[] = msgPool::required(_("Name"));
376 }
378 /* check every partition.
379 * if there is an invalid value defined, append an errorstr to message
380 */
382 /* Array that contain every partitionname mountpoint etc already assigned */
383 $alreadyUsed = array();
384 foreach($this->UsedAttrs as $attrs){
385 $alreadyUsed[$attrs] = array();
386 }
388 foreach($this->partitions as $key => $part){
390 /* Skip all checks, if preserve is set */
391 if($part['FAIpartitionFlags'] == "preserve"){
392 $this->partitions[$key]['FAIfsType'] = "preserve";
393 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
394 continue;
395 }
397 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
398 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
399 }
401 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
402 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
403 $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
404 }
405 }
406 if($part['FAIfsType'] == "swap"){
407 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
408 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
409 }
410 }
411 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
412 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
413 }
415 $tmp = split("-",$part['FAIpartitionSize']);
416 switch (count($tmp)){
417 case 0:
418 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
419 break;
420 case 1:
421 if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
422 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
423 }
424 break;
426 case 2:
427 if((!tests::is_id($tmp[0]))&&(!tests::is_id($tmp[1])) &&(!empty($tmp[1]))){
428 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
429 }elseif($tmp[0]>=$tmp[1]){
430 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
431 }
432 break;
434 default:
435 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
436 }
438 foreach($this->UsedAttrs as $attrs){
439 $alreadyUsed[$attrs][$key] = $part[$attrs];
440 }
441 }
443 $cnt = 0;
444 foreach($this->partitions as $key => $part){
445 if($part['FAIpartitionType'] == "primary"){
446 $cnt ++ ;
447 }
448 }
449 if($cnt > 3){
450 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
451 }
453 return ($message);
454 }
458 /* Return plugin informations for acl handling */
459 static function plInfo()
460 {
461 return (array(
462 "plShortName" => _("Partition table entry"),
463 "plDescription" => _("FAI partition table entry"),
464 "plSelfModify" => FALSE,
465 "plDepends" => array(),
466 "plPriority" => 27,
467 "plSection" => array("administration"),
468 "plCategory" => array("fai"),
469 "plProvidedAcls" => array(
470 "DISKcn" => _("Name"),
471 "DISKdescription" => _("Description"),
472 "FAIpartitionType" => _("Partition type"),
473 "FAIpartitionNr" => _("Partition no."),
474 "FAIfsType" => _("File system type"),
475 "FAImountPoint" => _("Mount point"),
476 "FAIpartitionSize" => _("Partition size"),
477 "FAImountOptions" => _("Mount options"),
478 "FAIfsOptions" => _("File system options"),
479 "FAIpartitionFlags" => _("Partition flags"))
480 ));
481 }
484 }
486 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
487 ?>