Code

8678a5903c89efcab632391d0139fbacac3cbd78
[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         = "";
40   var $sort_by          = "name";
41   var $sort_order       = "up";
43   var $view_logged = FALSE;
44   var $ui;
46   function faiScript ($config, $dn= NULL)
47   {
48     /* Load Attributes */
49     plugin::plugin ($config, $dn);
51     /* If "dn==new" we try to create a new entry
52      * Else we must read all objects from ldap which belong to this entry.
53      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
54      */
55     if($dn != "new"){
57       $this->dn =$dn;
59       /* Get FAIstate
60        */
61       if(isset($this->attrs['FAIstate'][0])){
62         $this->FAIstate = $this->attrs['FAIstate'][0];
63       }
65       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
66        */
67       $ldap     = $this->config->get_ldap_link();
68       $ldap->cd ($this->dn);
69       
70       $attrs_to_search = $this->subAttributes;
71       $attrs_to_search[] = "FAIstate";
72       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
74       while($object = $ldap->fetch()){
76         /* Skip objects, that are tagged as removed */
77         if(isset($object['FAIstate'][0])){
78           if(preg_match("/removed$/",$object['FAIstate'][0])){
79             continue;
80           }
81         }
83         /* Set status for save management */
84         $objects = array();
85         $objects['status']      = "FreshLoaded";
86         $objects['dn']          = $object['dn'];
87         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
88         $this->SubObjects[$objects['cn']] = $objects;
89       }
90     
91     }
92     $this->ui = get_userinfo();
93   }
96   /* Reload some attributes */
97   function get_object_attributes($object,$attributes)
98   {
99     $ldap = $this->config->get_ldap_link();
100     $ldap->cd($this->config->current['BASE']);
101     $ldap->cat($object['dn'],$attributes);
102     $tmp  = $ldap->fetch();
104     foreach($attributes as $attrs){
105       if(isset($tmp[$attrs][0])){
106         $var = $tmp[$attrs][0];
108         /* Check if we must decode some attributes */
109         if(in_array_ics($attrs,$this->sub64coded)){
110           $var = base64_decode($var);
111         }
113         /*  check if this is a binary entry */
114         if(in_array_ics($attrs,$this->subBinary)){
115           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
116         }
118         /* Fix slashes */
119         $var = addslashes($var);
120         $object[$attrs] = $var;
121       }
122     }
123     return($object);
124   }
126   
127   /* Return a valid dn to fetch acls. Because 'new' will not work. */
128   function acl_base_for_current_object($dn)
129   {
130     if($dn == "new"){
131       if($this->dn == "new"){
132         $dn= "cn=dummy,".$_SESSION['CurrentMainBase'];
133       }else{
134         $dn = $this->dn;
135       }
136     }
137     return($dn);
138   }
141   function execute()
142   {
143     /* Call parent execute */
144     plugin::execute();
146     if($this->is_account && !$this->view_logged){
147       $this->view_logged = TRUE;
148       new log("view","fai/".get_class($this),$this->dn);
149     }
151     /* Fill templating stuff */
152     $smarty= get_smarty();
153     $display= "";
155     /* Add new sub object */
156     if(isset($_POST['AddSubObject'])){
157       $this->dialog= new $this->subClassName($this->config,"new");
158       $this->dialog->set_acl_base($this->acl_base);
159       $this->dialog->set_acl_category("fai");
160       $this->dialog->parent = &$this;
161       $this->is_dialog=true;
162     }
164     if($this->dn != "new"){
165       $_SESSION['objectinfo']= $this->dn;
166     }
168     /* Handle posts */
169     $s_action = $s_entry = "";
170     foreach($_POST as $name => $value){
172       /* Edit script posted */
173       if(preg_match("/^editscript_/",$name)){
174         $s_action = "edit";
175         $s_entry = preg_replace("/^editscript_/","",$name);
176         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
177         break;
178       }
180       /* Delete script requested */
181       if(preg_match("/^deletescript_/",$name)){
182         $s_action = "remove";
183         $s_entry = preg_replace("/^deletescript_/","",$name);
184         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
185         break;
186       }
187     }
189     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
190       $s_entry = $_GET['id'];
191       if(isset($this->SubObjects[$s_entry])){
192         $s_action = "edit";
193       }
194     }
196     if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
198       /* Get object, and load missing entry values */
199       $obj  = $this->SubObjects[$s_entry];
200       if($obj['status'] == "FreshLoaded"){
201         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
202       }
204       /* Create new dialog and set acl attributes  */
205       $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
206       $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
207       $this->dialog->set_acl_category("fai");
209       /* Assign some additional dialog informations like headline and parent  */
210       $_SESSION['objectinfo'] = $obj['dn'];
211       $this->dialog->parent = &$this;
212       $this->is_dialog=true;
213     }
215     /* Check acls, are we allowed to delete an entry */
216     if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
217       $entry = $this->SubObjects[$s_entry];  
218       $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry")  ;
219       if(preg_match("/d/",$acl)){
220         $status = $entry['status'];
221         if($status == "edited" || $status == "FreshLoaded"){
222           $this->SubObjects[$s_entry]['status']= "delete";
223         }else{
224           unset($this->SubObjects[$s_entry]);
225         }
226       }
227     }
229       /* Save the edited entry */
230     if(isset($_POST['SaveSubObject'])){
232       /* Check if there are still errors remaining that must be fixed before saving */
233       $this->dialog->save_object();
234       $msgs = $this->dialog->check();
235       if(count($msgs)>0){
236         foreach($msgs as $msg){
237           print_red($msg);
238         }
239       }else{
241         /* Get return object */
242         $obj = $this->dialog->save();
244         /* If we have renamed the script entry, we must remove the old entry */
245         if(isset($obj['remove'])){
247           /* Get old entry values */
248           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
250           /* Depending on status, set new status */
251           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
252             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
253           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
254             unset($this->SubObjects[$obj['remove']['from']]);
255           }
257           /* Append the new entry */
258           $obj['status'] = "new";
259           $this->SubObjects[$obj['remove']['to']] = $obj;
260           unset($this->SubObjects[$obj['remove']['to']]['remove']);
261         }else{
262   
263           /* Set new status and append the entry */
264           if($obj['status'] == "FreshLoaded"){
265             $obj['status'] = "edited";
266           }
267           $this->SubObjects[$obj['cn']]=$obj;
268         }
269         $this->is_dialog=false;
270         unset($this->dialog);
271         $this->dialog=NULL;
273       }
274     }
276     /* Sort entries */
277     $tmp = $keys = array();
279     if($this->sort_by == "name"){
280       foreach($this->SubObjects as $key => $entry){
281         $keys[$key]=$entry['cn'];
282       }
283     }elseif($this->sort_by == "priority"){
284       foreach($this->SubObjects as $key => $entry){
285         $keys[$key]=$entry['FAIpriority'];
286       }
287     }
289     natcasesort($keys);
291     if($this->sort_order == "down"){
292       $keys =array_reverse($keys);
293     }
295     foreach($keys as $key => $order_var){
296       $tmp[$key]=$this->SubObjects[$key];
297     }
298     $this->SubObjects = $tmp;
300     /* Cancel Dialog */
301     if(isset($_POST['CancelSubObject'])){
302       $this->is_dialog=false; 
303       unset($this->dialog);
304       $this->dialog=NULL;
305     }
307     /* Print dialog if $this->dialog is set */
308     if($this->dialog){
309       $this->dialog->save_object();
310       $display = $this->dialog->execute();
311       return($display);
312     }
314     /* Divlist            added 23.02.2006 
315        Containing FAIscripts 
316      */
317     $divlist = new divlist("FAIscripts");
318     $plug = $_GET['plug'];
319    
320     if($this->sort_order == "up"){
321       $dir = "<img src='images/sort_up.png' title='"._("Sort direction")."' alt='\/' border=0>";
322     }else{
323       $dir = "<img src='images/sort_down.png' title='"._("Sort direction")."' alt='/\' border=0>";
324     }
325  
326     if($this->sort_by == "name"){
327       $sort_name = $dir;
328       $sort_prio = "";
329     }else{
330       $sort_name = "";
331       $sort_prio = $dir;
332     }
334     $divlist->SetHeader(array(  array("string"=>"<a href='?plug=".$plug."&amp;sort=name'>"._("Name").$sort_name."</a>"),
335                                 array("string"=>"<a href='?plug=".$plug."&amp;sort=priority'>".$sort_prio._("Priority")."</a>"),
336                                 array("string"=>_("Download")),
337                                 array("string"=>_("Action"))));
338     $divlist->SetHeight(300);
339     $divlist->SetWidth("100%");
340     foreach($this->getList(true) as $key => $name){
342       $dn= $this->acl_base_for_current_object($name['dn']);
343       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
344       $act = "";
345       
346       /* Hide delete icon if this object is freezed */
347       if($this->FAIstate == "freeze"){
348         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
349       }else{
350         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
351         if(preg_match("/d/",$acl)){
352           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
353         }
354       }
356       /* Check acls for download icon */
357       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
358       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
359         $down = "";
360       }else{
361         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
362           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
363           </a>"; 
364       } 
366       /* Check if we are allowed to view this object */
367       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn")  ;
368       if(preg_match("/r/",$s_acl)){
369         $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name['name']."</a>"),
370               array("string"=>$name['FAIpriority'] , "attach" => "style='width:20px;'"),
371               array("string"=>$down , "attach" => "style='width:20px;'"),
372               array("string"=>str_replace("%s",base64_encode($key),$act),
373                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
374       }
375     }
376     $smarty->assign("Entry_divlist",$divlist->DrawList());
379     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
380      * If we post the escaped strings they will be escaped again
381      */
382     foreach($this->attributes as $attrs){
383       if(get_magic_quotes_gpc()){
384         $smarty->assign($attrs,stripslashes($this->$attrs));
385       }else{
386         $smarty->assign($attrs,($this->$attrs));
387       }
388     }
390     $dn = $this->acl_base_for_current_object($this->dn);
391     $smarty->assign("sub_object_is_addable",  
392         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && 
393         !preg_match("/freeze/",$this->FAIstate));
395     $tmp = $this->plInfo();
396     foreach($tmp['plProvidedAcls'] as $name => $translated){
397       $smarty->assign($name."ACL",$this->getacl($name));
398     }
400     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
401     return($display);
402   }
404   /* Generate listbox friendly SubObject list
405    */
406   function getList($use_dns=false){
407     $a_return=array();
408     foreach($this->SubObjects as $obj){
409       if($obj['status'] != "delete"){
410         if($use_dns){
411           if((isset($obj['description']))&&(!empty($obj['description']))){
412             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
413           }else{
414             $a_return[$obj['cn']]['name']= $obj['cn'];
415           }
416           $a_return[$obj['cn']]['dn']= $obj['dn'];
417           $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
418         }else{
419           if((isset($obj['description']))&&(!empty($obj['description']))){
420             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
421           }else{
422             $a_return[$obj['cn']]= $obj['cn'];
423           }
424         }
425       }
426     }
427     return($a_return);
428   }
430   /* Delete me, and all my subtrees
431    */
432   function remove_from_parent()
433   {
434     if($this->acl_is_removeable()){
435       $ldap = $this->config->get_ldap_link();
436       $ldap->cd ($this->dn);
438       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
439       if($_SESSION['faifilter']['branch'] == "main"){
440         $use_dn = $this->dn;
441       }
442    
443       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
444  
445       prepare_to_save_FAI_object($use_dn,array(),true);
446       
447       foreach($this->SubObjects as $name => $obj){
448         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
449         if($_SESSION['faifilter']['branch'] == "main"){
450           $use_dn = $obj['dn'];
451         }
452         prepare_to_save_FAI_object($use_dn,array(),true);
453       }
454       $this->handle_post_events("remove");
455     }
456   }
459   /* Save data to object 
460    */
461   function save_object()
462   {
463     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
464       plugin::save_object();
465       foreach($this->attributes as $attrs){
466         if(isset($_POST[$attrs])){
467           $this->$attrs = $_POST[$attrs];
468         }
469       }
470     }
471     
472     /* Get sort order */
473     if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
474       if($this->sort_by == $_GET['sort']){
475         if($this->sort_order == "up"){
476           $this->sort_order = "down";
477         }elseif($this->sort_order == "down"){
478           $this->sort_order = "up";
479         }
480       }
481       $this->sort_by = $_GET['sort'];
482     }
483   }
486   /* Check supplied data */
487   function check()
488   {
489     /* Call common method to give check the hook */
490     $message= plugin::check();
492     return ($message);
493   }
496   /* Save to LDAP */
497   function save()
498   {
499     plugin::save();
501     $ldap = $this->config->get_ldap_link();
503     prepare_to_save_FAI_object($this->dn,$this->attrs);
504     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
506     if($this->initially_was_account){
507       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
508     }else{
509       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
510     }
512     /* Do object tagging */
513     $this->handle_object_tagging();
515     /* Prepare FAIscriptEntry to write it to ldap
516      * First sort array.
517      *  Because we must delete old entries first.
518      * After deletion, we perform add and modify 
519      */
520     $Objects = array();
522     /* We do not need to save untouched objects */
523     foreach($this->SubObjects as $name => $obj){
524       if($obj['status'] == "FreshLoaded"){
525         unset($this->SubObjects[$name]);
526       }
527     }
529     foreach($this->SubObjects as $name => $obj){
530       if($obj['status'] == "delete"){
531         $Objects[$name] = $obj; 
532       }
533     }
534     foreach($this->SubObjects as $name => $obj){
535       if($obj['status'] != "delete"){
536         $Objects[$name] = $obj; 
537       }
538     }
540     foreach($Objects as $name => $obj){
542       foreach($this->sub64coded as $codeIt){
543         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
544       }
546       $tmp = array();
547       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
548       foreach($attributes as $attrs){
550         if(empty($obj[$attrs])){
551           $obj[$attrs] = array();
552         }
553         if(!is_array($obj[$attrs])){
554           $tmp[$attrs] = stripslashes($obj[$attrs]);
555         }else{
556           $tmp[$attrs] = $obj[$attrs];
557         }
558       }    
560       $tmp['objectClass'] = $this->subClasses;
562       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
564       if($obj['status']=="new"){
565         $ldap->cat($sub_dn,array("objectClass"));
566         if($ldap->count()){
567           $obj['status']="edited";
568         }
569       }
571       if(empty($tmp['FAIpriority'])){
572         $tmp['FAIpriority']  ="0";
573       }
575       /* Check if gosaAdministrativeUnitTag is required as object class */
576       if($obj['status'] == "edited"){
577         $ldap->cat($sub_dn,array("objectClass"));
578         $attrs = $ldap->fetch();
579         if(isset($attrs['objectClass'])){
580           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
581             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
582           }
583         }
584       }
586       if($obj['status'] == "delete"){
587         prepare_to_save_FAI_object($sub_dn,array(),true);
588         $this->handle_post_events("remove");
589       }elseif($obj['status'] == "edited"){
590         prepare_to_save_FAI_object($sub_dn,$tmp);
591         $this->handle_post_events("modify");
592       }elseif($obj['status']=="new"){
593         prepare_to_save_FAI_object($sub_dn,$tmp);
594         $this->handle_post_events("add");
595       }
597       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
598     }
599   }
600   
602   /* Return plugin informations for acl handling */ 
603   function plInfo()
604   {
605     return (array( 
606           "plShortName" => _("Script"),
607           "plDescription" => _("FAI script"),
608           "plSelfModify"  => FALSE,
609           "plDepends"     => array(),
610           "plPriority"    => 18,
611           "plSection"     => array("administration"),
612           "plCategory"    => array("fai"),
613           "plProvidedAcls" => array(
614             "cn"                => _("Name")." ("._("Readonly").")",
615             "description"       => _("Description"))
616           ));
617   }
620 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
621 ?>