Code

Added printer patch
[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   function faiScript ($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
46     $this->acl ="#all#";
47     
48     /* If "dn==new" we try to create a new entry
49      * Else we must read all objects from ldap which belong to this entry.
50      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
51      */
52     if($dn != "new"){
54       /* Set acls
55        */
56       $ui   = get_userinfo();
57       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
58       $acli = get_module_permission($acl, "FAIclass", $this->dn);
59       $this->acl=$acli;
61       $this->dn =$dn;
63       /* Get FAIstate
64        */
65       if(isset($this->attrs['FAIstate'][0])){
66         $this->FAIstate = $this->attrs['FAIstate'][0];
67       }
69       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
70        */
71       $ldap     = $this->config->get_ldap_link();
72       $ldap->cd ($this->dn);
73       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
75       while($object = $ldap->fetch()){
76         /* Set status for save management */
77         $objects = array();
78         $objects['status']      = "FreshLoaded";
79         $objects['dn']          = $object['dn'];
80         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
81         $this->SubObjects[$objects['cn']] = $objects;
83       }
84     }
85   }
87   
88   /* Reload some attributes */
89   function get_object_attributes($object,$attributes)
90   {
91     $ldap = $this->config->get_ldap_link();
92     $ldap->cd($this->config->current['BASE']);
93     $ldap->cat($object['dn'],$attributes);
94     $tmp  = $ldap->fetch();
96     foreach($attributes as $attrs){
97       if(isset($tmp[$attrs][0])){
98         $var = $tmp[$attrs][0];
100         /* Check if we must decode some attributes */
101         if(in_array_ics($attrs,$this->sub64coded)){
102           $var = base64_decode($var);
103         }
105         /*  check if this is a binary entry */
106         if(in_array_ics($attrs,$this->subBinary)){
107           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
108         }
110         /* Fix slashes */
111         $var = addslashes($var);
112         $object[$attrs] = $var;
113       }
114     }
115     return($object);
116   }
119   function execute()
120   {
121     /* Call parent execute */
122     plugin::execute();
124     /* Fill templating stuff */
125     $smarty= get_smarty();
126     $display= "";
128     /* Add new sub object */
129     if(isset($_POST['AddSubObject'])){
130       $this->dialog= new $this->subClassName($this->config,"new");
131       $this->dialog->acl = $this->acl;
132       $this->is_dialog=true;
133     }
135     if($this->dn != "new"){
136       $_SESSION['objectinfo']= $this->dn;
137     }
140     /* New Listhandling 
141      */
142     $once = true;
143     foreach($_POST as $name => $value){
144       if(preg_match("/^editscript_/",$name)&&($once)){
145         $once = false;
146         $entry = preg_replace("/^editscript_/","",$name);
147         $entry = base64_decode(preg_replace("/_.*/","",$entry));
149         $obj  = $this->SubObjects[$entry];
150         if($obj['status'] == "FreshLoaded"){
151           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
152         }
154         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
155         $this->dialog->acl = $this->acl;
156         $_SESSION['objectinfo'] = $obj['dn'];
157         $this->dialog->parent = &$this;
158         $this->is_dialog=true;
160       }
161       if(preg_match("/^deletescript_/",$name)&&($once)){
162         $once = false;
163         $entry = preg_replace("/^deletescript_/","",$name);
164         $entry = base64_decode(preg_replace("/_.*/","",$entry));
165         if($this->SubObjects[$entry]['status'] == "edited"){
166           $this->SubObjects[$entry]['status']= "delete";
167         }else{
168           unset($this->SubObjects[$entry]);
169         }
170       }
171     }
172     ///// Ende new list handling
173     if(isset($_POST['SaveSubObject'])){
174       $this->dialog->save_object();
175       $msgs = $this->dialog->check();
176       if(count($msgs)>0){
177         foreach($msgs as $msg){
178           print_red($msg);
179         }
180       }else{
182         /* Get return object */
183         $obj = $this->dialog->save();
184         if(isset($obj['remove'])){
185           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
187           /* Depending on status, set new status */
188           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
189             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
190           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
191             unset($this->SubObjects[$obj['remove']['from']]);
192           }
193           $obj['status'] = "new";
194           $this->SubObjects[$obj['remove']['to']] = $obj;
195           unset($this->SubObjects[$obj['remove']['to']]['remove']);
196         }else{
197           if($obj['status'] == "FreshLoaded"){
198             $obj['status'] = "edited";
199           }
200           $this->SubObjects[$obj['cn']]=$obj;
201         }
203         $this->is_dialog=false;
204         unset($this->dialog);
205         $this->dialog=NULL;
206       }
207     }
209     /* Sort entries */
210     $tmp = $keys = array();
211     foreach($this->SubObjects as $key => $entry){
212       $keys[$key]=$key;
213     }
214     natcasesort($keys);
215     foreach($keys as $key){
216       $tmp[$key]=$this->SubObjects[$key];
217     }
218     $this->SubObjects = $tmp;
220     /* Cancel Dialog */
221     if(isset($_POST['CancelSubObject'])){
222       $this->is_dialog=false; 
223       unset($this->dialog);
224       $this->dialog=NULL;
225     }
227     /* Print dialog if $this->dialog is set */
228     if($this->dialog){
229       $this->dialog->save_object();
230       $display = $this->dialog->execute();
231       return($display);
232     }
234     /* Divlist            added 23.02.2006 
235        Containing FAIscripts 
236      */
237     $divlist = new divSelectBox("FAIscripts");
238     $divlist->setHeight(400);
239     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
240       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
241       $img_remo = "";
242     }else{
243       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
244       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
245     }
247     foreach($this->getList(true) as $key => $name){
249       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
250         $down = "";
251       }else{
252         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
253           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
254           </a>"; 
255       } 
257       $divlist->AddEntry(array( array("string"=>$name['name']),
258             array("string"=>$down , "attach" => "style='width:20px;'"),
259             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
260               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
261     }
262     $smarty->assign("Entry_divlist",$divlist->DrawList());
264     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
265      * If we post the escaped strings they will be escaped again
266      */
267     foreach($this->attributes as $attrs){
268       if(get_magic_quotes_gpc()){
269         $smarty->assign($attrs,stripslashes($this->$attrs));
270       }else{
271         $smarty->assign($attrs,($this->$attrs));
272       }
273     }
275     foreach($this->attributes as $attr){
276       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
277     }
279     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
280     return($display);
281   }
283   /* Generate listbox friendly SubObject list
284    */
285   function getList($use_dns=false){
286     $a_return=array();
287     foreach($this->SubObjects as $obj){
288       if($obj['status'] != "delete"){
289         if($use_dns){
290           if((isset($obj['description']))&&(!empty($obj['description']))){
291             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
292           }else{
293             $a_return[$obj['cn']]['name']= $obj['cn'];
294           }
295           $a_return[$obj['cn']]['dn']= $obj['dn'];
296         }else{
297           if((isset($obj['description']))&&(!empty($obj['description']))){
298             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
299           }else{
300             $a_return[$obj['cn']]= $obj['cn'];
301           }
302         }
303       }
304     }
305     return($a_return);
306   }
308   /* Delete me, and all my subtrees
309    */
310   function remove_from_parent()
311   {
312     $ldap = $this->config->get_ldap_link();
313     $ldap->cd ($this->dn);
314     $ldap->rmdir_recursive($this->dn);
315     show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
316     $this->handle_post_events("remove");    
317   }
320   /* Save data to object 
321    */
322   function save_object()
323   {
324     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
325       plugin::save_object();
326       foreach($this->attributes as $attrs){
327         if(isset($_POST[$attrs])){
328           $this->$attrs = $_POST[$attrs];
329         }
330       }
331     }
332   }
335   /* Check supplied data */
336   function check()
337   {
338     /* Call common method to give check the hook */
339     $message= plugin::check();
341     return ($message);
342   }
345   /* Save to LDAP */
346   function save()
347   {
348     plugin::save();
350     $ldap = $this->config->get_ldap_link();
352     $ldap->cat($this->dn,array("objectClass"));
353     if($ldap->count()!=0){
354       /* Write FAIscript to ldap*/
355       $ldap->cd($this->dn);
356       $this->cleanup();
357       $ldap->modify ($this->attrs); 
359     }else{
360       /* Write FAIscript to ldap*/
361       $ldap->cd($this->config->current['BASE']);
362       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
363       $ldap->cd($this->dn);
364       $ldap->add($this->attrs);
365     }
366     show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
368     /* Do object tagging */
369     $this->handle_object_tagging();
371     /* Prepare FAIscriptEntry to write it to ldap
372      * First sort array.
373      *  Because we must delete old entries first.
374      * After deletion, we perform add and modify 
375      */
376     $Objects = array();
378     /* We do not need to save untouched objects */
379     foreach($this->SubObjects as $name => $obj){
380       if($obj['status'] == "FreshLoaded"){
381         unset($this->SubObjects[$name]);
382       }
383     }
385     foreach($this->SubObjects as $name => $obj){
386       if($obj['status'] == "delete"){
387         $Objects[$name] = $obj; 
388       }
389     }
390     foreach($this->SubObjects as $name => $obj){
391       if($obj['status'] != "delete"){
392         $Objects[$name] = $obj; 
393       }
394     }
396     foreach($Objects as $name => $obj){
398       foreach($this->sub64coded as $codeIt){
399         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
400       }
402       $tmp = array();
403       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
404       foreach($attributes as $attrs){
405         if(empty($obj[$attrs])){
406           $obj[$attrs] = array();
407         }
408         if(!is_array($obj[$attrs])){
409           $tmp[$attrs] = stripslashes($obj[$attrs]);
410         }else{
411           $tmp[$attrs] = $obj[$attrs];
412         }
413       }    
415       $tmp['objectClass'] = $this->subClasses;
417       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
419       if($obj['status']=="new"){
420         $ldap->cat($sub_dn,array("objectClass"));
421         if($ldap->count()){
422           $obj['status']="edited";
423         }
424       }
426       if(empty($tmp['FAIpriority'])){
427         $tmp['FAIpriority']  ="0";
428       }
430       /* Check if gosaAdministrativeUnitTag is required as object class */
431       if($obj['status'] == "edited"){
432         $ldap->cat($sub_dn,array("objectClass"));
433         $attrs = $ldap->fetch();
434         if(isset($attrs['objectClass'])){
435           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
436             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
437           }
438         }
439       }
441       if($obj['status'] == "delete"){
442         $ldap->cd($sub_dn);
443         $ldap->rmdir_recursive($sub_dn);
444         $this->handle_post_events("remove");
445         show_ldap_error($ldap->get_error(), _("Removing FAI script failed")); 
446       }elseif($obj['status'] == "edited"){
447         $ldap->cd($sub_dn);
448         $this->cleanup();
449         $ldap->modify ($tmp); 
451         $this->handle_post_events("modify");
452       }elseif($obj['status']=="new"){
453         if($tmp['description']==array()){
454           unset($tmp['description']);
455         }
456         if($tmp['FAIscript']==array()){
457           $tmp['FAIscript']=" ";
458         }
459         $ldap->cd($this->config->current['BASE']);
460         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
461         $ldap->cd($sub_dn);
462         $ldap->add($tmp); 
463         $this->handle_post_events("add");
464         show_ldap_error($ldap->get_error(), _("Saving FAI script failed")); 
465       }
467       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
468     }
469   }
472 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
473 ?>