Code

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@1522 594d385d-05f5-0310...
[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       }
73     }
74   }
76   function execute()
77   {
78     /* Fill templating stuff */
79     $smarty= get_smarty();
80     $display= "";
82     /* Add new sub object */
83     if(isset($_POST['AddSubObject'])){
84       $this->dialog= new $this->subClassName($this->config,"new");
85       $this->is_dialog=true;
86     }
88     /* Edit selected Sub Object */
89     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
90       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
91       $this->is_dialog=true;
92     }
93     
94     /* Remove Sub object */
95     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
96       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
97         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
98       }else{
99         unset($this->SubObjects[$_POST['SubObject']]);
100       }
101     }
103     /* Save Dialog */
104     if(isset($_POST['SaveSubObject'])){
106       /* Perform post check*/
107       $this->dialog->save_object();
109       /* Get messages */
110       $msgs = $this->dialog->check();
112       /* print errors */
113       if(count($msgs)>0){
114         foreach($msgs as $msg){
115           print_red($msg);
116         }
117       }else{
119         /* Get return object */
120         $obj = $this->dialog->save();
121         if(isset($obj['remove'])){
123           /* Depending on status, set new status */
124           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
125             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
126           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
127             unset($this->SubObjects[$obj['remove']['from']]);
128           }
129           $obj['status'] = "new";
130           $this->SubObjects[$obj['remove']['to']] = $obj;
131           unset($this->SubObjects[$obj['remove']['to']]['remove']);
132         }else{
133           $this->SubObjects[$obj['cn']]=$obj;
134         }
135         $this->is_dialog=false;
136         unset($this->dialog);
137         $this->dialog=NULL;
138       }
139     }
141     /* Cancel Dialog */
142     if(isset($_POST['CancelSubObject'])){
143       $this->is_dialog=false; 
144       unset($this->dialog);
145       $this->dialog=NULL;
146     }
148     /* Print dialog if $this->dialog is set */
149     if($this->dialog){
150       $this->dialog->save_object();
151       $display = $this->dialog->execute();
152       return($display);
153     }
155     $smarty->assign("SubObjects",$this->getList());
156     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
158      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
159      * If we post the escaped strings they will be escaped again
160      */
161     foreach($this->attributes as $attrs){
162       if(get_magic_quotes_gpc()){
163         $smarty->assign($attrs,stripslashes($this->$attrs));
164       }else{
165         $smarty->assign($attrs,($this->$attrs));
166       }
167     }
170     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
171     return($display);
172   }
174   /* Generate listbox friendly SubObject list
175   */
176   function getList(){
177     $a_return=array();
178     foreach($this->SubObjects as $obj){
179       if($obj['status'] != "delete"){
180         if((isset($obj['description']))&&(!empty($obj['description']))){
181           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
182         }else{
183           $a_return[$obj['cn']]= $obj['cn'];
184         }
185       }
186     }
187     return($a_return);
188   }
190   /* Delete me, and all my subtrees
191    */
192   function remove_from_parent()
193   {
194     $ldap = $this->config->get_ldap_link();
195     $ldap->cd ($this->dn);
196     $ldap->rmdir_recursive($this->dn);
197     $this->handle_post_events("remove");    
198   }
201   /* Save data to object 
202    */
203   function save_object()
204   {
205     plugin::save_object();
206     foreach($this->attributes as $attrs){
207       if(isset($_POST[$attrs])){
208         $this->$attrs = $_POST[$attrs];
209       }
210     }
211   }
214   /* Check supplied data */
215   function check()
216   {
217     $message= array();
218     return ($message);
219   }
222   /* Save to LDAP */
223   function save()
224   {
225     plugin::save();
226  
227     $ldap = $this->config->get_ldap_link();
229     $ldap->cat($this->dn);
230     if($ldap->count()!=0){     
231       /* Write FAIscript to ldap*/ 
232       $ldap->cd($this->dn);
233       $ldap->modify($this->attrs);
234     }else{
235       /* Write FAIscript to ldap*/ 
236       $ldap->cd($this->config->current['BASE']);
237       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
238       $ldap->cd($this->dn);
239       $ldap->add($this->attrs);
240     }
241     show_ldap_error($ldap->get_error()); 
242  
243     /* Prepare FAIscriptEntry to write it to ldap
244      * First sort array.
245      *  Because we must delete old entries first.
246      * After deletion, we perform add and modify 
247      */
248     $Objects = array();
249     foreach($this->SubObjects as $name => $obj){
250       if($obj['status'] == "delete"){
251         $Objects[$name] = $obj; 
252       }
253     }
254     foreach($this->SubObjects as $name => $obj){
255       if($obj['status'] != "delete"){
256         $Objects[$name] = $obj; 
257       }
258     }
260     foreach($Objects as $name => $obj){
262       foreach($this->sub64coded as $codeIt){
263         $obj[$codeIt]=base64_encode($obj[$codeIt]);
264       }
266       $tmp = array();
267       foreach($this->subAttributes as $attrs){
268         if(empty($obj[$attrs])){
269           $obj[$attrs] = array();
270         }
271         $tmp[$attrs] = $obj[$attrs];
272       }    
274       $tmp['objectClass'] = $this->subClasses;
276       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
278       if($obj['status']=="new"){
279         $ldap->cat($sub_dn);
280         if($ldap->count()){
281           $obj['status']="modify";
282         }
283       }
284  
285       if($obj['status'] == "delete"){
286         $ldap->cd($sub_dn);
287         $ldap->rmdir_recursive($sub_dn);
288         $this->handle_post_events("remove");
289       }elseif($obj['status'] == "edited"){
290         $ldap->cd($sub_dn);
291         $ldap->modify($tmp);
292         $this->handle_post_events("modify");
293       }elseif($obj['status']=="new"){
294         if($tmp['description']==array()){
295           unset($tmp['description']);
296         }
297         $ldap->cd($this->config->current['BASE']);
298         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
299         $ldap->cd($sub_dn);
300         $ldap->add($tmp); 
301         $this->handle_post_events("add");
302       }
303       show_ldap_error($ldap->get_error()); 
304     }
305   }
308 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
309 ?>