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[$_GET['getFAIscript']])){
168 $obj = $this->SubObjects[$_GET['getFAIscript']];
169 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
170 send_binary_content($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 = $_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($this->FAIstate == "freeze"){
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=".$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=".$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());
389 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
390 * If we post the escaped strings they will be escaped again
391 */
392 foreach($this->attributes as $attrs){
393 if(get_magic_quotes_gpc()){
394 $smarty->assign($attrs,stripslashes($this->$attrs));
395 }else{
396 $smarty->assign($attrs,($this->$attrs));
397 }
398 }
400 $dn = $this->acl_base_for_current_object($this->dn);
401 $smarty->assign("sub_object_is_addable",
402 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
403 !preg_match("/freeze/",$this->FAIstate));
405 $tmp = $this->plInfo();
406 foreach($tmp['plProvidedAcls'] as $name => $translated){
407 $smarty->assign($name."ACL",$this->getacl($name));
408 }
410 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
411 return($display);
412 }
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"){
420 if($use_dns){
421 if((isset($obj['description']))&&(!empty($obj['description']))){
422 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
423 }else{
424 $a_return[$obj['cn']]['name']= $obj['cn'];
425 }
426 $a_return[$obj['cn']]['dn']= $obj['dn'];
427 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
428 }else{
429 if((isset($obj['description']))&&(!empty($obj['description']))){
430 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
431 }else{
432 $a_return[$obj['cn']]= $obj['cn'];
433 }
434 }
435 }
436 }
437 return($a_return);
438 }
440 /* Delete me, and all my subtrees
441 */
442 function remove_from_parent()
443 {
444 if($this->acl_is_removeable()){
445 $ldap = $this->config->get_ldap_link();
446 $ldap->cd ($this->dn);
447 $faifilter = session::get('faifilter');
448 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
449 if($faifilter['branch'] == "main"){
450 $use_dn = $this->dn;
451 }
453 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
455 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
457 foreach($this->SubObjects as $name => $obj){
458 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
459 if($faifilter['branch'] == "main"){
460 $use_dn = $obj['dn'];
461 }
462 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
463 }
464 $this->handle_post_events("remove");
465 }
466 }
469 /* Save data to object
470 */
471 function save_object()
472 {
473 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
474 plugin::save_object();
475 foreach($this->attributes as $attrs){
476 if(isset($_POST[$attrs])){
477 $this->$attrs = $_POST[$attrs];
478 }
479 }
480 }
482 /* Get sort order */
483 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
484 if($this->sort_by == $_GET['sort']){
485 if($this->sort_order == "up"){
486 $this->sort_order = "down";
487 }elseif($this->sort_order == "down"){
488 $this->sort_order = "up";
489 }
490 }
491 $this->sort_by = $_GET['sort'];
492 }
493 }
496 /* Check supplied data */
497 function check()
498 {
499 /* Call common method to give check the hook */
500 $message= plugin::check();
502 return ($message);
503 }
506 /* Save to LDAP */
507 function save()
508 {
509 plugin::save();
511 $ldap = $this->config->get_ldap_link();
513 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
514 if (!$ldap->success()){
515 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
516 }
518 if($this->initially_was_account){
519 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
520 }else{
521 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
522 }
524 /* Prepare FAIscriptEntry to write it to ldap
525 * First sort array.
526 * Because we must delete old entries first.
527 * After deletion, we perform add and modify
528 */
529 $Objects = array();
531 /* We do not need to save untouched objects */
532 foreach($this->SubObjects as $name => $obj){
533 if($obj['status'] == "FreshLoaded"){
534 unset($this->SubObjects[$name]);
535 }
536 }
538 foreach($this->SubObjects as $name => $obj){
539 if($obj['status'] == "delete"){
540 $Objects[$name] = $obj;
541 }
542 }
543 foreach($this->SubObjects as $name => $obj){
544 if($obj['status'] != "delete"){
545 $Objects[$name] = $obj;
546 }
547 }
549 foreach($Objects as $name => $obj){
551 foreach($this->sub64coded as $codeIt){
552 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
553 }
555 $tmp = array();
556 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
557 foreach($attributes as $attrs){
559 if(empty($obj[$attrs])){
560 $obj[$attrs] = array();
561 }
562 if(!is_array($obj[$attrs])){
563 $tmp[$attrs] = stripslashes($obj[$attrs]);
564 }else{
565 $tmp[$attrs] = $obj[$attrs];
566 }
567 }
569 $tmp['objectClass'] = $this->subClasses;
571 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
573 if($obj['status']=="new"){
574 $ldap->cat($sub_dn,array("objectClass"));
575 if($ldap->count()){
576 $obj['status']="edited";
577 }
578 }
580 if(empty($tmp['FAIpriority'])){
581 $tmp['FAIpriority'] ="0";
582 }
584 /* Tag object */
585 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
587 if($obj['status'] == "delete"){
588 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
589 $this->handle_post_events("remove");
590 }elseif($obj['status'] == "edited"){
591 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
592 $this->handle_post_events("modify");
593 }elseif($obj['status']=="new"){
594 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
595 $this->handle_post_events("add");
596 }
597 }
598 }
601 function PrepareForCopyPaste($source)
602 {
603 plugin::PrepareForCopyPaste($source);
605 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
606 */
607 $ldap = $this->config->get_ldap_link();
608 $ldap->cd ($source['dn']);
610 $attrs_to_search = $this->subAttributes;
611 $attrs_to_search[] = "FAIstate";
612 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
614 while($object = $ldap->fetch()){
616 /* Skip objects, that are tagged as removed */
617 if(isset($object['FAIstate'][0])){
618 if(preg_match("/removed$/",$object['FAIstate'][0])){
619 continue;
620 }
621 }
623 /* Set status for save management */
624 $objects = array();
625 $objects['status'] = "edited";
626 $objects['dn'] = $object['dn'];
627 $objects = $this->get_object_attributes($objects,$this->subAttributes);
628 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
630 $this->SubObjects[$objects['cn']] = $objects;
631 }
632 }
635 /* Return plugin informations for acl handling */
636 static function plInfo()
637 {
638 return (array(
639 "plShortName" => _("Script"),
640 "plDescription" => _("FAI script"),
641 "plSelfModify" => FALSE,
642 "plDepends" => array(),
643 "plPriority" => 18,
644 "plSection" => array("administration"),
645 "plCategory" => array("fai"),
646 "plProvidedAcls" => array(
647 "cn" => _("Name")." ("._("Readonly").")",
648 "description" => _("Description"))
649 ));
650 }
651 }
653 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
654 ?>