Code

Disable type field, if there are cartrigdes assigned.
[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     }
76   }
78   function execute()
79   {
80         /* Call parent execute */
81         plugin::execute();
83     /* Fill templating stuff */
84     $smarty= get_smarty();
85     $display= "";
87     /* Add new sub object */
88     if(isset($_POST['AddSubObject'])){
89       $this->dialog= new $this->subClassName($this->config,"new");
90       $this->is_dialog=true;
91     }
93     if($this->dn != "new"){
94       $_SESSION['objectinfo']= $this->dn;
95     }
98     /* Edit selected Sub Object */
99     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
101       $temp = $_POST['SubObject'][0];
102     
103       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$temp]);
104       $_SESSION['objectinfo'] = $this->SubObjects[$temp]['dn'];
105       $this->is_dialog=true;
106     }
107     
108     /* Remove Sub object */
109     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
110       foreach($_POST['SubObject'] as $temp){
111         if($this->SubObjects[$temp]['status'] == "edited"){
112           $this->SubObjects[$temp]['status']= "delete";
113         }else{
114           unset($this->SubObjects[$temp]);
115         }
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     }
147     /* Sort entries */
148     $tmp = $keys = array();
149     foreach($this->SubObjects as $key => $entry){
150       $keys[$key]=$key;
151     }
152     natcasesort($keys);
153     foreach($keys as $key){
154       $tmp[$key]=$this->SubObjects[$key];
155     }
156     $this->SubObjects = $tmp;
158     /* Cancel Dialog */
159     if(isset($_POST['CancelSubObject'])){
160       $this->is_dialog=false; 
161       unset($this->dialog);
162       $this->dialog=NULL;
163     }
165     /* Print dialog if $this->dialog is set */
166     if($this->dialog){
167       $this->dialog->save_object();
168       $display = $this->dialog->execute();
169       return($display);
170     }
172     $smarty->assign("SubObjects",$this->getList());
173     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
175      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
176      * If we post the escaped strings they will be escaped again
177      */
178     foreach($this->attributes as $attrs){
179       if(get_magic_quotes_gpc()){
180         $smarty->assign($attrs,stripslashes($this->$attrs));
181       }else{
182         $smarty->assign($attrs,($this->$attrs));
183       }
184     }
187     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
188     return($display);
189   }
191   /* Generate listbox friendly SubObject list
192   */
193   function getList(){
194     $a_return=array();
195     foreach($this->SubObjects as $obj){
196       if($obj['status'] != "delete"){
197       
198         if((isset($obj['description']))&&(!empty($obj['description']))){
199           if(strlen($obj['description']) > 40){
200             $obj['description'] = substr($obj['description'],0,40)."...";
201           }
202           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
203         }else{
204           $a_return[$obj['cn']]= $obj['cn'];
205         }
206       }
207     }
208     return($a_return);
209   }
211   /* Delete me, and all my subtrees
212    */
213   function remove_from_parent()
214   {
215     $ldap = $this->config->get_ldap_link();
216     $ldap->cd ($this->dn);
217     $ldap->rmdir_recursive($this->dn);
218     $this->handle_post_events("remove");    
219   }
222   /* Save data to object 
223    */
224   function save_object()
225   {
226     if(isset($_POST['FAItemplate_posted'])){
227       plugin::save_object();
228       foreach($this->attributes as $attrs){
229         if(isset($_POST[$attrs])){
230           $this->$attrs = $_POST[$attrs];
231         } 
232       }
233     }
234   }
237   /* Check supplied data */
238   function check()
239   {
240     $message= array();
241     return ($message);
242   }
245   /* Save to LDAP */
246   function save()
247   {
248     plugin::save();
249  
250     $ldap = $this->config->get_ldap_link();
251   
252     $ldap->cat($this->dn);
253     if($ldap->count()!=0){
254       /* Write FAIscript to ldap*/
255       $ldap->cd($this->dn);
256       $ldap->modify($this->attrs);
257     }else{
258       /* Write FAIscript to ldap*/
259       $ldap->cd($this->config->current['BASE']);
260       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
261       $ldap->cd($this->dn);
262       $ldap->add($this->attrs);
263     }
264     show_ldap_error($ldap->get_error());
265  
266     /* Prepare FAIscriptEntry to write it to ldap
267      * First sort array.
268      *  Because we must delete old entries first.
269      * After deletion, we perform add and modify 
270      */
271     $Objects = array();
272     foreach($this->SubObjects as $name => $obj){
273       if($obj['status'] == "delete"){
274         $Objects[$name] = $obj; 
275       }
276     }
277     foreach($this->SubObjects as $name => $obj){
278       if($obj['status'] != "delete"){
279         $Objects[$name] = $obj; 
280       }
281     }
283     foreach($Objects as $name => $obj){
285       foreach($this->sub64coded as $codeIt){
286         $obj[$codeIt]=base64_encode($obj[$codeIt]);
287       }
288       $tmp = array();
289       foreach($this->subAttributes as $attrs){
290         if(empty($obj[$attrs])){
291           $obj[$attrs] = array();
292         }
293         $tmp[$attrs] = $obj[$attrs];
294       }    
295         
296       $tmp['objectClass'] = $this->subClasses;
298       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
300       if($obj['status']=="new"){
301         $ldap->cat($sub_dn);
302         if($ldap->count()){
303           $obj['status']="modify";
304         }
305       }
307       if($obj['status'] == "delete"){
308         $ldap->cd($sub_dn);
309         $ldap->rmdir_recursive($sub_dn);
310         $this->handle_post_events("remove");
311       }elseif($obj['status'] == "edited"){
312         $ldap->cd($sub_dn);
313         $ldap->modify($tmp);
314         $this->handle_post_events("modify");
315       }elseif($obj['status']=="new"){
317         if($tmp['description']==array()){
318           unset($tmp['description']);
319         }
320         $ldap->cd($this->config->current['BASE']);
321         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
322         $ldap->cd($sub_dn);
323         $ldap->add($tmp); 
324         $this->handle_post_events("add");
325       }
326       show_ldap_error($ldap->get_error()); 
327     }
328   }
330   function readBinary($attr,$dn){
331     $Data  ="";
332     $ds= ldap_connect($this->config->current['SERVER']);
333     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
334     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
335       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
336       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
337     }
339     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
340       ldap_start_tls($ds);
341     }
343     $r  = ldap_bind($ds);
344     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
346     if ($sr) {
347       $ei=ldap_first_entry($ds, $sr);
348       if ($ei) {
349         if ($info = ldap_get_values_len($ds, $ei, $attr)){
350           $Data= $info[0];
351         }
352       }
353     }
355     /* close conncetion */
356     ldap_unbind($ds);
357     return($Data);
358   }
361 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
362 ?>