Code

Removed a couple of left over call time references
[gosa.git] / gosa-plugins / fai / 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","FAItemplatePath","FAIowner","FAImode");
28   var $sub_Load_Later   = array("FAItemplateFile"); 
29   var $sub64coded       = array();
30   var $subBinary        = array("FAItemplateFile");
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $SubObjects       = array();  // All leafobjects of this object
38   var $FAIstate         = "";
39   var $ui;
40   var $view_logged      = FALSE;
42   function faiTemplate (&$config, $dn= NULL)
43   {
44     /* Load Attributes */
45     plugin::plugin ($config, $dn);
47     /* If "dn==new" we try to create a new entry
48      * Else we must read all objects from ldap which belong to this entry.
49      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
50      */
51     if($dn != "new"){
52       $this->dn =$dn;
54       /* Get FAIstate
55        */
56       if(isset($this->attrs['FAIstate'][0])){
57         $this->FAIstate = $this->attrs['FAIstate'][0];
58       }
60       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
61        */
62       $ldap     = $this->config->get_ldap_link();
63       $ldap->cd ($this->dn);
64     
65       $attrs_to_search = $this->subAttributes;
66       $attrs_to_search[] = "FAIstate";
67       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
69       while($object = $ldap->fetch()){
71         /* Skip objects, that are tagged as removed */
72         if(isset($object['FAIstate'][0])){
73           if(preg_match("/removed$/",$object['FAIstate'][0])){
74             continue;
75           }
76         }
78         /* Set status for save management */
79         $objects = array();
80         $objects['status']      = "FreshLoaded";
81         $objects['dn']          = $object['dn'];
82         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
83         $this->SubObjects[$objects['cn']] = $objects;
84       }
85     }
86     $this->ui = get_userinfo();
87   }
90   /* Reload some attributes */
91   function get_object_attributes($object,$attributes)
92   {
93     $ldap = $this->config->get_ldap_link();
94     $ldap->cd($this->config->current['BASE']);
95     $ldap->cat($object['dn'],$attributes);
96     $tmp  = $ldap->fetch();
98     foreach($attributes as $attrs){
99       if(isset($tmp[$attrs][0])){
100         $var = $tmp[$attrs][0];
102         /* Check if we must decode some attributes */
103         if(in_array_ics($attrs,$this->sub64coded)){
104           $var = base64_decode($var);
105         }
107         /*  check if this is a binary entry */
108         if(in_array_ics($attrs,$this->subBinary)){
109           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110         }
112         $var = stripslashes($var);
113   
114         $object[$attrs] = $var;
115       }
116     }
117     return($object);
118   }
121   function execute()
122   {
123     /* Call parent execute */
124     plugin::execute();
126     if($this->is_account && !$this->view_logged){
127       $this->view_logged = TRUE;
128       new log("view","fai/".get_class($this),$this->dn);
129     }
131     /* Fill templating stuff */
132     $smarty= get_smarty();
133     $display= "";
135     /* New Listhandling
136      */
137     $once = true;
138     foreach($_POST as $name => $value){
139       if(preg_match("/^editscript_/",$name)&&($once)){
140         $once = false;
141         $entry = preg_replace("/^editscript_/","",$name);
142         $entry = base64_decode(preg_replace("/_.*/","",$entry));
143         $obj  = $this->SubObjects[$entry];
145         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
146         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
147         if(preg_match("/r/",$acl)){
148           if($obj['status'] == "FreshLoaded"){
149             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
150           }
151           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
152           $this->dialog->set_acl_base($this->acl_base);
153           $this->dialog->set_acl_category("fai");
155           session::set('objectinfo',$obj['dn']);
156           $this->dialog->parent = &$this;
157           $this->is_dialog=true;
158         }
159       }
160       if(preg_match("/^deletescript_/",$name)&&($once)){
161         $once = false;
162         $entry = preg_replace("/^deletescript_/","",$name);
163         $entry = base64_decode(preg_replace("/_.*/","",$entry));
164         $obj  = $this->SubObjects[$entry];
166         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
167         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
168         if(preg_match("/d/",$acl)){
169           $status = $this->SubObjects[$entry]['status'];
170           if($status == "edited" || $status == "FreshLoaded"){
171             $this->SubObjects[$entry]['status']= "delete";
172           }else{
173             unset($this->SubObjects[$entry]);
174           }
175         }
176       }
177     }
179     /* File download requested */
180     if(isset($_GET['getFAItemplate'])){
181       if(isset($this->SubObjects[$_GET['getFAItemplate']])){
182         $obj = $this->SubObjects[$_GET['getFAItemplate']];
183         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
184         send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
185       }
186     }
188     /* Edit entries via GET */
189     if(isset($_GET['act']) && isset($_GET['id'])){
190       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
191         $obj = $this->SubObjects[$_GET['id']];
192           if($obj['status'] == "FreshLoaded"){
193           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
194         }
195         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
196         $this->dialog->acl = $this->acl;
197         session::set('objectinfo',$obj['dn']);
198         $this->dialog->parent = &$this;
199         $this->is_dialog=true;
200       }
201     }
203     /* Add new sub object */
204     if(isset($_POST['AddSubObject'])){
205       $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
206       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
208       if(preg_match("/c/",$acl)){
209         $this->dialog= new $this->subClassName($this->config,"new");
210         $this->dialog->set_acl_base($this->acl_base);
211         $this->dialog->set_acl_category("fai");
212         $this->dialog->parent = &$this;
213         $this->is_dialog=true;
214       }
215     }
217     if($this->dn != "new"){
218       session::set('objectinfo',$this->dn);
219     }
221     /* Save Dialog */
222     if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
223       $this->dialog->save_object();
224       $msgs = $this->dialog->check();
225       if(count($msgs)>0){
226         foreach($msgs as $msg){
227           print_red($msg);
228         }
229       }else{
230         /* Get return object */
231         $obj = $this->dialog->save();
232         if(isset($obj['remove'])){
234           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
236           /* Depending on status, set new status */
237           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
238             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
239           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
240             unset($this->SubObjects[$obj['remove']['from']]);
241           }
242           $obj['status'] = "new";
243           $this->SubObjects[$obj['remove']['to']] = $obj;
244           unset($this->SubObjects[$obj['remove']['to']]['remove']);
245         }else{
246           if($obj['status'] == "FreshLoaded"){
247             $obj['status'] = "edited";
248           }
249           $this->SubObjects[$obj['cn']]=$obj;
250         }
251         $this->is_dialog=false;
252         unset($this->dialog);
253         $this->dialog=FALSE;
254       }
255     }
257     /* Sort entries */
258     $tmp = $keys = array();
259     foreach($this->SubObjects as $key => $entry){
260       $keys[$key]=$key;
261     }
262     natcasesort($keys);
263     foreach($keys as $key){
264       $tmp[$key]=$this->SubObjects[$key];
265     }
266     $this->SubObjects = $tmp;
268     /* Cancel Dialog */
269     if(isset($_POST['CancelSubObject'])){
270       $this->is_dialog=false; 
271       unset($this->dialog);
272       $this->dialog=FALSE;
273     }
275     /* Print dialog if $this->dialog is set */
276     if(is_object($this->dialog)){
277       $this->dialog->save_object();
278       $display = $this->dialog->execute();
279       return($display);
280     }
282      /* Divlist Containing FAItemplates */
283     $divlist = new divSelectBox("FAItemplates");
284     $divlist->setHeight(400);
286     $tmp = $this->getList(true);
287   
288     /* Create div list with all sub entries listed */
289     foreach($this->SubObjects as $key => $name){
291       /* Skip removed entries */ 
292       if($name['status'] == "delete") continue;
294       /* Get permissions */
295       $dn  = $this->acl_base_for_current_object($name['dn']);
296       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
297       $act = "";
299       /* Hide delete icon if this object is freezed */
300       if(preg_match("/freeze/",$this->FAIstate)){
301         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
302       }else{
303         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
304         if(preg_match("/d/",$acl)){
305           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
306         }
307       }
309       /* Check acls for download icon */
310       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
311       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
312         $down = "";
313       }else{
314         $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
315           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
316           </a>";
317       }
319       /* Check if we are allowed to view this object */
320       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
321       if(preg_match("/r/",$s_acl)){
323         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$tmp[$key]."</a>";
324         $divlist->AddEntry(array( array("string"=> $edit_link), 
325               array("string"=>$down , "attach" => "style='width:20px;'"),
326               array("string"=>str_replace("%s",base64_encode($key),$act),
327                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
328       }
329     }
330     $smarty->assign("Entry_divlist",$divlist->DrawList());
331     /* Divlist creation complete
332      */
334      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
335      * If we post the escaped strings they will be escaped again
336      */
337     foreach($this->attributes as $attrs){
338       if(get_magic_quotes_gpc()){
339         $smarty->assign($attrs,stripslashes($this->$attrs));
340       }else{
341         $smarty->assign($attrs,($this->$attrs));
342       }
343     }
345     $dn = $this->acl_base_for_current_object($this->dn);
346     $smarty->assign("sub_object_is_addable", 
347             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
348             !preg_match("/freeze/",$this->FAIstate));
350     foreach($this->attributes as $attr){
351       $smarty->assign($attr."ACL",$this->getacl($attr));
352     }
354     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
355     return($display);
356   }
359   function acl_base_for_current_object($dn)
360   {
361     if($dn == "new"){
362       if($this->dn == "new"){
363         $dn = session::get('CurrentMainBase');
364       }else{
365         $dn = $this->dn;
366       }
367     }
368     return($dn);
369   }
372   /* Generate listbox friendly SubObject list
373   */
374   function getList(){
375     $a_return=array();
376     foreach($this->SubObjects as $obj){
377       if($obj['status'] != "delete"){
378       
379         if((isset($obj['description']))&&(!empty($obj['description']))){
380           if(strlen($obj['description']) > 40){
381             $obj['description'] = substr($obj['description'],0,40)."...";
382           }
383           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
384         }else{
385           $a_return[$obj['cn']]= $obj['cn'];
386         }
387       }
388     }
389     return($a_return);
390   }
392   /* Delete me, and all my subtrees
393    */
394   function remove_from_parent()
395   {
396     $ldap = $this->config->get_ldap_link();
397     $ldap->cd ($this->dn);
399     $faifilter = session::get('faifilter');
400     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
401     if($faifilter['branch'] == "main"){
402       $use_dn = $this->dn;
403     }
405     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
406     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
408     foreach($this->SubObjects as $name => $obj){
409       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
410       if($faifilter['branch'] == "main"){
411         $use_dn = $obj['dn'];
412       }
413       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
414     }
415     $this->handle_post_events("remove");    
416   }
419   /* Save data to object 
420    */
421   function save_object()
422   {
423     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
424       plugin::save_object();
425     }
426   }
429   /* Check supplied data */
430   function check()
431   {
432     /* Call common method to give check the hook */
433     $message= plugin::check();
435     return ($message);
436   }
439   /* Save to LDAP */
440   function save()
441   {
442     plugin::save();
444     $ldap = $this->config->get_ldap_link();
446     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
447     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
449     if($this->initially_was_account){
450       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
451     }else{
452       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
453     }
455     /* Prepare FAIscriptEntry to write it to ldap
456      * First sort array.
457      *  Because we must delete old entries first.
458      * After deletion, we perform add and modify 
459      */
460     $Objects = array();
461     foreach($this->SubObjects as $name => $obj){
462       if($obj['status'] == "FreshLoaded"){
463         unset($this->SubObjects[$name]);
464       }
465     }
467     foreach($this->SubObjects as $name => $obj){
468       if($obj['status'] == "delete"){
469         $Objects[$name] = $obj; 
470       }
471     }
472     foreach($this->SubObjects as $name => $obj){
473       if($obj['status'] != "delete"){
474         $Objects[$name] = $obj; 
475       }
476     }
478     foreach($Objects as $name => $obj){
480       foreach($this->sub64coded as $codeIt){
481         $obj[$codeIt]=base64_encode($obj[$codeIt]);
482       }
483       $tmp = array();
484       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
485       foreach($attributes as $attrs){
486         if(empty($obj[$attrs])){
487           $obj[$attrs] = array();
488         }
489         $tmp[$attrs] =($obj[$attrs]);
490       }    
491         
492       $tmp['objectClass'] = $this->subClasses;
494       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
496       if($obj['status']=="new"){
497         $ldap->cat($sub_dn,array("objectClass"));
498         if($ldap->count()){
499           $obj['status']="edited";
500         }
501       }
503       /* Tag object */
504       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
506       if($obj['status'] == "delete"){
507         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
508         $this->handle_post_events("remove");
509       }elseif($obj['status'] == "edited"){
510         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
511         $this->handle_post_events("modify");
512       }elseif($obj['status']=="new"){
513         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
514         $this->handle_post_events("add");
515       }
517     }
518   }
521   function PrepareForCopyPaste($source)
522   {
523     plugin::PrepareForCopyPaste($source);
525     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
526      */
527     $ldap     = $this->config->get_ldap_link();
528     $ldap->cd ($source['dn']);
530     $attrs_to_search = $this->subAttributes;
531     $attrs_to_search[] = "FAIstate";
532     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
534     while($object = $ldap->fetch()){
536       /* Skip objects, that are tagged as removed */
537       if(isset($object['FAIstate'][0])){
538         if(preg_match("/removed$/",$object['FAIstate'][0])){
539           continue;
540         }
541       }
543       /* Set status for save management */
544       $objects = array();
545       $objects['status']      = "edited";
546       $objects['dn']          = $object['dn'];
547       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
548       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
550       $this->SubObjects[$objects['cn']] = $objects;
551     }
552   }
553   
555   /* Return plugin informations for acl handling */ 
556   static function plInfo()
557   {
558     return (array( 
559           "plShortName" => _("Template"),
560           "plDescription" => _("FAI template"),
561           "plSelfModify"  => FALSE,
562           "plDepends"     => array(),
563           "plPriority"    => 24,
564           "plSection"     => array("administration"),
565           "plCategory"    => array("fai"),
566           "plProvidedAcls" => array(
567             "cn"                => _("Name")." ("._("Readonly").")",
568             "description"       => _("Description"))
569           ));
570   }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>