Code

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