Code

Udpated JS focus,
[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     /* Fill templating stuff */
127     $smarty= get_smarty();
128     $display= "";
130     /* Add new sub object */
131     if(isset($_POST['AddSubObject'])){
132       $this->dialog= new $this->subClassName($this->config,"new");
133       $this->dialog->acl = $this->acl;
134       $this->dialog->parent = &$this;
135       $this->is_dialog=true;
136     }
138     if($this->dn != "new"){
139       $_SESSION['objectinfo']= $this->dn;
140     }
143     /* New Listhandling 
144      */
145     $once = true;
146     foreach($_POST as $name => $value){
147       if(preg_match("/^editscript_/",$name)&&($once)){
148         $once = false;
149         $entry = preg_replace("/^editscript_/","",$name);
150         $entry = base64_decode(preg_replace("/_.*/","",$entry));
152         $obj  = $this->SubObjects[$entry];
153         if($obj['status'] == "FreshLoaded"){
154           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
155         }
157         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
158         $this->dialog->acl = $this->acl;
159         $_SESSION['objectinfo'] = $obj['dn'];
160         $this->dialog->parent = &$this;
161         $this->is_dialog=true;
163       }
164       if(preg_match("/^deletescript_/",$name)&&($once)){
165         $once = false;
166         $entry = preg_replace("/^deletescript_/","",$name);
167         $entry = base64_decode(preg_replace("/_.*/","",$entry));
169         $status = $this->SubObjects[$entry]['status'];
170         if($status == "edited" || $status == "FreshLoaded"){
171           $this->SubObjects[$entry]['status']= "delete";
172         }else{
173           unset($this->SubObjects[$entry]);
174         }
175       }
176     }
177     ///// Ende new list handling
178     if(isset($_POST['SaveSubObject'])){
179       $this->dialog->save_object();
180       $msgs = $this->dialog->check();
181       if(count($msgs)>0){
182         foreach($msgs as $msg){
183           print_red($msg);
184         }
185       }else{
187         /* Get return object */
188         $obj = $this->dialog->save();
189         if(isset($obj['remove'])){
190           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
192           /* Depending on status, set new status */
193           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
194             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
195           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
196             unset($this->SubObjects[$obj['remove']['from']]);
197           }
198           $obj['status'] = "new";
199           $this->SubObjects[$obj['remove']['to']] = $obj;
200           unset($this->SubObjects[$obj['remove']['to']]['remove']);
201         }else{
202           if($obj['status'] == "FreshLoaded"){
203             $obj['status'] = "edited";
204           }
205           $this->SubObjects[$obj['cn']]=$obj;
206         }
208         $this->is_dialog=false;
209         unset($this->dialog);
210         $this->dialog=NULL;
211       }
212     }
214     /* Sort entries */
215     $tmp = $keys = array();
216     foreach($this->SubObjects as $key => $entry){
217       $keys[$key]=$key;
218     }
219     natcasesort($keys);
220     foreach($keys as $key){
221       $tmp[$key]=$this->SubObjects[$key];
222     }
223     $this->SubObjects = $tmp;
225     /* Cancel Dialog */
226     if(isset($_POST['CancelSubObject'])){
227       $this->is_dialog=false; 
228       unset($this->dialog);
229       $this->dialog=NULL;
230     }
232     /* Print dialog if $this->dialog is set */
233     if($this->dialog){
234       $this->dialog->save_object();
235       $display = $this->dialog->execute();
236       return($display);
237     }
239     /* Divlist            added 23.02.2006 
240        Containing FAIscripts 
241      */
242     $divlist = new divSelectBox("FAIscripts");
243     $divlist->setHeight(400);
244     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
245       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
246       $img_remo = "";
247     }else{
248       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
249       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
250     }
252     foreach($this->getList(true) as $key => $name){
254       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
255         $down = "";
256       }else{
257         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
258           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
259           </a>"; 
260       } 
262       $divlist->AddEntry(array( array("string"=>$name['name']),
263             array("string"=>$down , "attach" => "style='width:20px;'"),
264             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
265               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
266     }
267     $smarty->assign("Entry_divlist",$divlist->DrawList());
269     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
270      * If we post the escaped strings they will be escaped again
271      */
272     foreach($this->attributes as $attrs){
273       if(get_magic_quotes_gpc()){
274         $smarty->assign($attrs,stripslashes($this->$attrs));
275       }else{
276         $smarty->assign($attrs,($this->$attrs));
277       }
278     }
280     foreach($this->attributes as $attr){
281       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
282     }
284     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
285     return($display);
286   }
288   /* Generate listbox friendly SubObject list
289    */
290   function getList($use_dns=false){
291     $a_return=array();
292     foreach($this->SubObjects as $obj){
293       if($obj['status'] != "delete"){
294         if($use_dns){
295           if((isset($obj['description']))&&(!empty($obj['description']))){
296             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
297           }else{
298             $a_return[$obj['cn']]['name']= $obj['cn'];
299           }
300           $a_return[$obj['cn']]['dn']= $obj['dn'];
301         }else{
302           if((isset($obj['description']))&&(!empty($obj['description']))){
303             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
304           }else{
305             $a_return[$obj['cn']]= $obj['cn'];
306           }
307         }
308       }
309     }
310     return($a_return);
311   }
313   /* Delete me, and all my subtrees
314    */
315   function remove_from_parent()
316   {
317     $ldap = $this->config->get_ldap_link();
318     $ldap->cd ($this->dn);
319     $ldap->rmdir_recursive($this->dn);
320     show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
321     $this->handle_post_events("remove");    
322   }
325   /* Save data to object 
326    */
327   function save_object()
328   {
329     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
330       plugin::save_object();
331       foreach($this->attributes as $attrs){
332         if(isset($_POST[$attrs])){
333           $this->$attrs = $_POST[$attrs];
334         }
335       }
336     }
337   }
340   /* Check supplied data */
341   function check()
342   {
343     /* Call common method to give check the hook */
344     $message= plugin::check();
346     /* If this is a new script, check if a script with this name already exists */
347     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
348      
349       /* Check if current name is already used for fai scripts in selected release */ 
350       $dn = 'cn='.$this->cn.",ou=scripts,".$this->release;
351       $ldap = $this->config->get_ldap_link(); 
352       $ldap->cat($dn);
353       if($ldap->count()){
355         $r =convert_department_dn($this->release);;
356         $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
357       }
358     }
359     return ($message);
360   }
363   /* Save to LDAP */
364   function save()
365   {
366     plugin::save();
368     $ldap = $this->config->get_ldap_link();
370     /* Copy & Paste : Ensure that FAIstate is copied too */
371     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
372       $this->attrs['FAIstate'] = $this->FAIstate;
373     }
375     $ldap->cat($this->dn,array("objectClass"));
376     if($ldap->count()!=0){
377       /* Write FAIscript to ldap*/
378       $ldap->cd($this->dn);
379       $this->cleanup();
380       $ldap->modify ($this->attrs); 
382     }else{
383       /* Write FAIscript to ldap*/
384       $ldap->cd($this->config->current['BASE']);
385       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
386       $ldap->cd($this->dn);
387       $ldap->add($this->attrs);
388     }
389     show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
391     /* Do object tagging */
392     $this->handle_object_tagging();
394     /* Prepare FAIscriptEntry to write it to ldap
395      * First sort array.
396      *  Because we must delete old entries first.
397      * After deletion, we perform add and modify 
398      */
399     $Objects = array();
401     /* We do not need to save untouched objects */
402     foreach($this->SubObjects as $name => $obj){
403       if($obj['status'] == "FreshLoaded"){
404         if($this->copy_paste_mode){
405           $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
406           $this->SubObjects[$name]['status'] = "new";
407         }else{
408           unset($this->SubObjects[$name]);
409         }
410       }
411     }
413     foreach($this->SubObjects as $name => $obj){
414       if($obj['status'] == "delete"){
415         $Objects[$name] = $obj; 
416       }
417     }
418     foreach($this->SubObjects as $name => $obj){
419       if($obj['status'] != "delete"){
420         $Objects[$name] = $obj; 
421       }
422     }
424     foreach($Objects as $name => $obj){
426       foreach($this->sub64coded as $codeIt){
427         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
428       }
430       $tmp = array();
431       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
432       foreach($attributes as $attrs){
433         if(empty($obj[$attrs])){
434           $obj[$attrs] = array();
435         }
436         if(!is_array($obj[$attrs])){
437           $tmp[$attrs] = stripslashes($obj[$attrs]);
438         }else{
439           $tmp[$attrs] = $obj[$attrs];
440         }
441       }    
443       $tmp['objectClass'] = $this->subClasses;
445       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
447       if($obj['status']=="new"){
448         $ldap->cat($sub_dn,array("objectClass"));
449         if($ldap->count()){
450           $obj['status']="edited";
451         }
452       }
454       if(empty($tmp['FAIpriority'])){
455         $tmp['FAIpriority']  ="0";
456       }
458       /* Check if gosaAdministrativeUnitTag is required as object class */
459       if($obj['status'] == "edited"){
460         $ldap->cat($sub_dn,array("objectClass"));
461         $attrs = $ldap->fetch();
462         if(isset($attrs['objectClass'])){
463           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
464             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
465           }
466         }
467       }
469       if($obj['status'] == "delete"){
470         $ldap->cd($sub_dn);
471         $ldap->rmdir_recursive($sub_dn);
472         $this->handle_post_events("remove");
473         show_ldap_error($ldap->get_error(), _("Removing FAI script failed")); 
474       }elseif($obj['status'] == "edited"){
475         $ldap->cd($sub_dn);
476         $this->cleanup();
477         $ldap->modify ($tmp); 
479         $this->handle_post_events("modify");
480       }elseif($obj['status']=="new"){
481         if($tmp['description']==array()){
482           unset($tmp['description']);
483         }
484         if($tmp['FAIscript']==array()){
485           $tmp['FAIscript']=" ";
486         }
487         $ldap->cd($this->config->current['BASE']);
488         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
489         $ldap->cd($sub_dn);
490         $ldap->add($tmp); 
491         $this->handle_post_events("add");
492         show_ldap_error($ldap->get_error(), _("Saving FAI script failed")); 
493       }
495       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
496     }
497   }
499   
500   /* return copy & paste dialog
501    */
502   function getCopyDialog()
503   {
504     /* Ask for cn */
505     $smarty = get_smarty();
506     $smarty->assign("cn" ,$this->cn);
507     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
508     $ret = array();
509     $ret['string'] = $str;
510     $ret['status'] = "";
511     return($ret);
512   }
514   /* Get posted cn */
515   function saveCopyDialog()
516   {
517     if(isset($_POST['cn'])){
518       $this->cn = $_POST['cn'];
519     }
520   }
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>