Code

eb313dd9680a395cddc490d65df6bf7d606d45db
[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();
75     $this->scriptListWidget= new sortableListing($this->SubObjects, $this->convertList());
76     $this->scriptListWidget->setDeleteable(true);
77     $this->scriptListWidget->setInstantDelete(false);
78     $this->scriptListWidget->setEditable(true);
79     $this->scriptListWidget->setWidth("100%");
80     $this->scriptListWidget->setHeight("70px");
81     $this->scriptListWidget->setHeader(array(_("Name"),_("Description")));
82   }
84   function convertList()
85   {
86     $data = array();
87     foreach($this->SubObjects as $cn => $entry){
88       if($entry['status'] == "delete") continue;
89       $data[$cn] = array('data' => array($entry['cn'], $entry['description']));
90     }
91     return($data);
92   }
95   /* Reload some attributes */
96   function get_object_attributes($object,$attributes)
97   {
98     $ldap = $this->config->get_ldap_link();
99     $ldap->cd($this->config->current['BASE']);
100     $ldap->cat($object['dn'],$attributes);
101     $tmp  = $ldap->fetch();
103     foreach($attributes as $attrs){
104       if(isset($tmp[$attrs][0])){
105         $var = $tmp[$attrs][0];
107         /* Check if we must decode some attributes */
108         if(in_array_ics($attrs,$this->sub64coded)){
109           $var = postDecode($var);
110         }
112         /*  check if this is a binary entry */
113         if(in_array_ics($attrs,$this->subBinary)){
114           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
115         }
117         /* Fix slashes */
118         $var = addslashes($var);
119         $object[$attrs] = $var;
120       }
121     }
122     return($object);
123   }
125   
126   /* Return a valid dn to fetch acls. Because 'new' will not work. */
127   function acl_base_for_current_object($dn)
128   {
129     if($dn == "new" || $dn == ""){
130       if($this->dn == "new"){
131         $dn= $this->parent->parent->acl_base;
132       }else{
133         $dn = $this->dn;
134       }
135     }
136     return($dn);
137   }
140   function execute()
141   {
142     /* Call parent execute */
143     plugin::execute();
145     if($this->is_account && !$this->view_logged){
146       $this->view_logged = TRUE;
147       new log("view","fai/".get_class($this),$this->dn);
148     }
150     /* Fill templating stuff */
151     $smarty= get_smarty();
152     $display= "";
154     /* Add new sub object */
155     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
156       $this->dialog= new $this->subClassName($this->config,"new");
157       $this->dialog->FAIstate = $this->FAIstate;
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       set_object_info($this->dn);
166     }
168     /* Handle posts */
169     $s_action = $s_entry = "";
170     foreach($_POST as $name => $value){
171       if(preg_match("/^download_/",$name)){
172         $s_entry = postDecode(preg_replace("/^download_/","",$name));
173         $obj = $this->SubObjects[$s_entry];
174         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
175         send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript"); 
176         break;
177       }
178     }
180     $this->scriptListWidget->setAcl($this->getacl(""));
181     $this->scriptListWidget->save_object();
182     $action = $this->scriptListWidget->getAction();
183     if($action['action'] =="edit"){
184       $s_entry = $this->scriptListWidget->getKey($action['targets'][0]);
185       if(isset($this->SubObjects[$s_entry])){ 
187         $obj  = $this->SubObjects[$s_entry];
188         if($obj['status'] == "FreshLoaded"){
189           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
190         }
192         /* Create new dialog and set acl attributes  */
193         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
194         $this->dialog->FAIstate = $this->FAIstate;
195         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
196         $this->dialog->set_acl_category("fai");
198         /* Assign some additional dialog informations like headline and parent  */
199         set_object_info($obj['dn']);
200         $this->dialog->parent = &$this;
201         $this->is_dialog=true;
202       }
203     }
205     /* Check acls, are we allowed to delete an entry */
206     if($action['action'] =="delete"){
207       $s_entry = $this->scriptListWidget->getKey($action['targets'][0]);
208       if(isset($this->SubObjects[$s_entry])){ 
209         $entry = $this->SubObjects[$s_entry];  
210         $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry")  ;
211         if(preg_match("/d/",$acl)){
212           $status = $entry['status'];
213           if($status == "edited" || $status == "FreshLoaded"){
214             $this->SubObjects[$s_entry]['status']= "delete";
215           }else{
216             unset($this->SubObjects[$s_entry]);
217           }
218         }
219       }
220     }
222       /* Save the edited entry */
223     if(isset($_POST['SaveSubObject'])){
225       /* Check if there are still errors remaining that must be fixed before saving */
226       $this->dialog->save_object();
227       $msgs = $this->dialog->check();
228       if(count($msgs)>0){
229         foreach($msgs as $msg){
230           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
231         }
232       }else{
234         /* Get return object */
235         $obj = $this->dialog->save();
237         /* If we have renamed the script entry, we must remove the old entry */
238         if(isset($obj['remove'])){
240           /* Get old entry values */
241           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
243           /* Depending on status, set new status */
244           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
245             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
246           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
247             unset($this->SubObjects[$obj['remove']['from']]);
248           }
250           /* Append the new entry */
251           $obj['status'] = "new";
252           $this->SubObjects[$obj['remove']['to']] = $obj;
253           unset($this->SubObjects[$obj['remove']['to']]['remove']);
254         }else{
255   
256           /* Set new status and append the entry */
257           if($obj['status'] == "FreshLoaded"){
258             $obj['status'] = "edited";
259           }
260           $this->SubObjects[$obj['cn']]=$obj;
261         }
262         $this->is_dialog=false;
263         unset($this->dialog);
264         $this->dialog=FALSE;
266       }
267     }
269     /* Cancel Dialog */
270     if(isset($_POST['CancelSubObject'])){
271       $this->is_dialog=false; 
272       unset($this->dialog);
273       $this->dialog=FALSE;
274     }
276     /* Print dialog if $this->dialog is set */
277     if(is_object($this->dialog)){
278       $this->dialog->save_object();
279       $display = $this->dialog->execute();
280       return($display);
281     }
283     $this->scriptListWidget->setListData($this->SubObjects, $this->convertList());
284     $this->scriptListWidget->update();
285     $smarty->assign("Entry_divlist",$this->scriptListWidget->render());
287     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
288      * If we post the escaped strings they will be escaped again
289      */
290     foreach($this->attributes as $attrs){
291       if(get_magic_quotes_gpc()){
292         $smarty->assign($attrs,stripslashes($this->$attrs));
293       }else{
294         $smarty->assign($attrs,($this->$attrs));
295       }
296     }
298     $dn = $this->acl_base_for_current_object($this->dn);
299     $smarty->assign("sub_object_is_addable",  
300         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && 
301         !preg_match("/freeze/",$this->FAIstate));
303     $tmp = $this->plInfo();
304     foreach($tmp['plProvidedAcls'] as $name => $translated){
305       $smarty->assign($name."ACL",$this->getacl($name));
306     }
308     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
309     return($display);
310   }
313   /* Generate listbox friendly SubObject list
314    */
315   function getList($use_dns=false){
316     $a_return=array();
317     foreach($this->SubObjects as $obj){
318       if($obj['status'] != "delete"){
320         $cn   = stripslashes($obj['cn']);
321         $desc = "";
323         if((isset($obj['description']))&&(!empty($obj['description']))){
324           $desc = " [".stripslashes($obj['description'])."]";
325         }
327         if($use_dns){
328           $a_return[$obj['cn']]['name']= $cn.$desc;
329           $a_return[$obj['cn']]['dn']= $obj['dn'];
330           $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
331         }else{
332           $a_return[$obj['cn']] =  $cn.$desc;
333         }
334       }
335     }
336     return($a_return);
337   }
340   /* Delete me, and all my subtrees
341    */
342   function remove_from_parent()
343   {
344     if($this->acl_is_removeable()){
345       $ldap = $this->config->get_ldap_link();
346       $ldap->cd ($this->dn);
347       $release = $this->parent->parent->fai_release;
348       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
349       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
350       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
351  
352       foreach($this->SubObjects as $name => $obj){
353         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
354         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
355       }
356       $this->handle_post_events("remove");
357     }
358   }
361   /* Save data to object 
362    */
363   function save_object()
364   {
365     if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
366       plugin::save_object();
367     }
368     
369     /* Get sort order */
370     if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
371       if($this->sort_by == $_GET['sort']){
372         if($this->sort_order == "up"){
373           $this->sort_order = "down";
374         }elseif($this->sort_order == "down"){
375           $this->sort_order = "up";
376         }
377       }
378       $this->sort_by = $_GET['sort'];
379     }
380   }
383   /* Check supplied data */
384   function check()
385   {
386     /* Call common method to give check the hook */
387     $message= plugin::check();
389     /* Ensure that we do not overwrite an allready existing entry 
390      */
391     if($this->is_new){
392       $release = $this->parent->parent->fai_release;
393       $new_dn= 'cn='.$this->cn.",".get_ou('faiScriptRDN').get_ou('faiBaseRDN').$release;
394       $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
395       if(isset($res[$this->cn])){
396         $message[] = msgPool::duplicated(_("Name"));
397       }
398     }
400     return ($message);
401   }
404   /* Save to LDAP */
405   function save()
406   {
407     plugin::save();
409     $ldap = $this->config->get_ldap_link();
411     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
413     if($this->initially_was_account){
414       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
415     }else{
416       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
417     }
419     /* Prepare FAIscriptEntry to write it to ldap
420      * First sort array.
421      *  Because we must delete old entries first.
422      * After deletion, we perform add and modify 
423      */
424     $Objects = array();
426     /* We do not need to save untouched objects */
427     foreach($this->SubObjects as $name => $obj){
428       if($obj['status'] == "FreshLoaded"){
429         unset($this->SubObjects[$name]);
430       }
431     }
433     foreach($this->SubObjects as $name => $obj){
434       if($obj['status'] == "delete"){
435         $Objects[$name] = $obj; 
436       }
437     }
438     foreach($this->SubObjects as $name => $obj){
439       if($obj['status'] != "delete"){
440         $Objects[$name] = $obj; 
441       }
442     }
444     foreach($Objects as $name => $obj){
446       foreach($this->sub64coded as $codeIt){
447         $obj[$codeIt]=postEncode(stripslashes($obj[$codeIt]));
448       }
450       $tmp = array();
451       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
452       foreach($attributes as $attrs){
453         if(!isset($obj[$attrs])) continue; 
454         if($obj[$attrs] == ""){
455           $obj[$attrs] = array();
456         }
457         if(!is_array($obj[$attrs])){
458           $tmp[$attrs] = stripslashes($obj[$attrs]);
459         }else{
460           $tmp[$attrs] = $obj[$attrs];
461         }
462       }    
464       $tmp['objectClass'] = $this->subClasses;
466       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
468       if($obj['status']=="new"){
469         $ldap->cat($sub_dn,array("objectClass"));
470         if($ldap->count()){
471           $obj['status']="edited";
472         }
473       }
475       if(empty($tmp['FAIpriority'])){
476         $tmp['FAIpriority']  ="0";
477       }
479       /* Tag object */
480       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
482       if($obj['status'] == "delete"){
483         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
484         $this->handle_post_events("remove");
485       }elseif($obj['status'] == "edited"){
486         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
487         $this->handle_post_events("modify");
488       }elseif($obj['status']=="new"){
489         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
490         $this->handle_post_events("add");
491       }
492     }
493   }
496   function PrepareForCopyPaste($source)
497   {
498     plugin::PrepareForCopyPaste($source);
500     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
501      */
502     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
503     foreach($res as $obj){
505       /* Skip not relevant objects */
506       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
508       $objects = array();
509       $objects['status']      = "edited";
510       $objects['dn']          = $obj['dn'];
511       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
512       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
513       $this->SubObjects[$objects['cn']] = $objects;
514     }
515   }
518   /*! \brief  Used for copy & paste.
519               Returns a HTML input mask, which allows to change the cn of this entry.
520       @param  Array   Array containing current status && a HTML template.
521    */
522   function getCopyDialog()
523   {
524     $vars = array("cn");
525     $smarty = get_smarty();
526     $smarty->assign("cn", htmlentities($this->cn));
527     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
528     $ret = array();
529     $ret['string'] = $str;
530     $ret['status'] = "";
531     return($ret);
532   }
535   /*! \brief  Used for copy & paste.
536               Some entries must be renamed to avaoid duplicate entries.
537    */
538   function saveCopyDialog()
539   {
540     if(isset($_POST['cn'])){
541       $this->cn = get_post('cn');
542     }
543   }
544   
546   /* Return plugin informations for acl handling */ 
547   static function plInfo()
548   {
549     return (array( 
550           "plShortName" => _("Script"),
551           "plDescription" => _("FAI script"),
552           "plSelfModify"  => FALSE,
553           "plDepends"     => array(),
554           "plPriority"    => 18,
555           "plSection"     => array("administration"),
556           "plCategory"    => array("fai"),
557           "plProvidedAcls" => array(
558             "cn"                => _("Name")." ("._("Readonly").")",
559             "description"       => _("Description"))
560           ));
561   }
564 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
565 ?>