Code

08859d81615720686d424ca00568ba3bcabd378e
[gosa.git] / plugins / admin / fai / class_faiScript.inc
1 <?php
3 class faiScript 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","FAIscript");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIscriptEntry";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiScriptEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIpriority","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 faiScript ($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      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45      */
46     if($dn != "new"){
47       $this->dn =$dn;
49       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50        */
51       $ldap     = $this->config->get_ldap_link();
52       $ldap->cd ($this->dn);
53       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
55       while($object = $ldap->fetch()){
56         /* Set status for save management */
57   
58         foreach($this->subAttributes as $attrs){
59           if(!isset($object[$attrs][0])){
60             $this->SubObjects[$object['cn'][0]][$attrs]="";
61           }else{
62             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
63           }
64         }
65      
66         foreach($this->sub64coded as $codeIt){
67           $this->SubObjects[$object['cn'][0]][$codeIt]=(base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]));
68         }
70         foreach($this->subAttributes as $attrs){
71           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
72         }
74         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes ($this->readBinary("FAIscript",$object['dn']));
75  
76         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
77         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
78       }
79     }
80   }
82   function execute()
83   {
84         /* Call parent execute */
85         plugin::execute();
87     /* Fill templating stuff */
88     $smarty= get_smarty();
89     $display= "";
91     /* Add new sub object */
92     if(isset($_POST['AddSubObject'])){
93       $this->dialog= new $this->subClassName($this->config,"new");
94       $this->is_dialog=true;
95     }
97     if($this->dn != "new"){
98       $_SESSION['objectinfo']= $this->dn;
99     }
102     /* New Listhandling 
103      */
104     $once = true;
105     foreach($_POST as $name => $value){
106       if(preg_match("/^editscript_/",$name)&&($once)){
107         $once = false;
108         $entry = preg_replace("/^editscript_/","",$name);
109         $entry = base64_decode(preg_replace("/_.*/","",$entry));
110         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
111         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
112         $this->is_dialog=true;
113       }
114       if(preg_match("/^deletescript_/",$name)&&($once)){
115         $once = false;
116         $entry = preg_replace("/^deletescript_/","",$name);
117         $entry = base64_decode(preg_replace("/_.*/","",$entry));
118         if($this->SubObjects[$entry]['status'] == "edited"){
119           $this->SubObjects[$entry]['status']= "delete";
120         }else{
121           unset($this->SubObjects[$entry]);
122         }
123       }
124     }
125     ///// Ende new list handling
126     
128     /* Edit selected Sub Object */
129     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
130       $script = $_POST['SubObject'][0];
131     
132       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
133       $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
134       $this->is_dialog=true;
135     }
136     
137     /* Remove Sub object */
138     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
140       foreach($_POST['SubObject'] as $script){
141         if($this->SubObjects[$script]['status'] == "edited"){
142           $this->SubObjects[$script]['status']= "delete";
143         }else{
144           unset($this->SubObjects[$script]);
145         }
146       }
147     }
149     /* Save Dialog */
150     if(isset($_POST['SaveSubObject'])){
151       $this->dialog->save_object();
152       $msgs = $this->dialog->check();
153       if(count($msgs)>0){
154         foreach($msgs as $msg){
155           print_red($msg);
156         }
157       }else{
158         $obj = $this->dialog->save();
159         if(isset($obj['remove'])){
160           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
161             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
162           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
163             unset($this->SubObjects[$obj['remove']['from']]);
164           }
165           $obj['status'] = "new";
166           $this->SubObjects[$obj['remove']['to']] = $obj;
167           unset($this->SubObjects[$obj['remove']['to']]['remove']);
168         }else{
169           $this->SubObjects[$obj['cn']]=$obj;
170         }
171         $this->is_dialog=false;
172         unset($this->dialog);
173         $this->dialog=NULL;
174       }
175     }
177     /* Sort entries */
178     $tmp = $keys = array();
179     foreach($this->SubObjects as $key => $entry){
180       $keys[$key]=$key;
181     }
182     natcasesort($keys);
183     foreach($keys as $key){
184       $tmp[$key]=$this->SubObjects[$key];
185     }
186     $this->SubObjects = $tmp;
188     /* Cancel Dialog */
189     if(isset($_POST['CancelSubObject'])){
190       $this->is_dialog=false; 
191       unset($this->dialog);
192       $this->dialog=NULL;
193     }
195     /* Print dialog if $this->dialog is set */
196     if($this->dialog){
197       $this->dialog->save_object();
198       $display = $this->dialog->execute();
199       return($display);
200     }
202     /* Divlist            added 23.02.2006 
203        Containing FAIscripts 
204      */
205     $divlist = new divSelectBox("FAIscripts");
206     $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
207     $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
209     foreach($this->getList(true) as $key => $name){
210       
211       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
212         $down = "";
213       }else{
214         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' target='_blank'>
215                       <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
216                  </a>"; 
217       } 
219       $divlist->AddEntry(array( array("string"=>$name['name']),
220                                 array("string"=>$down , "attach" => "style='width:20px;'"),
221                                 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
222                                       "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
223     }
224     $smarty->assign("Entry_divlist",$divlist->DrawList());
225     /* Divlist creation complete
226      */
228     $smarty->assign("SubObjects",$this->getList());
229     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
230      
231     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
232     * If we post the escaped strings they will be escaped again
233     */
234     foreach($this->attributes as $attrs){
235       if(get_magic_quotes_gpc()){
236         $smarty->assign($attrs,stripslashes($this->$attrs));
237       }else{
238         $smarty->assign($attrs,($this->$attrs));
239       }
240     }
242     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
243     return($display);
244   }
246   /* Generate listbox friendly SubObject list
247   */
248   function getList($use_dns=false){
249     $a_return=array();
250     foreach($this->SubObjects as $obj){
251       if($obj['status'] != "delete"){
252         if($use_dns){
253           if((isset($obj['description']))&&(!empty($obj['description']))){
254             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
255           }else{
256             $a_return[$obj['cn']]['name']= $obj['cn'];
257           }
258           $a_return[$obj['cn']]['dn']= $obj['dn'];
259         }else{
260           if((isset($obj['description']))&&(!empty($obj['description']))){
261             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
262           }else{
263             $a_return[$obj['cn']]= $obj['cn'];
264           }
265         }
266       }
267     }
268     return($a_return);
269   }
271   /* Delete me, and all my subtrees
272    */
273   function remove_from_parent()
274   {
275     $ldap = $this->config->get_ldap_link();
276     $ldap->cd ($this->dn);
277     $ldap->rmdir_recursive($this->dn);
278     $this->handle_post_events("remove");    
279   }
282   /* Save data to object 
283    */
284   function save_object()
285   {
286     if(isset($_POST['FAIscript_posted'])){
287       plugin::save_object();
288       foreach($this->attributes as $attrs){
289         if(isset($_POST[$attrs])){
290           $this->$attrs = $_POST[$attrs];
291         }
292       }
293     }
294   }
297   /* Check supplied data */
298   function check()
299   {
300     $message= array();
301     return ($message);
302   }
305   /* Save to LDAP */
306   function save()
307   {
308     plugin::save();
309  
310     $ldap = $this->config->get_ldap_link();
311   
312     $ldap->cat($this->dn);
313     if($ldap->count()!=0){
314       /* Write FAIscript to ldap*/
315       $ldap->cd($this->dn);
316       $this->cleanup();
317 $ldap->modify ($this->attrs); 
319     }else{
320       /* Write FAIscript to ldap*/
321       $ldap->cd($this->config->current['BASE']);
322       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
323       $ldap->cd($this->dn);
324       $ldap->add($this->attrs);
325     }
326     show_ldap_error($ldap->get_error());
327  
328     /* Prepare FAIscriptEntry to write it to ldap
329      * First sort array.
330      *  Because we must delete old entries first.
331      * After deletion, we perform add and modify 
332      */
333     $Objects = array();
334     foreach($this->SubObjects as $name => $obj){
335       if($obj['status'] == "delete"){
336         $Objects[$name] = $obj; 
337       }
338     }
339     foreach($this->SubObjects as $name => $obj){
340       if($obj['status'] != "delete"){
341         $Objects[$name] = $obj; 
342       }
343     }
345     foreach($Objects as $name => $obj){
347       foreach($this->sub64coded as $codeIt){
348         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
349       }
351       $tmp = array();
352       foreach($this->subAttributes as $attrs){
353         if(empty($obj[$attrs])){
354           $obj[$attrs] = array();
355         }
356         if(!is_array($obj[$attrs])){
357           $tmp[$attrs] = stripslashes($obj[$attrs]);
358         }else{
359           $tmp[$attrs] = $obj[$attrs];
360         }
361       }    
362         
363       $tmp['objectClass'] = $this->subClasses;
365       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
367       if($obj['status']=="new"){
368         $ldap->cat($sub_dn);
369         if($ldap->count()){
370           $obj['status']="modify";
371         }
372       }
374       if(empty($tmp['FAIpriority'])){
375         $tmp['FAIpriority']  ="0";
376       }
377   
378       if($obj['status'] == "delete"){
379         $ldap->cd($sub_dn);
380         $ldap->rmdir_recursive($sub_dn);
381         $this->handle_post_events("remove");
382       }elseif($obj['status'] == "edited"){
383         $ldap->cd($sub_dn);
384         $this->cleanup();
385 $ldap->modify ($tmp); 
387         $this->handle_post_events("modify");
388       }elseif($obj['status']=="new"){
389         if($tmp['description']==array()){
390           unset($tmp['description']);
391         }
392         if($tmp['FAIscript']==array()){
393           $tmp['FAIscript']=" ";
394         }
395         $ldap->cd($this->config->current['BASE']);
396         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
397         $ldap->cd($sub_dn);
398         $ldap->add($tmp); 
399         $this->handle_post_events("add");
400       }
401       show_ldap_error($ldap->get_error()); 
402     }
403   }
406   function readBinary($attr,$dn){
407     $Data  ="";
408     $ds= ldap_connect($this->config->current['SERVER']);
409     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
410     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
411       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
412       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
413     }
415     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
416       ldap_start_tls($ds);
417     }
419     $r  = ldap_bind($ds);
420     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
422     if ($sr) {
423       $ei=ldap_first_entry($ds, $sr);
424       if ($ei) {
425         if ($info = ldap_get_values_len($ds, $ei, $attr)){
426           $Data= $info[0];
427         }
428       }
429     }
431     /* close conncetion */
432     ldap_unbind($ds);
433     return($Data);
434   }
439 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
440 ?>