1 <?php
3 class faiHook 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","FAIhook");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "FAIhookEntry";
16 var $subClasses = array("top","FAIclass","FAIhookEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "faiHookEntry";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description","FAItask");
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 $base = "";
36 var $release = "";
37 var $copy_paste_mode = false;
38 var $cut_paste_mode = false;
40 var $CopyPasteVars = array("SubObjects");
42 function faiHook ($config, $dn= NULL)
43 {
44 /* Load Attributes */
45 plugin::plugin ($config, $dn);
47 $this->acl ="#all#";
49 /* If "dn==new" we try to create a new entry
50 * Else we must read all objects from ldap which belong to this entry.
51 */
52 if($dn != "new"){
53 $this->dn =$dn;
55 /* Set acls
56 */
57 $ui = get_userinfo();
58 $acl = get_permissions ($this->dn, $ui->subtreeACL);
59 $acli = get_module_permission($acl, "FAIclass", $this->dn);
60 $this->acl=$acli;
62 /* Get FAIstate
63 */
64 if(isset($this->attrs['FAIstate'][0])){
65 $this->FAIstate = $this->attrs['FAIstate'][0];
66 }
68 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
69 */
70 $ldap = $this->config->get_ldap_link();
71 $ldap->cd ($this->dn);
72 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
74 $data = array();
75 while($object = $ldap->fetch()){
76 $data[] = $object;
77 }
78 foreach($data as $object){
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;
86 }
87 }
88 }
91 /* Reload some attributes */
92 function get_object_attributes($object,$attributes)
93 {
94 $ldap = $this->config->get_ldap_link();
95 $ldap->cd($this->config->current['BASE']);
96 $ldap->cat($object['dn'],$attributes);
97 $tmp = $ldap->fetch();
99 foreach($attributes as $attrs){
100 if(isset($tmp[$attrs][0])){
101 $var = $tmp[$attrs][0];
103 /* Check if we must decode some attributes */
104 if(in_array_ics($attrs,$this->sub64coded)){
105 $var = base64_decode($var);
106 }
108 /* check if this is a binary entry */
109 if(in_array_ics($attrs,$this->subBinary)){
110 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
111 }
113 /* Fix slashes */
114 $var = addslashes($var);
115 $object[$attrs] = $var;
116 }
117 }
118 return($object);
119 }
122 function getUsedFAItask($cn)
123 {
124 $ret = array();
125 foreach($this->SubObjects as $name => $class){
126 if($class['cn'] == $cn){
127 continue;
128 }
129 if($class['status'] != "delete"){
130 $ret[$class['FAItask']] = $class['FAItask'];
131 }
132 }
133 return($ret);
134 }
137 function execute()
138 {
139 /* Call parent execute */
140 plugin::execute();
142 /* Fill templating stuff */
143 $smarty= get_smarty();
144 $display= "";
146 /* New Listhandling
147 */
148 $once = true;
149 foreach($_POST as $name => $value){
150 if(preg_match("/^editscript_/",$name)&&($once)){
151 $once = false;
152 $entry = preg_replace("/^editscript_/","",$name);
153 $entry = base64_decode(preg_replace("/_.*/","",$entry));
155 $obj = $this->SubObjects[$entry];
156 if($obj['status'] == "FreshLoaded"){
157 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
158 }
160 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
161 $this->dialog->acl = $this->acl;
162 $_SESSION['objectinfo'] = $obj['dn'];
163 $this->dialog->parent = &$this;
164 $this->is_dialog=true;
165 }
166 if(preg_match("/^deletescript_/",$name)&&($once)){
167 $once = false;
168 $entry = preg_replace("/^deletescript_/","",$name);
169 $entry = base64_decode(preg_replace("/_.*/","",$entry));
171 $status = $this->SubObjects[$entry]['status'];
172 if($status == "edited" || $status == "FreshLoaded"){
173 $this->SubObjects[$entry]['status']= "delete";
174 }else{
175 unset($this->SubObjects[$entry]);
176 }
177 }
178 }
180 /* Edit entries via GET */
181 if(isset($_GET['act']) && isset($_GET['id'])){
182 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
183 $obj = $this->SubObjects[$_GET['id']];
184 if($obj['status'] == "FreshLoaded"){
185 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
186 }
187 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
188 $this->dialog->acl = $this->acl;
189 $_SESSION['objectinfo'] = $obj['dn'];
190 $this->dialog->parent = &$this;
191 $this->is_dialog=true;
192 }
193 }
195 ///// Ende new list handling
197 /* Add new sub object */
198 if(isset($_POST['AddSubObject'])){
199 $this->dialog= new $this->subClassName($this->config,"new");
200 $this->dialog->acl = $this->acl;
201 $this->dialog->parent = &$this;
202 $this->is_dialog=true;
203 }
205 if($this->dn != "new"){
206 $_SESSION['objectinfo']= $this->dn;
207 }
209 /* Save Dialog */
210 if(isset($_POST['SaveSubObject'])){
212 /* Perform post check*/
213 $this->dialog->save_object();
215 /* Get messages */
216 $msgs = $this->dialog->check();
218 /* print errors */
219 if(count($msgs)>0){
220 foreach($msgs as $msg){
221 print_red($msg);
222 }
223 }else{
225 /* Get return object */
226 $obj = $this->dialog->save();
227 if(isset($obj['remove'])){
228 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
230 /* Depending on status, set new status */
231 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
232 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
233 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
234 unset($this->SubObjects[$obj['remove']['from']]);
235 }
236 $obj['status'] = "new";
237 $this->SubObjects[$obj['remove']['to']] = $obj;
238 unset($this->SubObjects[$obj['remove']['to']]['remove']);
239 }else{
240 if($obj['status'] == "FreshLoaded"){
241 $obj['status'] = "edited";
242 }
243 $this->SubObjects[$obj['cn']]=$obj;
244 }
246 $this->is_dialog=false;
247 unset($this->dialog);
248 $this->dialog=NULL;
249 }
250 }
252 /* Sort entries */
253 $tmp = $keys = array();
254 foreach($this->SubObjects as $key => $entry){
255 $keys[$key]=$key;
256 }
257 natcasesort($keys);
258 foreach($keys as $key){
259 $tmp[$key]=$this->SubObjects[$key];
260 }
261 $this->SubObjects = $tmp;
263 /* Cancel Dialog */
264 if(isset($_POST['CancelSubObject'])){
265 $this->is_dialog=false;
266 unset($this->dialog);
267 $this->dialog=NULL;
268 }
270 /* Print dialog if $this->dialog is set */
271 if($this->dialog){
272 $this->dialog->save_object();
273 $display = $this->dialog->execute();
274 return($display);
275 }
279 /* Divlist added 28.02.2006
280 Containing FAIscripts
281 */
283 $divlist = new divSelectBox("FAIhooks");
284 $divlist->setHeight(400);
285 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
286 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
287 $img_remo = "";
288 }else{
289 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
290 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
291 }
293 foreach($this->getList(true) as $key => $name){
295 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
296 $down = "";
297 }else{
298 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
299 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
300 </a>";
301 }
303 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name['name']."</a>";
305 $divlist->AddEntry(array( array("string"=>$edit_link),
306 array("string"=>$down , "attach" => "style='width:20px;'"),
307 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
308 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
309 }
310 $smarty->assign("Entry_divlist",$divlist->DrawList());
311 /* Divlist creation complete
312 */
314 $smarty->assign("SubObjects",$this->getList());
316 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
317 * If we post the escaped strings they will be escaped again
318 */
319 foreach($this->attributes as $attrs){
320 if(get_magic_quotes_gpc()){
321 $smarty->assign($attrs,stripslashes($this->$attrs));
322 }else{
323 $smarty->assign($attrs,($this->$attrs));
324 }
325 }
327 foreach($this->attributes as $attr){
328 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
329 }
331 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
332 return($display);
333 }
335 /* Generate listbox friendly SubObject list
336 */
337 function getList($use_dns=false){
338 $a_return=array();
339 foreach($this->SubObjects as $obj){
340 if($obj['status'] != "delete"){
341 if($use_dns){
342 if((isset($obj['description']))&&(!empty($obj['description']))){
343 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
344 }else{
345 $a_return[$obj['cn']]['name']= $obj['cn'];
346 }
347 $a_return[$obj['cn']]['dn']= $obj['dn'];
348 }else{
349 if((isset($obj['description']))&&(!empty($obj['description']))){
350 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
351 }else{
352 $a_return[$obj['cn']]= $obj['cn'];
353 }
354 }
355 }
356 }
357 return($a_return);
358 }
361 /* Delete me, and all my subtrees
362 */
363 function remove_from_parent()
364 {
365 $ldap = $this->config->get_ldap_link();
366 $ldap->cd ($this->dn);
367 $ldap->rmdir_recursive($this->dn);
368 show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed"));
369 $this->handle_post_events("remove");
370 }
373 /* Save data to object
374 */
375 function save_object()
376 {
377 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
378 plugin::save_object();
379 foreach($this->attributes as $attrs){
380 if(isset($_POST[$attrs])){
381 $this->$attrs = $_POST[$attrs];
382 }
383 }
384 }
385 }
388 /* Check supplied data */
389 function check()
390 {
391 /* Call common method to give check the hook */
392 $message= plugin::check();
394 /* If this is a new script, check if a script with this name already exists */
395 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
397 /* Check if current name is already used for fai scripts in selected release */
398 $dn = 'cn='.$this->cn.",ou=hooks,".$this->release;
399 $ldap = $this->config->get_ldap_link();
400 $ldap->cat($dn);
401 if($ldap->count()){
403 $r =convert_department_dn($this->release);;
404 $message[] = sprintf(_("Can't insert a fai hook named '%s' in '%s' there is already a hook with the given name."),$this->cn,$r);
405 }
406 }
407 return ($message);
408 }
411 /* Save to LDAP */
412 function save()
413 {
414 plugin::save();
416 $ldap = $this->config->get_ldap_link();
418 /* Copy & Paste : Ensure that FAIstate is copied too */
419 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
420 $this->attrs['FAIstate'] = $this->FAIstate;
421 }
423 $ldap->cat($this->dn,array("objectClass"));
424 if($ldap->count()!=0){
425 /* Write FAIscript to ldap*/
426 $ldap->cd($this->dn);
427 $this->cleanup();
428 $ldap->modify ($this->attrs);
430 }else{
431 /* Write FAIscript to ldap*/
432 $ldap->cd($this->config->current['BASE']);
433 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
434 $ldap->cd($this->dn);
435 $ldap->add($this->attrs);
436 }
437 show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed"));
439 $ldap->cd($this->dn);
441 /* Prepare FAIscriptEntry to write it to ldap
442 * First sort array.
443 * Because we must delete old entries first.
444 * After deletion, we perform add and modify
445 */
446 $Objects = array();
449 /* We do not need to save untouched objects */
450 foreach($this->SubObjects as $name => $obj){
451 if($obj['status'] == "FreshLoaded"){
452 if($this->copy_paste_mode){
453 $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
454 $this->SubObjects[$name]['status'] = "new";
455 }else{
456 unset($this->SubObjects[$name]);
457 }
458 }
459 }
461 /* Add objects that must be removed first.*/
462 foreach($this->SubObjects as $name => $obj){
463 if($obj['status'] == "delete"){
464 $Objects[$name] = $obj;
465 }
466 }
468 /* Add objects to add/modify */
469 foreach($this->SubObjects as $name => $obj){
470 if($obj['status'] != "delete"){
471 $Objects[$name] = $obj;
472 }
473 }
475 /* Walk through list of objects */
476 foreach($Objects as $name => $obj){
478 /* Encode attribtues if required */
479 foreach($this->sub64coded as $codeIt){
480 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
481 }
483 /* Create ldap entry */
484 $tmp = array();
485 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
486 foreach($attributes as $attrs){
488 if(empty($obj[$attrs])){
489 $obj[$attrs] = array();
490 }
491 if(!is_array($obj[$attrs])){
492 $tmp[$attrs] = stripslashes($obj[$attrs]);
493 }else{
494 $tmp[$attrs] = $obj[$attrs];
495 }
496 }
498 $tmp['objectClass'] = $this->subClasses;
500 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
502 if($obj['status']=="new"){
503 $ldap->cat($sub_dn,array("objectClass"));
504 if($ldap->count()){
505 $obj['status']="edited";
506 }
507 }
509 /* Check if gosaAdministrativeUnitTag is required as object class */
510 if($obj['status'] == "edited"){
511 $ldap->cat($sub_dn,array("objectClass"));
512 $attrs = $ldap->fetch();
513 if(isset($attrs['objectClass'])){
514 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
515 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
516 }
517 }
518 }
520 /* Tag object */
521 $this->tag_attrs(&$tmp, $sub_dn, $this->gosaUnitTag);
523 if($obj['status'] == "delete"){
524 $ldap->cd($sub_dn);
525 $ldap->rmdir_recursive($sub_dn);
526 $this->handle_post_events("remove");
527 show_ldap_error($ldap->get_error(), _("Removing FAI hook failed"));
528 }elseif($obj['status'] == "edited"){
529 $ldap->cd($sub_dn);
530 $this->cleanup();
531 $ldap->modify ($tmp);
532 $this->handle_post_events("modify");
533 show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
534 }elseif($obj['status']=="new"){
535 if($tmp['description']==array()){
536 unset($tmp['description']);
537 }
538 $ldap->cd($this->config->current['BASE']);
539 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
540 $ldap->cd($sub_dn);
541 $ldap->add($tmp);
542 $this->handle_post_events("add");
543 show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
544 }
545 }
546 }
548 /* return copy & paste dialog
549 */
550 function getCopyDialog()
551 {
552 /* Ask for cn */
553 $smarty = get_smarty();
554 $smarty->assign("cn" ,$this->cn);
555 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
556 $ret = array();
557 $ret['string'] = $str;
558 $ret['status'] = "";
559 return($ret);
560 }
562 /* Get posted cn */
563 function saveCopyDialog()
564 {
565 if(isset($_POST['cn'])){
566 $this->cn = $_POST['cn'];
567 }
568 }
569 }
571 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
572 ?>