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 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
48 */
49 $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
50 foreach($res as $obj){
52 /* Skip not relevant objects */
53 if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
55 $objects = array();
56 $objects['status'] = "FreshLoaded";
57 $objects['dn'] = $obj['dn'];
58 $objects = $this->get_object_attributes($objects,$this->subAttributes);
59 $this->SubObjects[$objects['cn']] = $objects;
60 }
61 }
62 $this->is_new = FALSE;
63 if($this->dn == "new"){
64 $this->is_new =TRUE;
65 }
66 $this->ui = get_userinfo();
68 $this->hookListWidget= new sortableListing($this->convertList(TRUE), $this->convertList());
69 $this->hookListWidget->setDeleteable(true);
70 $this->hookListWidget->setInstantDelete(false);
71 $this->hookListWidget->setEditable(true);
72 $this->hookListWidget->setWidth("100%");
73 $this->hookListWidget->setHeight("140px");
74 $this->hookListWidget->setHeader(array(_("Name"),_("Description")));
75 }
78 function convertList($type = FALSE)
79 {
80 $data = array();
81 $s_acl = $this->ui->get_permissions($this->dn,"fai/faiScriptEntry","FAIscript");
82 foreach($this->SubObjects as $cn => $entry){
83 if($entry['status'] == "delete") continue;
84 if($type){
85 $data[$cn] = $entry;
86 }else{
87 if(!isset($entry['description'])) $entry['description']="";
88 $data[$cn] = array('data' => array($entry['cn'], $entry['description']));
89 }
90 }
91 return($data);
92 }
95 /* Reload some attributes */
96 function get_object_attributes($object,$attributes)
97 {
98 $ldap = $this->config->get_ldap_link();
99 $ldap->cd($this->config->current['BASE']);
100 $ldap->cat($object['dn'],$attributes);
101 $tmp = $ldap->fetch();
103 foreach($attributes as $attrs){
104 if(isset($tmp[$attrs][0])){
105 $var = $tmp[$attrs][0];
107 /* Check if we must decode some attributes */
108 if(in_array_ics($attrs,$this->sub64coded)){
109 $var = base64_decode($var);
110 }
112 /* check if this is a binary entry */
113 if(in_array_ics($attrs,$this->subBinary)){
114 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
115 }
117 /* Fix slashes */
118 $var = addslashes($var);
119 $object[$attrs] = $var;
120 }
121 }
122 return($object);
123 }
126 function acl_base_for_current_object($dn)
127 {
128 if($dn == "new" || $dn == ""){
129 if($this->dn == "new"){
130 $dn= $this->parent->parent->acl_base;
131 }else{
132 $dn = $this->dn;
133 }
134 }
135 return($dn);
136 }
139 function getUsedFAItask($cn)
140 {
141 $ret = array();
142 foreach($this->SubObjects as $name => $class){
143 if($class['cn'] == $cn){
144 continue;
145 }
146 if($class['status'] != "delete"){
147 $ret[$class['FAItask']] = $class['FAItask'];
148 }
149 }
150 return($ret);
151 }
153 function execute()
154 {
155 /* Call parent execute */
156 plugin::execute();
158 if($this->is_account && !$this->view_logged){
159 $this->view_logged = TRUE;
160 new log("view","fai/".get_class($this),$this->dn);
161 }
163 /* Fill templating stuff */
164 $smarty= get_smarty();
165 $display= "";
167 /* New Listhandling
168 */
169 $once = true;
171 $this->hookListWidget->setAcl($this->getacl(""));
172 $this->hookListWidget->save_object();
173 $action = $this->hookListWidget->getAction();
175 if($action['action'] =="edit"){
176 $s_entry = $this->hookListWidget->getKey($action['targets'][0]);
177 if(isset($this->SubObjects[$s_entry])){
178 $obj = $this->SubObjects[$s_entry];
179 if($obj['status'] == "FreshLoaded"){
180 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
181 }
183 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
184 $this->dialog->FAIstate = $this->FAIstate;
185 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
186 $this->dialog->set_acl_category("fai");
188 set_object_info($obj['dn']);
189 $this->dialog->parent = &$this;
190 $this->is_dialog=true;
191 }
192 }
194 if($action['action'] =="delete"){
195 $s_entry = $this->hookListWidget->getKey($action['targets'][0]);
196 if(isset($this->SubObjects[$s_entry])){
197 $dn = $this->acl_base_for_current_object($this->SubObjects[$s_entry]['dn']);
198 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
199 if(preg_match("/d/",$acl)){
200 $status = $this->SubObjects[$s_entry]['status'];
201 if($status == "edited" || $status == "FreshLoaded"){
202 $this->SubObjects[$s_entry]['status']= "delete";
203 }else{
204 unset($this->SubObjects[$s_entry]);
205 }
206 }
207 }
208 }
210 /* Add new sub object */
211 if(isset($_POST['AddSubObject'])){
212 $c_dn = $this->acl_base_for_current_object($this->dn);
213 $this->dialog= new $this->subClassName($this->config,"new");
214 $this->dialog->set_acl_base($c_dn);
215 $this->dialog->FAIstate = $this->FAIstate;
216 $this->dialog->set_acl_category("fai");
217 $this->dialog->parent = &$this;
218 $this->is_dialog=true;
219 }
222 /* Save Dialog */
223 if(isset($_POST['SaveSubObject'])){
225 /* Perform post check*/
226 $this->dialog->save_object();
228 /* Get messages */
229 $msgs = $this->dialog->check();
231 /* print errors */
232 if(count($msgs)>0){
233 foreach($msgs as $msg){
234 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
235 }
236 }else{
238 /* Get return object */
239 $obj = $this->dialog->save();
240 if(isset($obj['remove'])){
242 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
244 /* Depending on status, set new status */
245 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
246 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
247 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
248 unset($this->SubObjects[$obj['remove']['from']]);
249 }
250 $obj['status'] = "new";
251 $this->SubObjects[$obj['remove']['to']] = $obj;
252 unset($this->SubObjects[$obj['remove']['to']]['remove']);
253 }else{
254 if($obj['status'] == "FreshLoaded"){
255 $obj['status'] = "edited";
256 }
257 $this->SubObjects[$obj['cn']]=$obj;
258 }
259 $this->is_dialog=false;
260 unset($this->dialog);
261 $this->dialog=FALSE;
262 }
263 }
265 /* Sort entries */
266 $tmp = $keys = array();
267 foreach($this->SubObjects as $key => $entry){
268 $keys[$key]=$key;
269 }
270 natcasesort($keys);
271 foreach($keys as $key){
272 $tmp[$key]=$this->SubObjects[$key];
273 }
274 $this->SubObjects = $tmp;
276 /* Cancel Dialog */
277 if(isset($_POST['CancelSubObject'])){
278 $this->is_dialog=false;
279 unset($this->dialog);
280 $this->dialog=FALSE;
281 }
283 /* Print dialog if $this->dialog is set */
284 if(is_object($this->dialog)){
285 $this->dialog->save_object();
286 $display = $this->dialog->execute();
287 return($display);
288 }
290 $this->hookListWidget->setListData($this->convertList(TRUE), $this->convertList());
291 $this->hookListWidget->update();
292 $smarty->assign("Entry_listing",$this->hookListWidget->render());
294 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
295 * If we post the escaped strings they will be escaped again
296 */
297 foreach($this->attributes as $attrs){
298 if(get_magic_quotes_gpc()){
299 $smarty->assign($attrs,stripslashes($this->$attrs));
300 }else{
301 $smarty->assign($attrs,($this->$attrs));
302 }
303 }
305 $tmp = $this->plInfo();
307 $c_dn = $this->acl_base_for_current_object($this->dn);
308 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
309 foreach($tmp['plProvidedAcls'] as $name => $translation){
310 $smarty->assign($name."ACL",$this->getacl($name));
311 }
313 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
314 return($display);
315 }
317 /* Generate listbox friendly SubObject list
318 */
319 function getList($use_dns=false){
320 $a_return=array();
321 foreach($this->SubObjects as $obj){
322 if($obj['status'] != "delete"){
324 $cn = stripslashes($obj['cn']);
325 $desc = "";
327 if((isset($obj['description']))&&(!empty($obj['description']))){
328 $desc = " [".stripslashes($obj['description'])."]";
329 }
331 if($use_dns){
332 $a_return[$obj['cn']]['name']= $cn.$desc;
333 $a_return[$obj['cn']]['dn']= $obj['dn'];
334 }else{
335 $a_return[$obj['cn']] = $cn.$desc;
336 }
337 }
338 }
339 return($a_return);
340 }
343 /* Delete me, and all my subtrees
344 */
345 function remove_from_parent()
346 {
347 if($this->acl_is_removeable()){
348 $ldap = $this->config->get_ldap_link();
349 $ldap->cd ($this->dn);
350 $release = $this->parent->parent->fai_release;
351 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
352 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
353 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
355 foreach($this->SubObjects as $name => $obj){
356 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
357 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
358 }
359 $this->handle_post_events("remove");
360 }
361 }
364 /* Save data to object
365 */
366 function save_object()
367 {
368 if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
369 plugin::save_object();
370 }
371 }
374 /* Check supplied data */
375 function check()
376 {
377 /* Call common method to give check the hook */
378 $message= plugin::check();
380 /* Ensure that we do not overwrite an allready existing entry
381 */
382 if($this->is_new){
383 $release = $this->parent->parent->fai_release;
384 $new_dn= 'cn='.$this->cn.",".get_ou('faiHookRDN').get_ou('faiBaseRDN').$release;
385 $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
386 if(isset($res[$this->cn])){
387 $message[] = msgPool::duplicated(_("Name"));
388 }
389 }
391 return ($message);
392 }
395 /* Save to LDAP */
396 function save()
397 {
398 plugin::save();
400 $ldap = $this->config->get_ldap_link();
402 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
404 if($this->initially_was_account){
405 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
406 }else{
407 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
408 }
410 $ldap->cd($this->dn);
412 /* Prepare FAIscriptEntry to write it to ldap
413 * First sort array.
414 * Because we must delete old entries first.
415 * After deletion, we perform add and modify
416 */
417 $Objects = array();
419 /* We do not need to save untouched objects */
420 foreach($this->SubObjects as $name => $obj){
421 if($obj['status'] != "FreshLoaded"){
422 $Objects[$name] = $obj;
423 }
424 }
426 foreach($Objects as $name => $obj){
428 foreach($this->sub64coded as $codeIt){
429 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
430 }
432 $tmp = array();
433 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
434 foreach($attributes as $attrs){
435 if(!isset($obj[$attrs])) continue;
436 if($obj[$attrs] == ""){
437 $obj[$attrs] = array();
438 }
439 if(!is_array($obj[$attrs])){
440 $tmp[$attrs] = stripslashes($obj[$attrs]);
441 }else{
442 $tmp[$attrs] = $obj[$attrs];
443 }
444 }
446 $tmp['objectClass'] = $this->subClasses;
448 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
450 if($obj['status']=="new"){
451 $ldap->cat($sub_dn,array("objectClass"));
452 if($ldap->count()){
453 $obj['status']="edited";
454 }
455 }
457 /* Tag object */
458 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
460 if($obj['status'] == "delete"){
461 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
462 $this->handle_post_events("remove");
463 }elseif($obj['status'] == "edited"){
464 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
465 $this->handle_post_events("modify");
466 }elseif($obj['status']=="new"){
467 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
468 $this->handle_post_events("add");
469 }
470 }
471 }
474 function PrepareForCopyPaste($source)
475 {
476 plugin::PrepareForCopyPaste($source);
478 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
479 */
480 $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
481 foreach($res as $obj){
483 /* Skip not relevant objects */
484 if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
486 $objects = array();
487 $objects['status'] = "edited";
488 $objects['dn'] = $obj['dn'];
489 $objects = $this->get_object_attributes($objects,$this->subAttributes);
490 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
491 $this->SubObjects[$objects['cn']] = $objects;
492 }
493 }
496 /* Return plugin informations for acl handling */
497 static function plInfo()
498 {
499 return (array(
500 "plShortName" => _("Hook"),
501 "plDescription" => _("FAI hook"),
502 "plSelfModify" => FALSE,
503 "plDepends" => array(),
504 "plPriority" => 20,
505 "plSection" => array("administration"),
506 "plCategory" => array("fai"),
508 "plProperties" =>
509 array(
510 array(
511 "name" => "faiHookRDN",
512 "type" => "rdn",
513 "default" => "ou=hooks,",
514 "description" => "The 'faiHookRDN' statement defines the location where new fai-hooks will be created. The default is 'ou=hooks,'.",
515 "check" => "gosaProperty::isRdn",
516 "migrate" => "",
517 "group" => "plugin",
518 "mandatory" => FALSE)),
521 "plProvidedAcls" => array(
522 "cn" => _("Name")." ("._("Read only").")",
523 "description" => _("Description"))
524 ));
525 }
528 /*! \brief Used for copy & paste.
529 Returns a HTML input mask, which allows to change the cn of this entry.
530 @param Array Array containing current status && a HTML template.
531 */
532 function getCopyDialog()
533 {
534 $vars = array("cn");
535 $smarty = get_smarty();
536 $smarty->assign("cn", htmlentities($this->cn));
537 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
538 $ret = array();
539 $ret['string'] = $str;
540 $ret['status'] = "";
541 return($ret);
542 }
545 /*! \brief Used for copy & paste.
546 Some entries must be renamed to avaoid duplicate entries.
547 */
548 function saveCopyDialog()
549 {
550 if(isset($_POST['cn'])){
551 $this->cn = get_post('cn');
552 }
553 }
554 }
556 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
557 ?>