Code

68b5926c504018cce50b411efae7be7a7fa5c181
[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;
43   var $cut_paste_mode   = false;
45   var $CopyPasteVars  = array("SubObjects");
47   function faiHook ($config, $dn= NULL)
48   {
49     /* Load Attributes */
50     plugin::plugin ($config, $dn);
52     $this->acl ="#all#";
54     /* If "dn==new" we try to create a new entry
55      * Else we must read all objects from ldap which belong to this entry.
56      */
57     if($dn != "new"){
58       $this->dn =$dn;
59     
60       /* Set acls 
61        */
62       $ui   = get_userinfo();
63       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
64       $acli = get_module_permission($acl, "FAIclass", $this->dn);
65       $this->acl=$acli;
67       /* Get FAIstate
68        */
69       if(isset($this->attrs['FAIstate'][0])){
70         $this->FAIstate = $this->attrs['FAIstate'][0];
71       }
73       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
74        */
75       $ldap     = $this->config->get_ldap_link();
76       $ldap->cd ($this->dn);
77       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
79       while($object = $ldap->fetch()){
81         /* Set status for save management */
82         $objects = array();
83         $objects['status']      = "FreshLoaded";
84         $objects['dn']          = $object['dn'];
85         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
86         $this->SubObjects[$objects['cn']] = $objects;
87       }
88     }
89   }
92   /* Reload some attributes */
93   function get_object_attributes($object,$attributes)
94   {
95     $ldap = $this->config->get_ldap_link();
96     $ldap->cd($this->config->current['BASE']);
97     $ldap->cat($object['dn'],$attributes);
98     $tmp  = $ldap->fetch();
99   
100     foreach($attributes as $attrs){
101       if(isset($tmp[$attrs][0])){
102         $var = $tmp[$attrs][0];
104         /* Check if we must decode some attributes */
105         if(in_array_ics($attrs,$this->sub64coded)){
106           $var = base64_decode($var);
107         }
109         /*  check if this is a binary entry */
110         if(in_array_ics($attrs,$this->subBinary)){
111           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
112         }
114         /* Fix slashes */
115         $var = addslashes($var);
116         $object[$attrs] = $var;
117       }
118     }
119     return($object);
120   }
123   function getUsedFAItask($cn)
124   {
125     $ret = array();
126     foreach($this->SubObjects as $name => $class){
127       if($class['cn'] == $cn){
128         continue;
129       } 
130       if($class['status'] != "delete"){
131         $ret[$class['FAItask']] = $class['FAItask'];
132       }
133     }
134     return($ret);
135   }
138   function execute()
139   {
140     /* Call parent execute */
141     plugin::execute();
143     /* Fill templating stuff */
144     $smarty= get_smarty();
145     $display= "";
147     /* New Listhandling
148      */
149     $once = true;
150     foreach($_POST as $name => $value){
151       if(preg_match("/^editscript_/",$name)&&($once)){
152         $once = false;
153         $entry = preg_replace("/^editscript_/","",$name);
154         $entry = base64_decode(preg_replace("/_.*/","",$entry));
156         $obj  = $this->SubObjects[$entry];
157         if($obj['status'] == "FreshLoaded"){
158           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
159         }
161         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
162         $this->dialog->acl = $this->acl;
163         $_SESSION['objectinfo'] = $obj['dn'];
164         $this->dialog->parent = &$this;
165         $this->is_dialog=true;
166       }
167       if(preg_match("/^deletescript_/",$name)&&($once)){
168         $once = false;
169         $entry = preg_replace("/^deletescript_/","",$name);
170         $entry = base64_decode(preg_replace("/_.*/","",$entry));
172         $status = $this->SubObjects[$entry]['status'];
173         if($status == "edited" || $status == "FreshLoaded"){
174           $this->SubObjects[$entry]['status']= "delete";
175         }else{
176           unset($this->SubObjects[$entry]);
177         }
178       }
179     }
180     ///// Ende new list handling
182     /* Add new sub object */
183     if(isset($_POST['AddSubObject'])){
184       $this->dialog= new $this->subClassName($this->config,"new");
185       $this->dialog->acl = $this->acl;
186       $this->dialog->parent = &$this;
187       $this->is_dialog=true;
188     }
190     if($this->dn != "new"){
191       $_SESSION['objectinfo']= $this->dn;
192     }
194     /* Save Dialog */
195     if(isset($_POST['SaveSubObject'])){
197       /* Perform post check*/
198       $this->dialog->save_object();
200       /* Get messages */
201       $msgs = $this->dialog->check();
203       /* print errors */
204       if(count($msgs)>0){
205         foreach($msgs as $msg){
206           print_red($msg);
207         }
208       }else{
210         /* Get return object */
211         $obj = $this->dialog->save();
212         if(isset($obj['remove'])){
213           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
215           /* Depending on status, set new status */
216           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
217             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
218           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
219             unset($this->SubObjects[$obj['remove']['from']]);
220           }
221           $obj['status'] = "new";
222           $this->SubObjects[$obj['remove']['to']] = $obj;
223           unset($this->SubObjects[$obj['remove']['to']]['remove']);
224         }else{
225           if($obj['status'] == "FreshLoaded"){
226             $obj['status'] = "edited";
227           }
228           $this->SubObjects[$obj['cn']]=$obj;
229         }
230     
231         $this->is_dialog=false;
232         unset($this->dialog);
233         $this->dialog=NULL;
234       }
235     }
237     /* Sort entries */
238     $tmp = $keys = array();
239     foreach($this->SubObjects as $key => $entry){
240       $keys[$key]=$key;
241     }
242     natcasesort($keys);
243     foreach($keys as $key){
244       $tmp[$key]=$this->SubObjects[$key];
245     }
246     $this->SubObjects = $tmp;
248     /* Cancel Dialog */
249     if(isset($_POST['CancelSubObject'])){
250       $this->is_dialog=false; 
251       unset($this->dialog);
252       $this->dialog=NULL;
253     }
255     /* Print dialog if $this->dialog is set */
256     if($this->dialog){
257       $this->dialog->save_object();
258       $display = $this->dialog->execute();
259       return($display);
260     }
264     /* Divlist            added 28.02.2006
265        Containing FAIscripts
266      */
268     $divlist = new divSelectBox("FAIhooks");
269     $divlist->setHeight(400);
270     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
271       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
272       $img_remo = ""; 
273     }else{
274       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
275       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
276     }
278     foreach($this->getList(true) as $key => $name){
280       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
281         $down = "";
282       }else{
283         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
284           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
285           </a>";
286       }
288       $divlist->AddEntry(array( array("string"=>$name['name']),
289             array("string"=>$down , "attach" => "style='width:20px;'"),
290             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
291               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
292     }
293     $smarty->assign("Entry_divlist",$divlist->DrawList());
294     /* Divlist creation complete
295      */
297     $smarty->assign("SubObjects",$this->getList());
299     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
300      * If we post the escaped strings they will be escaped again
301      */
302     foreach($this->attributes as $attrs){
303       if(get_magic_quotes_gpc()){
304         $smarty->assign($attrs,stripslashes($this->$attrs));
305       }else{
306         $smarty->assign($attrs,($this->$attrs));
307       }
308     }
310     foreach($this->attributes as $attr){
311       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
312     }
314     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
315     return($display);
316   }
318   /* Generate listbox friendly SubObject list
319    */
320   function getList($use_dns=false){
321     $a_return=array();
322     foreach($this->SubObjects as $obj){
323       if($obj['status'] != "delete"){
324         if($use_dns){
325           if((isset($obj['description']))&&(!empty($obj['description']))){
326             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
327           }else{
328             $a_return[$obj['cn']]['name']= $obj['cn'];
329           }
330           $a_return[$obj['cn']]['dn']= $obj['dn'];
331         }else{
332           if((isset($obj['description']))&&(!empty($obj['description']))){
333             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
334           }else{
335             $a_return[$obj['cn']]= $obj['cn'];
336           }
337         }
338       }
339     }
340     return($a_return);
341   }
344   /* Delete me, and all my subtrees
345    */
346   function remove_from_parent()
347   {
348     $ldap = $this->config->get_ldap_link();
349     $ldap->cd ($this->dn);
350     $ldap->rmdir_recursive($this->dn);
351     show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed")); 
352     $this->handle_post_events("remove");    
353   }
356   /* Save data to object 
357    */
358   function save_object()
359   {
360     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
361       plugin::save_object();
362       foreach($this->attributes as $attrs){
363         if(isset($_POST[$attrs])){
364           $this->$attrs = $_POST[$attrs];
365         }
366       }
367     }
368   }
371   /* Check supplied data */
372   function check()
373   {
374     /* Call common method to give check the hook */
375     $message= plugin::check();
377     /* If this is a new script, check if a script with this name already exists */
378     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
380       /* Check if current name is already used for fai scripts in selected release */
381       $dn = 'cn='.$this->cn.",ou=hooks,".$this->release;
382       $ldap = $this->config->get_ldap_link();
383       $ldap->cat($dn);
384       if($ldap->count()){
386         $r =convert_department_dn($this->release);;
387         $message[] = sprintf(_("Can't insert a fai hook named '%s' in '%s' there is already a hook with the given name."),$this->cn,$r);
388       }
389     }
390     return ($message);
391   }
394   /* Save to LDAP */
395   function save()
396   {
397     plugin::save();
399     $ldap = $this->config->get_ldap_link();
401     /* Copy & Paste : Ensure that FAIstate is copied too */
402     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
403       $this->attrs['FAIstate'] = $this->FAIstate;
404     }
406     $ldap->cat($this->dn,array("objectClass"));
407     if($ldap->count()!=0){     
408       /* Write FAIscript to ldap*/ 
409       $ldap->cd($this->dn);
410       $this->cleanup();
411       $ldap->modify ($this->attrs); 
413     }else{
414       /* Write FAIscript to ldap*/ 
415       $ldap->cd($this->config->current['BASE']);
416       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
417       $ldap->cd($this->dn);
418       $ldap->add($this->attrs);
419     }
420     show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed")); 
422     /* Do object tagging */
423     $this->handle_object_tagging();
425     $ldap->cd($this->dn);
427     /* Prepare FAIscriptEntry to write it to ldap
428      * First sort array.
429      *  Because we must delete old entries first.
430      * After deletion, we perform add and modify 
431      */
432     $Objects = array();
435     /* We do not need to save untouched objects */
436     foreach($this->SubObjects as $name => $obj){
437       if($obj['status'] == "FreshLoaded"){
438         if($this->copy_paste_mode){
439           $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
440           $this->SubObjects[$name]['status'] = "new";
441         }else{
442           unset($this->SubObjects[$name]);
443         }
444       } 
445     }
447     /* Add objects that must be removed first.*/
448     foreach($this->SubObjects as $name => $obj){
449       if($obj['status'] == "delete"){
450         $Objects[$name] = $obj; 
451       }
452     }
454     /* Add objects to add/modify */
455     foreach($this->SubObjects as $name => $obj){
456       if($obj['status'] != "delete"){
457         $Objects[$name] = $obj; 
458       }
459     }
461     /* Walk through list of objects */
462     foreach($Objects as $name => $obj){
464       /* Encode attribtues if required */
465       foreach($this->sub64coded as $codeIt){
466         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
467       }
469       /* Create ldap entry */
470       $tmp = array();
471       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
472       foreach($attributes as $attrs){
473   
474         if(empty($obj[$attrs])){
475           $obj[$attrs] = array();
476         }
477         if(!is_array($obj[$attrs])){
478           $tmp[$attrs] = stripslashes($obj[$attrs]);
479         }else{
480           $tmp[$attrs] = $obj[$attrs];
481         }
482       }    
484       $tmp['objectClass'] = $this->subClasses;
486       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
488       if($obj['status']=="new"){
489         $ldap->cat($sub_dn,array("objectClass"));
490         if($ldap->count()){
491           $obj['status']="edited";
492         }
493       }
495       /* Check if gosaAdministrativeUnitTag is required as object class */
496       if($obj['status'] == "edited"){
497         $ldap->cat($sub_dn,array("objectClass"));
498         $attrs = $ldap->fetch();
499         if(isset($attrs['objectClass'])){
500           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
501             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
502           }
503         }
504       }
506       if($obj['status'] == "delete"){
507         $ldap->cd($sub_dn);
508         $ldap->rmdir_recursive($sub_dn);
509         $this->handle_post_events("remove");
510         show_ldap_error($ldap->get_error(), _("Removing FAI hook failed")); 
511       }elseif($obj['status'] == "edited"){
512         $ldap->cd($sub_dn);
513         $this->cleanup();
514         $ldap->modify ($tmp); 
515         $this->handle_post_events("modify");
516         show_ldap_error($ldap->get_error(), _("Saving FAI hook failed")); 
517       }elseif($obj['status']=="new"){
518         if($tmp['description']==array()){
519           unset($tmp['description']);
520         }
521         $ldap->cd($this->config->current['BASE']);
522         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
523         $ldap->cd($sub_dn);
524         $ldap->add($tmp); 
525         $this->handle_post_events("add");
526         show_ldap_error($ldap->get_error(), _("Saving FAI hook failed")); 
527       }
528       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
529     }
530   }
531   
532   /* return copy & paste dialog
533    */
534   function getCopyDialog()
535   {
536     /* Ask for cn */
537     $smarty = get_smarty();
538     $smarty->assign("cn" ,$this->cn);
539     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
540     $ret = array();
541     $ret['string'] = $str;
542     $ret['status'] = "";
543     return($ret);
544   }
546   /* Get posted cn */
547   function saveCopyDialog()
548   {
549     if(isset($_POST['cn'])){
550       $this->cn = $_POST['cn'];
551     }
552   }
555 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
556 ?>