Code

Added FAIprofile selection for Workstations
[gosa.git] / plugins / admin / fai / class_faiHook.inc
1 <?php
3 class faiHook 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","FAIhook");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIhookEntry";
21   var $subClasses       = array("top","FAIclass","FAIhookEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiHookEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItask","FAIscript"); 
28   var $sub64coded       = array("FAItask","FAIscript");
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   function faiHook ($config, $dn= NULL)
38   {
39     /* Load Attributes */
40     plugin::plugin ($config, $dn);
42     /* If "dn==new" we try to create a new entry
43      * Else we must read all objects from ldap which belong to this entry.
44      */
45     if($dn != "new"){
46       $this->dn =$dn;
48       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
49        */
50       $ldap     = $this->config->get_ldap_link();
51       $ldap->cd ($this->dn);
52       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
54       while($object = $ldap->fetch()){
55         /* Set status for save management */
56   
57         foreach($this->subAttributes as $attrs){
58           if(!isset($object[$attrs][0])){
59             $this->SubObjects[$object['cn'][0]][$attrs]="";
60           }else{
61             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
62           }
63         }
64      
65         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
66         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
68         foreach($this->sub64coded as $codeIt){
69           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
70         }
72         foreach($this->subAttributes as $attrs){
73           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
74         }
75       }
76     }
77   }
79   function execute()
80   {
81     /* Fill templating stuff */
82     $smarty= get_smarty();
83     $display= "";
85     /* Add new sub object */
86     if(isset($_POST['AddSubObject'])){
87       $this->dialog= new $this->subClassName($this->config,"new");
88       $this->is_dialog=true;
89     }
91     /* Edit selected Sub Object */
92     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
93       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
94       $this->is_dialog=true;
95     }
96     
97     /* Remove Sub object */
98     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
99       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
100         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
101       }else{
102         unset($this->SubObjects[$_POST['SubObject']]);
103       }
104     }
106     /* Save Dialog */
107     if(isset($_POST['SaveSubObject'])){
109       /* Perform post check*/
110       $this->dialog->save_object();
112       /* Get messages */
113       $msgs = $this->dialog->check();
115       /* print errors */
116       if(count($msgs)>0){
117         foreach($msgs as $msg){
118           print_red($msg);
119         }
120       }else{
122         /* Get return object */
123         $obj = $this->dialog->save();
124         if(isset($obj['remove'])){
126           /* Depending on status, set new status */
127           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
128             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
129           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
130             unset($this->SubObjects[$obj['remove']['from']]);
131           }
132           $obj['status'] = "new";
133           $this->SubObjects[$obj['remove']['to']] = $obj;
134           unset($this->SubObjects[$obj['remove']['to']]['remove']);
135         }else{
136           $this->SubObjects[$obj['cn']]=$obj;
137         }
138         $this->is_dialog=false;
139         unset($this->dialog);
140         $this->dialog=NULL;
141       }
142     }
144     /* Cancel Dialog */
145     if(isset($_POST['CancelSubObject'])){
146       $this->is_dialog=false; 
147       unset($this->dialog);
148       $this->dialog=NULL;
149     }
151     /* Print dialog if $this->dialog is set */
152     if($this->dialog){
153       $this->dialog->save_object();
154       $display = $this->dialog->execute();
155       return($display);
156     }
158     $smarty->assign("SubObjects",$this->getList());
159     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
161      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
162      * If we post the escaped strings they will be escaped again
163      */
164     foreach($this->attributes as $attrs){
165       if(get_magic_quotes_gpc()){
166         $smarty->assign($attrs,stripslashes($this->$attrs));
167       }else{
168         $smarty->assign($attrs,($this->$attrs));
169       }
170     }
173     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
174     return($display);
175   }
177   /* Generate listbox friendly SubObject list
178   */
179   function getList(){
180     $a_return=array();
181     foreach($this->SubObjects as $obj){
182       if($obj['status'] != "delete"){
183         if((isset($obj['description']))&&(!empty($obj['description']))){
184           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
185         }else{
186           $a_return[$obj['cn']]= $obj['cn'];
187         }
188       }
189     }
190     return($a_return);
191   }
193   /* Delete me, and all my subtrees
194    */
195   function remove_from_parent()
196   {
197     $ldap = $this->config->get_ldap_link();
198     $ldap->cd ($this->dn);
199     $ldap->rmdir_recursive($this->dn);
200     $this->handle_post_events("remove");    
201   }
204   /* Save data to object 
205    */
206   function save_object()
207   {
208     plugin::save_object();
209     foreach($this->attributes as $attrs){
210       if(isset($_POST[$attrs])){
211         $this->$attrs = $_POST[$attrs];
212       }
213     }
214   }
217   /* Check supplied data */
218   function check()
219   {
220     $message= array();
221     return ($message);
222   }
225   /* Save to LDAP */
226   function save()
227   {
228     plugin::save();
229  
230     $ldap = $this->config->get_ldap_link();
232     $ldap->cat($this->dn);
233     if($ldap->count()!=0){     
234       /* Write FAIscript to ldap*/ 
235       $ldap->cd($this->dn);
236       $ldap->modify($this->attrs);
237     }else{
238       /* Write FAIscript to ldap*/ 
239       $ldap->cd($this->config->current['BASE']);
240       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
241       $ldap->cd($this->dn);
242       $ldap->add($this->attrs);
243     }
244     show_ldap_error($ldap->get_error()); 
245  
246     /* Prepare FAIscriptEntry to write it to ldap
247      * First sort array.
248      *  Because we must delete old entries first.
249      * After deletion, we perform add and modify 
250      */
251     $Objects = array();
252     foreach($this->SubObjects as $name => $obj){
253       if($obj['status'] == "delete"){
254         $Objects[$name] = $obj; 
255       }
256     }
257     foreach($this->SubObjects as $name => $obj){
258       if($obj['status'] != "delete"){
259         $Objects[$name] = $obj; 
260       }
261     }
263     foreach($Objects as $name => $obj){
265       foreach($this->sub64coded as $codeIt){
266         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
267       }
269       $tmp = array();
270       foreach($this->subAttributes as $attrs){
271         if(empty($obj[$attrs])){
272           $obj[$attrs] = array();
273         }
274         if(!is_array($obj[$attrs])){
275           $tmp[$attrs] = stripslashes($obj[$attrs]);
276         }else{
277           $tmp[$attrs] = $obj[$attrs];
278         }
279       }    
281       $tmp['objectClass'] = $this->subClasses;
283       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
285       if($obj['status']=="new"){
286         $ldap->cat($sub_dn);
287         if($ldap->count()){
288           $obj['status']="modify";
289         }
290       }
291  
292       if($obj['status'] == "delete"){
293         $ldap->cd($sub_dn);
294         $ldap->rmdir_recursive($sub_dn);
295         $this->handle_post_events("remove");
296       }elseif($obj['status'] == "edited"){
297         $ldap->cd($sub_dn);
298         $ldap->modify($tmp);
299         $this->handle_post_events("modify");
300       }elseif($obj['status']=="new"){
301         if($tmp['description']==array()){
302           unset($tmp['description']);
303         }
304         $ldap->cd($this->config->current['BASE']);
305         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
306         $ldap->cd($sub_dn);
307         $ldap->add($tmp); 
308         $this->handle_post_events("add");
309       }
310       show_ldap_error($ldap->get_error()); 
311     }
312   }
315 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
316 ?>