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();
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 */
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 }
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 $this->SubObjects[$object['cn'][0]]['FAIscript'] = addslashes($this->readBinary("FAIscript",$object['dn']));
76 }
77 }
78 }
80 function getUsedFAItask($cn)
81 {
82 $ret = array();
83 foreach($this->SubObjects as $name => $class){
84 if($class['cn'] == $cn){
85 continue;
86 }
87 if($class['status'] != "delete"){
88 $ret[$class['FAItask']] = $class['FAItask'];
89 }
90 }
91 return($ret);
92 }
94 function execute()
95 {
96 /* Call parent execute */
97 plugin::execute();
99 /* Fill templating stuff */
100 $smarty= get_smarty();
101 $display= "";
103 /* Add new sub object */
104 if(isset($_POST['AddSubObject'])){
105 $this->dialog= new $this->subClassName($this->config,"new");
106 $this->dialog->parent = &$this;
107 $this->is_dialog=true;
108 }
110 if($this->dn != "new"){
111 $_SESSION['objectinfo']= $this->dn;
112 }
113 /* Edit selected Sub Object */
114 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
115 $hook = $_POST['SubObject'][0];
117 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$hook]);
118 $this->dialog->parent = &$this;
119 $_SESSION['objectinfo'] = $this->SubObjects[$hook]['dn'];
120 $this->is_dialog=true;
121 }
123 /* Remove Sub object */
124 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
125 foreach($_POST['SubObject'] as $hook){
126 if($this->SubObjects[$hook]['status'] == "edited"){
127 $this->SubObjects[$hook]['status']= "delete";
128 }else{
129 unset($this->SubObjects[$hook]);
130 }
131 }
132 }
134 /* Save Dialog */
135 if(isset($_POST['SaveSubObject'])){
137 /* Perform post check*/
138 $this->dialog->save_object();
140 /* Get messages */
141 $msgs = $this->dialog->check();
143 /* print errors */
144 if(count($msgs)>0){
145 foreach($msgs as $msg){
146 print_red($msg);
147 }
148 }else{
150 /* Get return object */
151 $obj = $this->dialog->save();
152 if(isset($obj['remove'])){
154 /* Depending on status, set new status */
155 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
156 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
157 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
158 unset($this->SubObjects[$obj['remove']['from']]);
159 }
160 $obj['status'] = "new";
161 $this->SubObjects[$obj['remove']['to']] = $obj;
162 unset($this->SubObjects[$obj['remove']['to']]['remove']);
163 }else{
164 $this->SubObjects[$obj['cn']]=$obj;
165 }
166 $this->is_dialog=false;
167 unset($this->dialog);
168 $this->dialog=NULL;
169 }
170 }
172 /* Sort entries */
173 $tmp = $keys = array();
174 foreach($this->SubObjects as $key => $entry){
175 $keys[$key]=$key;
176 }
177 natcasesort($keys);
178 foreach($keys as $key){
179 $tmp[$key]=$this->SubObjects[$key];
180 }
181 $this->SubObjects = $tmp;
183 /* Cancel Dialog */
184 if(isset($_POST['CancelSubObject'])){
185 $this->is_dialog=false;
186 unset($this->dialog);
187 $this->dialog=NULL;
188 }
190 /* Print dialog if $this->dialog is set */
191 if($this->dialog){
192 $this->dialog->save_object();
193 $display = $this->dialog->execute();
194 return($display);
195 }
197 $smarty->assign("SubObjects",$this->getList());
198 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
200 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
201 * If we post the escaped strings they will be escaped again
202 */
203 foreach($this->attributes as $attrs){
204 if(get_magic_quotes_gpc()){
205 $smarty->assign($attrs,stripslashes($this->$attrs));
206 }else{
207 $smarty->assign($attrs,($this->$attrs));
208 }
209 }
211 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
212 return($display);
213 }
215 /* Generate listbox friendly SubObject list
216 */
217 function getList(){
218 $a_return=array();
219 foreach($this->SubObjects as $obj){
220 if($obj['status'] != "delete"){
221 if((isset($obj['description']))&&(!empty($obj['description']))){
222 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
223 }else{
224 $a_return[$obj['cn']]= $obj['cn'];
225 }
226 }
227 }
228 return($a_return);
229 }
231 /* Delete me, and all my subtrees
232 */
233 function remove_from_parent()
234 {
235 $ldap = $this->config->get_ldap_link();
236 $ldap->cd ($this->dn);
237 $ldap->rmdir_recursive($this->dn);
238 $this->handle_post_events("remove");
239 }
242 /* Save data to object
243 */
244 function save_object()
245 {
246 if(isset($_POST['FAIhook_posted'])){
247 plugin::save_object();
248 foreach($this->attributes as $attrs){
249 if(isset($_POST[$attrs])){
250 $this->$attrs = $_POST[$attrs];
251 }
252 }
253 }
254 }
257 /* Check supplied data */
258 function check()
259 {
260 $message= array();
261 return ($message);
262 }
265 /* Save to LDAP */
266 function save()
267 {
268 plugin::save();
270 $ldap = $this->config->get_ldap_link();
272 $ldap->cat($this->dn);
273 if($ldap->count()!=0){
274 /* Write FAIscript to ldap*/
275 $ldap->cd($this->dn);
276 $ldap->modify($this->attrs);
277 }else{
278 /* Write FAIscript to ldap*/
279 $ldap->cd($this->config->current['BASE']);
280 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
281 $ldap->cd($this->dn);
282 $ldap->add($this->attrs);
283 }
284 show_ldap_error($ldap->get_error());
286 /* Prepare FAIscriptEntry to write it to ldap
287 * First sort array.
288 * Because we must delete old entries first.
289 * After deletion, we perform add and modify
290 */
291 $Objects = array();
292 foreach($this->SubObjects as $name => $obj){
293 if($obj['status'] == "delete"){
294 $Objects[$name] = $obj;
295 }
296 }
297 foreach($this->SubObjects as $name => $obj){
298 if($obj['status'] != "delete"){
299 $Objects[$name] = $obj;
300 }
301 }
303 foreach($Objects as $name => $obj){
305 foreach($this->sub64coded as $codeIt){
306 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
307 }
309 $tmp = array();
310 foreach($this->subAttributes as $attrs){
311 if(empty($obj[$attrs])){
312 $obj[$attrs] = array();
313 }
314 if(!is_array($obj[$attrs])){
315 $tmp[$attrs] = stripslashes($obj[$attrs]);
316 }else{
317 $tmp[$attrs] = $obj[$attrs];
318 }
319 }
321 $tmp['objectClass'] = $this->subClasses;
323 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
325 if($obj['status']=="new"){
326 $ldap->cat($sub_dn);
327 if($ldap->count()){
328 $obj['status']="modify";
329 }
330 }
332 if($obj['status'] == "delete"){
333 $ldap->cd($sub_dn);
334 $ldap->rmdir_recursive($sub_dn);
335 $this->handle_post_events("remove");
336 }elseif($obj['status'] == "edited"){
337 $ldap->cd($sub_dn);
338 $ldap->modify($tmp);
339 $this->handle_post_events("modify");
340 }elseif($obj['status']=="new"){
341 if($tmp['description']==array()){
342 unset($tmp['description']);
343 }
344 $ldap->cd($this->config->current['BASE']);
345 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
346 $ldap->cd($sub_dn);
347 $ldap->add($tmp);
348 $this->handle_post_events("add");
349 }
350 show_ldap_error($ldap->get_error());
351 }
352 }
354 function readBinary($attr,$dn){
355 $Data ="";
356 $ds= ldap_connect($this->config->current['SERVER']);
357 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
358 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
359 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
360 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
361 }
363 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
364 ldap_start_tls($ds);
365 }
367 $r = ldap_bind($ds);
368 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
370 if ($sr) {
371 $ei=ldap_first_entry($ds, $sr);
372 if ($ei) {
373 if ($info = ldap_get_values_len($ds, $ei, $attr)){
374 $Data= $info[0];
375 }
376 }
377 }
379 /* close conncetion */
380 ldap_unbind($ds);
381 return($Data);
382 }
385 }
387 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
388 ?>