Code

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