Code

Readded action command to template
[gosa.git] / plugins / admin / fai / class_faiScript.inc
1 <?php
3 class faiScript 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","FAIscript");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIscriptEntry";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiScriptEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIpriority"); 
28   var $sub_Load_Later   = array("FAIscript");
29   var $sub64coded       = array();
30   var $subBinary        = array("FAIscript");
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 $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIstate         ="";
41   function faiScript ($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
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"){
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()){
70         /* Set status for save management */
71         $objects = array();
72         $objects['status']      = "FreshLoaded";
73         $objects['dn']          = $object['dn'];
74         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
75         $this->SubObjects[$objects['cn']] = $objects;
76       }
77     }
78   }
81   /* Reload some attributes */
82   function get_object_attributes($object,$attributes)
83   {
84     $ldap = $this->config->get_ldap_link();
85     $ldap->cd($this->config->current['BASE']);
86     $ldap->cat($object['dn'],$attributes);
87     $tmp  = $ldap->fetch();
89     foreach($attributes as $attrs){
90       if(isset($tmp[$attrs][0])){
91         $var = $tmp[$attrs][0];
93         /* Check if we must decode some attributes */
94         if(in_array_ics($attrs,$this->sub64coded)){
95           $var = base64_decode($var);
96         }
98         /*  check if this is a binary entry */
99         if(in_array_ics($attrs,$this->subBinary)){
100           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
101         }
103         /* Fix slashes */
104         $var = addslashes($var);
105         $object[$attrs] = $var;
106       }
107     }
108     return($object);
109   }
112   function execute()
113   {
114     /* Call parent execute */
115     plugin::execute();
117     /* Fill templating stuff */
118     $smarty= get_smarty();
119     $display= "";
121     /* Add new sub object */
122     if(isset($_POST['AddSubObject'])){
123       $this->dialog= new $this->subClassName($this->config,"new");
124       $this->dialog->set_acl_base($_SESSION['CurrentMainBase']);
125       $this->dialog->set_acl_category("fai");
126       $this->is_dialog=true;
127     }
129     if($this->dn != "new"){
130       $_SESSION['objectinfo']= $this->dn;
131     }
134     /* New Listhandling 
135      */
136     $once = true;
137     foreach($_POST as $name => $value){
138       if(preg_match("/^editscript_/",$name)&&($once)){
140         $once = false;
141         $entry = preg_replace("/^editscript_/","",$name);
142         $entry = base64_decode(preg_replace("/_.*/","",$entry));
144         $obj  = $this->SubObjects[$entry];
145         if($obj['status'] == "FreshLoaded"){
146           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
147         }
148         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
149         $this->dialog->set_acl_base($this->dn);
150         $this->dialog->set_acl_category("fai");
153         $_SESSION['objectinfo'] = $obj['dn'];
154         $this->dialog->parent = &$this;
155         $this->is_dialog=true;
156       }
157       if(preg_match("/^deletescript_/",$name)&&($once)){
158         $once = false;
159         $entry = preg_replace("/^deletescript_/","",$name);
160         $entry = base64_decode(preg_replace("/_.*/","",$entry));
162         if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
163           $this->SubObjects[$entry]['status']= "delete";
164         }else{
165           unset($this->SubObjects[$entry]);
166         }
167       }
168     }
169     ///// Ende new list handling
172     /* Edit selected Sub Object */
173     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
174       $script = $_POST['SubObject'][0];
175       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
176       $this->dialog->set_acl_base($_SESSION['CurrentMainBase']);
177       $this->dialog->set_acl_category("fai");
178       $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
179       $this->is_dialog=true;
180     }
183     /* Save Dialog */
184     if(isset($_POST['SaveSubObject'])){
185       $this->dialog->save_object();
186       $msgs = $this->dialog->check();
187       if(count($msgs)>0){
188         foreach($msgs as $msg){
189           print_red($msg);
190         }
191       }else{
192            /* Get return object */
193         $obj = $this->dialog->save();
194         if(isset($obj['remove'])){
196           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
198           /* Depending on status, set new status */
199           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
200             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
201           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
202             unset($this->SubObjects[$obj['remove']['from']]);
203           }
204           $obj['status'] = "new";
205           $this->SubObjects[$obj['remove']['to']] = $obj;
206           unset($this->SubObjects[$obj['remove']['to']]['remove']);
207         }else{
208           if($obj['status'] == "FreshLoaded"){
209             $obj['status'] = "edited";
210           }
211           $this->SubObjects[$obj['cn']]=$obj;
212         }
213         $this->is_dialog=false;
214         unset($this->dialog);
215         $this->dialog=NULL;
217       }
218     }
220     /* Sort entries */
221     $tmp = $keys = array();
222     foreach($this->SubObjects as $key => $entry){
223       $keys[$key]=$key;
224     }
225     natcasesort($keys);
226     foreach($keys as $key){
227       $tmp[$key]=$this->SubObjects[$key];
228     }
229     $this->SubObjects = $tmp;
231     /* Cancel Dialog */
232     if(isset($_POST['CancelSubObject'])){
233       $this->is_dialog=false; 
234       unset($this->dialog);
235       $this->dialog=NULL;
236     }
238     /* Print dialog if $this->dialog is set */
239     if($this->dialog){
240       $this->dialog->save_object();
241       $display = $this->dialog->execute();
242       return($display);
243     }
245     /* Divlist            added 23.02.2006 
246        Containing FAIscripts 
247      */
248     $divlist = new divSelectBox("FAIscripts");
249     $divlist->setHeight(400);
250     if(($this->FAIstate == "freeze")){
251       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
252       $img_remo = "";
253     }else{
254       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
255       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
256     }
258     foreach($this->getList(true) as $key => $name){
260       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
261         $down = "";
262       }else{
263         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
264           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
265           </a>"; 
266       } 
268       $divlist->AddEntry(array( array("string"=>$name['name']),
269             array("string"=>$down , "attach" => "style='width:20px;'"),
270             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
271               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
272     }
273     $smarty->assign("Entry_divlist",$divlist->DrawList());
275     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
276      * If we post the escaped strings they will be escaped again
277      */
278     foreach($this->attributes as $attrs){
279       if(get_magic_quotes_gpc()){
280         $smarty->assign($attrs,stripslashes($this->$attrs));
281       }else{
282         $smarty->assign($attrs,($this->$attrs));
283       }
284     }
286     $tmp = $this->plInfo();
287     $this->ui = get_userinfo();
288     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($this->dn,"fai/faiScriptEntry")));
289     foreach($tmp['plProvidedAcls'] as $name => $translated){
290       $smarty->assign($name."ACL",$this->getacl($name));
291     }
293     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
294     return($display);
295   }
297   /* Generate listbox friendly SubObject list
298    */
299   function getList($use_dns=false){
300     $a_return=array();
301     foreach($this->SubObjects as $obj){
302       if($obj['status'] != "delete"){
303         if($use_dns){
304           if((isset($obj['description']))&&(!empty($obj['description']))){
305             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
306           }else{
307             $a_return[$obj['cn']]['name']= $obj['cn'];
308           }
309           $a_return[$obj['cn']]['dn']= $obj['dn'];
310         }else{
311           if((isset($obj['description']))&&(!empty($obj['description']))){
312             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
313           }else{
314             $a_return[$obj['cn']]= $obj['cn'];
315           }
316         }
317       }
318     }
319     return($a_return);
320   }
322   /* Delete me, and all my subtrees
323    */
324   function remove_from_parent()
325   {
326     $ldap = $this->config->get_ldap_link();
327     $ldap->cd ($this->dn);
329 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
330     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
331     if($_SESSION['faifilter']['branch'] == "main"){
332       $use_dn = $this->dn;
333     }
334   
335     prepare_to_save_FAI_object($use_dn,array(),true);
336     
337     foreach($this->SubObjects as $name => $obj){
338 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
339       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
340       if($_SESSION['faifilter']['branch'] == "main"){
341         $use_dn = $obj['dn'];
342       }
343       prepare_to_save_FAI_object($use_dn,array(),true);
344     }
345     $this->handle_post_events("remove");
346   }
349   /* Save data to object 
350    */
351   function save_object()
352   {
353     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
354       plugin::save_object();
355       foreach($this->attributes as $attrs){
356         if(isset($_POST[$attrs])){
357           $this->$attrs = $_POST[$attrs];
358         }
359       }
360     }
361   }
364   /* Check supplied data */
365   function check()
366   {
367     /* Call common method to give check the hook */
368     $message= plugin::check();
370     return ($message);
371   }
374   /* Save to LDAP */
375   function save()
376   {
377     plugin::save();
379     $ldap = $this->config->get_ldap_link();
381     prepare_to_save_FAI_object($this->dn,$this->attrs);
382     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
384     /* Do object tagging */
385     $this->handle_object_tagging();
387     /* Prepare FAIscriptEntry to write it to ldap
388      * First sort array.
389      *  Because we must delete old entries first.
390      * After deletion, we perform add and modify 
391      */
392     $Objects = array();
394     /* We do not need to save untouched objects */
395     foreach($this->SubObjects as $name => $obj){
396       if($obj['status'] == "FreshLoaded"){
397         unset($this->SubObjects[$name]);
398       }
399     }
401     foreach($this->SubObjects as $name => $obj){
402       if($obj['status'] == "delete"){
403         $Objects[$name] = $obj; 
404       }
405     }
406     foreach($this->SubObjects as $name => $obj){
407       if($obj['status'] != "delete"){
408         $Objects[$name] = $obj; 
409       }
410     }
412     foreach($Objects as $name => $obj){
414       foreach($this->sub64coded as $codeIt){
415         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
416       }
418       $tmp = array();
419       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
420       foreach($attributes as $attrs){
422         if(empty($obj[$attrs])){
423           $obj[$attrs] = array();
424         }
425         if(!is_array($obj[$attrs])){
426           $tmp[$attrs] = stripslashes($obj[$attrs]);
427         }else{
428           $tmp[$attrs] = $obj[$attrs];
429         }
430       }    
432       $tmp['objectClass'] = $this->subClasses;
434       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
436       if($obj['status']=="new"){
437         $ldap->cat($sub_dn,array("objectClass"));
438         if($ldap->count()){
439           $obj['status']="edited";
440         }
441       }
443       if(empty($tmp['FAIpriority'])){
444         $tmp['FAIpriority']  ="0";
445       }
447       /* Check if gosaAdministrativeUnitTag is required as object class */
448       if($obj['status'] == "edited"){
449         $ldap->cat($sub_dn,array("objectClass"));
450         $attrs = $ldap->fetch();
451         if(isset($attrs['objectClass'])){
452           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
453             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
454           }
455         }
456       }
458       if($obj['status'] == "delete"){
459         prepare_to_save_FAI_object($sub_dn,array(),true);
460         $this->handle_post_events("remove");
461       }elseif($obj['status'] == "edited"){
462         prepare_to_save_FAI_object($sub_dn,$tmp);
463         $this->handle_post_events("modify");
464       }elseif($obj['status']=="new"){
465         prepare_to_save_FAI_object($sub_dn,$tmp);
466         $this->handle_post_events("add");
467       }
469       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
470     }
471   }
472   
474   /* Return plugin informations for acl handling */ 
475   function plInfo()
476   {
477     return (array( 
478           "plShortName" => _("Script"),
479           "plDescription" => _("FAI script"),
480           "plSelfModify"  => FALSE,
481           "plDepends"     => array(),
482           "plPriority"    => 0,
483           "plSection"     => array("administration"),
484           "plCategory"    => array("fai"),
485           "plProvidedAcls" => array(
486             "cn"                => _("Name")." ("._("Readonly").")",
487             "description"       => _("Description"))
488           ));
489   }
492 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
493 ?>