Code

Made faiScript w3c conform
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiScript.inc
1 <?php
3 class faiScript extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIscript");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAIscriptEntry";
16   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiScriptEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAIpriority"); 
23   var $sub_Load_Later   = array("FAIscript");
24   var $sub64coded       = array();
25   var $subBinary        = array("FAIscript");
27   /* Specific attributes */
28   var $cn               = "";       // The class name for this object
29   var $description      = "";       // The description for this set of partitions
30   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
31   var $SubObjects       = array();  // All leafobjects of this object
33   var $FAIstate         = "branch";
34   var $sort_by          = "name";
35   var $sort_order       = "up";
37   var $view_logged = FALSE;
38   var $ui;
40   function faiScript (&$config, $dn= NULL)
41   {
42     /* Load Attributes */
43     plugin::plugin ($config, $dn);
45     /* If "dn==new" we try to create a new entry
46      * Else we must read all objects from ldap which belong to this entry.
47      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
48      */
49     if($dn != "new"){
50       $this->dn =$dn;
52       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
53        */
54       $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
55       foreach($res as $obj){
57         /* Skip not relevant objects */
58         if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
60         $objects = array();
61         $objects['status']      = "FreshLoaded";
62         $objects['dn']          = $obj['dn'];
63         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
64         $this->SubObjects[$objects['cn']] = $objects;
65       }
66     }
68     $this->is_new = FALSE;
69     if($this->dn == "new"){
70       $this->is_new =TRUE;
71     }
72  
73     $this->ui = get_userinfo();
74   }
77   /* Reload some attributes */
78   function get_object_attributes($object,$attributes)
79   {
80     $ldap = $this->config->get_ldap_link();
81     $ldap->cd($this->config->current['BASE']);
82     $ldap->cat($object['dn'],$attributes);
83     $tmp  = $ldap->fetch();
85     foreach($attributes as $attrs){
86       if(isset($tmp[$attrs][0])){
87         $var = $tmp[$attrs][0];
89         /* Check if we must decode some attributes */
90         if(in_array_ics($attrs,$this->sub64coded)){
91           $var = postDecode($var);
92         }
94         /*  check if this is a binary entry */
95         if(in_array_ics($attrs,$this->subBinary)){
96           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
97         }
99         /* Fix slashes */
100         $var = addslashes($var);
101         $object[$attrs] = $var;
102       }
103     }
104     return($object);
105   }
107   
108   /* Return a valid dn to fetch acls. Because 'new' will not work. */
109   function acl_base_for_current_object($dn)
110   {
111     if($dn == "new" || $dn == ""){
112       if($this->dn == "new"){
113         $dn= $this->parent->parent->acl_base;
114       }else{
115         $dn = $this->dn;
116       }
117     }
118     return($dn);
119   }
122   function execute()
123   {
124     /* Call parent execute */
125     plugin::execute();
127     if($this->is_account && !$this->view_logged){
128       $this->view_logged = TRUE;
129       new log("view","fai/".get_class($this),$this->dn);
130     }
132     /* Fill templating stuff */
133     $smarty= get_smarty();
134     $display= "";
136     /* Add new sub object */
137     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
138       $this->dialog= new $this->subClassName($this->config,"new");
139       $this->dialog->FAIstate = $this->FAIstate;
140       $this->dialog->set_acl_base($this->acl_base);
141       $this->dialog->set_acl_category("fai");
142       $this->dialog->parent = &$this;
143       $this->is_dialog=true;
144     }
146     if($this->dn != "new"){
147       set_object_info($this->dn);
148     }
150     /* Handle posts */
151     $s_action = $s_entry = "";
152     foreach($_POST as $name => $value){
154       if(preg_match("/^download_/",$name)){
155         $s_entry = postDecode(preg_replace("/^download_/","",$name));
156         $obj = $this->SubObjects[$s_entry];
157         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
158         send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript"); 
159         break;
160       }
162       /* Edit script posted */
163       if(preg_match("/^editscript_/",$name)){
164         $s_action = "edit";
165         $s_entry = preg_replace("/^editscript_/","",$name);
166         $s_entry = postDecode(preg_replace("/_.*/","",$s_entry));
167         break;
168       }
170       /* Delete script requested */
171       if(preg_match("/^deletescript_/",$name) && !preg_match("/freeze/i",$this->FAIstate)){
172         $s_action = "remove";
173         $s_entry = preg_replace("/^deletescript_/","",$name);
174         $s_entry = postDecode(preg_replace("/_.*/","",$s_entry));
175         break;
176       }
177     }
179     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
180       $s_entry = postDecode($_GET['id']);
181       if(isset($this->SubObjects[$s_entry])){
182         $s_action = "edit";
183       }
184     }
186     if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
188       /* Get object, and load missing entry values */
189       $obj  = $this->SubObjects[$s_entry];
190       if($obj['status'] == "FreshLoaded"){
191         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
192       }
194       /* Create new dialog and set acl attributes  */
195       $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
196       $this->dialog->FAIstate = $this->FAIstate;
197       $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
198       $this->dialog->set_acl_category("fai");
200       /* Assign some additional dialog informations like headline and parent  */
201       set_object_info($obj['dn']);
202       $this->dialog->parent = &$this;
203       $this->is_dialog=true;
204     }
206     /* Check acls, are we allowed to delete an entry */
207     if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
208       $entry = $this->SubObjects[$s_entry];  
209       $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry")  ;
210       if(preg_match("/d/",$acl)){
211         $status = $entry['status'];
212         if($status == "edited" || $status == "FreshLoaded"){
213           $this->SubObjects[$s_entry]['status']= "delete";
214         }else{
215           unset($this->SubObjects[$s_entry]);
216         }
217       }
218     }
220       /* Save the edited entry */
221     if(isset($_POST['SaveSubObject'])){
223       /* Check if there are still errors remaining that must be fixed before saving */
224       $this->dialog->save_object();
225       $msgs = $this->dialog->check();
226       if(count($msgs)>0){
227         foreach($msgs as $msg){
228           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
229         }
230       }else{
232         /* Get return object */
233         $obj = $this->dialog->save();
235         /* If we have renamed the script entry, we must remove the old entry */
236         if(isset($obj['remove'])){
238           /* Get old entry values */
239           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
241           /* Depending on status, set new status */
242           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
243             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
244           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
245             unset($this->SubObjects[$obj['remove']['from']]);
246           }
248           /* Append the new entry */
249           $obj['status'] = "new";
250           $this->SubObjects[$obj['remove']['to']] = $obj;
251           unset($this->SubObjects[$obj['remove']['to']]['remove']);
252         }else{
253   
254           /* Set new status and append the entry */
255           if($obj['status'] == "FreshLoaded"){
256             $obj['status'] = "edited";
257           }
258           $this->SubObjects[$obj['cn']]=$obj;
259         }
260         $this->is_dialog=false;
261         unset($this->dialog);
262         $this->dialog=FALSE;
264       }
265     }
267     /* Sort entries */
268     $tmp = $keys = array();
270     if($this->sort_by == "name"){
271       foreach($this->SubObjects as $key => $entry){
272         $keys[$key]=$entry['cn'];
273       }
274     }elseif($this->sort_by == "priority"){
275       foreach($this->SubObjects as $key => $entry){
276         $keys[$key]=$entry['FAIpriority'];
277       }
278     }
280     natcasesort($keys);
282     if($this->sort_order == "down"){
283       $keys =array_reverse($keys);
284     }
286     foreach($keys as $key => $order_var){
287       $tmp[$key]=$this->SubObjects[$key];
288     }
289     $this->SubObjects = $tmp;
291     /* Cancel Dialog */
292     if(isset($_POST['CancelSubObject'])){
293       $this->is_dialog=false; 
294       unset($this->dialog);
295       $this->dialog=FALSE;
296     }
298     /* Print dialog if $this->dialog is set */
299     if(is_object($this->dialog)){
300       $this->dialog->save_object();
301       $display = $this->dialog->execute();
302       return($display);
303     }
305     /* Divlist            added 23.02.2006 
306        Containing FAIscripts 
307      */
308     $divlist = new divlist("FAIscripts");
309     $divlist->SetEntriesPerPage(0);
310     $plug = $_GET['plug'];
311    
312     if($this->sort_order == "up"){
313       $dir = image('images/lists/sort-up.png');
314     }else{
315       $dir = image('images/lists/sort-down.png');
316     }
317  
318     if($this->sort_by == "name"){
319       $sort_name = $dir;
320       $sort_prio = "";
321     }else{
322       $sort_name = "";
323       $sort_prio = $dir;
324     }
326     $divlist->SetHeader(array(  array("string"=>"<a href='?plug=".$plug."&amp;sort=name'>"._("Name")."</a>".$sort_name),
327                                 array("string"=>"<a href='?plug=".$plug."&amp;sort=priority'>".$sort_prio._("Priority")."</a>",
328                                       "attach"=>"style='width:100px;'"),
329                                 array("string"=>_("Download"),
330                                       "attach"=>"style='width:100px;'"),
331                                 array("string"=>_("Action"),
332                                       "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
333     $divlist->SetHeight(300);
334     $divlist->SetWidth("100%");
335     foreach($this->getList(true) as $key => $name){
337       $dn= $this->acl_base_for_current_object($name['dn']);
338       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
339       $act = "";
340       
341       /* Hide delete icon if this object is freezed */
342       if(preg_match("/freeze/", $this->FAIstate)){
343         $act .= image('images/lists/edit.png', 'editscript_%s',msgPool::editButton());
344       }else{
345         $act .= image('images/lists/edit.png', 'editscript_%s',msgPool::editButton());
346         if(preg_match("/d/",$acl)){
347           $act .= image('images/lists/trash.png', 'deletescript_%s',msgPool::delButton());
348         }
349       }
351       /* Check acls for download icon */
352       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
353       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
354         $down = "";
355       }else{
356         $down = image('images/save.png', 'download_'.postEncode($key));
357       } 
359       /* Check if we are allowed to view this object */
360       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn")  ;
361       if(preg_match("/r/",$s_acl)){
362         $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".postEncode($key)."'>".$name['name']."</a>"),
363               array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
364               array("string"=>$down , "attach" => "style='width:100px;'"),
365               array("string"=>str_replace("%s",postEncode($key),$act),
366                 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
367       }
368     }
369     $smarty->assign("Entry_divlist",$divlist->DrawList());
371     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
372      * If we post the escaped strings they will be escaped again
373      */
374     foreach($this->attributes as $attrs){
375       if(get_magic_quotes_gpc()){
376         $smarty->assign($attrs,stripslashes($this->$attrs));
377       }else{
378         $smarty->assign($attrs,($this->$attrs));
379       }
380     }
382     $dn = $this->acl_base_for_current_object($this->dn);
383     $smarty->assign("sub_object_is_addable",  
384         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && 
385         !preg_match("/freeze/",$this->FAIstate));
387     $tmp = $this->plInfo();
388     foreach($tmp['plProvidedAcls'] as $name => $translated){
389       $smarty->assign($name."ACL",$this->getacl($name));
390     }
392     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
393     return($display);
394   }
397   /* Generate listbox friendly SubObject list
398    */
399   function getList($use_dns=false){
400     $a_return=array();
401     foreach($this->SubObjects as $obj){
402       if($obj['status'] != "delete"){
404         $cn   = stripslashes($obj['cn']);
405         $desc = "";
407         if((isset($obj['description']))&&(!empty($obj['description']))){
408           $desc = " [".stripslashes($obj['description'])."]";
409         }
411         if($use_dns){
412           $a_return[$obj['cn']]['name']= $cn.$desc;
413           $a_return[$obj['cn']]['dn']= $obj['dn'];
414           $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
415         }else{
416           $a_return[$obj['cn']] =  $cn.$desc;
417         }
418       }
419     }
420     return($a_return);
421   }
424   /* Delete me, and all my subtrees
425    */
426   function remove_from_parent()
427   {
428     if($this->acl_is_removeable()){
429       $ldap = $this->config->get_ldap_link();
430       $ldap->cd ($this->dn);
431       $release = $this->parent->parent->fai_release;
432       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
433       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
434       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
435  
436       foreach($this->SubObjects as $name => $obj){
437         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
438         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
439       }
440       $this->handle_post_events("remove");
441     }
442   }
445   /* Save data to object 
446    */
447   function save_object()
448   {
449     if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
450       plugin::save_object();
451     }
452     
453     /* Get sort order */
454     if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
455       if($this->sort_by == $_GET['sort']){
456         if($this->sort_order == "up"){
457           $this->sort_order = "down";
458         }elseif($this->sort_order == "down"){
459           $this->sort_order = "up";
460         }
461       }
462       $this->sort_by = $_GET['sort'];
463     }
464   }
467   /* Check supplied data */
468   function check()
469   {
470     /* Call common method to give check the hook */
471     $message= plugin::check();
473     /* Ensure that we do not overwrite an allready existing entry 
474      */
475     if($this->is_new){
476       $release = $this->parent->parent->fai_release;
477       $new_dn= 'cn='.$this->cn.",".get_ou('faiScriptRDN').get_ou('faiBaseRDN').$release;
478       $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
479       if(isset($res[$this->cn])){
480         $message[] = msgPool::duplicated(_("Name"));
481       }
482     }
484     return ($message);
485   }
488   /* Save to LDAP */
489   function save()
490   {
491     plugin::save();
493     $ldap = $this->config->get_ldap_link();
495     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
497     if($this->initially_was_account){
498       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
499     }else{
500       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
501     }
503     /* Prepare FAIscriptEntry to write it to ldap
504      * First sort array.
505      *  Because we must delete old entries first.
506      * After deletion, we perform add and modify 
507      */
508     $Objects = array();
510     /* We do not need to save untouched objects */
511     foreach($this->SubObjects as $name => $obj){
512       if($obj['status'] == "FreshLoaded"){
513         unset($this->SubObjects[$name]);
514       }
515     }
517     foreach($this->SubObjects as $name => $obj){
518       if($obj['status'] == "delete"){
519         $Objects[$name] = $obj; 
520       }
521     }
522     foreach($this->SubObjects as $name => $obj){
523       if($obj['status'] != "delete"){
524         $Objects[$name] = $obj; 
525       }
526     }
528     foreach($Objects as $name => $obj){
530       foreach($this->sub64coded as $codeIt){
531         $obj[$codeIt]=postEncode(stripslashes($obj[$codeIt]));
532       }
534       $tmp = array();
535       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
536       foreach($attributes as $attrs){
537         if(!isset($obj[$attrs])) continue; 
538         if($obj[$attrs] == ""){
539           $obj[$attrs] = array();
540         }
541         if(!is_array($obj[$attrs])){
542           $tmp[$attrs] = stripslashes($obj[$attrs]);
543         }else{
544           $tmp[$attrs] = $obj[$attrs];
545         }
546       }    
548       $tmp['objectClass'] = $this->subClasses;
550       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
552       if($obj['status']=="new"){
553         $ldap->cat($sub_dn,array("objectClass"));
554         if($ldap->count()){
555           $obj['status']="edited";
556         }
557       }
559       if(empty($tmp['FAIpriority'])){
560         $tmp['FAIpriority']  ="0";
561       }
563       /* Tag object */
564       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
566       if($obj['status'] == "delete"){
567         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
568         $this->handle_post_events("remove");
569       }elseif($obj['status'] == "edited"){
570         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
571         $this->handle_post_events("modify");
572       }elseif($obj['status']=="new"){
573         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
574         $this->handle_post_events("add");
575       }
576     }
577   }
580   function PrepareForCopyPaste($source)
581   {
582     plugin::PrepareForCopyPaste($source);
584     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
585      */
586     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
587     foreach($res as $obj){
589       /* Skip not relevant objects */
590       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
592       $objects = array();
593       $objects['status']      = "edited";
594       $objects['dn']          = $obj['dn'];
595       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
596       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
597       $this->SubObjects[$objects['cn']] = $objects;
598     }
599   }
602   /*! \brief  Used for copy & paste.
603               Returns a HTML input mask, which allows to change the cn of this entry.
604       @param  Array   Array containing current status && a HTML template.
605    */
606   function getCopyDialog()
607   {
608     $vars = array("cn");
609     $smarty = get_smarty();
610     $smarty->assign("cn", htmlentities($this->cn));
611     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
612     $ret = array();
613     $ret['string'] = $str;
614     $ret['status'] = "";
615     return($ret);
616   }
619   /*! \brief  Used for copy & paste.
620               Some entries must be renamed to avaoid duplicate entries.
621    */
622   function saveCopyDialog()
623   {
624     if(isset($_POST['cn'])){
625       $this->cn = get_post('cn');
626     }
627   }
628   
630   /* Return plugin informations for acl handling */ 
631   static function plInfo()
632   {
633     return (array( 
634           "plShortName" => _("Script"),
635           "plDescription" => _("FAI script"),
636           "plSelfModify"  => FALSE,
637           "plDepends"     => array(),
638           "plPriority"    => 18,
639           "plSection"     => array("administration"),
640           "plCategory"    => array("fai"),
641           "plProvidedAcls" => array(
642             "cn"                => _("Name")." ("._("Readonly").")",
643             "description"       => _("Description"))
644           ));
645   }
648 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
649 ?>