Code

Switched log call
[gosa.git] / plugins / admin / fai / class_faiScript.inc
1 <?php
3 class faiScript extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIscript");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIscriptEntry";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiScriptEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIpriority"); 
28   var $sub_Load_Later   = array("FAIscript");
29   var $sub64coded       = array();
30   var $subBinary        = array("FAIscript");
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIstate         ="";
41   var $view_logged = FALSE;
42   var $ui;
44   function faiScript ($config, $dn= NULL)
45   {
46     /* Load Attributes */
47     plugin::plugin ($config, $dn);
49     /* If "dn==new" we try to create a new entry
50      * Else we must read all objects from ldap which belong to this entry.
51      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
52      */
53     if($dn != "new"){
55       $this->dn =$dn;
57       /* Get FAIstate
58        */
59       if(isset($this->attrs['FAIstate'][0])){
60         $this->FAIstate = $this->attrs['FAIstate'][0];
61       }
63       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
64        */
65       $ldap     = $this->config->get_ldap_link();
66       $ldap->cd ($this->dn);
67       
68       $attrs_to_search = $this->subAttributes;
69       $attrs_to_search[] = "FAIstate";
70       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
72       while($object = $ldap->fetch()){
74         /* Skip objects, that are tagged as removed */
75         if(isset($object['FAIstate'][0])){
76           if(preg_match("/removed$/",$object['FAIstate'][0])){
77             continue;
78           }
79         }
81         /* Set status for save management */
82         $objects = array();
83         $objects['status']      = "FreshLoaded";
84         $objects['dn']          = $object['dn'];
85         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
86         $this->SubObjects[$objects['cn']] = $objects;
87       }
88     
89     }
90     $this->ui = get_userinfo();
91   }
94   /* Reload some attributes */
95   function get_object_attributes($object,$attributes)
96   {
97     $ldap = $this->config->get_ldap_link();
98     $ldap->cd($this->config->current['BASE']);
99     $ldap->cat($object['dn'],$attributes);
100     $tmp  = $ldap->fetch();
102     foreach($attributes as $attrs){
103       if(isset($tmp[$attrs][0])){
104         $var = $tmp[$attrs][0];
106         /* Check if we must decode some attributes */
107         if(in_array_ics($attrs,$this->sub64coded)){
108           $var = base64_decode($var);
109         }
111         /*  check if this is a binary entry */
112         if(in_array_ics($attrs,$this->subBinary)){
113           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
114         }
116         /* Fix slashes */
117         $var = addslashes($var);
118         $object[$attrs] = $var;
119       }
120     }
121     return($object);
122   }
124   
125   /* Return a valid dn to fetch acls. Because 'new' will not work. */
126   function acl_base_for_current_object($dn)
127   {
128     if($dn == "new"){
129       if($this->dn == "new"){
130         $dn= "cn=dummy,".$_SESSION['CurrentMainBase'];
131       }else{
132         $dn = $this->dn;
133       }
134     }
135     return($dn);
136   }
139   function execute()
140   {
141     /* Call parent execute */
142     plugin::execute();
144     if($this->is_account && !$this->view_logged){
145       $this->view_logged = TRUE;
146       new log("view","fai/".get_class($this),$this->dn);
147     }
149     /* Fill templating stuff */
150     $smarty= get_smarty();
151     $display= "";
153     /* Add new sub object */
154     if(isset($_POST['AddSubObject'])){
155       $this->dialog= new $this->subClassName($this->config,"new");
156       $this->dialog->set_acl_base($this->acl_base);
157       $this->dialog->set_acl_category("fai");
158       $this->is_dialog=true;
159     }
161     if($this->dn != "new"){
162       $_SESSION['objectinfo']= $this->dn;
163     }
165     /* Handle posts */
166     $once = true;
167     foreach($_POST as $name => $value){
169       /* Edit script posted */
170       if(preg_match("/^editscript_/",$name)&&($once)){
172         /* Get posted entry id */
173         $once = false;
174         $entry = preg_replace("/^editscript_/","",$name);
175         $entry = base64_decode(preg_replace("/_.*/","",$entry));
177         /* Get object, and load missing entry values */
178         $obj  = $this->SubObjects[$entry];
179         if($obj['status'] == "FreshLoaded"){
180           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
181         }
183         /* Create new dialog and set acl attributes  */
184         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
185         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
186         $this->dialog->set_acl_category("fai");
188         /* Assign some additional dialog informations like headline and parent  */
189         $_SESSION['objectinfo'] = $obj['dn'];
190         $this->dialog->parent = &$this;
191         $this->is_dialog=true;
192       }
194       /* Delete script requested */
195       if(preg_match("/^deletescript_/",$name)&&($once)){
197         /* Parse out posted entry id */
198         $once = false;
199         $entry = preg_replace("/^deletescript_/","",$name);
200         $entry = base64_decode(preg_replace("/_.*/","",$entry));
202         /* Check acls, are we allowed to delete an entry */
203         $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry")  ;
204         if(preg_match("/d/",$acl)){
205           $status = $this->SubObjects[$entry]['status'];
206           if($status == "edited" || $status == "FreshLoaded"){
207             $this->SubObjects[$entry]['status']= "delete";
208           }else{
209              unset($this->SubObjects[$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           print_red($msg);
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=NULL;
260       }
261     }
263     /* Sort entries */
264     $tmp = $keys = array();
265     foreach($this->SubObjects as $key => $entry){
266       $keys[$key]=$key;
267     }
268     natcasesort($keys);
269     foreach($keys as $key){
270       $tmp[$key]=$this->SubObjects[$key];
271     }
272     $this->SubObjects = $tmp;
274     /* Cancel Dialog */
275     if(isset($_POST['CancelSubObject'])){
276       $this->is_dialog=false; 
277       unset($this->dialog);
278       $this->dialog=NULL;
279     }
281     /* Print dialog if $this->dialog is set */
282     if($this->dialog){
283       $this->dialog->save_object();
284       $display = $this->dialog->execute();
285       return($display);
286     }
288     /* Divlist            added 23.02.2006 
289        Containing FAIscripts 
290      */
291     $divlist = new divSelectBox("FAIscripts");
292     $divlist->setHeight(400);
293     foreach($this->getList(true) as $key => $name){
295       $dn= $this->acl_base_for_current_object($name['dn']);
296       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
297       $act = "";
298       
299       /* Hide delete icon if this object is freezed */
300       if($this->FAIstate == "freeze"){
301         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
302       }else{
303         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
304         if(preg_match("/d/",$acl)){
305           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
306         }
307       }
309       /* Check acls for download icon */
310       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
311       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
312         $down = "";
313       }else{
314         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
315           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
316           </a>"; 
317       } 
319       /* Check if we are allowed to view this object */
320       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn")  ;
321       if(preg_match("/r/",$s_acl)){
322         $divlist->AddEntry(array( array("string"=>$name['name']),
323               array("string"=>$down , "attach" => "style='width:20px;'"),
324               array("string"=>str_replace("%s",base64_encode($key),$act),
325                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
326       }
327     }
328     $smarty->assign("Entry_divlist",$divlist->DrawList());
331     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
332      * If we post the escaped strings they will be escaped again
333      */
334     foreach($this->attributes as $attrs){
335       if(get_magic_quotes_gpc()){
336         $smarty->assign($attrs,stripslashes($this->$attrs));
337       }else{
338         $smarty->assign($attrs,($this->$attrs));
339       }
340     }
342     $dn = $this->acl_base_for_current_object($this->dn);
343     $smarty->assign("sub_object_is_addable",  
344         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) && 
345         !preg_match("/freeze/",$this->FAIstate));
347     $tmp = $this->plInfo();
348     foreach($tmp['plProvidedAcls'] as $name => $translated){
349       $smarty->assign($name."ACL",$this->getacl($name));
350     }
352     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
353     return($display);
354   }
356   /* Generate listbox friendly SubObject list
357    */
358   function getList($use_dns=false){
359     $a_return=array();
360     foreach($this->SubObjects as $obj){
361       if($obj['status'] != "delete"){
362         if($use_dns){
363           if((isset($obj['description']))&&(!empty($obj['description']))){
364             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
365           }else{
366             $a_return[$obj['cn']]['name']= $obj['cn'];
367           }
368           $a_return[$obj['cn']]['dn']= $obj['dn'];
369         }else{
370           if((isset($obj['description']))&&(!empty($obj['description']))){
371             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
372           }else{
373             $a_return[$obj['cn']]= $obj['cn'];
374           }
375         }
376       }
377     }
378     return($a_return);
379   }
381   /* Delete me, and all my subtrees
382    */
383   function remove_from_parent()
384   {
385     if($this->acl_is_removeable()){
386       $ldap = $this->config->get_ldap_link();
387       $ldap->cd ($this->dn);
389       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
390       if($_SESSION['faifilter']['branch'] == "main"){
391         $use_dn = $this->dn;
392       }
393    
394       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
395  
396       prepare_to_save_FAI_object($use_dn,array(),true);
397       
398       foreach($this->SubObjects as $name => $obj){
399         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
400         if($_SESSION['faifilter']['branch'] == "main"){
401           $use_dn = $obj['dn'];
402         }
403         prepare_to_save_FAI_object($use_dn,array(),true);
404       }
405       $this->handle_post_events("remove");
406     }
407   }
410   /* Save data to object 
411    */
412   function save_object()
413   {
414     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
415       plugin::save_object();
416       foreach($this->attributes as $attrs){
417         if(isset($_POST[$attrs])){
418           $this->$attrs = $_POST[$attrs];
419         }
420       }
421     }
422   }
425   /* Check supplied data */
426   function check()
427   {
428     /* Call common method to give check the hook */
429     $message= plugin::check();
431     return ($message);
432   }
435   /* Save to LDAP */
436   function save()
437   {
438     plugin::save();
440     $ldap = $this->config->get_ldap_link();
442     prepare_to_save_FAI_object($this->dn,$this->attrs);
443     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
445     if($this->initially_was_account){
446       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
447     }else{
448       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
449     }
451     /* Do object tagging */
452     $this->handle_object_tagging();
454     /* Prepare FAIscriptEntry to write it to ldap
455      * First sort array.
456      *  Because we must delete old entries first.
457      * After deletion, we perform add and modify 
458      */
459     $Objects = array();
461     /* We do not need to save untouched objects */
462     foreach($this->SubObjects as $name => $obj){
463       if($obj['status'] == "FreshLoaded"){
464         unset($this->SubObjects[$name]);
465       }
466     }
468     foreach($this->SubObjects as $name => $obj){
469       if($obj['status'] == "delete"){
470         $Objects[$name] = $obj; 
471       }
472     }
473     foreach($this->SubObjects as $name => $obj){
474       if($obj['status'] != "delete"){
475         $Objects[$name] = $obj; 
476       }
477     }
479     foreach($Objects as $name => $obj){
481       foreach($this->sub64coded as $codeIt){
482         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
483       }
485       $tmp = array();
486       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
487       foreach($attributes as $attrs){
489         if(empty($obj[$attrs])){
490           $obj[$attrs] = array();
491         }
492         if(!is_array($obj[$attrs])){
493           $tmp[$attrs] = stripslashes($obj[$attrs]);
494         }else{
495           $tmp[$attrs] = $obj[$attrs];
496         }
497       }    
499       $tmp['objectClass'] = $this->subClasses;
501       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
503       if($obj['status']=="new"){
504         $ldap->cat($sub_dn,array("objectClass"));
505         if($ldap->count()){
506           $obj['status']="edited";
507         }
508       }
510       if(empty($tmp['FAIpriority'])){
511         $tmp['FAIpriority']  ="0";
512       }
514       /* Check if gosaAdministrativeUnitTag is required as object class */
515       if($obj['status'] == "edited"){
516         $ldap->cat($sub_dn,array("objectClass"));
517         $attrs = $ldap->fetch();
518         if(isset($attrs['objectClass'])){
519           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
520             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
521           }
522         }
523       }
525       if($obj['status'] == "delete"){
526         prepare_to_save_FAI_object($sub_dn,array(),true);
527         $this->handle_post_events("remove");
528       }elseif($obj['status'] == "edited"){
529         prepare_to_save_FAI_object($sub_dn,$tmp);
530         $this->handle_post_events("modify");
531       }elseif($obj['status']=="new"){
532         prepare_to_save_FAI_object($sub_dn,$tmp);
533         $this->handle_post_events("add");
534       }
536       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
537     }
538   }
539   
541   /* Return plugin informations for acl handling */ 
542   function plInfo()
543   {
544     return (array( 
545           "plShortName" => _("Script"),
546           "plDescription" => _("FAI script"),
547           "plSelfModify"  => FALSE,
548           "plDepends"     => array(),
549           "plPriority"    => 18,
550           "plSection"     => array("administration"),
551           "plCategory"    => array("fai"),
552           "plProvidedAcls" => array(
553             "cn"                => _("Name")." ("._("Readonly").")",
554             "description"       => _("Description"))
555           ));
556   }
559 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
560 ?>