99417316ef67624c8a4e2bbec8a979b8e1f565ac
1 <?php
3 class faiPartitionTable 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("cn","description");
13 var $objectclasses = array("top","FAIclass","FAIpartitionTable");
15 /* Specific attributes */
16 var $cn = ""; // The class name for this object
17 var $description = ""; // The description for this set of partitions
18 var $disks = array(); // All defined Disks
19 var $is_dialog = false; // specifies which buttons will be shown to save or abort
21 var $FAIstate = "";
22 var $ui;
24 var $view_logged = FALSE;
26 function faiPartitionTable (&$config, $dn= NULL)
27 {
28 /* Load Attributes */
29 plugin::plugin ($config, $dn);
31 $this->acl ="#all#";
33 $this->ui = get_userinfo();
35 /* If "dn==new" we try to create a new entry
36 * Else we must read all objects from ldap which belong to this entry.
37 * First read disks from ldap ... and then the partition definitions for the disks.
38 */
39 if($dn != "new"){
40 $this->dn =$dn;
42 /* Get FAIstate
43 */
44 if(isset($this->attrs['FAIstate'][0])){
45 $this->FAIstate = $this->attrs['FAIstate'][0];
46 }
48 /* Read all disks from ldap taht are defined fot this partition table
49 */
50 $ldap = $this->config->get_ldap_link();
51 $ldap->cd ($this->dn);
52 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
53 while($object = $ldap->fetch()){
55 /* Skip objects, that are tagged as removed */
56 if(isset($object['FAIstate'][0])){
57 if(preg_match("/removed$/",$object['FAIstate'][0])){
58 continue;
59 }
60 }
62 $this->disks[$object['cn'][0]]['status'] = "edited";
63 $this->disks[$object['cn'][0]]['dn'] = $object['dn'];
64 $this->disks[$object['cn'][0]]['cn'] = $object['cn'][0];
65 if(isset($object['description'][0])){
66 $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
67 }else{
68 $this->disks[$object['cn'][0]]['description'] = "";
69 }
70 $this->disks[$object['cn'][0]]['partitions'] = array();
71 }
73 /* read all partitions for each disk
74 */
75 foreach($this->disks as $name => $disk){
76 $ldap->cd ($disk['dn']);
77 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
78 while($partition = $ldap->fetch()){
80 /* Skip objects, that are tagged as removed */
81 if(isset($partition['FAIstate'][0])){
82 if(preg_match("/removed$/",$partition['FAIstate'][0])){
83 continue;
84 }
85 }
87 /* remove count ... from ldap result
88 */
89 foreach($partition as $key=>$val){
90 if((is_numeric($key))||($key=="count")||($key=="dn")){
91 unset($partition[$key]);
92 }else{
93 $partition[$key] = $val[0];
94 }
95 }
97 /* Append fetched partitions
98 */
99 $partition['status']="edited";
100 $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition;
101 }
102 }
103 }
104 ksort($this->disks);
105 }
108 function acl_base_for_current_object($dn)
109 {
110 if($dn == "new"){
111 if($this->dn == "new"){
112 $dn = session::get('CurrentMainBase');
113 }else{
114 $dn = $this->dn;
115 }
116 }
117 return($dn);
118 }
121 function execute()
122 {
123 /* Call parent execute */
124 plugin::execute();
126 if($this->is_account && !$this->view_logged){
127 $this->view_logged = TRUE;
128 new log("view","fai/".get_class($this),$this->dn);
129 }
131 /* Fill templating stuff */
132 $smarty= get_smarty();
133 $display= "";
135 /* Add Disk to this Partitionset
136 * This code adds a new HDD to the disks
137 * A new Dialog will be opened
138 */
139 if(isset($_POST['AddDisk'])){
140 $usedDiskNames =array();
141 foreach($this->disks as $key=>$disk){
142 $usedDiskNames[]= $key;
143 }
144 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames);
145 $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
146 $this->dialog->set_acl_category("fai");
147 $this->dialog->FAIstate = $this->FAIstate;
148 $this->is_dialog = true;
149 }
151 /* Edit disk.
152 * Open dialog which allows us to edit the selected entry
153 */
155 if($this->dn != "new"){
156 session::set('objectinfo',$this->dn);
157 }
159 if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
160 $usedDiskNames =array();
161 $Udisk = $_POST['disks'][0];
162 if(isset($this->disks[$Udisk])){
164 foreach($this->disks as $key=>$disk){
165 if($key != $Udisk){
166 $usedDiskNames[]= $key;
167 }
168 }
170 /* Set object info string, which will be displayed in plugin info line */
171 if(isset($this->disks[$Udisk]['dn'])){
172 session::set('objectinfo',$this->disks[$Udisk]['dn']);
173 $dn = $this->disks[$Udisk]['dn'];
174 }else{
175 session::set('objectinfo',"");
176 $dn = "new";
177 }
179 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]);
180 $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
181 $this->dialog->set_acl_category("fai");
182 $this->dialog->FAIstate = $this->FAIstate;
184 $this->is_dialog = true;
185 }
186 }
188 /* Edit aborted, close dialog, without saving anything
189 */
190 if(isset($_POST['CancelDisk'])){
191 unset($this->dialog);
192 $this->dialog = FALSE;
193 $this->is_dialog=false;
194 }
196 /* Dialog saved
197 * Save given data from Dialog, if no error is occurred
198 */
199 if(isset($_POST['SaveDisk'])){
201 if($this->FAIstate != "freeze"){
202 $this->dialog->save_object();
203 if(count($this->dialog->check())){
204 foreach($this->dialog->check() as $msg){
205 print_red($msg);
206 }
207 }else{
208 $disk = $this->dialog->save();
209 if(isset($disk['rename'])){
210 if($this->disks[$disk['rename']['from']]['status']=="edited"){
211 $this->disks[$disk['rename']['from']]['status']="delete";
212 }else{
213 unset($this->disks[$disk['rename']['from']]);
214 }
216 foreach($disk['partitions'] as $key => $val){
217 if($disk['partitions'][$key]['status']!="delete"){
218 $disk['partitions'][$key]['status']= "new";
219 }
220 }
222 $disk['status']="new";
223 $disk['cn']= $disk['rename']['to'];
224 }
226 $this->disks[$disk['cn']]=$disk;
227 unset($this->dialog);
228 $this->dialog = FALSE;
229 $this->is_dialog=false;
230 ksort($this->disks);
231 }
232 }else{
233 $this->dialog = FALSE;
234 $this->is_dialog=false;
235 }
236 }
238 /* Delete selected disk drive from list
239 * Assign delete status for all its partitions
240 */
241 if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
242 if($this->FAIstate != "freeze"){
243 foreach($_POST['disks'] as $disk) {
245 if(isset($this->disks[$disk])){
246 if($this->disks[$disk]['status']=="edited"){
247 $this->disks[$disk."-delete"]=$this->disks[$disk];
248 unset($this->disks[$disk]);
249 $disk = $disk."-delete";
250 $this->disks[$disk]['status']="delete";
251 foreach($this->disks[$disk]['partitions'] as $name => $value ){
252 if($value['status']=="edited"){
253 $this->disks[$disk]['partitions'][$name]['status']="delete";
254 }else{
255 unset($this->disks[$disk]['partitions'][$name]);
256 }
257 }
258 }else{
259 unset($this->disks[$disk]);
260 }
261 }
262 }
263 }
264 }
266 /* Display dialog if one is defined
267 */
268 if(is_object($this->dialog)){
269 $this->dialog->save_object();
270 return($this->dialog->execute());
271 }
273 /* Assign all attributes to smarty engine
274 */
275 foreach($this->attributes as $attrs){
276 $smarty->assign($attrs,$this->$attrs);
277 if($this->$attrs){
278 $smarty->assign($attrs."CHK"," ");
279 }else{
280 $smarty->assign($attrs."CHK"," disabled ");
281 }
282 }
284 $dn = $this->acl_base_for_current_object($this->dn);
285 $smarty->assign("sub_object_is_addable",
286 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
287 !preg_match("/freeze/",$this->FAIstate));
289 $tmp = $this->plInfo();
290 foreach($tmp['plProvidedAcls'] as $name => $translated){
291 $smarty->assign($name."ACL",$this->getacl($name));
292 }
293 $disks = $this->getDisks();
294 $smarty->assign("disks" ,$disks);
295 $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
296 return($display);
297 }
299 function getDisks(){
300 /* Return all available disks for this partition table
301 * Return in listBox friendly array
302 */
303 $a_return = array();
304 foreach($this->disks as $key => $disk){
306 $dn = "new";
307 if(isset($obj['dn'])){
308 $dn = $obj['dn'];
309 }
310 $dn = $this->acl_base_for_current_object($dn);
311 $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
312 if(preg_match("/(r|w)/",$acl)) {
314 if($disk['status'] != "delete"){
315 $cnt=0;
316 foreach($disk['partitions'] as $val){
317 if($val['status']!="delete"){
318 $cnt ++;
319 }
320 }
321 if(!empty($disk['description'])){
322 if($cnt == 1){
323 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
324 }else{
325 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
326 }
327 }else{
328 if($cnt == 1){
329 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
330 }else{
331 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
332 }
333 }
334 }
335 }
336 }
337 return($a_return);
338 }
341 /* Delete me, and all my subtrees
342 */
343 function remove_from_parent()
344 {
345 $ldap = $this->config->get_ldap_link();
346 $ldap->cd ($this->dn);
348 $faifilter = session::get('faifilter');
349 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
350 if($faifilter['branch'] == "main"){
351 $use_dn = $this->dn;
352 }
354 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
356 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
358 foreach($this->disks as $disk){
360 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
361 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $disk_dn);
362 if($faifilter['branch'] == "main"){
363 $use_dn = $disk_dn;
364 }
365 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
367 foreach($disk['partitions'] as $key => $partition){
369 $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;
370 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $partition_dn);
371 if($faifilter['branch'] == "main"){
372 $use_dn = $disk_dn;
373 }
374 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
375 }
376 }
377 }
380 /* Save data to object
381 */
382 function save_object()
383 {
384 if($this->FAIstate == "freeze") return;
385 plugin::save_object();
386 foreach($this->attributes as $attrs){
387 if(isset($_POST[$attrs])){
388 $this->$attrs = $_POST[$attrs];
389 }
390 }
391 }
394 /* Check supplied data */
395 function check()
396 {
397 /* Call common method to give check the hook */
398 $message= plugin::check();
400 return ($message);
401 }
404 /* Save to LDAP */
405 function save()
406 {
408 plugin::save();
409 /* Save current settings.
410 * 1 : We must save the partition table, with its description and cn
411 * 2 : Append Disk with cn and description.
412 * 3 : Save partitions for each disk
413 */
415 $ldap = $this->config->get_ldap_link();
417 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
418 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/partition table with dn '%s' failed."),$this->dn));
420 if($this->initially_was_account){
421 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
422 }else{
423 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
424 }
426 /* Sort entries, because we must delete entries with status="delete" first */
427 $order = array();
428 foreach($this->disks as $key => $disk){
429 if($disk['status'] == "delete"){
430 $order[$key] = $disk;
431 }
432 }
433 foreach($this->disks as $key => $disk){
434 if($disk['status'] != "delete"){
435 $order[$key] = $disk;
436 }
437 }
439 /* Append all disks to ldap */
440 foreach($order as $cn=>$disk){
441 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
442 $disk_attrs['cn'] = $disk['cn'];
443 $disk_attrs['description'] = $disk['description'];
444 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
446 if($disk['status']=="new"){
447 $ldap->cat($disk_dn,array("objectClass"));
448 if($ldap->count()){
449 $disk['status']="edited";
450 }
451 }
453 /* Tag object */
454 $this->tag_attrs(&$disk_attrs, $disk_dn, $this->gosaUnitTag);
456 if($disk['status'] == "delete"){
457 FAI::prepare_to_save_FAI_object($disk_dn,array(),true);
458 $this->handle_post_events("remove");
459 }elseif($disk['status'] == "edited"){
460 FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
461 $this->handle_post_events("modify");
462 }elseif($disk['status']=="new"){
463 FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
464 $this->handle_post_events("add");
465 }
467 if($disk['status']!="delete")
468 /* Add all partitions */
469 foreach($disk['partitions'] as $key => $partition){
470 $partition_attrs = array();
472 foreach($partition as $key => $value){
473 if(!empty($value)){
474 $partition_attrs[$key]=$value;
475 }else{
476 unset($partition_attrs[$key]);
477 }
478 }
480 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
481 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
482 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
484 unset($partition_attrs['status']);
485 unset($partition_attrs['old_cn']);
487 if($partition['status']=="new"){
488 $ldap->cat($partition_dn,array("objectClass"));
489 if($ldap->count()){
490 $partition['status']="edited";
491 }
492 }
494 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
495 $partition_attrs['FAImountPoint']="swap";
496 }
498 /* Tag object */
499 $this->tag_attrs(&$partition_attrs, $partition_dn, $this->gosaUnitTag);
501 if($partition['status'] == "delete"){
502 FAI::prepare_to_save_FAI_object($partition_dn,array(),true);
503 $this->handle_post_events("remove");
504 }elseif($partition['status'] == "edited"){
505 FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
506 $this->handle_post_events("modify");
507 }elseif($partition['status']=="new"){
508 FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
509 $this->handle_post_events("add");
510 }
511 }
512 }
513 $this->handle_post_events("add");
514 }
517 function PrepareForCopyPaste($source)
518 {
519 plugin::PrepareForCopyPaste($source);
520 /* Get FAIstate
521 */
522 if(isset($source['FAIstate'][0])){
523 $this->FAIstate = $source['FAIstate'][0];
524 }
526 /* Read all disks from ldap taht are defined fot this partition table
527 */
528 $ldap = $this->config->get_ldap_link();
529 $ldap->cd ($source['dn']);
530 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
531 while($object = $ldap->fetch()){
533 /* Skip objects, that are tagged as removed */
534 if(isset($object['FAIstate'][0])){
535 if(preg_match("/removed$/",$object['FAIstate'][0])){
536 continue;
537 }
538 }
540 $this->disks[$object['cn'][0]]['status'] = "edited";
541 $this->disks[$object['cn'][0]]['dn'] = $object['dn'];
542 $this->disks[$object['cn'][0]]['cn'] = $object['cn'][0];
543 if(isset($object['description'][0])){
544 $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
545 }else{
546 $this->disks[$object['cn'][0]]['description'] = "";
547 }
548 $this->disks[$object['cn'][0]]['partitions'] = array();
549 }
551 /* read all partitions for each disk
552 */
553 foreach($this->disks as $name => $disk){
554 $ldap->cd ($disk['dn']);
555 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
556 while($partition = $ldap->fetch()){
558 /* Skip objects, that are tagged as removed */
559 if(isset($partition['FAIstate'][0])){
560 if(preg_match("/removed$/",$partition['FAIstate'][0])){
561 continue;
562 }
563 }
565 /* remove count ... from ldap result
566 */
567 foreach($partition as $key=>$val){
568 if((is_numeric($key))||($key=="count")||($key=="dn")){
569 unset($partition[$key]);
570 }else{
571 $partition[$key] = $val[0];
572 }
573 }
575 /* Append fetched partitions
576 */
577 $partition['status']="edited";
578 $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition;
579 }
580 }
581 ksort($this->disks);
582 }
585 /* Return plugin informations for acl handling */
586 static function plInfo()
587 {
588 return (array(
589 "plShortName" => _("Partition table"),
590 "plDescription" => _("FAI partition table"),
591 "plSelfModify" => FALSE,
592 "plDepends" => array(),
593 "plPriority" => 26,
594 "plSection" => array("administration"),
595 "plCategory" => array("fai"),
596 "plProvidedAcls" => array(
597 "cn" => _("Name")." ("._("Read only").")",
598 "description" => _("Description"))
599 ));
600 }
601 }
603 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
604 ?>