Code

Apply patch for #3716
[gosa.git] / trunk / 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       $res = FAI::get_leaf_objects($this->dn, $this->cn, $this->subClass, get_ou('faiScriptRDN'));
54       foreach($res as $obj){
55         $objects = array();
56         $objects['status']      = "FreshLoaded";
57         $objects['dn']          = $obj['dn'];
58         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
59         $this->SubObjects[$objects['cn']] = $objects;
60       }
61     }
63     $this->is_new = FALSE;
64     if($this->dn == "new"){
65       $this->is_new =TRUE;
66     }
67  
68     $this->ui = get_userinfo();
69   }
72   /* Reload some attributes */
73   function get_object_attributes($object,$attributes)
74   {
75     $ldap = $this->config->get_ldap_link();
76     $ldap->cd($this->config->current['BASE']);
77     $ldap->cat($object['dn'],$attributes);
78     $tmp  = $ldap->fetch();
80     foreach($attributes as $attrs){
81       if(isset($tmp[$attrs][0])){
82         $var = $tmp[$attrs][0];
84         /* Check if we must decode some attributes */
85         if(in_array_ics($attrs,$this->sub64coded)){
86           $var = base64_decode($var);
87         }
89         /*  check if this is a binary entry */
90         if(in_array_ics($attrs,$this->subBinary)){
91           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
92         }
94         /* Fix slashes */
95         $var = addslashes($var);
96         $object[$attrs] = $var;
97       }
98     }
99     return($object);
100   }
102   
103   /* Return a valid dn to fetch acls. Because 'new' will not work. */
104   function acl_base_for_current_object($dn)
105   {
106     if($dn == "new" || $dn == ""){
107       if($this->dn == "new"){
108         $dn= $this->parent->parent->acl_base;
109       }else{
110         $dn = $this->dn;
111       }
112     }
113     return($dn);
114   }
117   function execute()
118   {
119     /* Call parent execute */
120     plugin::execute();
122     if($this->is_account && !$this->view_logged){
123       $this->view_logged = TRUE;
124       new log("view","fai/".get_class($this),$this->dn);
125     }
127     /* Fill templating stuff */
128     $smarty= get_smarty();
129     $display= "";
131     /* Add new sub object */
132     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
133       $this->dialog= new $this->subClassName($this->config,"new");
134       $this->dialog->FAIstate = $this->FAIstate;
135       $this->dialog->set_acl_base($this->acl_base);
136       $this->dialog->set_acl_category("fai");
137       $this->dialog->parent = &$this;
138       $this->is_dialog=true;
139     }
141     if($this->dn != "new"){
142       set_object_info($this->dn);
143     }
145     /* File download requested */
146     if(isset($_GET['getFAIscript'])){
147       if(isset($this->SubObjects[base64_decode($_GET['getFAIscript'])])){
148         $obj = $this->SubObjects[base64_decode($_GET['getFAIscript'])];
149         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
150         send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript"); 
151       }
152     }
153     
154     /* Handle posts */
155     $s_action = $s_entry = "";
156     foreach($_POST as $name => $value){
158       /* Edit script posted */
159       if(preg_match("/^editscript_/",$name)){
160         $s_action = "edit";
161         $s_entry = preg_replace("/^editscript_/","",$name);
162         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
163         break;
164       }
166       /* Delete script requested */
167       if(preg_match("/^deletescript_/",$name) && !preg_match("/freeze/i",$this->FAIstate)){
168         $s_action = "remove";
169         $s_entry = preg_replace("/^deletescript_/","",$name);
170         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
171         break;
172       }
173     }
175     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
176       $s_entry = base64_decode($_GET['id']);
177       if(isset($this->SubObjects[$s_entry])){
178         $s_action = "edit";
179       }
180     }
182     if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
184       /* Get object, and load missing entry values */
185       $obj  = $this->SubObjects[$s_entry];
186       if($obj['status'] == "FreshLoaded"){
187         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
188       }
190       /* Create new dialog and set acl attributes  */
191       $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
192       $this->dialog->FAIstate = $this->FAIstate;
193       $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
194       $this->dialog->set_acl_category("fai");
196       /* Assign some additional dialog informations like headline and parent  */
197       set_object_info($obj['dn']);
198       $this->dialog->parent = &$this;
199       $this->is_dialog=true;
200     }
202     /* Check acls, are we allowed to delete an entry */
203     if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
204       $entry = $this->SubObjects[$s_entry];  
205       $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry")  ;
206       if(preg_match("/d/",$acl)){
207         $status = $entry['status'];
208         if($status == "edited" || $status == "FreshLoaded"){
209           $this->SubObjects[$s_entry]['status']= "delete";
210         }else{
211           unset($this->SubObjects[$s_entry]);
212         }
213       }
214     }
216       /* Save the edited entry */
217     if(isset($_POST['SaveSubObject'])){
219       /* Check if there are still errors remaining that must be fixed before saving */
220       $this->dialog->save_object();
221       $msgs = $this->dialog->check();
222       if(count($msgs)>0){
223         foreach($msgs as $msg){
224           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
225         }
226       }else{
228         /* Get return object */
229         $obj = $this->dialog->save();
231         /* If we have renamed the script entry, we must remove the old entry */
232         if(isset($obj['remove'])){
234           /* Get old entry values */
235           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
237           /* Depending on status, set new status */
238           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
239             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
240           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
241             unset($this->SubObjects[$obj['remove']['from']]);
242           }
244           /* Append the new entry */
245           $obj['status'] = "new";
246           $this->SubObjects[$obj['remove']['to']] = $obj;
247           unset($this->SubObjects[$obj['remove']['to']]['remove']);
248         }else{
249   
250           /* Set new status and append the entry */
251           if($obj['status'] == "FreshLoaded"){
252             $obj['status'] = "edited";
253           }
254           $this->SubObjects[$obj['cn']]=$obj;
255         }
256         $this->is_dialog=false;
257         unset($this->dialog);
258         $this->dialog=FALSE;
260       }
261     }
263     /* Sort entries */
264     $tmp = $keys = array();
266     if($this->sort_by == "name"){
267       foreach($this->SubObjects as $key => $entry){
268         $keys[$key]=$entry['cn'];
269       }
270     }elseif($this->sort_by == "priority"){
271       foreach($this->SubObjects as $key => $entry){
272         $keys[$key]=$entry['FAIpriority'];
273       }
274     }
276     natcasesort($keys);
278     if($this->sort_order == "down"){
279       $keys =array_reverse($keys);
280     }
282     foreach($keys as $key => $order_var){
283       $tmp[$key]=$this->SubObjects[$key];
284     }
285     $this->SubObjects = $tmp;
287     /* Cancel Dialog */
288     if(isset($_POST['CancelSubObject'])){
289       $this->is_dialog=false; 
290       unset($this->dialog);
291       $this->dialog=FALSE;
292     }
294     /* Print dialog if $this->dialog is set */
295     if(is_object($this->dialog)){
296       $this->dialog->save_object();
297       $display = $this->dialog->execute();
298       return($display);
299     }
301     /* Divlist            added 23.02.2006 
302        Containing FAIscripts 
303      */
304     $divlist = new divlist("FAIscripts");
305     $divlist->SetEntriesPerPage(0);
306     $plug = $_GET['plug'];
307    
308     if($this->sort_order == "up"){
309       $dir = "<img src='images/lists/sort-up.png' title='"._("Sort direction")."' alt='\/' border=0>";
310     }else{
311       $dir = "<img src='images/lists/sort-down.png' title='"._("Sort direction")."' alt='/\' border=0>";
312     }
313  
314     if($this->sort_by == "name"){
315       $sort_name = $dir;
316       $sort_prio = "";
317     }else{
318       $sort_name = "";
319       $sort_prio = $dir;
320     }
322     $divlist->SetHeader(array(  array("string"=>"<a href='?plug=".$plug."&amp;sort=name'>"._("Name").$sort_name."</a>"),
323                                 array("string"=>"<a href='?plug=".$plug."&amp;sort=priority'>".$sort_prio._("Priority")."</a>",
324                                       "attach"=>"style='width:100px;'"),
325                                 array("string"=>_("Download"),
326                                       "attach"=>"style='width:100px;'"),
327                                 array("string"=>_("Action"),
328                                       "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
329     $divlist->SetHeight(300);
330     $divlist->SetWidth("100%");
331     foreach($this->getList(true) as $key => $name){
333       $dn= $this->acl_base_for_current_object($name['dn']);
334       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
335       $act = "";
336       
337       /* Hide delete icon if this object is freezed */
338       if(preg_match("/freeze/", $this->FAIstate)){
339         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
340       }else{
341         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
342         if(preg_match("/d/",$acl)){
343           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
344         }
345       }
347       /* Check acls for download icon */
348       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
349       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
350         $down = "";
351       }else{
352         $down = "<a href='?plug=".$_GET['plug']."&getFAIscript=".base64_encode($key)."'>
353           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
354           </a>"; 
355       } 
357       /* Check if we are allowed to view this object */
358       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn")  ;
359       if(preg_match("/r/",$s_acl)){
360         $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".base64_encode($key)."'>".$name['name']."</a>"),
361               array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
362               array("string"=>$down , "attach" => "style='width:100px;'"),
363               array("string"=>str_replace("%s",base64_encode($key),$act),
364                 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
365       }
366     }
367     $smarty->assign("Entry_divlist",$divlist->DrawList());
369     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
370      * If we post the escaped strings they will be escaped again
371      */
372     foreach($this->attributes as $attrs){
373       if(get_magic_quotes_gpc()){
374         $smarty->assign($attrs,stripslashes($this->$attrs));
375       }else{
376         $smarty->assign($attrs,($this->$attrs));
377       }
378     }
380     $dn = $this->acl_base_for_current_object($this->dn);
381     $smarty->assign("sub_object_is_addable",  
382         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && 
383         !preg_match("/freeze/",$this->FAIstate));
385     $tmp = $this->plInfo();
386     foreach($tmp['plProvidedAcls'] as $name => $translated){
387       $smarty->assign($name."ACL",$this->getacl($name));
388     }
390     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
391     return($display);
392   }
395   /* Generate listbox friendly SubObject list
396    */
397   function getList($use_dns=false){
398     $a_return=array();
399     foreach($this->SubObjects as $obj){
400       if($obj['status'] != "delete"){
402         $cn   = stripslashes($obj['cn']);
403         $desc = "";
405         if((isset($obj['description']))&&(!empty($obj['description']))){
406           $desc = " [".stripslashes($obj['description'])."]";
407         }
409         if($use_dns){
410           $a_return[$obj['cn']]['name']= $cn.$desc;
411           $a_return[$obj['cn']]['dn']= $obj['dn'];
412           $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
413         }else{
414           $a_return[$obj['cn']] =  $cn.$desc;
415         }
416       }
417     }
418     return($a_return);
419   }
422   /* Delete me, and all my subtrees
423    */
424   function remove_from_parent()
425   {
426     if($this->acl_is_removeable()){
427       $ldap = $this->config->get_ldap_link();
428       $ldap->cd ($this->dn);
429       $release = $this->parent->parent->fai_release;
430       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
431       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
432       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
433  
434       foreach($this->SubObjects as $name => $obj){
435         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
436         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
437       }
438       $this->handle_post_events("remove");
439     }
440   }
443   /* Save data to object 
444    */
445   function save_object()
446   {
447     if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
448       plugin::save_object();
449     }
450     
451     /* Get sort order */
452     if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
453       if($this->sort_by == $_GET['sort']){
454         if($this->sort_order == "up"){
455           $this->sort_order = "down";
456         }elseif($this->sort_order == "down"){
457           $this->sort_order = "up";
458         }
459       }
460       $this->sort_by = $_GET['sort'];
461     }
462   }
465   /* Check supplied data */
466   function check()
467   {
468     /* Call common method to give check the hook */
469     $message= plugin::check();
471     /* Ensure that we do not overwrite an allready existing entry 
472      */
473     if($this->is_new){
474       $release = $this->parent->parent->fai_release;
475       $new_dn= 'cn='.$this->cn.",".get_ou('faiScriptRDN').get_ou('faiBaseRDN').$release;
476       $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
477       if(isset($res[$this->cn])){
478         $message[] = msgPool::duplicated(_("Name"));
479       }
480     }
482     return ($message);
483   }
486   /* Save to LDAP */
487   function save()
488   {
489     plugin::save();
491     $ldap = $this->config->get_ldap_link();
493     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
495     if($this->initially_was_account){
496       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
497     }else{
498       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
499     }
501     /* Prepare FAIscriptEntry to write it to ldap
502      * First sort array.
503      *  Because we must delete old entries first.
504      * After deletion, we perform add and modify 
505      */
506     $Objects = array();
508     /* We do not need to save untouched objects */
509     foreach($this->SubObjects as $name => $obj){
510       if($obj['status'] == "FreshLoaded"){
511         unset($this->SubObjects[$name]);
512       }
513     }
515     foreach($this->SubObjects as $name => $obj){
516       if($obj['status'] == "delete"){
517         $Objects[$name] = $obj; 
518       }
519     }
520     foreach($this->SubObjects as $name => $obj){
521       if($obj['status'] != "delete"){
522         $Objects[$name] = $obj; 
523       }
524     }
526     foreach($Objects as $name => $obj){
528       foreach($this->sub64coded as $codeIt){
529         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
530       }
532       $tmp = array();
533       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
534       foreach($attributes as $attrs){
535         if(!isset($obj[$attrs])) continue; 
536         if($obj[$attrs] == ""){
537           $obj[$attrs] = array();
538         }
539         if(!is_array($obj[$attrs])){
540           $tmp[$attrs] = stripslashes($obj[$attrs]);
541         }else{
542           $tmp[$attrs] = $obj[$attrs];
543         }
544       }    
546       $tmp['objectClass'] = $this->subClasses;
548       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
550       if($obj['status']=="new"){
551         $ldap->cat($sub_dn,array("objectClass"));
552         if($ldap->count()){
553           $obj['status']="edited";
554         }
555       }
557       if(empty($tmp['FAIpriority'])){
558         $tmp['FAIpriority']  ="0";
559       }
561       /* Tag object */
562       $ui= get_userinfo();
563       $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
565       if($obj['status'] == "delete"){
566         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
567         $this->handle_post_events("remove");
568       }elseif($obj['status'] == "edited"){
569         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
570         $this->handle_post_events("modify");
571       }elseif($obj['status']=="new"){
572         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
573         $this->handle_post_events("add");
574       }
575     }
576   }
579   function PrepareForCopyPaste($source)
580   {
581     plugin::PrepareForCopyPaste($source);
583     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
584      */
585     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
586     foreach($res as $obj){
588       /* Skip not relevant objects */
589       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
591       $objects = array();
592       $objects['status']      = "edited";
593       $objects['dn']          = $obj['dn'];
594       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
595       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
596       $this->SubObjects[$objects['cn']] = $objects;
597     }
598   }
601   /*! \brief  Used for copy & paste.
602               Returns a HTML input mask, which allows to change the cn of this entry.
603       @param  Array   Array containing current status && a HTML template.
604    */
605   function getCopyDialog()
606   {
607     $vars = array("cn");
608     $smarty = get_smarty();
609     $smarty->assign("cn", htmlentities($this->cn));
610     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
611     $ret = array();
612     $ret['string'] = $str;
613     $ret['status'] = "";
614     return($ret);
615   }
618   /*! \brief  Used for copy & paste.
619               Some entries must be renamed to avaoid duplicate entries.
620    */
621   function saveCopyDialog()
622   {
623     if(isset($_POST['cn'])){
624       $this->cn = get_post('cn');
625     }
626   }
627   
629   /* Return plugin informations for acl handling */ 
630   static function plInfo()
631   {
632     return (array( 
633           "plShortName" => _("Script"),
634           "plDescription" => _("FAI script"),
635           "plSelfModify"  => FALSE,
636           "plDepends"     => array(),
637           "plPriority"    => 18,
638           "plSection"     => array("administration"),
639           "plCategory"    => array("fai"),
640           "plProvidedAcls" => array(
641             "cn"                => _("Name")." ("._("Readonly").")",
642             "description"       => _("Description"))
643           ));
644   }
647 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
648 ?>