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 $ui;
43 function faiScript ($config, $dn= NULL)
44 {
45 /* Load Attributes */
46 plugin::plugin ($config, $dn);
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 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
51 */
52 if($dn != "new"){
54 $this->dn =$dn;
56 /* Get FAIstate
57 */
58 if(isset($this->attrs['FAIstate'][0])){
59 $this->FAIstate = $this->attrs['FAIstate'][0];
60 }
62 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
63 */
64 $ldap = $this->config->get_ldap_link();
65 $ldap->cd ($this->dn);
67 $attrs_to_search = $this->subAttributes;
68 $attrs_to_search[] = "FAIstate";
69 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
71 while($object = $ldap->fetch()){
73 /* Skip objects, that are tagged as removed */
74 if(isset($object['FAIstate'][0])){
75 if(preg_match("/removed$/",$object['FAIstate'][0])){
76 continue;
77 }
78 }
80 /* Set status for save management */
81 $objects = array();
82 $objects['status'] = "FreshLoaded";
83 $objects['dn'] = $object['dn'];
84 $objects = $this->get_object_attributes($objects,$this->subAttributes);
85 $this->SubObjects[$objects['cn']] = $objects;
86 }
88 @log::log("view","fai/".get_class($this),$this->dn);
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 /* Fill templating stuff */
145 $smarty= get_smarty();
146 $display= "";
148 /* Add new sub object */
149 if(isset($_POST['AddSubObject'])){
150 $this->dialog= new $this->subClassName($this->config,"new");
151 $this->dialog->set_acl_base($this->acl_base);
152 $this->dialog->set_acl_category("fai");
153 $this->is_dialog=true;
154 }
156 if($this->dn != "new"){
157 $_SESSION['objectinfo']= $this->dn;
158 }
160 /* Handle posts */
161 $once = true;
162 foreach($_POST as $name => $value){
164 /* Edit script posted */
165 if(preg_match("/^editscript_/",$name)&&($once)){
167 /* Get posted entry id */
168 $once = false;
169 $entry = preg_replace("/^editscript_/","",$name);
170 $entry = base64_decode(preg_replace("/_.*/","",$entry));
172 /* Get object, and load missing entry values */
173 $obj = $this->SubObjects[$entry];
174 if($obj['status'] == "FreshLoaded"){
175 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
176 }
178 /* Create new dialog and set acl attributes */
179 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
180 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
181 $this->dialog->set_acl_category("fai");
183 /* Assign some additional dialog informations like headline and parent */
184 $_SESSION['objectinfo'] = $obj['dn'];
185 $this->dialog->parent = &$this;
186 $this->is_dialog=true;
187 }
189 /* Delete script requested */
190 if(preg_match("/^deletescript_/",$name)&&($once)){
192 /* Parse out posted entry id */
193 $once = false;
194 $entry = preg_replace("/^deletescript_/","",$name);
195 $entry = base64_decode(preg_replace("/_.*/","",$entry));
197 /* Check acls, are we allowed to delete an entry */
198 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry") ;
199 if(preg_match("/d/",$acl)){
200 $status = $this->SubObjects[$entry]['status'];
201 if($status == "edited" || $status == "FreshLoaded"){
202 $this->SubObjects[$entry]['status']= "delete";
203 }else{
204 unset($this->SubObjects[$entry]);
205 }
206 }
207 }
208 }
211 /* Save the edited entry */
212 if(isset($_POST['SaveSubObject'])){
214 /* Check if there are still errors remaining that must be fixed before saving */
215 $this->dialog->save_object();
216 $msgs = $this->dialog->check();
217 if(count($msgs)>0){
218 foreach($msgs as $msg){
219 print_red($msg);
220 }
221 }else{
223 /* Get return object */
224 $obj = $this->dialog->save();
226 /* If we have renamed the script entry, we must remove the old entry */
227 if(isset($obj['remove'])){
229 /* Get old entry values */
230 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
232 /* Depending on status, set new status */
233 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
234 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
235 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
236 unset($this->SubObjects[$obj['remove']['from']]);
237 }
239 /* Append the new entry */
240 $obj['status'] = "new";
241 $this->SubObjects[$obj['remove']['to']] = $obj;
242 unset($this->SubObjects[$obj['remove']['to']]['remove']);
243 }else{
245 /* Set new status and append the entry */
246 if($obj['status'] == "FreshLoaded"){
247 $obj['status'] = "edited";
248 }
249 $this->SubObjects[$obj['cn']]=$obj;
250 }
251 $this->is_dialog=false;
252 unset($this->dialog);
253 $this->dialog=NULL;
255 }
256 }
258 /* Sort entries */
259 $tmp = $keys = array();
260 foreach($this->SubObjects as $key => $entry){
261 $keys[$key]=$key;
262 }
263 natcasesort($keys);
264 foreach($keys as $key){
265 $tmp[$key]=$this->SubObjects[$key];
266 }
267 $this->SubObjects = $tmp;
269 /* Cancel Dialog */
270 if(isset($_POST['CancelSubObject'])){
271 $this->is_dialog=false;
272 unset($this->dialog);
273 $this->dialog=NULL;
274 }
276 /* Print dialog if $this->dialog is set */
277 if($this->dialog){
278 $this->dialog->save_object();
279 $display = $this->dialog->execute();
280 return($display);
281 }
283 /* Divlist added 23.02.2006
284 Containing FAIscripts
285 */
286 $divlist = new divSelectBox("FAIscripts");
287 $divlist->setHeight(400);
288 foreach($this->getList(true) as $key => $name){
290 $dn= $this->acl_base_for_current_object($name['dn']);
291 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
292 $act = "";
294 /* Hide delete icon if this object is freezed */
295 if($this->FAIstate == "freeze"){
296 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
297 }else{
298 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
299 if(preg_match("/d/",$acl)){
300 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
301 }
302 }
304 /* Check acls for download icon */
305 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
306 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
307 $down = "";
308 }else{
309 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
310 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
311 </a>";
312 }
314 /* Check if we are allowed to view this object */
315 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
316 if(preg_match("/r/",$s_acl)){
317 $divlist->AddEntry(array( array("string"=>$name['name']),
318 array("string"=>$down , "attach" => "style='width:20px;'"),
319 array("string"=>str_replace("%s",base64_encode($key),$act),
320 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
321 }
322 }
323 $smarty->assign("Entry_divlist",$divlist->DrawList());
326 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
327 * If we post the escaped strings they will be escaped again
328 */
329 foreach($this->attributes as $attrs){
330 if(get_magic_quotes_gpc()){
331 $smarty->assign($attrs,stripslashes($this->$attrs));
332 }else{
333 $smarty->assign($attrs,($this->$attrs));
334 }
335 }
337 $dn = $this->acl_base_for_current_object($this->dn);
338 $smarty->assign("sub_object_is_addable",
339 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
340 !preg_match("/freeze/",$this->FAIstate));
342 $tmp = $this->plInfo();
343 foreach($tmp['plProvidedAcls'] as $name => $translated){
344 $smarty->assign($name."ACL",$this->getacl($name));
345 }
347 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
348 return($display);
349 }
351 /* Generate listbox friendly SubObject list
352 */
353 function getList($use_dns=false){
354 $a_return=array();
355 foreach($this->SubObjects as $obj){
356 if($obj['status'] != "delete"){
357 if($use_dns){
358 if((isset($obj['description']))&&(!empty($obj['description']))){
359 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
360 }else{
361 $a_return[$obj['cn']]['name']= $obj['cn'];
362 }
363 $a_return[$obj['cn']]['dn']= $obj['dn'];
364 }else{
365 if((isset($obj['description']))&&(!empty($obj['description']))){
366 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
367 }else{
368 $a_return[$obj['cn']]= $obj['cn'];
369 }
370 }
371 }
372 }
373 return($a_return);
374 }
376 /* Delete me, and all my subtrees
377 */
378 function remove_from_parent()
379 {
380 if($this->acl_is_removeable()){
381 $ldap = $this->config->get_ldap_link();
382 $ldap->cd ($this->dn);
384 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
385 if($_SESSION['faifilter']['branch'] == "main"){
386 $use_dn = $this->dn;
387 }
389 @log::log("remove","fai/".get_class($this),$use_dn,$this->attributes);
391 prepare_to_save_FAI_object($use_dn,array(),true);
393 foreach($this->SubObjects as $name => $obj){
394 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
395 if($_SESSION['faifilter']['branch'] == "main"){
396 $use_dn = $obj['dn'];
397 }
398 prepare_to_save_FAI_object($use_dn,array(),true);
399 }
400 $this->handle_post_events("remove");
401 }
402 }
405 /* Save data to object
406 */
407 function save_object()
408 {
409 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
410 plugin::save_object();
411 foreach($this->attributes as $attrs){
412 if(isset($_POST[$attrs])){
413 $this->$attrs = $_POST[$attrs];
414 }
415 }
416 }
417 }
420 /* Check supplied data */
421 function check()
422 {
423 /* Call common method to give check the hook */
424 $message= plugin::check();
426 return ($message);
427 }
430 /* Save to LDAP */
431 function save()
432 {
433 plugin::save();
435 $ldap = $this->config->get_ldap_link();
437 prepare_to_save_FAI_object($this->dn,$this->attrs);
438 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
440 if($this->initially_was_account){
441 @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
442 }else{
443 @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
444 }
446 /* Do object tagging */
447 $this->handle_object_tagging();
449 /* Prepare FAIscriptEntry to write it to ldap
450 * First sort array.
451 * Because we must delete old entries first.
452 * After deletion, we perform add and modify
453 */
454 $Objects = array();
456 /* We do not need to save untouched objects */
457 foreach($this->SubObjects as $name => $obj){
458 if($obj['status'] == "FreshLoaded"){
459 unset($this->SubObjects[$name]);
460 }
461 }
463 foreach($this->SubObjects as $name => $obj){
464 if($obj['status'] == "delete"){
465 $Objects[$name] = $obj;
466 }
467 }
468 foreach($this->SubObjects as $name => $obj){
469 if($obj['status'] != "delete"){
470 $Objects[$name] = $obj;
471 }
472 }
474 foreach($Objects as $name => $obj){
476 foreach($this->sub64coded as $codeIt){
477 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
478 }
480 $tmp = array();
481 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
482 foreach($attributes as $attrs){
484 if(empty($obj[$attrs])){
485 $obj[$attrs] = array();
486 }
487 if(!is_array($obj[$attrs])){
488 $tmp[$attrs] = stripslashes($obj[$attrs]);
489 }else{
490 $tmp[$attrs] = $obj[$attrs];
491 }
492 }
494 $tmp['objectClass'] = $this->subClasses;
496 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
498 if($obj['status']=="new"){
499 $ldap->cat($sub_dn,array("objectClass"));
500 if($ldap->count()){
501 $obj['status']="edited";
502 }
503 }
505 if(empty($tmp['FAIpriority'])){
506 $tmp['FAIpriority'] ="0";
507 }
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";
516 }
517 }
518 }
520 if($obj['status'] == "delete"){
521 prepare_to_save_FAI_object($sub_dn,array(),true);
522 $this->handle_post_events("remove");
523 }elseif($obj['status'] == "edited"){
524 prepare_to_save_FAI_object($sub_dn,$tmp);
525 $this->handle_post_events("modify");
526 }elseif($obj['status']=="new"){
527 prepare_to_save_FAI_object($sub_dn,$tmp);
528 $this->handle_post_events("add");
529 }
531 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
532 }
533 }
536 /* Return plugin informations for acl handling */
537 function plInfo()
538 {
539 return (array(
540 "plShortName" => _("Script"),
541 "plDescription" => _("FAI script"),
542 "plSelfModify" => FALSE,
543 "plDepends" => array(),
544 "plPriority" => 18,
545 "plSection" => array("administration"),
546 "plCategory" => array("fai"),
547 "plProvidedAcls" => array(
548 "cn" => _("Name")." ("._("Readonly").")",
549 "description" => _("Description"))
550 ));
551 }
552 }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>