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)){
154 $once = false;
155 $entry = preg_replace("/^deletescript_/","",$name);
156 $entry = base64_decode(preg_replace("/_.*/","",$entry));
158 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
159 $this->SubObjects[$entry]['status']= "delete";
160 }else{
161 unset($this->SubObjects[$entry]);
162 }
163 }
164 }
165 ///// Ende new list handling
167 /* Add new sub object */
168 if(isset($_POST['AddSubObject'])){
170 $c_dn = $this->dn;
171 if($c_dn == "new"){
172 $c_dn = $_SESSION['CurrentMainBase'];
173 }
175 $this->dialog= new $this->subClassName($this->config,"new");
176 $this->dialog->set_acl_base($c_dn);
177 $this->dialog->set_acl_category("fai");
178 $this->dialog->parent = &$this;
179 $this->is_dialog=true;
180 }
183 /* Save Dialog */
184 if(isset($_POST['SaveSubObject'])){
186 /* Perform post check*/
187 $this->dialog->save_object();
189 /* Get messages */
190 $msgs = $this->dialog->check();
192 /* print errors */
193 if(count($msgs)>0){
194 foreach($msgs as $msg){
195 print_red($msg);
196 }
197 }else{
199 /* Get return object */
200 $obj = $this->dialog->save();
201 if(isset($obj['remove'])){
203 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
205 /* Depending on status, set new status */
206 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
207 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
208 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
209 unset($this->SubObjects[$obj['remove']['from']]);
210 }
211 $obj['status'] = "new";
212 $this->SubObjects[$obj['remove']['to']] = $obj;
213 unset($this->SubObjects[$obj['remove']['to']]['remove']);
214 }else{
215 if($obj['status'] == "FreshLoaded"){
216 $obj['status'] = "edited";
217 }
218 $this->SubObjects[$obj['cn']]=$obj;
219 }
220 $this->is_dialog=false;
221 unset($this->dialog);
222 $this->dialog=NULL;
223 }
224 }
226 /* Sort entries */
227 $tmp = $keys = array();
228 foreach($this->SubObjects as $key => $entry){
229 $keys[$key]=$key;
230 }
231 natcasesort($keys);
232 foreach($keys as $key){
233 $tmp[$key]=$this->SubObjects[$key];
234 }
235 $this->SubObjects = $tmp;
237 /* Cancel Dialog */
238 if(isset($_POST['CancelSubObject'])){
239 $this->is_dialog=false;
240 unset($this->dialog);
241 $this->dialog=NULL;
242 }
244 /* Print dialog if $this->dialog is set */
245 if($this->dialog){
246 $this->dialog->save_object();
247 $display = $this->dialog->execute();
248 return($display);
249 }
253 /* Divlist added 28.02.2006
254 Containing FAIscripts
255 */
257 $divlist = new divSelectBox("FAIhooks");
258 $divlist->setHeight(400);
259 if($this->FAIstate == "freeze"){
260 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
261 $img_remo = "";
262 }else{
263 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
264 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
265 }
267 foreach($this->getList(true) as $key => $name){
269 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
270 $down = "";
271 }else{
272 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
273 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
274 </a>";
275 }
277 $divlist->AddEntry(array( array("string"=>$name['name']),
278 array("string"=>$down , "attach" => "style='width:20px;'"),
279 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
280 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
281 }
282 $smarty->assign("Entry_divlist",$divlist->DrawList());
283 /* Divlist creation complete
284 */
286 $smarty->assign("SubObjects",$this->getList());
288 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
289 * If we post the escaped strings they will be escaped again
290 */
291 foreach($this->attributes as $attrs){
292 if(get_magic_quotes_gpc()){
293 $smarty->assign($attrs,stripslashes($this->$attrs));
294 }else{
295 $smarty->assign($attrs,($this->$attrs));
296 }
297 }
299 $tmp = $this->plInfo();
300 $this->ui = get_userinfo();
302 $c_dn = $this->dn;
303 if($c_dn == "new"){
304 $c_dn = $_SESSION['CurrentMainBase'];
305 }
307 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
308 foreach($tmp['plProvidedAcls'] as $name => $translation){
309 $smarty->assign($name."ACL",$this->getacl($name));
310 }
312 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
313 return($display);
314 }
316 /* Generate listbox friendly SubObject list
317 */
318 function getList($use_dns=false){
319 $a_return=array();
320 foreach($this->SubObjects as $obj){
321 if($obj['status'] != "delete"){
322 if($use_dns){
323 if((isset($obj['description']))&&(!empty($obj['description']))){
324 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
325 }else{
326 $a_return[$obj['cn']]['name']= $obj['cn'];
327 }
328 $a_return[$obj['cn']]['dn']= $obj['dn'];
329 }else{
330 if((isset($obj['description']))&&(!empty($obj['description']))){
331 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
332 }else{
333 $a_return[$obj['cn']]= $obj['cn'];
334 }
335 }
336 }
337 }
338 return($a_return);
339 }
342 /* Delete me, and all my subtrees
343 */
344 function remove_from_parent()
345 {
346 $ldap = $this->config->get_ldap_link();
347 $ldap->cd ($this->dn);
349 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
350 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
351 if($_SESSION['faifilter']['branch'] == "main"){
352 $use_dn = $this->dn;
353 }
355 prepare_to_save_FAI_object($use_dn,array(),true);
357 foreach($this->SubObjects as $name => $obj){
358 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
359 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
360 if($_SESSION['faifilter']['branch'] == "main"){
361 $use_dn = $obj['dn'];
362 }
363 prepare_to_save_FAI_object($use_dn,array(),true);
364 }
365 $this->handle_post_events("remove");
366 }
369 /* Save data to object
370 */
371 function save_object()
372 {
373 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
374 plugin::save_object();
375 foreach($this->attributes as $attrs){
376 if(isset($_POST[$attrs])){
377 $this->$attrs = $_POST[$attrs];
378 }
379 }
380 }
381 }
384 /* Check supplied data */
385 function check()
386 {
387 /* Call common method to give check the hook */
388 $message= plugin::check();
390 return ($message);
391 }
394 /* Save to LDAP */
395 function save()
396 {
397 plugin::save();
399 $ldap = $this->config->get_ldap_link();
401 prepare_to_save_FAI_object($this->dn,$this->attrs);
402 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
404 /* Do object tagging */
405 $this->handle_object_tagging();
407 $ldap->cd($this->dn);
409 /* Prepare FAIscriptEntry to write it to ldap
410 * First sort array.
411 * Because we must delete old entries first.
412 * After deletion, we perform add and modify
413 */
414 $Objects = array();
416 /* We do not need to save untouched objects */
417 foreach($this->SubObjects as $name => $obj){
418 if($obj['status'] == "FreshLoaded"){
419 unset($this->SubObjects[$name]);
420 }
421 }
423 foreach($this->SubObjects as $name => $obj){
424 if($obj['status'] == "delete"){
425 $Objects[$name] = $obj;
426 }
427 }
428 foreach($this->SubObjects as $name => $obj){
429 if($obj['status'] != "delete"){
430 $Objects[$name] = $obj;
431 }
432 }
434 foreach($Objects as $name => $obj){
436 foreach($this->sub64coded as $codeIt){
437 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
438 }
440 $tmp = array();
441 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
442 foreach($attributes as $attrs){
443 if(empty($obj[$attrs])){
444 $obj[$attrs] = array();
445 }
446 if(!is_array($obj[$attrs])){
447 $tmp[$attrs] = stripslashes($obj[$attrs]);
448 }else{
449 $tmp[$attrs] = $obj[$attrs];
450 }
451 }
453 $tmp['objectClass'] = $this->subClasses;
455 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
457 if($obj['status']=="new"){
458 $ldap->cat($sub_dn,array("objectClass"));
459 if($ldap->count()){
460 $obj['status']="edited";
461 }
462 }
464 /* Check if gosaAdministrativeUnitTag is required as object class */
465 if($obj['status'] == "edited"){
466 $ldap->cat($sub_dn,array("objectClass"));
467 $attrs = $ldap->fetch();
468 if(isset($attrs['objectClass'])){
469 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
470 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
471 }
472 }
473 }
475 if($obj['status'] == "delete"){
476 prepare_to_save_FAI_object($sub_dn,array(),true);
477 $this->handle_post_events("remove");
478 }elseif($obj['status'] == "edited"){
479 prepare_to_save_FAI_object($sub_dn,$tmp);
480 $this->handle_post_events("modify");
481 }elseif($obj['status']=="new"){
482 prepare_to_save_FAI_object($sub_dn,$tmp);
483 $this->handle_post_events("add");
484 }
485 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
486 }
487 }
489 /* Return plugin informations for acl handling */
490 function plInfo()
491 {
492 return (array(
493 "plShortName" => _("Hook"),
494 "plDescription" => _("FAI hook"),
495 "plSelfModify" => FALSE,
496 "plDepends" => array(),
497 "plPriority" => 20,
498 "plSection" => array("administration"),
499 "plCategory" => array("fai"),
500 "plProvidedAcls" => array(
501 "cn" => _("Name")." ("._("Read only").")",
502 "description" => _("Description"))
503 ));
504 }
505 }
507 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
508 ?>