4f471c07a7b13b183a6d94121bd99df541cad4da
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 = "branch";
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"){
50 $this->dn =$dn;
52 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
53 */
54 $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
55 foreach($res as $obj){
56 $objects = array();
57 $objects['status'] = "FreshLoaded";
58 $objects['dn'] = $obj['dn'];
59 $objects = $this->get_object_attributes($objects,$this->subAttributes);
60 $this->SubObjects[$objects['cn']] = $objects;
61 }
62 }
64 $this->is_new = FALSE;
65 if($this->dn == "new"){
66 $this->is_new =TRUE;
67 }
69 $this->ui = get_userinfo();
70 }
73 /* Reload some attributes */
74 function get_object_attributes($object,$attributes)
75 {
76 $ldap = $this->config->get_ldap_link();
77 $ldap->cd($this->config->current['BASE']);
78 $ldap->cat($object['dn'],$attributes);
79 $tmp = $ldap->fetch();
81 foreach($attributes as $attrs){
82 if(isset($tmp[$attrs][0])){
83 $var = $tmp[$attrs][0];
85 /* Check if we must decode some attributes */
86 if(in_array_ics($attrs,$this->sub64coded)){
87 $var = base64_decode($var);
88 }
90 /* check if this is a binary entry */
91 if(in_array_ics($attrs,$this->subBinary)){
92 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
93 }
95 /* Fix slashes */
96 $var = addslashes($var);
97 $object[$attrs] = $var;
98 }
99 }
100 return($object);
101 }
104 /* Return a valid dn to fetch acls. Because 'new' will not work. */
105 function acl_base_for_current_object($dn)
106 {
107 if($dn == "new"){
108 if($this->dn == "new"){
109 $dn= "cn=dummy,".session::get('CurrentMainBase');
110 }else{
111 $dn = $this->dn;
112 }
113 }
114 return($dn);
115 }
118 function execute()
119 {
120 /* Call parent execute */
121 plugin::execute();
123 if($this->is_account && !$this->view_logged){
124 $this->view_logged = TRUE;
125 new log("view","fai/".get_class($this),$this->dn);
126 }
128 /* Fill templating stuff */
129 $smarty= get_smarty();
130 $display= "";
132 /* Add new sub object */
133 if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
134 $this->dialog= new $this->subClassName($this->config,"new");
135 $this->dialog->FAIstate = $this->FAIstate;
136 $this->dialog->set_acl_base($this->acl_base);
137 $this->dialog->set_acl_category("fai");
138 $this->dialog->parent = &$this;
139 $this->is_dialog=true;
140 }
142 if($this->dn != "new"){
143 session::set('objectinfo',$this->dn);
144 }
146 /* File download requested */
147 if(isset($_GET['getFAIscript'])){
148 if(isset($this->SubObjects[base64_decode($_GET['getFAIscript'])])){
149 $obj = $this->SubObjects[base64_decode($_GET['getFAIscript'])];
150 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
151 send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript");
152 }
153 }
155 /* Handle posts */
156 $s_action = $s_entry = "";
157 foreach($_POST as $name => $value){
159 /* Edit script posted */
160 if(preg_match("/^editscript_/",$name)){
161 $s_action = "edit";
162 $s_entry = preg_replace("/^editscript_/","",$name);
163 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
164 break;
165 }
167 /* Delete script requested */
168 if(preg_match("/^deletescript_/",$name) && !preg_match("/freeze/i",$this->FAIstate)){
169 $s_action = "remove";
170 $s_entry = preg_replace("/^deletescript_/","",$name);
171 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
172 break;
173 }
174 }
176 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
177 $s_entry = base64_decode($_GET['id']);
178 if(isset($this->SubObjects[$s_entry])){
179 $s_action = "edit";
180 }
181 }
183 if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
185 /* Get object, and load missing entry values */
186 $obj = $this->SubObjects[$s_entry];
187 if($obj['status'] == "FreshLoaded"){
188 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
189 }
191 /* Create new dialog and set acl attributes */
192 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
193 $this->dialog->FAIstate = $this->FAIstate;
194 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
195 $this->dialog->set_acl_category("fai");
197 /* Assign some additional dialog informations like headline and parent */
198 session::set('objectinfo',$obj['dn']);
199 $this->dialog->parent = &$this;
200 $this->is_dialog=true;
201 }
203 /* Check acls, are we allowed to delete an entry */
204 if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
205 $entry = $this->SubObjects[$s_entry];
206 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry") ;
207 if(preg_match("/d/",$acl)){
208 $status = $entry['status'];
209 if($status == "edited" || $status == "FreshLoaded"){
210 $this->SubObjects[$s_entry]['status']= "delete";
211 }else{
212 unset($this->SubObjects[$s_entry]);
213 }
214 }
215 }
217 /* Save the edited entry */
218 if(isset($_POST['SaveSubObject'])){
220 /* Check if there are still errors remaining that must be fixed before saving */
221 $this->dialog->save_object();
222 $msgs = $this->dialog->check();
223 if(count($msgs)>0){
224 foreach($msgs as $msg){
225 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
226 }
227 }else{
229 /* Get return object */
230 $obj = $this->dialog->save();
232 /* If we have renamed the script entry, we must remove the old entry */
233 if(isset($obj['remove'])){
235 /* Get old entry values */
236 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
238 /* Depending on status, set new status */
239 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
240 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
241 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
242 unset($this->SubObjects[$obj['remove']['from']]);
243 }
245 /* Append the new entry */
246 $obj['status'] = "new";
247 $this->SubObjects[$obj['remove']['to']] = $obj;
248 unset($this->SubObjects[$obj['remove']['to']]['remove']);
249 }else{
251 /* Set new status and append the entry */
252 if($obj['status'] == "FreshLoaded"){
253 $obj['status'] = "edited";
254 }
255 $this->SubObjects[$obj['cn']]=$obj;
256 }
257 $this->is_dialog=false;
258 unset($this->dialog);
259 $this->dialog=FALSE;
261 }
262 }
264 /* Sort entries */
265 $tmp = $keys = array();
267 if($this->sort_by == "name"){
268 foreach($this->SubObjects as $key => $entry){
269 $keys[$key]=$entry['cn'];
270 }
271 }elseif($this->sort_by == "priority"){
272 foreach($this->SubObjects as $key => $entry){
273 $keys[$key]=$entry['FAIpriority'];
274 }
275 }
277 natcasesort($keys);
279 if($this->sort_order == "down"){
280 $keys =array_reverse($keys);
281 }
283 foreach($keys as $key => $order_var){
284 $tmp[$key]=$this->SubObjects[$key];
285 }
286 $this->SubObjects = $tmp;
288 /* Cancel Dialog */
289 if(isset($_POST['CancelSubObject'])){
290 $this->is_dialog=false;
291 unset($this->dialog);
292 $this->dialog=FALSE;
293 }
295 /* Print dialog if $this->dialog is set */
296 if(is_object($this->dialog)){
297 $this->dialog->save_object();
298 $display = $this->dialog->execute();
299 return($display);
300 }
302 /* Divlist added 23.02.2006
303 Containing FAIscripts
304 */
305 $divlist = new divlist("FAIscripts");
306 $divlist->SetEntriesPerPage(0);
307 $plug = $_GET['plug'];
309 if($this->sort_order == "up"){
310 $dir = "<img src='images/lists/sort-up.png' title='"._("Sort direction")."' alt='\/' border=0>";
311 }else{
312 $dir = "<img src='images/lists/sort-down.png' title='"._("Sort direction")."' alt='/\' border=0>";
313 }
315 if($this->sort_by == "name"){
316 $sort_name = $dir;
317 $sort_prio = "";
318 }else{
319 $sort_name = "";
320 $sort_prio = $dir;
321 }
323 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name").$sort_name."</a>"),
324 array("string"=>"<a href='?plug=".$plug."&sort=priority'>".$sort_prio._("Priority")."</a>",
325 "attach"=>"style='width:100px;'"),
326 array("string"=>_("Download"),
327 "attach"=>"style='width:100px;'"),
328 array("string"=>_("Action"),
329 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
330 $divlist->SetHeight(300);
331 $divlist->SetWidth("100%");
332 foreach($this->getList(true) as $key => $name){
334 $dn= $this->acl_base_for_current_object($name['dn']);
335 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
336 $act = "";
338 /* Hide delete icon if this object is freezed */
339 if(preg_match("/freeze/", $this->FAIstate)){
340 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
341 }else{
342 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
343 if(preg_match("/d/",$acl)){
344 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
345 }
346 }
348 /* Check acls for download icon */
349 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
350 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
351 $down = "";
352 }else{
353 $down = "<a href='?plug=".$_GET['plug']."&getFAIscript=".base64_encode($key)."'>
354 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
355 </a>";
356 }
358 /* Check if we are allowed to view this object */
359 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
360 if(preg_match("/r/",$s_acl)){
361 $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($key)."'>".$name['name']."</a>"),
362 array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
363 array("string"=>$down , "attach" => "style='width:100px;'"),
364 array("string"=>str_replace("%s",base64_encode($key),$act),
365 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
366 }
367 }
368 $smarty->assign("Entry_divlist",$divlist->DrawList());
370 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
371 * If we post the escaped strings they will be escaped again
372 */
373 foreach($this->attributes as $attrs){
374 if(get_magic_quotes_gpc()){
375 $smarty->assign($attrs,stripslashes($this->$attrs));
376 }else{
377 $smarty->assign($attrs,($this->$attrs));
378 }
379 }
381 $dn = $this->acl_base_for_current_object($this->dn);
382 $smarty->assign("sub_object_is_addable",
383 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
384 !preg_match("/freeze/",$this->FAIstate));
386 $tmp = $this->plInfo();
387 foreach($tmp['plProvidedAcls'] as $name => $translated){
388 $smarty->assign($name."ACL",$this->getacl($name));
389 }
391 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
392 return($display);
393 }
396 /* Generate listbox friendly SubObject list
397 */
398 function getList($use_dns=false){
399 $a_return=array();
400 foreach($this->SubObjects as $obj){
401 if($obj['status'] != "delete"){
403 $cn = stripslashes($obj['cn']);
404 $desc = "";
406 if((isset($obj['description']))&&(!empty($obj['description']))){
407 $desc = " [".stripslashes($obj['description'])."]";
408 }
410 if($use_dns){
411 $a_return[$obj['cn']]['name']= $cn.$desc;
412 $a_return[$obj['cn']]['dn']= $obj['dn'];
413 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
414 }else{
415 $a_return[$obj['cn']] = $cn.$desc;
416 }
417 }
418 }
419 return($a_return);
420 }
423 /* Delete me, and all my subtrees
424 */
425 function remove_from_parent()
426 {
427 if($this->acl_is_removeable()){
428 $ldap = $this->config->get_ldap_link();
429 $ldap->cd ($this->dn);
430 $release = $this->parent->parent->fai_release;
431 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
432 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
433 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
435 foreach($this->SubObjects as $name => $obj){
436 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
437 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
438 }
439 $this->handle_post_events("remove");
440 }
441 }
444 /* Save data to object
445 */
446 function save_object()
447 {
448 if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
449 plugin::save_object();
450 foreach($this->attributes as $attrs){
451 if(isset($_POST[$attrs])){
452 $this->$attrs = $_POST[$attrs];
453 }
454 }
455 }
457 /* Get sort order */
458 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
459 if($this->sort_by == $_GET['sort']){
460 if($this->sort_order == "up"){
461 $this->sort_order = "down";
462 }elseif($this->sort_order == "down"){
463 $this->sort_order = "up";
464 }
465 }
466 $this->sort_by = $_GET['sort'];
467 }
468 }
471 /* Check supplied data */
472 function check()
473 {
474 /* Call common method to give check the hook */
475 $message= plugin::check();
477 /* Ensure that we do not overwrite an allready existing entry
478 */
479 if($this->is_new){
480 $release = $this->parent->parent->fai_release;
481 $new_dn= 'cn='.$this->cn.",".get_ou('faiscriptou').get_ou('faiou').$release;
482 $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
483 if(isset($res[$this->cn])){
484 $message[] = msgPool::duplicated(_("Name"));
485 }
486 }
488 return ($message);
489 }
492 /* Save to LDAP */
493 function save()
494 {
495 plugin::save();
497 $ldap = $this->config->get_ldap_link();
499 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
501 if($this->initially_was_account){
502 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
503 }else{
504 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
505 }
507 /* Prepare FAIscriptEntry to write it to ldap
508 * First sort array.
509 * Because we must delete old entries first.
510 * After deletion, we perform add and modify
511 */
512 $Objects = array();
514 /* We do not need to save untouched objects */
515 foreach($this->SubObjects as $name => $obj){
516 if($obj['status'] == "FreshLoaded"){
517 unset($this->SubObjects[$name]);
518 }
519 }
521 foreach($this->SubObjects as $name => $obj){
522 if($obj['status'] == "delete"){
523 $Objects[$name] = $obj;
524 }
525 }
526 foreach($this->SubObjects as $name => $obj){
527 if($obj['status'] != "delete"){
528 $Objects[$name] = $obj;
529 }
530 }
532 foreach($Objects as $name => $obj){
534 foreach($this->sub64coded as $codeIt){
535 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
536 }
538 $tmp = array();
539 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
540 foreach($attributes as $attrs){
542 if(empty($obj[$attrs])){
543 $obj[$attrs] = array();
544 }
545 if(!is_array($obj[$attrs])){
546 $tmp[$attrs] = stripslashes($obj[$attrs]);
547 }else{
548 $tmp[$attrs] = $obj[$attrs];
549 }
550 }
552 $tmp['objectClass'] = $this->subClasses;
554 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
556 if($obj['status']=="new"){
557 $ldap->cat($sub_dn,array("objectClass"));
558 if($ldap->count()){
559 $obj['status']="edited";
560 }
561 }
563 if(empty($tmp['FAIpriority'])){
564 $tmp['FAIpriority'] ="0";
565 }
567 /* Tag object */
568 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
570 if($obj['status'] == "delete"){
571 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
572 $this->handle_post_events("remove");
573 }elseif($obj['status'] == "edited"){
574 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
575 $this->handle_post_events("modify");
576 }elseif($obj['status']=="new"){
577 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
578 $this->handle_post_events("add");
579 }
580 }
581 }
584 function PrepareForCopyPaste($source)
585 {
586 plugin::PrepareForCopyPaste($source);
588 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
589 */
590 $ldap = $this->config->get_ldap_link();
591 $ldap->cd ($source['dn']);
593 $attrs_to_search = $this->subAttributes;
594 $attrs_to_search[] = "FAIstate";
595 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
597 while($object = $ldap->fetch()){
599 /* Skip objects, that are tagged as removed */
600 if(isset($object['FAIstate'][0])){
601 if(preg_match("/removed$/",$object['FAIstate'][0])){
602 continue;
603 }
604 }
606 /* Set status for save management */
607 $objects = array();
608 $objects['status'] = "edited";
609 $objects['dn'] = $object['dn'];
610 $objects = $this->get_object_attributes($objects,$this->subAttributes);
611 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
613 $this->SubObjects[$objects['cn']] = $objects;
614 }
615 }
618 /*! \brief Used for copy & paste.
619 Returns a HTML input mask, which allows to change the cn of this entry.
620 @param Array Array containing current status && a HTML template.
621 */
622 function getCopyDialog()
623 {
624 $vars = array("cn");
625 $smarty = get_smarty();
626 $smarty->assign("cn", htmlentities($this->cn));
627 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
628 $ret = array();
629 $ret['string'] = $str;
630 $ret['status'] = "";
631 return($ret);
632 }
635 /*! \brief Used for copy & paste.
636 Some entries must be renamed to avaoid duplicate entries.
637 */
638 function saveCopyDialog()
639 {
640 if(isset($_POST['cn'])){
641 $this->cn = get_post('cn');
642 }
643 }
646 /* Return plugin informations for acl handling */
647 static function plInfo()
648 {
649 return (array(
650 "plShortName" => _("Script"),
651 "plDescription" => _("FAI script"),
652 "plSelfModify" => FALSE,
653 "plDepends" => array(),
654 "plPriority" => 18,
655 "plSection" => array("administration"),
656 "plCategory" => array("fai"),
657 "plProvidedAcls" => array(
658 "cn" => _("Name")." ("._("Readonly").")",
659 "description" => _("Description"))
660 ));
661 }
662 }
664 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
665 ?>