Code

f8d4c6b8b20e235aa37634cbe0f68f9f77b5d3e1
[gosa.git] / class_faiTemplate.inc
1 <?php
3 class faiTemplate 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","FAItemplate");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAItemplateEntry";
21   var $subClasses       = array("top","FAIclass","FAItemplateEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiTemplateEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItemplateFile","FAItemplatePath","FAIowner","FAImode"); 
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 faiTemplate ($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         }
66         foreach($this->sub64coded as $codeIt){
67           $this->SubObjects[$object['cn'][0]][$codeIt]= base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
68         }       
70         $this->SubObjects[$object['cn'][0]]['FAItemplateFile']= $this->readBinary("FAItemplateFile",$object['dn']);
71        
72         $this->SubObjects[$object['cn'][0]]['status']= "edited";
73         $this->SubObjects[$object['cn'][0]]['dn']= $object['dn'];
74       }
75       ksort($this->SubObjects);
76     }
77   }
79   function execute()
80   {
81         /* Call parent execute */
82         plugin::execute();
84     /* Fill templating stuff */
85     $smarty= get_smarty();
86     $display= "";
88     /* Add new sub object */
89     if(isset($_POST['AddSubObject'])){
90       $this->dialog= new $this->subClassName($this->config,"new");
91       $this->is_dialog=true;
92     }
94     if($this->dn != "new"){
95       $_SESSION['objectinfo']= $this->dn;
96     }
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'])){
117       $this->dialog->save_object();
118       $msgs = $this->dialog->check();
119       if(count($msgs)>0){
120         foreach($msgs as $msg){
121           print_red($msg);
122         }
123       }else{
124         $obj = $this->dialog->save();
125         if(isset($obj['remove'])){
126           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
127             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
128           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
129             unset($this->SubObjects[$obj['remove']['from']]);
130           }
131           $obj['status'] = "new";
132           $this->SubObjects[$obj['remove']['to']] = $obj;
133           unset($this->SubObjects[$obj['remove']['to']]['remove']);
134         }else{
135           $this->SubObjects[$obj['cn']]=$obj;
136         }
137         $this->is_dialog=false;
138         unset($this->dialog);
139         $this->dialog=NULL;
140         ksort($this->SubObjects);
141       }
142     }
144     /* Cancel Dialog */
145     if(isset($_POST['CancelSubObject'])){
146       $this->is_dialog=false; 
147       unset($this->dialog);
148       $this->dialog=NULL;
149     }
151     /* Print dialog if $this->dialog is set */
152     if($this->dialog){
153       $this->dialog->save_object();
154       $display = $this->dialog->execute();
155       return($display);
156     }
158     $smarty->assign("SubObjects",$this->getList());
159     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
161      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
162      * If we post the escaped strings they will be escaped again
163      */
164     foreach($this->attributes as $attrs){
165       if(get_magic_quotes_gpc()){
166         $smarty->assign($attrs,stripslashes($this->$attrs));
167       }else{
168         $smarty->assign($attrs,($this->$attrs));
169       }
170     }
173     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
174     return($display);
175   }
177   /* Generate listbox friendly SubObject list
178   */
179   function getList(){
180     $a_return=array();
181     foreach($this->SubObjects as $obj){
182       if($obj['status'] != "delete"){
183       
184         if((isset($obj['description']))&&(!empty($obj['description']))){
185           if(strlen($obj['description']) > 60){
186             $obj['description'] = substr($obj['description'],0,60)."...";
187           }
188           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
189         }else{
190           $a_return[$obj['cn']]= $obj['cn'];
191         }
192       }
193     }
194     return($a_return);
195   }
197   /* Delete me, and all my subtrees
198    */
199   function remove_from_parent()
200   {
201     $ldap = $this->config->get_ldap_link();
202     $ldap->cd ($this->dn);
203     $ldap->rmdir_recursive($this->dn);
204     $this->handle_post_events("remove");    
205   }
208   /* Save data to object 
209    */
210   function save_object()
211   {
212     if(isset($_POST['FAItemplate_posted'])){
213       plugin::save_object();
214       foreach($this->attributes as $attrs){
215         if(isset($_POST[$attrs])){
216           $this->$attrs = $_POST[$attrs];
217         } 
218       }
219     }
220   }
223   /* Check supplied data */
224   function check()
225   {
226     $message= array();
227     return ($message);
228   }
231   /* Save to LDAP */
232   function save()
233   {
234     plugin::save();
235  
236     $ldap = $this->config->get_ldap_link();
237   
238     $ldap->cat($this->dn);
239     if($ldap->count()!=0){
240       /* Write FAIscript to ldap*/
241       $ldap->cd($this->dn);
242       $ldap->modify($this->attrs);
243     }else{
244       /* Write FAIscript to ldap*/
245       $ldap->cd($this->config->current['BASE']);
246       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
247       $ldap->cd($this->dn);
248       $ldap->add($this->attrs);
249     }
250     show_ldap_error($ldap->get_error());
251  
252     /* Prepare FAIscriptEntry to write it to ldap
253      * First sort array.
254      *  Because we must delete old entries first.
255      * After deletion, we perform add and modify 
256      */
257     $Objects = array();
258     foreach($this->SubObjects as $name => $obj){
259       if($obj['status'] == "delete"){
260         $Objects[$name] = $obj; 
261       }
262     }
263     foreach($this->SubObjects as $name => $obj){
264       if($obj['status'] != "delete"){
265         $Objects[$name] = $obj; 
266       }
267     }
269     foreach($Objects as $name => $obj){
271       foreach($this->sub64coded as $codeIt){
272         $obj[$codeIt]=base64_encode($obj[$codeIt]);
273       }
274       $tmp = array();
275       foreach($this->subAttributes as $attrs){
276         if(empty($obj[$attrs])){
277           $obj[$attrs] = array();
278         }
279         $tmp[$attrs] = $obj[$attrs];
280       }    
281         
282       $tmp['objectClass'] = $this->subClasses;
284       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
286       if($obj['status']=="new"){
287         $ldap->cat($sub_dn);
288         if($ldap->count()){
289           $obj['status']="modify";
290         }
291       }
293       if($obj['status'] == "delete"){
294         $ldap->cd($sub_dn);
295         $ldap->rmdir_recursive($sub_dn);
296         $this->handle_post_events("remove");
297       }elseif($obj['status'] == "edited"){
298         $ldap->cd($sub_dn);
299         $ldap->modify($tmp);
300         $this->handle_post_events("modify");
301       }elseif($obj['status']=="new"){
303         if($tmp['description']==array()){
304           unset($tmp['description']);
305         }
306         $ldap->cd($this->config->current['BASE']);
307         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
308         $ldap->cd($sub_dn);
309         $ldap->add($tmp); 
310         $this->handle_post_events("add");
311       }
312       show_ldap_error($ldap->get_error()); 
313     }
314   }
316   function readBinary($attr,$dn){
317     $Data  ="";
318     $ds= ldap_connect($this->config->current['SERVER']);
319     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
320     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
321       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
322       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
323     }
325     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
326       ldap_start_tls($ds);
327     }
329     $r  = ldap_bind($ds);
330     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
332     if ($sr) {
333       $ei=ldap_first_entry($ds, $sr);
334       if ($ei) {
335         if ($info = ldap_get_values_len($ds, $ei, $attr)){
336           $Data= $info[0];
337         }
338       }
339     }
341     /* close conncetion */
342     ldap_unbind($ds);
343     return($Data);
344   }
347 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
348 ?>