Code

Added "cn" to Copy & Paste dialog for copied fai objects.
[gosa.git] / plugins / admin / fai / class_faiHook.inc
1 <?php
3 class faiHook 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;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIhook");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIhookEntry";
21   var $subClasses       = array("top","FAIclass","FAIhookEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiHookEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItask"); 
28   var $sub_Load_Later   = array("FAIscript"); 
29   var $sub64coded       = array();
30   var $subBinary        = array("FAIscript");
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIstate         = "";  
40   var $base             = "";
41   var $release          = "";
42   var $copy_paste_mode  = false;
44   var $CopyPasteVars  = array("SubObjects","FAIstate");
46   function faiHook ($config, $dn= NULL)
47   {
48     /* Load Attributes */
49     plugin::plugin ($config, $dn);
51     $this->acl ="#all#";
53     /* If "dn==new" we try to create a new entry
54      * Else we must read all objects from ldap which belong to this entry.
55      */
56     if($dn != "new"){
57       $this->dn =$dn;
58     
59       /* Set acls 
60        */
61       $ui   = get_userinfo();
62       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
63       $acli = get_module_permission($acl, "FAIclass", $this->dn);
64       $this->acl=$acli;
66       /* Get FAIstate
67        */
68       if(isset($this->attrs['FAIstate'][0])){
69         $this->FAIstate = $this->attrs['FAIstate'][0];
70       }
72       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
73        */
74       $ldap     = $this->config->get_ldap_link();
75       $ldap->cd ($this->dn);
76       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
78       while($object = $ldap->fetch()){
80         /* Set status for save management */
81         $objects = array();
82         $objects['status']      = "FreshLoaded";
83         $objects['dn']          = $object['dn'];
84         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
85         $this->SubObjects[$objects['cn']] = $objects;
86       }
87     }
88   }
91   /* Reload some attributes */
92   function get_object_attributes($object,$attributes)
93   {
94     $ldap = $this->config->get_ldap_link();
95     $ldap->cd($this->config->current['BASE']);
96     $ldap->cat($object['dn'],$attributes);
97     $tmp  = $ldap->fetch();
98   
99     foreach($attributes as $attrs){
100       if(isset($tmp[$attrs][0])){
101         $var = $tmp[$attrs][0];
103         /* Check if we must decode some attributes */
104         if(in_array_ics($attrs,$this->sub64coded)){
105           $var = base64_decode($var);
106         }
108         /*  check if this is a binary entry */
109         if(in_array_ics($attrs,$this->subBinary)){
110           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
111         }
113         /* Fix slashes */
114         $var = addslashes($var);
115         $object[$attrs] = $var;
116       }
117     }
118     return($object);
119   }
122   function getUsedFAItask($cn)
123   {
124     $ret = array();
125     foreach($this->SubObjects as $name => $class){
126       if($class['cn'] == $cn){
127         continue;
128       } 
129       if($class['status'] != "delete"){
130         $ret[$class['FAItask']] = $class['FAItask'];
131       }
132     }
133     return($ret);
134   }
137   function execute()
138   {
139     /* Call parent execute */
140     plugin::execute();
142     /* Fill templating stuff */
143     $smarty= get_smarty();
144     $display= "";
146     /* New Listhandling
147      */
148     $once = true;
149     foreach($_POST as $name => $value){
150       if(preg_match("/^editscript_/",$name)&&($once)){
151         $once = false;
152         $entry = preg_replace("/^editscript_/","",$name);
153         $entry = base64_decode(preg_replace("/_.*/","",$entry));
155         $obj  = $this->SubObjects[$entry];
156         if($obj['status'] == "FreshLoaded"){
157           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
158         }
160         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
161         $this->dialog->acl = $this->acl;
162         $_SESSION['objectinfo'] = $obj['dn'];
163         $this->dialog->parent = &$this;
164         $this->is_dialog=true;
165       }
166       if(preg_match("/^deletescript_/",$name)&&($once)){
167         $once = false;
168         $entry = preg_replace("/^deletescript_/","",$name);
169         $entry = base64_decode(preg_replace("/_.*/","",$entry));
171         $status = $this->SubObjects[$entry]['status'];
172         if($status == "edited" || $status == "FreshLoaded"){
173           $this->SubObjects[$entry]['status']= "delete";
174         }else{
175           unset($this->SubObjects[$entry]);
176         }
177       }
178     }
179     ///// Ende new list handling
181     /* Add new sub object */
182     if(isset($_POST['AddSubObject'])){
183       $this->dialog= new $this->subClassName($this->config,"new");
184       $this->dialog->acl = $this->acl;
185       $this->dialog->parent = &$this;
186       $this->is_dialog=true;
187     }
189     if($this->dn != "new"){
190       $_SESSION['objectinfo']= $this->dn;
191     }
193     /* Save Dialog */
194     if(isset($_POST['SaveSubObject'])){
196       /* Perform post check*/
197       $this->dialog->save_object();
199       /* Get messages */
200       $msgs = $this->dialog->check();
202       /* print errors */
203       if(count($msgs)>0){
204         foreach($msgs as $msg){
205           print_red($msg);
206         }
207       }else{
209         /* Get return object */
210         $obj = $this->dialog->save();
211         if(isset($obj['remove'])){
212           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
214           /* Depending on status, set new status */
215           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
216             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
217           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
218             unset($this->SubObjects[$obj['remove']['from']]);
219           }
220           $obj['status'] = "new";
221           $this->SubObjects[$obj['remove']['to']] = $obj;
222           unset($this->SubObjects[$obj['remove']['to']]['remove']);
223         }else{
224           if($obj['status'] == "FreshLoaded"){
225             $obj['status'] = "edited";
226           }
227           $this->SubObjects[$obj['cn']]=$obj;
228         }
229     
230         $this->is_dialog=false;
231         unset($this->dialog);
232         $this->dialog=NULL;
233       }
234     }
236     /* Sort entries */
237     $tmp = $keys = array();
238     foreach($this->SubObjects as $key => $entry){
239       $keys[$key]=$key;
240     }
241     natcasesort($keys);
242     foreach($keys as $key){
243       $tmp[$key]=$this->SubObjects[$key];
244     }
245     $this->SubObjects = $tmp;
247     /* Cancel Dialog */
248     if(isset($_POST['CancelSubObject'])){
249       $this->is_dialog=false; 
250       unset($this->dialog);
251       $this->dialog=NULL;
252     }
254     /* Print dialog if $this->dialog is set */
255     if($this->dialog){
256       $this->dialog->save_object();
257       $display = $this->dialog->execute();
258       return($display);
259     }
263     /* Divlist            added 28.02.2006
264        Containing FAIscripts
265      */
267     $divlist = new divSelectBox("FAIhooks");
268     $divlist->setHeight(400);
269     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
270       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
271       $img_remo = ""; 
272     }else{
273       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
274       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
275     }
277     foreach($this->getList(true) as $key => $name){
279       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
280         $down = "";
281       }else{
282         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
283           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
284           </a>";
285       }
287       $divlist->AddEntry(array( array("string"=>$name['name']),
288             array("string"=>$down , "attach" => "style='width:20px;'"),
289             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
290               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
291     }
292     $smarty->assign("Entry_divlist",$divlist->DrawList());
293     /* Divlist creation complete
294      */
296     $smarty->assign("SubObjects",$this->getList());
298     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
299      * If we post the escaped strings they will be escaped again
300      */
301     foreach($this->attributes as $attrs){
302       if(get_magic_quotes_gpc()){
303         $smarty->assign($attrs,stripslashes($this->$attrs));
304       }else{
305         $smarty->assign($attrs,($this->$attrs));
306       }
307     }
309     foreach($this->attributes as $attr){
310       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
311     }
313     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
314     return($display);
315   }
317   /* Generate listbox friendly SubObject list
318    */
319   function getList($use_dns=false){
320     $a_return=array();
321     foreach($this->SubObjects as $obj){
322       if($obj['status'] != "delete"){
323         if($use_dns){
324           if((isset($obj['description']))&&(!empty($obj['description']))){
325             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
326           }else{
327             $a_return[$obj['cn']]['name']= $obj['cn'];
328           }
329           $a_return[$obj['cn']]['dn']= $obj['dn'];
330         }else{
331           if((isset($obj['description']))&&(!empty($obj['description']))){
332             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
333           }else{
334             $a_return[$obj['cn']]= $obj['cn'];
335           }
336         }
337       }
338     }
339     return($a_return);
340   }
343   /* Delete me, and all my subtrees
344    */
345   function remove_from_parent()
346   {
347     $ldap = $this->config->get_ldap_link();
348     $ldap->cd ($this->dn);
349     $ldap->rmdir_recursive($this->dn);
350     show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed")); 
351     $this->handle_post_events("remove");    
352   }
355   /* Save data to object 
356    */
357   function save_object()
358   {
359     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
360       plugin::save_object();
361       foreach($this->attributes as $attrs){
362         if(isset($_POST[$attrs])){
363           $this->$attrs = $_POST[$attrs];
364         }
365       }
366     }
367   }
370   /* Check supplied data */
371   function check()
372   {
373     /* Call common method to give check the hook */
374     $message= plugin::check();
376     /* If this is a new script, check if a script with this name already exists */
377     if(!empty($this->release) && $this->copy_paste_mode){
379       /* Check if current name is already used for fai scripts in selected release */
380       $dn = 'cn='.$this->cn.",ou=hooks,".$this->release;
381       $ldap = $this->config->get_ldap_link();
382       $ldap->cat($dn);
383       if($ldap->count()){
385         $r =convert_department_dn($this->release);;
386         $message[] = sprintf(_("Can't insert a fai hook named '%s' in '%s' there is already a hook with the given name."),$this->cn,$r);
387       }
388     }
389     return ($message);
390   }
393   /* Save to LDAP */
394   function save()
395   {
396     plugin::save();
398     $ldap = $this->config->get_ldap_link();
400     /* Copy & Paste : Ensure that FAIstate is copied too */
401     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
402       $this->attrs['FAIstate'] = $this->FAIstate;
403     }
405     $ldap->cat($this->dn,array("objectClass"));
406     if($ldap->count()!=0){     
407       /* Write FAIscript to ldap*/ 
408       $ldap->cd($this->dn);
409       $this->cleanup();
410       $ldap->modify ($this->attrs); 
412     }else{
413       /* Write FAIscript to ldap*/ 
414       $ldap->cd($this->config->current['BASE']);
415       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
416       $ldap->cd($this->dn);
417       $ldap->add($this->attrs);
418     }
419     show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed")); 
421     /* Do object tagging */
422     $this->handle_object_tagging();
424     $ldap->cd($this->dn);
426     /* Prepare FAIscriptEntry to write it to ldap
427      * First sort array.
428      *  Because we must delete old entries first.
429      * After deletion, we perform add and modify 
430      */
431     $Objects = array();
434     /* We do not need to save untouched objects */
435     foreach($this->SubObjects as $name => $obj){
436       if($obj['status'] == "FreshLoaded"){
437         if($this->copy_paste_mode){
438           $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
439           $this->SubObjects[$name]['status'] = "new";
440         }else{
441           unset($this->SubObjects[$name]);
442         }
443       } 
444     }
446     /* Add objects that must be removed first.*/
447     foreach($this->SubObjects as $name => $obj){
448       if($obj['status'] == "delete"){
449         $Objects[$name] = $obj; 
450       }
451     }
453     /* Add objects to add/modify */
454     foreach($this->SubObjects as $name => $obj){
455       if($obj['status'] != "delete"){
456         $Objects[$name] = $obj; 
457       }
458     }
460     /* Walk through list of objects */
461     foreach($Objects as $name => $obj){
463       /* Encode attribtues if required */
464       foreach($this->sub64coded as $codeIt){
465         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
466       }
468       /* Create ldap entry */
469       $tmp = array();
470       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
471       foreach($attributes as $attrs){
472   
473         if(empty($obj[$attrs])){
474           $obj[$attrs] = array();
475         }
476         if(!is_array($obj[$attrs])){
477           $tmp[$attrs] = stripslashes($obj[$attrs]);
478         }else{
479           $tmp[$attrs] = $obj[$attrs];
480         }
481       }    
483       $tmp['objectClass'] = $this->subClasses;
485       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
487       if($obj['status']=="new"){
488         $ldap->cat($sub_dn,array("objectClass"));
489         if($ldap->count()){
490           $obj['status']="edited";
491         }
492       }
494       /* Check if gosaAdministrativeUnitTag is required as object class */
495       if($obj['status'] == "edited"){
496         $ldap->cat($sub_dn,array("objectClass"));
497         $attrs = $ldap->fetch();
498         if(isset($attrs['objectClass'])){
499           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
500             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
501           }
502         }
503       }
505       if($obj['status'] == "delete"){
506         $ldap->cd($sub_dn);
507         $ldap->rmdir_recursive($sub_dn);
508         $this->handle_post_events("remove");
509         show_ldap_error($ldap->get_error(), _("Removing FAI hook failed")); 
510       }elseif($obj['status'] == "edited"){
511         $ldap->cd($sub_dn);
512         $this->cleanup();
513         $ldap->modify ($tmp); 
514         $this->handle_post_events("modify");
515         show_ldap_error($ldap->get_error(), _("Saving FAI hook failed")); 
516       }elseif($obj['status']=="new"){
517         if($tmp['description']==array()){
518           unset($tmp['description']);
519         }
520         $ldap->cd($this->config->current['BASE']);
521         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
522         $ldap->cd($sub_dn);
523         $ldap->add($tmp); 
524         $this->handle_post_events("add");
525         show_ldap_error($ldap->get_error(), _("Saving FAI hook failed")); 
526       }
527       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
528     }
529   }
530   
531   /* return copy & paste dialog
532    */
533   function getCopyDialog()
534   {
535     /* Ask for cn */
536     $smarty = get_smarty();
537     $smarty->assign("cn" ,$this->cn);
538     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
539     $ret = array();
540     $ret['string'] = $str;
541     $ret['status'] = "";
542     return($ret);
543   }
545   /* Get posted cn */
546   function saveCopyDialog()
547   {
548     if(isset($_POST['cn'])){
549       $this->cn = $_POST['cn'];
550     }
551   }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>