d94b760c24e9e159ac6dd1b6182422f7117eab56
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_encode($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 /* Ensure that we do not overwrite an allready existing entry
504 */
505 if($this->is_new){
506 $new_dn= 'cn='.$this->cn.",".get_ou('faiscriptou').get_ou('faiou').session::get('CurrentMainBase');
507 $faifilter = session::get('faifilter');
508 if($faifilter['branch']!="main"){
509 $new_dn ='cn='.$this->cn.",".get_ou('faiscriptou').$faifilter['branch'];
510 }
512 $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
513 if(isset($res[$this->cn])){
514 $message[] = msgPool::duplicated(_("Name"));
515 }
516 }
518 return ($message);
519 }
522 /* Save to LDAP */
523 function save()
524 {
525 plugin::save();
527 $ldap = $this->config->get_ldap_link();
529 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
531 if($this->initially_was_account){
532 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
533 }else{
534 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
535 }
537 /* Prepare FAIscriptEntry to write it to ldap
538 * First sort array.
539 * Because we must delete old entries first.
540 * After deletion, we perform add and modify
541 */
542 $Objects = array();
544 /* We do not need to save untouched objects */
545 foreach($this->SubObjects as $name => $obj){
546 if($obj['status'] == "FreshLoaded"){
547 unset($this->SubObjects[$name]);
548 }
549 }
551 foreach($this->SubObjects as $name => $obj){
552 if($obj['status'] == "delete"){
553 $Objects[$name] = $obj;
554 }
555 }
556 foreach($this->SubObjects as $name => $obj){
557 if($obj['status'] != "delete"){
558 $Objects[$name] = $obj;
559 }
560 }
562 foreach($Objects as $name => $obj){
564 foreach($this->sub64coded as $codeIt){
565 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
566 }
568 $tmp = array();
569 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
570 foreach($attributes as $attrs){
572 if(empty($obj[$attrs])){
573 $obj[$attrs] = array();
574 }
575 if(!is_array($obj[$attrs])){
576 $tmp[$attrs] = stripslashes($obj[$attrs]);
577 }else{
578 $tmp[$attrs] = $obj[$attrs];
579 }
580 }
582 $tmp['objectClass'] = $this->subClasses;
584 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
586 if($obj['status']=="new"){
587 $ldap->cat($sub_dn,array("objectClass"));
588 if($ldap->count()){
589 $obj['status']="edited";
590 }
591 }
593 if(empty($tmp['FAIpriority'])){
594 $tmp['FAIpriority'] ="0";
595 }
597 /* Tag object */
598 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
600 if($obj['status'] == "delete"){
601 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
602 $this->handle_post_events("remove");
603 }elseif($obj['status'] == "edited"){
604 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
605 $this->handle_post_events("modify");
606 }elseif($obj['status']=="new"){
607 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
608 $this->handle_post_events("add");
609 }
610 }
611 }
614 function PrepareForCopyPaste($source)
615 {
616 plugin::PrepareForCopyPaste($source);
618 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
619 */
620 $ldap = $this->config->get_ldap_link();
621 $ldap->cd ($source['dn']);
623 $attrs_to_search = $this->subAttributes;
624 $attrs_to_search[] = "FAIstate";
625 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
627 while($object = $ldap->fetch()){
629 /* Skip objects, that are tagged as removed */
630 if(isset($object['FAIstate'][0])){
631 if(preg_match("/removed$/",$object['FAIstate'][0])){
632 continue;
633 }
634 }
636 /* Set status for save management */
637 $objects = array();
638 $objects['status'] = "edited";
639 $objects['dn'] = $object['dn'];
640 $objects = $this->get_object_attributes($objects,$this->subAttributes);
641 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
643 $this->SubObjects[$objects['cn']] = $objects;
644 }
645 }
648 /*! \brief Used for copy & paste.
649 Returns a HTML input mask, which allows to change the cn of this entry.
650 @param Array Array containing current status && a HTML template.
651 */
652 function getCopyDialog()
653 {
654 $vars = array("cn");
655 $smarty = get_smarty();
656 $smarty->assign("cn", htmlentities($this->cn));
657 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
658 $ret = array();
659 $ret['string'] = $str;
660 $ret['status'] = "";
661 return($ret);
662 }
665 /*! \brief Used for copy & paste.
666 Some entries must be renamed to avaoid duplicate entries.
667 */
668 function saveCopyDialog()
669 {
670 if(isset($_POST['cn'])){
671 $this->cn = get_post('cn');
672 }
673 }
676 /* Return plugin informations for acl handling */
677 static function plInfo()
678 {
679 return (array(
680 "plShortName" => _("Script"),
681 "plDescription" => _("FAI script"),
682 "plSelfModify" => FALSE,
683 "plDepends" => array(),
684 "plPriority" => 18,
685 "plSection" => array("administration"),
686 "plCategory" => array("fai"),
687 "plProvidedAcls" => array(
688 "cn" => _("Name")." ("._("Readonly").")",
689 "description" => _("Description"))
690 ));
691 }
692 }
694 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
695 ?>