Code

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