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 $this->acl ="#all#";
48 /* If "dn==new" we try to create a new entry
49 * Else we must read all objects from ldap which belong to this entry.
50 */
51 if($dn != "new"){
52 $this->dn =$dn;
54 /* Set acls
55 */
56 $ui = get_userinfo();
57 $acl = get_permissions ($this->dn, $ui->subtreeACL);
58 $acli = get_module_permission($acl, "FAIclass", $this->dn);
59 $this->acl=$acli;
61 /* Get FAIstate
62 */
63 if(isset($this->attrs['FAIstate'][0])){
64 $this->FAIstate = $this->attrs['FAIstate'][0];
65 }
67 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68 */
69 $ldap = $this->config->get_ldap_link();
70 $ldap->cd ($this->dn);
71 $attrs_to_search = $this->subAttributes;
72 $attrs_to_search[] = "FAIstate";
73 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", array("dn"));
75 while($object = $ldap->fetch()){
76 /* Set status for save management */
77 $objects = array();
78 $objects['status'] = "FreshLoaded";
79 $objects['dn'] = $object['dn'];
80 $objects = $this->get_object_attributes($objects,$this->subAttributes);
81 $this->SubObjects[$objects['cn']] = $objects;
82 }
83 }
84 }
87 /* Reload some attributes */
88 function get_object_attributes($object,$attributes)
89 {
90 $ldap = $this->config->get_ldap_link();
91 $ldap->cd($this->config->current['BASE']);
92 $ldap->cat($object['dn'],$attributes);
93 $tmp = $ldap->fetch();
95 foreach($attributes as $attrs){
96 if(isset($tmp[$attrs][0])){
97 $var = $tmp[$attrs][0];
99 /* Check if we must decode some attributes */
100 if(in_array_ics($attrs,$this->sub64coded)){
101 $var = base64_decode($var);
102 }
104 /* check if this is a binary entry */
105 if(in_array_ics($attrs,$this->subBinary)){
106 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
107 }
109 /* Fix slashes */
110 $var = addslashes($var);
111 $object[$attrs] = $var;
112 }
113 }
114 return($object);
115 }
118 function getUsedFAItask($cn)
119 {
120 $ret = array();
121 foreach($this->SubObjects as $name => $class){
122 if($class['cn'] == $cn){
123 continue;
124 }
125 if($class['status'] != "delete"){
126 $ret[$class['FAItask']] = $class['FAItask'];
127 }
128 }
129 return($ret);
130 }
132 function execute()
133 {
134 /* Call parent execute */
135 plugin::execute();
137 /* Fill templating stuff */
138 $smarty= get_smarty();
139 $display= "";
141 /* New Listhandling
142 */
143 $once = true;
144 foreach($_POST as $name => $value){
145 if(preg_match("/^editscript_/",$name)&&($once)){
146 $once = false;
147 $entry = preg_replace("/^editscript_/","",$name);
148 $entry = base64_decode(preg_replace("/_.*/","",$entry));
150 $obj = $this->SubObjects[$entry];
151 if($obj['status'] == "FreshLoaded"){
152 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
153 }
154 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
157 $_SESSION['objectinfo'] = $obj['dn'];
158 $this->dialog->parent = &$this;
159 $this->is_dialog=true;
160 }
161 if(preg_match("/^deletescript_/",$name)&&($once)){
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 ///// Ende new list handling
175 /* Add new sub object */
176 if(isset($_POST['AddSubObject'])){
177 $this->dialog= new $this->subClassName($this->config,"new");
178 $this->dialog->acl = $this->acl;
179 $this->dialog->parent = &$this;
180 $this->is_dialog=true;
181 }
184 /* Save Dialog */
185 if(isset($_POST['SaveSubObject'])){
187 /* Perform post check*/
188 $this->dialog->save_object();
190 /* Get messages */
191 $msgs = $this->dialog->check();
193 /* print errors */
194 if(count($msgs)>0){
195 foreach($msgs as $msg){
196 print_red($msg);
197 }
198 }else{
200 /* Get return object */
201 $obj = $this->dialog->save();
202 if(isset($obj['remove'])){
204 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
206 /* Depending on status, set new status */
207 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
208 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
209 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
210 unset($this->SubObjects[$obj['remove']['from']]);
211 }
212 $obj['status'] = "new";
213 $this->SubObjects[$obj['remove']['to']] = $obj;
214 unset($this->SubObjects[$obj['remove']['to']]['remove']);
215 }else{
216 if($obj['status'] == "FreshLoaded"){
217 $obj['status'] = "edited";
218 }
219 $this->SubObjects[$obj['cn']]=$obj;
220 }
221 $this->is_dialog=false;
222 unset($this->dialog);
223 $this->dialog=NULL;
224 }
225 }
227 /* Sort entries */
228 $tmp = $keys = array();
229 foreach($this->SubObjects as $key => $entry){
230 $keys[$key]=$key;
231 }
232 natcasesort($keys);
233 foreach($keys as $key){
234 $tmp[$key]=$this->SubObjects[$key];
235 }
236 $this->SubObjects = $tmp;
238 /* Cancel Dialog */
239 if(isset($_POST['CancelSubObject'])){
240 $this->is_dialog=false;
241 unset($this->dialog);
242 $this->dialog=NULL;
243 }
245 /* Print dialog if $this->dialog is set */
246 if($this->dialog){
247 $this->dialog->save_object();
248 $display = $this->dialog->execute();
249 return($display);
250 }
254 /* Divlist added 28.02.2006
255 Containing FAIscripts
256 */
258 $divlist = new divSelectBox("FAIhooks");
259 $divlist->setHeight(400);
260 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
261 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
262 $img_remo = "";
263 }else{
264 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
265 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
266 }
268 foreach($this->getList(true) as $key => $name){
270 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
271 $down = "";
272 }else{
273 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
274 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
275 </a>";
276 }
278 $divlist->AddEntry(array( array("string"=>$name['name']),
279 array("string"=>$down , "attach" => "style='width:20px;'"),
280 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
281 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
282 }
283 $smarty->assign("Entry_divlist",$divlist->DrawList());
284 /* Divlist creation complete
285 */
287 $smarty->assign("SubObjects",$this->getList());
289 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
290 * If we post the escaped strings they will be escaped again
291 */
292 foreach($this->attributes as $attrs){
293 if(get_magic_quotes_gpc()){
294 $smarty->assign($attrs,stripslashes($this->$attrs));
295 }else{
296 $smarty->assign($attrs,($this->$attrs));
297 }
298 }
300 foreach($this->attributes as $attr){
301 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
302 }
304 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
305 return($display);
306 }
308 /* Generate listbox friendly SubObject list
309 */
310 function getList($use_dns=false){
311 $a_return=array();
312 foreach($this->SubObjects as $obj){
313 if($obj['status'] != "delete"){
314 if($use_dns){
315 if((isset($obj['description']))&&(!empty($obj['description']))){
316 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
317 }else{
318 $a_return[$obj['cn']]['name']= $obj['cn'];
319 }
320 $a_return[$obj['cn']]['dn']= $obj['dn'];
321 }else{
322 if((isset($obj['description']))&&(!empty($obj['description']))){
323 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
324 }else{
325 $a_return[$obj['cn']]= $obj['cn'];
326 }
327 }
328 }
329 }
330 return($a_return);
331 }
334 /* Delete me, and all my subtrees
335 */
336 function remove_from_parent()
337 {
338 $ldap = $this->config->get_ldap_link();
339 $ldap->cd ($this->dn);
341 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
342 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
343 if($_SESSION['faifilter']['branch'] == "main"){
344 $use_dn = $this->dn;
345 }
347 prepare_to_save_FAI_object($use_dn,array(),true);
349 foreach($this->SubObjects as $name => $obj){
350 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
351 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
352 if($_SESSION['faifilter']['branch'] == "main"){
353 $use_dn = $obj['dn'];
354 }
355 prepare_to_save_FAI_object($use_dn,array(),true);
356 }
357 $this->handle_post_events("remove");
358 }
361 /* Save data to object
362 */
363 function save_object()
364 {
365 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
366 plugin::save_object();
367 foreach($this->attributes as $attrs){
368 if(isset($_POST[$attrs])){
369 $this->$attrs = $_POST[$attrs];
370 }
371 }
372 }
373 }
376 /* Check supplied data */
377 function check()
378 {
379 /* Call common method to give check the hook */
380 $message= plugin::check();
382 return ($message);
383 }
386 /* Save to LDAP */
387 function save()
388 {
389 plugin::save();
391 $ldap = $this->config->get_ldap_link();
393 prepare_to_save_FAI_object($this->dn,$this->attrs);
394 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
396 /* Do object tagging */
397 $this->handle_object_tagging();
399 $ldap->cd($this->dn);
401 /* Prepare FAIscriptEntry to write it to ldap
402 * First sort array.
403 * Because we must delete old entries first.
404 * After deletion, we perform add and modify
405 */
406 $Objects = array();
408 /* We do not need to save untouched objects */
409 foreach($this->SubObjects as $name => $obj){
410 if($obj['status'] == "FreshLoaded"){
411 unset($this->SubObjects[$name]);
412 }
413 }
415 foreach($this->SubObjects as $name => $obj){
416 if($obj['status'] == "delete"){
417 $Objects[$name] = $obj;
418 }
419 }
420 foreach($this->SubObjects as $name => $obj){
421 if($obj['status'] != "delete"){
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(empty($obj[$attrs])){
436 $obj[$attrs] = array();
437 }
438 if(!is_array($obj[$attrs])){
439 $tmp[$attrs] = stripslashes($obj[$attrs]);
440 }else{
441 $tmp[$attrs] = $obj[$attrs];
442 }
443 }
445 $tmp['objectClass'] = $this->subClasses;
447 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
449 if($obj['status']=="new"){
450 $ldap->cat($sub_dn,array("objectClass"));
451 if($ldap->count()){
452 $obj['status']="edited";
453 }
454 }
456 /* Check if gosaAdministrativeUnitTag is required as object class */
457 if($obj['status'] == "edited"){
458 $ldap->cat($sub_dn,array("objectClass"));
459 $attrs = $ldap->fetch();
460 if(isset($attrs['objectClass'])){
461 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
462 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
463 }
464 }
465 }
467 if($obj['status'] == "delete"){
468 prepare_to_save_FAI_object($sub_dn,array(),true);
469 $this->handle_post_events("remove");
470 }elseif($obj['status'] == "edited"){
471 prepare_to_save_FAI_object($sub_dn,$tmp);
472 $this->handle_post_events("modify");
473 }elseif($obj['status']=="new"){
474 prepare_to_save_FAI_object($sub_dn,$tmp);
475 $this->handle_post_events("add");
476 }
477 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
478 }
479 }
481 /* Return plugin informations for acl handling */
482 function plInfo()
483 {
484 return (array(
485 "plShortName" => _("Hook"),
486 "plDescription" => _("FAI hook"),
487 "plSelfModify" => FALSE,
488 "plDepends" => array(),
489 "plPriority" => 0,
490 "plSection" => array("administration"),
491 "plCategory" => array("fai"),
492 "plProvidedAcls" => array(
493 "cn" => _("Name"),
494 "description" => _("Description"),
495 "FAItast" => _("Task"),
496 "FAIscript" => _("FAI script"))
497 ));
498 }
499 }
501 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
502 ?>