1 <?php
3 class faiHook extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIhook");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIhookEntry";
21 var $subClasses = array("top","FAIclass","FAIhookEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiHookEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAItask");
28 var $sub_Load_Later = array("FAIscript");
29 var $sub64coded = array();
30 var $subBinary = array("FAIscript");
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIstate = "";
41 function faiHook ($config, $dn= NULL)
42 {
43 /* Load Attributes */
44 plugin::plugin ($config, $dn);
46 /* If "dn==new" we try to create a new entry
47 * Else we must read all objects from ldap which belong to this entry.
48 */
49 if($dn != "new"){
50 $this->dn =$dn;
52 /* Get FAIstate
53 */
54 if(isset($this->attrs['FAIstate'][0])){
55 $this->FAIstate = $this->attrs['FAIstate'][0];
56 }
58 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
59 */
60 $ldap = $this->config->get_ldap_link();
61 $ldap->cd ($this->dn);
62 $attrs_to_search = $this->subAttributes;
63 $attrs_to_search[] = "FAIstate";
64 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", array("dn"));
66 while($object = $ldap->fetch()){
67 /* Set status for save management */
68 $objects = array();
69 $objects['status'] = "FreshLoaded";
70 $objects['dn'] = $object['dn'];
71 $objects = $this->get_object_attributes($objects,$this->subAttributes);
72 $this->SubObjects[$objects['cn']] = $objects;
73 }
74 }
75 }
78 /* Reload some attributes */
79 function get_object_attributes($object,$attributes)
80 {
81 $ldap = $this->config->get_ldap_link();
82 $ldap->cd($this->config->current['BASE']);
83 $ldap->cat($object['dn'],$attributes);
84 $tmp = $ldap->fetch();
86 foreach($attributes as $attrs){
87 if(isset($tmp[$attrs][0])){
88 $var = $tmp[$attrs][0];
90 /* Check if we must decode some attributes */
91 if(in_array_ics($attrs,$this->sub64coded)){
92 $var = base64_decode($var);
93 }
95 /* check if this is a binary entry */
96 if(in_array_ics($attrs,$this->subBinary)){
97 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
98 }
100 /* Fix slashes */
101 $var = addslashes($var);
102 $object[$attrs] = $var;
103 }
104 }
105 return($object);
106 }
109 function getUsedFAItask($cn)
110 {
111 $ret = array();
112 foreach($this->SubObjects as $name => $class){
113 if($class['cn'] == $cn){
114 continue;
115 }
116 if($class['status'] != "delete"){
117 $ret[$class['FAItask']] = $class['FAItask'];
118 }
119 }
120 return($ret);
121 }
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
128 /* Fill templating stuff */
129 $smarty= get_smarty();
130 $display= "";
132 /* New Listhandling
133 */
134 $once = true;
135 foreach($_POST as $name => $value){
136 if(preg_match("/^editscript_/",$name)&&($once)){
137 $once = false;
138 $entry = preg_replace("/^editscript_/","",$name);
139 $entry = base64_decode(preg_replace("/_.*/","",$entry));
141 $obj = $this->SubObjects[$entry];
142 if($obj['status'] == "FreshLoaded"){
143 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
144 }
145 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
146 $this->dialog->set_acl_base($this->dn);
147 $this->dialog->set_acl_category("fai");
149 $_SESSION['objectinfo'] = $obj['dn'];
150 $this->dialog->parent = &$this;
151 $this->is_dialog=true;
152 }
153 if(preg_match("/^deletescript_/",$name)&&($once)){
155 $dn= $name['dn'];
156 if($dn == "new"){
157 $dn = $_SESSION['CurrentMainBase'];
158 }
159 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
161 if(preg_match("/d/",$acl)){
162 $once = false;
163 $entry = preg_replace("/^deletescript_/","",$name);
164 $entry = base64_decode(preg_replace("/_.*/","",$entry));
166 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
167 $this->SubObjects[$entry]['status']= "delete";
168 }else{
169 unset($this->SubObjects[$entry]);
170 }
171 }
172 }
173 }
174 ///// Ende new list handling
176 /* Add new sub object */
177 if(isset($_POST['AddSubObject'])){
179 $c_dn = $this->dn;
180 if($c_dn == "new"){
181 $c_dn = $_SESSION['CurrentMainBase'];
182 }
184 $this->dialog= new $this->subClassName($this->config,"new");
185 $this->dialog->set_acl_base($c_dn);
186 $this->dialog->set_acl_category("fai");
187 $this->dialog->parent = &$this;
188 $this->is_dialog=true;
189 }
192 /* Save Dialog */
193 if(isset($_POST['SaveSubObject'])){
195 /* Perform post check*/
196 $this->dialog->save_object();
198 /* Get messages */
199 $msgs = $this->dialog->check();
201 /* print errors */
202 if(count($msgs)>0){
203 foreach($msgs as $msg){
204 print_red($msg);
205 }
206 }else{
208 /* Get return object */
209 $obj = $this->dialog->save();
210 if(isset($obj['remove'])){
212 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
214 /* Depending on status, set new status */
215 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
216 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
217 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
218 unset($this->SubObjects[$obj['remove']['from']]);
219 }
220 $obj['status'] = "new";
221 $this->SubObjects[$obj['remove']['to']] = $obj;
222 unset($this->SubObjects[$obj['remove']['to']]['remove']);
223 }else{
224 if($obj['status'] == "FreshLoaded"){
225 $obj['status'] = "edited";
226 }
227 $this->SubObjects[$obj['cn']]=$obj;
228 }
229 $this->is_dialog=false;
230 unset($this->dialog);
231 $this->dialog=NULL;
232 }
233 }
235 /* Sort entries */
236 $tmp = $keys = array();
237 foreach($this->SubObjects as $key => $entry){
238 $keys[$key]=$key;
239 }
240 natcasesort($keys);
241 foreach($keys as $key){
242 $tmp[$key]=$this->SubObjects[$key];
243 }
244 $this->SubObjects = $tmp;
246 /* Cancel Dialog */
247 if(isset($_POST['CancelSubObject'])){
248 $this->is_dialog=false;
249 unset($this->dialog);
250 $this->dialog=NULL;
251 }
253 /* Print dialog if $this->dialog is set */
254 if($this->dialog){
255 $this->dialog->save_object();
256 $display = $this->dialog->execute();
257 return($display);
258 }
262 /* Divlist added 28.02.2006
263 Containing FAIscripts
264 */
266 $divlist = new divSelectBox("FAIhooks");
267 $divlist->setHeight(400);
269 foreach($this->getList(true) as $key => $name){
271 $dn= $name['dn'];
272 if($dn == "new"){
273 $dn = $_SESSION['CurrentMainBase'];
274 }
275 $ui = get_userinfo();
276 $acl = $ui->get_permissions($dn,"fai/faiScriptEntry") ;
277 $act = "";
279 if($this->FAIstate == "freeze"){
280 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
281 }else{
282 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
283 if(preg_match("/d/",$acl)){
284 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
285 }
286 }
289 $s_acl = $ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
290 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
291 $down = "";
292 }else{
293 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
294 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
295 </a>";
296 }
298 $divlist->AddEntry(array( array("string"=>$name['name']),
299 array("string"=>$down , "attach" => "style='width:20px;'"),
300 array("string"=>str_replace("%s",base64_encode($key),$act),
301 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
302 }
303 $smarty->assign("Entry_divlist",$divlist->DrawList());
304 /* Divlist creation complete
305 */
307 $smarty->assign("SubObjects",$this->getList());
309 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
310 * If we post the escaped strings they will be escaped again
311 */
312 foreach($this->attributes as $attrs){
313 if(get_magic_quotes_gpc()){
314 $smarty->assign($attrs,stripslashes($this->$attrs));
315 }else{
316 $smarty->assign($attrs,($this->$attrs));
317 }
318 }
320 $tmp = $this->plInfo();
321 $this->ui = get_userinfo();
323 $c_dn = $this->dn;
324 if($c_dn == "new"){
325 $c_dn = $_SESSION['CurrentMainBase'];
326 }
328 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
329 foreach($tmp['plProvidedAcls'] as $name => $translation){
330 $smarty->assign($name."ACL",$this->getacl($name));
331 }
333 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
334 return($display);
335 }
337 /* Generate listbox friendly SubObject list
338 */
339 function getList($use_dns=false){
340 $a_return=array();
341 foreach($this->SubObjects as $obj){
342 if($obj['status'] != "delete"){
343 if($use_dns){
344 if((isset($obj['description']))&&(!empty($obj['description']))){
345 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
346 }else{
347 $a_return[$obj['cn']]['name']= $obj['cn'];
348 }
349 $a_return[$obj['cn']]['dn']= $obj['dn'];
350 }else{
351 if((isset($obj['description']))&&(!empty($obj['description']))){
352 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
353 }else{
354 $a_return[$obj['cn']]= $obj['cn'];
355 }
356 }
357 }
358 }
359 return($a_return);
360 }
363 /* Delete me, and all my subtrees
364 */
365 function remove_from_parent()
366 {
367 $ldap = $this->config->get_ldap_link();
368 $ldap->cd ($this->dn);
370 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
371 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
372 if($_SESSION['faifilter']['branch'] == "main"){
373 $use_dn = $this->dn;
374 }
376 prepare_to_save_FAI_object($use_dn,array(),true);
378 foreach($this->SubObjects as $name => $obj){
379 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
380 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
381 if($_SESSION['faifilter']['branch'] == "main"){
382 $use_dn = $obj['dn'];
383 }
384 prepare_to_save_FAI_object($use_dn,array(),true);
385 }
386 $this->handle_post_events("remove");
387 }
390 /* Save data to object
391 */
392 function save_object()
393 {
394 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
395 plugin::save_object();
396 foreach($this->attributes as $attrs){
397 if(isset($_POST[$attrs])){
398 $this->$attrs = $_POST[$attrs];
399 }
400 }
401 }
402 }
405 /* Check supplied data */
406 function check()
407 {
408 /* Call common method to give check the hook */
409 $message= plugin::check();
411 return ($message);
412 }
415 /* Save to LDAP */
416 function save()
417 {
418 plugin::save();
420 $ldap = $this->config->get_ldap_link();
422 prepare_to_save_FAI_object($this->dn,$this->attrs);
423 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
425 /* Do object tagging */
426 $this->handle_object_tagging();
428 $ldap->cd($this->dn);
430 /* Prepare FAIscriptEntry to write it to ldap
431 * First sort array.
432 * Because we must delete old entries first.
433 * After deletion, we perform add and modify
434 */
435 $Objects = array();
437 /* We do not need to save untouched objects */
438 foreach($this->SubObjects as $name => $obj){
439 if($obj['status'] == "FreshLoaded"){
440 unset($this->SubObjects[$name]);
441 }
442 }
444 foreach($this->SubObjects as $name => $obj){
445 if($obj['status'] == "delete"){
446 $Objects[$name] = $obj;
447 }
448 }
449 foreach($this->SubObjects as $name => $obj){
450 if($obj['status'] != "delete"){
451 $Objects[$name] = $obj;
452 }
453 }
455 foreach($Objects as $name => $obj){
457 foreach($this->sub64coded as $codeIt){
458 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
459 }
461 $tmp = array();
462 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
463 foreach($attributes as $attrs){
464 if(empty($obj[$attrs])){
465 $obj[$attrs] = array();
466 }
467 if(!is_array($obj[$attrs])){
468 $tmp[$attrs] = stripslashes($obj[$attrs]);
469 }else{
470 $tmp[$attrs] = $obj[$attrs];
471 }
472 }
474 $tmp['objectClass'] = $this->subClasses;
476 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
478 if($obj['status']=="new"){
479 $ldap->cat($sub_dn,array("objectClass"));
480 if($ldap->count()){
481 $obj['status']="edited";
482 }
483 }
485 /* Check if gosaAdministrativeUnitTag is required as object class */
486 if($obj['status'] == "edited"){
487 $ldap->cat($sub_dn,array("objectClass"));
488 $attrs = $ldap->fetch();
489 if(isset($attrs['objectClass'])){
490 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
491 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
492 }
493 }
494 }
496 if($obj['status'] == "delete"){
497 prepare_to_save_FAI_object($sub_dn,array(),true);
498 $this->handle_post_events("remove");
499 }elseif($obj['status'] == "edited"){
500 prepare_to_save_FAI_object($sub_dn,$tmp);
501 $this->handle_post_events("modify");
502 }elseif($obj['status']=="new"){
503 prepare_to_save_FAI_object($sub_dn,$tmp);
504 $this->handle_post_events("add");
505 }
506 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
507 }
508 }
510 /* Return plugin informations for acl handling */
511 function plInfo()
512 {
513 return (array(
514 "plShortName" => _("Hook"),
515 "plDescription" => _("FAI hook"),
516 "plSelfModify" => FALSE,
517 "plDepends" => array(),
518 "plPriority" => 20,
519 "plSection" => array("administration"),
520 "plCategory" => array("fai"),
521 "plProvidedAcls" => array(
522 "cn" => _("Name")." ("._("Read only").")",
523 "description" => _("Description"))
524 ));
525 }
526 }
528 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
529 ?>