Code

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