3
class faiHook extends plugin
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 $dialog = NULL; // a dialog, e.g. new disk dialog
32
var $SubObjects = array(); // All leafobjects of this object
37
var $copy_paste_mode = false;
38
var $cut_paste_mode = false;
40
var $CopyPasteVars = array("SubObjects");
42
function faiHook ($config, $dn= NULL)
45
plugin::plugin ($config, $dn);
49
/* If "dn==new" we try to create a new entry
50
* Else we must read all objects from ldap which belong to this entry.
58
$acl = get_permissions ($this->dn, $ui->subtreeACL);
59
$acli = get_module_permission($acl, "FAIclass", $this->dn);
64
if(isset($this->attrs['FAIstate'][0])){
65
$this->FAIstate = $this->attrs['FAIstate'][0];
68
/* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
70
$ldap = $this->config->get_ldap_link();
71
$ldap->cd ($this->dn);
72
$ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
75
while($object = $ldap->fetch()){
78
foreach($data as $object){
80
/* Set status for save management */
82
$objects['status'] = "FreshLoaded";
83
$objects['dn'] = $object['dn'];
84
$objects = $this->get_object_attributes($objects,$this->subAttributes);
85
$this->SubObjects[$objects['cn']] = $objects;
91
/* Reload some attributes */
92
function get_object_attributes($object,$attributes)
94
$ldap = $this->config->get_ldap_link();
95
$ldap->cd($this->config->current['BASE']);
96
$ldap->cat($object['dn'],$attributes);
97
$tmp = $ldap->fetch();
99
foreach($attributes as $attrs){
100
if(isset($tmp[$attrs][0])){
101
$var = $tmp[$attrs][0];
103
/* Check if we must decode some attributes */
104
if(in_array_ics($attrs,$this->sub64coded)){
105
$var = base64_decode($var);
108
/* check if this is a binary entry */
109
if(in_array_ics($attrs,$this->subBinary)){
110
$var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
114
$var = addslashes($var);
115
$object[$attrs] = $var;
122
function getUsedFAItask($cn)
125
foreach($this->SubObjects as $name => $class){
126
if($class['cn'] == $cn){
129
if($class['status'] != "delete"){
130
$ret[$class['FAItask']] = $class['FAItask'];
139
/* Call parent execute */
142
/* Fill templating stuff */
143
$smarty= get_smarty();
149
foreach($_POST as $name => $value){
150
if(preg_match("/^editscript_/",$name)&&($once)){
152
$entry = preg_replace("/^editscript_/","",$name);
153
$entry = base64_decode(preg_replace("/_.*/","",$entry));
155
$obj = $this->SubObjects[$entry];
156
if($obj['status'] == "FreshLoaded"){
157
$obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
160
$this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
161
$this->dialog->acl = $this->acl;
162
$_SESSION['objectinfo'] = $obj['dn'];
163
$this->dialog->parent = &$this;
164
$this->is_dialog=true;
166
if(preg_match("/^deletescript_/",$name)&&($once)){
168
$entry = preg_replace("/^deletescript_/","",$name);
169
$entry = base64_decode(preg_replace("/_.*/","",$entry));
171
$status = $this->SubObjects[$entry]['status'];
172
if($status == "edited" || $status == "FreshLoaded"){
173
$this->SubObjects[$entry]['status']= "delete";
175
unset($this->SubObjects[$entry]);
180
/* Edit entries via GET */
181
if(isset($_GET['act']) && isset($_GET['id'])){
182
if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
183
$obj = $this->SubObjects[$_GET['id']];
184
if($obj['status'] == "FreshLoaded"){
185
$obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
187
$this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
188
$this->dialog->acl = $this->acl;
189
$_SESSION['objectinfo'] = $obj['dn'];
190
$this->dialog->parent = &$this;
191
$this->is_dialog=true;
195
///// Ende new list handling
197
/* Add new sub object */
198
if(isset($_POST['AddSubObject'])){
199
$this->dialog= new $this->subClassName($this->config,"new");
200
$this->dialog->acl = $this->acl;
201
$this->dialog->parent = &$this;
202
$this->is_dialog=true;
205
if($this->dn != "new"){
206
$_SESSION['objectinfo']= $this->dn;
210
if(isset($_POST['SaveSubObject'])){
212
/* Perform post check*/
213
$this->dialog->save_object();
216
$msgs = $this->dialog->check();
220
foreach($msgs as $msg){
225
/* Get return object */
226
$obj = $this->dialog->save();
227
if(isset($obj['remove'])){
228
$old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
230
/* Depending on status, set new status */
231
if($old_stat == "edited" || $old_stat == "FreshLoaded"){
232
$this->SubObjects[$obj['remove']['from']]['status'] = "delete";
233
}elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
234
unset($this->SubObjects[$obj['remove']['from']]);
236
$obj['status'] = "new";
237
$this->SubObjects[$obj['remove']['to']] = $obj;
238
unset($this->SubObjects[$obj['remove']['to']]['remove']);
240
if($obj['status'] == "FreshLoaded"){
241
$obj['status'] = "edited";
243
$this->SubObjects[$obj['cn']]=$obj;
246
$this->is_dialog=false;
247
unset($this->dialog);
253
$tmp = $keys = array();
254
foreach($this->SubObjects as $key => $entry){
258
foreach($keys as $key){
259
$tmp[$key]=$this->SubObjects[$key];
261
$this->SubObjects = $tmp;
264
if(isset($_POST['CancelSubObject'])){
265
$this->is_dialog=false;
266
unset($this->dialog);
270
/* Print dialog if $this->dialog is set */
272
$this->dialog->save_object();
273
$display = $this->dialog->execute();
279
/* Divlist added 28.02.2006
280
Containing FAIscripts
283
$divlist = new divSelectBox("FAIhooks");
284
$divlist->setHeight(400);
285
if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
286
$img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
289
$img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
290
$img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
293
foreach($this->getList(true) as $key => $name){
295
if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
298
$down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
299
<img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
303
$edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name['name']."</a>";
305
$divlist->AddEntry(array( array("string"=>$edit_link),
306
array("string"=>$down , "attach" => "style='width:20px;'"),
307
array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
308
"attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
310
$smarty->assign("Entry_divlist",$divlist->DrawList());
311
/* Divlist creation complete
314
$smarty->assign("SubObjects",$this->getList());
316
/* Magic quotes GPC, escapes every ' " \, to solve some security risks
317
* If we post the escaped strings they will be escaped again
319
foreach($this->attributes as $attrs){
320
if(get_magic_quotes_gpc()){
321
$smarty->assign($attrs,stripslashes($this->$attrs));
323
$smarty->assign($attrs,($this->$attrs));
327
foreach($this->attributes as $attr){
328
$smarty->assign($attr."ACL",chkacl($this->acl,$attr));
331
$display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
335
/* Generate listbox friendly SubObject list
337
function getList($use_dns=false){
339
foreach($this->SubObjects as $obj){
340
if($obj['status'] != "delete"){
342
if((isset($obj['description']))&&(!empty($obj['description']))){
343
$a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
345
$a_return[$obj['cn']]['name']= $obj['cn'];
347
$a_return[$obj['cn']]['dn']= $obj['dn'];
349
if((isset($obj['description']))&&(!empty($obj['description']))){
350
$a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
352
$a_return[$obj['cn']]= $obj['cn'];
361
/* Delete me, and all my subtrees
363
function remove_from_parent()
365
$ldap = $this->config->get_ldap_link();
366
$ldap->cd ($this->dn);
367
$ldap->rmdir_recursive($this->dn);
368
show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed"));
369
$this->handle_post_events("remove");
373
/* Save data to object
375
function save_object()
377
if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
378
plugin::save_object();
379
foreach($this->attributes as $attrs){
380
if(isset($_POST[$attrs])){
381
$this->$attrs = $_POST[$attrs];
388
/* Check supplied data */
391
/* Call common method to give check the hook */
392
$message= plugin::check();
394
/* If this is a new script, check if a script with this name already exists */
395
if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
397
/* Check if current name is already used for fai scripts in selected release */
398
$dn = 'cn='.$this->cn.",ou=hooks,".$this->release;
399
$ldap = $this->config->get_ldap_link();
403
$r =convert_department_dn($this->release);;
404
$message[] = sprintf(_("Can't insert a fai hook named '%s' in '%s' there is already a hook with the given name."),$this->cn,$r);
416
$ldap = $this->config->get_ldap_link();
418
/* Copy & Paste : Ensure that FAIstate is copied too */
419
if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
420
$this->attrs['FAIstate'] = $this->FAIstate;
423
$ldap->cat($this->dn,array("objectClass"));
424
if($ldap->count()!=0){
425
/* Write FAIscript to ldap*/
426
$ldap->cd($this->dn);
428
$ldap->modify ($this->attrs);
431
/* Write FAIscript to ldap*/
432
$ldap->cd($this->config->current['BASE']);
433
$ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
434
$ldap->cd($this->dn);
435
$ldap->add($this->attrs);
437
show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed"));
439
$ldap->cd($this->dn);
441
/* Prepare FAIscriptEntry to write it to ldap
443
* Because we must delete old entries first.
444
* After deletion, we perform add and modify
449
/* We do not need to save untouched objects */
450
foreach($this->SubObjects as $name => $obj){
451
if($obj['status'] == "FreshLoaded"){
452
if($this->copy_paste_mode){
453
$this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
454
$this->SubObjects[$name]['status'] = "new";
456
unset($this->SubObjects[$name]);
461
/* Add objects that must be removed first.*/
462
foreach($this->SubObjects as $name => $obj){
463
if($obj['status'] == "delete"){
464
$Objects[$name] = $obj;
468
/* Add objects to add/modify */
469
foreach($this->SubObjects as $name => $obj){
470
if($obj['status'] != "delete"){
471
$Objects[$name] = $obj;
475
/* Walk through list of objects */
476
foreach($Objects as $name => $obj){
478
/* Encode attribtues if required */
479
foreach($this->sub64coded as $codeIt){
480
$obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
483
/* Create ldap entry */
485
$attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
486
foreach($attributes as $attrs){
488
if(empty($obj[$attrs])){
489
$obj[$attrs] = array();
491
if(!is_array($obj[$attrs])){
492
$tmp[$attrs] = stripslashes($obj[$attrs]);
494
$tmp[$attrs] = $obj[$attrs];
498
$tmp['objectClass'] = $this->subClasses;
500
$sub_dn = "cn=".$obj['cn'].",".$this->dn;
502
if($obj['status']=="new"){
503
$ldap->cat($sub_dn,array("objectClass"));
505
$obj['status']="edited";
509
/* Check if gosaAdministrativeUnitTag is required as object class */
510
if($obj['status'] == "edited"){
511
$ldap->cat($sub_dn,array("objectClass"));
512
$attrs = $ldap->fetch();
513
if(isset($attrs['objectClass'])){
514
if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
515
$tmp['objectClass'][] = "gosaAdministrativeUnitTag";
521
$this->tag_attrs(&$tmp, $sub_dn, $this->gosaUnitTag);
523
if($obj['status'] == "delete"){
525
$ldap->rmdir_recursive($sub_dn);
526
$this->handle_post_events("remove");
527
show_ldap_error($ldap->get_error(), _("Removing FAI hook failed"));
528
}elseif($obj['status'] == "edited"){
531
$ldap->modify ($tmp);
532
$this->handle_post_events("modify");
533
show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
534
}elseif($obj['status']=="new"){
535
if($tmp['description']==array()){
536
unset($tmp['description']);
538
$ldap->cd($this->config->current['BASE']);
539
$ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
542
$this->handle_post_events("add");
543
show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
548
/* return copy & paste dialog
550
function getCopyDialog()
553
$smarty = get_smarty();
554
$smarty->assign("cn" ,$this->cn);
555
$str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
557
$ret['string'] = $str;
563
function saveCopyDialog()
565
if(isset($_POST['cn'])){
566
$this->cn = $_POST['cn'];
571
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: