Code

Removed debug output
[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","FAIscript"); 
28   var $sub64coded       = array();
30   /* Specific attributes */
31   var $cn               = "";       // The class name for this object
32   var $description      = "";       // The description for this set of partitions
33   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
34   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
35   var $SubObjects       = array();  // All leafobjects of this object
37   var $FAIstate         = "";  
39   function faiHook ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl ="#all#";
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      */
49     if($dn != "new"){
50       $this->dn =$dn;
51     
52       /* Set acls 
53        */
54       $ui   = get_userinfo();
55       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
56       $acli = get_module_permission($acl, "FAIclass", $this->dn);
57       $this->acl=$acli;
59       /* Get FAIstate
60        */
61       if(isset($this->attrs['FAIstate'][0])){
62         $this->FAIstate = $this->attrs['FAIstate'][0];
63       }
65       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
66        */
67       $ldap     = $this->config->get_ldap_link();
68       $ldap->cd ($this->dn);
69       $attrs_to_search = $this->subAttributes;
70       $attrs_to_search[] = "FAIstate";
71       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
73       while($object = $ldap->fetch()){
75         /* Skip objects, that are tagged as removed */
76         if(isset($object['FAIstate'][0])){
77           if(preg_match("/removed$/",$object['FAIstate'][0])){
78             continue;
79           }
80         }
82         /* Set status for save management */
83         foreach($this->subAttributes as $attrs){
84           if(!isset($object[$attrs][0])){
85             $this->SubObjects[$object['cn'][0]][$attrs]="";
86           }else{
87             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
88           }
89         }
91         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
92         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
94         foreach($this->sub64coded as $codeIt){
95           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
96         }
98         foreach($this->subAttributes as $attrs){
99           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
100         }
101         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes( $ldap->get_attribute($object['dn'], "FAIscript",$r_array=0));
102       }
103     }
104   }
106   function getUsedFAItask($cn)
107   {
108     $ret = array();
109     foreach($this->SubObjects as $name => $class){
110       if($class['cn'] == $cn){
111         continue;
112       } 
113       if($class['status'] != "delete"){
114         $ret[$class['FAItask']] = $class['FAItask'];
115       }
116     }
117     return($ret);
118   }
120   function execute()
121   {
122     /* Call parent execute */
123     plugin::execute();
125     /* Fill templating stuff */
126     $smarty= get_smarty();
127     $display= "";
129     /* New Listhandling
130      */
131     $once = true;
132     foreach($_POST as $name => $value){
133       if(preg_match("/^editscript_/",$name)&&($once)){
134         $once = false;
135         $entry = preg_replace("/^editscript_/","",$name);
136         $entry = base64_decode(preg_replace("/_.*/","",$entry));
137         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
138         $this->dialog->acl = $this->acl;
139         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
140         $this->dialog->parent = &$this;
141         $this->is_dialog=true;
142       }
143       if(preg_match("/^deletescript_/",$name)&&($once)){
144         $once = false;
145         $entry = preg_replace("/^deletescript_/","",$name);
146         $entry = base64_decode(preg_replace("/_.*/","",$entry));
147         if($this->SubObjects[$entry]['status'] == "edited"){
148           $this->SubObjects[$entry]['status']= "delete";
149         }else{
150           unset($this->SubObjects[$entry]);
151         }
152       }
153     }
154     ///// Ende new list handling
156     /* Add new sub object */
157     if(isset($_POST['AddSubObject'])){
158       $this->dialog= new $this->subClassName($this->config,"new");
159       $this->dialog->acl = $this->acl;
160       $this->dialog->parent = &$this;
161       $this->is_dialog=true;
162     }
164     if($this->dn != "new"){
165       $_SESSION['objectinfo']= $this->dn;
166     }
167     /* Edit selected Sub Object */
168     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
169       $hook = $_POST['SubObject'][0];
171       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$hook]);
172       $this->dialog->acl = $this->acl;
173       $this->dialog->parent = &$this;
174       $_SESSION['objectinfo'] = $this->SubObjects[$hook]['dn'];
175       $this->is_dialog=true;
176     }
178     /* Remove Sub object */
179     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
180       foreach($_POST['SubObject'] as $hook){
181         if($this->SubObjects[$hook]['status'] == "edited"){
182           $this->SubObjects[$hook]['status']= "delete";
183         }else{
184           unset($this->SubObjects[$hook]);
185         }
186       }
187     }
189     /* Save Dialog */
190     if(isset($_POST['SaveSubObject'])){
192       /* Perform post check*/
193       $this->dialog->save_object();
195       /* Get messages */
196       $msgs = $this->dialog->check();
198       /* print errors */
199       if(count($msgs)>0){
200         foreach($msgs as $msg){
201           print_red($msg);
202         }
203       }else{
205         /* Get return object */
206         $obj = $this->dialog->save();
207         if(isset($obj['remove'])){
209           /* Depending on status, set new status */
210           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
211             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
212           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
213             unset($this->SubObjects[$obj['remove']['from']]);
214           }
215           $obj['status'] = "new";
216           $this->SubObjects[$obj['remove']['to']] = $obj;
217           unset($this->SubObjects[$obj['remove']['to']]['remove']);
218         }else{
219           $this->SubObjects[$obj['cn']]=$obj;
220         }
221         $this->is_dialog=false;
222         unset($this->dialog);
223         $this->dialog=NULL;
224       }
225     }
227     /* Sort entries */
228     $tmp = $keys = array();
229     foreach($this->SubObjects as $key => $entry){
230       $keys[$key]=$key;
231     }
232     natcasesort($keys);
233     foreach($keys as $key){
234       $tmp[$key]=$this->SubObjects[$key];
235     }
236     $this->SubObjects = $tmp;
238     /* Cancel Dialog */
239     if(isset($_POST['CancelSubObject'])){
240       $this->is_dialog=false; 
241       unset($this->dialog);
242       $this->dialog=NULL;
243     }
245     /* Print dialog if $this->dialog is set */
246     if($this->dialog){
247       $this->dialog->save_object();
248       $display = $this->dialog->execute();
249       return($display);
250     }
254     /* Divlist            added 28.02.2006
255        Containing FAIscripts
256      */
258     $divlist = new divSelectBox("FAIhooks");
259     $divlist->setHeight(400);
260     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
261       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
262       $img_remo = ""; 
263     }else{
264       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
265       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
266     }
268     foreach($this->getList(true) as $key => $name){
270       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
271         $down = "";
272       }else{
273         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
274           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
275           </a>";
276       }
278       $divlist->AddEntry(array( array("string"=>$name['name']),
279             array("string"=>$down , "attach" => "style='width:20px;'"),
280             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
281               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
282     }
283     $smarty->assign("Entry_divlist",$divlist->DrawList());
284     /* Divlist creation complete
285      */
287     $smarty->assign("SubObjects",$this->getList());
289     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
290      * If we post the escaped strings they will be escaped again
291      */
292     foreach($this->attributes as $attrs){
293       if(get_magic_quotes_gpc()){
294         $smarty->assign($attrs,stripslashes($this->$attrs));
295       }else{
296         $smarty->assign($attrs,($this->$attrs));
297       }
298     }
300     foreach($this->attributes as $attr){
301       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
302     }
304     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
305     return($display);
306   }
308   /* Generate listbox friendly SubObject list
309    */
310   function getList($use_dns=false){
311     $a_return=array();
312     foreach($this->SubObjects as $obj){
313       if($obj['status'] != "delete"){
314         if($use_dns){
315           if((isset($obj['description']))&&(!empty($obj['description']))){
316             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
317           }else{
318             $a_return[$obj['cn']]['name']= $obj['cn'];
319           }
320           $a_return[$obj['cn']]['dn']= $obj['dn'];
321         }else{
322           if((isset($obj['description']))&&(!empty($obj['description']))){
323             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
324           }else{
325             $a_return[$obj['cn']]= $obj['cn'];
326           }
327         }
328       }
329     }
330     return($a_return);
331   }
334   /* Delete me, and all my subtrees
335    */
336   function remove_from_parent()
337   {
338     $ldap = $this->config->get_ldap_link();
339     $ldap->cd ($this->dn);
341     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
342     if($_SESSION['faifilter']['branch'] == "main"){
343       $use_dn = $this->dn;
344     }
346     prepare_to_save_FAI_object($use_dn,array(),true);
348     foreach($this->SubObjects as $name => $obj){
349       $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
350       if($_SESSION['faifilter']['branch'] == "main"){
351         $use_dn = $obj['dn'];
352       }
353       prepare_to_save_FAI_object($use_dn,array(),true);
354     }
355     $this->handle_post_events("remove");    
356   }
359   /* Save data to object 
360    */
361   function save_object()
362   {
363     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
364       plugin::save_object();
365       foreach($this->attributes as $attrs){
366         if(isset($_POST[$attrs])){
367           $this->$attrs = $_POST[$attrs];
368         }
369       }
370     }
371   }
374   /* Check supplied data */
375   function check()
376   {
377     /* Call common method to give check the hook */
378     $message= plugin::check();
380     return ($message);
381   }
384   /* Save to LDAP */
385   function save()
386   {
387     plugin::save();
389     $ldap = $this->config->get_ldap_link();
391     prepare_to_save_FAI_object($this->dn,$this->attrs);
392     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
394     /* Do object tagging */
395     $this->handle_object_tagging();
397     $ldap->cd($this->dn);
399     /* Prepare FAIscriptEntry to write it to ldap
400      * First sort array.
401      *  Because we must delete old entries first.
402      * After deletion, we perform add and modify 
403      */
404     $Objects = array();
405     foreach($this->SubObjects as $name => $obj){
406       if($obj['status'] == "delete"){
407         $Objects[$name] = $obj; 
408       }
409     }
410     foreach($this->SubObjects as $name => $obj){
411       if($obj['status'] != "delete"){
412         $Objects[$name] = $obj; 
413       }
414     }
416     foreach($Objects as $name => $obj){
418       foreach($this->sub64coded as $codeIt){
419         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
420       }
422       $tmp = array();
423       foreach($this->subAttributes as $attrs){
424         if(empty($obj[$attrs])){
425           $obj[$attrs] = array();
426         }
427         if(!is_array($obj[$attrs])){
428           $tmp[$attrs] = stripslashes($obj[$attrs]);
429         }else{
430           $tmp[$attrs] = $obj[$attrs];
431         }
432       }    
434       $tmp['objectClass'] = $this->subClasses;
436       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
438       if($obj['status']=="new"){
439         $ldap->cat($sub_dn,array("objectClass"));
440         if($ldap->count()){
441           $obj['status']="edited";
442         }
443       }
445       /* Check if gosaAdministrativeUnitTag is required as object class */
446       if($obj['status'] == "edited"){
447         $ldap->cat($sub_dn,array("objectClass"));
448         $attrs = $ldap->fetch();
449         if(isset($attrs['objectClass'])){
450           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
451             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
452           }
453         }
454       }
456       if($obj['status'] == "delete"){
457         prepare_to_save_FAI_object($sub_dn,array(),true);
458         $this->handle_post_events("remove");
459       }elseif($obj['status'] == "edited"){
460         prepare_to_save_FAI_object($sub_dn,$tmp);
461         $this->handle_post_events("modify");
462       }elseif($obj['status']=="new"){
463         prepare_to_save_FAI_object($sub_dn,$tmp);
464         $this->handle_post_events("add");
465       }
466       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
467     }
468   }
471 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
472 ?>