Code

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