1 <?php
3 class faiPartitionTableEntry extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account= TRUE;
7 var $attributes = array("DISK_cn","DISK_description");
8 var $objectclasses= array();
10 var $DISK_cn = "";
11 var $DISK_description = "";
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 }
67 }
68 }
70 /* Set default attributes */
71 $this->DISK_cn = $disk['cn'];
72 $this->DISK_description = $disk['description'];
73 $this->partitions = $disk['partitions'];
74 $this->is_edit = true;
75 $this->old_cn = $disk['cn'];
76 }
77 }
79 function execute()
80 {
81 /* Call parent execute */
82 plugin::execute();
84 /* Fill templating stuff */
85 $smarty = get_smarty();
86 $s_action = "";
87 $s_entry = "";
88 $display = "";
90 /* Assign values
91 * And Checkbox selection
92 */
93 foreach($this->attributes as $attrs){
94 $smarty->assign($attrs,$this->$attrs);
95 if($this->$attrs){
96 $smarty->assign($attrs."CHK"," ");
97 }else{
98 $smarty->assign($attrs."CHK"," disabled ");
99 }
100 }
102 /* Check all Posts if there is something usefull for us,
103 * For example : Delete is posted as Delete_1
104 * The number specifies the index we want to delete
105 */
106 foreach($_POST as $name => $value){
107 if(preg_match("/Delete_.*/",$name)){
108 $tmp = split("_",$name);
109 $s_action = "remove";
110 $s_entry = $tmp[1];
111 }
112 }
114 /* To remove a partition we unset the index posted.
115 * We must sort the index again, else we possibly got problems
116 * with partitions order.
117 */
118 if($s_action == "remove"){
119 if($this->partitions[$s_entry]['status'] == "edited"){
120 $this->deletePartitions[] = $this->partitions[$s_entry];
121 unset($this->partitions[$s_entry]);
122 }else{
123 unset($this->partitions[$s_entry]);
124 }
125 $tmp= array();
126 foreach($this->partitions as $part){
127 $tmp[count($tmp)+1]=$part;
128 }
129 $this->partitions = $tmp;
130 }
132 /* To add a partitions we only append an empty
133 * array to the already defined partitions.
134 */
135 if(isset($_POST['AddPartition'])){
137 /* FAIpartitionNr is used to indentify an entry.
138 (The index of an entry may vary)
139 Walk through the given entries and check for a free FAIpartitionNr. */
140 $ids = array();
141 foreach($this->partitions as $part){
142 $ids[$part['FAIpartitionNr']] = $part['FAIpartitionNr'];
143 }
144 $new_nr = 1 ;
145 while(isset($ids[$new_nr]) && $new_nr < 100){
146 $new_nr ++ ;
147 }
149 /* Add empty attributes */
150 foreach($this->UsedAttrs as $attr){
151 $tmp[$attr] = "";
152 }
153 $tmp["old_cn"] = "";
154 $tmp['status'] ="new";
155 $tmp['FAIpartitionNr'] = $new_nr;
157 if(empty($this->partitions)){
158 $this->partitions[1]=$tmp;
159 }else{
160 $this->partitions[]=$tmp;
161 }
162 }
164 /* $setup contains a table with the partitions.
165 */
166 $smarty->assign("setup", $this->generateParts());
167 foreach($this->attributes as $attr){
168 $smarty->assign($attr,$this->$attr);
169 }
171 foreach($this->UsedAttrs as $attr){
172 if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
173 $smarty->assign($attr."ACL"," disabled ");
174 }else{
175 $smarty->assign($attr."ACL"," ");
176 }
177 }
180 /* The user can't change a diskname
181 * if we are currently in edit mode for the specified disk
182 */
183 // if($this->is_edit){
184 // $smarty->assign("DISK_cnACL"," disabled ");
185 // }else{
186 $smarty->assign("DISK_cnACL","");
187 // }
189 /* Fetch template and show the result
190 */
191 $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
192 return($display);
193 }
195 function generateParts()
196 {
197 /* Define Arrays with allowed syntax */
198 $PartitionTypes = array("primary"=>_("primary"),"logical"=>_("logical"));
199 $FAIfsTypes = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
201 /* Display Header */
202 $str = "<table summary='' style='width:100%'>";
203 if (count($this->partitions)){
204 $str .= "<tr>";
205 $str .= "<td>"._("Type")."</td>";
206 $str .= "<td>"._("FS type")."</td>";
207 $str .= "<td>"._("Mount point")."</td>";
208 $str .= "<td>"._("Size in MB")."</td>";
209 $str .= "<td>"._("Mount options")."</td>";
210 $str .= "<td>"._("FS option")."</td>";
211 $str .= "<td>"._("Preserve")."</td>";
212 $str .= "<td> </td>";
213 $str .= "</tr>";
214 }
216 /* Walk through all defined partitions.
217 * Create a new row for each partition and append it to
218 * the header defined above.
219 * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
220 */
221 foreach($this->partitions as $key => $part){
223 $dis = "";
224 if($part['FAIpartitionFlags'] == "preserve"){
225 $dis = " disabled ";
226 }
228 $disableALL = "";
229 if($this->FAIstate =="freeze"){
230 $disableALL = " disabled ";
231 }
233 if($part['status']!="delete"){
234 /* Generate Partition select box
235 */
236 $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
237 foreach($PartitionTypes as $type => $PTname){
238 if($part['FAIpartitionType'] == $type){
239 $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
240 }else{
241 $PartitionType .="<option value='".$type."'>".$PTname."</option>";
242 }
243 }
244 $PartitionType.="</select>";
247 /* Generate fsType select box
248 */
249 $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
250 foreach($FAIfsTypes as $type){
251 if($part['FAIfsType'] == $type){
252 $FAIfsType .="<option value='".$type."' selected >".$type."</option>";
253 }else{
254 $FAIfsType .="<option value='".$type."'>".$type."</option>";
255 }
256 }
257 $FAIfsType.="</select>";
259 $str .= "\n<tr>";
260 $str .= "\n<td>".$PartitionType."</td>";
261 $str .= "\n<td>".$FAIfsType."</td>";
262 $str .= "\n<td><input name='FAImountPoint_".$key."' ".$disableALL." ".$dis." value='".$part['FAImountPoint']."' id='FAImountPoint_".$key."'></td>";
263 $str .= "\n<td><input name='FAIpartitionSize_".$key."' ".$disableALL." ".$dis." value='".$part['FAIpartitionSize']."' id='FAIpartitionSize_".$key."' size=12></td>";
264 $str .= "\n<td><input name='FAImountOptions_".$key."' ".$disableALL." ".$dis." value='".$part['FAImountOptions']."' id='FAImountOptions_".$key."' style='width:100px;'></td>";
265 $str .= "\n<td><input name='FAIfsOptions_".$key."' ".$disableALL." ".$dis." value='".$part['FAIfsOptions']."' id='FAIfsOptions_".$key."' style='width:100px;'></td>";
267 $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
268 "changeState('FAIpartitionSize_".$key."') ; ".
269 "changeState('FAImountOptions_".$key."') ; ".
270 "changeState('FAIfsType_".$key."') ; ".
271 "changeState('FAIfsOptions_".$key."') ; \"";
274 if($part['FAIpartitionFlags']!=false){
275 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
276 }else{
277 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
278 }
279 $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
280 $str .= "\n</tr>";
281 }
282 }
283 $str.="</table>";
284 return($str);
286 }
288 function save()
289 {
290 $tmp = array();
291 $tmp['cn'] = $this->DISK_cn;
293 /* Attach deleted */
294 foreach($this->deletePartitions as $key=>$val) {
295 $this->partitions[$val['FAIpartitionNr']."-delete"]=$val;
296 $this->partitions[$val['FAIpartitionNr']."-delete"]['status']="delete";
297 }
299 $tmp['description'] = $this->DISK_description;
300 $tmp['partitions'] = $this->partitions;
301 $tmp['status'] = $this->status;
303 /* If hdd name has changed, tell partitionTable to rename it */
304 if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
305 $tmp['rename']['from'] = $this->old_cn;
306 $tmp['rename']['to'] = $this->DISK_cn;
307 }
308 return($tmp);
309 }
312 /* Save data to object */
313 function save_object()
314 {
315 if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
316 plugin::save_object();
318 /* Check base attributes */
319 foreach($this->attributes as $attrs){
320 if(isset($_POST[$attrs])){
321 $this->$attrs = $_POST[$attrs];
322 }
323 }
325 foreach($this->partitions as $key => $part){
326 foreach($this->UsedAttrs as $attrs){
328 if(!in_array_ics($attrs,array("FAIpartitionNr"))){
329 if(isset($_POST[$attrs."_".$key])){
330 $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
331 }else{
332 $this->partitions[$key][$attrs] = false;
333 }
334 }
335 }
336 }
337 }
338 }
341 /* Check supplied data */
342 function check()
343 {
344 /* Call common method to give check the hook */
345 $message= plugin::check();
347 /* check every partition.
348 * if there is an invalid value defined, append an errorstr to message
349 */
351 /* Array that contain every partitionname mountpoint etc already assigned */
352 $alreadyUsed = array();
353 foreach($this->UsedAttrs as $attrs){
354 $alreadyUsed[$attrs] = array();
355 }
357 /* Check disk name, to prevent from saving empty names */
358 if(empty($this->DISK_cn)){
359 $message[] = _("Please specify a valid disk name");
360 }
362 foreach($this->partitions as $key => $part){
364 /* Skip all checks, if preserve is set */
365 if($part['FAIpartitionFlags'] == "preserve"){
366 $this->partitions[$key]['FAIfsType'] = "preserve";
367 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
368 continue;
369 }
371 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
372 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
373 }
375 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
376 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
377 $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
378 }
379 }
380 if($part['FAIfsType'] == "swap"){
381 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
382 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
383 }
384 }
385 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
386 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
387 }
389 $tmp = split("-",$part['FAIpartitionSize']);
390 switch (count($tmp)){
391 case 0:
392 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
393 break;
394 case 1:
395 if (!is_id(is_id($tmp[0]))){
396 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
397 }
398 break;
400 case 2:
401 if((!is_id($tmp[0]))&&(!is_id($tmp[1]))&&(!empty($tmp[1]))){
402 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
403 }elseif($tmp[0]>=$tmp[1] &&(!empty($tmp[1]))){
404 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
405 }
406 break;
408 default:
409 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
410 }
412 foreach($this->UsedAttrs as $attrs){
413 $alreadyUsed[$attrs][$key] = $part[$attrs];
414 }
415 }
417 $cnt = 0;
418 foreach($this->partitions as $key => $part){
419 if($part['FAIpartitionType'] == "primary"){
420 $cnt ++ ;
421 }
422 }
423 if($cnt > 3){
424 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
425 }
427 return ($message);
428 }
430 }
432 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
433 ?>