Code

daf503fff379e79ddd15e9e063d23b9be4dcb646
[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']= $ldap->get_attribute($object['dn'], "FAItemplateFile",$r_array=0);
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());
193      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
194      * If we post the escaped strings they will be escaped again
195      */
196     foreach($this->attributes as $attrs){
197       if(get_magic_quotes_gpc()){
198         $smarty->assign($attrs,stripslashes($this->$attrs));
199       }else{
200         $smarty->assign($attrs,($this->$attrs));
201       }
202     }
204     foreach($this->attributes as $attr){
205       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
206     }
208     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
209     return($display);
210   }
212   /* Generate listbox friendly SubObject list
213   */
214   function getList(){
215     $a_return=array();
216     foreach($this->SubObjects as $obj){
217       if($obj['status'] != "delete"){
218       
219         if((isset($obj['description']))&&(!empty($obj['description']))){
220           if(strlen($obj['description']) > 40){
221             $obj['description'] = substr($obj['description'],0,40)."...";
222           }
223           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
224         }else{
225           $a_return[$obj['cn']]= $obj['cn'];
226         }
227       }
228     }
229     return($a_return);
230   }
232   /* Delete me, and all my subtrees
233    */
234   function remove_from_parent()
235   {
236     $ldap = $this->config->get_ldap_link();
237     $ldap->cd ($this->dn);
238     $ldap->rmdir_recursive($this->dn);
239     $this->handle_post_events("remove");    
240   }
243   /* Save data to object 
244    */
245   function save_object()
246   {
247     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
248       plugin::save_object();
249       foreach($this->attributes as $attrs){
250         if(isset($_POST[$attrs])){
251           $this->$attrs = $_POST[$attrs];
252         } 
253       }
254     }
255   }
258   /* Check supplied data */
259   function check()
260   {
261     /* Call common method to give check the hook */
262     $message= plugin::check();
264     return ($message);
265   }
268   /* Save to LDAP */
269   function save()
270   {
271     plugin::save();
273     $ldap = $this->config->get_ldap_link();
275     $ldap->cat($this->dn);
276     if($ldap->count()!=0){
277       /* Write FAIscript to ldap*/
278       $ldap->cd($this->dn);
279       $this->cleanup();
280       $ldap->modify ($this->attrs); 
282     }else{
283       /* Write FAIscript to ldap*/
284       $ldap->cd($this->config->current['BASE']);
285       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
286       $ldap->cd($this->dn);
287       $ldap->add($this->attrs);
288     }
289     show_ldap_error($ldap->get_error());
291     /* Prepare FAIscriptEntry to write it to ldap
292      * First sort array.
293      *  Because we must delete old entries first.
294      * After deletion, we perform add and modify 
295      */
296     $Objects = array();
297     foreach($this->SubObjects as $name => $obj){
298       if($obj['status'] == "delete"){
299         $Objects[$name] = $obj; 
300       }
301     }
302     foreach($this->SubObjects as $name => $obj){
303       if($obj['status'] != "delete"){
304         $Objects[$name] = $obj; 
305       }
306     }
308     foreach($Objects as $name => $obj){
310       foreach($this->sub64coded as $codeIt){
311         $obj[$codeIt]=base64_encode($obj[$codeIt]);
312       }
313       $tmp = array();
314       foreach($this->subAttributes as $attrs){
315         if(empty($obj[$attrs])){
316           $obj[$attrs] = array();
317         }
318         $tmp[$attrs] = $obj[$attrs];
319       }    
320         
321       $tmp['objectClass'] = $this->subClasses;
323       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
325       if($obj['status']=="new"){
326         $ldap->cat($sub_dn);
327         if($ldap->count()){
328           $obj['status']="modify";
329         }
330       }
332       if($obj['status'] == "delete"){
333         $ldap->cd($sub_dn);
334         $ldap->rmdir_recursive($sub_dn);
335         $this->handle_post_events("remove");
336       }elseif($obj['status'] == "edited"){
337         $ldap->cd($sub_dn);
338         $this->cleanup();
339 $ldap->modify ($tmp); 
341         $this->handle_post_events("modify");
342       }elseif($obj['status']=="new"){
344         if($tmp['description']==array()){
345           unset($tmp['description']);
346         }
347         $ldap->cd($this->config->current['BASE']);
348         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
349         $ldap->cd($sub_dn);
350         $ldap->add($tmp); 
351         $this->handle_post_events("add");
352       }
353       show_ldap_error($ldap->get_error()); 
354     }
355   }
358 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
359 ?>