Code

0de6e7508d28dcc5318983ff52da452f4f34c0b1
[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()
82   {
83     $ret = array();
84     foreach($this->SubObjects as $name => $class){
85       if($class['status'] != "delete"){
86         $ret[$class['FAItask']] = $class['FAItask'];
87       }
88     }
89     return($ret);
90   }
92   function execute()
93   {
94         /* Call parent execute */
95         plugin::execute();
97     /* Fill templating stuff */
98     $smarty= get_smarty();
99     $display= "";
101     /* Add new sub object */
102     if(isset($_POST['AddSubObject'])){
103       $this->dialog= new $this->subClassName($this->config,"new");
104       $this->dialog->parent = &$this;
105       $this->is_dialog=true;
106     }
107   
108     if($this->dn != "new"){
109       $_SESSION['objectinfo']= $this->dn;
110     }
111     /* Edit selected Sub Object */
112     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
113       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
114       $this->dialog->parent = &$this;
115       $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
116       $this->is_dialog=true;
117     }
118     
119     /* Remove Sub object */
120     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
121       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
122         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
123       }else{
124         unset($this->SubObjects[$_POST['SubObject']]);
125       }
126     }
128     /* Save Dialog */
129     if(isset($_POST['SaveSubObject'])){
131       /* Perform post check*/
132       $this->dialog->save_object();
134       /* Get messages */
135       $msgs = $this->dialog->check();
137       /* print errors */
138       if(count($msgs)>0){
139         foreach($msgs as $msg){
140           print_red($msg);
141         }
142       }else{
144         /* Get return object */
145         $obj = $this->dialog->save();
146         if(isset($obj['remove'])){
148           /* Depending on status, set new status */
149           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
150             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
151           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
152             unset($this->SubObjects[$obj['remove']['from']]);
153           }
154           $obj['status'] = "new";
155           $this->SubObjects[$obj['remove']['to']] = $obj;
156           unset($this->SubObjects[$obj['remove']['to']]['remove']);
157         }else{
158           $this->SubObjects[$obj['cn']]=$obj;
159         }
160         $this->is_dialog=false;
161         unset($this->dialog);
162         $this->dialog=NULL;
163         ksort($this->SubObjects);
164       }
165     }
167     /* Cancel Dialog */
168     if(isset($_POST['CancelSubObject'])){
169       $this->is_dialog=false; 
170       unset($this->dialog);
171       $this->dialog=NULL;
172     }
174     /* Print dialog if $this->dialog is set */
175     if($this->dialog){
176       $this->dialog->save_object();
177       $display = $this->dialog->execute();
178       return($display);
179     }
181     $smarty->assign("SubObjects",$this->getList());
182     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
184      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
185      * If we post the escaped strings they will be escaped again
186      */
187     foreach($this->attributes as $attrs){
188       if(get_magic_quotes_gpc()){
189         $smarty->assign($attrs,stripslashes($this->$attrs));
190       }else{
191         $smarty->assign($attrs,($this->$attrs));
192       }
193     }
196     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
197     return($display);
198   }
200   /* Generate listbox friendly SubObject list
201   */
202   function getList(){
203     $a_return=array();
204     foreach($this->SubObjects as $obj){
205       if($obj['status'] != "delete"){
206         if((isset($obj['description']))&&(!empty($obj['description']))){
207           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
208         }else{
209           $a_return[$obj['cn']]= $obj['cn'];
210         }
211       }
212     }
213     return($a_return);
214   }
216   /* Delete me, and all my subtrees
217    */
218   function remove_from_parent()
219   {
220     $ldap = $this->config->get_ldap_link();
221     $ldap->cd ($this->dn);
222     $ldap->rmdir_recursive($this->dn);
223     $this->handle_post_events("remove");    
224   }
227   /* Save data to object 
228    */
229   function save_object()
230   {
231     if(isset($_POST['FAIhook_posted'])){
232       plugin::save_object();
233       foreach($this->attributes as $attrs){
234         if(isset($_POST[$attrs])){
235           $this->$attrs = $_POST[$attrs];
236         }
237       }
238     }
239   }
242   /* Check supplied data */
243   function check()
244   {
245     $message= array();
246     return ($message);
247   }
250   /* Save to LDAP */
251   function save()
252   {
253     plugin::save();
254  
255     $ldap = $this->config->get_ldap_link();
257     $ldap->cat($this->dn);
258     if($ldap->count()!=0){     
259       /* Write FAIscript to ldap*/ 
260       $ldap->cd($this->dn);
261       $ldap->modify($this->attrs);
262     }else{
263       /* Write FAIscript to ldap*/ 
264       $ldap->cd($this->config->current['BASE']);
265       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
266       $ldap->cd($this->dn);
267       $ldap->add($this->attrs);
268     }
269     show_ldap_error($ldap->get_error()); 
270  
271     /* Prepare FAIscriptEntry to write it to ldap
272      * First sort array.
273      *  Because we must delete old entries first.
274      * After deletion, we perform add and modify 
275      */
276     $Objects = array();
277     foreach($this->SubObjects as $name => $obj){
278       if($obj['status'] == "delete"){
279         $Objects[$name] = $obj; 
280       }
281     }
282     foreach($this->SubObjects as $name => $obj){
283       if($obj['status'] != "delete"){
284         $Objects[$name] = $obj; 
285       }
286     }
288     foreach($Objects as $name => $obj){
290       foreach($this->sub64coded as $codeIt){
291         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
292       }
294       $tmp = array();
295       foreach($this->subAttributes as $attrs){
296         if(empty($obj[$attrs])){
297           $obj[$attrs] = array();
298         }
299         if(!is_array($obj[$attrs])){
300           $tmp[$attrs] = stripslashes($obj[$attrs]);
301         }else{
302           $tmp[$attrs] = $obj[$attrs];
303         }
304       }    
306       $tmp['objectClass'] = $this->subClasses;
308       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
310       if($obj['status']=="new"){
311         $ldap->cat($sub_dn);
312         if($ldap->count()){
313           $obj['status']="modify";
314         }
315       }
316  
317       if($obj['status'] == "delete"){
318         $ldap->cd($sub_dn);
319         $ldap->rmdir_recursive($sub_dn);
320         $this->handle_post_events("remove");
321       }elseif($obj['status'] == "edited"){
322         $ldap->cd($sub_dn);
323         $ldap->modify($tmp);
324         $this->handle_post_events("modify");
325       }elseif($obj['status']=="new"){
326         if($tmp['description']==array()){
327           unset($tmp['description']);
328         }
329         $ldap->cd($this->config->current['BASE']);
330         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
331         $ldap->cd($sub_dn);
332         $ldap->add($tmp); 
333         $this->handle_post_events("add");
334       }
335       show_ldap_error($ldap->get_error()); 
336     }
337   }
338   
339   function readBinary($attr,$dn){
340     $Data  ="";
341     $ds= ldap_connect($this->config->current['SERVER']);
342     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
343     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
344       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
345       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
346     }
348     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
349       ldap_start_tls($ds);
350     }
352     $r  = ldap_bind($ds);
353     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
355     if ($sr) {
356       $ei=ldap_first_entry($ds, $sr);
357       if ($ei) {
358         if ($info = ldap_get_values_len($ds, $ei, $attr)){
359           $Data= $info[0];
360         }
361       }
362     }
364     /* close conncetion */
365     ldap_unbind($ds);
366     return($Data);
367   }
372 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
373 ?>