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[$_GET['getFAIHook']])){
203 $obj = $this->SubObjects[$_GET['getFAIHook']];
204 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
205 send_binary_content($obj['FAIscript'],$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[$_GET['id']])){
212 $obj = $this->SubObjects[$_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($this->FAIstate == "freeze"){
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=".$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=".$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")) && $this->FAIstate!="freeze");
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"){
387 if($use_dns){
388 if((isset($obj['description']))&&(!empty($obj['description']))){
389 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
390 }else{
391 $a_return[$obj['cn']]['name']= $obj['cn'];
392 }
393 $a_return[$obj['cn']]['dn']= $obj['dn'];
394 }else{
395 if((isset($obj['description']))&&(!empty($obj['description']))){
396 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
397 }else{
398 $a_return[$obj['cn']]= $obj['cn'];
399 }
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'])) && ($this->FAIstate != "freeze")){
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 return ($message);
457 }
460 /* Save to LDAP */
461 function save()
462 {
463 plugin::save();
465 $ldap = $this->config->get_ldap_link();
467 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
468 if (!$ldap->success()){
469 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
470 }
472 if($this->initially_was_account){
473 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
474 }else{
475 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
476 }
478 $ldap->cd($this->dn);
480 /* Prepare FAIscriptEntry to write it to ldap
481 * First sort array.
482 * Because we must delete old entries first.
483 * After deletion, we perform add and modify
484 */
485 $Objects = array();
487 /* We do not need to save untouched objects */
488 foreach($this->SubObjects as $name => $obj){
489 if($obj['status'] == "FreshLoaded"){
490 unset($this->SubObjects[$name]);
491 }
492 }
494 foreach($this->SubObjects as $name => $obj){
495 if($obj['status'] == "delete"){
496 $Objects[$name] = $obj;
497 }
498 }
499 foreach($this->SubObjects as $name => $obj){
500 if($obj['status'] != "delete"){
501 $Objects[$name] = $obj;
502 }
503 }
505 foreach($Objects as $name => $obj){
507 foreach($this->sub64coded as $codeIt){
508 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
509 }
511 $tmp = array();
512 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
513 foreach($attributes as $attrs){
514 if(empty($obj[$attrs])){
515 $obj[$attrs] = array();
516 }
517 if(!is_array($obj[$attrs])){
518 $tmp[$attrs] = stripslashes($obj[$attrs]);
519 }else{
520 $tmp[$attrs] = $obj[$attrs];
521 }
522 }
524 $tmp['objectClass'] = $this->subClasses;
526 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
528 if($obj['status']=="new"){
529 $ldap->cat($sub_dn,array("objectClass"));
530 if($ldap->count()){
531 $obj['status']="edited";
532 }
533 }
535 /* Tag object */
536 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
538 if($obj['status'] == "delete"){
539 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
540 $this->handle_post_events("remove");
541 }elseif($obj['status'] == "edited"){
542 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
543 $this->handle_post_events("modify");
544 }elseif($obj['status']=="new"){
545 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
546 $this->handle_post_events("add");
547 }
548 }
549 }
552 function PrepareForCopyPaste($source)
553 {
554 plugin::PrepareForCopyPaste($source);
556 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
557 */
558 $ldap = $this->config->get_ldap_link();
559 $ldap->cd ($source['dn']);
561 $attrs_to_search = $this->subAttributes;
562 $attrs_to_search[] = "FAIstate";
563 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
565 while($object = $ldap->fetch()){
567 /* Skip objects, that are tagged as removed */
568 if(isset($object['FAIstate'][0])){
569 if(preg_match("/removed$/",$object['FAIstate'][0])){
570 continue;
571 }
572 }
574 /* Set status for save management */
575 $objects = array();
576 $objects['status'] = "edited";
577 $objects['dn'] = $object['dn'];
578 $objects = $this->get_object_attributes($objects,$this->subAttributes);
579 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
581 $this->SubObjects[$objects['cn']] = $objects;
582 }
583 }
586 /* Return plugin informations for acl handling */
587 static function plInfo()
588 {
589 return (array(
590 "plShortName" => _("Hook"),
591 "plDescription" => _("FAI hook"),
592 "plSelfModify" => FALSE,
593 "plDepends" => array(),
594 "plPriority" => 20,
595 "plSection" => array("administration"),
596 "plCategory" => array("fai"),
597 "plProvidedAcls" => array(
598 "cn" => _("Name")." ("._("Read only").")",
599 "description" => _("Description"))
600 ));
601 }
602 }
604 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
605 ?>