Code

Fixed ogroup saving
[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'])){
124       $c_dn = $this->dn;
125       if($c_dn == "new"){
126         $c_dn = $_SESSION['CurrentMainBase'];
127       }
129       $this->dialog= new $this->subClassName($this->config,"new");
130       $this->dialog->set_acl_base($c_dn);
131       $this->dialog->set_acl_category("fai");
132       $this->is_dialog=true;
133     }
135     if($this->dn != "new"){
136       $_SESSION['objectinfo']= $this->dn;
137     }
140     /* New Listhandling 
141      */
142     $once = true;
143     foreach($_POST as $name => $value){
144       if(preg_match("/^editscript_/",$name)&&($once)){
146         $once = false;
147         $entry = preg_replace("/^editscript_/","",$name);
148         $entry = base64_decode(preg_replace("/_.*/","",$entry));
150         $obj  = $this->SubObjects[$entry];
151         if($obj['status'] == "FreshLoaded"){
152           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
153         }
154         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
155         $this->dialog->set_acl_base($this->dn);
156         $this->dialog->set_acl_category("fai");
159         $_SESSION['objectinfo'] = $obj['dn'];
160         $this->dialog->parent = &$this;
161         $this->is_dialog=true;
162       }
163       if(preg_match("/^deletescript_/",$name)&&($once)){
164         $once = false;
165         $entry = preg_replace("/^deletescript_/","",$name);
166         $entry = base64_decode(preg_replace("/_.*/","",$entry));
168     
169         $dn = $this->SubObjects[$entry]['dn'];
171         if($dn == "new"){
172           $dn = $_SESSION['CurrentMainBase'];
173         }
174         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
176         if(preg_match("/d/",$acl)){
177           if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
178             $this->SubObjects[$entry]['status']= "delete";
179           }else{
180              unset($this->SubObjects[$entry]);
181           }
182         }
183       }
184     }
185     ///// Ende new list handling
188     /* Edit selected Sub Object */
189     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
190       $script = $_POST['SubObject'][0];
191       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
192       $this->dialog->set_acl_base($_SESSION['CurrentMainBase']);
193       $this->dialog->set_acl_category("fai");
194       $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
195       $this->is_dialog=true;
196     }
199     /* Save Dialog */
200     if(isset($_POST['SaveSubObject'])){
201       $this->dialog->save_object();
202       $msgs = $this->dialog->check();
203       if(count($msgs)>0){
204         foreach($msgs as $msg){
205           print_red($msg);
206         }
207       }else{
208            /* Get return object */
209         $obj = $this->dialog->save();
210         if(isset($obj['remove'])){
212           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
214           /* Depending on status, set new status */
215           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
216             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
217           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
218             unset($this->SubObjects[$obj['remove']['from']]);
219           }
220           $obj['status'] = "new";
221           $this->SubObjects[$obj['remove']['to']] = $obj;
222           unset($this->SubObjects[$obj['remove']['to']]['remove']);
223         }else{
224           if($obj['status'] == "FreshLoaded"){
225             $obj['status'] = "edited";
226           }
227           $this->SubObjects[$obj['cn']]=$obj;
228         }
229         $this->is_dialog=false;
230         unset($this->dialog);
231         $this->dialog=NULL;
233       }
234     }
236     /* Sort entries */
237     $tmp = $keys = array();
238     foreach($this->SubObjects as $key => $entry){
239       $keys[$key]=$key;
240     }
241     natcasesort($keys);
242     foreach($keys as $key){
243       $tmp[$key]=$this->SubObjects[$key];
244     }
245     $this->SubObjects = $tmp;
247     /* Cancel Dialog */
248     if(isset($_POST['CancelSubObject'])){
249       $this->is_dialog=false; 
250       unset($this->dialog);
251       $this->dialog=NULL;
252     }
254     /* Print dialog if $this->dialog is set */
255     if($this->dialog){
256       $this->dialog->save_object();
257       $display = $this->dialog->execute();
258       return($display);
259     }
261     /* Divlist            added 23.02.2006 
262        Containing FAIscripts 
263      */
264     $divlist = new divSelectBox("FAIscripts");
265     $divlist->setHeight(400);
266     foreach($this->getList(true) as $key => $name){
268       $dn= $name['dn'];
269       if($dn == "new"){
270         $dn = $_SESSION['CurrentMainBase'];
271       }
272       $ui = get_userinfo();
273       $acl = $ui->get_permissions($dn,"fai/faiScriptEntry")  ;
274       $act = "";
275       
276       if($this->FAIstate == "freeze"){
277         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
278       }else{
279         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
280         if(preg_match("/d/",$acl)){
281           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
282         }
283       }
285       $s_acl = $ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
286       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
287         $down = "";
288       }else{
289         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
290           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
291           </a>"; 
292       } 
294       $divlist->AddEntry(array( array("string"=>$name['name']),
295             array("string"=>$down , "attach" => "style='width:20px;'"),
296             array("string"=>str_replace("%s",base64_encode($key),$act),
297               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
298     }
299     $smarty->assign("Entry_divlist",$divlist->DrawList());
301     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
302      * If we post the escaped strings they will be escaped again
303      */
304     foreach($this->attributes as $attrs){
305       if(get_magic_quotes_gpc()){
306         $smarty->assign($attrs,stripslashes($this->$attrs));
307       }else{
308         $smarty->assign($attrs,($this->$attrs));
309       }
310     }
312     $tmp = $this->plInfo();
313     $this->ui = get_userinfo();
315     $c_dn = $this->dn;  
316     if($c_dn == "new"){
317       $c_dn = $_SESSION['CurrentMainBase'];
318     }
319     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
320     foreach($tmp['plProvidedAcls'] as $name => $translated){
321       $smarty->assign($name."ACL",$this->getacl($name));
322     }
324     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
325     return($display);
326   }
328   /* Generate listbox friendly SubObject list
329    */
330   function getList($use_dns=false){
331     $a_return=array();
332     foreach($this->SubObjects as $obj){
333       if($obj['status'] != "delete"){
334         if($use_dns){
335           if((isset($obj['description']))&&(!empty($obj['description']))){
336             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
337           }else{
338             $a_return[$obj['cn']]['name']= $obj['cn'];
339           }
340           $a_return[$obj['cn']]['dn']= $obj['dn'];
341         }else{
342           if((isset($obj['description']))&&(!empty($obj['description']))){
343             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
344           }else{
345             $a_return[$obj['cn']]= $obj['cn'];
346           }
347         }
348       }
349     }
350     return($a_return);
351   }
353   /* Delete me, and all my subtrees
354    */
355   function remove_from_parent()
356   {
357     $ldap = $this->config->get_ldap_link();
358     $ldap->cd ($this->dn);
360 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
361     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
362     if($_SESSION['faifilter']['branch'] == "main"){
363       $use_dn = $this->dn;
364     }
365   
366     prepare_to_save_FAI_object($use_dn,array(),true);
367     
368     foreach($this->SubObjects as $name => $obj){
369 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
370       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
371       if($_SESSION['faifilter']['branch'] == "main"){
372         $use_dn = $obj['dn'];
373       }
374       prepare_to_save_FAI_object($use_dn,array(),true);
375     }
376     $this->handle_post_events("remove");
377   }
380   /* Save data to object 
381    */
382   function save_object()
383   {
384     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
385       plugin::save_object();
386       foreach($this->attributes as $attrs){
387         if(isset($_POST[$attrs])){
388           $this->$attrs = $_POST[$attrs];
389         }
390       }
391     }
392   }
395   /* Check supplied data */
396   function check()
397   {
398     /* Call common method to give check the hook */
399     $message= plugin::check();
401     return ($message);
402   }
405   /* Save to LDAP */
406   function save()
407   {
408     plugin::save();
410     $ldap = $this->config->get_ldap_link();
412     prepare_to_save_FAI_object($this->dn,$this->attrs);
413     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
415     /* Do object tagging */
416     $this->handle_object_tagging();
418     /* Prepare FAIscriptEntry to write it to ldap
419      * First sort array.
420      *  Because we must delete old entries first.
421      * After deletion, we perform add and modify 
422      */
423     $Objects = array();
425     /* We do not need to save untouched objects */
426     foreach($this->SubObjects as $name => $obj){
427       if($obj['status'] == "FreshLoaded"){
428         unset($this->SubObjects[$name]);
429       }
430     }
432     foreach($this->SubObjects as $name => $obj){
433       if($obj['status'] == "delete"){
434         $Objects[$name] = $obj; 
435       }
436     }
437     foreach($this->SubObjects as $name => $obj){
438       if($obj['status'] != "delete"){
439         $Objects[$name] = $obj; 
440       }
441     }
443     foreach($Objects as $name => $obj){
445       foreach($this->sub64coded as $codeIt){
446         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
447       }
449       $tmp = array();
450       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
451       foreach($attributes as $attrs){
453         if(empty($obj[$attrs])){
454           $obj[$attrs] = array();
455         }
456         if(!is_array($obj[$attrs])){
457           $tmp[$attrs] = stripslashes($obj[$attrs]);
458         }else{
459           $tmp[$attrs] = $obj[$attrs];
460         }
461       }    
463       $tmp['objectClass'] = $this->subClasses;
465       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
467       if($obj['status']=="new"){
468         $ldap->cat($sub_dn,array("objectClass"));
469         if($ldap->count()){
470           $obj['status']="edited";
471         }
472       }
474       if(empty($tmp['FAIpriority'])){
475         $tmp['FAIpriority']  ="0";
476       }
478       /* Check if gosaAdministrativeUnitTag is required as object class */
479       if($obj['status'] == "edited"){
480         $ldap->cat($sub_dn,array("objectClass"));
481         $attrs = $ldap->fetch();
482         if(isset($attrs['objectClass'])){
483           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
484             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
485           }
486         }
487       }
489       if($obj['status'] == "delete"){
490         prepare_to_save_FAI_object($sub_dn,array(),true);
491         $this->handle_post_events("remove");
492       }elseif($obj['status'] == "edited"){
493         prepare_to_save_FAI_object($sub_dn,$tmp);
494         $this->handle_post_events("modify");
495       }elseif($obj['status']=="new"){
496         prepare_to_save_FAI_object($sub_dn,$tmp);
497         $this->handle_post_events("add");
498       }
500       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
501     }
502   }
503   
505   /* Return plugin informations for acl handling */ 
506   function plInfo()
507   {
508     return (array( 
509           "plShortName" => _("Script"),
510           "plDescription" => _("FAI script"),
511           "plSelfModify"  => FALSE,
512           "plDepends"     => array(),
513           "plPriority"    => 18,
514           "plSection"     => array("administration"),
515           "plCategory"    => array("fai"),
516           "plProvidedAcls" => array(
517             "cn"                => _("Name")." ("._("Readonly").")",
518             "description"       => _("Description"))
519           ));
520   }
523 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
524 ?>