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 $SubObjects = array(); // All leafobjects of this object
32 var $view_logged = FALSE;
33 var $FAIstate = "";
34 var $ui;
36 function faiHook (&$config, $dn= NULL)
37 {
38 /* Load Attributes */
39 plugin::plugin ($config, $dn);
41 /* If "dn==new" we try to create a new entry
42 * Else we must read all objects from ldap which belong to this entry.
43 */
44 if($dn != "new"){
45 $this->dn =$dn;
47 /* Get FAIstate
48 */
49 if(isset($this->attrs['FAIstate'][0])){
50 $this->FAIstate = $this->attrs['FAIstate'][0];
51 }
53 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
54 */
55 $ldap = $this->config->get_ldap_link();
56 $ldap->cd ($this->dn);
57 $attrs_to_search = $this->subAttributes;
58 $attrs_to_search[] = "FAIstate";
59 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", $attrs_to_search);
61 $data = array();
62 while($object = $ldap->fetch()){
63 $data[] = $object;
64 }
65 foreach($data as $object){
67 /* Skip objects, that are tagged as removed */
68 if(isset($object['FAIstate'][0])){
69 if(preg_match("/removed$/",$object['FAIstate'][0])){
70 continue;
71 }
72 }
74 /* Set status for save management */
75 $objects = array();
76 $objects['status'] = "FreshLoaded";
77 $objects['dn'] = $object['dn'];
78 $objects = $this->get_object_attributes($objects,$this->subAttributes);
79 $this->SubObjects[$objects['cn']] = $objects;
80 }
81 }
82 $this->ui = get_userinfo();
83 }
86 /* Reload some attributes */
87 function get_object_attributes($object,$attributes)
88 {
89 $ldap = $this->config->get_ldap_link();
90 $ldap->cd($this->config->current['BASE']);
91 $ldap->cat($object['dn'],$attributes);
92 $tmp = $ldap->fetch();
94 foreach($attributes as $attrs){
95 if(isset($tmp[$attrs][0])){
96 $var = $tmp[$attrs][0];
98 /* Check if we must decode some attributes */
99 if(in_array_ics($attrs,$this->sub64coded)){
100 $var = base64_decode($var);
101 }
103 /* check if this is a binary entry */
104 if(in_array_ics($attrs,$this->subBinary)){
105 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
106 }
108 /* Fix slashes */
109 $var = addslashes($var);
110 $object[$attrs] = $var;
111 }
112 }
113 return($object);
114 }
117 function acl_base_for_current_object($dn)
118 {
119 if($dn == "new"){
120 if($this->dn == "new"){
121 $dn = session::get('CurrentMainBase');
122 }else{
123 $dn = $this->dn;
124 }
125 }
126 return($dn);
127 }
130 function getUsedFAItask($cn)
131 {
132 $ret = array();
133 foreach($this->SubObjects as $name => $class){
134 if($class['cn'] == $cn){
135 continue;
136 }
137 if($class['status'] != "delete"){
138 $ret[$class['FAItask']] = $class['FAItask'];
139 }
140 }
141 return($ret);
142 }
144 function execute()
145 {
146 /* Call parent execute */
147 plugin::execute();
149 if($this->is_account && !$this->view_logged){
150 $this->view_logged = TRUE;
151 new log("view","fai/".get_class($this),$this->dn);
152 }
154 /* Fill templating stuff */
155 $smarty= get_smarty();
156 $display= "";
158 /* New Listhandling
159 */
160 $once = true;
161 foreach($_POST as $name => $value){
162 if(preg_match("/^editscript_/",$name)&&($once)){
163 $once = false;
164 $entry = preg_replace("/^editscript_/","",$name);
165 $entry = base64_decode(preg_replace("/_.*/","",$entry));
167 $obj = $this->SubObjects[$entry];
168 if($obj['status'] == "FreshLoaded"){
169 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
170 }
172 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
173 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
174 $this->dialog->set_acl_category("fai");
176 session::set('objectinfo',$obj['dn']);
177 $this->dialog->parent = &$this;
178 $this->is_dialog=true;
179 }
180 if(preg_match("/^deletescript_/",$name)&&($once)){
181 $entry = preg_replace("/^deletescript_/","",$name);
182 $entry = base64_decode(preg_replace("/_.*/","",$entry));
184 $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
185 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
187 if(preg_match("/d/",$acl)){
188 $once = false;
190 $status = $this->SubObjects[$entry]['status'];
191 if($status == "edited" || $status == "FreshLoaded"){
192 $this->SubObjects[$entry]['status']= "delete";
193 }else{
194 unset($this->SubObjects[$entry]);
195 }
196 }
197 }
198 }
200 /* File download requested */
201 if(isset($_GET['getFAIHook'])){
202 if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
203 $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
204 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
205 send_binary_content(stripslashes($obj['FAIhook']),$obj['cn'].".FAIhook");
206 }
207 }
209 /* Edit entries via GET */
210 if(isset($_GET['act']) && isset($_GET['id'])){
211 if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
212 $obj = $this->SubObjects[base64_decode($_GET['id'])];
213 if($obj['status'] == "FreshLoaded"){
214 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
215 }
216 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
217 $this->dialog->acl = $this->acl;
218 session::set('objectinfo',$obj['dn']);
219 $this->dialog->parent = &$this;
220 $this->is_dialog=true;
221 }
222 }
224 ///// Ende new list handling
226 /* Add new sub object */
227 if(isset($_POST['AddSubObject'])){
229 $c_dn = $this->acl_base_for_current_object($this->dn);
230 $this->dialog= new $this->subClassName($this->config,"new");
231 $this->dialog->set_acl_base($c_dn);
232 $this->dialog->set_acl_category("fai");
233 $this->dialog->parent = &$this;
234 $this->is_dialog=true;
235 }
238 /* Save Dialog */
239 if(isset($_POST['SaveSubObject'])){
241 /* Perform post check*/
242 $this->dialog->save_object();
244 /* Get messages */
245 $msgs = $this->dialog->check();
247 /* print errors */
248 if(count($msgs)>0){
249 foreach($msgs as $msg){
250 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
251 }
252 }else{
254 /* Get return object */
255 $obj = $this->dialog->save();
256 if(isset($obj['remove'])){
258 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
260 /* Depending on status, set new status */
261 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
262 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
263 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
264 unset($this->SubObjects[$obj['remove']['from']]);
265 }
266 $obj['status'] = "new";
267 $this->SubObjects[$obj['remove']['to']] = $obj;
268 unset($this->SubObjects[$obj['remove']['to']]['remove']);
269 }else{
270 if($obj['status'] == "FreshLoaded"){
271 $obj['status'] = "edited";
272 }
273 $this->SubObjects[$obj['cn']]=$obj;
274 }
275 $this->is_dialog=false;
276 unset($this->dialog);
277 $this->dialog=FALSE;
278 }
279 }
281 /* Sort entries */
282 $tmp = $keys = array();
283 foreach($this->SubObjects as $key => $entry){
284 $keys[$key]=$key;
285 }
286 natcasesort($keys);
287 foreach($keys as $key){
288 $tmp[$key]=$this->SubObjects[$key];
289 }
290 $this->SubObjects = $tmp;
292 /* Cancel Dialog */
293 if(isset($_POST['CancelSubObject'])){
294 $this->is_dialog=false;
295 unset($this->dialog);
296 $this->dialog=FALSE;
297 }
299 /* Print dialog if $this->dialog is set */
300 if(is_object($this->dialog)){
301 $this->dialog->save_object();
302 $display = $this->dialog->execute();
303 return($display);
304 }
308 /* Divlist added 28.02.2006
309 Containing FAIscripts
310 */
312 $divlist = new divSelectBox("FAIhooks");
313 $divlist->setHeight(400);
315 foreach($this->getList(true) as $key => $name){
317 $dn= $this->acl_base_for_current_object($name['dn']);
318 $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry") ;
319 $act = "";
321 /* Check if this object is freezed, in this case hide the delete icon */
322 if(preg_match("/^freeze/", $this->FAIstate)){
323 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("Edit")."' alt='"._("Edit")."'>";
324 }else{
325 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("Edit")."' alt='"._("Edit")."'>";
326 if(preg_match("/d/",$acl)){
327 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("Delete")."' alt='"._("Delete")."'>";
328 }
329 }
331 /* Check if we are allowed to use the export button for this object */
332 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript") ;
333 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
334 $down = "";
335 }else{
336 $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
337 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
338 </a>";
339 }
341 /* Check if we are allowed to view the object */
342 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn") ;
343 if(preg_match("/r/",$s_acl)){
345 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($key)."'>".$name['name']."</a>";
346 $divlist->AddEntry(array( array("string"=>$edit_link),
347 array("string"=>$down , "attach" => "style='width:20px;'"),
348 array("string"=>str_replace("%s",base64_encode($key),$act),
349 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
350 }
351 }
352 $smarty->assign("Entry_divlist",$divlist->DrawList());
353 /* Divlist creation complete
354 */
356 $smarty->assign("SubObjects",$this->getList());
358 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
359 * If we post the escaped strings they will be escaped again
360 */
361 foreach($this->attributes as $attrs){
362 if(get_magic_quotes_gpc()){
363 $smarty->assign($attrs,stripslashes($this->$attrs));
364 }else{
365 $smarty->assign($attrs,($this->$attrs));
366 }
367 }
369 $tmp = $this->plInfo();
371 $c_dn = $this->acl_base_for_current_object($this->dn);
372 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
373 foreach($tmp['plProvidedAcls'] as $name => $translation){
374 $smarty->assign($name."ACL",$this->getacl($name));
375 }
377 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
378 return($display);
379 }
381 /* Generate listbox friendly SubObject list
382 */
383 function getList($use_dns=false){
384 $a_return=array();
385 foreach($this->SubObjects as $obj){
386 if($obj['status'] != "delete"){
388 $cn = stripslashes($obj['cn']);
389 $desc = "";
391 if((isset($obj['description']))&&(!empty($obj['description']))){
392 $desc = " [".stripslashes($obj['description'])."]";
393 }
395 if($use_dns){
396 $a_return[$obj['cn']]['name']= $cn.$desc;
397 $a_return[$obj['cn']]['dn']= $obj['dn'];
398 }else{
399 $a_return[$obj['cn']] = $cn.$desc;
400 }
401 }
402 }
403 return($a_return);
404 }
407 /* Delete me, and all my subtrees
408 */
409 function remove_from_parent()
410 {
411 $ldap = $this->config->get_ldap_link();
412 $ldap->cd ($this->dn);
414 $faifilter = session::get('faifilter');
415 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
416 if($faifilter['branch'] == "main"){
417 $use_dn = $this->dn;
418 }
420 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
422 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
424 foreach($this->SubObjects as $name => $obj){
425 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
426 if($faifilter['branch'] == "main"){
427 $use_dn = $obj['dn'];
428 }
429 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
430 }
431 $this->handle_post_events("remove");
432 }
435 /* Save data to object
436 */
437 function save_object()
438 {
439 if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
440 plugin::save_object();
441 foreach($this->attributes as $attrs){
442 if(isset($_POST[$attrs])){
443 $this->$attrs = $_POST[$attrs];
444 }
445 }
446 }
447 }
450 /* Check supplied data */
451 function check()
452 {
453 /* Call common method to give check the hook */
454 $message= plugin::check();
456 /* Ensure that we do not overwrite an allready existing entry
457 */
458 if($this->is_new){
459 $new_dn= 'cn='.$this->cn.",".get_ou('faihookou').get_ou('faiou').session::get('CurrentMainBase');
460 $faifilter = session::get('faifilter');
461 if($faifilter['branch']!="main"){
462 $new_dn ='cn='.$this->cn.",".get_ou('faihookou').$faifilter['branch'];
463 }
465 $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
466 if(isset($res[$this->cn])){
467 $message[] = msgPool::duplicated(_("Name"));
468 }
469 }
471 return ($message);
472 }
475 /* Save to LDAP */
476 function save()
477 {
478 plugin::save();
480 $ldap = $this->config->get_ldap_link();
482 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
484 if($this->initially_was_account){
485 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
486 }else{
487 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
488 }
490 $ldap->cd($this->dn);
492 /* Prepare FAIscriptEntry to write it to ldap
493 * First sort array.
494 * Because we must delete old entries first.
495 * After deletion, we perform add and modify
496 */
497 $Objects = array();
499 /* We do not need to save untouched objects */
500 foreach($this->SubObjects as $name => $obj){
501 if($obj['status'] != "FreshLoaded"){
502 $Objects[$name] = $obj;
503 }
504 }
506 foreach($Objects as $name => $obj){
508 foreach($this->sub64coded as $codeIt){
509 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
510 }
512 $tmp = array();
513 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
514 foreach($attributes as $attrs){
515 if(empty($obj[$attrs])){
516 $obj[$attrs] = array();
517 }
518 if(!is_array($obj[$attrs])){
519 $tmp[$attrs] = stripslashes($obj[$attrs]);
520 }else{
521 $tmp[$attrs] = $obj[$attrs];
522 }
523 }
525 $tmp['objectClass'] = $this->subClasses;
527 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
529 if($obj['status']=="new"){
530 $ldap->cat($sub_dn,array("objectClass"));
531 if($ldap->count()){
532 $obj['status']="edited";
533 }
534 }
536 /* Tag object */
537 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
539 if($obj['status'] == "delete"){
540 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
541 $this->handle_post_events("remove");
542 }elseif($obj['status'] == "edited"){
543 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
544 $this->handle_post_events("modify");
545 }elseif($obj['status']=="new"){
546 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
547 $this->handle_post_events("add");
548 }
549 }
550 }
553 function PrepareForCopyPaste($source)
554 {
555 plugin::PrepareForCopyPaste($source);
557 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
558 */
559 $ldap = $this->config->get_ldap_link();
560 $ldap->cd ($source['dn']);
562 $attrs_to_search = $this->subAttributes;
563 $attrs_to_search[] = "FAIstate";
564 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
566 while($object = $ldap->fetch()){
568 /* Skip objects, that are tagged as removed */
569 if(isset($object['FAIstate'][0])){
570 if(preg_match("/removed$/",$object['FAIstate'][0])){
571 continue;
572 }
573 }
575 /* Set status for save management */
576 $objects = array();
577 $objects['status'] = "edited";
578 $objects['dn'] = $object['dn'];
579 $objects = $this->get_object_attributes($objects,$this->subAttributes);
580 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
582 $this->SubObjects[$objects['cn']] = $objects;
583 }
584 }
587 /* Return plugin informations for acl handling */
588 static function plInfo()
589 {
590 return (array(
591 "plShortName" => _("Hook"),
592 "plDescription" => _("FAI hook"),
593 "plSelfModify" => FALSE,
594 "plDepends" => array(),
595 "plPriority" => 20,
596 "plSection" => array("administration"),
597 "plCategory" => array("fai"),
598 "plProvidedAcls" => array(
599 "cn" => _("Name")." ("._("Read only").")",
600 "description" => _("Description"))
601 ));
602 }
605 /*! \brief Used for copy & paste.
606 Returns a HTML input mask, which allows to change the cn of this entry.
607 @param Array Array containing current status && a HTML template.
608 */
609 function getCopyDialog()
610 {
611 $vars = array("cn");
612 $smarty = get_smarty();
613 $smarty->assign("cn", htmlentities($this->cn));
614 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
615 $ret = array();
616 $ret['string'] = $str;
617 $ret['status'] = "";
618 return($ret);
619 }
622 /*! \brief Used for copy & paste.
623 Some entries must be renamed to avaoid duplicate entries.
624 */
625 function saveCopyDialog()
626 {
627 if(isset($_POST['cn'])){
628 $this->cn = get_post('cn');
629 }
630 }
631 }
633 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
634 ?>