Code

Same here
[gosa.git] / plugins / admin / fai / class_faiVariable.inc
1 <?php
3 class faiVariable 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","FAIvariable");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIvariableEntry";
21   var $subClasses       = array("top","FAIclass","FAIvariableEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiVariableEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIvariableContent"); 
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 faiVariable ($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      */
51     if($dn != "new"){
52       $this->dn =$dn;
54       /* Set acls
55        */
56       $ui   = get_userinfo();
57       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
58       $acli = get_module_permission($acl, "FAIclass", $this->dn);
59       $this->acl=$acli;
61       /* Get FAIstate
62        */
63       if(isset($this->attrs['FAIstate'][0])){
64         $this->FAIstate = $this->attrs['FAIstate'][0];
65       }
67       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68        */
69       $ldap     = $this->config->get_ldap_link();
70       $ldap->cd ($this->dn);
72       $attrs_to_search = $this->subAttributes;
73       $attrs_to_search[] = "FAIstate";
74       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
76       while($object = $ldap->fetch()){
77       
78         /* Skip objects, that are tagged as removed */
79         if(isset($object['FAIstate'][0])){
80           if(preg_match("/removed$/",$object['FAIstate'][0])){
81             continue;
82           }
83         }
85         /* Set status for save management */
86         foreach($this->subAttributes as $attrs){
87           if(!isset($object[$attrs][0])){
88             $this->SubObjects[$object['cn'][0]][$attrs]="";
89           }else{
90             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
91           }
92         }
93      
94         foreach($this->sub64coded as $codeIt){
95           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
96         }
97  
98         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
99         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
100       }
101     }
102   }
104   function execute()
105   {
106         /* Call parent execute */
107         plugin::execute();
109     /* Fill templating stuff */
110     $smarty= get_smarty();
111     $display= "";
113     /* Add new sub object */
114     if(isset($_POST['AddSubObject'])){
115       $this->dialog= new $this->subClassName($this->config,"new");
116       $this->dialog->acl = $this->acl;
117       $this->is_dialog=true;
118     }
120     if($this->dn != "new"){
121       $_SESSION['objectinfo']= $this->dn;
122     }
125     /* Edit selected Sub Object */
126     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
128       $var = $_POST['SubObject'][0];
129       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
130       $this->dialog->acl = $this->acl;
131       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
132       $this->is_dialog=true;
133     }
134     
135     /* Remove Sub object */
136     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
137       foreach($_POST['SubObject'] as $var){
138         if($this->SubObjects[$var]['status'] == "edited"){
139           $this->SubObjects[$var]['status']= "delete";
140         }else{
141           unset($this->SubObjects[$var]);
142         }
143       }
144     }
146     /* Save Dialog */
147     if(isset($_POST['SaveSubObject'])){
148       $this->dialog->save_object();
149       $msgs = $this->dialog->check();
150       if(count($msgs)>0){
151         foreach($msgs as $msg){
152           print_red($msg);
153         }
154       }else{
155         $obj = $this->dialog->save();
156         if(isset($obj['remove'])){
157           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
158             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
159           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
160             unset($this->SubObjects[$obj['remove']['from']]);
161           }
162           $obj['status'] = "new";
163           $this->SubObjects[$obj['remove']['to']] = $obj;
164           unset($this->SubObjects[$obj['remove']['to']]['remove']);
165         }else{
166           $this->SubObjects[$obj['cn']]=$obj;
167         }
168         $this->is_dialog=false;
169         unset($this->dialog);
170         $this->dialog=NULL;
171       }
172     }
174     /* Sort entries */
175     $tmp = $keys = array();
176     foreach($this->SubObjects as $key => $entry){
177       $keys[$key]=$key;
178     }
179     natcasesort($keys);
180     foreach($keys as $key){
181       $tmp[$key]=$this->SubObjects[$key];
182     } 
183     $this->SubObjects = $tmp;
185     /* Cancel Dialog */
186     if(isset($_POST['CancelSubObject'])){
187       $this->is_dialog=false; 
188       unset($this->dialog);
189       $this->dialog=NULL;
190     }
192     /* Print dialog if $this->dialog is set */
193     if($this->dialog){
194       $this->dialog->save_object();
195       $display = $this->dialog->execute();
196       return($display);
197     }
199     $smarty->assign("SubObjects",$this->getList());
201       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
202      * If we post the escaped strings they will be escaped again
203      */
205     foreach($this->attributes as $attrs){
206       if(get_magic_quotes_gpc()){
207         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
208       }else{
209         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
210       }
211     }
213       foreach($this->attributes as $attr){
214       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
215     }
218     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
219     return($display);
220   }
222   /* Generate listbox friendly SubObject list
223   */
224   function getList(){
225     $a_return=array();
226     foreach($this->SubObjects as $obj){
227       if($obj['status'] != "delete"){
229         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
230           if (preg_match("/\[\*\]/", $obj['description'])){
231             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
232           } else {
233             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
234           }
235         }else{
236           if (preg_match("/\[\*\]/", $obj['description'])){
237             $a_return[$obj['cn']]= $obj['cn'];
238           } else {
239             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
240           }
241         }
242       }
243     }
244     return($a_return);
245   }
247   /* Delete me, and all my subtrees
248    */
249   function remove_from_parent()
250   {
251     $ldap = $this->config->get_ldap_link();
252     $ldap->cd ($this->dn);
254     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
255     if($_SESSION['faifilter']['branch'] == "main"){
256       $use_dn = $this->dn;
257     }
259     prepare_to_save_FAI_object($use_dn,array(),true);
261     foreach($this->SubObjects as $name => $obj){
262       $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
263       if($_SESSION['faifilter']['branch'] == "main"){
264         $use_dn = $obj['dn'];
265       }
266       prepare_to_save_FAI_object($use_dn,array(),true);
267     }
268     $this->handle_post_events("remove");
269   }
272   /* Save data to object 
273    */
274   function save_object()
275   {
276     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
277       plugin::save_object();
278       foreach($this->attributes as $attrs){
279         if(isset($_POST[$attrs])){
280           $this->$attrs = $_POST[$attrs];
281         }
282       }
283     }
284   }
287   /* Check supplied data */
288   function check()
289   {
290     /* Call common method to give check the hook */
291     $message= plugin::check();
293     return ($message);
294   }
297   /* Save to LDAP */
298   function save()
299   {
300     plugin::save();
301  
302     $ldap = $this->config->get_ldap_link();
303     prepare_to_save_FAI_object($this->dn,$this->attrs);
304     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
305  
306     /* Do object tagging */
307     $this->handle_object_tagging();
308  
309     /* Prepare FAIscriptEntry to write it to ldap
310      * First sort array.
311      *  Because we must delete old entries first.
312      * After deletion, we perform add and modify 
313      */
314     $Objects = array();
315     foreach($this->SubObjects as $name => $obj){
316       if($obj['status'] == "delete"){
317         $Objects[$name] = $obj; 
318       }
319     }
320     foreach($this->SubObjects as $name => $obj){
321       if($obj['status'] != "delete"){
322         $Objects[$name] = $obj; 
323       }
324     }
326     foreach($Objects as $name => $obj){
328       foreach($this->sub64coded as $codeIt){
329         $obj[$codeIt]=base64_encode($obj[$codeIt]);
330       }
332       $tmp = array();
333       foreach($this->subAttributes as $attrs){
334         if(empty($obj[$attrs])){
335           $obj[$attrs] = array();
336         }
337         $tmp[$attrs] = $obj[$attrs];
338       }    
339         
340       $tmp['objectClass'] = $this->subClasses;
342       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
344       if($obj['status']=="new"){
345         $ldap->cat($sub_dn,array("objectClass"));
346         if($ldap->count()){
347           $obj['status']="edited";
348         }
349       }
351       /* Check if gosaAdministrativeUnitTag is required as object class */
352       if($obj['status'] == "edited"){
353         $ldap->cat($sub_dn,array("objectClass"));
354         $attrs = $ldap->fetch();
355         if(isset($attrs['objectClass'])){
356           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
357             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
358           }
359         }
360       }
361       
362       if($obj['status'] == "delete"){
363         prepare_to_save_FAI_object($sub_dn,array(),true);
364         $this->handle_post_events("remove");
365       }elseif($obj['status'] == "edited"){
366         prepare_to_save_FAI_object($sub_dn,$tmp);
367         $this->handle_post_events("modify");
368       }elseif($obj['status']=="new"){
369         prepare_to_save_FAI_object($sub_dn,$tmp);
370         $this->handle_post_events("add");
371       }
373       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
374     }
375   }
377   
378   /* Return plugin informations for acl handling */ 
379   function plInfo()
380   {
381     return (array( 
382           "plShortName" => _("Variable"),
383           "plDescription" => _("FAI variable"),
384           "plSelfModify"  => FALSE,
385           "plDepends"     => array(),
386           "plPriority"    => 0,
387           "plSection"     => array("administration"),
388           "plCategory"    => array("fai"),
389           "plProvidedAcls" => array(
390             "cn"                => _("Name"),
391             "description"       => _("Description"),
392             "FAIvariableContent"=> _("Variable content"))
393           ));
394   }
397 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
398 ?>