Code

Removed seperate readBinary function
[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","FAIscript"); 
28   var $sub64coded       = array();
30   /* Specific attributes */
31   var $cn               = "";       // The class name for this object
32   var $description      = "";       // The description for this set of partitions
33   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
34   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
35   var $SubObjects       = array();  // All leafobjects of this object
37   var $FAIstate         ="";
39   function faiScript ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl ="#all#";
45     
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49      */
50     if($dn != "new"){
52       /* Set acls
53        */
54       $ui   = get_userinfo();
55       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
56       $acli = get_module_permission($acl, "FAIclass", $this->dn);
57       $this->acl=$acli;
59       $this->dn =$dn;
61       /* Get FAIstate
62        */
63       if(isset($this->attrs['FAIstate'][0])){
64         $this->FAIstate = $this->attrs['FAIstate'][0];
65       }
67       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68        */
69       $ldap     = $this->config->get_ldap_link();
70       $ldap->cd ($this->dn);
71       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
73       while($object = $ldap->fetch()){
74         /* Set status for save management */
76         foreach($this->subAttributes as $attrs){
77           if(!isset($object[$attrs][0])){
78             $this->SubObjects[$object['cn'][0]][$attrs]="";
79           }else{
80             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
81           }
82         }
84         foreach($this->sub64coded as $codeIt){
85           $this->SubObjects[$object['cn'][0]][$codeIt]=(base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]));
86         }
88         foreach($this->subAttributes as $attrs){
89           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
90         }
92         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes ($ldap->get_attribute($object['dn'], "FAIscript",$r_array=0));
94         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
95         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
96       }
97     }
98   }
100   function execute()
101   {
102     /* Call parent execute */
103     plugin::execute();
105     /* Fill templating stuff */
106     $smarty= get_smarty();
107     $display= "";
109     /* Add new sub object */
110     if(isset($_POST['AddSubObject'])){
111       $this->dialog= new $this->subClassName($this->config,"new");
112       $this->dialog->acl = $this->acl;
113       $this->is_dialog=true;
114     }
116     if($this->dn != "new"){
117       $_SESSION['objectinfo']= $this->dn;
118     }
121     /* New Listhandling 
122      */
123     $once = true;
124     foreach($_POST as $name => $value){
125       if(preg_match("/^editscript_/",$name)&&($once)){
126         $once = false;
127         $entry = preg_replace("/^editscript_/","",$name);
128         $entry = base64_decode(preg_replace("/_.*/","",$entry));
129         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
130         $this->dialog->acl = $this->acl;
131         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
132         $this->is_dialog=true;
133       }
134       if(preg_match("/^deletescript_/",$name)&&($once)){
135         $once = false;
136         $entry = preg_replace("/^deletescript_/","",$name);
137         $entry = base64_decode(preg_replace("/_.*/","",$entry));
138         if($this->SubObjects[$entry]['status'] == "edited"){
139           $this->SubObjects[$entry]['status']= "delete";
140         }else{
141           unset($this->SubObjects[$entry]);
142         }
143       }
144     }
145     ///// Ende new list handling
148     /* Edit selected Sub Object */
149     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
150       $script = $_POST['SubObject'][0];
151       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
152       $this->dialog->acl = $this->acl;
153       $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
154       $this->is_dialog=true;
155     }
157     /* Remove Sub object */
158     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
160       foreach($_POST['SubObject'] as $script){
161         if($this->SubObjects[$script]['status'] == "edited"){
162           $this->SubObjects[$script]['status']= "delete";
163         }else{
164           unset($this->SubObjects[$script]);
165         }
166       }
167     }
169     /* Save Dialog */
170     if(isset($_POST['SaveSubObject'])){
171       $this->dialog->save_object();
172       $msgs = $this->dialog->check();
173       if(count($msgs)>0){
174         foreach($msgs as $msg){
175           print_red($msg);
176         }
177       }else{
178         $obj = $this->dialog->save();
179         if(isset($obj['remove'])){
180           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
181             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
182           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
183             unset($this->SubObjects[$obj['remove']['from']]);
184           }
185           $obj['status'] = "new";
186           $this->SubObjects[$obj['remove']['to']] = $obj;
187           unset($this->SubObjects[$obj['remove']['to']]['remove']);
188         }else{
189           $this->SubObjects[$obj['cn']]=$obj;
190         }
191         $this->is_dialog=false;
192         unset($this->dialog);
193         $this->dialog=NULL;
194       }
195     }
197     /* Sort entries */
198     $tmp = $keys = array();
199     foreach($this->SubObjects as $key => $entry){
200       $keys[$key]=$key;
201     }
202     natcasesort($keys);
203     foreach($keys as $key){
204       $tmp[$key]=$this->SubObjects[$key];
205     }
206     $this->SubObjects = $tmp;
208     /* Cancel Dialog */
209     if(isset($_POST['CancelSubObject'])){
210       $this->is_dialog=false; 
211       unset($this->dialog);
212       $this->dialog=NULL;
213     }
215     /* Print dialog if $this->dialog is set */
216     if($this->dialog){
217       $this->dialog->save_object();
218       $display = $this->dialog->execute();
219       return($display);
220     }
222     /* Divlist            added 23.02.2006 
223        Containing FAIscripts 
224      */
225     $divlist = new divSelectBox("FAIscripts");
226     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
227       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
228       $img_remo = "";
229     }else{
230       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
231       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
232     }
234     foreach($this->getList(true) as $key => $name){
236       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
237         $down = "";
238       }else{
239         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' target='_blank'>
240           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
241           </a>"; 
242       } 
244       $divlist->AddEntry(array( array("string"=>$name['name']),
245             array("string"=>$down , "attach" => "style='width:20px;'"),
246             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
247               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
248     }
249     $smarty->assign("Entry_divlist",$divlist->DrawList());
250     /* Divlist creation complete
251      */
253     $smarty->assign("SubObjects",$this->getList());
254     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
256     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
257      * If we post the escaped strings they will be escaped again
258      */
259     foreach($this->attributes as $attrs){
260       if(get_magic_quotes_gpc()){
261         $smarty->assign($attrs,stripslashes($this->$attrs));
262       }else{
263         $smarty->assign($attrs,($this->$attrs));
264       }
265     }
267     foreach($this->attributes as $attr){
268       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
269     }
271     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
272     return($display);
273   }
275   /* Generate listbox friendly SubObject list
276    */
277   function getList($use_dns=false){
278     $a_return=array();
279     foreach($this->SubObjects as $obj){
280       if($obj['status'] != "delete"){
281         if($use_dns){
282           if((isset($obj['description']))&&(!empty($obj['description']))){
283             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
284           }else{
285             $a_return[$obj['cn']]['name']= $obj['cn'];
286           }
287           $a_return[$obj['cn']]['dn']= $obj['dn'];
288         }else{
289           if((isset($obj['description']))&&(!empty($obj['description']))){
290             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
291           }else{
292             $a_return[$obj['cn']]= $obj['cn'];
293           }
294         }
295       }
296     }
297     return($a_return);
298   }
300   /* Delete me, and all my subtrees
301    */
302   function remove_from_parent()
303   {
304     $ldap = $this->config->get_ldap_link();
305     $ldap->cd ($this->dn);
306     $ldap->rmdir_recursive($this->dn);
307     $this->handle_post_events("remove");    
308   }
311   /* Save data to object 
312    */
313   function save_object()
314   {
315     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
316       plugin::save_object();
317       foreach($this->attributes as $attrs){
318         if(isset($_POST[$attrs])){
319           $this->$attrs = $_POST[$attrs];
320         }
321       }
322     }
323   }
326   /* Check supplied data */
327   function check()
328   {
329     /* Call common method to give check the hook */
330     $message= plugin::check();
332     return ($message);
333   }
336   /* Save to LDAP */
337   function save()
338   {
339     plugin::save();
341     $ldap = $this->config->get_ldap_link();
343     $ldap->cat($this->dn);
344     if($ldap->count()!=0){
345       /* Write FAIscript to ldap*/
346       $ldap->cd($this->dn);
347       $this->cleanup();
348       $ldap->modify ($this->attrs); 
350     }else{
351       /* Write FAIscript to ldap*/
352       $ldap->cd($this->config->current['BASE']);
353       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
354       $ldap->cd($this->dn);
355       $ldap->add($this->attrs);
356     }
357     show_ldap_error($ldap->get_error());
359     /* Prepare FAIscriptEntry to write it to ldap
360      * First sort array.
361      *  Because we must delete old entries first.
362      * After deletion, we perform add and modify 
363      */
364     $Objects = array();
365     foreach($this->SubObjects as $name => $obj){
366       if($obj['status'] == "delete"){
367         $Objects[$name] = $obj; 
368       }
369     }
370     foreach($this->SubObjects as $name => $obj){
371       if($obj['status'] != "delete"){
372         $Objects[$name] = $obj; 
373       }
374     }
376     foreach($Objects as $name => $obj){
378       foreach($this->sub64coded as $codeIt){
379         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
380       }
382       $tmp = array();
383       foreach($this->subAttributes as $attrs){
384         if(empty($obj[$attrs])){
385           $obj[$attrs] = array();
386         }
387         if(!is_array($obj[$attrs])){
388           $tmp[$attrs] = stripslashes($obj[$attrs]);
389         }else{
390           $tmp[$attrs] = $obj[$attrs];
391         }
392       }    
394       $tmp['objectClass'] = $this->subClasses;
396       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
398       if($obj['status']=="new"){
399         $ldap->cat($sub_dn);
400         if($ldap->count()){
401           $obj['status']="modify";
402         }
403       }
405       if(empty($tmp['FAIpriority'])){
406         $tmp['FAIpriority']  ="0";
407       }
409       if($obj['status'] == "delete"){
410         $ldap->cd($sub_dn);
411         $ldap->rmdir_recursive($sub_dn);
412         $this->handle_post_events("remove");
413       }elseif($obj['status'] == "edited"){
414         $ldap->cd($sub_dn);
415         $this->cleanup();
416         $ldap->modify ($tmp); 
418         $this->handle_post_events("modify");
419       }elseif($obj['status']=="new"){
420         if($tmp['description']==array()){
421           unset($tmp['description']);
422         }
423         if($tmp['FAIscript']==array()){
424           $tmp['FAIscript']=" ";
425         }
426         $ldap->cd($this->config->current['BASE']);
427         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
428         $ldap->cd($sub_dn);
429         $ldap->add($tmp); 
430         $this->handle_post_events("add");
431       }
432       show_ldap_error($ldap->get_error()); 
433     }
434   }
437 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
438 ?>