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 = "";
40 var $sort_by = "name";
41 var $sort_order = "up";
43 var $view_logged = FALSE;
44 var $ui;
46 function faiScript ($config, $dn= NULL)
47 {
48 /* Load Attributes */
49 plugin::plugin ($config, $dn);
51 /* If "dn==new" we try to create a new entry
52 * Else we must read all objects from ldap which belong to this entry.
53 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
54 */
55 if($dn != "new"){
57 $this->dn =$dn;
59 /* Get FAIstate
60 */
61 if(isset($this->attrs['FAIstate'][0])){
62 $this->FAIstate = $this->attrs['FAIstate'][0];
63 }
65 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
66 */
67 $ldap = $this->config->get_ldap_link();
68 $ldap->cd ($this->dn);
70 $attrs_to_search = $this->subAttributes;
71 $attrs_to_search[] = "FAIstate";
72 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
74 while($object = $ldap->fetch()){
76 /* Skip objects, that are tagged as removed */
77 if(isset($object['FAIstate'][0])){
78 if(preg_match("/removed$/",$object['FAIstate'][0])){
79 continue;
80 }
81 }
83 /* Set status for save management */
84 $objects = array();
85 $objects['status'] = "FreshLoaded";
86 $objects['dn'] = $object['dn'];
87 $objects = $this->get_object_attributes($objects,$this->subAttributes);
88 $this->SubObjects[$objects['cn']] = $objects;
89 }
91 }
92 $this->ui = get_userinfo();
93 }
96 /* Reload some attributes */
97 function get_object_attributes($object,$attributes)
98 {
99 $ldap = $this->config->get_ldap_link();
100 $ldap->cd($this->config->current['BASE']);
101 $ldap->cat($object['dn'],$attributes);
102 $tmp = $ldap->fetch();
104 foreach($attributes as $attrs){
105 if(isset($tmp[$attrs][0])){
106 $var = $tmp[$attrs][0];
108 /* Check if we must decode some attributes */
109 if(in_array_ics($attrs,$this->sub64coded)){
110 $var = base64_decode($var);
111 }
113 /* check if this is a binary entry */
114 if(in_array_ics($attrs,$this->subBinary)){
115 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
116 }
118 /* Fix slashes */
119 $var = addslashes($var);
120 $object[$attrs] = $var;
121 }
122 }
123 return($object);
124 }
127 /* Return a valid dn to fetch acls. Because 'new' will not work. */
128 function acl_base_for_current_object($dn)
129 {
130 if($dn == "new"){
131 if($this->dn == "new"){
132 $dn= "cn=dummy,".$_SESSION['CurrentMainBase'];
133 }else{
134 $dn = $this->dn;
135 }
136 }
137 return($dn);
138 }
141 function execute()
142 {
143 /* Call parent execute */
144 plugin::execute();
146 if($this->is_account && !$this->view_logged){
147 $this->view_logged = TRUE;
148 new log("view","fai/".get_class($this),$this->dn);
149 }
151 /* Fill templating stuff */
152 $smarty= get_smarty();
153 $display= "";
155 /* Add new sub object */
156 if(isset($_POST['AddSubObject'])){
157 $this->dialog= new $this->subClassName($this->config,"new");
158 $this->dialog->set_acl_base($this->acl_base);
159 $this->dialog->set_acl_category("fai");
160 $this->dialog->parent = &$this;
161 $this->is_dialog=true;
162 }
164 if($this->dn != "new"){
165 $_SESSION['objectinfo']= $this->dn;
166 }
168 /* Handle posts */
169 $once = true;
170 foreach($_POST as $name => $value){
172 /* Edit script posted */
173 if(preg_match("/^editscript_/",$name)&&($once)){
175 /* Get posted entry id */
176 $once = false;
177 $entry = preg_replace("/^editscript_/","",$name);
178 $entry = base64_decode(preg_replace("/_.*/","",$entry));
180 /* Get object, and load missing entry values */
181 $obj = $this->SubObjects[$entry];
182 if($obj['status'] == "FreshLoaded"){
183 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
184 }
186 /* Create new dialog and set acl attributes */
187 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
188 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
189 $this->dialog->set_acl_category("fai");
191 /* Assign some additional dialog informations like headline and parent */
192 $_SESSION['objectinfo'] = $obj['dn'];
193 $this->dialog->parent = &$this;
194 $this->is_dialog=true;
195 }
197 /* Delete script requested */
198 if(preg_match("/^deletescript_/",$name)&&($once)){
200 /* Parse out posted entry id */
201 $once = false;
202 $entry = preg_replace("/^deletescript_/","",$name);
203 $entry = base64_decode(preg_replace("/_.*/","",$entry));
205 /* Check acls, are we allowed to delete an entry */
206 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry") ;
207 if(preg_match("/d/",$acl)){
208 $status = $this->SubObjects[$entry]['status'];
209 if($status == "edited" || $status == "FreshLoaded"){
210 $this->SubObjects[$entry]['status']= "delete";
211 }else{
212 unset($this->SubObjects[$entry]);
213 }
214 }
215 }
216 }
219 /* Save the edited entry */
220 if(isset($_POST['SaveSubObject'])){
222 /* Check if there are still errors remaining that must be fixed before saving */
223 $this->dialog->save_object();
224 $msgs = $this->dialog->check();
225 if(count($msgs)>0){
226 foreach($msgs as $msg){
227 print_red($msg);
228 }
229 }else{
231 /* Get return object */
232 $obj = $this->dialog->save();
234 /* If we have renamed the script entry, we must remove the old entry */
235 if(isset($obj['remove'])){
237 /* Get old entry values */
238 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
240 /* Depending on status, set new status */
241 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
242 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
243 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
244 unset($this->SubObjects[$obj['remove']['from']]);
245 }
247 /* Append the new entry */
248 $obj['status'] = "new";
249 $this->SubObjects[$obj['remove']['to']] = $obj;
250 unset($this->SubObjects[$obj['remove']['to']]['remove']);
251 }else{
253 /* Set new status and append the entry */
254 if($obj['status'] == "FreshLoaded"){
255 $obj['status'] = "edited";
256 }
257 $this->SubObjects[$obj['cn']]=$obj;
258 }
259 $this->is_dialog=false;
260 unset($this->dialog);
261 $this->dialog=NULL;
263 }
264 }
266 /* Sort entries */
267 $tmp = $keys = array();
269 if($this->sort_by == "name"){
270 foreach($this->SubObjects as $key => $entry){
271 $keys[$key]=$entry['cn'];
272 }
273 }elseif($this->sort_by == "priority"){
274 foreach($this->SubObjects as $key => $entry){
275 $keys[$key]=$entry['FAIpriority'];
276 }
277 }
279 natcasesort($keys);
281 if($this->sort_order == "down"){
282 $keys =array_reverse($keys);
283 }
285 foreach($keys as $key => $order_var){
286 $tmp[$key]=$this->SubObjects[$key];
287 }
288 $this->SubObjects = $tmp;
290 /* Cancel Dialog */
291 if(isset($_POST['CancelSubObject'])){
292 $this->is_dialog=false;
293 unset($this->dialog);
294 $this->dialog=NULL;
295 }
297 /* Print dialog if $this->dialog is set */
298 if($this->dialog){
299 $this->dialog->save_object();
300 $display = $this->dialog->execute();
301 return($display);
302 }
304 /* Divlist added 23.02.2006
305 Containing FAIscripts
306 */
307 $divlist = new divlist("FAIscripts");
308 $plug = $_GET['plug'];
310 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name")."</a>"),
311 array("string"=>"<a href='?plug=".$plug."&sort=priority'>"._("Priority")."</a>"),
312 array("string"=>_("Download")),
313 array("string"=>_("Action"))));
314 $divlist->SetHeight(300);
315 $divlist->SetWidth("100%");
316 foreach($this->getList(true) as $key => $name){
318 $dn= $this->acl_base_for_current_object($name['dn']);
319 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
320 $act = "";
322 /* Hide delete icon if this object is freezed */
323 if($this->FAIstate == "freeze"){
324 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
325 }else{
326 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
327 if(preg_match("/d/",$acl)){
328 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
329 }
330 }
332 /* Check acls for download icon */
333 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
334 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
335 $down = "";
336 }else{
337 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
338 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
339 </a>";
340 }
342 /* Check if we are allowed to view this object */
343 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
344 if(preg_match("/r/",$s_acl)){
345 $divlist->AddEntry(array( array("string"=>$name['name']),
346 array("string"=>$name['FAIpriority'] , "attach" => "style='width:20px;'"),
347 array("string"=>$down , "attach" => "style='width:20px;'"),
348 array("string"=>str_replace("%s",base64_encode($key),$act),
349 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
350 }
351 }
352 $smarty->assign("Entry_divlist",$divlist->DrawList());
355 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
356 * If we post the escaped strings they will be escaped again
357 */
358 foreach($this->attributes as $attrs){
359 if(get_magic_quotes_gpc()){
360 $smarty->assign($attrs,stripslashes($this->$attrs));
361 }else{
362 $smarty->assign($attrs,($this->$attrs));
363 }
364 }
366 $dn = $this->acl_base_for_current_object($this->dn);
367 $smarty->assign("sub_object_is_addable",
368 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
369 !preg_match("/freeze/",$this->FAIstate));
371 $tmp = $this->plInfo();
372 foreach($tmp['plProvidedAcls'] as $name => $translated){
373 $smarty->assign($name."ACL",$this->getacl($name));
374 }
376 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
377 return($display);
378 }
380 /* Generate listbox friendly SubObject list
381 */
382 function getList($use_dns=false){
383 $a_return=array();
384 foreach($this->SubObjects as $obj){
385 if($obj['status'] != "delete"){
386 if($use_dns){
387 if((isset($obj['description']))&&(!empty($obj['description']))){
388 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
389 }else{
390 $a_return[$obj['cn']]['name']= $obj['cn'];
391 }
392 $a_return[$obj['cn']]['dn']= $obj['dn'];
393 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
394 }else{
395 if((isset($obj['description']))&&(!empty($obj['description']))){
396 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
397 }else{
398 $a_return[$obj['cn']]= $obj['cn'];
399 }
400 }
401 }
402 }
403 return($a_return);
404 }
406 /* Delete me, and all my subtrees
407 */
408 function remove_from_parent()
409 {
410 if($this->acl_is_removeable()){
411 $ldap = $this->config->get_ldap_link();
412 $ldap->cd ($this->dn);
414 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
415 if($_SESSION['faifilter']['branch'] == "main"){
416 $use_dn = $this->dn;
417 }
419 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
421 prepare_to_save_FAI_object($use_dn,array(),true);
423 foreach($this->SubObjects as $name => $obj){
424 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
425 if($_SESSION['faifilter']['branch'] == "main"){
426 $use_dn = $obj['dn'];
427 }
428 prepare_to_save_FAI_object($use_dn,array(),true);
429 }
430 $this->handle_post_events("remove");
431 }
432 }
435 /* Save data to object
436 */
437 function save_object()
438 {
439 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
440 plugin::save_object();
441 foreach($this->attributes as $attrs){
442 if(isset($_POST[$attrs])){
443 $this->$attrs = $_POST[$attrs];
444 }
445 }
446 }
448 /* Get sort order */
449 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
450 if($this->sort_by == $_GET['sort']){
451 if($this->sort_order == "up"){
452 $this->sort_order = "down";
453 }elseif($this->sort_order == "down"){
454 $this->sort_order = "up";
455 }
456 }
457 $this->sort_by = $_GET['sort'];
458 }
459 }
462 /* Check supplied data */
463 function check()
464 {
465 /* Call common method to give check the hook */
466 $message= plugin::check();
468 return ($message);
469 }
472 /* Save to LDAP */
473 function save()
474 {
475 plugin::save();
477 $ldap = $this->config->get_ldap_link();
479 prepare_to_save_FAI_object($this->dn,$this->attrs);
480 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
482 if($this->initially_was_account){
483 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
484 }else{
485 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
486 }
488 /* Do object tagging */
489 $this->handle_object_tagging();
491 /* Prepare FAIscriptEntry to write it to ldap
492 * First sort array.
493 * Because we must delete old entries first.
494 * After deletion, we perform add and modify
495 */
496 $Objects = array();
498 /* We do not need to save untouched objects */
499 foreach($this->SubObjects as $name => $obj){
500 if($obj['status'] == "FreshLoaded"){
501 unset($this->SubObjects[$name]);
502 }
503 }
505 foreach($this->SubObjects as $name => $obj){
506 if($obj['status'] == "delete"){
507 $Objects[$name] = $obj;
508 }
509 }
510 foreach($this->SubObjects as $name => $obj){
511 if($obj['status'] != "delete"){
512 $Objects[$name] = $obj;
513 }
514 }
516 foreach($Objects as $name => $obj){
518 foreach($this->sub64coded as $codeIt){
519 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
520 }
522 $tmp = array();
523 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
524 foreach($attributes as $attrs){
526 if(empty($obj[$attrs])){
527 $obj[$attrs] = array();
528 }
529 if(!is_array($obj[$attrs])){
530 $tmp[$attrs] = stripslashes($obj[$attrs]);
531 }else{
532 $tmp[$attrs] = $obj[$attrs];
533 }
534 }
536 $tmp['objectClass'] = $this->subClasses;
538 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
540 if($obj['status']=="new"){
541 $ldap->cat($sub_dn,array("objectClass"));
542 if($ldap->count()){
543 $obj['status']="edited";
544 }
545 }
547 if(empty($tmp['FAIpriority'])){
548 $tmp['FAIpriority'] ="0";
549 }
551 /* Check if gosaAdministrativeUnitTag is required as object class */
552 if($obj['status'] == "edited"){
553 $ldap->cat($sub_dn,array("objectClass"));
554 $attrs = $ldap->fetch();
555 if(isset($attrs['objectClass'])){
556 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
557 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
558 }
559 }
560 }
562 if($obj['status'] == "delete"){
563 prepare_to_save_FAI_object($sub_dn,array(),true);
564 $this->handle_post_events("remove");
565 }elseif($obj['status'] == "edited"){
566 prepare_to_save_FAI_object($sub_dn,$tmp);
567 $this->handle_post_events("modify");
568 }elseif($obj['status']=="new"){
569 prepare_to_save_FAI_object($sub_dn,$tmp);
570 $this->handle_post_events("add");
571 }
573 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
574 }
575 }
578 /* Return plugin informations for acl handling */
579 function plInfo()
580 {
581 return (array(
582 "plShortName" => _("Script"),
583 "plDescription" => _("FAI script"),
584 "plSelfModify" => FALSE,
585 "plDepends" => array(),
586 "plPriority" => 18,
587 "plSection" => array("administration"),
588 "plCategory" => array("fai"),
589 "plProvidedAcls" => array(
590 "cn" => _("Name")." ("._("Readonly").")",
591 "description" => _("Description"))
592 ));
593 }
594 }
596 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
597 ?>