Code

Udpated body format
[gosa.git] / plugins / 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 $dialog           = NULL;     // a dialog, e.g. new disk dialog
32   var $SubObjects       = array();  // All leafobjects of this object
34   var $FAIstate         ="";
36   var $base             ="";
37   var $release          ="";
38   var $copy_paste_mode  = false;
39   var $cut_paste_mode   = false;
40   var $CopyPasteVars    = array("SubObjects");
43   function faiScript ($config, $dn= NULL)
44   {
45     /* Load Attributes */
46     plugin::plugin ($config, $dn);
48     $this->acl ="#all#";
49     
50     /* If "dn==new" we try to create a new entry
51      * Else we must read all objects from ldap which belong to this entry.
52      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
53      */
54     if($dn != "new"){
56       /* Set acls
57        */
58       $ui   = get_userinfo();
59       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
60       $acli = get_module_permission($acl, "FAIclass", $this->dn);
61       $this->acl=$acli;
63       $this->dn =$dn;
65       /* Get FAIstate
66        */
67       if(isset($this->attrs['FAIstate'][0])){
68         $this->FAIstate = $this->attrs['FAIstate'][0];
69       }
71       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
72        */
73       $ldap     = $this->config->get_ldap_link();
74       $ldap->cd ($this->dn);
75       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
77       while($object = $ldap->fetch()){
78         /* Set status for save management */
79         $objects = array();
80         $objects['status']      = "FreshLoaded";
81         $objects['dn']          = $object['dn'];
82         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
83         $this->SubObjects[$objects['cn']] = $objects;
85       }
86     }
87   }
89   
90   /* Reload some attributes */
91   function get_object_attributes($object,$attributes)
92   {
93     $ldap = $this->config->get_ldap_link();
94     $ldap->cd($this->config->current['BASE']);
95     $ldap->cat($object['dn'],$attributes);
96     $tmp  = $ldap->fetch();
98     foreach($attributes as $attrs){
99       if(isset($tmp[$attrs][0])){
100         $var = $tmp[$attrs][0];
102         /* Check if we must decode some attributes */
103         if(in_array_ics($attrs,$this->sub64coded)){
104           $var = base64_decode($var);
105         }
107         /*  check if this is a binary entry */
108         if(in_array_ics($attrs,$this->subBinary)){
109           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110         }
112         /* Fix slashes */
113         $var = addslashes($var);
114         $object[$attrs] = $var;
115       }
116     }
117     return($object);
118   }
121   function execute()
122   {
123     /* Call parent execute */
124     plugin::execute();
126     $as[$bd][d] =2;
128     /* Fill templating stuff */
129     $smarty= get_smarty();
130     $display= "";
132     /* Add new sub object */
133     if(isset($_POST['AddSubObject'])){
134       $this->dialog= new $this->subClassName($this->config,"new");
135       $this->dialog->acl = $this->acl;
136       $this->dialog->parent = &$this;
137       $this->is_dialog=true;
138     }
140     if($this->dn != "new"){
141       $_SESSION['objectinfo']= $this->dn;
142     }
145     /* New Listhandling 
146      */
147     $once = true;
148     foreach($_POST as $name => $value){
149       if(preg_match("/^editscript_/",$name)&&($once)){
150         $once = false;
151         $entry = preg_replace("/^editscript_/","",$name);
152         $entry = base64_decode(preg_replace("/_.*/","",$entry));
154         $obj  = $this->SubObjects[$entry];
155         if($obj['status'] == "FreshLoaded"){
156           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
157         }
159         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
160         $this->dialog->acl = $this->acl;
161         $_SESSION['objectinfo'] = $obj['dn'];
162         $this->dialog->parent = &$this;
163         $this->is_dialog=true;
165       }
166       if(preg_match("/^deletescript_/",$name)&&($once)){
167         $once = false;
168         $entry = preg_replace("/^deletescript_/","",$name);
169         $entry = base64_decode(preg_replace("/_.*/","",$entry));
171         $status = $this->SubObjects[$entry]['status'];
172         if($status == "edited" || $status == "FreshLoaded"){
173           $this->SubObjects[$entry]['status']= "delete";
174         }else{
175           unset($this->SubObjects[$entry]);
176         }
177       }
178     }
179     ///// Ende new list handling
180     if(isset($_POST['SaveSubObject'])){
181       $this->dialog->save_object();
182       $msgs = $this->dialog->check();
183       if(count($msgs)>0){
184         foreach($msgs as $msg){
185           print_red($msg);
186         }
187       }else{
189         /* Get return object */
190         $obj = $this->dialog->save();
191         if(isset($obj['remove'])){
192           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
194           /* Depending on status, set new status */
195           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
196             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
197           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
198             unset($this->SubObjects[$obj['remove']['from']]);
199           }
200           $obj['status'] = "new";
201           $this->SubObjects[$obj['remove']['to']] = $obj;
202           unset($this->SubObjects[$obj['remove']['to']]['remove']);
203         }else{
204           if($obj['status'] == "FreshLoaded"){
205             $obj['status'] = "edited";
206           }
207           $this->SubObjects[$obj['cn']]=$obj;
208         }
210         $this->is_dialog=false;
211         unset($this->dialog);
212         $this->dialog=NULL;
213       }
214     }
216     /* Sort entries */
217     $tmp = $keys = array();
218     foreach($this->SubObjects as $key => $entry){
219       $keys[$key]=$key;
220     }
221     natcasesort($keys);
222     foreach($keys as $key){
223       $tmp[$key]=$this->SubObjects[$key];
224     }
225     $this->SubObjects = $tmp;
227     /* Cancel Dialog */
228     if(isset($_POST['CancelSubObject'])){
229       $this->is_dialog=false; 
230       unset($this->dialog);
231       $this->dialog=NULL;
232     }
234     /* Print dialog if $this->dialog is set */
235     if($this->dialog){
236       $this->dialog->save_object();
237       $display = $this->dialog->execute();
238       return($display);
239     }
241     /* Divlist            added 23.02.2006 
242        Containing FAIscripts 
243      */
244     $divlist = new divSelectBox("FAIscripts");
245     $divlist->setHeight(400);
246     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
247       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
248       $img_remo = "";
249     }else{
250       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
251       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
252     }
254     foreach($this->getList(true) as $key => $name){
256       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
257         $down = "";
258       }else{
259         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
260           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
261           </a>"; 
262       } 
264       $divlist->AddEntry(array( array("string"=>$name['name']),
265             array("string"=>$down , "attach" => "style='width:20px;'"),
266             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
267               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
268     }
269     $smarty->assign("Entry_divlist",$divlist->DrawList());
271     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
272      * If we post the escaped strings they will be escaped again
273      */
274     foreach($this->attributes as $attrs){
275       if(get_magic_quotes_gpc()){
276         $smarty->assign($attrs,stripslashes($this->$attrs));
277       }else{
278         $smarty->assign($attrs,($this->$attrs));
279       }
280     }
282     foreach($this->attributes as $attr){
283       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
284     }
286     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
287     return($display);
288   }
290   /* Generate listbox friendly SubObject list
291    */
292   function getList($use_dns=false){
293     $a_return=array();
294     foreach($this->SubObjects as $obj){
295       if($obj['status'] != "delete"){
296         if($use_dns){
297           if((isset($obj['description']))&&(!empty($obj['description']))){
298             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
299           }else{
300             $a_return[$obj['cn']]['name']= $obj['cn'];
301           }
302           $a_return[$obj['cn']]['dn']= $obj['dn'];
303         }else{
304           if((isset($obj['description']))&&(!empty($obj['description']))){
305             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
306           }else{
307             $a_return[$obj['cn']]= $obj['cn'];
308           }
309         }
310       }
311     }
312     return($a_return);
313   }
315   /* Delete me, and all my subtrees
316    */
317   function remove_from_parent()
318   {
319     $ldap = $this->config->get_ldap_link();
320     $ldap->cd ($this->dn);
321     $ldap->rmdir_recursive($this->dn);
322     show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
323     $this->handle_post_events("remove");    
324   }
327   /* Save data to object 
328    */
329   function save_object()
330   {
331     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
332       plugin::save_object();
333       foreach($this->attributes as $attrs){
334         if(isset($_POST[$attrs])){
335           $this->$attrs = $_POST[$attrs];
336         }
337       }
338     }
339   }
342   /* Check supplied data */
343   function check()
344   {
345     /* Call common method to give check the hook */
346     $message= plugin::check();
348     /* If this is a new script, check if a script with this name already exists */
349     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
350      
351       /* Check if current name is already used for fai scripts in selected release */ 
352       $dn = 'cn='.$this->cn.",ou=scripts,".$this->release;
353       $ldap = $this->config->get_ldap_link(); 
354       $ldap->cat($dn);
355       if($ldap->count()){
357         $r =convert_department_dn($this->release);;
358         $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
359       }
360     }
361     return ($message);
362   }
365   /* Save to LDAP */
366   function save()
367   {
368     plugin::save();
370     $ldap = $this->config->get_ldap_link();
372     /* Copy & Paste : Ensure that FAIstate is copied too */
373     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
374       $this->attrs['FAIstate'] = $this->FAIstate;
375     }
377     $ldap->cat($this->dn,array("objectClass"));
378     if($ldap->count()!=0){
379       /* Write FAIscript to ldap*/
380       $ldap->cd($this->dn);
381       $this->cleanup();
382       $ldap->modify ($this->attrs); 
384     }else{
385       /* Write FAIscript to ldap*/
386       $ldap->cd($this->config->current['BASE']);
387       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
388       $ldap->cd($this->dn);
389       $ldap->add($this->attrs);
390     }
391     show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
393     /* Do object tagging */
394     $this->handle_object_tagging();
396     /* Prepare FAIscriptEntry to write it to ldap
397      * First sort array.
398      *  Because we must delete old entries first.
399      * After deletion, we perform add and modify 
400      */
401     $Objects = array();
403     /* We do not need to save untouched objects */
404     foreach($this->SubObjects as $name => $obj){
405       if($obj['status'] == "FreshLoaded"){
406         if($this->copy_paste_mode){
407           $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
408           $this->SubObjects[$name]['status'] = "new";
409         }else{
410           unset($this->SubObjects[$name]);
411         }
412       }
413     }
415     foreach($this->SubObjects as $name => $obj){
416       if($obj['status'] == "delete"){
417         $Objects[$name] = $obj; 
418       }
419     }
420     foreach($this->SubObjects as $name => $obj){
421       if($obj['status'] != "delete"){
422         $Objects[$name] = $obj; 
423       }
424     }
426     foreach($Objects as $name => $obj){
428       foreach($this->sub64coded as $codeIt){
429         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
430       }
432       $tmp = array();
433       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
434       foreach($attributes as $attrs){
435         if(empty($obj[$attrs])){
436           $obj[$attrs] = array();
437         }
438         if(!is_array($obj[$attrs])){
439           $tmp[$attrs] = stripslashes($obj[$attrs]);
440         }else{
441           $tmp[$attrs] = $obj[$attrs];
442         }
443       }    
445       $tmp['objectClass'] = $this->subClasses;
447       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
449       if($obj['status']=="new"){
450         $ldap->cat($sub_dn,array("objectClass"));
451         if($ldap->count()){
452           $obj['status']="edited";
453         }
454       }
456       if(empty($tmp['FAIpriority'])){
457         $tmp['FAIpriority']  ="0";
458       }
460       /* Check if gosaAdministrativeUnitTag is required as object class */
461       if($obj['status'] == "edited"){
462         $ldap->cat($sub_dn,array("objectClass"));
463         $attrs = $ldap->fetch();
464         if(isset($attrs['objectClass'])){
465           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
466             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
467           }
468         }
469       }
471       if($obj['status'] == "delete"){
472         $ldap->cd($sub_dn);
473         $ldap->rmdir_recursive($sub_dn);
474         $this->handle_post_events("remove");
475         show_ldap_error($ldap->get_error(), _("Removing FAI script failed")); 
476       }elseif($obj['status'] == "edited"){
477         $ldap->cd($sub_dn);
478         $this->cleanup();
479         $ldap->modify ($tmp); 
481         $this->handle_post_events("modify");
482       }elseif($obj['status']=="new"){
483         if($tmp['description']==array()){
484           unset($tmp['description']);
485         }
486         if($tmp['FAIscript']==array()){
487           $tmp['FAIscript']=" ";
488         }
489         $ldap->cd($this->config->current['BASE']);
490         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
491         $ldap->cd($sub_dn);
492         $ldap->add($tmp); 
493         $this->handle_post_events("add");
494         show_ldap_error($ldap->get_error(), _("Saving FAI script failed")); 
495       }
497       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
498     }
499   }
501   
502   /* return copy & paste dialog
503    */
504   function getCopyDialog()
505   {
506     /* Ask for cn */
507     $smarty = get_smarty();
508     $smarty->assign("cn" ,$this->cn);
509     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
510     $ret = array();
511     $ret['string'] = $str;
512     $ret['status'] = "";
513     return($ret);
514   }
516   /* Get posted cn */
517   function saveCopyDialog()
518   {
519     if(isset($_POST['cn'])){
520       $this->cn = $_POST['cn'];
521     }
522   }
527 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
528 ?>