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("DISK_cn","DISK_description");
13 var $objectclasses= array();
15 var $DISK_cn = "";
16 var $DISK_description = "";
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->DISK_cn = $disk['cn'];
77 $this->DISK_description = $disk['description'];
78 $this->partitions = $disk['partitions'];
79 $this->is_edit = true;
80 $this->old_cn = $disk['cn'];
81 }
82 }
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)){
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"){
124 if($this->partitions[$s_entry]['status'] == "edited"){
125 $this->deletePartitions[] = $this->partitions[$s_entry];
126 unset($this->partitions[$s_entry]);
127 }else{
128 unset($this->partitions[$s_entry]);
129 }
130 $tmp= array();
131 foreach($this->partitions as $part){
132 $tmp[count($tmp)+1]=$part;
133 }
134 $this->partitions = $tmp;
135 }
137 /* To add a partitions we only append an empty
138 * array to the already defined partitions.
139 */
140 if(isset($_POST['AddPartition'])){
142 /* FAIpartitionNr is used to indentify an entry.
143 (The index of an entry may vary)
144 Walk through the given entries and check for a free FAIpartitionNr. */
145 $ids = array();
146 foreach($this->partitions as $part){
147 $ids[$part['FAIpartitionNr']] = $part['FAIpartitionNr'];
148 }
149 $new_nr = 1 ;
150 while(isset($ids[$new_nr]) && $new_nr < 100){
151 $new_nr ++ ;
152 }
154 /* Add empty attributes */
155 foreach($this->UsedAttrs as $attr){
156 $tmp[$attr] = "";
157 }
158 $tmp["old_cn"] = "";
159 $tmp['status'] ="new";
160 $tmp['FAIpartitionNr'] = $new_nr;
161 $this->partitions[]=$tmp;
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","reiserfs","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 foreach($this->partitions as $key => $part){
359 /* Skip all checks, if preserve is set */
360 if($part['FAIpartitionFlags'] == "preserve"){
361 $this->partitions[$key]['FAIfsType'] = "preserve";
362 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
363 continue;
364 }
366 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
367 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
368 }
370 if($part['FAIfsType']!="swap"){
371 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
372 $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
373 }
374 }
375 if($part['FAIfsType'] == "swap"){
376 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
377 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
378 }
379 }
380 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
381 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
382 }
384 $tmp = split("-",$part['FAIpartitionSize']);
385 switch (count($tmp)){
386 case 0:
387 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
388 break;
389 case 1:
390 if (!is_id(is_id($tmp[0]))){
391 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
392 }
393 break;
395 case 2:
396 if((!is_id($tmp[0]))||(!is_id($tmp[1]))){
397 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
398 }elseif($tmp[0]>=$tmp[1]){
399 $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
400 }
401 break;
403 default:
404 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
405 }
407 foreach($this->UsedAttrs as $attrs){
408 $alreadyUsed[$attrs][$key] = $part[$attrs];
409 }
410 }
412 $cnt = 0;
413 foreach($this->partitions as $key => $part){
414 if($part['FAIpartitionType'] == "primary"){
415 $cnt ++ ;
416 }
417 }
418 if($cnt > 3){
419 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
420 }
422 return ($message);
423 }
425 }
427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
428 ?>