Code

Fixed categorie saving
[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   function faiHook ($config, $dn= NULL)
38   {
39     /* Load Attributes */
40     plugin::plugin ($config, $dn);
42     /* If "dn==new" we try to create a new entry
43      * Else we must read all objects from ldap which belong to this entry.
44      */
45     if($dn != "new"){
46       $this->dn =$dn;
48       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
49        */
50       $ldap     = $this->config->get_ldap_link();
51       $ldap->cd ($this->dn);
52       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
54       while($object = $ldap->fetch()){
55         /* Set status for save management */
56   
57         foreach($this->subAttributes as $attrs){
58           if(!isset($object[$attrs][0])){
59             $this->SubObjects[$object['cn'][0]][$attrs]="";
60           }else{
61             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
62           }
63         }
64      
65         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
66         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
68         foreach($this->sub64coded as $codeIt){
69           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
70         }
72         foreach($this->subAttributes as $attrs){
73           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
74         }
75         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes($this->readBinary("FAIscript",$object['dn']));
76       }
77       ksort($this->SubObjects);
78     }
79   }
81   function getUsedFAItask($cn)
82   {
83     $ret = array();
84     foreach($this->SubObjects as $name => $class){
85       if($class['cn'] == $cn){
86         continue;
87       } 
88       if($class['status'] != "delete"){
89         $ret[$class['FAItask']] = $class['FAItask'];
90       }
91     }
92     return($ret);
93   }
95   function execute()
96   {
97         /* Call parent execute */
98         plugin::execute();
100     /* Fill templating stuff */
101     $smarty= get_smarty();
102     $display= "";
104     /* Add new sub object */
105     if(isset($_POST['AddSubObject'])){
106       $this->dialog= new $this->subClassName($this->config,"new");
107       $this->dialog->parent = &$this;
108       $this->is_dialog=true;
109     }
110   
111     if($this->dn != "new"){
112       $_SESSION['objectinfo']= $this->dn;
113     }
114     /* Edit selected Sub Object */
115     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
116       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
117       $this->dialog->parent = &$this;
118       $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
119       $this->is_dialog=true;
120     }
121     
122     /* Remove Sub object */
123     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
124       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
125         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
126       }else{
127         unset($this->SubObjects[$_POST['SubObject']]);
128       }
129     }
131     /* Save Dialog */
132     if(isset($_POST['SaveSubObject'])){
134       /* Perform post check*/
135       $this->dialog->save_object();
137       /* Get messages */
138       $msgs = $this->dialog->check();
140       /* print errors */
141       if(count($msgs)>0){
142         foreach($msgs as $msg){
143           print_red($msg);
144         }
145       }else{
147         /* Get return object */
148         $obj = $this->dialog->save();
149         if(isset($obj['remove'])){
151           /* Depending on status, set new status */
152           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
153             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
154           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
155             unset($this->SubObjects[$obj['remove']['from']]);
156           }
157           $obj['status'] = "new";
158           $this->SubObjects[$obj['remove']['to']] = $obj;
159           unset($this->SubObjects[$obj['remove']['to']]['remove']);
160         }else{
161           $this->SubObjects[$obj['cn']]=$obj;
162         }
163         $this->is_dialog=false;
164         unset($this->dialog);
165         $this->dialog=NULL;
166         ksort($this->SubObjects);
167       }
168     }
170     /* Cancel Dialog */
171     if(isset($_POST['CancelSubObject'])){
172       $this->is_dialog=false; 
173       unset($this->dialog);
174       $this->dialog=NULL;
175     }
177     /* Print dialog if $this->dialog is set */
178     if($this->dialog){
179       $this->dialog->save_object();
180       $display = $this->dialog->execute();
181       return($display);
182     }
184     $smarty->assign("SubObjects",$this->getList());
185     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
187      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
188      * If we post the escaped strings they will be escaped again
189      */
190     foreach($this->attributes as $attrs){
191       if(get_magic_quotes_gpc()){
192         $smarty->assign($attrs,stripslashes($this->$attrs));
193       }else{
194         $smarty->assign($attrs,($this->$attrs));
195       }
196     }
199     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
200     return($display);
201   }
203   /* Generate listbox friendly SubObject list
204   */
205   function getList(){
206     $a_return=array();
207     foreach($this->SubObjects as $obj){
208       if($obj['status'] != "delete"){
209         if((isset($obj['description']))&&(!empty($obj['description']))){
210           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
211         }else{
212           $a_return[$obj['cn']]= $obj['cn'];
213         }
214       }
215     }
216     return($a_return);
217   }
219   /* Delete me, and all my subtrees
220    */
221   function remove_from_parent()
222   {
223     $ldap = $this->config->get_ldap_link();
224     $ldap->cd ($this->dn);
225     $ldap->rmdir_recursive($this->dn);
226     $this->handle_post_events("remove");    
227   }
230   /* Save data to object 
231    */
232   function save_object()
233   {
234     if(isset($_POST['FAIhook_posted'])){
235       plugin::save_object();
236       foreach($this->attributes as $attrs){
237         if(isset($_POST[$attrs])){
238           $this->$attrs = $_POST[$attrs];
239         }
240       }
241     }
242   }
245   /* Check supplied data */
246   function check()
247   {
248     $message= array();
249     return ($message);
250   }
253   /* Save to LDAP */
254   function save()
255   {
256     plugin::save();
257  
258     $ldap = $this->config->get_ldap_link();
260     $ldap->cat($this->dn);
261     if($ldap->count()!=0){     
262       /* Write FAIscript to ldap*/ 
263       $ldap->cd($this->dn);
264       $ldap->modify($this->attrs);
265     }else{
266       /* Write FAIscript to ldap*/ 
267       $ldap->cd($this->config->current['BASE']);
268       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
269       $ldap->cd($this->dn);
270       $ldap->add($this->attrs);
271     }
272     show_ldap_error($ldap->get_error()); 
273  
274     /* Prepare FAIscriptEntry to write it to ldap
275      * First sort array.
276      *  Because we must delete old entries first.
277      * After deletion, we perform add and modify 
278      */
279     $Objects = array();
280     foreach($this->SubObjects as $name => $obj){
281       if($obj['status'] == "delete"){
282         $Objects[$name] = $obj; 
283       }
284     }
285     foreach($this->SubObjects as $name => $obj){
286       if($obj['status'] != "delete"){
287         $Objects[$name] = $obj; 
288       }
289     }
291     foreach($Objects as $name => $obj){
293       foreach($this->sub64coded as $codeIt){
294         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
295       }
297       $tmp = array();
298       foreach($this->subAttributes as $attrs){
299         if(empty($obj[$attrs])){
300           $obj[$attrs] = array();
301         }
302         if(!is_array($obj[$attrs])){
303           $tmp[$attrs] = stripslashes($obj[$attrs]);
304         }else{
305           $tmp[$attrs] = $obj[$attrs];
306         }
307       }    
309       $tmp['objectClass'] = $this->subClasses;
311       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
313       if($obj['status']=="new"){
314         $ldap->cat($sub_dn);
315         if($ldap->count()){
316           $obj['status']="modify";
317         }
318       }
319  
320       if($obj['status'] == "delete"){
321         $ldap->cd($sub_dn);
322         $ldap->rmdir_recursive($sub_dn);
323         $this->handle_post_events("remove");
324       }elseif($obj['status'] == "edited"){
325         $ldap->cd($sub_dn);
326         $ldap->modify($tmp);
327         $this->handle_post_events("modify");
328       }elseif($obj['status']=="new"){
329         if($tmp['description']==array()){
330           unset($tmp['description']);
331         }
332         $ldap->cd($this->config->current['BASE']);
333         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
334         $ldap->cd($sub_dn);
335         $ldap->add($tmp); 
336         $this->handle_post_events("add");
337       }
338       show_ldap_error($ldap->get_error()); 
339     }
340   }
341   
342   function readBinary($attr,$dn){
343     $Data  ="";
344     $ds= ldap_connect($this->config->current['SERVER']);
345     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
346     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
347       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
348       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
349     }
351     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
352       ldap_start_tls($ds);
353     }
355     $r  = ldap_bind($ds);
356     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
358     if ($sr) {
359       $ei=ldap_first_entry($ds, $sr);
360       if ($ei) {
361         if ($info = ldap_get_values_len($ds, $ei, $attr)){
362           $Data= $info[0];
363         }
364       }
365     }
367     /* close conncetion */
368     ldap_unbind($ds);
369     return($Data);
370   }
375 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
376 ?>