Code

Recent changes in win2003sp1 required some fixes (backward compatible).
[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();
289     $c_dn = $this->dn;  
290     if($c_dn == "new"){
291       $c_dn = $_SESSION['CurrentMainBase'];
292     }
293     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
294     foreach($tmp['plProvidedAcls'] as $name => $translated){
295       $smarty->assign($name."ACL",$this->getacl($name));
296     }
298     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
299     return($display);
300   }
302   /* Generate listbox friendly SubObject list
303    */
304   function getList($use_dns=false){
305     $a_return=array();
306     foreach($this->SubObjects as $obj){
307       if($obj['status'] != "delete"){
308         if($use_dns){
309           if((isset($obj['description']))&&(!empty($obj['description']))){
310             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
311           }else{
312             $a_return[$obj['cn']]['name']= $obj['cn'];
313           }
314           $a_return[$obj['cn']]['dn']= $obj['dn'];
315         }else{
316           if((isset($obj['description']))&&(!empty($obj['description']))){
317             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
318           }else{
319             $a_return[$obj['cn']]= $obj['cn'];
320           }
321         }
322       }
323     }
324     return($a_return);
325   }
327   /* Delete me, and all my subtrees
328    */
329   function remove_from_parent()
330   {
331     $ldap = $this->config->get_ldap_link();
332     $ldap->cd ($this->dn);
334 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
335     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
336     if($_SESSION['faifilter']['branch'] == "main"){
337       $use_dn = $this->dn;
338     }
339   
340     prepare_to_save_FAI_object($use_dn,array(),true);
341     
342     foreach($this->SubObjects as $name => $obj){
343 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
344       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
345       if($_SESSION['faifilter']['branch'] == "main"){
346         $use_dn = $obj['dn'];
347       }
348       prepare_to_save_FAI_object($use_dn,array(),true);
349     }
350     $this->handle_post_events("remove");
351   }
354   /* Save data to object 
355    */
356   function save_object()
357   {
358     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
359       plugin::save_object();
360       foreach($this->attributes as $attrs){
361         if(isset($_POST[$attrs])){
362           $this->$attrs = $_POST[$attrs];
363         }
364       }
365     }
366   }
369   /* Check supplied data */
370   function check()
371   {
372     /* Call common method to give check the hook */
373     $message= plugin::check();
375     return ($message);
376   }
379   /* Save to LDAP */
380   function save()
381   {
382     plugin::save();
384     $ldap = $this->config->get_ldap_link();
386     prepare_to_save_FAI_object($this->dn,$this->attrs);
387     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
389     /* Do object tagging */
390     $this->handle_object_tagging();
392     /* Prepare FAIscriptEntry to write it to ldap
393      * First sort array.
394      *  Because we must delete old entries first.
395      * After deletion, we perform add and modify 
396      */
397     $Objects = array();
399     /* We do not need to save untouched objects */
400     foreach($this->SubObjects as $name => $obj){
401       if($obj['status'] == "FreshLoaded"){
402         unset($this->SubObjects[$name]);
403       }
404     }
406     foreach($this->SubObjects as $name => $obj){
407       if($obj['status'] == "delete"){
408         $Objects[$name] = $obj; 
409       }
410     }
411     foreach($this->SubObjects as $name => $obj){
412       if($obj['status'] != "delete"){
413         $Objects[$name] = $obj; 
414       }
415     }
417     foreach($Objects as $name => $obj){
419       foreach($this->sub64coded as $codeIt){
420         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
421       }
423       $tmp = array();
424       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
425       foreach($attributes as $attrs){
427         if(empty($obj[$attrs])){
428           $obj[$attrs] = array();
429         }
430         if(!is_array($obj[$attrs])){
431           $tmp[$attrs] = stripslashes($obj[$attrs]);
432         }else{
433           $tmp[$attrs] = $obj[$attrs];
434         }
435       }    
437       $tmp['objectClass'] = $this->subClasses;
439       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
441       if($obj['status']=="new"){
442         $ldap->cat($sub_dn,array("objectClass"));
443         if($ldap->count()){
444           $obj['status']="edited";
445         }
446       }
448       if(empty($tmp['FAIpriority'])){
449         $tmp['FAIpriority']  ="0";
450       }
452       /* Check if gosaAdministrativeUnitTag is required as object class */
453       if($obj['status'] == "edited"){
454         $ldap->cat($sub_dn,array("objectClass"));
455         $attrs = $ldap->fetch();
456         if(isset($attrs['objectClass'])){
457           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
458             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
459           }
460         }
461       }
463       if($obj['status'] == "delete"){
464         prepare_to_save_FAI_object($sub_dn,array(),true);
465         $this->handle_post_events("remove");
466       }elseif($obj['status'] == "edited"){
467         prepare_to_save_FAI_object($sub_dn,$tmp);
468         $this->handle_post_events("modify");
469       }elseif($obj['status']=="new"){
470         prepare_to_save_FAI_object($sub_dn,$tmp);
471         $this->handle_post_events("add");
472       }
474       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
475     }
476   }
477   
479   /* Return plugin informations for acl handling */ 
480   function plInfo()
481   {
482     return (array( 
483           "plShortName" => _("Script"),
484           "plDescription" => _("FAI script"),
485           "plSelfModify"  => FALSE,
486           "plDepends"     => array(),
487           "plPriority"    => 0,
488           "plSection"     => array("administration"),
489           "plCategory"    => array("fai"),
490           "plProvidedAcls" => array(
491             "cn"                => _("Name")." ("._("Readonly").")",
492             "description"       => _("Description"))
493           ));
494   }
497 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
498 ?>