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 while($object = $ldap->fetch()){
76 /* Set status for save management */
77 $objects = array();
78 $objects['status'] = "FreshLoaded";
79 $objects['dn'] = $object['dn'];
80 $objects = $this->get_object_attributes($objects,$this->subAttributes);
81 $this->SubObjects[$objects['cn']] = $objects;
82 }
83 }
84 }
87 /* Reload some attributes */
88 function get_object_attributes($object,$attributes)
89 {
90 $ldap = $this->config->get_ldap_link();
91 $ldap->cd($this->config->current['BASE']);
92 $ldap->cat($object['dn'],$attributes);
93 $tmp = $ldap->fetch();
95 foreach($attributes as $attrs){
96 if(isset($tmp[$attrs][0])){
97 $var = $tmp[$attrs][0];
99 /* Check if we must decode some attributes */
100 if(in_array_ics($attrs,$this->sub64coded)){
101 $var = base64_decode($var);
102 }
104 /* check if this is a binary entry */
105 if(in_array_ics($attrs,$this->subBinary)){
106 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
107 }
109 /* Fix slashes */
110 $var = addslashes($var);
111 $object[$attrs] = $var;
112 }
113 }
114 return($object);
115 }
118 function getUsedFAItask($cn)
119 {
120 $ret = array();
121 foreach($this->SubObjects as $name => $class){
122 if($class['cn'] == $cn){
123 continue;
124 }
125 if($class['status'] != "delete"){
126 $ret[$class['FAItask']] = $class['FAItask'];
127 }
128 }
129 return($ret);
130 }
133 function execute()
134 {
135 /* Call parent execute */
136 plugin::execute();
138 /* Fill templating stuff */
139 $smarty= get_smarty();
140 $display= "";
142 /* New Listhandling
143 */
144 $once = true;
145 foreach($_POST as $name => $value){
146 if(preg_match("/^editscript_/",$name)&&($once)){
147 $once = false;
148 $entry = preg_replace("/^editscript_/","",$name);
149 $entry = base64_decode(preg_replace("/_.*/","",$entry));
151 $obj = $this->SubObjects[$entry];
152 if($obj['status'] == "FreshLoaded"){
153 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
154 }
156 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
157 $this->dialog->acl = $this->acl;
158 $_SESSION['objectinfo'] = $obj['dn'];
159 $this->dialog->parent = &$this;
160 $this->is_dialog=true;
161 }
162 if(preg_match("/^deletescript_/",$name)&&($once)){
163 $once = false;
164 $entry = preg_replace("/^deletescript_/","",$name);
165 $entry = base64_decode(preg_replace("/_.*/","",$entry));
167 $status = $this->SubObjects[$entry]['status'];
168 if($status == "edited" || $status == "FreshLoaded"){
169 $this->SubObjects[$entry]['status']= "delete";
170 }else{
171 unset($this->SubObjects[$entry]);
172 }
173 }
174 }
176 /* Edit entries via GET */
177 if(isset($_GET['act']) && isset($_GET['id'])){
178 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
179 $obj = $this->SubObjects[$_GET['id']];
180 if($obj['status'] == "FreshLoaded"){
181 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
182 }
183 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
184 $this->dialog->acl = $this->acl;
185 $_SESSION['objectinfo'] = $obj['dn'];
186 $this->dialog->parent = &$this;
187 $this->is_dialog=true;
188 }
189 }
191 ///// Ende new list handling
193 /* Add new sub object */
194 if(isset($_POST['AddSubObject'])){
195 $this->dialog= new $this->subClassName($this->config,"new");
196 $this->dialog->acl = $this->acl;
197 $this->dialog->parent = &$this;
198 $this->is_dialog=true;
199 }
201 if($this->dn != "new"){
202 $_SESSION['objectinfo']= $this->dn;
203 }
205 /* Save Dialog */
206 if(isset($_POST['SaveSubObject'])){
208 /* Perform post check*/
209 $this->dialog->save_object();
211 /* Get messages */
212 $msgs = $this->dialog->check();
214 /* print errors */
215 if(count($msgs)>0){
216 foreach($msgs as $msg){
217 print_red($msg);
218 }
219 }else{
221 /* Get return object */
222 $obj = $this->dialog->save();
223 if(isset($obj['remove'])){
224 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
226 /* Depending on status, set new status */
227 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
228 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
229 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
230 unset($this->SubObjects[$obj['remove']['from']]);
231 }
232 $obj['status'] = "new";
233 $this->SubObjects[$obj['remove']['to']] = $obj;
234 unset($this->SubObjects[$obj['remove']['to']]['remove']);
235 }else{
236 if($obj['status'] == "FreshLoaded"){
237 $obj['status'] = "edited";
238 }
239 $this->SubObjects[$obj['cn']]=$obj;
240 }
242 $this->is_dialog=false;
243 unset($this->dialog);
244 $this->dialog=NULL;
245 }
246 }
248 /* Sort entries */
249 $tmp = $keys = array();
250 foreach($this->SubObjects as $key => $entry){
251 $keys[$key]=$key;
252 }
253 natcasesort($keys);
254 foreach($keys as $key){
255 $tmp[$key]=$this->SubObjects[$key];
256 }
257 $this->SubObjects = $tmp;
259 /* Cancel Dialog */
260 if(isset($_POST['CancelSubObject'])){
261 $this->is_dialog=false;
262 unset($this->dialog);
263 $this->dialog=NULL;
264 }
266 /* Print dialog if $this->dialog is set */
267 if($this->dialog){
268 $this->dialog->save_object();
269 $display = $this->dialog->execute();
270 return($display);
271 }
275 /* Divlist added 28.02.2006
276 Containing FAIscripts
277 */
279 $divlist = new divSelectBox("FAIhooks");
280 $divlist->setHeight(400);
281 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
282 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
283 $img_remo = "";
284 }else{
285 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
286 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
287 }
289 foreach($this->getList(true) as $key => $name){
291 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
292 $down = "";
293 }else{
294 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
295 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
296 </a>";
297 }
299 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name['name']."</a>";
301 $divlist->AddEntry(array( array("string"=>$edit_link),
302 array("string"=>$down , "attach" => "style='width:20px;'"),
303 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
304 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
305 }
306 $smarty->assign("Entry_divlist",$divlist->DrawList());
307 /* Divlist creation complete
308 */
310 $smarty->assign("SubObjects",$this->getList());
312 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
313 * If we post the escaped strings they will be escaped again
314 */
315 foreach($this->attributes as $attrs){
316 if(get_magic_quotes_gpc()){
317 $smarty->assign($attrs,stripslashes($this->$attrs));
318 }else{
319 $smarty->assign($attrs,($this->$attrs));
320 }
321 }
323 foreach($this->attributes as $attr){
324 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
325 }
327 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
328 return($display);
329 }
331 /* Generate listbox friendly SubObject list
332 */
333 function getList($use_dns=false){
334 $a_return=array();
335 foreach($this->SubObjects as $obj){
336 if($obj['status'] != "delete"){
337 if($use_dns){
338 if((isset($obj['description']))&&(!empty($obj['description']))){
339 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
340 }else{
341 $a_return[$obj['cn']]['name']= $obj['cn'];
342 }
343 $a_return[$obj['cn']]['dn']= $obj['dn'];
344 }else{
345 if((isset($obj['description']))&&(!empty($obj['description']))){
346 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
347 }else{
348 $a_return[$obj['cn']]= $obj['cn'];
349 }
350 }
351 }
352 }
353 return($a_return);
354 }
357 /* Delete me, and all my subtrees
358 */
359 function remove_from_parent()
360 {
361 $ldap = $this->config->get_ldap_link();
362 $ldap->cd ($this->dn);
363 $ldap->rmdir_recursive($this->dn);
364 show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed"));
365 $this->handle_post_events("remove");
366 }
369 /* Save data to object
370 */
371 function save_object()
372 {
373 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
374 plugin::save_object();
375 foreach($this->attributes as $attrs){
376 if(isset($_POST[$attrs])){
377 $this->$attrs = $_POST[$attrs];
378 }
379 }
380 }
381 }
384 /* Check supplied data */
385 function check()
386 {
387 /* Call common method to give check the hook */
388 $message= plugin::check();
390 /* If this is a new script, check if a script with this name already exists */
391 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
393 /* Check if current name is already used for fai scripts in selected release */
394 $dn = 'cn='.$this->cn.",ou=hooks,".$this->release;
395 $ldap = $this->config->get_ldap_link();
396 $ldap->cat($dn);
397 if($ldap->count()){
399 $r =convert_department_dn($this->release);;
400 $message[] = sprintf(_("Can't insert a fai hook named '%s' in '%s' there is already a hook with the given name."),$this->cn,$r);
401 }
402 }
403 return ($message);
404 }
407 /* Save to LDAP */
408 function save()
409 {
410 plugin::save();
412 $ldap = $this->config->get_ldap_link();
414 /* Copy & Paste : Ensure that FAIstate is copied too */
415 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
416 $this->attrs['FAIstate'] = $this->FAIstate;
417 }
419 $ldap->cat($this->dn,array("objectClass"));
420 if($ldap->count()!=0){
421 /* Write FAIscript to ldap*/
422 $ldap->cd($this->dn);
423 $this->cleanup();
424 $ldap->modify ($this->attrs);
426 }else{
427 /* Write FAIscript to ldap*/
428 $ldap->cd($this->config->current['BASE']);
429 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
430 $ldap->cd($this->dn);
431 $ldap->add($this->attrs);
432 }
433 show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed"));
435 /* Do object tagging */
436 $this->handle_object_tagging();
438 $ldap->cd($this->dn);
440 /* Prepare FAIscriptEntry to write it to ldap
441 * First sort array.
442 * Because we must delete old entries first.
443 * After deletion, we perform add and modify
444 */
445 $Objects = array();
448 /* We do not need to save untouched objects */
449 foreach($this->SubObjects as $name => $obj){
450 if($obj['status'] == "FreshLoaded"){
451 if($this->copy_paste_mode){
452 $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
453 $this->SubObjects[$name]['status'] = "new";
454 }else{
455 unset($this->SubObjects[$name]);
456 }
457 }
458 }
460 /* Add objects that must be removed first.*/
461 foreach($this->SubObjects as $name => $obj){
462 if($obj['status'] == "delete"){
463 $Objects[$name] = $obj;
464 }
465 }
467 /* Add objects to add/modify */
468 foreach($this->SubObjects as $name => $obj){
469 if($obj['status'] != "delete"){
470 $Objects[$name] = $obj;
471 }
472 }
474 /* Walk through list of objects */
475 foreach($Objects as $name => $obj){
477 /* Encode attribtues if required */
478 foreach($this->sub64coded as $codeIt){
479 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
480 }
482 /* Create ldap entry */
483 $tmp = array();
484 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
485 foreach($attributes as $attrs){
487 if(empty($obj[$attrs])){
488 $obj[$attrs] = array();
489 }
490 if(!is_array($obj[$attrs])){
491 $tmp[$attrs] = stripslashes($obj[$attrs]);
492 }else{
493 $tmp[$attrs] = $obj[$attrs];
494 }
495 }
497 $tmp['objectClass'] = $this->subClasses;
499 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
501 if($obj['status']=="new"){
502 $ldap->cat($sub_dn,array("objectClass"));
503 if($ldap->count()){
504 $obj['status']="edited";
505 }
506 }
508 /* Check if gosaAdministrativeUnitTag is required as object class */
509 if($obj['status'] == "edited"){
510 $ldap->cat($sub_dn,array("objectClass"));
511 $attrs = $ldap->fetch();
512 if(isset($attrs['objectClass'])){
513 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
514 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
515 }
516 }
517 }
519 if($obj['status'] == "delete"){
520 $ldap->cd($sub_dn);
521 $ldap->rmdir_recursive($sub_dn);
522 $this->handle_post_events("remove");
523 show_ldap_error($ldap->get_error(), _("Removing FAI hook failed"));
524 }elseif($obj['status'] == "edited"){
525 $ldap->cd($sub_dn);
526 $this->cleanup();
527 $ldap->modify ($tmp);
528 $this->handle_post_events("modify");
529 show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
530 }elseif($obj['status']=="new"){
531 if($tmp['description']==array()){
532 unset($tmp['description']);
533 }
534 $ldap->cd($this->config->current['BASE']);
535 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
536 $ldap->cd($sub_dn);
537 $ldap->add($tmp);
538 $this->handle_post_events("add");
539 show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
540 }
541 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
542 }
543 }
545 /* return copy & paste dialog
546 */
547 function getCopyDialog()
548 {
549 /* Ask for cn */
550 $smarty = get_smarty();
551 $smarty->assign("cn" ,$this->cn);
552 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
553 $ret = array();
554 $ret['string'] = $str;
555 $ret['status'] = "";
556 return($ret);
557 }
559 /* Get posted cn */
560 function saveCopyDialog()
561 {
562 if(isset($_POST['cn'])){
563 $this->cn = $_POST['cn'];
564 }
565 }
566 }
568 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
569 ?>