Code

Freezed releases can now be viewed, but not changed ...
[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   var $FAIstate         = "";
39   function faiTemplate ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl="#all#";
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49      */
50     if($dn != "new"){
51       $this->dn =$dn;
53       /* Set acls
54        */
55       $ui   = get_userinfo();
56       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
57       $acli = get_module_permission($acl, "FAIclass", $this->dn);
58       $this->acl=$acli;
60       /* Get FAIstate
61        */
62       if(isset($this->attrs['FAIstate'][0])){
63         $this->FAIstate = $this->attrs['FAIstate'][0];
64       }
66       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
67        */
68       $ldap     = $this->config->get_ldap_link();
69       $ldap->cd ($this->dn);
70       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
72       while($object = $ldap->fetch()){
73         /* Set status for save management */
74   
75         foreach($this->subAttributes as $attrs){
76           if(!isset($object[$attrs][0])){
77             $this->SubObjects[$object['cn'][0]][$attrs]="";
78           }else{
79             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
80           }
81         }
83         foreach($this->sub64coded as $codeIt){
84           $this->SubObjects[$object['cn'][0]][$codeIt]= base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
85         }       
87         $this->SubObjects[$object['cn'][0]]['FAItemplateFile']= $this->readBinary("FAItemplateFile",$object['dn']);
88        
89         $this->SubObjects[$object['cn'][0]]['status']= "edited";
90         $this->SubObjects[$object['cn'][0]]['dn']= $object['dn'];
91       }
92     }
93   }
95   function execute()
96   {
97         /* Call parent execute */
98         plugin::execute();
100     /* Fill templating stuff */
101     $smarty= get_smarty();
102     $display= "";
104     /* Add new sub object */
105     if(isset($_POST['AddSubObject'])){
106       $this->dialog= new $this->subClassName($this->config,"new");
107       $this->dialog->acl = $this->acl;
108       $this->is_dialog=true;
109     }
111     if($this->dn != "new"){
112       $_SESSION['objectinfo']= $this->dn;
113     }
116     /* Edit selected Sub Object */
117     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
119       $temp = $_POST['SubObject'][0];
120     
121       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$temp]);
122       $this->dialog->acl = $this->acl;
123       $_SESSION['objectinfo'] = $this->SubObjects[$temp]['dn'];
124       $this->is_dialog=true;
125     }
126     
127     /* Remove Sub object */
128     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
129       foreach($_POST['SubObject'] as $temp){
130         if($this->SubObjects[$temp]['status'] == "edited"){
131           $this->SubObjects[$temp]['status']= "delete";
132         }else{
133           unset($this->SubObjects[$temp]);
134         }
135       }
136     }
138     /* Save Dialog */
139     if(isset($_POST['SaveSubObject'])){
140       $this->dialog->save_object();
141       $msgs = $this->dialog->check();
142       if(count($msgs)>0){
143         foreach($msgs as $msg){
144           print_red($msg);
145         }
146       }else{
147         $obj = $this->dialog->save();
148         if(isset($obj['remove'])){
149           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
150             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
151           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
152             unset($this->SubObjects[$obj['remove']['from']]);
153           }
154           $obj['status'] = "new";
155           $this->SubObjects[$obj['remove']['to']] = $obj;
156           unset($this->SubObjects[$obj['remove']['to']]['remove']);
157         }else{
158           $this->SubObjects[$obj['cn']]=$obj;
159         }
160         $this->is_dialog=false;
161         unset($this->dialog);
162         $this->dialog=NULL;
163       }
164     }
166     /* Sort entries */
167     $tmp = $keys = array();
168     foreach($this->SubObjects as $key => $entry){
169       $keys[$key]=$key;
170     }
171     natcasesort($keys);
172     foreach($keys as $key){
173       $tmp[$key]=$this->SubObjects[$key];
174     }
175     $this->SubObjects = $tmp;
177     /* Cancel Dialog */
178     if(isset($_POST['CancelSubObject'])){
179       $this->is_dialog=false; 
180       unset($this->dialog);
181       $this->dialog=NULL;
182     }
184     /* Print dialog if $this->dialog is set */
185     if($this->dialog){
186       $this->dialog->save_object();
187       $display = $this->dialog->execute();
188       return($display);
189     }
191     $smarty->assign("SubObjects",$this->getList());
192     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
194      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
195      * If we post the escaped strings they will be escaped again
196      */
197     foreach($this->attributes as $attrs){
198       if(get_magic_quotes_gpc()){
199         $smarty->assign($attrs,stripslashes($this->$attrs));
200       }else{
201         $smarty->assign($attrs,($this->$attrs));
202       }
203     }
205     foreach($this->attributes as $attr){
206       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
207     }
209     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
210     return($display);
211   }
213   /* Generate listbox friendly SubObject list
214   */
215   function getList(){
216     $a_return=array();
217     foreach($this->SubObjects as $obj){
218       if($obj['status'] != "delete"){
219       
220         if((isset($obj['description']))&&(!empty($obj['description']))){
221           if(strlen($obj['description']) > 40){
222             $obj['description'] = substr($obj['description'],0,40)."...";
223           }
224           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
225         }else{
226           $a_return[$obj['cn']]= $obj['cn'];
227         }
228       }
229     }
230     return($a_return);
231   }
233   /* Delete me, and all my subtrees
234    */
235   function remove_from_parent()
236   {
237     $ldap = $this->config->get_ldap_link();
238     $ldap->cd ($this->dn);
239     $ldap->rmdir_recursive($this->dn);
240     $this->handle_post_events("remove");    
241   }
244   /* Save data to object 
245    */
246   function save_object()
247   {
248     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
249       plugin::save_object();
250       foreach($this->attributes as $attrs){
251         if(isset($_POST[$attrs])){
252           $this->$attrs = $_POST[$attrs];
253         } 
254       }
255     }
256   }
259   /* Check supplied data */
260   function check()
261   {
262     $message= array();
263     return ($message);
264   }
267   /* Save to LDAP */
268   function save()
269   {
270     plugin::save();
272     $ldap = $this->config->get_ldap_link();
274     $ldap->cat($this->dn);
275     if($ldap->count()!=0){
276       /* Write FAIscript to ldap*/
277       $ldap->cd($this->dn);
278       $this->cleanup();
279       $ldap->modify ($this->attrs); 
281     }else{
282       /* Write FAIscript to ldap*/
283       $ldap->cd($this->config->current['BASE']);
284       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
285       $ldap->cd($this->dn);
286       $ldap->add($this->attrs);
287     }
288     show_ldap_error($ldap->get_error());
290     /* Prepare FAIscriptEntry to write it to ldap
291      * First sort array.
292      *  Because we must delete old entries first.
293      * After deletion, we perform add and modify 
294      */
295     $Objects = array();
296     foreach($this->SubObjects as $name => $obj){
297       if($obj['status'] == "delete"){
298         $Objects[$name] = $obj; 
299       }
300     }
301     foreach($this->SubObjects as $name => $obj){
302       if($obj['status'] != "delete"){
303         $Objects[$name] = $obj; 
304       }
305     }
307     foreach($Objects as $name => $obj){
309       foreach($this->sub64coded as $codeIt){
310         $obj[$codeIt]=base64_encode($obj[$codeIt]);
311       }
312       $tmp = array();
313       foreach($this->subAttributes as $attrs){
314         if(empty($obj[$attrs])){
315           $obj[$attrs] = array();
316         }
317         $tmp[$attrs] = $obj[$attrs];
318       }    
319         
320       $tmp['objectClass'] = $this->subClasses;
322       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
324       if($obj['status']=="new"){
325         $ldap->cat($sub_dn);
326         if($ldap->count()){
327           $obj['status']="modify";
328         }
329       }
331       if($obj['status'] == "delete"){
332         $ldap->cd($sub_dn);
333         $ldap->rmdir_recursive($sub_dn);
334         $this->handle_post_events("remove");
335       }elseif($obj['status'] == "edited"){
336         $ldap->cd($sub_dn);
337         $this->cleanup();
338 $ldap->modify ($tmp); 
340         $this->handle_post_events("modify");
341       }elseif($obj['status']=="new"){
343         if($tmp['description']==array()){
344           unset($tmp['description']);
345         }
346         $ldap->cd($this->config->current['BASE']);
347         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
348         $ldap->cd($sub_dn);
349         $ldap->add($tmp); 
350         $this->handle_post_events("add");
351       }
352       show_ldap_error($ldap->get_error()); 
353     }
354   }
356   function readBinary($attr,$dn){
357     $Data  ="";
358     $ds= ldap_connect($this->config->current['SERVER']);
359     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
360     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
361       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
362       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
363     }
365     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
366       ldap_start_tls($ds);
367     }
369     $r  = ldap_bind($ds);
370     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
372     if ($sr) {
373       $ei=ldap_first_entry($ds, $sr);
374       if ($ei) {
375         if ($info = ldap_get_values_len($ds, $ei, $attr)){
376           $Data= $info[0];
377         }
378       }
379     }
381     /* close conncetion */
382     ldap_unbind($ds);
383     return($Data);
384   }
387 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
388 ?>