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() && !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 /* Fetch template and show the result
168 */
169 $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
170 return($display);
171 }
173 function generateParts()
174 {
175 /* Define Arrays with allowed syntax */
176 $PartitionTypes = array("primary"=>_("primary"),"logical"=>_("logical"));
177 $FAIfsTypes = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
179 /* Display Header */
180 $str = "";
181 if (count($this->partitions)){
182 $str .= "<tr>";
183 $str .= "<td><b>"._("Type")."</b></td>";
184 $str .= "<td><b>"._("FS type")."</b></td>";
185 $str .= "<td><b>"._("Mount point")."</b></td>";
186 $str .= "<td><b>"._("Size in MB")."</b></td>";
187 $str .= "<td><b>"._("Mount options")."</b></td>";
188 $str .= "<td><b>"._("FS option")."</b></td>";
189 $str .= "<td><b>"._("Preserve")."</b></td>";
190 $str .= "<td> </td>";
191 $str .= "</tr>";
192 }
194 /* Walk through all defined partitions.
195 * Create a new row for each partition and append it to
196 * the header defined above.
197 * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
198 */
199 foreach($this->partitions as $key => $part){
201 $dis = "";
202 if($part['FAIpartitionFlags'] == "preserve"){
203 $dis = " disabled ";
204 }
206 $disableALL = "";
207 if (preg_match("/freeze/", $this->FAIstate)){
208 $disableALL = " disabled ";
209 }
211 if($part['status']!="delete"){
213 /* Generate Partition select box
214 */
215 $PartitionType = "";
216 if($this->acl_is_writeable("FAIpartitionType")){
217 $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
218 foreach($PartitionTypes as $type => $PTname){
219 if($part['FAIpartitionType'] == $type){
220 $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
221 }else{
222 $PartitionType .="<option value='".$type."'>".$PTname."</option>";
223 }
224 }
225 $PartitionType.="</select>";
226 }elseif($this->acl_is_readable("FAIpartitionType")){
227 $PartitionType = $part['FAIpartitionType'];
228 }
231 /* Generate fsType select box
232 */
233 $FAIfsType= "";
234 if($this->acl_is_writeable("FAIfsType")){
235 $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
236 foreach($FAIfsTypes as $type){
237 if($part['FAIfsType'] == $type){
238 $FAIfsType .="<option value='".$type."' selected >".$type."</option>";
239 }else{
240 $FAIfsType .="<option value='".$type."'>".$type."</option>";
241 }
242 }
243 $FAIfsType.="</select>";
244 }elseif($this->acl_is_readable("FAIfsType")){
245 $FAIfsType = $part['FAIfsType'];
246 }
248 foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
249 $$attr = "";
250 if($this->acl_is_writeable($attr)){
251 $$attr = "<input name='".$attr."_".$key."' ".$disableALL." ".$dis."
252 value='".$part[$attr]."' id='".$attr."_".$key."'>";
254 }elseif($this->acl_is_readable($attr)){
255 $$attr = $part[$attr];
256 }
257 }
260 $str .= "\n<tr>";
261 $str .= "\n<td>".$PartitionType."</td>";
262 $str .= "\n<td>".$FAIfsType."</td>";
263 $str .= "\n<td>".$FAImountPoint."</td>";
264 $str .= "\n<td>".$FAIpartitionSize."</td>";
265 $str .= "\n<td>".$FAImountOptions."</td>";
266 $str .= "\n<td>".$FAIfsOptions."</td>";
268 $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
269 "changeState('FAIpartitionSize_".$key."') ; ".
270 "changeState('FAImountOptions_".$key."') ; ".
271 "changeState('FAIfsType_".$key."') ; ".
272 "changeState('FAIfsOptions_".$key."') ; \"";
274 if($this->acl_is_writeable("FAIpartitionFlags")){
275 if($part['FAIpartitionFlags']!=false){
276 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
277 }else{
278 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
279 }
280 }else{
281 if($part['FAIpartitionFlags']!=false){
282 $str .= "<td>"._("False")."</td>";
283 }else{
284 $str .= "<td>"._("True")."</td>";
285 }
286 }
287 if($this->acl_is_removeable()){
288 $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
289 }else{
290 $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";
291 }
292 $str .= "\n</tr>";
293 }
294 }
296 if(!empty($str)){
297 $str = "<table summary='' style='width:100%'>".$str."</table>";
298 }
299 return($str);
301 }
303 function save()
304 {
305 $tmp = array();
306 $tmp['cn'] = $this->DISKcn;
308 /* Attach partitions */
309 foreach($this->partitions as $key=>$val) {
310 $this->partitions[$key]['FAIpartitionNr']=$key;
311 }
313 /* Attach deleted */
314 foreach($this->deletePartitions as $key=>$val) {
315 $this->partitions[$key."-delete"]=$val;
316 $this->partitions[$key."-delete"]['status']="delete";
317 }
319 $tmp['description'] = $this->DISKdescription;
320 $tmp['partitions'] = $this->partitions;
321 $tmp['status'] = $this->status;
323 /* If hdd name has changed, tell partitionTable to rename it */
324 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
325 $tmp['rename']['from'] = $this->old_cn;
326 $tmp['rename']['to'] = $this->DISKcn;
327 }
328 return($tmp);
329 }
332 /* Save data to object */
333 function save_object()
334 {
335 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
336 plugin::save_object();
338 foreach($this->partitions as $key => $part){
339 foreach($this->UsedAttrs as $attrs){
341 if($this->acl_is_writeable($attrs)){
343 if(isset($_POST[$attrs."_".$key])){
344 $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
345 }else{
346 $this->partitions[$key][$attrs] = false;
347 }
348 }
349 }
351 /* Adapt ext3 to have -j option */
352 if ($this->partitions[$key]["FAIfsType"] == "ext3") {
353 $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
354 $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
355 }
356 }
358 }
359 }
362 /* Check supplied data */
363 function check()
364 {
365 /* Call common method to give check the hook */
366 $message= plugin::check();
368 /* Check for an empty disk name */
369 $d = trim($this->DISKcn);
370 if($d == "" ){
371 $message[] = msgPool::required(_("Name"));
372 }
373 if(preg_match("/[^a-z0-9_\-]/i",$d)){
374 $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
375 }
377 /* check every partition.
378 * if there is an invalid value defined, append an errorstr to message
379 */
381 /* Array that contain every partitionname mountpoint etc already assigned */
382 $alreadyUsed = array();
383 foreach($this->UsedAttrs as $attrs){
384 $alreadyUsed[$attrs] = array();
385 }
387 foreach($this->partitions as $key => $part){
389 /* Skip all checks, if preserve is set */
390 if($part['FAIpartitionFlags'] == "preserve"){
391 $this->partitions[$key]['FAIfsType'] = "preserve";
392 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
393 continue;
394 }
396 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
397 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
398 }
400 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
401 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
402 $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
403 }
404 }
405 if($part['FAIfsType'] == "swap"){
406 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
407 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
408 }
409 }
410 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
411 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
412 }
414 $tmp = split("-",$part['FAIpartitionSize']);
415 switch (count($tmp)){
416 case 0:
417 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
418 break;
419 case 1:
420 if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
421 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
422 }
423 break;
425 case 2:
426 if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
427 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
428 }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
429 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
430 }
431 break;
433 default:
434 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
435 }
437 foreach($this->UsedAttrs as $attrs){
438 $alreadyUsed[$attrs][$key] = $part[$attrs];
439 }
440 }
442 $cnt = 0;
443 foreach($this->partitions as $key => $part){
444 if($part['FAIpartitionType'] == "primary"){
445 $cnt ++ ;
446 }
447 }
448 if($cnt > 3){
449 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
450 }
452 return ($message);
453 }
457 /* Return plugin informations for acl handling */
458 static function plInfo()
459 {
460 return (array(
461 "plShortName" => _("Partition table entry"),
462 "plDescription" => _("FAI partition table entry"),
463 "plSelfModify" => FALSE,
464 "plDepends" => array(),
465 "plPriority" => 27,
466 "plSection" => array("administration"),
467 "plCategory" => array("fai"),
468 "plProvidedAcls" => array(
469 "DISKcn" => _("Name"),
470 "DISKdescription" => _("Description"),
471 "FAIpartitionType" => _("Partition type"),
472 "FAIpartitionNr" => _("Partition no."),
473 "FAIfsType" => _("File system type"),
474 "FAImountPoint" => _("Mount point"),
475 "FAIpartitionSize" => _("Partition size"),
476 "FAImountOptions" => _("Mount options"),
477 "FAIfsOptions" => _("File system options"),
478 "FAIpartitionFlags" => _("Partition flags"))
479 ));
480 }
483 }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>