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("/".normalizePreg($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();
67 }
70 /* Reload some attributes */
71 function get_object_attributes($object,$attributes)
72 {
73 $ldap = $this->config->get_ldap_link();
74 $ldap->cd($this->config->current['BASE']);
75 $ldap->cat($object['dn'],$attributes);
76 $tmp = $ldap->fetch();
78 foreach($attributes as $attrs){
79 if(isset($tmp[$attrs][0])){
80 $var = $tmp[$attrs][0];
82 /* Check if we must decode some attributes */
83 if(in_array_ics($attrs,$this->sub64coded)){
84 $var = base64_decode($var);
85 }
87 /* check if this is a binary entry */
88 if(in_array_ics($attrs,$this->subBinary)){
89 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
90 }
92 /* Fix slashes */
93 $var = addslashes($var);
94 $object[$attrs] = $var;
95 }
96 }
97 return($object);
98 }
101 function acl_base_for_current_object($dn)
102 {
103 if($dn == "new"){
104 if($this->dn == "new"){
105 $dn = session::get('CurrentMainBase');
106 }else{
107 $dn = $this->dn;
108 }
109 }
110 return($dn);
111 }
114 function getUsedFAItask($cn)
115 {
116 $ret = array();
117 foreach($this->SubObjects as $name => $class){
118 if($class['cn'] == $cn){
119 continue;
120 }
121 if($class['status'] != "delete"){
122 $ret[$class['FAItask']] = $class['FAItask'];
123 }
124 }
125 return($ret);
126 }
128 function execute()
129 {
130 /* Call parent execute */
131 plugin::execute();
133 if($this->is_account && !$this->view_logged){
134 $this->view_logged = TRUE;
135 new log("view","fai/".get_class($this),$this->dn);
136 }
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->FAIstate = $this->FAIstate;
158 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
159 $this->dialog->set_acl_category("fai");
161 session::set('objectinfo',$obj['dn']);
162 $this->dialog->parent = &$this;
163 $this->is_dialog=true;
164 }
165 if(preg_match("/^deletescript_/",$name)&&($once)){
166 $entry = preg_replace("/^deletescript_/","",$name);
167 $entry = base64_decode(preg_replace("/_.*/","",$entry));
169 $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
170 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
172 if(preg_match("/d/",$acl)){
173 $once = false;
175 $status = $this->SubObjects[$entry]['status'];
176 if($status == "edited" || $status == "FreshLoaded"){
177 $this->SubObjects[$entry]['status']= "delete";
178 }else{
179 unset($this->SubObjects[$entry]);
180 }
181 }
182 }
183 }
185 /* File download requested */
186 if(isset($_GET['getFAIHook'])){
187 if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
188 $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
189 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
190 send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIhook");
191 }
192 }
194 /* Edit entries via GET */
195 if(isset($_GET['act']) && isset($_GET['id'])){
196 if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
197 $obj = $this->SubObjects[base64_decode($_GET['id'])];
198 if($obj['status'] == "FreshLoaded"){
199 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
200 }
201 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
202 $this->dialog->FAIstate = $this->FAIstate;
203 $this->dialog->acl = $this->acl;
204 session::set('objectinfo',$obj['dn']);
205 $this->dialog->parent = &$this;
206 $this->is_dialog=true;
207 }
208 }
210 ///// Ende new list handling
212 /* Add new sub object */
213 if(isset($_POST['AddSubObject'])){
215 $c_dn = $this->acl_base_for_current_object($this->dn);
216 $this->dialog= new $this->subClassName($this->config,"new");
217 $this->dialog->set_acl_base($c_dn);
218 $this->dialog->FAIstate = $this->FAIstate;
219 $this->dialog->set_acl_category("fai");
220 $this->dialog->parent = &$this;
221 $this->is_dialog=true;
222 }
225 /* Save Dialog */
226 if(isset($_POST['SaveSubObject'])){
228 /* Perform post check*/
229 $this->dialog->save_object();
231 /* Get messages */
232 $msgs = $this->dialog->check();
234 /* print errors */
235 if(count($msgs)>0){
236 foreach($msgs as $msg){
237 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
238 }
239 }else{
241 /* Get return object */
242 $obj = $this->dialog->save();
243 if(isset($obj['remove'])){
245 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
247 /* Depending on status, set new status */
248 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
249 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
250 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
251 unset($this->SubObjects[$obj['remove']['from']]);
252 }
253 $obj['status'] = "new";
254 $this->SubObjects[$obj['remove']['to']] = $obj;
255 unset($this->SubObjects[$obj['remove']['to']]['remove']);
256 }else{
257 if($obj['status'] == "FreshLoaded"){
258 $obj['status'] = "edited";
259 }
260 $this->SubObjects[$obj['cn']]=$obj;
261 }
262 $this->is_dialog=false;
263 unset($this->dialog);
264 $this->dialog=FALSE;
265 }
266 }
268 /* Sort entries */
269 $tmp = $keys = array();
270 foreach($this->SubObjects as $key => $entry){
271 $keys[$key]=$key;
272 }
273 natcasesort($keys);
274 foreach($keys as $key){
275 $tmp[$key]=$this->SubObjects[$key];
276 }
277 $this->SubObjects = $tmp;
279 /* Cancel Dialog */
280 if(isset($_POST['CancelSubObject'])){
281 $this->is_dialog=false;
282 unset($this->dialog);
283 $this->dialog=FALSE;
284 }
286 /* Print dialog if $this->dialog is set */
287 if(is_object($this->dialog)){
288 $this->dialog->save_object();
289 $display = $this->dialog->execute();
290 return($display);
291 }
295 /* Divlist added 28.02.2006
296 Containing FAIscripts
297 */
299 $divlist = new divSelectBox("FAIhooks");
300 $divlist->setHeight(400);
302 foreach($this->getList(true) as $key => $name){
304 $dn= $this->acl_base_for_current_object($name['dn']);
305 $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry") ;
306 $act = "";
308 /* Check if this object is freezed, in this case hide the delete icon */
309 if(preg_match("/^freeze/", $this->FAIstate)){
310 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("Edit")."' alt='"._("Edit")."'>";
311 }else{
312 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("Edit")."' alt='"._("Edit")."'>";
313 if(preg_match("/d/",$acl)){
314 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("Delete")."' alt='"._("Delete")."'>";
315 }
316 }
318 /* Check if we are allowed to use the export button for this object */
319 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript") ;
320 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
321 $down = "";
322 }else{
323 $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
324 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
325 </a>";
326 }
328 /* Check if we are allowed to view the object */
329 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn") ;
330 if(preg_match("/r/",$s_acl)){
332 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($key)."'>".$name['name']."</a>";
333 $divlist->AddEntry(array( array("string"=>$edit_link),
334 array("string"=>$down , "attach" => "style='width:20px;'"),
335 array("string"=>str_replace("%s",base64_encode($key),$act),
336 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
337 }
338 }
339 $smarty->assign("Entry_divlist",$divlist->DrawList());
340 /* Divlist creation complete
341 */
343 $smarty->assign("SubObjects",$this->getList());
345 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
346 * If we post the escaped strings they will be escaped again
347 */
348 foreach($this->attributes as $attrs){
349 if(get_magic_quotes_gpc()){
350 $smarty->assign($attrs,stripslashes($this->$attrs));
351 }else{
352 $smarty->assign($attrs,($this->$attrs));
353 }
354 }
356 $tmp = $this->plInfo();
358 $c_dn = $this->acl_base_for_current_object($this->dn);
359 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
360 foreach($tmp['plProvidedAcls'] as $name => $translation){
361 $smarty->assign($name."ACL",$this->getacl($name));
362 }
364 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
365 return($display);
366 }
368 /* Generate listbox friendly SubObject list
369 */
370 function getList($use_dns=false){
371 $a_return=array();
372 foreach($this->SubObjects as $obj){
373 if($obj['status'] != "delete"){
375 $cn = stripslashes($obj['cn']);
376 $desc = "";
378 if((isset($obj['description']))&&(!empty($obj['description']))){
379 $desc = " [".stripslashes($obj['description'])."]";
380 }
382 if($use_dns){
383 $a_return[$obj['cn']]['name']= $cn.$desc;
384 $a_return[$obj['cn']]['dn']= $obj['dn'];
385 }else{
386 $a_return[$obj['cn']] = $cn.$desc;
387 }
388 }
389 }
390 return($a_return);
391 }
394 /* Delete me, and all my subtrees
395 */
396 function remove_from_parent()
397 {
398 if($this->acl_is_removeable()){
399 $ldap = $this->config->get_ldap_link();
400 $ldap->cd ($this->dn);
401 $release = $this->parent->parent->fai_release;
402 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
403 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
404 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
406 foreach($this->SubObjects as $name => $obj){
407 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
408 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
409 }
410 $this->handle_post_events("remove");
411 }
412 }
415 /* Save data to object
416 */
417 function save_object()
418 {
419 if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
420 plugin::save_object();
421 foreach($this->attributes as $attrs){
422 if(isset($_POST[$attrs])){
423 $this->$attrs = $_POST[$attrs];
424 }
425 }
426 }
427 }
430 /* Check supplied data */
431 function check()
432 {
433 /* Call common method to give check the hook */
434 $message= plugin::check();
436 /* Ensure that we do not overwrite an allready existing entry
437 */
438 if($this->is_new){
439 $release = $this->parent->parent->fai_release;
440 $new_dn= 'cn='.$this->cn.",".get_ou('faihookou').get_ou('faiou').$release;
441 $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
442 if(isset($res[$this->cn])){
443 $message[] = msgPool::duplicated(_("Name"));
444 }
445 }
447 return ($message);
448 }
451 /* Save to LDAP */
452 function save()
453 {
454 plugin::save();
456 $ldap = $this->config->get_ldap_link();
458 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
460 if($this->initially_was_account){
461 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
462 }else{
463 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
464 }
466 $ldap->cd($this->dn);
468 /* Prepare FAIscriptEntry to write it to ldap
469 * First sort array.
470 * Because we must delete old entries first.
471 * After deletion, we perform add and modify
472 */
473 $Objects = array();
475 /* We do not need to save untouched objects */
476 foreach($this->SubObjects as $name => $obj){
477 if($obj['status'] != "FreshLoaded"){
478 $Objects[$name] = $obj;
479 }
480 }
482 foreach($Objects as $name => $obj){
484 foreach($this->sub64coded as $codeIt){
485 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
486 }
488 $tmp = array();
489 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
490 foreach($attributes as $attrs){
491 if(empty($obj[$attrs])){
492 $obj[$attrs] = array();
493 }
494 if(!is_array($obj[$attrs])){
495 $tmp[$attrs] = stripslashes($obj[$attrs]);
496 }else{
497 $tmp[$attrs] = $obj[$attrs];
498 }
499 }
501 $tmp['objectClass'] = $this->subClasses;
503 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
505 if($obj['status']=="new"){
506 $ldap->cat($sub_dn,array("objectClass"));
507 if($ldap->count()){
508 $obj['status']="edited";
509 }
510 }
512 /* Tag object */
513 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
515 if($obj['status'] == "delete"){
516 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
517 $this->handle_post_events("remove");
518 }elseif($obj['status'] == "edited"){
519 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
520 $this->handle_post_events("modify");
521 }elseif($obj['status']=="new"){
522 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
523 $this->handle_post_events("add");
524 }
525 }
526 }
529 function PrepareForCopyPaste($source)
530 {
531 plugin::PrepareForCopyPaste($source);
533 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
534 */
535 $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
536 foreach($res as $obj){
538 /* Skip not relevant objects */
539 if(!preg_match("/".normalizePreg($source['dn'])."$/i",$obj['dn'])) continue;
541 $objects = array();
542 $objects['status'] = "edited";
543 $objects['dn'] = $obj['dn'];
544 $objects = $this->get_object_attributes($objects,$this->subAttributes);
545 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
546 $this->SubObjects[$objects['cn']] = $objects;
547 }
548 }
551 /* Return plugin informations for acl handling */
552 static function plInfo()
553 {
554 return (array(
555 "plShortName" => _("Hook"),
556 "plDescription" => _("FAI hook"),
557 "plSelfModify" => FALSE,
558 "plDepends" => array(),
559 "plPriority" => 20,
560 "plSection" => array("administration"),
561 "plCategory" => array("fai"),
562 "plProvidedAcls" => array(
563 "cn" => _("Name")." ("._("Read only").")",
564 "description" => _("Description"))
565 ));
566 }
569 /*! \brief Used for copy & paste.
570 Returns a HTML input mask, which allows to change the cn of this entry.
571 @param Array Array containing current status && a HTML template.
572 */
573 function getCopyDialog()
574 {
575 $vars = array("cn");
576 $smarty = get_smarty();
577 $smarty->assign("cn", htmlentities($this->cn));
578 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
579 $ret = array();
580 $ret['string'] = $str;
581 $ret['status'] = "";
582 return($ret);
583 }
586 /*! \brief Used for copy & paste.
587 Some entries must be renamed to avaoid duplicate entries.
588 */
589 function saveCopyDialog()
590 {
591 if(isset($_POST['cn'])){
592 $this->cn = get_post('cn');
593 }
594 }
595 }
597 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
598 ?>