Code

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