Code

da995c33e5799147b3be96a650e1887567a81302
[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->convertList(TRUE), $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("140px");
81     $this->scriptListWidget->setHeader(array(_("Name"),_("Description")));
82   }
85   function convertList($type = FALSE)
86   {
87     $data = array();
88     $s_acl = $this->ui->get_permissions($this->dn,"fai/faiScriptEntry","FAIscript");
89     foreach($this->SubObjects as $cn => $entry){
90       if($entry['status'] == "delete") continue;
91       if($type){
92         $data[$cn] = $entry;
93       }else{
94         if(!isset($entry['description'])) $entry['description']="";
95         $data[$cn] = array('data' => array($entry['cn'], $entry['description']));
96       }
97     }
98     return($data);
99   }
102   /* Reload some attributes */
103   function get_object_attributes($object,$attributes)
104   {
105     $ldap = $this->config->get_ldap_link();
106     $ldap->cd($this->config->current['BASE']);
107     $ldap->cat($object['dn'],$attributes);
108     $tmp  = $ldap->fetch();
110     foreach($attributes as $attrs){
111       if(isset($tmp[$attrs][0])){
112         $var = $tmp[$attrs][0];
114         /* Check if we must decode some attributes */
115         if(in_array_ics($attrs,$this->sub64coded)){
116           $var = base64_decode($var);
117         }
119         /*  check if this is a binary entry */
120         if(in_array_ics($attrs,$this->subBinary)){
121           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
122         }
124         $object[$attrs] = $var;
125       }
126     }
127     return($object);
128   }
130   
131   /* Return a valid dn to fetch acls. Because 'new' will not work. */
132   function acl_base_for_current_object($dn)
133   {
134     if($dn == "new" || $dn == ""){
135       if($this->dn == "new"){
136         $dn= $this->parent->parent->acl_base;
137       }else{
138         $dn = $this->dn;
139       }
140     }
141     return($dn);
142   }
145   function execute()
146   {
147     /* Call parent execute */
148     plugin::execute();
150     if($this->is_account && !$this->view_logged){
151       $this->view_logged = TRUE;
152       new log("view","fai/".get_class($this),$this->dn);
153     }
155     /* Fill templating stuff */
156     $smarty= get_smarty();
157     $display= "";
159     /* Add new sub object */
160     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
161       $this->dialog= new $this->subClassName($this->config,"new");
162       $this->dialog->FAIstate = $this->FAIstate;
163       $this->dialog->set_acl_base($this->acl_base);
164       $this->dialog->set_acl_category("fai");
165       $this->dialog->parent = &$this;
166       $this->is_dialog=true;
167     }
169     if($this->dn != "new"){
170       set_object_info($this->dn);
171     }
173     $this->scriptListWidget->setAcl($this->getacl(""));
174     $this->scriptListWidget->save_object();
175     $action = $this->scriptListWidget->getAction();
176     if($action['action'] =="edit"){
177       $s_entry = $this->scriptListWidget->getKey($action['targets'][0]);
178       if(isset($this->SubObjects[$s_entry])){ 
180         $obj  = $this->SubObjects[$s_entry];
181         if($obj['status'] == "FreshLoaded"){
182           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
183         }
185         /* Create new dialog and set acl attributes  */
186         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
187         $this->dialog->FAIstate = $this->FAIstate;
188         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
189         $this->dialog->set_acl_category("fai");
191         /* Assign some additional dialog informations like headline and parent  */
192         set_object_info($obj['dn']);
193         $this->dialog->parent = &$this;
194         $this->is_dialog=true;
195       }
196     }
198     /* Check acls, are we allowed to delete an entry */
199     if($action['action'] =="delete"){
200       $s_entry = $this->scriptListWidget->getKey($action['targets'][0]);
201       if(isset($this->SubObjects[$s_entry])){ 
202         $entry = $this->SubObjects[$s_entry];  
203         $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry")  ;
204         if(preg_match("/d/",$acl)){
205           $status = $entry['status'];
206           if($status == "edited" || $status == "FreshLoaded"){
207             $this->SubObjects[$s_entry]['status']= "delete";
208           }else{
209             unset($this->SubObjects[$s_entry]);
210           }
211         }
212       }
213     }
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     /* Cancel Dialog */
264     if(isset($_POST['CancelSubObject'])){
265       $this->is_dialog=false; 
266       unset($this->dialog);
267       $this->dialog=FALSE;
268     }
270     /* Print dialog if $this->dialog is set */
271     if(is_object($this->dialog)){
272       $this->dialog->save_object();
273       $display = $this->dialog->execute();
274       return($display);
275     }
277     $this->scriptListWidget->setListData($this->convertList(TRUE), $this->convertList());
278     $this->scriptListWidget->update();
279     $smarty->assign("Entry_listing",$this->scriptListWidget->render());
281     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
282      * If we post the escaped strings they will be escaped again
283      */
284     foreach($this->attributes as $attrs){
285       $smarty->assign($attrs,set_post($this->$attrs));
286     }
288     $dn = $this->acl_base_for_current_object($this->dn);
289     $smarty->assign("sub_object_is_addable",  
290         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && 
291         !preg_match("/freeze/",$this->FAIstate));
293     $tmp = $this->plInfo();
294     foreach($tmp['plProvidedAcls'] as $name => $translated){
295       $smarty->assign($name."ACL",$this->getacl($name));
296     }
298     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
299     return($display);
300   }
303   /* Delete me, and all my subtrees
304    */
305   function remove_from_parent()
306   {
307     if($this->acl_is_removeable()){
308       $ldap = $this->config->get_ldap_link();
309       $ldap->cd ($this->dn);
310       $release = $this->parent->parent->fai_release;
311       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
312       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
313       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
314  
315       foreach($this->SubObjects as $name => $obj){
316         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
317         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
318       }
319       $this->handle_post_events("remove");
320     }
321   }
324   /* Save data to object 
325    */
326   function save_object()
327   {
328     if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
329       plugin::save_object();
330     }
331     
332     /* Get sort order */
333     if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
334       if($this->sort_by == $_GET['sort']){
335         if($this->sort_order == "up"){
336           $this->sort_order = "down";
337         }elseif($this->sort_order == "down"){
338           $this->sort_order = "up";
339         }
340       }
341       $this->sort_by = $_GET['sort'];
342     }
343   }
346   /* Check supplied data */
347   function check()
348   {
349     /* Call common method to give check the hook */
350     $message= plugin::check();
352     /* Ensure that we do not overwrite an allready existing entry 
353      */
354     if($this->is_new){
355       $release = $this->parent->parent->fai_release;
356       $new_dn= 'cn='.$this->cn.",".get_ou("faiScript", "faiScriptRDN").get_ou("faiManagement", "faiBaseRDN").$release;
357       $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
358       if(isset($res[$this->cn])){
359         $message[] = msgPool::duplicated(_("Name"));
360       }
361     }
363     return ($message);
364   }
367   /* Save to LDAP */
368   function save()
369   {
370     plugin::save();
372     $ldap = $this->config->get_ldap_link();
374     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
376     if($this->initially_was_account){
377       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
378     }else{
379       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
380     }
382     /* Prepare FAIscriptEntry to write it to ldap
383      * First sort array.
384      *  Because we must delete old entries first.
385      * After deletion, we perform add and modify 
386      */
387     $Objects = array();
389     /* We do not need to save untouched objects */
390     foreach($this->SubObjects as $name => $obj){
391       if($obj['status'] == "FreshLoaded"){
392         unset($this->SubObjects[$name]);
393       }
394     }
396     foreach($this->SubObjects as $name => $obj){
397       if($obj['status'] == "delete"){
398         $Objects[$name] = $obj; 
399       }
400     }
401     foreach($this->SubObjects as $name => $obj){
402       if($obj['status'] != "delete"){
403         $Objects[$name] = $obj; 
404       }
405     }
407     foreach($Objects as $name => $obj){
409       foreach($this->sub64coded as $codeIt){
410         $obj[$codeIt]=base64_encode($obj[$codeIt]);
411       }
413       $tmp = array();
414       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
415       foreach($attributes as $attrs){
416         if(!isset($obj[$attrs])) continue; 
417         if($obj[$attrs] == ""){
418           $obj[$attrs] = array();
419         }
420         $tmp[$attrs] = $obj[$attrs];
421       }    
423       $tmp['objectClass'] = $this->subClasses;
425       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
427       if($obj['status']=="new"){
428         $ldap->cat($sub_dn,array("objectClass"));
429         if($ldap->count()){
430           $obj['status']="edited";
431         }
432       }
434       if(empty($tmp['FAIpriority'])){
435         $tmp['FAIpriority']  ="0";
436       }
438       /* Tag object */
439       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
441       if($obj['status'] == "delete"){
442         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
443         $this->handle_post_events("remove");
444       }elseif($obj['status'] == "edited"){
445         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
446         $this->handle_post_events("modify");
447       }elseif($obj['status']=="new"){
448         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
449         $this->handle_post_events("add");
450       }
451     }
452   }
455   function PrepareForCopyPaste($source)
456   {
457     plugin::PrepareForCopyPaste($source);
459     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
460      */
461     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
462     foreach($res as $obj){
464       /* Skip not relevant objects */
465       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
467       $objects = array();
468       $objects['status']      = "edited";
469       $objects['dn']          = $obj['dn'];
470       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
471       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
472       $this->SubObjects[$objects['cn']] = $objects;
473     }
474   }
477   /*! \brief  Used for copy & paste.
478               Returns a HTML input mask, which allows to change the cn of this entry.
479       @param  Array   Array containing current status && a HTML template.
480    */
481   function getCopyDialog()
482   {
483     $vars = array("cn");
484     $smarty = get_smarty();
485     $smarty->assign("cn", set_post($this->cn));
486     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
487     $ret = array();
488     $ret['string'] = $str;
489     $ret['status'] = "";
490     return($ret);
491   }
494   /*! \brief  Used for copy & paste.
495               Some entries must be renamed to avaoid duplicate entries.
496    */
497   function saveCopyDialog()
498   {
499     if(isset($_POST['cn'])){
500       $this->cn = get_post('cn');
501     }
502   }
503   
505   /* Return plugin informations for acl handling */ 
506   static function plInfo()
507   {
508     return (array( 
509           "plShortName" => _("Script"),
510           "plDescription" => _("FAI script"),
511           "plSelfModify"  => FALSE,
512           "plDepends"     => array(),
513           "plPriority"    => 18,
514           "plSection"     => array("administration"),
515           "plCategory"    => array("fai"),
516           "plProperties" =>
517           array(
518               array(
519                   "name"          => "faiScriptRDN",
520                   "type"          => "rdn",
521                   "default"       => "ou=scripts,",
522                   "description"   => _("RDN for FAI script storage."),
523                   "check"         => "gosaProperty::isRdn",
524                   "migrate"       => "migrate_faiScriptRDN",
525                   "group"         => "plugin",
526                   "mandatory"     => TRUE
527                   )
528               ),
531           "plProvidedAcls" => array(
532             "cn"                => _("Name")." ("._("Read-only").")",
533             "description"       => _("Description"))
534           ));
535   }
538 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
539 ?>