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 }
90 $this->is_new = FALSE;
91 if($this->dn == "new"){
92 $this->is_new =TRUE;
93 }
95 $this->ui = get_userinfo();
96 }
99 /* Reload some attributes */
100 function get_object_attributes($object,$attributes)
101 {
102 $ldap = $this->config->get_ldap_link();
103 $ldap->cd($this->config->current['BASE']);
104 $ldap->cat($object['dn'],$attributes);
105 $tmp = $ldap->fetch();
107 foreach($attributes as $attrs){
108 if(isset($tmp[$attrs][0])){
109 $var = $tmp[$attrs][0];
111 /* Check if we must decode some attributes */
112 if(in_array_ics($attrs,$this->sub64coded)){
113 $var = base64_decode($var);
114 }
116 /* check if this is a binary entry */
117 if(in_array_ics($attrs,$this->subBinary)){
118 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
119 }
121 /* Fix slashes */
122 $var = addslashes($var);
123 $object[$attrs] = $var;
124 }
125 }
126 return($object);
127 }
130 /* Return a valid dn to fetch acls. Because 'new' will not work. */
131 function acl_base_for_current_object($dn)
132 {
133 if($dn == "new"){
134 if($this->dn == "new"){
135 $dn= "cn=dummy,".session::get('CurrentMainBase');
136 }else{
137 $dn = $this->dn;
138 }
139 }
140 return($dn);
141 }
144 function execute()
145 {
146 /* Call parent execute */
147 plugin::execute();
149 if($this->is_account && !$this->view_logged){
150 $this->view_logged = TRUE;
151 new log("view","fai/".get_class($this),$this->dn);
152 }
154 /* Fill templating stuff */
155 $smarty= get_smarty();
156 $display= "";
158 /* Add new sub object */
159 if(isset($_POST['AddSubObject'])){
160 $this->dialog= new $this->subClassName($this->config,"new");
161 $this->dialog->set_acl_base($this->acl_base);
162 $this->dialog->set_acl_category("fai");
163 $this->dialog->parent = &$this;
164 $this->is_dialog=true;
165 }
167 if($this->dn != "new"){
168 session::set('objectinfo',$this->dn);
169 }
171 /* File download requested */
172 if(isset($_GET['getFAIscript'])){
173 if(isset($this->SubObjects[base64_decode($_GET['getFAIscript'])])){
174 $obj = $this->SubObjects[base64_decode($_GET['getFAIscript'])];
175 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
176 send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript");
177 }
178 }
180 /* Handle posts */
181 $s_action = $s_entry = "";
182 foreach($_POST as $name => $value){
184 /* Edit script posted */
185 if(preg_match("/^editscript_/",$name)){
186 $s_action = "edit";
187 $s_entry = preg_replace("/^editscript_/","",$name);
188 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
189 break;
190 }
192 /* Delete script requested */
193 if(preg_match("/^deletescript_/",$name)){
194 $s_action = "remove";
195 $s_entry = preg_replace("/^deletescript_/","",$name);
196 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
197 break;
198 }
199 }
201 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
202 $s_entry = base64_decode($_GET['id']);
203 if(isset($this->SubObjects[$s_entry])){
204 $s_action = "edit";
205 }
206 }
208 if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
210 /* Get object, and load missing entry values */
211 $obj = $this->SubObjects[$s_entry];
212 if($obj['status'] == "FreshLoaded"){
213 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
214 }
216 /* Create new dialog and set acl attributes */
217 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
218 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
219 $this->dialog->set_acl_category("fai");
221 /* Assign some additional dialog informations like headline and parent */
222 session::set('objectinfo',$obj['dn']);
223 $this->dialog->parent = &$this;
224 $this->is_dialog=true;
225 }
227 /* Check acls, are we allowed to delete an entry */
228 if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
229 $entry = $this->SubObjects[$s_entry];
230 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry") ;
231 if(preg_match("/d/",$acl)){
232 $status = $entry['status'];
233 if($status == "edited" || $status == "FreshLoaded"){
234 $this->SubObjects[$s_entry]['status']= "delete";
235 }else{
236 unset($this->SubObjects[$s_entry]);
237 }
238 }
239 }
241 /* Save the edited entry */
242 if(isset($_POST['SaveSubObject'])){
244 /* Check if there are still errors remaining that must be fixed before saving */
245 $this->dialog->save_object();
246 $msgs = $this->dialog->check();
247 if(count($msgs)>0){
248 foreach($msgs as $msg){
249 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
250 }
251 }else{
253 /* Get return object */
254 $obj = $this->dialog->save();
256 /* If we have renamed the script entry, we must remove the old entry */
257 if(isset($obj['remove'])){
259 /* Get old entry values */
260 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
262 /* Depending on status, set new status */
263 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
264 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
265 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
266 unset($this->SubObjects[$obj['remove']['from']]);
267 }
269 /* Append the new entry */
270 $obj['status'] = "new";
271 $this->SubObjects[$obj['remove']['to']] = $obj;
272 unset($this->SubObjects[$obj['remove']['to']]['remove']);
273 }else{
275 /* Set new status and append the entry */
276 if($obj['status'] == "FreshLoaded"){
277 $obj['status'] = "edited";
278 }
279 $this->SubObjects[$obj['cn']]=$obj;
280 }
281 $this->is_dialog=false;
282 unset($this->dialog);
283 $this->dialog=FALSE;
285 }
286 }
288 /* Sort entries */
289 $tmp = $keys = array();
291 if($this->sort_by == "name"){
292 foreach($this->SubObjects as $key => $entry){
293 $keys[$key]=$entry['cn'];
294 }
295 }elseif($this->sort_by == "priority"){
296 foreach($this->SubObjects as $key => $entry){
297 $keys[$key]=$entry['FAIpriority'];
298 }
299 }
301 natcasesort($keys);
303 if($this->sort_order == "down"){
304 $keys =array_reverse($keys);
305 }
307 foreach($keys as $key => $order_var){
308 $tmp[$key]=$this->SubObjects[$key];
309 }
310 $this->SubObjects = $tmp;
312 /* Cancel Dialog */
313 if(isset($_POST['CancelSubObject'])){
314 $this->is_dialog=false;
315 unset($this->dialog);
316 $this->dialog=FALSE;
317 }
319 /* Print dialog if $this->dialog is set */
320 if(is_object($this->dialog)){
321 $this->dialog->save_object();
322 $display = $this->dialog->execute();
323 return($display);
324 }
326 /* Divlist added 23.02.2006
327 Containing FAIscripts
328 */
329 $divlist = new divlist("FAIscripts");
330 $divlist->SetEntriesPerPage(0);
331 $plug = $_GET['plug'];
333 if($this->sort_order == "up"){
334 $dir = "<img src='images/sort_up.png' title='"._("Sort direction")."' alt='\/' border=0>";
335 }else{
336 $dir = "<img src='images/sort_down.png' title='"._("Sort direction")."' alt='/\' border=0>";
337 }
339 if($this->sort_by == "name"){
340 $sort_name = $dir;
341 $sort_prio = "";
342 }else{
343 $sort_name = "";
344 $sort_prio = $dir;
345 }
347 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name").$sort_name."</a>"),
348 array("string"=>"<a href='?plug=".$plug."&sort=priority'>".$sort_prio._("Priority")."</a>",
349 "attach"=>"style='width:100px;'"),
350 array("string"=>_("Download"),
351 "attach"=>"style='width:100px;'"),
352 array("string"=>_("Action"),
353 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
354 $divlist->SetHeight(300);
355 $divlist->SetWidth("100%");
356 foreach($this->getList(true) as $key => $name){
358 $dn= $this->acl_base_for_current_object($name['dn']);
359 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
360 $act = "";
362 /* Hide delete icon if this object is freezed */
363 if(preg_match("/freeze/", $this->FAIstate)){
364 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
365 }else{
366 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
367 if(preg_match("/d/",$acl)){
368 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
369 }
370 }
372 /* Check acls for download icon */
373 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
374 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
375 $down = "";
376 }else{
377 $down = "<a href='?plug=".$_GET['plug']."&getFAIscript=".base64_encode($key)."'>
378 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
379 </a>";
380 }
382 /* Check if we are allowed to view this object */
383 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
384 if(preg_match("/r/",$s_acl)){
385 $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($key)."'>".$name['name']."</a>"),
386 array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
387 array("string"=>$down , "attach" => "style='width:100px;'"),
388 array("string"=>str_replace("%s",base64_encode($key),$act),
389 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
390 }
391 }
392 $smarty->assign("Entry_divlist",$divlist->DrawList());
394 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
395 * If we post the escaped strings they will be escaped again
396 */
397 foreach($this->attributes as $attrs){
398 if(get_magic_quotes_gpc()){
399 $smarty->assign($attrs,stripslashes($this->$attrs));
400 }else{
401 $smarty->assign($attrs,($this->$attrs));
402 }
403 }
405 $dn = $this->acl_base_for_current_object($this->dn);
406 $smarty->assign("sub_object_is_addable",
407 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
408 !preg_match("/freeze/",$this->FAIstate));
410 $tmp = $this->plInfo();
411 foreach($tmp['plProvidedAcls'] as $name => $translated){
412 $smarty->assign($name."ACL",$this->getacl($name));
413 }
415 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
416 return($display);
417 }
420 /* Generate listbox friendly SubObject list
421 */
422 function getList($use_dns=false){
423 $a_return=array();
424 foreach($this->SubObjects as $obj){
425 if($obj['status'] != "delete"){
427 $cn = stripslashes($obj['cn']);
428 $desc = "";
430 if((isset($obj['description']))&&(!empty($obj['description']))){
431 $desc = " [".stripslashes($obj['description'])."]";
432 }
434 if($use_dns){
435 $a_return[$obj['cn']]['name']= $cn.$desc;
436 $a_return[$obj['cn']]['dn']= $obj['dn'];
437 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
438 }else{
439 $a_return[$obj['cn']] = $cn.$desc;
440 }
441 }
442 }
443 return($a_return);
444 }
447 /* Delete me, and all my subtrees
448 */
449 function remove_from_parent()
450 {
451 if($this->acl_is_removeable()){
452 $ldap = $this->config->get_ldap_link();
453 $ldap->cd ($this->dn);
454 $faifilter = session::get('faifilter');
455 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
456 if($faifilter['branch'] == "main"){
457 $use_dn = $this->dn;
458 }
460 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
462 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
464 foreach($this->SubObjects as $name => $obj){
465 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
466 if($faifilter['branch'] == "main"){
467 $use_dn = $obj['dn'];
468 }
469 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
470 }
471 $this->handle_post_events("remove");
472 }
473 }
476 /* Save data to object
477 */
478 function save_object()
479 {
480 if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
481 plugin::save_object();
482 foreach($this->attributes as $attrs){
483 if(isset($_POST[$attrs])){
484 $this->$attrs = $_POST[$attrs];
485 }
486 }
487 }
489 /* Get sort order */
490 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
491 if($this->sort_by == $_GET['sort']){
492 if($this->sort_order == "up"){
493 $this->sort_order = "down";
494 }elseif($this->sort_order == "down"){
495 $this->sort_order = "up";
496 }
497 }
498 $this->sort_by = $_GET['sort'];
499 }
500 }
503 /* Check supplied data */
504 function check()
505 {
506 /* Call common method to give check the hook */
507 $message= plugin::check();
509 /* Ensure that we do not overwrite an allready existing entry
510 */
511 if($this->is_new){
512 $new_dn= 'cn='.$this->cn.",".get_ou('faiscriptou').get_ou('faiou').session::get('CurrentMainBase');
513 $faifilter = session::get('faifilter');
514 if($faifilter['branch']!="main"){
515 $new_dn ='cn='.$this->cn.",".get_ou('faiscriptou').$faifilter['branch'];
516 }
518 $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
519 if(isset($res[$this->cn])){
520 $message[] = msgPool::duplicated(_("Name"));
521 }
522 }
524 return ($message);
525 }
528 /* Save to LDAP */
529 function save()
530 {
531 plugin::save();
533 $ldap = $this->config->get_ldap_link();
535 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
537 if($this->initially_was_account){
538 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
539 }else{
540 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
541 }
543 /* Prepare FAIscriptEntry to write it to ldap
544 * First sort array.
545 * Because we must delete old entries first.
546 * After deletion, we perform add and modify
547 */
548 $Objects = array();
550 /* We do not need to save untouched objects */
551 foreach($this->SubObjects as $name => $obj){
552 if($obj['status'] == "FreshLoaded"){
553 unset($this->SubObjects[$name]);
554 }
555 }
557 foreach($this->SubObjects as $name => $obj){
558 if($obj['status'] == "delete"){
559 $Objects[$name] = $obj;
560 }
561 }
562 foreach($this->SubObjects as $name => $obj){
563 if($obj['status'] != "delete"){
564 $Objects[$name] = $obj;
565 }
566 }
568 foreach($Objects as $name => $obj){
570 foreach($this->sub64coded as $codeIt){
571 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
572 }
574 $tmp = array();
575 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
576 foreach($attributes as $attrs){
578 if(empty($obj[$attrs])){
579 $obj[$attrs] = array();
580 }
581 if(!is_array($obj[$attrs])){
582 $tmp[$attrs] = stripslashes($obj[$attrs]);
583 }else{
584 $tmp[$attrs] = $obj[$attrs];
585 }
586 }
588 $tmp['objectClass'] = $this->subClasses;
590 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
592 if($obj['status']=="new"){
593 $ldap->cat($sub_dn,array("objectClass"));
594 if($ldap->count()){
595 $obj['status']="edited";
596 }
597 }
599 if(empty($tmp['FAIpriority'])){
600 $tmp['FAIpriority'] ="0";
601 }
603 /* Tag object */
604 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
606 if($obj['status'] == "delete"){
607 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
608 $this->handle_post_events("remove");
609 }elseif($obj['status'] == "edited"){
610 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
611 $this->handle_post_events("modify");
612 }elseif($obj['status']=="new"){
613 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
614 $this->handle_post_events("add");
615 }
616 }
617 }
620 function PrepareForCopyPaste($source)
621 {
622 plugin::PrepareForCopyPaste($source);
624 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
625 */
626 $ldap = $this->config->get_ldap_link();
627 $ldap->cd ($source['dn']);
629 $attrs_to_search = $this->subAttributes;
630 $attrs_to_search[] = "FAIstate";
631 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
633 while($object = $ldap->fetch()){
635 /* Skip objects, that are tagged as removed */
636 if(isset($object['FAIstate'][0])){
637 if(preg_match("/removed$/",$object['FAIstate'][0])){
638 continue;
639 }
640 }
642 /* Set status for save management */
643 $objects = array();
644 $objects['status'] = "edited";
645 $objects['dn'] = $object['dn'];
646 $objects = $this->get_object_attributes($objects,$this->subAttributes);
647 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
649 $this->SubObjects[$objects['cn']] = $objects;
650 }
651 }
654 /*! \brief Used for copy & paste.
655 Returns a HTML input mask, which allows to change the cn of this entry.
656 @param Array Array containing current status && a HTML template.
657 */
658 function getCopyDialog()
659 {
660 $vars = array("cn");
661 $smarty = get_smarty();
662 $smarty->assign("cn", htmlentities($this->cn));
663 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
664 $ret = array();
665 $ret['string'] = $str;
666 $ret['status'] = "";
667 return($ret);
668 }
671 /*! \brief Used for copy & paste.
672 Some entries must be renamed to avaoid duplicate entries.
673 */
674 function saveCopyDialog()
675 {
676 if(isset($_POST['cn'])){
677 $this->cn = get_post('cn');
678 }
679 }
682 /* Return plugin informations for acl handling */
683 static function plInfo()
684 {
685 return (array(
686 "plShortName" => _("Script"),
687 "plDescription" => _("FAI script"),
688 "plSelfModify" => FALSE,
689 "plDepends" => array(),
690 "plPriority" => 18,
691 "plSection" => array("administration"),
692 "plCategory" => array("fai"),
693 "plProvidedAcls" => array(
694 "cn" => _("Name")." ("._("Readonly").")",
695 "description" => _("Description"))
696 ));
697 }
698 }
700 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
701 ?>