Code

Filter stripping
[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   var $ui;
43   function faiScript ($config, $dn= NULL)
44   {
45     /* Load Attributes */
46     plugin::plugin ($config, $dn);
48     /* If "dn==new" we try to create a new entry
49      * Else we must read all objects from ldap which belong to this entry.
50      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
51      */
52     if($dn != "new"){
54       $this->dn =$dn;
56       /* Get FAIstate
57        */
58       if(isset($this->attrs['FAIstate'][0])){
59         $this->FAIstate = $this->attrs['FAIstate'][0];
60       }
62       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
63        */
64       $ldap     = $this->config->get_ldap_link();
65       $ldap->cd ($this->dn);
66       
67       $attrs_to_search = $this->subAttributes;
68       $attrs_to_search[] = "FAIstate";
69       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
71       while($object = $ldap->fetch()){
72         /* Set status for save management */
73         $objects = array();
74         $objects['status']      = "FreshLoaded";
75         $objects['dn']          = $object['dn'];
76         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
77         $this->SubObjects[$objects['cn']] = $objects;
78       }
79     }
80     $this->ui = get_userinfo();
81   }
84   /* Reload some attributes */
85   function get_object_attributes($object,$attributes)
86   {
87     $ldap = $this->config->get_ldap_link();
88     $ldap->cd($this->config->current['BASE']);
89     $ldap->cat($object['dn'],$attributes);
90     $tmp  = $ldap->fetch();
92     foreach($attributes as $attrs){
93       if(isset($tmp[$attrs][0])){
94         $var = $tmp[$attrs][0];
96         /* Check if we must decode some attributes */
97         if(in_array_ics($attrs,$this->sub64coded)){
98           $var = base64_decode($var);
99         }
101         /*  check if this is a binary entry */
102         if(in_array_ics($attrs,$this->subBinary)){
103           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
104         }
106         /* Fix slashes */
107         $var = addslashes($var);
108         $object[$attrs] = $var;
109       }
110     }
111     return($object);
112   }
114   function acl_base_for_current_object($dn)
115   {
116     if($dn == "new"){
117       if($this->dn == "new"){
118         $dn= $_SESSION['CurrentMainBase'];
119       }else{
120         $dn = $this->dn;
121       }
122     }
123     return($dn);
124   }
127   function execute()
128   {
129     /* Call parent execute */
130     plugin::execute();
132     /* Fill templating stuff */
133     $smarty= get_smarty();
134     $display= "";
136     /* Add new sub object */
137     if(isset($_POST['AddSubObject'])){
138       $this->dialog= new $this->subClassName($this->config,"new");
139       $this->dialog->set_acl_base($this->acl_base);
140       $this->dialog->set_acl_category("fai");
141       $this->is_dialog=true;
142     }
144     if($this->dn != "new"){
145       $_SESSION['objectinfo']= $this->dn;
146     }
148     /* New Listhandling 
149      */
150     $once = true;
151     foreach($_POST as $name => $value){
152       if(preg_match("/^editscript_/",$name)&&($once)){
154         $once = false;
155         $entry = preg_replace("/^editscript_/","",$name);
156         $entry = base64_decode(preg_replace("/_.*/","",$entry));
158         $obj  = $this->SubObjects[$entry];
159         if($obj['status'] == "FreshLoaded"){
160           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
161         }
162         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
163         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
164         $this->dialog->set_acl_category("fai");
167         $_SESSION['objectinfo'] = $obj['dn'];
168         $this->dialog->parent = &$this;
169         $this->is_dialog=true;
170       }
171       if(preg_match("/^deletescript_/",$name)&&($once)){
172         $once = false;
173         $entry = preg_replace("/^deletescript_/","",$name);
174         $entry = base64_decode(preg_replace("/_.*/","",$entry));
176         $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry")  ;
177         if(preg_match("/d/",$acl)){
178           if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
179             $this->SubObjects[$entry]['status']= "delete";
180           }else{
181              unset($this->SubObjects[$entry]);
182           }
183         }
184       }
185     }
186     ///// Ende new list handling
189     /* Edit selected Sub Object */
190     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
191       $script = $_POST['SubObject'][0];
192       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
193       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->SubObjects[$script]['dn']));
194       $this->dialog->set_acl_category("fai");
195       $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
196       $this->is_dialog=true;
197     }
200     /* Save Dialog */
201     if(isset($_POST['SaveSubObject'])){
202       $this->dialog->save_object();
203       $msgs = $this->dialog->check();
204       if(count($msgs)>0){
205         foreach($msgs as $msg){
206           print_red($msg);
207         }
208       }else{
209            /* Get return object */
210         $obj = $this->dialog->save();
211         if(isset($obj['remove'])){
213           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
215           /* Depending on status, set new status */
216           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
217             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
218           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
219             unset($this->SubObjects[$obj['remove']['from']]);
220           }
221           $obj['status'] = "new";
222           $this->SubObjects[$obj['remove']['to']] = $obj;
223           unset($this->SubObjects[$obj['remove']['to']]['remove']);
224         }else{
225           if($obj['status'] == "FreshLoaded"){
226             $obj['status'] = "edited";
227           }
228           $this->SubObjects[$obj['cn']]=$obj;
229         }
230         $this->is_dialog=false;
231         unset($this->dialog);
232         $this->dialog=NULL;
234       }
235     }
237     /* Sort entries */
238     $tmp = $keys = array();
239     foreach($this->SubObjects as $key => $entry){
240       $keys[$key]=$key;
241     }
242     natcasesort($keys);
243     foreach($keys as $key){
244       $tmp[$key]=$this->SubObjects[$key];
245     }
246     $this->SubObjects = $tmp;
248     /* Cancel Dialog */
249     if(isset($_POST['CancelSubObject'])){
250       $this->is_dialog=false; 
251       unset($this->dialog);
252       $this->dialog=NULL;
253     }
255     /* Print dialog if $this->dialog is set */
256     if($this->dialog){
257       $this->dialog->save_object();
258       $display = $this->dialog->execute();
259       return($display);
260     }
262     /* Divlist            added 23.02.2006 
263        Containing FAIscripts 
264      */
265     $divlist = new divSelectBox("FAIscripts");
266     $divlist->setHeight(400);
267     foreach($this->getList(true) as $key => $name){
269       $dn= $this->acl_base_for_current_object($name['dn']);
270       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
271       $act = "";
272       
273       if($this->FAIstate == "freeze"){
274         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
275       }else{
276         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
277         if(preg_match("/d/",$acl)){
278           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
279         }
280       }
282       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
283       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
284         $down = "";
285       }else{
286         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
287           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
288           </a>"; 
289       } 
291       $divlist->AddEntry(array( array("string"=>$name['name']),
292             array("string"=>$down , "attach" => "style='width:20px;'"),
293             array("string"=>str_replace("%s",base64_encode($key),$act),
294               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
295     }
296     $smarty->assign("Entry_divlist",$divlist->DrawList());
298     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
299      * If we post the escaped strings they will be escaped again
300      */
301     foreach($this->attributes as $attrs){
302       if(get_magic_quotes_gpc()){
303         $smarty->assign($attrs,stripslashes($this->$attrs));
304       }else{
305         $smarty->assign($attrs,($this->$attrs));
306       }
307     }
309     $dn = $this->acl_base_for_current_object($this->dn);
310     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
311     $tmp = $this->plInfo();
312     foreach($tmp['plProvidedAcls'] as $name => $translated){
313       $smarty->assign($name."ACL",$this->getacl($name));
314     }
316     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
317     return($display);
318   }
320   /* Generate listbox friendly SubObject list
321    */
322   function getList($use_dns=false){
323     $a_return=array();
324     foreach($this->SubObjects as $obj){
325       if($obj['status'] != "delete"){
326         if($use_dns){
327           if((isset($obj['description']))&&(!empty($obj['description']))){
328             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
329           }else{
330             $a_return[$obj['cn']]['name']= $obj['cn'];
331           }
332           $a_return[$obj['cn']]['dn']= $obj['dn'];
333         }else{
334           if((isset($obj['description']))&&(!empty($obj['description']))){
335             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
336           }else{
337             $a_return[$obj['cn']]= $obj['cn'];
338           }
339         }
340       }
341     }
342     return($a_return);
343   }
345   /* Delete me, and all my subtrees
346    */
347   function remove_from_parent()
348   {
349     if($this->acl_is_removeable()){
350       $ldap = $this->config->get_ldap_link();
351       $ldap->cd ($this->dn);
353       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
354       if($_SESSION['faifilter']['branch'] == "main"){
355         $use_dn = $this->dn;
356       }
357     
358       prepare_to_save_FAI_object($use_dn,array(),true);
359       
360       foreach($this->SubObjects as $name => $obj){
361         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
362         if($_SESSION['faifilter']['branch'] == "main"){
363           $use_dn = $obj['dn'];
364         }
365         prepare_to_save_FAI_object($use_dn,array(),true);
366       }
367       $this->handle_post_events("remove");
368     }
369   }
372   /* Save data to object 
373    */
374   function save_object()
375   {
376     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
377       plugin::save_object();
378       foreach($this->attributes as $attrs){
379         if(isset($_POST[$attrs])){
380           $this->$attrs = $_POST[$attrs];
381         }
382       }
383     }
384   }
387   /* Check supplied data */
388   function check()
389   {
390     /* Call common method to give check the hook */
391     $message= plugin::check();
393     return ($message);
394   }
397   /* Save to LDAP */
398   function save()
399   {
400     plugin::save();
402     $ldap = $this->config->get_ldap_link();
404     prepare_to_save_FAI_object($this->dn,$this->attrs);
405     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
407     /* Do object tagging */
408     $this->handle_object_tagging();
410     /* Prepare FAIscriptEntry to write it to ldap
411      * First sort array.
412      *  Because we must delete old entries first.
413      * After deletion, we perform add and modify 
414      */
415     $Objects = array();
417     /* We do not need to save untouched objects */
418     foreach($this->SubObjects as $name => $obj){
419       if($obj['status'] == "FreshLoaded"){
420         unset($this->SubObjects[$name]);
421       }
422     }
424     foreach($this->SubObjects as $name => $obj){
425       if($obj['status'] == "delete"){
426         $Objects[$name] = $obj; 
427       }
428     }
429     foreach($this->SubObjects as $name => $obj){
430       if($obj['status'] != "delete"){
431         $Objects[$name] = $obj; 
432       }
433     }
435     foreach($Objects as $name => $obj){
437       foreach($this->sub64coded as $codeIt){
438         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
439       }
441       $tmp = array();
442       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
443       foreach($attributes as $attrs){
445         if(empty($obj[$attrs])){
446           $obj[$attrs] = array();
447         }
448         if(!is_array($obj[$attrs])){
449           $tmp[$attrs] = stripslashes($obj[$attrs]);
450         }else{
451           $tmp[$attrs] = $obj[$attrs];
452         }
453       }    
455       $tmp['objectClass'] = $this->subClasses;
457       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
459       if($obj['status']=="new"){
460         $ldap->cat($sub_dn,array("objectClass"));
461         if($ldap->count()){
462           $obj['status']="edited";
463         }
464       }
466       if(empty($tmp['FAIpriority'])){
467         $tmp['FAIpriority']  ="0";
468       }
470       /* Check if gosaAdministrativeUnitTag is required as object class */
471       if($obj['status'] == "edited"){
472         $ldap->cat($sub_dn,array("objectClass"));
473         $attrs = $ldap->fetch();
474         if(isset($attrs['objectClass'])){
475           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
476             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
477           }
478         }
479       }
481       if($obj['status'] == "delete"){
482         prepare_to_save_FAI_object($sub_dn,array(),true);
483         $this->handle_post_events("remove");
484       }elseif($obj['status'] == "edited"){
485         prepare_to_save_FAI_object($sub_dn,$tmp);
486         $this->handle_post_events("modify");
487       }elseif($obj['status']=="new"){
488         prepare_to_save_FAI_object($sub_dn,$tmp);
489         $this->handle_post_events("add");
490       }
492       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
493     }
494   }
495   
497   /* Return plugin informations for acl handling */ 
498   function plInfo()
499   {
500     return (array( 
501           "plShortName" => _("Script"),
502           "plDescription" => _("FAI script"),
503           "plSelfModify"  => FALSE,
504           "plDepends"     => array(),
505           "plPriority"    => 18,
506           "plSection"     => array("administration"),
507           "plCategory"    => array("fai"),
508           "plProvidedAcls" => array(
509             "cn"                => _("Name")." ("._("Readonly").")",
510             "description"       => _("Description"))
511           ));
512   }
515 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
516 ?>