Code

FAI package
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiVariable.inc
1 <?php
3 class faiVariable extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIvariable");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAIvariableEntry";
16   var $subClasses       = array("top","FAIclass","FAIvariableEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiVariableEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAIvariableContent"); 
23   var $sub64coded       = array();  
25   /* Specific attributes */
26   var $cn               = "";       // The class name for this object
27   var $description      = "";       // The description for this set of partitions
28   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
29   var $SubObjects       = array();  // All leafobjects of this object
31   var $FAIstate         = "";
32   var $ui   ;
33   var $view_logged      = FALSE;
35   function faiVariable (&$config, $dn= NULL)
36   {
37     /* Load Attributes */
38     plugin::plugin ($config, $dn);
40     if($dn != "new"){
41       $this->dn =$dn;
43       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
44        */
45       $ldap     = $this->config->get_ldap_link();
46       $ldap->cd ($this->dn);
48       $attrs_to_search = $this->subAttributes;
49       $attrs_to_search[] = "FAIstate";
50       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
52       while($object = $ldap->fetch()){
53       
54         /* Skip objects, that are tagged as removed */
55         if(isset($object['FAIstate'][0])){
56           if(preg_match("/removed$/",$object['FAIstate'][0])){
57             continue;
58           }
59         }
61         /* Set status for save management */
62         foreach($this->subAttributes as $attrs){
63           if(!isset($object[$attrs][0])){
64             $this->SubObjects[$object['cn'][0]][$attrs]="";
65           }else{
66             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
67           }
68         }
69      
70         foreach($this->sub64coded as $codeIt){
71           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
72         }
73  
74         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
75         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
76       }
78     }
79     $this->is_new = FALSE;
80     if($this->dn == "new"){
81       $this->is_new =TRUE;
82     }
83     $this->ui = get_userinfo();
84   }
87   function acl_base_for_current_object($dn)
88   {
89     if($dn == "new"){
90       if($this->dn == "new"){
91         $dn = session::get('CurrentMainBase');
92       }else{
93         $dn = $this->dn;
94       }
95     }
96     return($dn);
97   }
100   function execute()
101   {
102     /* Call parent execute */
103     plugin::execute();
105     if($this->is_account && !$this->view_logged){
106       $this->view_logged = TRUE;
107       new log("view","fai/".get_class($this),$this->dn);
108     }
110     /* Fill templating stuff */
111     $smarty= get_smarty();
112     $display= "";
114     /* Add new sub object */
115     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
116       $this->dialog= new $this->subClassName($this->config,"new");
117       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
118       $this->dialog->set_acl_category("fai");
119       $this->dialog->parent = &$this;
120       $this->dialog->FAIstate = $this->FAIstate;
121       $this->is_dialog=true;
122     }
124     if($this->dn != "new"){
125       session::set('objectinfo',$this->dn);
126     }
129     /* Edit selected Sub Object */
130     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
132       $var = $_POST['SubObject'][0];
133       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
134       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
135       $this->dialog->set_acl_category("fai");
136       $this->dialog->set_acl_base($c_dn);
137       $this->dialog->FAIstate = $this->FAIstate;
138       $this->dialog->parent = &$this;
139       session::set('objectinfo',$this->SubObjects[$var]['dn']);
140       $this->is_dialog=true;
141     }
142     
143     /* Remove Sub object */
144     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject'])) && !preg_match("/freeze/i",$this->FAIstate)){
145       foreach($_POST['SubObject'] as $var){
147         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
148         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
149         if(preg_match("/d/",$acl)){
150           if($this->SubObjects[$var]['status'] == "edited"){
151             $this->SubObjects[$var]['status']= "delete";
152           }else{
153             unset($this->SubObjects[$var]);
154           }
155         }
156       }
157     }
159     /* Save Dialog */
160     if(isset($_POST['SaveSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
161       $this->dialog->save_object();
162       $msgs = $this->dialog->check();
163       if(count($msgs)>0){
164         foreach($msgs as $msg){
165           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
166         }
167       }else{
168         $obj = $this->dialog->save();
169         if(isset($obj['remove'])){
170           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
171             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
172           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
173             unset($this->SubObjects[$obj['remove']['from']]);
174           }
175           $obj['status'] = "new";
176           $this->SubObjects[$obj['remove']['to']] = $obj;
177           unset($this->SubObjects[$obj['remove']['to']]['remove']);
178         }else{
179           $this->SubObjects[$obj['cn']]=$obj;
180         }
181         $this->is_dialog=false;
182         unset($this->dialog);
183         $this->dialog=FALSE;
184       }
185     }
187     /* Sort entries */
188     $tmp = $keys = array();
189     foreach($this->SubObjects as $key => $entry){
190       $keys[$key]=$key;
191     }
192     natcasesort($keys);
193     foreach($keys as $key){
194       $tmp[$key]=$this->SubObjects[$key];
195     } 
196     $this->SubObjects = $tmp;
198     /* Cancel Dialog */
199     if(isset($_POST['CancelSubObject'])){
200       $this->is_dialog=false; 
201       unset($this->dialog);
202       $this->dialog=FALSE;
203     }
205     /* Print dialog if $this->dialog is set */
206     if(is_object($this->dialog)){
207       $this->dialog->save_object();
208       $display = $this->dialog->execute();
209       return($display);
210     }
212     $ui = get_userinfo();
213     $ret = $this->getList();
214     $tmp = array();
215     foreach($this->SubObjects as $key => $obj){
216       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
217       if((preg_match("/r/",$acl) || $obj['dn'] == "new") && isset($ret[$key])){
218         $tmp[$key] = $ret[$key];
219       } 
220     }
221     $smarty->assign("SubObjects",$tmp);
224     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
225      * If we post the escaped strings they will be escaped again
226      */
227     foreach($this->attributes as $attrs){
228       if(get_magic_quotes_gpc()){
229         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
230       }else{
231         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
232       }
233     }
235     $c_dn = $this->acl_base_for_current_object($this->dn);
236     $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
237     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
239     $tmp = $this->plInfo();
240     foreach($tmp['plProvidedAcls'] as $name => $translation) {
241       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
242     }
243     
245     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
246     return($display);
247   }
249   /* Generate listbox friendly SubObject list
250   */
251   function getList(){
252     $a_return=array();
253     foreach($this->SubObjects as $obj){
254       if($obj['status'] != "delete"){
256         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
257           if (preg_match("/\[\*\]/", $obj['description'])){
258             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
259           } else {
260             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
261           }
262         }else{
263           if (preg_match("/\[\*\]/", $obj['description'])){
264             $a_return[$obj['cn']]= $obj['cn'];
265           } else {
266             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
267           }
268         }
269       }
270     }
271     return($a_return);
272   }
274   /* Delete me, and all my subtrees
275    */
276   function remove_from_parent()
277   {
278     if($this->acl_is_removeable()){
279       $ldap = $this->config->get_ldap_link();
280       $ldap->cd ($this->dn);
281       $release = $this->parent->parent->fai_release;
282       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
283       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
284       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
286       foreach($this->SubObjects as $name => $obj){
287         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
288         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
289       }
290       $this->handle_post_events("remove");
291     }
292   }
295   /* Save data to object 
296    */
297   function save_object()
298   {
299     if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
300       plugin::save_object();
301       foreach($this->attributes as $attrs){
302         if(isset($_POST[$attrs])){
303           $this->$attrs = $_POST[$attrs];
304         }
305       }
306     }
307   }
310   /* Check supplied data */
311   function check()
312   {
313     /* Call common method to give check the hook */
314     $message= plugin::check();
316     /* Ensure that we do not overwrite an allready existing entry 
317      */
318     if($this->is_new){
319       $release = $this->parent->parent->fai_release;
320       $new_dn= 'cn='.$this->cn.",".get_ou('faivariableou').get_ou('faiou').$release;
321       $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
322       if(isset($res[$this->cn])){
323         $message[] = msgPool::duplicated(_("Name"));
324       }
325     }
327     return ($message);
328   }
331   /* Save to LDAP */
332   function save()
333   {
334     plugin::save();
335  
336     $ldap = $this->config->get_ldap_link();
337     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
339     if($this->initially_was_account){
340       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
341     }else{
342       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
343     }
344  
345     /* Prepare FAIscriptEntry to write it to ldap
346      * First sort array.
347      *  Because we must delete old entries first.
348      * After deletion, we perform add and modify 
349      */
350     $Objects = array();
351     foreach($this->SubObjects as $name => $obj){
352       if($obj['status'] == "delete"){
353         $Objects[$name] = $obj; 
354       }
355     }
356     foreach($this->SubObjects as $name => $obj){
357       if($obj['status'] != "delete"){
358         $Objects[$name] = $obj; 
359       }
360     }
362     foreach($Objects as $name => $obj){
364       foreach($this->sub64coded as $codeIt){
365         $obj[$codeIt]=base64_encode($obj[$codeIt]);
366       }
368       $tmp = array();
369       foreach($this->subAttributes as $attrs){
370         if(empty($obj[$attrs])){
371           $obj[$attrs] = array();
372         }
373         $tmp[$attrs] = $obj[$attrs];
374       }    
375         
376       $tmp['objectClass'] = $this->subClasses;
378       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
380       if($obj['status']=="new"){
381         $ldap->cat($sub_dn,array("objectClass"));
382         if($ldap->count()){
383           $obj['status']="edited";
384         }
385       }
387       /* Tag object */
388       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
389       
390       if($obj['status'] == "delete"){
391         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
392         $this->handle_post_events("remove");
393       }elseif($obj['status'] == "edited"){
394         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
395         $this->handle_post_events("modify");
396       }elseif($obj['status']=="new"){
397         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
398         $this->handle_post_events("add");
399       }
401     }
402   }
405   function PrepareForCopyPaste($source)
406   {
407     plugin::PrepareForCopyPaste($source);
409     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
410      */
411     $ldap     = $this->config->get_ldap_link();
412     $ldap->cd ($source['dn']);
413     $attrs_to_search = $this->subAttributes;
414     $attrs_to_search[] = "FAIstate";
415     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
416     while($object = $ldap->fetch()){
418       /* Skip objects, that are tagged as removed */
419       if(isset($object['FAIstate'][0])){
420         if(preg_match("/removed$/",$object['FAIstate'][0])){
421           continue;
422         }
423       }
425       /* Set status for save management */
426       foreach($this->subAttributes as $attrs){
427         if(!isset($object[$attrs][0])){
428           $this->SubObjects[$object['cn'][0]][$attrs]="";
429         }else{
430           $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
431         }
432       }
433       foreach($this->sub64coded as $codeIt){
434         $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
435       }
436       $this->SubObjects[$object['cn'][0]]['status']      = "edited";
437       $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
438     }
439   } 
441  
442   /* Return plugin informations for acl handling */ 
443   static function plInfo()
444   {
445     return (array( 
446           "plShortName" => _("Variable"),
447           "plDescription" => _("FAI variable"),
448           "plSelfModify"  => FALSE,
449           "plDepends"     => array(),
450           "plPriority"    => 22,
451           "plSection"     => array("administration"),
452           "plCategory"    => array("fai"),
453           "plProvidedAcls" => array(
454             "cn"                => _("Name")." ("._("Read only").")",
455             "description"       => _("Description"))
456           ));
457   }
460   /*! \brief  Used for copy & paste.
461     Returns a HTML input mask, which allows to change the cn of this entry.
462     @param  Array   Array containing current status && a HTML template.
463    */
464   function getCopyDialog()
465   {
466     $vars = array("cn");
467     $smarty = get_smarty();
468     $smarty->assign("cn", htmlentities($this->cn));
469     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
470     $ret = array();
471     $ret['string'] = $str;
472     $ret['status'] = "";
473     return($ret);
474   }
477   /*! \brief  Used for copy & paste.
478     Some entries must be renamed to avaoid duplicate entries.
479    */
480   function saveCopyDialog()
481   {
482     if(isset($_POST['cn'])){
483       $this->cn = get_post('cn');
484     }
485   }
488 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
489 ?>