Code

Fixed export/edit/remove acls for fai-script.
[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       /* Hide delete icon if this object is freezed */
274       if($this->FAIstate == "freeze"){
275         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
276       }else{
277         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
278         if(preg_match("/d/",$acl)){
279           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
280         }
281       }
283       /* Check acls for download icon */
284       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
285       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
286         $down = "";
287       }else{
288         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
289           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
290           </a>"; 
291       } 
293       /* Check if we are allowed to view this object */
294       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn")  ;
295       if(preg_match("/r/",$s_acl)){
296         $divlist->AddEntry(array( array("string"=>$name['name']),
297               array("string"=>$down , "attach" => "style='width:20px;'"),
298               array("string"=>str_replace("%s",base64_encode($key),$act),
299                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
300       }
301     }
302     $smarty->assign("Entry_divlist",$divlist->DrawList());
304     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
305      * If we post the escaped strings they will be escaped again
306      */
307     foreach($this->attributes as $attrs){
308       if(get_magic_quotes_gpc()){
309         $smarty->assign($attrs,stripslashes($this->$attrs));
310       }else{
311         $smarty->assign($attrs,($this->$attrs));
312       }
313     }
315     $dn = $this->acl_base_for_current_object($this->dn);
316     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
317     $tmp = $this->plInfo();
318     foreach($tmp['plProvidedAcls'] as $name => $translated){
319       $smarty->assign($name."ACL",$this->getacl($name));
320     }
322     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
323     return($display);
324   }
326   /* Generate listbox friendly SubObject list
327    */
328   function getList($use_dns=false){
329     $a_return=array();
330     foreach($this->SubObjects as $obj){
331       if($obj['status'] != "delete"){
332         if($use_dns){
333           if((isset($obj['description']))&&(!empty($obj['description']))){
334             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
335           }else{
336             $a_return[$obj['cn']]['name']= $obj['cn'];
337           }
338           $a_return[$obj['cn']]['dn']= $obj['dn'];
339         }else{
340           if((isset($obj['description']))&&(!empty($obj['description']))){
341             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
342           }else{
343             $a_return[$obj['cn']]= $obj['cn'];
344           }
345         }
346       }
347     }
348     return($a_return);
349   }
351   /* Delete me, and all my subtrees
352    */
353   function remove_from_parent()
354   {
355     if($this->acl_is_removeable()){
356       $ldap = $this->config->get_ldap_link();
357       $ldap->cd ($this->dn);
359       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
360       if($_SESSION['faifilter']['branch'] == "main"){
361         $use_dn = $this->dn;
362       }
363     
364       prepare_to_save_FAI_object($use_dn,array(),true);
365       
366       foreach($this->SubObjects as $name => $obj){
367         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
368         if($_SESSION['faifilter']['branch'] == "main"){
369           $use_dn = $obj['dn'];
370         }
371         prepare_to_save_FAI_object($use_dn,array(),true);
372       }
373       $this->handle_post_events("remove");
374     }
375   }
378   /* Save data to object 
379    */
380   function save_object()
381   {
382     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
383       plugin::save_object();
384       foreach($this->attributes as $attrs){
385         if(isset($_POST[$attrs])){
386           $this->$attrs = $_POST[$attrs];
387         }
388       }
389     }
390   }
393   /* Check supplied data */
394   function check()
395   {
396     /* Call common method to give check the hook */
397     $message= plugin::check();
399     return ($message);
400   }
403   /* Save to LDAP */
404   function save()
405   {
406     plugin::save();
408     $ldap = $this->config->get_ldap_link();
410     prepare_to_save_FAI_object($this->dn,$this->attrs);
411     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
413     /* Do object tagging */
414     $this->handle_object_tagging();
416     /* Prepare FAIscriptEntry to write it to ldap
417      * First sort array.
418      *  Because we must delete old entries first.
419      * After deletion, we perform add and modify 
420      */
421     $Objects = array();
423     /* We do not need to save untouched objects */
424     foreach($this->SubObjects as $name => $obj){
425       if($obj['status'] == "FreshLoaded"){
426         unset($this->SubObjects[$name]);
427       }
428     }
430     foreach($this->SubObjects as $name => $obj){
431       if($obj['status'] == "delete"){
432         $Objects[$name] = $obj; 
433       }
434     }
435     foreach($this->SubObjects as $name => $obj){
436       if($obj['status'] != "delete"){
437         $Objects[$name] = $obj; 
438       }
439     }
441     foreach($Objects as $name => $obj){
443       foreach($this->sub64coded as $codeIt){
444         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
445       }
447       $tmp = array();
448       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
449       foreach($attributes as $attrs){
451         if(empty($obj[$attrs])){
452           $obj[$attrs] = array();
453         }
454         if(!is_array($obj[$attrs])){
455           $tmp[$attrs] = stripslashes($obj[$attrs]);
456         }else{
457           $tmp[$attrs] = $obj[$attrs];
458         }
459       }    
461       $tmp['objectClass'] = $this->subClasses;
463       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
465       if($obj['status']=="new"){
466         $ldap->cat($sub_dn,array("objectClass"));
467         if($ldap->count()){
468           $obj['status']="edited";
469         }
470       }
472       if(empty($tmp['FAIpriority'])){
473         $tmp['FAIpriority']  ="0";
474       }
476       /* Check if gosaAdministrativeUnitTag is required as object class */
477       if($obj['status'] == "edited"){
478         $ldap->cat($sub_dn,array("objectClass"));
479         $attrs = $ldap->fetch();
480         if(isset($attrs['objectClass'])){
481           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
482             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
483           }
484         }
485       }
487       if($obj['status'] == "delete"){
488         prepare_to_save_FAI_object($sub_dn,array(),true);
489         $this->handle_post_events("remove");
490       }elseif($obj['status'] == "edited"){
491         prepare_to_save_FAI_object($sub_dn,$tmp);
492         $this->handle_post_events("modify");
493       }elseif($obj['status']=="new"){
494         prepare_to_save_FAI_object($sub_dn,$tmp);
495         $this->handle_post_events("add");
496       }
498       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
499     }
500   }
501   
503   /* Return plugin informations for acl handling */ 
504   function plInfo()
505   {
506     return (array( 
507           "plShortName" => _("Script"),
508           "plDescription" => _("FAI script"),
509           "plSelfModify"  => FALSE,
510           "plDepends"     => array(),
511           "plPriority"    => 18,
512           "plSection"     => array("administration"),
513           "plCategory"    => array("fai"),
514           "plProvidedAcls" => array(
515             "cn"                => _("Name")." ("._("Readonly").")",
516             "description"       => _("Description"))
517           ));
518   }
521 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
522 ?>