Code

Added copy & paste for ogroups
[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 ($this->readBinary("FAIscript",$object['dn']));
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     $message= array();
330     return ($message);
331   }
334   /* Save to LDAP */
335   function save()
336   {
337     plugin::save();
339     $ldap = $this->config->get_ldap_link();
341     $ldap->cat($this->dn);
342     if($ldap->count()!=0){
343       /* Write FAIscript to ldap*/
344       $ldap->cd($this->dn);
345       $this->cleanup();
346       $ldap->modify ($this->attrs); 
348     }else{
349       /* Write FAIscript to ldap*/
350       $ldap->cd($this->config->current['BASE']);
351       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
352       $ldap->cd($this->dn);
353       $ldap->add($this->attrs);
354     }
355     show_ldap_error($ldap->get_error());
357     /* Prepare FAIscriptEntry to write it to ldap
358      * First sort array.
359      *  Because we must delete old entries first.
360      * After deletion, we perform add and modify 
361      */
362     $Objects = array();
363     foreach($this->SubObjects as $name => $obj){
364       if($obj['status'] == "delete"){
365         $Objects[$name] = $obj; 
366       }
367     }
368     foreach($this->SubObjects as $name => $obj){
369       if($obj['status'] != "delete"){
370         $Objects[$name] = $obj; 
371       }
372     }
374     foreach($Objects as $name => $obj){
376       foreach($this->sub64coded as $codeIt){
377         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
378       }
380       $tmp = array();
381       foreach($this->subAttributes as $attrs){
382         if(empty($obj[$attrs])){
383           $obj[$attrs] = array();
384         }
385         if(!is_array($obj[$attrs])){
386           $tmp[$attrs] = stripslashes($obj[$attrs]);
387         }else{
388           $tmp[$attrs] = $obj[$attrs];
389         }
390       }    
392       $tmp['objectClass'] = $this->subClasses;
394       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
396       if($obj['status']=="new"){
397         $ldap->cat($sub_dn);
398         if($ldap->count()){
399           $obj['status']="modify";
400         }
401       }
403       if(empty($tmp['FAIpriority'])){
404         $tmp['FAIpriority']  ="0";
405       }
407       if($obj['status'] == "delete"){
408         $ldap->cd($sub_dn);
409         $ldap->rmdir_recursive($sub_dn);
410         $this->handle_post_events("remove");
411       }elseif($obj['status'] == "edited"){
412         $ldap->cd($sub_dn);
413         $this->cleanup();
414         $ldap->modify ($tmp); 
416         $this->handle_post_events("modify");
417       }elseif($obj['status']=="new"){
418         if($tmp['description']==array()){
419           unset($tmp['description']);
420         }
421         if($tmp['FAIscript']==array()){
422           $tmp['FAIscript']=" ";
423         }
424         $ldap->cd($this->config->current['BASE']);
425         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
426         $ldap->cd($sub_dn);
427         $ldap->add($tmp); 
428         $this->handle_post_events("add");
429       }
430       show_ldap_error($ldap->get_error()); 
431     }
432   }
435   function readBinary($attr,$dn){
436     $Data  ="";
437     $ds= ldap_connect($this->config->current['SERVER']);
438     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
439     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
440       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
441       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
442     }
444     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
445       ldap_start_tls($ds);
446     }
448     $r  = ldap_bind($ds);
449     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
451     if ($sr) {
452       $ei=ldap_first_entry($ds, $sr);
453       if ($ei) {
454         if ($info = ldap_get_values_len($ds, $ei, $attr)){
455           $Data= $info[0];
456         }
457       }
458     }
460     /* close conncetion */
461     ldap_unbind($ds);
462     return($Data);
463   }
468 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
469 ?>