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 if($_SESSION['faifilter']['branch'] == "main"){
310 $use_dn = $this->dn;
311 }
313 prepare_to_save_FAI_object($use_dn,array(),true);
315 foreach($this->disks as $disk){
317 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
318 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $disk_dn);
319 if($_SESSION['faifilter']['branch'] == "main"){
320 $use_dn = $disk_dn;
321 }
322 prepare_to_save_FAI_object($use_dn,array(),true);
324 foreach($disk['partitions'] as $key => $partition){
326 $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;
327 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $partition_dn);
328 if($_SESSION['faifilter']['branch'] == "main"){
329 $use_dn = $disk_dn;
330 }
331 prepare_to_save_FAI_object($use_dn,array(),true);
332 }
333 }
334 }
337 /* Save data to object
338 */
339 function save_object()
340 {
341 if($this->FAIstate == "freeze") return;
342 plugin::save_object();
343 foreach($this->attributes as $attrs){
344 if(isset($_POST[$attrs])){
345 $this->$attrs = $_POST[$attrs];
346 }
347 }
348 }
351 /* Check supplied data */
352 function check()
353 {
354 /* Call common method to give check the hook */
355 $message= plugin::check();
357 return ($message);
358 }
361 /* Save to LDAP */
362 function save()
363 {
365 plugin::save();
366 /* Save current settings.
367 * 1 : We must save the partition table, with its description and cn
368 * 2 : Append Disk with cn and description.
369 * 3 : Save partitions for each disk
370 */
372 $ldap = $this->config->get_ldap_link();
374 prepare_to_save_FAI_object($this->dn,$this->attrs);
375 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/partition table with dn '%s' failed."),$this->dn));
377 /* Do object tagging */
378 $this->handle_object_tagging();
380 /* Sort entries, because we must delete entries with status="delete" first */
381 $order = array();
382 foreach($this->disks as $key => $disk){
383 if($disk['status'] == "delete"){
384 $order[$key] = $disk;
385 }
386 }
387 foreach($this->disks as $key => $disk){
388 if($disk['status'] != "delete"){
389 $order[$key] = $disk;
390 }
391 }
393 /* Append all disks to ldap */
394 foreach($order as $cn=>$disk){
395 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
396 $disk_attrs['cn'] = $disk['cn'];
397 $disk_attrs['description'] = $disk['description'];
398 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
400 if($disk['status']=="new"){
401 $ldap->cat($disk_dn,array("objectClass"));
402 if($ldap->count()){
403 $disk['status']="edited";
404 }
405 }
407 /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for disks */
408 $ldap->cat($disk_dn,array("objectClass"));
409 $attrs = $ldap->fetch();
410 if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
411 $disk_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
412 }
414 if($disk['status'] == "delete"){
415 prepare_to_save_FAI_object($disk_dn,array(),true);
416 $this->handle_post_events("remove");
417 }elseif($disk['status'] == "edited"){
418 prepare_to_save_FAI_object($disk_dn,$disk_attrs);
419 $this->handle_post_events("modify");
420 }elseif($disk['status']=="new"){
421 prepare_to_save_FAI_object($disk_dn,$disk_attrs);
422 $this->handle_post_events("add");
423 }
425 $this->handle_object_tagging($disk_dn, $this->gosaUnitTag);
427 if($disk['status']!="delete")
428 /* Add all partitions */
429 foreach($disk['partitions'] as $key => $partition){
430 $partition_attrs = array();
432 foreach($partition as $key => $value){
433 if(!empty($value)){
434 $partition_attrs[$key]=$value;
435 }else{
436 unset($partition_attrs[$key]);
437 }
438 }
440 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
441 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
442 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
444 unset($partition_attrs['status']);
445 unset($partition_attrs['old_cn']);
447 if($partition['status']=="new"){
448 $ldap->cat($partition_dn,array("objectClass"));
449 if($ldap->count()){
450 $partition['status']="edited";
451 }
452 }
454 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
455 $partition_attrs['FAImountPoint']="swap";
456 }
458 /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for partitions */
459 $ldap->cat($partition_dn,array("objectClass"));
460 $attrs = $ldap->fetch();
461 if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
462 $partition_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
463 }
465 if($partition['status'] == "delete"){
466 prepare_to_save_FAI_object($partition_dn,array(),true);
467 $this->handle_post_events("remove");
468 }elseif($partition['status'] == "edited"){
469 prepare_to_save_FAI_object($partition_dn,$partition_attrs);
470 $this->handle_post_events("modify");
471 }elseif($partition['status']=="new"){
472 prepare_to_save_FAI_object($partition_dn,$partition_attrs);
473 $this->handle_post_events("add");
474 }
476 $this->handle_object_tagging($partition_dn, $this->gosaUnitTag);
477 }
478 }
479 $this->handle_post_events("add");
480 }
481 }
483 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
484 ?>