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
20 var $dialog = NULL; // a dialog, e.g. new disk dialog
22 var $FAIstate = "";
24 function faiPartitionTable ($config, $dn= NULL)
25 {
26 /* Load Attributes */
27 plugin::plugin ($config, $dn);
29 $this->acl ="#all#";
31 /* If "dn==new" we try to create a new entry
32 * Else we must read all objects from ldap which belong to this entry.
33 * First read disks from ldap ... and then the partition definitions for the disks.
34 */
35 if($dn != "new"){
36 $this->dn =$dn;
38 /* Set acls
39 */
40 $ui = get_userinfo();
41 $acl = get_permissions ($this->dn, $ui->subtreeACL);
42 $acli = get_module_permission($acl, "FAIclass", $this->dn);
43 $this->acl=$acli;
45 /* Get FAIstate
46 */
47 if(isset($this->attrs['FAIstate'][0])){
48 $this->FAIstate = $this->attrs['FAIstate'][0];
49 }
51 /* Read all disks from ldap taht are defined fot this partition table
52 */
53 $ldap = $this->config->get_ldap_link();
54 $ldap->cd ($this->dn);
55 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
56 while($object = $ldap->fetch()){
58 /* Skip objects, that are tagged as removed */
59 if(isset($object['FAIstate'][0])){
60 if(preg_match("/removed$/",$object['FAIstate'][0])){
61 continue;
62 }
63 }
65 $this->disks[$object['cn'][0]]['status'] = "edited";
66 $this->disks[$object['cn'][0]]['dn'] = $object['dn'];
67 $this->disks[$object['cn'][0]]['cn'] = $object['cn'][0];
68 if(isset($object['description'][0])){
69 $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
70 }else{
71 $this->disks[$object['cn'][0]]['description'] = "";
72 }
73 $this->disks[$object['cn'][0]]['partitions'] = array();
74 }
76 /* read all partitions for each disk
77 */
78 foreach($this->disks as $name => $disk){
79 $ldap->cd ($disk['dn']);
80 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
81 while($partition = $ldap->fetch()){
83 /* Skip objects, that are tagged as removed */
84 if(isset($partition['FAIstate'][0])){
85 if(preg_match("/removed$/",$partition['FAIstate'][0])){
86 continue;
87 }
88 }
90 /* remove count ... from ldap result
91 */
92 foreach($partition as $key=>$val){
93 if((is_numeric($key))||($key=="count")||($key=="dn")){
94 unset($partition[$key]);
95 }else{
96 $partition[$key] = $val[0];
97 }
98 }
100 /* Append fetched partitions
101 */
102 $partition['status']="edited";
103 $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition;
104 }
105 }
106 }
107 ksort($this->disks);
108 }
110 function execute()
111 {
112 /* Call parent execute */
113 plugin::execute();
115 /* Fill templating stuff */
116 $smarty= get_smarty();
117 $display= "";
119 /* Add Disk to this Partitionset
120 * This code adds a new HDD to the disks
121 * A new Dialog will be opened
122 */
123 if(isset($_POST['AddDisk'])){
124 $usedDiskNames =array();
125 foreach($this->disks as $key=>$disk){
126 $usedDiskNames[]= $key;
127 }
128 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames);
129 $this->dialog->acl = $this->acl;
130 $this->dialog->FAIstate = $this->FAIstate;
131 $this->is_dialog = true;
132 }
134 /* Edit disk.
135 * Open dialog which allows us to edit the selected entry
136 */
138 if($this->dn != "new"){
139 $_SESSION['objectinfo']= $this->dn;
140 }
142 if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
143 $usedDiskNames =array();
145 $Udisk = $_POST['disks'][0];
147 foreach($this->disks as $key=>$disk){
148 if($key != $Udisk){
149 $usedDiskNames[]= $key;
150 }
151 }
152 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]);
153 $this->dialog->acl = $this->acl;
154 $this->dialog->FAIstate = $this->FAIstate;
156 /* Set object info string, which will be displayed in plugin info line */
157 if(isset($this->disks[$Udisk]['dn'])){
158 $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
159 }else{
160 $_SESSION['objectinfo'] = "";
161 }
163 $this->is_dialog = true;
164 }
166 /* Edit aborted, close dialog, without saving anything
167 */
168 if(isset($_POST['CancelDisk'])){
169 unset($this->dialog);
170 $this->dialog = NULL;
171 $this->is_dialog=false;
172 }
174 /* Dialog saved
175 * Save given data from Dialog, if no error is occured
176 */
177 if(isset($_POST['SaveDisk'])){
179 if($this->FAIstate != "freeze"){
180 $this->dialog->save_object();
181 if(count($this->dialog->check())){
182 foreach($this->dialog->check() as $msg){
183 print_red($msg);
184 }
185 }else{
186 $disk = $this->dialog->save();
187 if(isset($disk['rename'])){
188 if($this->disks[$disk['rename']['from']]['status']=="edited"){
189 $this->disks[$disk['rename']['from']]['status']="delete";
190 }else{
191 unset($this->disks[$disk['rename']['from']]);
192 }
194 foreach($disk['partitions'] as $key => $val){
195 if($disk['partitions'][$key]['status']!="delete"){
196 $disk['partitions'][$key]['status']= "new";
197 }
198 }
200 $disk['status']="new";
201 $disk['cn']= $disk['rename']['to'];
202 }
204 $this->disks[$disk['cn']]=$disk;
205 unset($this->dialog);
206 $this->dialog = NULL;
207 $this->is_dialog=false;
208 ksort($this->disks);
209 }
210 }else{
211 $this->dialog = NULL;
212 $this->is_dialog=false;
213 }
214 }
216 /* Delete selected disk drive from list
217 * Assign delete status for all its partitions
218 */
219 if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
220 if($this->FAIstate != "freeze"){
221 foreach($_POST['disks'] as $disk) {
222 if($this->disks[$disk]['status']=="edited"){
223 $this->disks[$disk."-delete"]=$this->disks[$disk];
224 unset($this->disks[$disk]);
225 $disk = $disk."-delete";
226 $this->disks[$disk]['status']="delete";
227 foreach($this->disks[$disk]['partitions'] as $name => $value ){
228 if($value['status']=="edited"){
229 $this->disks[$disk]['partitions'][$name]['status']="delete";
230 }else{
231 unset($this->disks[$disk]['partitions'][$name]);
232 }
233 }
234 }else{
235 unset($this->disks[$disk]);
236 }
237 }
238 }
239 }
241 /* Display dialog if one is defined
242 */
243 if(isset($this->dialog)){
244 $this->dialog->save_object();
245 return($this->dialog->execute());
246 }
248 /* Assign all attributes to smarty engine
249 */
250 foreach($this->attributes as $attrs){
251 $smarty->assign($attrs,$this->$attrs);
252 if($this->$attrs){
253 $smarty->assign($attrs."CHK"," ");
254 }else{
255 $smarty->assign($attrs."CHK"," disabled ");
256 }
257 }
259 foreach($this->attributes as $attr){
260 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
261 }
263 $disks = $this->getDisks();
264 $smarty->assign("disks" ,$disks);
265 $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
266 return($display);
267 }
269 function getDisks(){
270 /* Return all available disks for this partition table
271 * Return in listBox friendly array
272 */
273 $a_return = array();
274 foreach($this->disks as $key => $disk){
275 if($disk['status'] != "delete"){
276 $cnt=0;
277 foreach($disk['partitions'] as $val){
278 if($val['status']!="delete"){
279 $cnt ++;
280 }
281 }
282 if(!empty($disk['description'])){
283 if($cnt == 1){
284 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
285 }else{
286 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
287 }
288 }else{
289 if($cnt == 1){
290 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
291 }else{
292 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
293 }
294 }
295 }
296 }
297 return($a_return);
298 }
301 /* Delete me, and all my subtrees
302 */
303 function remove_from_parent()
304 {
305 $ldap = $this->config->get_ldap_link();
306 $ldap->cd ($this->dn);
308 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
309 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
310 if($_SESSION['faifilter']['branch'] == "main"){
311 $use_dn = $this->dn;
312 }
314 prepare_to_save_FAI_object($use_dn,array(),true);
316 foreach($this->disks as $disk){
318 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
319 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $disk_dn);
320 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $disk_dn);
321 if($_SESSION['faifilter']['branch'] == "main"){
322 $use_dn = $disk_dn;
323 }
324 prepare_to_save_FAI_object($use_dn,array(),true);
326 foreach($disk['partitions'] as $key => $partition){
328 $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;
329 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $partition_dn);
330 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $partition_dn);
331 if($_SESSION['faifilter']['branch'] == "main"){
332 $use_dn = $disk_dn;
333 }
334 prepare_to_save_FAI_object($use_dn,array(),true);
335 }
336 }
337 }
340 /* Save data to object
341 */
342 function save_object()
343 {
344 if($this->FAIstate == "freeze") return;
345 plugin::save_object();
346 foreach($this->attributes as $attrs){
347 if(isset($_POST[$attrs])){
348 $this->$attrs = $_POST[$attrs];
349 }
350 }
351 }
354 /* Check supplied data */
355 function check()
356 {
357 /* Call common method to give check the hook */
358 $message= plugin::check();
360 return ($message);
361 }
364 /* Save to LDAP */
365 function save()
366 {
368 plugin::save();
369 /* Save current settings.
370 * 1 : We must save the partition table, with its description and cn
371 * 2 : Append Disk with cn and description.
372 * 3 : Save partitions for each disk
373 */
375 $ldap = $this->config->get_ldap_link();
377 prepare_to_save_FAI_object($this->dn,$this->attrs);
378 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/partition table with dn '%s' failed."),$this->dn));
380 /* Do object tagging */
381 $this->handle_object_tagging();
383 /* Sort entries, because we must delete entries with status="delete" first */
384 $order = array();
385 foreach($this->disks as $key => $disk){
386 if($disk['status'] == "delete"){
387 $order[$key] = $disk;
388 }
389 }
390 foreach($this->disks as $key => $disk){
391 if($disk['status'] != "delete"){
392 $order[$key] = $disk;
393 }
394 }
396 /* Append all disks to ldap */
397 foreach($order as $cn=>$disk){
398 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
399 $disk_attrs['cn'] = $disk['cn'];
400 $disk_attrs['description'] = $disk['description'];
401 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
403 if($disk['status']=="new"){
404 $ldap->cat($disk_dn,array("objectClass"));
405 if($ldap->count()){
406 $disk['status']="edited";
407 }
408 }
410 /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for disks */
411 $ldap->cat($disk_dn,array("objectClass"));
412 $attrs = $ldap->fetch();
413 if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
414 $disk_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
415 }
417 if($disk['status'] == "delete"){
418 prepare_to_save_FAI_object($disk_dn,array(),true);
419 $this->handle_post_events("remove");
420 }elseif($disk['status'] == "edited"){
421 prepare_to_save_FAI_object($disk_dn,$disk_attrs);
422 $this->handle_post_events("modify");
423 }elseif($disk['status']=="new"){
424 prepare_to_save_FAI_object($disk_dn,$disk_attrs);
425 $this->handle_post_events("add");
426 }
428 $this->handle_object_tagging($disk_dn, $this->gosaUnitTag);
430 if($disk['status']!="delete")
431 /* Add all partitions */
432 foreach($disk['partitions'] as $key => $partition){
433 $partition_attrs = array();
435 foreach($partition as $key => $value){
436 if(!empty($value)){
437 $partition_attrs[$key]=$value;
438 }else{
439 unset($partition_attrs[$key]);
440 }
441 }
443 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
444 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
445 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
447 unset($partition_attrs['status']);
448 unset($partition_attrs['old_cn']);
450 if($partition['status']=="new"){
451 $ldap->cat($partition_dn,array("objectClass"));
452 if($ldap->count()){
453 $partition['status']="edited";
454 }
455 }
457 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
458 $partition_attrs['FAImountPoint']="swap";
459 }
461 /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for partitions */
462 $ldap->cat($partition_dn,array("objectClass"));
463 $attrs = $ldap->fetch();
464 if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
465 $partition_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
466 }
468 if($partition['status'] == "delete"){
469 prepare_to_save_FAI_object($partition_dn,array(),true);
470 $this->handle_post_events("remove");
471 }elseif($partition['status'] == "edited"){
472 prepare_to_save_FAI_object($partition_dn,$partition_attrs);
473 $this->handle_post_events("modify");
474 }elseif($partition['status']=="new"){
475 prepare_to_save_FAI_object($partition_dn,$partition_attrs);
476 $this->handle_post_events("add");
477 }
479 $this->handle_object_tagging($partition_dn, $this->gosaUnitTag);
480 }
481 }
482 $this->handle_post_events("add");
483 }
486 /* Return plugin informations for acl handling */
487 function plInfo()
488 {
489 return (array(
490 "plShortName" => _("Partition table"),
491 "plDescription" => _("FAI partition table"),
492 "plSelfModify" => FALSE,
493 "plDepends" => array(),
494 "plPriority" => 0,
495 "plSection" => array("administration"),
496 "plCategory" => array("fai"),
497 "plProvidedAcls" => array(
498 "cn" => _("Name"),
499 "FAIpartitionType" => _("Partition type"),
500 "FAIpartitionNr" => _("Partition no."),
501 "FAIfsType" => _("File system type"),
502 "FAImountPoint" => _("Mount point"),
503 "FAIpartitionSize" => _("Partition size"),
504 "FAImountOptions" => _("Mount options"),
505 "FAIfsOptions" => _("File system options"),
506 "FAIpartitionFlags" => _("Partition flags"))
507 ));
508 }
509 }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>