Code

FAI release management reworked, only touched faiscript.
[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 ($ldap->get_attribute($object['dn'], "FAIscript",$r_array=0));
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     $divlist->setHeight(400);
227     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
228       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
229       $img_remo = "";
230     }else{
231       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
232       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
233     }
235     foreach($this->getList(true) as $key => $name){
237       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
238         $down = "";
239       }else{
240         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
241           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
242           </a>"; 
243       } 
245       $divlist->AddEntry(array( array("string"=>$name['name']),
246             array("string"=>$down , "attach" => "style='width:20px;'"),
247             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
248               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
249     }
250     $smarty->assign("Entry_divlist",$divlist->DrawList());
252     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
253      * If we post the escaped strings they will be escaped again
254      */
255     foreach($this->attributes as $attrs){
256       if(get_magic_quotes_gpc()){
257         $smarty->assign($attrs,stripslashes($this->$attrs));
258       }else{
259         $smarty->assign($attrs,($this->$attrs));
260       }
261     }
263     foreach($this->attributes as $attr){
264       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
265     }
267     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
268     return($display);
269   }
271   /* Generate listbox friendly SubObject list
272    */
273   function getList($use_dns=false){
274     $a_return=array();
275     foreach($this->SubObjects as $obj){
276       if($obj['status'] != "delete"){
277         if($use_dns){
278           if((isset($obj['description']))&&(!empty($obj['description']))){
279             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
280           }else{
281             $a_return[$obj['cn']]['name']= $obj['cn'];
282           }
283           $a_return[$obj['cn']]['dn']= $obj['dn'];
284         }else{
285           if((isset($obj['description']))&&(!empty($obj['description']))){
286             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
287           }else{
288             $a_return[$obj['cn']]= $obj['cn'];
289           }
290         }
291       }
292     }
293     return($a_return);
294   }
296   /* Delete me, and all my subtrees
297    */
298   function remove_from_parent()
299   {
300     $ldap = $this->config->get_ldap_link();
301     $ldap->cd ($this->dn);
303     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
305     $attr = prepare_ldap_fetch_to_be_saved($this->attrs);
306     if(isset($attr['FAIstate'][0])){
307       if(!preg_match("/removed$/",$attr['FAIstate'][0])){
308         $attr['FAIstate'][0] .= "|removed";
309       }   
310     }else{
311       $attr['FAIstate'][0] = "|removed";
312     }
313    
314     prepare_to_save_FAI_object($use_dn,$attr);
315     
316     foreach($this->SubObjects as $name => $obj){
317    
318       $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
320       $ldap->cat($obj['dn']);
321       $attr = prepare_ldap_fetch_to_be_saved($ldap->fetch());
322       if(isset($attr['FAIstate'][0])){
323         if(!preg_match("/removed$/",$attr['FAIstate'][0])){
324           $attr['FAIstate'][0] .= "|removed";
325         }   
326       }else{
327         $attr['FAIstate'][0] = "|removed";
328       }
329       prepare_to_save_FAI_object($use_dn,$attr);
330     }
331   }
334   /* Save data to object 
335    */
336   function save_object()
337   {
338     if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
339       plugin::save_object();
340       foreach($this->attributes as $attrs){
341         if(isset($_POST[$attrs])){
342           $this->$attrs = $_POST[$attrs];
343         }
344       }
345     }
346   }
349   /* Check supplied data */
350   function check()
351   {
352     /* Call common method to give check the hook */
353     $message= plugin::check();
355     return ($message);
356   }
359   /* Save to LDAP */
360   function save()
361   {
362     plugin::save();
364     $ldap = $this->config->get_ldap_link();
365     $ldap->cat($this->dn,array("objectClass"));
367     prepare_to_save_FAI_object($this->dn,$this->attrs);
369     if($ldap->count()!=0){
370       /* Write FAIscript to ldap*/
371       $ldap->cd($this->dn);
372       $this->cleanup();
373 //      $ldap->modify ($this->attrs); 
375     }else{
376       /* Write FAIscript to ldap*/
377       $ldap->cd($this->config->current['BASE']);
378       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
379       $ldap->cd($this->dn);
380 //      $ldap->add($this->attrs);
381     }
382     show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
384     /* Do object tagging */
385     $this->handle_object_tagging();
387     /* Prepare FAIscriptEntry to write it to ldap
388      * First sort array.
389      *  Because we must delete old entries first.
390      * After deletion, we perform add and modify 
391      */
392     $Objects = array();
393     foreach($this->SubObjects as $name => $obj){
394       if($obj['status'] == "delete"){
395         $Objects[$name] = $obj; 
396       }
397     }
398     foreach($this->SubObjects as $name => $obj){
399       if($obj['status'] != "delete"){
400         $Objects[$name] = $obj; 
401       }
402     }
404     foreach($Objects as $name => $obj){
406       foreach($this->sub64coded as $codeIt){
407         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
408       }
410       $tmp = array();
411       foreach($this->subAttributes as $attrs){
412         if(empty($obj[$attrs])){
413           $obj[$attrs] = array();
414         }
415         if(!is_array($obj[$attrs])){
416           $tmp[$attrs] = stripslashes($obj[$attrs]);
417         }else{
418           $tmp[$attrs] = $obj[$attrs];
419         }
420       }    
422       $tmp['objectClass'] = $this->subClasses;
424       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
426       if($obj['status']=="new"){
427         $ldap->cat($sub_dn,array("objectClass"));
428         if($ldap->count()){
429           $obj['status']="edited";
430         }
431       }
433       if(empty($tmp['FAIpriority'])){
434         $tmp['FAIpriority']  ="0";
435       }
437       /* Check if gosaAdministrativeUnitTag is required as object class */
438       if($obj['status'] == "edited"){
439         $ldap->cat($sub_dn,array("objectClass"));
440         $attrs = $ldap->fetch();
441         if(isset($attrs['objectClass'])){
442           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
443             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
444           }
445         }
446       }
449       if($obj['status'] == "delete"){
450   
451         $tmp['FAIstate'] = "|removed";
452         prepare_to_save_FAI_object($sub_dn,$tmp);
454         $ldap->cd($sub_dn);
455         //$ldap->rmdir_recursive($sub_dn);
456         $this->handle_post_events("remove");
457         show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/script entry with dn '%s' failed."),$this->dn));
458       }elseif($obj['status'] == "edited"){
459         $ldap->cd($sub_dn);
460         $this->cleanup();
461         prepare_to_save_FAI_object($sub_dn,$tmp);
462        // $ldap->modify ($tmp); 
464         $this->handle_post_events("modify");
465       }elseif($obj['status']=="new"){
466         if($tmp['description']==array()){
467           unset($tmp['description']);
468         }
469         if($tmp['FAIscript']==array()){
470           $tmp['FAIscript']=" ";
471         }
472         $ldap->cd($this->config->current['BASE']);
473         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
474         $ldap->cd($sub_dn);
475         prepare_to_save_FAI_object($sub_dn,$tmp);
476        // $ldap->add($tmp); 
477         $this->handle_post_events("add");
478         show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/script entry with dn '%s' failed."),$this->dn));
479       }
481       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
482     }
483   }
486 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
487 ?>