Code

ac945d890fedffa402ae1542b5017aa9546b303a
[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         = "";
35   var $sort_by          = "name";
36   var $sort_order       = "up";
38   var $base             ="";
39   var $release          ="";
40   var $copy_paste_mode  = false;
41   var $cut_paste_mode   = false;
42   var $CopyPasteVars    = array("SubObjects");
45   function faiScript ($config, $dn= NULL)
46   {
47     /* Load Attributes */
48     plugin::plugin ($config, $dn);
50     $this->acl ="#all#";
51     
52     /* If "dn==new" we try to create a new entry
53      * Else we must read all objects from ldap which belong to this entry.
54      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
55      */
56     if($dn != "new"){
58       /* Set acls
59        */
60       $ui   = get_userinfo();
61       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
62       $acli = get_module_permission($acl, "FAIclass", $this->dn);
63       $this->acl=$acli;
65       $this->dn =$dn;
67       /* Get FAIstate
68        */
69       if(isset($this->attrs['FAIstate'][0])){
70         $this->FAIstate = $this->attrs['FAIstate'][0];
71       }
73       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
74        */
75       $ldap     = $this->config->get_ldap_link();
76       $ldap->cd ($this->dn);
77       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
79       while($object = $ldap->fetch()){
80         /* Set status for save management */
81         $objects = array();
82         $objects['status']      = "FreshLoaded";
83         $objects['dn']          = $object['dn'];
84         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
85         $this->SubObjects[$objects['cn']] = $objects;
87       }
88     }
89   }
91   
92   /* Reload some attributes */
93   function get_object_attributes($object,$attributes)
94   {
95     $ldap = $this->config->get_ldap_link();
96     $ldap->cd($this->config->current['BASE']);
97     $ldap->cat($object['dn'],$attributes);
98     $tmp  = $ldap->fetch();
100     foreach($attributes as $attrs){
101       if(isset($tmp[$attrs][0])){
102         $var = $tmp[$attrs][0];
104         /* Check if we must decode some attributes */
105         if(in_array_ics($attrs,$this->sub64coded)){
106           $var = base64_decode($var);
107         }
109         /*  check if this is a binary entry */
110         if(in_array_ics($attrs,$this->subBinary)){
111           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
112         }
114         /* Fix slashes */
115         $var = addslashes($var);
116         $object[$attrs] = $var;
117       }
118     }
119     return($object);
120   }
123   function execute()
124   {
125     /* Call parent execute */
126     plugin::execute();
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     }
144     /* Handle posts */
145     $s_action = $s_entry = "";
146     foreach($_POST as $name => $value){
148       /* Edit script posted */
149       if(preg_match("/^editscript_/",$name)){
150         $s_action = "edit";
151         $s_entry = preg_replace("/^editscript_/","",$name);
152         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
153         break;
154       }
156       /* Delete script requested */
157       if(preg_match("/^deletescript_/",$name)){
158         $s_action = "remove";
159         $s_entry = preg_replace("/^deletescript_/","",$name);
160         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
161         break;
162       }
163     }
165     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
166       $s_entry = $_GET['id'];
167       if(isset($this->SubObjects[$s_entry])){
168         $s_action = "edit";
169       }
170     }
172     if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
174       /* Get object, and load missing entry values */
175       $obj  = $this->SubObjects[$s_entry];
176       if($obj['status'] == "FreshLoaded"){
177         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
178       }
179       
180       /* Create new dialog and set acl attributes  */
181       $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
182       $this->dialog->acl = $this->acl;
183       $_SESSION['objectinfo'] = $obj['dn'];
184       $this->dialog->parent = &$this;
185       $this->is_dialog=true;
186     }
188     /* Check acls, are we allowed to delete an entry */
189     if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
190       $entry = $this->SubObjects[$s_entry];
191       $status = $entry['status'];
192       if($status == "edited" || $status == "FreshLoaded"){
193         $this->SubObjects[$s_entry]['status']= "delete";
194       }else{
195         unset($this->SubObjects[$s_entry]);
196       }
197     }
199     ///// Ende new list handling
200     if(isset($_POST['SaveSubObject'])){
201       $this->dialog->save_object();
202       $msgs = $this->dialog->check();
203       if(count($msgs)>0){
204         foreach($msgs as $msg){
205           print_red($msg);
206         }
207       }else{
209         /* Get return object */
210         $obj = $this->dialog->save();
211         if(isset($obj['remove'])){
212           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
214           /* Depending on status, set new status */
215           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
216             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
217           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
218             unset($this->SubObjects[$obj['remove']['from']]);
219           }
220           $obj['status'] = "new";
221           $this->SubObjects[$obj['remove']['to']] = $obj;
222           unset($this->SubObjects[$obj['remove']['to']]['remove']);
223         }else{
224           if($obj['status'] == "FreshLoaded"){
225             $obj['status'] = "edited";
226           }
227           $this->SubObjects[$obj['cn']]=$obj;
228         }
230         $this->is_dialog=false;
231         unset($this->dialog);
232         $this->dialog=NULL;
233       }
234     }
236     /* Sort entries */
237     $tmp = $keys = array();
239     if($this->sort_by == "name"){
240       foreach($this->SubObjects as $key => $entry){
241         $keys[$key]=$entry['cn'];
242       }
243     }elseif($this->sort_by == "priority"){
244       foreach($this->SubObjects as $key => $entry){
245         $keys[$key]=$entry['FAIpriority'];
246       }
247     }
249     natcasesort($keys);
251     if($this->sort_order == "down"){
252       $keys =array_reverse($keys);
253     }
255     foreach($keys as $key => $order_var){
256       $tmp[$key]=$this->SubObjects[$key];
257     }
258     $this->SubObjects = $tmp;
260     /* Cancel Dialog */
261     if(isset($_POST['CancelSubObject'])){
262       $this->is_dialog=false; 
263       unset($this->dialog);
264       $this->dialog=NULL;
265     }
267     /* Print dialog if $this->dialog is set */
268     if($this->dialog){
269       $this->dialog->save_object();
270       $display = $this->dialog->execute();
271       return($display);
272     }
274     /* Divlist            added 23.02.2006 
275        Containing FAIscripts 
276      */
277     $divlist = new divlist("FAIscripts");
278     $divlist -> SetEntriesPerPage(0); 
279     $plug = $_GET['plug'];
281      if($this->sort_order == "up"){
282        $dir = "<img src='images/sort_up.png' title='"._("Sort direction")."' alt='\/' border=0>";
283      }else{
284        $dir = "<img src='images/sort_down.png' title='"._("Sort direction")."' alt='/\' border=0>";
285      }
287      if($this->sort_by == "name"){
288        $sort_name = $dir;
289        $sort_prio = "";
290      }else{
291        $sort_name = "";
292        $sort_prio = $dir;
293      }
295      $divlist->SetHeader(array(  
296           array("string"=>"<a href='?plug=".$plug."&amp;sort=name'>"._("Name").$sort_name."</a>"),
297           array("string"=>"<a href='?plug=".$plug."&amp;sort=priority'>".$sort_prio._("Priority")."</a>",
298                 "attach" => "style='width:100px;'"),
299           array("string"=>_("Download"),
300                 "attach" => "style='width:100px;'"),
301           array("string"=>_("Action"),
302                 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
303     $divlist->SetHeight(300);
304     $divlist->SetWidth("100%");
306     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
307       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
308       $img_remo = "";
309     }else{
310       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
311       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
312     }
314     foreach($this->getList(true) as $key => $name){
316       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
317         $down = "";
318       }else{
319         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
320           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
321           </a>"; 
322       } 
324       $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name['name']."</a>"),
325             array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
326             array("string"=>$down , "attach" => "style='width:100px;'"),
327             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
328               "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
329     }
330     $smarty->assign("Entry_divlist",$divlist->DrawList());
332     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
333      * If we post the escaped strings they will be escaped again
334      */
335     foreach($this->attributes as $attrs){
336       if(get_magic_quotes_gpc()){
337         $smarty->assign($attrs,stripslashes($this->$attrs));
338       }else{
339         $smarty->assign($attrs,($this->$attrs));
340       }
341     }
343     foreach($this->attributes as $attr){
344       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
345     }
347     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
348     return($display);
349   }
351   /* Generate listbox friendly SubObject list
352    */
353   function getList($use_dns=false){
354     $a_return=array();
355     foreach($this->SubObjects as $obj){
356       if($obj['status'] != "delete"){
357         if($use_dns){
358           if((isset($obj['description']))&&(!empty($obj['description']))){
359             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
360           }else{
361             $a_return[$obj['cn']]['name']= $obj['cn'];
362           }
363           $a_return[$obj['cn']]['dn']= $obj['dn'];
364           $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
365         }else{
366           if((isset($obj['description']))&&(!empty($obj['description']))){
367             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
368           }else{
369             $a_return[$obj['cn']]= $obj['cn'];
370           }
371         }
372       }
373     }
374     return($a_return);
375   }
377   /* Delete me, and all my subtrees
378    */
379   function remove_from_parent()
380   {
381     $ldap = $this->config->get_ldap_link();
382     $ldap->cd ($this->dn);
383     $ldap->rmdir_recursive($this->dn);
384     show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
385     $this->handle_post_events("remove");    
386   }
389   /* Save data to object 
390    */
391   function save_object()
392   {
393     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
394       plugin::save_object();
395       foreach($this->attributes as $attrs){
396         if(isset($_POST[$attrs])){
397           $this->$attrs = $_POST[$attrs];
398         }
399       }
400     }
401     
402     /* Get sort order */
403     if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
404       if($this->sort_by == $_GET['sort']){
405         if($this->sort_order == "up"){
406           $this->sort_order = "down";
407         }elseif($this->sort_order == "down"){
408           $this->sort_order = "up";
409         }
410       }
411       $this->sort_by = $_GET['sort'];
412     }
413   }
416   /* Check supplied data */
417   function check()
418   {
419     /* Call common method to give check the hook */
420     $message= plugin::check();
422     /* If this is a new script, check if a script with this name already exists */
423     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
424      
425       /* Check if current name is already used for fai scripts in selected release */ 
426       $dn = 'cn='.$this->cn.",ou=scripts,".$this->release;
427       $ldap = $this->config->get_ldap_link(); 
428       $ldap->cat($dn);
429       if($ldap->count()){
431         $r =convert_department_dn($this->release);;
432         $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
433       }
434     }
435     return ($message);
436   }
439   /* Save to LDAP */
440   function save()
441   {
442     plugin::save();
444     $ldap = $this->config->get_ldap_link();
446     /* Copy & Paste : Ensure that FAIstate is copied too */
447     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
448       $this->attrs['FAIstate'] = $this->FAIstate;
449     }
451     $ldap->cat($this->dn,array("objectClass"));
452     if($ldap->count()!=0){
453       /* Write FAIscript to ldap*/
454       $ldap->cd($this->dn);
455       $this->cleanup();
456       $ldap->modify ($this->attrs); 
458     }else{
459       /* Write FAIscript to ldap*/
460       $ldap->cd($this->config->current['BASE']);
461       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
462       $ldap->cd($this->dn);
463       $ldap->add($this->attrs);
464     }
465     show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
467     /* Do object tagging */
468     $this->handle_object_tagging();
470     /* Prepare FAIscriptEntry to write it to ldap
471      * First sort array.
472      *  Because we must delete old entries first.
473      * After deletion, we perform add and modify 
474      */
475     $Objects = array();
477     /* We do not need to save untouched objects */
478     foreach($this->SubObjects as $name => $obj){
479       if($obj['status'] == "FreshLoaded"){
480         if($this->copy_paste_mode){
481           $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
482           $this->SubObjects[$name]['status'] = "new";
483         }else{
484           unset($this->SubObjects[$name]);
485         }
486       }
487     }
489     foreach($this->SubObjects as $name => $obj){
490       if($obj['status'] == "delete"){
491         $Objects[$name] = $obj; 
492       }
493     }
494     foreach($this->SubObjects as $name => $obj){
495       if($obj['status'] != "delete"){
496         $Objects[$name] = $obj; 
497       }
498     }
500     foreach($Objects as $name => $obj){
502       foreach($this->sub64coded as $codeIt){
503         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
504       }
506       $tmp = array();
507       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
508       foreach($attributes as $attrs){
509         if(empty($obj[$attrs])){
510           $obj[$attrs] = array();
511         }
512         if(!is_array($obj[$attrs])){
513           $tmp[$attrs] = stripslashes($obj[$attrs]);
514         }else{
515           $tmp[$attrs] = $obj[$attrs];
516         }
517       }    
519       $tmp['objectClass'] = $this->subClasses;
521       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
523       if($obj['status']=="new"){
524         $ldap->cat($sub_dn,array("objectClass"));
525         if($ldap->count()){
526           $obj['status']="edited";
527         }
528       }
530       if(empty($tmp['FAIpriority'])){
531         $tmp['FAIpriority']  ="0";
532       }
534       /* Check if gosaAdministrativeUnitTag is required as object class */
535       if($obj['status'] == "edited"){
536         $ldap->cat($sub_dn,array("objectClass"));
537         $attrs = $ldap->fetch();
538         if(isset($attrs['objectClass'])){
539           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
540             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
541           }
542         }
543       }
545       if($obj['status'] == "delete"){
546         $ldap->cd($sub_dn);
547         $ldap->rmdir_recursive($sub_dn);
548         $this->handle_post_events("remove");
549         show_ldap_error($ldap->get_error(), _("Removing FAI script failed")); 
550       }elseif($obj['status'] == "edited"){
551         $ldap->cd($sub_dn);
552         $this->cleanup();
553         $ldap->modify ($tmp); 
555         $this->handle_post_events("modify");
556       }elseif($obj['status']=="new"){
557         if($tmp['description']==array()){
558           unset($tmp['description']);
559         }
560         if($tmp['FAIscript']==array()){
561           $tmp['FAIscript']=" ";
562         }
563         $ldap->cd($this->config->current['BASE']);
564         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
565         $ldap->cd($sub_dn);
566         $ldap->add($tmp); 
567         $this->handle_post_events("add");
568         show_ldap_error($ldap->get_error(), _("Saving FAI script failed")); 
569       }
571       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
572     }
573   }
575   
576   /* return copy & paste dialog
577    */
578   function getCopyDialog()
579   {
580     /* Ask for cn */
581     $smarty = get_smarty();
582     $smarty->assign("cn" ,$this->cn);
583     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
584     $ret = array();
585     $ret['string'] = $str;
586     $ret['status'] = "";
587     return($ret);
588   }
590   /* Get posted cn */
591   function saveCopyDialog()
592   {
593     if(isset($_POST['cn'])){
594       $this->cn = $_POST['cn'];
595     }
596   }
601 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
602 ?>