Code

Fixed height
[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     /* New Listhandling
105      */
106     $once = true;
107     foreach($_POST as $name => $value){
108       if(preg_match("/^editscript_/",$name)&&($once)){
109         $once = false;
110         $entry = preg_replace("/^editscript_/","",$name);
111         $entry = base64_decode(preg_replace("/_.*/","",$entry));
112         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
113         $this->dialog->acl = $this->acl;
114         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
115         $this->dialog->parent = &$this;
116         $this->is_dialog=true;
117       }
118       if(preg_match("/^deletescript_/",$name)&&($once)){
119         $once = false;
120         $entry = preg_replace("/^deletescript_/","",$name);
121         $entry = base64_decode(preg_replace("/_.*/","",$entry));
122         if($this->SubObjects[$entry]['status'] == "edited"){
123           $this->SubObjects[$entry]['status']= "delete";
124         }else{
125           unset($this->SubObjects[$entry]);
126         }
127       }
128     }
130     /* Add new sub object */
131     if(isset($_POST['AddSubObject'])){
132       $this->dialog= new $this->subClassName($this->config,"new");
133       $this->dialog->acl = $this->acl;
134       $this->is_dialog=true;
135     }
137     if($this->dn != "new"){
138       $_SESSION['objectinfo']= $this->dn;
139     }
141     /* Save Dialog */
142     if(isset($_POST['SaveSubObject'])){
143       $this->dialog->save_object();
144       $msgs = $this->dialog->check();
145       if(count($msgs)>0){
146         foreach($msgs as $msg){
147           print_red($msg);
148         }
149       }else{
150         $obj = $this->dialog->save();
151         if(isset($obj['remove'])){
152           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
153             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
154           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
155             unset($this->SubObjects[$obj['remove']['from']]);
156           }
157           $obj['status'] = "new";
158           $this->SubObjects[$obj['remove']['to']] = $obj;
159           unset($this->SubObjects[$obj['remove']['to']]['remove']);
160         }else{
161           $this->SubObjects[$obj['cn']]=$obj;
162         }
163         $this->is_dialog=false;
164         unset($this->dialog);
165         $this->dialog=NULL;
166       }
167     }
169     /* Sort entries */
170     $tmp = $keys = array();
171     foreach($this->SubObjects as $key => $entry){
172       $keys[$key]=$key;
173     }
174     natcasesort($keys);
175     foreach($keys as $key){
176       $tmp[$key]=$this->SubObjects[$key];
177     }
178     $this->SubObjects = $tmp;
180     /* Cancel Dialog */
181     if(isset($_POST['CancelSubObject'])){
182       $this->is_dialog=false; 
183       unset($this->dialog);
184       $this->dialog=NULL;
185     }
187     /* Print dialog if $this->dialog is set */
188     if($this->dialog){
189       $this->dialog->save_object();
190       $display = $this->dialog->execute();
191       return($display);
192     }
194      /* Divlist            added 28.02.2006
195        Containing FAIscripts
196      */
197     $divlist = new divSelectBox("FAItemplates");
198     $divlist->setHeight(400);
199     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
200       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
201       $img_remo = "";
202     }else{
203       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
204       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
205     }
207     foreach($this->getList(true) as $key => $name){
209       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
210         $down = "";
211       }else{
212   
213         $dn = $this->SubObjects[$key]['dn'];       
214  
215         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."' target='_blank'>
216           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
217           </a>";
218       }
220       $divlist->AddEntry(array( array("string"=>$name),
221             array("string"=>$down , "attach" => "style='width:20px;'"),
222             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
223               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
224     }
225     $smarty->assign("Entry_divlist",$divlist->DrawList());
226     /* Divlist creation complete
227      */
229     $smarty->assign("SubObjects",$this->getList());
231      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
232      * If we post the escaped strings they will be escaped again
233      */
234     foreach($this->attributes as $attrs){
235       if(get_magic_quotes_gpc()){
236         $smarty->assign($attrs,stripslashes($this->$attrs));
237       }else{
238         $smarty->assign($attrs,($this->$attrs));
239       }
240     }
242     foreach($this->attributes as $attr){
243       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
244     }
246     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
247     return($display);
248   }
250   /* Generate listbox friendly SubObject list
251   */
252   function getList(){
253     $a_return=array();
254     foreach($this->SubObjects as $obj){
255       if($obj['status'] != "delete"){
256       
257         if((isset($obj['description']))&&(!empty($obj['description']))){
258           if(strlen($obj['description']) > 40){
259             $obj['description'] = substr($obj['description'],0,40)."...";
260           }
261           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
262         }else{
263           $a_return[$obj['cn']]= $obj['cn'];
264         }
265       }
266     }
267     return($a_return);
268   }
270   /* Delete me, and all my subtrees
271    */
272   function remove_from_parent()
273   {
274     $ldap = $this->config->get_ldap_link();
275     $ldap->cd ($this->dn);
276     $ldap->rmdir_recursive($this->dn);
277     $this->handle_post_events("remove");    
278   }
281   /* Save data to object 
282    */
283   function save_object()
284   {
285     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
286       plugin::save_object();
287       foreach($this->attributes as $attrs){
288         if(isset($_POST[$attrs])){
289           $this->$attrs = $_POST[$attrs];
290         } 
291       }
292     }
293   }
296   /* Check supplied data */
297   function check()
298   {
299     /* Call common method to give check the hook */
300     $message= plugin::check();
302     return ($message);
303   }
306   /* Save to LDAP */
307   function save()
308   {
309     plugin::save();
311     $ldap = $this->config->get_ldap_link();
313     $ldap->cat($this->dn);
314     if($ldap->count()!=0){
315       /* Write FAIscript to ldap*/
316       $ldap->cd($this->dn);
317       $this->cleanup();
318       $ldap->modify ($this->attrs); 
320     }else{
321       /* Write FAIscript to ldap*/
322       $ldap->cd($this->config->current['BASE']);
323       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
324       $ldap->cd($this->dn);
325       $ldap->add($this->attrs);
326     }
327     show_ldap_error($ldap->get_error());
329     /* Prepare FAIscriptEntry to write it to ldap
330      * First sort array.
331      *  Because we must delete old entries first.
332      * After deletion, we perform add and modify 
333      */
334     $Objects = array();
335     foreach($this->SubObjects as $name => $obj){
336       if($obj['status'] == "delete"){
337         $Objects[$name] = $obj; 
338       }
339     }
340     foreach($this->SubObjects as $name => $obj){
341       if($obj['status'] != "delete"){
342         $Objects[$name] = $obj; 
343       }
344     }
346     foreach($Objects as $name => $obj){
348       foreach($this->sub64coded as $codeIt){
349         $obj[$codeIt]=base64_encode($obj[$codeIt]);
350       }
351       $tmp = array();
352       foreach($this->subAttributes as $attrs){
353         if(empty($obj[$attrs])){
354           $obj[$attrs] = array();
355         }
356         $tmp[$attrs] = $obj[$attrs];
357       }    
358         
359       $tmp['objectClass'] = $this->subClasses;
361       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
363       if($obj['status']=="new"){
364         $ldap->cat($sub_dn);
365         if($ldap->count()){
366           $obj['status']="modify";
367         }
368       }
370       if($obj['status'] == "delete"){
371         $ldap->cd($sub_dn);
372         $ldap->rmdir_recursive($sub_dn);
373         $this->handle_post_events("remove");
374       }elseif($obj['status'] == "edited"){
375         $ldap->cd($sub_dn);
376         $this->cleanup();
377 $ldap->modify ($tmp); 
379         $this->handle_post_events("modify");
380       }elseif($obj['status']=="new"){
382         if($tmp['description']==array()){
383           unset($tmp['description']);
384         }
385         $ldap->cd($this->config->current['BASE']);
386         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
387         $ldap->cd($sub_dn);
388         $ldap->add($tmp); 
389         $this->handle_post_events("add");
390       }
391       show_ldap_error($ldap->get_error()); 
392     }
393   }
396 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
397 ?>