1 <?php
3 class faiScript extends plugin
4 {
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","FAIscript");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "FAIscriptEntry";
16 var $subClasses = array("top","FAIclass","FAIscriptEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "faiScriptEntry";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description","FAIpriority");
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 $SubObjects = array(); // All leafobjects of this object
33 var $FAIstate = "";
34 var $sort_by = "name";
35 var $sort_order = "up";
37 var $view_logged = FALSE;
38 var $ui;
40 function faiScript (&$config, $dn= NULL)
41 {
42 /* Load Attributes */
43 plugin::plugin ($config, $dn);
45 /* If "dn==new" we try to create a new entry
46 * Else we must read all objects from ldap which belong to this entry.
47 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
48 */
49 if($dn != "new"){
51 $this->dn =$dn;
53 /* Get FAIstate
54 */
55 if(isset($this->attrs['FAIstate'][0])){
56 $this->FAIstate = $this->attrs['FAIstate'][0];
57 }
59 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
60 */
61 $ldap = $this->config->get_ldap_link();
62 $ldap->cd ($this->dn);
64 $attrs_to_search = $this->subAttributes;
65 $attrs_to_search[] = "FAIstate";
66 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
67 $data = array();
68 while($object = $ldap->fetch()){
69 $data[] = $object;
70 }
71 foreach($data as $object){
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 }
89 $this->ui = get_userinfo();
90 }
93 /* Reload some attributes */
94 function get_object_attributes($object,$attributes)
95 {
96 $ldap = $this->config->get_ldap_link();
97 $ldap->cd($this->config->current['BASE']);
98 $ldap->cat($object['dn'],$attributes);
99 $tmp = $ldap->fetch();
101 foreach($attributes as $attrs){
102 if(isset($tmp[$attrs][0])){
103 $var = $tmp[$attrs][0];
105 /* Check if we must decode some attributes */
106 if(in_array_ics($attrs,$this->sub64coded)){
107 $var = base64_decode($var);
108 }
110 /* check if this is a binary entry */
111 if(in_array_ics($attrs,$this->subBinary)){
112 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
113 }
115 /* Fix slashes */
116 $var = addslashes($var);
117 $object[$attrs] = $var;
118 }
119 }
120 return($object);
121 }
124 /* Return a valid dn to fetch acls. Because 'new' will not work. */
125 function acl_base_for_current_object($dn)
126 {
127 if($dn == "new"){
128 if($this->dn == "new"){
129 $dn= "cn=dummy,".session::get('CurrentMainBase');
130 }else{
131 $dn = $this->dn;
132 }
133 }
134 return($dn);
135 }
138 function execute()
139 {
140 /* Call parent execute */
141 plugin::execute();
143 if($this->is_account && !$this->view_logged){
144 $this->view_logged = TRUE;
145 new log("view","fai/".get_class($this),$this->dn);
146 }
148 /* Fill templating stuff */
149 $smarty= get_smarty();
150 $display= "";
152 /* Add new sub object */
153 if(isset($_POST['AddSubObject'])){
154 $this->dialog= new $this->subClassName($this->config,"new");
155 $this->dialog->set_acl_base($this->acl_base);
156 $this->dialog->set_acl_category("fai");
157 $this->dialog->parent = &$this;
158 $this->is_dialog=true;
159 }
161 if($this->dn != "new"){
162 session::set('objectinfo',$this->dn);
163 }
165 /* File download requested */
166 if(isset($_GET['getFAIscript'])){
167 if(isset($this->SubObjects[base64_decode($_GET['getFAIscript'])])){
168 $obj = $this->SubObjects[base64_decode($_GET['getFAIscript'])];
169 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
170 send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript");
171 }
172 }
174 /* Handle posts */
175 $s_action = $s_entry = "";
176 foreach($_POST as $name => $value){
178 /* Edit script posted */
179 if(preg_match("/^editscript_/",$name)){
180 $s_action = "edit";
181 $s_entry = preg_replace("/^editscript_/","",$name);
182 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
183 break;
184 }
186 /* Delete script requested */
187 if(preg_match("/^deletescript_/",$name)){
188 $s_action = "remove";
189 $s_entry = preg_replace("/^deletescript_/","",$name);
190 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
191 break;
192 }
193 }
195 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
196 $s_entry = base64_decode($_GET['id']);
197 if(isset($this->SubObjects[$s_entry])){
198 $s_action = "edit";
199 }
200 }
202 if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
204 /* Get object, and load missing entry values */
205 $obj = $this->SubObjects[$s_entry];
206 if($obj['status'] == "FreshLoaded"){
207 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
208 }
210 /* Create new dialog and set acl attributes */
211 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
212 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
213 $this->dialog->set_acl_category("fai");
215 /* Assign some additional dialog informations like headline and parent */
216 session::set('objectinfo',$obj['dn']);
217 $this->dialog->parent = &$this;
218 $this->is_dialog=true;
219 }
221 /* Check acls, are we allowed to delete an entry */
222 if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
223 $entry = $this->SubObjects[$s_entry];
224 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry") ;
225 if(preg_match("/d/",$acl)){
226 $status = $entry['status'];
227 if($status == "edited" || $status == "FreshLoaded"){
228 $this->SubObjects[$s_entry]['status']= "delete";
229 }else{
230 unset($this->SubObjects[$s_entry]);
231 }
232 }
233 }
235 /* Save the edited entry */
236 if(isset($_POST['SaveSubObject'])){
238 /* Check if there are still errors remaining that must be fixed before saving */
239 $this->dialog->save_object();
240 $msgs = $this->dialog->check();
241 if(count($msgs)>0){
242 foreach($msgs as $msg){
243 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
244 }
245 }else{
247 /* Get return object */
248 $obj = $this->dialog->save();
250 /* If we have renamed the script entry, we must remove the old entry */
251 if(isset($obj['remove'])){
253 /* Get old entry values */
254 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
256 /* Depending on status, set new status */
257 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
258 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
259 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
260 unset($this->SubObjects[$obj['remove']['from']]);
261 }
263 /* Append the new entry */
264 $obj['status'] = "new";
265 $this->SubObjects[$obj['remove']['to']] = $obj;
266 unset($this->SubObjects[$obj['remove']['to']]['remove']);
267 }else{
269 /* Set new status and append the entry */
270 if($obj['status'] == "FreshLoaded"){
271 $obj['status'] = "edited";
272 }
273 $this->SubObjects[$obj['cn']]=$obj;
274 }
275 $this->is_dialog=false;
276 unset($this->dialog);
277 $this->dialog=FALSE;
279 }
280 }
282 /* Sort entries */
283 $tmp = $keys = array();
285 if($this->sort_by == "name"){
286 foreach($this->SubObjects as $key => $entry){
287 $keys[$key]=$entry['cn'];
288 }
289 }elseif($this->sort_by == "priority"){
290 foreach($this->SubObjects as $key => $entry){
291 $keys[$key]=$entry['FAIpriority'];
292 }
293 }
295 natcasesort($keys);
297 if($this->sort_order == "down"){
298 $keys =array_reverse($keys);
299 }
301 foreach($keys as $key => $order_var){
302 $tmp[$key]=$this->SubObjects[$key];
303 }
304 $this->SubObjects = $tmp;
306 /* Cancel Dialog */
307 if(isset($_POST['CancelSubObject'])){
308 $this->is_dialog=false;
309 unset($this->dialog);
310 $this->dialog=FALSE;
311 }
313 /* Print dialog if $this->dialog is set */
314 if(is_object($this->dialog)){
315 $this->dialog->save_object();
316 $display = $this->dialog->execute();
317 return($display);
318 }
320 /* Divlist added 23.02.2006
321 Containing FAIscripts
322 */
323 $divlist = new divlist("FAIscripts");
324 $divlist->SetEntriesPerPage(0);
325 $plug = $_GET['plug'];
327 if($this->sort_order == "up"){
328 $dir = "<img src='images/sort_up.png' title='"._("Sort direction")."' alt='\/' border=0>";
329 }else{
330 $dir = "<img src='images/sort_down.png' title='"._("Sort direction")."' alt='/\' border=0>";
331 }
333 if($this->sort_by == "name"){
334 $sort_name = $dir;
335 $sort_prio = "";
336 }else{
337 $sort_name = "";
338 $sort_prio = $dir;
339 }
341 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name").$sort_name."</a>"),
342 array("string"=>"<a href='?plug=".$plug."&sort=priority'>".$sort_prio._("Priority")."</a>",
343 "attach"=>"style='width:100px;'"),
344 array("string"=>_("Download"),
345 "attach"=>"style='width:100px;'"),
346 array("string"=>_("Action"),
347 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
348 $divlist->SetHeight(300);
349 $divlist->SetWidth("100%");
350 foreach($this->getList(true) as $key => $name){
352 $dn= $this->acl_base_for_current_object($name['dn']);
353 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
354 $act = "";
356 /* Hide delete icon if this object is freezed */
357 if(preg_match("/freeze/", $this->FAIstate)){
358 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
359 }else{
360 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
361 if(preg_match("/d/",$acl)){
362 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
363 }
364 }
366 /* Check acls for download icon */
367 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
368 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
369 $down = "";
370 }else{
371 $down = "<a href='?plug=".$_GET['plug']."&getFAIscript=".base64_encode($key)."'>
372 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
373 </a>";
374 }
376 /* Check if we are allowed to view this object */
377 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
378 if(preg_match("/r/",$s_acl)){
379 $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_decode($key)."'>".$name['name']."</a>"),
380 array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
381 array("string"=>$down , "attach" => "style='width:100px;'"),
382 array("string"=>str_replace("%s",base64_encode($key),$act),
383 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
384 }
385 }
386 $smarty->assign("Entry_divlist",$divlist->DrawList());
388 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
389 * If we post the escaped strings they will be escaped again
390 */
391 foreach($this->attributes as $attrs){
392 if(get_magic_quotes_gpc()){
393 $smarty->assign($attrs,stripslashes($this->$attrs));
394 }else{
395 $smarty->assign($attrs,($this->$attrs));
396 }
397 }
399 $dn = $this->acl_base_for_current_object($this->dn);
400 $smarty->assign("sub_object_is_addable",
401 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
402 !preg_match("/freeze/",$this->FAIstate));
404 $tmp = $this->plInfo();
405 foreach($tmp['plProvidedAcls'] as $name => $translated){
406 $smarty->assign($name."ACL",$this->getacl($name));
407 }
409 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
410 return($display);
411 }
414 /* Generate listbox friendly SubObject list
415 */
416 function getList($use_dns=false){
417 $a_return=array();
418 foreach($this->SubObjects as $obj){
419 if($obj['status'] != "delete"){
421 $cn = stripslashes($obj['cn']);
422 $desc = "";
424 if((isset($obj['description']))&&(!empty($obj['description']))){
425 $desc = " [".stripslashes($obj['description'])."]";
426 }
428 if($use_dns){
429 $a_return[$obj['cn']]['name']= $cn.$desc;
430 $a_return[$obj['cn']]['dn']= $obj['dn'];
431 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
432 }else{
433 $a_return[$obj['cn']] = $cn.$desc;
434 }
435 }
436 }
437 return($a_return);
438 }
441 /* Delete me, and all my subtrees
442 */
443 function remove_from_parent()
444 {
445 if($this->acl_is_removeable()){
446 $ldap = $this->config->get_ldap_link();
447 $ldap->cd ($this->dn);
448 $faifilter = session::get('faifilter');
449 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
450 if($faifilter['branch'] == "main"){
451 $use_dn = $this->dn;
452 }
454 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
456 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
458 foreach($this->SubObjects as $name => $obj){
459 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
460 if($faifilter['branch'] == "main"){
461 $use_dn = $obj['dn'];
462 }
463 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
464 }
465 $this->handle_post_events("remove");
466 }
467 }
470 /* Save data to object
471 */
472 function save_object()
473 {
474 if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
475 plugin::save_object();
476 foreach($this->attributes as $attrs){
477 if(isset($_POST[$attrs])){
478 $this->$attrs = $_POST[$attrs];
479 }
480 }
481 }
483 /* Get sort order */
484 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
485 if($this->sort_by == $_GET['sort']){
486 if($this->sort_order == "up"){
487 $this->sort_order = "down";
488 }elseif($this->sort_order == "down"){
489 $this->sort_order = "up";
490 }
491 }
492 $this->sort_by = $_GET['sort'];
493 }
494 }
497 /* Check supplied data */
498 function check()
499 {
500 /* Call common method to give check the hook */
501 $message= plugin::check();
503 return ($message);
504 }
507 /* Save to LDAP */
508 function save()
509 {
510 plugin::save();
512 $ldap = $this->config->get_ldap_link();
514 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
516 if($this->initially_was_account){
517 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
518 }else{
519 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
520 }
522 /* Prepare FAIscriptEntry to write it to ldap
523 * First sort array.
524 * Because we must delete old entries first.
525 * After deletion, we perform add and modify
526 */
527 $Objects = array();
529 /* We do not need to save untouched objects */
530 foreach($this->SubObjects as $name => $obj){
531 if($obj['status'] == "FreshLoaded"){
532 unset($this->SubObjects[$name]);
533 }
534 }
536 foreach($this->SubObjects as $name => $obj){
537 if($obj['status'] == "delete"){
538 $Objects[$name] = $obj;
539 }
540 }
541 foreach($this->SubObjects as $name => $obj){
542 if($obj['status'] != "delete"){
543 $Objects[$name] = $obj;
544 }
545 }
547 foreach($Objects as $name => $obj){
549 foreach($this->sub64coded as $codeIt){
550 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
551 }
553 $tmp = array();
554 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
555 foreach($attributes as $attrs){
557 if(empty($obj[$attrs])){
558 $obj[$attrs] = array();
559 }
560 if(!is_array($obj[$attrs])){
561 $tmp[$attrs] = stripslashes($obj[$attrs]);
562 }else{
563 $tmp[$attrs] = $obj[$attrs];
564 }
565 }
567 $tmp['objectClass'] = $this->subClasses;
569 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
571 if($obj['status']=="new"){
572 $ldap->cat($sub_dn,array("objectClass"));
573 if($ldap->count()){
574 $obj['status']="edited";
575 }
576 }
578 if(empty($tmp['FAIpriority'])){
579 $tmp['FAIpriority'] ="0";
580 }
582 /* Tag object */
583 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
585 if($obj['status'] == "delete"){
586 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
587 $this->handle_post_events("remove");
588 }elseif($obj['status'] == "edited"){
589 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
590 $this->handle_post_events("modify");
591 }elseif($obj['status']=="new"){
592 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
593 $this->handle_post_events("add");
594 }
595 }
596 }
599 function PrepareForCopyPaste($source)
600 {
601 plugin::PrepareForCopyPaste($source);
603 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
604 */
605 $ldap = $this->config->get_ldap_link();
606 $ldap->cd ($source['dn']);
608 $attrs_to_search = $this->subAttributes;
609 $attrs_to_search[] = "FAIstate";
610 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
612 while($object = $ldap->fetch()){
614 /* Skip objects, that are tagged as removed */
615 if(isset($object['FAIstate'][0])){
616 if(preg_match("/removed$/",$object['FAIstate'][0])){
617 continue;
618 }
619 }
621 /* Set status for save management */
622 $objects = array();
623 $objects['status'] = "edited";
624 $objects['dn'] = $object['dn'];
625 $objects = $this->get_object_attributes($objects,$this->subAttributes);
626 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
628 $this->SubObjects[$objects['cn']] = $objects;
629 }
630 }
633 /* Return plugin informations for acl handling */
634 static function plInfo()
635 {
636 return (array(
637 "plShortName" => _("Script"),
638 "plDescription" => _("FAI script"),
639 "plSelfModify" => FALSE,
640 "plDepends" => array(),
641 "plPriority" => 18,
642 "plSection" => array("administration"),
643 "plCategory" => array("fai"),
644 "plProvidedAcls" => array(
645 "cn" => _("Name")." ("._("Readonly").")",
646 "description" => _("Description"))
647 ));
648 }
649 }
651 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
652 ?>