1 <?php
3 class faiDiskEntry 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 = "";
12 var $DISKFAIdiskOptions = "";
14 var $partitions = array();
15 var $UsedAttrs = array();
16 var $is_edit = false;
17 var $old_cn = "";
18 var $status = "new";
19 var $deletePartitions = array();
20 var $fstabkey = "device";
21 var $disklabel = "msdos";
23 var $FAIstate = "";
24 var $FAIdiskType = "disk";
26 function faiDiskEntry (&$config, $dn= NULL,$parent,$disk,$type)
27 {
28 plugin::plugin ($config, $dn);
30 $this->parent = $parent;
32 $this->UsedAttrs = array("cn","description","FAIpartitionType",
33 "FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
34 "FAImountOptions","FAIfsOptions","FAIpartitionFlags","FAIdiskOption");
36 /* Set disk type */
37 $this->FAIdiskType = $type;
39 /* Default status is new */
40 $this->status = "new";
42 /* We want to edit an entry */
43 if($disk){
45 /* Set disk status */
46 $this->status = $disk['status'];
48 /* Walk through partitions */
49 foreach($disk['partitions'] as $name => $values){
51 /* If a partition is already marked as delete, attach it to deletePartitions only. */
52 if($values['status'] == "delete"){
53 unset($disk['partitions'][$name]);
54 $this->deletePartitions[]=$values;
55 }else{
57 /* Set status, to know which partition must be deleted from ldap
58 new : Neu partition entry // save
59 edited : Update partition entry in ldap
60 deleted: Remove partition from ldap
61 */
63 /* If status is not new, set to edit mode.
64 * New means that this partition currently wasn't saved to ldap.
65 */
66 if($disk['partitions'][$name]['status']!="new"){
67 $disk['partitions'][$name]['status']="edited";
68 }
70 $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
71 $disk['partitions'][$name]['FAIdiskType']= $this->FAIdiskType;
73 /* Assign empty attributes, if attribute is missing */
74 foreach($this->UsedAttrs as $attr){
75 if(!isset($values[$attr])){
76 $disk['partitions'][$name][$attr]="";
77 }
78 }
80 if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
81 $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
82 }
83 }
84 }
86 /* Set default attributes */
87 $this->DISKcn = $disk['cn'];
88 $this->DISKdescription = $disk['description'];
89 $this->partitions = $disk['partitions'];
90 $this->is_edit = true;
91 $this->old_cn = $disk['cn'];
93 /* Move faiDiskOption */
94 if (isset($disk['FAIdiskOption'])){
95 foreach($disk['FAIdiskOption'] as $option) {
96 if (preg_match("/fstabkey:(device|label|uuid)/", $option)){
97 $this->fstabkey= preg_replace("/^.*fstabkey:(device|label|uuid).*$/", "$1", $option);
98 continue;
99 }
100 if (preg_match("/disklabel:(msdos|gpt)/", $option)){
101 $this->disklabel= preg_replace("/^.*disklabel:(msdos|gpt).*$/", "$1", $option);
102 continue;
103 }
104 if (preg_match("/^bootable:/", $option)){
105 $bootable = split(",", trim(preg_replace("/^bootable:/","",$option),","));
106 foreach($bootable as $bootflag){
107 $this->partitions[$bootflag]['bootable'] = TRUE;
108 }
109 break;
110 }
111 }
112 } else {
113 $this->fstabkey= "device";
114 }
115 }
116 }
119 function execute()
120 {
121 /* Call parent execute */
122 plugin::execute();
124 /* Fill templating stuff */
125 $smarty = get_smarty();
126 $s_action = "";
127 $s_entry = "";
128 $display = "";
130 /* Load parameters */
131 if (isset($_POST['disklabel']) && preg_match("/^(msdos|gpt)$/", $_POST['disklabel'])){
132 $this->disklabel= $_POST['disklabel'];
133 }
134 if (isset($_POST['fstabkey']) && preg_match("/^(device|label|uuid)$/", $_POST['fstabkey'])){
135 $this->fstabkey= $_POST['fstabkey'];
136 }
138 /* Assign values
139 * And Checkbox selection
140 */
141 foreach($this->attributes as $attrs){
142 $smarty->assign($attrs,$this->$attrs);
143 if($this->$attrs){
144 $smarty->assign($attrs."CHK"," ");
145 }else{
146 $smarty->assign($attrs."CHK"," disabled ");
147 }
148 }
150 /* Check all Posts if there is something usefull for us,
151 * For example : Delete is posted as Delete_1
152 * The number specifies the index we want to delete
153 */
154 foreach($_POST as $name => $value){
155 if((preg_match("/Delete_.*/",$name)) &&
156 $this->acl_is_removeable() &&
157 !preg_match("/freeze/i",$this->FAIstate)){
158 $tmp = split("_",$name);
159 $s_action = "remove";
160 $s_entry = $tmp[1];
161 }
162 }
164 /* To remove a partition we unset the index posted.
165 * We must sort the index again, else we possibly got problems
166 * with partitions order.
167 */
168 if($s_action == "remove" && $this->acl_is_removeable() &&
169 !preg_match("/freeze/i",$this->FAIstate)){
170 if($this->partitions[$s_entry]['status'] == "edited"){
171 $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
172 $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
173 unset($this->partitions[$s_entry]);
174 }else{
175 unset($this->partitions[$s_entry]);
176 }
177 $tmp= array();
178 foreach($this->partitions as $part){
179 $tmp[count($tmp)+1]=$part;
180 }
181 $this->partitions = $tmp;
182 }
185 /* Add / Edit partitions here.
186 */
187 foreach($_POST as $name => $value){
188 if(preg_match("/^EditPartition_/",$name)){
189 $id = preg_replace("/^EditPartition_/","",$name);
190 $id = preg_replace("/_.*$/","",$id);
191 if(isset($this->partitions[$id])){
192 $this->dialog = new faiPartition($this->config,$this->partitions[$id], $this,$this->FAIdiskType);
193 break;
194 }
195 }
196 }
197 if(isset($_POST['AddPartition']) && !preg_match("/freeze/i",$this->FAIstate)){
198 $this->dialog = new faiPartition($this->config, array(), $this,$this->FAIdiskType);
199 }
200 if($this->dialog instanceOf plugin && isset($_POST['PartitionCancel'])){
201 $this->dialog = null;
202 }
203 if($this->dialog instanceOf plugin && isset($_POST['PartitionSave'])){
204 $this->dialog->save_object();
205 $attrs = $this->dialog->save();
206 if(isset($attrs['FAIpartitionNr'])){
207 $this->partitions[$attrs['FAIpartitionNr']] = $attrs;
208 }else{
209 $this->partitions[] = $attrs;
210 }
211 $this->dialog = null;
212 }
213 if($this->dialog instanceOf plugin){
214 $this->dialog->save_object();
215 return($this->dialog->execute());
216 }
219 /* $setup contains a table with the partitions.
220 */
221 $smarty->assign("setup", $this->generateParts());
222 foreach($this->attributes as $attr){
223 $smarty->assign($attr,$this->$attr);
224 }
226 $tmp = $this->plInfo();
227 $sacl = "";
228 foreach($tmp['plProvidedAcls'] as $name => $translated){
229 $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
230 $smarty->assign($name."ACL",$acl);
231 }
233 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
234 $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
236 // Fill boxes
237 $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
238 $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
239 $smarty->assign("fstabkey", $this->fstabkey);
240 $smarty->assign("disklabel", $this->disklabel);
242 /* Fetch template and show the result
243 */
244 $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
245 return($display);
246 }
248 function generateParts()
249 {
251 $divlist = new divSelectBox("DiskEntries");
252 foreach($this->partitions as $key => $part){
253 $number =array(
254 "string" => $part['FAIpartitionNr'],
255 "attach" => "style='width:20px;'");
256 $size =array(
257 "string" => $part['FAIpartitionSize'],
258 "attach" => "style='width:100px;'");
259 $fstype =array(
260 "string" => $part['FAIfsType'],
261 "attach" => "style='width:60px;'");
262 $type =array(
263 "string" => $part['FAIpartitionType'],
264 "attach" => "style='width:80px;'");
265 $opt =array(
266 "string" => $part['FAImountOptions'],
267 "attach" => "style='width:80px;'");
268 $fsopt =array(
269 "string" => $part['FAIfsOptions'],
270 "attach" => "style='width:80px;'");
271 $flags =array(
272 "string" => $part['FAIpartitionFlags'],
273 "attach" => "style='width:80px;'");
274 $mntp =array("string" => $part['FAImountPoint']);
277 $action =array(
278 "string" => "<input type='image' src='images/lists/edit.png' name='EditPartition_".$key."'>",
279 "attach" => "style='width:40px;'");
282 $fields = array($number,$type,$mntp,$size,$fstype, $opt,$fsopt,$flags,$action);
283 $divlist->AddEntry($fields);
284 }
285 return($divlist->DrawList());
286 }
288 function save()
289 {
290 $tmp = array();
291 $tmp['cn'] = $this->DISKcn;
293 /* Attach partitions */
294 foreach($this->partitions as $key=>$val) {
295 $this->partitions[$key]['FAIpartitionNr']=$key;
296 }
298 /* Attach deleted */
299 foreach($this->deletePartitions as $key=>$val) {
300 $this->partitions[$key."-delete"]=$val;
301 $this->partitions[$key."-delete"]['status']="delete";
302 }
304 $tmp['description'] = $this->DISKdescription;
305 $tmp['partitions'] = $this->partitions;
306 $tmp['status'] = $this->status;
307 $tmp['FAIdiskType'] = $this->FAIdiskType;
309 /* Assemble flags */
310 $tmp['FAIdiskOption'] = array("fstabkey:".$this->fstabkey, "disklabel:".$this->disklabel);
312 /* If hdd name has changed, tell partitionTable to rename it */
313 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
314 $tmp['rename']['from'] = $this->old_cn;
315 $tmp['rename']['to'] = $this->DISKcn;
316 }
317 return($tmp);
318 }
321 /* Save data to object */
322 function save_object()
323 {
324 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
325 plugin::save_object();
326 }
327 }
330 /* Check supplied data */
331 function check()
332 {
333 /* Call common method to give check the hook */
334 $message= plugin::check();
336 /* Check for an empty disk name */
337 $d = trim($this->DISKcn);
338 if($d == "" ){
339 $message[] = msgPool::required(_("Name"));
340 }
341 if(preg_match("/[^a-z0-9_\-]/i",$d)){
342 $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
343 }
345 /* check every partition.
346 * if there is an invalid value defined, append an errorstr to message
347 */
349 /* Array that contain every partitionname mountpoint etc already assigned */
350 $alreadyUsed = array();
351 foreach($this->UsedAttrs as $attrs){
352 $alreadyUsed[$attrs] = array();
353 }
355 foreach($this->partitions as $key => $part){
357 /* Skip all checks, if preserve is set */
358 if($part['FAIpartitionFlags'] == "preserve"){
359 $this->partitions[$key]['FAIfsType'] = "preserve";
360 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
361 continue;
362 }
364 if($part['FAImountPoint'] != "-" && (in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
365 $message[]=sprintf(_("Please enter a unique mount point for partition %s"),($key));
366 }
368 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
369 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
370 $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
371 }
372 }
373 if($part['FAIfsType'] == "swap"){
374 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
375 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
376 }
377 }
378 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
379 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
380 }
382 $tmp = split("-",$part['FAIpartitionSize']);
383 switch (count($tmp)){
384 case 0:
385 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
386 break;
387 case 1:
388 if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
389 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
390 }
391 break;
393 case 2:
394 if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
395 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
396 }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
397 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
398 }
399 break;
401 default:
402 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
403 }
405 foreach($this->UsedAttrs as $attrs){
406 $alreadyUsed[$attrs][$key] = $part[$attrs];
407 }
408 }
410 $cnt = 0;
411 foreach($this->partitions as $key => $part){
412 if($part['FAIpartitionType'] == "primary"){
413 $cnt ++ ;
414 }
415 }
416 if($cnt > 3){
417 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
418 }
420 return ($message);
421 }
425 /* Return plugin informations for acl handling */
426 static function plInfo()
427 {
428 return (array(
429 "plShortName" => _("Partition table entry"),
430 "plDescription" => _("FAI partition table entry"),
431 "plSelfModify" => FALSE,
432 "plDepends" => array(),
433 "plPriority" => 27,
434 "plSection" => array("administration"),
435 "plCategory" => array("fai"),
436 "plProvidedAcls" => array(
437 "DISKcn" => _("Name"),
438 "DISKdescription" => _("Description"),
439 "DISKFAIdiskOption" => _("Disk options"),
440 "FAIpartitionType" => _("Partition type"),
441 "FAIpartitionNr" => _("Partition no."),
442 "FAIfsType" => _("File system type"),
443 "FAImountPoint" => _("Mount point"),
444 "FAIpartitionSize" => _("Partition size"),
445 "FAImountOptions" => _("Mount options"),
446 "FAIfsOptions" => _("File system options"),
447 "FAIpartitionFlags" => _("Partition flags"))
448 ));
449 }
452 }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>