1 <?php
3 class faiScript 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","FAIscript");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIscriptEntry";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiScriptEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIpriority");
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 var $view_logged = FALSE;
42 var $ui;
44 function faiScript ($config, $dn= NULL)
45 {
46 /* Load Attributes */
47 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.
51 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
52 */
53 if($dn != "new"){
55 $this->dn =$dn;
57 /* Get FAIstate
58 */
59 if(isset($this->attrs['FAIstate'][0])){
60 $this->FAIstate = $this->attrs['FAIstate'][0];
61 }
63 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
64 */
65 $ldap = $this->config->get_ldap_link();
66 $ldap->cd ($this->dn);
68 $attrs_to_search = $this->subAttributes;
69 $attrs_to_search[] = "FAIstate";
70 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
72 while($object = $ldap->fetch()){
74 /* Skip objects, that are tagged as removed */
75 if(isset($object['FAIstate'][0])){
76 if(preg_match("/removed$/",$object['FAIstate'][0])){
77 continue;
78 }
79 }
81 /* Set status for save management */
82 $objects = array();
83 $objects['status'] = "FreshLoaded";
84 $objects['dn'] = $object['dn'];
85 $objects = $this->get_object_attributes($objects,$this->subAttributes);
86 $this->SubObjects[$objects['cn']] = $objects;
87 }
89 }
90 $this->ui = get_userinfo();
91 }
94 /* Reload some attributes */
95 function get_object_attributes($object,$attributes)
96 {
97 $ldap = $this->config->get_ldap_link();
98 $ldap->cd($this->config->current['BASE']);
99 $ldap->cat($object['dn'],$attributes);
100 $tmp = $ldap->fetch();
102 foreach($attributes as $attrs){
103 if(isset($tmp[$attrs][0])){
104 $var = $tmp[$attrs][0];
106 /* Check if we must decode some attributes */
107 if(in_array_ics($attrs,$this->sub64coded)){
108 $var = base64_decode($var);
109 }
111 /* check if this is a binary entry */
112 if(in_array_ics($attrs,$this->subBinary)){
113 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
114 }
116 /* Fix slashes */
117 $var = addslashes($var);
118 $object[$attrs] = $var;
119 }
120 }
121 return($object);
122 }
125 /* Return a valid dn to fetch acls. Because 'new' will not work. */
126 function acl_base_for_current_object($dn)
127 {
128 if($dn == "new"){
129 if($this->dn == "new"){
130 $dn= "cn=dummy,".$_SESSION['CurrentMainBase'];
131 }else{
132 $dn = $this->dn;
133 }
134 }
135 return($dn);
136 }
139 function execute()
140 {
141 /* Call parent execute */
142 plugin::execute();
144 if($this->is_account && !$this->view_logged){
145 $this->view_logged = TRUE;
146 new log("view","fai/".get_class($this),$this->dn);
147 }
149 /* Fill templating stuff */
150 $smarty= get_smarty();
151 $display= "";
153 /* Add new sub object */
154 if(isset($_POST['AddSubObject'])){
155 $this->dialog= new $this->subClassName($this->config,"new");
156 $this->dialog->set_acl_base($this->acl_base);
157 $this->dialog->set_acl_category("fai");
158 $this->dialog->parent = &$this;
159 $this->is_dialog=true;
160 }
162 if($this->dn != "new"){
163 $_SESSION['objectinfo']= $this->dn;
164 }
166 /* Handle posts */
167 $once = true;
168 foreach($_POST as $name => $value){
170 /* Edit script posted */
171 if(preg_match("/^editscript_/",$name)&&($once)){
173 /* Get posted entry id */
174 $once = false;
175 $entry = preg_replace("/^editscript_/","",$name);
176 $entry = base64_decode(preg_replace("/_.*/","",$entry));
178 /* Get object, and load missing entry values */
179 $obj = $this->SubObjects[$entry];
180 if($obj['status'] == "FreshLoaded"){
181 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
182 }
184 /* Create new dialog and set acl attributes */
185 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
186 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
187 $this->dialog->set_acl_category("fai");
189 /* Assign some additional dialog informations like headline and parent */
190 $_SESSION['objectinfo'] = $obj['dn'];
191 $this->dialog->parent = &$this;
192 $this->is_dialog=true;
193 }
195 /* Delete script requested */
196 if(preg_match("/^deletescript_/",$name)&&($once)){
198 /* Parse out posted entry id */
199 $once = false;
200 $entry = preg_replace("/^deletescript_/","",$name);
201 $entry = base64_decode(preg_replace("/_.*/","",$entry));
203 /* Check acls, are we allowed to delete an entry */
204 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry") ;
205 if(preg_match("/d/",$acl)){
206 $status = $this->SubObjects[$entry]['status'];
207 if($status == "edited" || $status == "FreshLoaded"){
208 $this->SubObjects[$entry]['status']= "delete";
209 }else{
210 unset($this->SubObjects[$entry]);
211 }
212 }
213 }
214 }
217 /* Save the edited entry */
218 if(isset($_POST['SaveSubObject'])){
220 /* Check if there are still errors remaining that must be fixed before saving */
221 $this->dialog->save_object();
222 $msgs = $this->dialog->check();
223 if(count($msgs)>0){
224 foreach($msgs as $msg){
225 print_red($msg);
226 }
227 }else{
229 /* Get return object */
230 $obj = $this->dialog->save();
232 /* If we have renamed the script entry, we must remove the old entry */
233 if(isset($obj['remove'])){
235 /* Get old entry values */
236 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
238 /* Depending on status, set new status */
239 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
240 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
241 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
242 unset($this->SubObjects[$obj['remove']['from']]);
243 }
245 /* Append the new entry */
246 $obj['status'] = "new";
247 $this->SubObjects[$obj['remove']['to']] = $obj;
248 unset($this->SubObjects[$obj['remove']['to']]['remove']);
249 }else{
251 /* Set new status and append the entry */
252 if($obj['status'] == "FreshLoaded"){
253 $obj['status'] = "edited";
254 }
255 $this->SubObjects[$obj['cn']]=$obj;
256 }
257 $this->is_dialog=false;
258 unset($this->dialog);
259 $this->dialog=NULL;
261 }
262 }
264 /* Sort entries */
265 $tmp = $keys = array();
266 foreach($this->SubObjects as $key => $entry){
267 $keys[$key]=$key;
268 }
269 natcasesort($keys);
270 foreach($keys as $key){
271 $tmp[$key]=$this->SubObjects[$key];
272 }
273 $this->SubObjects = $tmp;
275 /* Cancel Dialog */
276 if(isset($_POST['CancelSubObject'])){
277 $this->is_dialog=false;
278 unset($this->dialog);
279 $this->dialog=NULL;
280 }
282 /* Print dialog if $this->dialog is set */
283 if($this->dialog){
284 $this->dialog->save_object();
285 $display = $this->dialog->execute();
286 return($display);
287 }
289 /* Divlist added 23.02.2006
290 Containing FAIscripts
291 */
292 $divlist = new divSelectBox("FAIscripts");
293 $divlist->setHeight(400);
294 foreach($this->getList(true) as $key => $name){
296 $dn= $this->acl_base_for_current_object($name['dn']);
297 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
298 $act = "";
300 /* Hide delete icon if this object is freezed */
301 if($this->FAIstate == "freeze"){
302 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
303 }else{
304 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
305 if(preg_match("/d/",$acl)){
306 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
307 }
308 }
310 /* Check acls for download icon */
311 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
312 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
313 $down = "";
314 }else{
315 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
316 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
317 </a>";
318 }
320 /* Check if we are allowed to view this object */
321 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
322 if(preg_match("/r/",$s_acl)){
323 $divlist->AddEntry(array( array("string"=>$name['name']),
324 array("string"=>$down , "attach" => "style='width:20px;'"),
325 array("string"=>str_replace("%s",base64_encode($key),$act),
326 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
327 }
328 }
329 $smarty->assign("Entry_divlist",$divlist->DrawList());
332 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
333 * If we post the escaped strings they will be escaped again
334 */
335 foreach($this->attributes as $attrs){
336 if(get_magic_quotes_gpc()){
337 $smarty->assign($attrs,stripslashes($this->$attrs));
338 }else{
339 $smarty->assign($attrs,($this->$attrs));
340 }
341 }
343 $dn = $this->acl_base_for_current_object($this->dn);
344 $smarty->assign("sub_object_is_addable",
345 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
346 !preg_match("/freeze/",$this->FAIstate));
348 $tmp = $this->plInfo();
349 foreach($tmp['plProvidedAcls'] as $name => $translated){
350 $smarty->assign($name."ACL",$this->getacl($name));
351 }
353 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
354 return($display);
355 }
357 /* Generate listbox friendly SubObject list
358 */
359 function getList($use_dns=false){
360 $a_return=array();
361 foreach($this->SubObjects as $obj){
362 if($obj['status'] != "delete"){
363 if($use_dns){
364 if((isset($obj['description']))&&(!empty($obj['description']))){
365 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
366 }else{
367 $a_return[$obj['cn']]['name']= $obj['cn'];
368 }
369 $a_return[$obj['cn']]['dn']= $obj['dn'];
370 }else{
371 if((isset($obj['description']))&&(!empty($obj['description']))){
372 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
373 }else{
374 $a_return[$obj['cn']]= $obj['cn'];
375 }
376 }
377 }
378 }
379 return($a_return);
380 }
382 /* Delete me, and all my subtrees
383 */
384 function remove_from_parent()
385 {
386 if($this->acl_is_removeable()){
387 $ldap = $this->config->get_ldap_link();
388 $ldap->cd ($this->dn);
390 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
391 if($_SESSION['faifilter']['branch'] == "main"){
392 $use_dn = $this->dn;
393 }
395 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
397 prepare_to_save_FAI_object($use_dn,array(),true);
399 foreach($this->SubObjects as $name => $obj){
400 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
401 if($_SESSION['faifilter']['branch'] == "main"){
402 $use_dn = $obj['dn'];
403 }
404 prepare_to_save_FAI_object($use_dn,array(),true);
405 }
406 $this->handle_post_events("remove");
407 }
408 }
411 /* Save data to object
412 */
413 function save_object()
414 {
415 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
416 plugin::save_object();
417 foreach($this->attributes as $attrs){
418 if(isset($_POST[$attrs])){
419 $this->$attrs = $_POST[$attrs];
420 }
421 }
422 }
423 }
426 /* Check supplied data */
427 function check()
428 {
429 /* Call common method to give check the hook */
430 $message= plugin::check();
432 return ($message);
433 }
436 /* Save to LDAP */
437 function save()
438 {
439 plugin::save();
441 $ldap = $this->config->get_ldap_link();
443 prepare_to_save_FAI_object($this->dn,$this->attrs);
444 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
446 if($this->initially_was_account){
447 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
448 }else{
449 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
450 }
452 /* Do object tagging */
453 $this->handle_object_tagging();
455 /* Prepare FAIscriptEntry to write it to ldap
456 * First sort array.
457 * Because we must delete old entries first.
458 * After deletion, we perform add and modify
459 */
460 $Objects = array();
462 /* We do not need to save untouched objects */
463 foreach($this->SubObjects as $name => $obj){
464 if($obj['status'] == "FreshLoaded"){
465 unset($this->SubObjects[$name]);
466 }
467 }
469 foreach($this->SubObjects as $name => $obj){
470 if($obj['status'] == "delete"){
471 $Objects[$name] = $obj;
472 }
473 }
474 foreach($this->SubObjects as $name => $obj){
475 if($obj['status'] != "delete"){
476 $Objects[$name] = $obj;
477 }
478 }
480 foreach($Objects as $name => $obj){
482 foreach($this->sub64coded as $codeIt){
483 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
484 }
486 $tmp = array();
487 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
488 foreach($attributes as $attrs){
490 if(empty($obj[$attrs])){
491 $obj[$attrs] = array();
492 }
493 if(!is_array($obj[$attrs])){
494 $tmp[$attrs] = stripslashes($obj[$attrs]);
495 }else{
496 $tmp[$attrs] = $obj[$attrs];
497 }
498 }
500 $tmp['objectClass'] = $this->subClasses;
502 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
504 if($obj['status']=="new"){
505 $ldap->cat($sub_dn,array("objectClass"));
506 if($ldap->count()){
507 $obj['status']="edited";
508 }
509 }
511 if(empty($tmp['FAIpriority'])){
512 $tmp['FAIpriority'] ="0";
513 }
515 /* Check if gosaAdministrativeUnitTag is required as object class */
516 if($obj['status'] == "edited"){
517 $ldap->cat($sub_dn,array("objectClass"));
518 $attrs = $ldap->fetch();
519 if(isset($attrs['objectClass'])){
520 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
521 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
522 }
523 }
524 }
526 if($obj['status'] == "delete"){
527 prepare_to_save_FAI_object($sub_dn,array(),true);
528 $this->handle_post_events("remove");
529 }elseif($obj['status'] == "edited"){
530 prepare_to_save_FAI_object($sub_dn,$tmp);
531 $this->handle_post_events("modify");
532 }elseif($obj['status']=="new"){
533 prepare_to_save_FAI_object($sub_dn,$tmp);
534 $this->handle_post_events("add");
535 }
537 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
538 }
539 }
542 /* Return plugin informations for acl handling */
543 function plInfo()
544 {
545 return (array(
546 "plShortName" => _("Script"),
547 "plDescription" => _("FAI script"),
548 "plSelfModify" => FALSE,
549 "plDepends" => array(),
550 "plPriority" => 18,
551 "plSection" => array("administration"),
552 "plCategory" => array("fai"),
553 "plProvidedAcls" => array(
554 "cn" => _("Name")." ("._("Readonly").")",
555 "description" => _("Description"))
556 ));
557 }
558 }
560 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
561 ?>