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']." [".$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']." [".$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 if($_SESSION['faifilter']['branch'] == "main"){
343 $use_dn = $this->dn;
344 }
346 prepare_to_save_FAI_object($use_dn,array(),true);
348 foreach($this->SubObjects as $name => $obj){
349 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
350 if($_SESSION['faifilter']['branch'] == "main"){
351 $use_dn = $obj['dn'];
352 }
353 prepare_to_save_FAI_object($use_dn,array(),true);
354 }
355 $this->handle_post_events("remove");
356 }
359 /* Save data to object
360 */
361 function save_object()
362 {
363 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
364 plugin::save_object();
365 foreach($this->attributes as $attrs){
366 if(isset($_POST[$attrs])){
367 $this->$attrs = $_POST[$attrs];
368 }
369 }
370 }
371 }
374 /* Check supplied data */
375 function check()
376 {
377 /* Call common method to give check the hook */
378 $message= plugin::check();
380 return ($message);
381 }
384 /* Save to LDAP */
385 function save()
386 {
387 plugin::save();
389 $ldap = $this->config->get_ldap_link();
391 prepare_to_save_FAI_object($this->dn,$this->attrs);
392 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
394 /* Do object tagging */
395 $this->handle_object_tagging();
397 $ldap->cd($this->dn);
399 /* Prepare FAIscriptEntry to write it to ldap
400 * First sort array.
401 * Because we must delete old entries first.
402 * After deletion, we perform add and modify
403 */
404 $Objects = array();
406 /* We do not need to save untouched objects */
407 foreach($this->SubObjects as $name => $obj){
408 if($obj['status'] == "FreshLoaded"){
409 unset($this->SubObjects[$name]);
410 }
411 }
413 foreach($this->SubObjects as $name => $obj){
414 if($obj['status'] == "delete"){
415 $Objects[$name] = $obj;
416 }
417 }
418 foreach($this->SubObjects as $name => $obj){
419 if($obj['status'] != "delete"){
420 $Objects[$name] = $obj;
421 }
422 }
424 foreach($Objects as $name => $obj){
426 foreach($this->sub64coded as $codeIt){
427 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
428 }
430 $tmp = array();
431 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
432 foreach($attributes as $attrs){
433 if(empty($obj[$attrs])){
434 $obj[$attrs] = array();
435 }
436 if(!is_array($obj[$attrs])){
437 $tmp[$attrs] = stripslashes($obj[$attrs]);
438 }else{
439 $tmp[$attrs] = $obj[$attrs];
440 }
441 }
443 $tmp['objectClass'] = $this->subClasses;
445 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
447 if($obj['status']=="new"){
448 $ldap->cat($sub_dn,array("objectClass"));
449 if($ldap->count()){
450 $obj['status']="edited";
451 }
452 }
454 /* Check if gosaAdministrativeUnitTag is required as object class */
455 if($obj['status'] == "edited"){
456 $ldap->cat($sub_dn,array("objectClass"));
457 $attrs = $ldap->fetch();
458 if(isset($attrs['objectClass'])){
459 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
460 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
461 }
462 }
463 }
465 if($obj['status'] == "delete"){
466 prepare_to_save_FAI_object($sub_dn,array(),true);
467 $this->handle_post_events("remove");
468 }elseif($obj['status'] == "edited"){
469 prepare_to_save_FAI_object($sub_dn,$tmp);
470 $this->handle_post_events("modify");
471 }elseif($obj['status']=="new"){
472 prepare_to_save_FAI_object($sub_dn,$tmp);
473 $this->handle_post_events("add");
474 }
475 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
476 }
477 }
479 /* Return plugin informations for acl handling */
480 function plInfo()
481 {
482 return (array(
483 "plShortName" => _("Hook"),
484 "plDescription" => _("FAI hook"),
485 "plSelfModify" => FALSE,
486 "plDepends" => array(),
487 "plPriority" => 0,
488 "plSection" => array("administration"),
489 "plCategory" => array("fai"),
490 "plProvidedAcls" => array(
491 "cn" => _("Name"),
492 "description" => _("Description"),
493 "FAItast" => _("Task"),
494 "FAIscript" => _("FAI script"))
495 ));
496 }
497 }
499 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
500 ?>