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 $parent = null;
19 function faiPartitionTableEntry (&$config, $dn= NULL,&$parent,$disk=false)
20 {
21 plugin::plugin ($config, $dn);
22 $this->UsedAttrs = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
23 "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
25 $this->parent = $parent;
27 /* We want to edit an entry */
28 if($disk){
30 /* Walk through partitions */
31 foreach($disk['partitions'] as $name => $values){
33 $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
35 /* Assign empty attributes, if attribute is missing */
36 foreach($this->UsedAttrs as $attr){
37 if(!isset($values[$attr])){
38 $disk['partitions'][$name][$attr]="";
39 }
40 }
42 if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
43 $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
44 }
45 }
47 /* Set default attributes */
48 $this->DISKcn = $disk['cn'];
49 $this->DISKdescription = $disk['description'];
50 $this->partitions = $disk['partitions'];
51 $this->is_edit = true;
52 $this->old_cn = $disk['cn'];
53 }
54 }
57 function execute()
58 {
59 /* Call parent execute */
60 plugin::execute();
62 /* Fill templating stuff */
63 $smarty = get_smarty();
64 $s_action = "";
65 $s_entry = "";
66 $display = "";
68 /* Assign values
69 * And Checkbox selection
70 */
71 foreach($this->attributes as $attrs){
72 $smarty->assign($attrs,$this->$attrs);
73 if($this->$attrs){
74 $smarty->assign($attrs."CHK"," ");
75 }else{
76 $smarty->assign($attrs."CHK"," disabled ");
77 }
78 }
80 /* Check all Posts if there is something usefull for us,
81 * For example : Delete is posted as Delete_1
82 * The number specifies the index we want to delete
83 */
84 foreach($_POST as $name => $value){
85 if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
86 $tmp = explode("_",$name);
87 $s_action = "remove";
88 $s_entry = $tmp[1];
89 }
90 }
92 /* To remove a partition we unset the index posted.
93 * We must sort the index again, else we possibly got problems
94 * with partitions order.
95 */
96 if($s_action == "remove" && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
97 unset($this->partitions[$s_entry]);
98 $tmp= array();
99 foreach($this->partitions as $part){
100 $tmp[count($tmp)+1]=$part;
101 }
102 $this->partitions = $tmp;
103 }
105 /* To add a partitions we only append an empty
106 * array to the already defined partitions.
107 */
108 if(isset($_POST['AddPartition']) && $this->acl_is_createable() && !preg_match("/freeze/i",$this->FAIstate)){
109 foreach($this->UsedAttrs as $attr){
110 $tmp[$attr] = "";
111 }
112 $tmp["old_cn"] = "";
113 $this->partitions[count($this->partitions)+1]=$tmp;
114 }
116 /* $setup contains a table with the partitions.
117 */
118 $smarty->assign("setup", $this->generateParts());
119 foreach($this->attributes as $attr){
120 $smarty->assign($attr,$this->$attr);
121 }
123 $tmp = $this->plInfo();
124 $sacl = "";
125 foreach($tmp['plProvidedAcls'] as $name => $translated){
126 $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
127 $smarty->assign($name."ACL",$acl);
128 }
130 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
131 $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
133 /* Fetch template and show the result
134 */
135 $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
136 return($display);
137 }
139 function generateParts()
140 {
141 /* Define Arrays with allowed syntax */
142 $PartitionTypes = array("primary"=>_("primary"),"logical"=>_("logical"));
143 $FAIfsTypes = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
145 /* Display Header */
146 $str = "";
147 if (count($this->partitions)){
148 $str .= "<tr>";
149 $str .= "<td><b>"._("Type")."</b></td>";
150 $str .= "<td><b>"._("FS type")."</b></td>";
151 $str .= "<td><b>"._("Mount point")."</b></td>";
152 $str .= "<td><b>"._("Size in MB")."</b></td>";
153 $str .= "<td><b>"._("Mount options")."</b></td>";
154 $str .= "<td><b>"._("FS option")."</b></td>";
155 $str .= "<td><b>"._("Preserve")."</b></td>";
156 $str .= "<td> </td>";
157 $str .= "</tr>";
158 }
160 /* Walk through all defined partitions.
161 * Create a new row for each partition and append it to
162 * the header defined above.
163 * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
164 */
165 foreach($this->partitions as $key => $part){
167 $dis = "";
168 if($part['FAIpartitionFlags'] == "preserve"){
169 $dis = " disabled ";
170 }
172 $disableALL = "";
173 if (preg_match("/freeze/", $this->FAIstate)){
174 $disableALL = " disabled ";
175 }
177 /* Generate Partition select box
178 */
179 $PartitionType = "";
180 if($this->acl_is_writeable("FAIpartitionType")){
181 $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
182 foreach($PartitionTypes as $type => $PTname){
183 if($part['FAIpartitionType'] == $type){
184 $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
185 }else{
186 $PartitionType .="<option value='".$type."'>".$PTname."</option>";
187 }
188 }
189 $PartitionType.="</select>";
190 }elseif($this->acl_is_readable("FAIpartitionType")){
191 $PartitionType = $part['FAIpartitionType'];
192 }
195 /* Generate fsType select box
196 */
197 $FAIfsType= "";
198 if($this->acl_is_writeable("FAIfsType")){
199 $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
200 foreach($FAIfsTypes as $type){
201 if($part['FAIfsType'] == $type){
202 $FAIfsType .="<option value='".$type."' selected >".$type."</option>";
203 }else{
204 $FAIfsType .="<option value='".$type."'>".$type."</option>";
205 }
206 }
207 $FAIfsType.="</select>";
208 }elseif($this->acl_is_readable("FAIfsType")){
209 $FAIfsType = $part['FAIfsType'];
210 }
212 foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
213 $$attr = "";
214 if($this->acl_is_writeable($attr)){
215 $$attr = "<input name='".$attr."_".$key."' ".$disableALL." ".$dis."
216 value='".$part[$attr]."' id='".$attr."_".$key."'>";
218 }elseif($this->acl_is_readable($attr)){
219 $$attr = $part[$attr];
220 }
221 }
224 $str .= "\n<tr>";
225 $str .= "\n<td>".$PartitionType."</td>";
226 $str .= "\n<td>".$FAIfsType."</td>";
227 $str .= "\n<td>".$FAImountPoint."</td>";
228 $str .= "\n<td>".$FAIpartitionSize."</td>";
229 $str .= "\n<td>".$FAImountOptions."</td>";
230 $str .= "\n<td>".$FAIfsOptions."</td>";
232 $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
233 "changeState('FAIpartitionSize_".$key."') ; ".
234 "changeState('FAImountOptions_".$key."') ; ".
235 "changeState('FAIfsType_".$key."') ; ".
236 "changeState('FAIfsOptions_".$key."') ; \"";
238 if($this->acl_is_writeable("FAIpartitionFlags")){
239 if($part['FAIpartitionFlags']!=false){
240 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
241 }else{
242 $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
243 }
244 }else{
245 if($part['FAIpartitionFlags']!=false){
246 $str .= "<td>"._("False")."</td>";
247 }else{
248 $str .= "<td>"._("True")."</td>";
249 }
250 }
251 if($this->acl_is_removeable()){
252 $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
253 }else{
254 $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";
255 }
256 $str .= "\n</tr>";
257 }
259 if(!empty($str)){
260 $str = "<table summary='' style='width:100%'>".$str."</table>";
261 }
262 return($str);
264 }
266 function save()
267 {
268 $tmp = array();
269 $tmp['cn'] = $this->DISKcn;
271 /* Attach partitions */
272 foreach($this->partitions as $key=>$val) {
273 $this->partitions[$key]['FAIpartitionNr']=$key;
274 $this->partitions[$key]['cn']=$key;
275 }
277 $tmp['description'] = $this->DISKdescription;
278 $tmp['partitions'] = $this->partitions;
280 /* If hdd name has changed, tell partitionTable to rename it */
281 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
282 $tmp['rename']['from'] = $this->old_cn;
283 $tmp['rename']['to'] = $this->DISKcn;
284 }
285 $tmp['FAIdiskType'] = "old";
286 return($tmp);
287 }
290 /* Save data to object */
291 function save_object()
292 {
293 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
294 plugin::save_object();
296 foreach($this->partitions as $key => $part){
297 foreach($this->UsedAttrs as $attrs){
299 if($this->acl_is_writeable($attrs)){
301 if(isset($_POST[$attrs."_".$key])){
302 $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
303 }else{
304 $this->partitions[$key][$attrs] = false;
305 }
306 }
307 }
309 /* Adapt ext3 to have -j option */
310 if ($this->partitions[$key]["FAIfsType"] == "ext3") {
311 $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
312 $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
313 }
314 }
316 }
317 }
320 /* Check supplied data */
321 function check()
322 {
323 /* Call common method to give check the hook */
324 $message= plugin::check();
326 /* Check for an empty disk name */
327 $d = trim($this->DISKcn);
328 if($d == "" ){
329 $message[] = msgPool::required(_("Name"));
330 }
331 if(preg_match("/[^a-z0-9_\-]/i",$d)){
332 $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
333 }
335 if($this->old_cn != $this->DISKcn && in_array($this->DISKcn,$this->parent->getUsedDiskNames())){
336 $message[] = msgPool::duplicated(_("Name"));
337 }
339 /* check every partition.
340 * if there is an invalid value defined, append an errorstr to message
341 */
343 /* Array that contain every partitionname mountpoint etc already assigned */
344 $alreadyUsed = array();
345 foreach($this->UsedAttrs as $attrs){
346 $alreadyUsed[$attrs] = array();
347 }
349 foreach($this->partitions as $key => $part){
351 /* Skip all checks, if preserve is set */
352 if($part['FAIpartitionFlags'] == "preserve"){
353 $this->partitions[$key]['FAIfsType'] = "preserve";
354 $this->partitions[$key]['FAIpartitionSize'] = "preserve";
355 continue;
356 }
358 if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
359 $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
360 }
362 if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
363 if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
364 $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
365 }
366 }
367 if($part['FAIfsType'] == "swap"){
368 if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
369 $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
370 }
371 }
372 if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
373 $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
374 }
376 $tmp = explode("-",$part['FAIpartitionSize']);
377 switch (count($tmp)){
378 case 0:
379 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
380 break;
381 case 1:
382 if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
383 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
384 }
385 break;
387 case 2:
388 if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
389 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
390 }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
391 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
392 }
393 break;
395 default:
396 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
397 }
399 foreach($this->UsedAttrs as $attrs){
400 $alreadyUsed[$attrs][$key] = $part[$attrs];
401 }
402 }
404 $cnt = 0;
405 foreach($this->partitions as $key => $part){
406 if($part['FAIpartitionType'] == "primary"){
407 $cnt ++ ;
408 }
409 }
410 if($cnt > 3){
411 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
412 }
414 return ($message);
415 }
419 /* Return plugin informations for acl handling */
420 static function plInfo()
421 {
422 return (array(
423 "plShortName" => _("Partition table entry"),
424 "plDescription" => _("FAI partition table entry"),
425 "plSelfModify" => FALSE,
426 "plDepends" => array(),
427 "plPriority" => 27,
428 "plSection" => array("administration"),
429 "plCategory" => array("fai"),
430 "plProvidedAcls" => array(
431 "DISKcn" => _("Name"),
432 "DISKdescription" => _("Description"),
433 "FAIpartitionType" => _("Partition type"),
434 "FAIpartitionNr" => _("Partition no."),
435 "FAIfsType" => _("File system type"),
436 "FAImountPoint" => _("Mount point"),
437 "FAIpartitionSize" => _("Partition size"),
438 "FAImountOptions" => _("Mount options"),
439 "FAIfsOptions" => _("File system options"),
440 "FAIpartitionFlags" => _("Partition flags"))
441 ));
442 }
445 }
447 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
448 ?>