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){
57 /* Skip not relevant objects */
58 if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
60 $objects = array();
61 $objects['status'] = "FreshLoaded";
62 $objects['dn'] = $obj['dn'];
63 $objects = $this->get_object_attributes($objects,$this->subAttributes);
64 $this->SubObjects[$objects['cn']] = $objects;
65 }
66 }
68 $this->is_new = FALSE;
69 if($this->dn == "new"){
70 $this->is_new =TRUE;
71 }
73 $this->ui = get_userinfo();
74 }
77 /* Reload some attributes */
78 function get_object_attributes($object,$attributes)
79 {
80 $ldap = $this->config->get_ldap_link();
81 $ldap->cd($this->config->current['BASE']);
82 $ldap->cat($object['dn'],$attributes);
83 $tmp = $ldap->fetch();
85 foreach($attributes as $attrs){
86 if(isset($tmp[$attrs][0])){
87 $var = $tmp[$attrs][0];
89 /* Check if we must decode some attributes */
90 if(in_array_ics($attrs,$this->sub64coded)){
91 $var = base64_decode($var);
92 }
94 /* check if this is a binary entry */
95 if(in_array_ics($attrs,$this->subBinary)){
96 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
97 }
99 /* Fix slashes */
100 $var = addslashes($var);
101 $object[$attrs] = $var;
102 }
103 }
104 return($object);
105 }
108 /* Return a valid dn to fetch acls. Because 'new' will not work. */
109 function acl_base_for_current_object($dn)
110 {
111 if($dn == "new" || $dn == ""){
112 if($this->dn == "new"){
113 $dn= $this->parent->parent->acl_base;
114 }else{
115 $dn = $this->dn;
116 }
117 }
118 return($dn);
119 }
122 function execute()
123 {
124 /* Call parent execute */
125 plugin::execute();
127 if($this->is_account && !$this->view_logged){
128 $this->view_logged = TRUE;
129 new log("view","fai/".get_class($this),$this->dn);
130 }
132 /* Fill templating stuff */
133 $smarty= get_smarty();
134 $display= "";
136 /* Add new sub object */
137 if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
138 $this->dialog= new $this->subClassName($this->config,"new");
139 $this->dialog->FAIstate = $this->FAIstate;
140 $this->dialog->set_acl_base($this->acl_base);
141 $this->dialog->set_acl_category("fai");
142 $this->dialog->parent = &$this;
143 $this->is_dialog=true;
144 }
146 if($this->dn != "new"){
147 set_object_info($this->dn);
148 }
150 /* File download requested */
151 if(isset($_GET['getFAIscript'])){
152 if(isset($this->SubObjects[base64_decode($_GET['getFAIscript'])])){
153 $obj = $this->SubObjects[base64_decode($_GET['getFAIscript'])];
154 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
155 send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIscript");
156 }
157 }
159 /* Handle posts */
160 $s_action = $s_entry = "";
161 foreach($_POST as $name => $value){
163 /* Edit script posted */
164 if(preg_match("/^editscript_/",$name)){
165 $s_action = "edit";
166 $s_entry = preg_replace("/^editscript_/","",$name);
167 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
168 break;
169 }
171 /* Delete script requested */
172 if(preg_match("/^deletescript_/",$name) && !preg_match("/freeze/i",$this->FAIstate)){
173 $s_action = "remove";
174 $s_entry = preg_replace("/^deletescript_/","",$name);
175 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
176 break;
177 }
178 }
180 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
181 $s_entry = base64_decode($_GET['id']);
182 if(isset($this->SubObjects[$s_entry])){
183 $s_action = "edit";
184 }
185 }
187 if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
189 /* Get object, and load missing entry values */
190 $obj = $this->SubObjects[$s_entry];
191 if($obj['status'] == "FreshLoaded"){
192 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
193 }
195 /* Create new dialog and set acl attributes */
196 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
197 $this->dialog->FAIstate = $this->FAIstate;
198 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
199 $this->dialog->set_acl_category("fai");
201 /* Assign some additional dialog informations like headline and parent */
202 set_object_info($obj['dn']);
203 $this->dialog->parent = &$this;
204 $this->is_dialog=true;
205 }
207 /* Check acls, are we allowed to delete an entry */
208 if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
209 $entry = $this->SubObjects[$s_entry];
210 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry") ;
211 if(preg_match("/d/",$acl)){
212 $status = $entry['status'];
213 if($status == "edited" || $status == "FreshLoaded"){
214 $this->SubObjects[$s_entry]['status']= "delete";
215 }else{
216 unset($this->SubObjects[$s_entry]);
217 }
218 }
219 }
221 /* Save the edited entry */
222 if(isset($_POST['SaveSubObject'])){
224 /* Check if there are still errors remaining that must be fixed before saving */
225 $this->dialog->save_object();
226 $msgs = $this->dialog->check();
227 if(count($msgs)>0){
228 foreach($msgs as $msg){
229 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
230 }
231 }else{
233 /* Get return object */
234 $obj = $this->dialog->save();
236 /* If we have renamed the script entry, we must remove the old entry */
237 if(isset($obj['remove'])){
239 /* Get old entry values */
240 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
242 /* Depending on status, set new status */
243 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
244 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
245 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
246 unset($this->SubObjects[$obj['remove']['from']]);
247 }
249 /* Append the new entry */
250 $obj['status'] = "new";
251 $this->SubObjects[$obj['remove']['to']] = $obj;
252 unset($this->SubObjects[$obj['remove']['to']]['remove']);
253 }else{
255 /* Set new status and append the entry */
256 if($obj['status'] == "FreshLoaded"){
257 $obj['status'] = "edited";
258 }
259 $this->SubObjects[$obj['cn']]=$obj;
260 }
261 $this->is_dialog=false;
262 unset($this->dialog);
263 $this->dialog=FALSE;
265 }
266 }
268 /* Sort entries */
269 $tmp = $keys = array();
271 if($this->sort_by == "name"){
272 foreach($this->SubObjects as $key => $entry){
273 $keys[$key]=$entry['cn'];
274 }
275 }elseif($this->sort_by == "priority"){
276 foreach($this->SubObjects as $key => $entry){
277 $keys[$key]=$entry['FAIpriority'];
278 }
279 }
281 natcasesort($keys);
283 if($this->sort_order == "down"){
284 $keys =array_reverse($keys);
285 }
287 foreach($keys as $key => $order_var){
288 $tmp[$key]=$this->SubObjects[$key];
289 }
290 $this->SubObjects = $tmp;
292 /* Cancel Dialog */
293 if(isset($_POST['CancelSubObject'])){
294 $this->is_dialog=false;
295 unset($this->dialog);
296 $this->dialog=FALSE;
297 }
299 /* Print dialog if $this->dialog is set */
300 if(is_object($this->dialog)){
301 $this->dialog->save_object();
302 $display = $this->dialog->execute();
303 return($display);
304 }
306 /* Divlist added 23.02.2006
307 Containing FAIscripts
308 */
309 $divlist = new divlist("FAIscripts");
310 $divlist->SetEntriesPerPage(0);
311 $plug = $_GET['plug'];
313 if($this->sort_order == "up"){
314 $dir = "<img src='images/lists/sort-up.png' title='"._("Sort direction")."' alt='\/' border=0>";
315 }else{
316 $dir = "<img src='images/lists/sort-down.png' title='"._("Sort direction")."' alt='/\' border=0>";
317 }
319 if($this->sort_by == "name"){
320 $sort_name = $dir;
321 $sort_prio = "";
322 }else{
323 $sort_name = "";
324 $sort_prio = $dir;
325 }
327 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name").$sort_name."</a>"),
328 array("string"=>"<a href='?plug=".$plug."&sort=priority'>".$sort_prio._("Priority")."</a>",
329 "attach"=>"style='width:100px;'"),
330 array("string"=>_("Download"),
331 "attach"=>"style='width:100px;'"),
332 array("string"=>_("Action"),
333 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
334 $divlist->SetHeight(300);
335 $divlist->SetWidth("100%");
336 foreach($this->getList(true) as $key => $name){
338 $dn= $this->acl_base_for_current_object($name['dn']);
339 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
340 $act = "";
342 /* Hide delete icon if this object is freezed */
343 if(preg_match("/freeze/", $this->FAIstate)){
344 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
345 }else{
346 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
347 if(preg_match("/d/",$acl)){
348 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
349 }
350 }
352 /* Check acls for download icon */
353 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
354 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
355 $down = "";
356 }else{
357 $down = "<a href='?plug=".$_GET['plug']."&getFAIscript=".base64_encode($key)."'>
358 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
359 </a>";
360 }
362 /* Check if we are allowed to view this object */
363 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
364 if(preg_match("/r/",$s_acl)){
365 $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($key)."'>".$name['name']."</a>"),
366 array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
367 array("string"=>$down , "attach" => "style='width:100px;'"),
368 array("string"=>str_replace("%s",base64_encode($key),$act),
369 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
370 }
371 }
372 $smarty->assign("Entry_divlist",$divlist->DrawList());
374 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
375 * If we post the escaped strings they will be escaped again
376 */
377 foreach($this->attributes as $attrs){
378 if(get_magic_quotes_gpc()){
379 $smarty->assign($attrs,stripslashes($this->$attrs));
380 }else{
381 $smarty->assign($attrs,($this->$attrs));
382 }
383 }
385 $dn = $this->acl_base_for_current_object($this->dn);
386 $smarty->assign("sub_object_is_addable",
387 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
388 !preg_match("/freeze/",$this->FAIstate));
390 $tmp = $this->plInfo();
391 foreach($tmp['plProvidedAcls'] as $name => $translated){
392 $smarty->assign($name."ACL",$this->getacl($name));
393 }
395 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
396 return($display);
397 }
400 /* Generate listbox friendly SubObject list
401 */
402 function getList($use_dns=false){
403 $a_return=array();
404 foreach($this->SubObjects as $obj){
405 if($obj['status'] != "delete"){
407 $cn = stripslashes($obj['cn']);
408 $desc = "";
410 if((isset($obj['description']))&&(!empty($obj['description']))){
411 $desc = " [".stripslashes($obj['description'])."]";
412 }
414 if($use_dns){
415 $a_return[$obj['cn']]['name']= $cn.$desc;
416 $a_return[$obj['cn']]['dn']= $obj['dn'];
417 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
418 }else{
419 $a_return[$obj['cn']] = $cn.$desc;
420 }
421 }
422 }
423 return($a_return);
424 }
427 /* Delete me, and all my subtrees
428 */
429 function remove_from_parent()
430 {
431 if($this->acl_is_removeable()){
432 $ldap = $this->config->get_ldap_link();
433 $ldap->cd ($this->dn);
434 $release = $this->parent->parent->fai_release;
435 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
436 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
437 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
439 foreach($this->SubObjects as $name => $obj){
440 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
441 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
442 }
443 $this->handle_post_events("remove");
444 }
445 }
448 /* Save data to object
449 */
450 function save_object()
451 {
452 if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
453 plugin::save_object();
454 }
456 /* Get sort order */
457 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
458 if($this->sort_by == $_GET['sort']){
459 if($this->sort_order == "up"){
460 $this->sort_order = "down";
461 }elseif($this->sort_order == "down"){
462 $this->sort_order = "up";
463 }
464 }
465 $this->sort_by = $_GET['sort'];
466 }
467 }
470 /* Check supplied data */
471 function check()
472 {
473 /* Call common method to give check the hook */
474 $message= plugin::check();
476 /* Ensure that we do not overwrite an allready existing entry
477 */
478 if($this->is_new){
479 $release = $this->parent->parent->fai_release;
480 $new_dn= 'cn='.$this->cn.",".get_ou('faiScriptRDN').get_ou('faiBaseRDN').$release;
481 $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
482 if(isset($res[$this->cn])){
483 $message[] = msgPool::duplicated(_("Name"));
484 }
485 }
487 return ($message);
488 }
491 /* Save to LDAP */
492 function save()
493 {
494 plugin::save();
496 $ldap = $this->config->get_ldap_link();
498 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
500 if($this->initially_was_account){
501 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
502 }else{
503 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
504 }
506 /* Prepare FAIscriptEntry to write it to ldap
507 * First sort array.
508 * Because we must delete old entries first.
509 * After deletion, we perform add and modify
510 */
511 $Objects = array();
513 /* We do not need to save untouched objects */
514 foreach($this->SubObjects as $name => $obj){
515 if($obj['status'] == "FreshLoaded"){
516 unset($this->SubObjects[$name]);
517 }
518 }
520 foreach($this->SubObjects as $name => $obj){
521 if($obj['status'] == "delete"){
522 $Objects[$name] = $obj;
523 }
524 }
525 foreach($this->SubObjects as $name => $obj){
526 if($obj['status'] != "delete"){
527 $Objects[$name] = $obj;
528 }
529 }
531 foreach($Objects as $name => $obj){
533 foreach($this->sub64coded as $codeIt){
534 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
535 }
537 $tmp = array();
538 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
539 foreach($attributes as $attrs){
540 if(!isset($obj[$attrs])) continue;
541 if($obj[$attrs] == ""){
542 $obj[$attrs] = array();
543 }
544 if(!is_array($obj[$attrs])){
545 $tmp[$attrs] = stripslashes($obj[$attrs]);
546 }else{
547 $tmp[$attrs] = $obj[$attrs];
548 }
549 }
551 $tmp['objectClass'] = $this->subClasses;
553 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
555 if($obj['status']=="new"){
556 $ldap->cat($sub_dn,array("objectClass"));
557 if($ldap->count()){
558 $obj['status']="edited";
559 }
560 }
562 if(empty($tmp['FAIpriority'])){
563 $tmp['FAIpriority'] ="0";
564 }
566 /* Tag object */
567 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
569 if($obj['status'] == "delete"){
570 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
571 $this->handle_post_events("remove");
572 }elseif($obj['status'] == "edited"){
573 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
574 $this->handle_post_events("modify");
575 }elseif($obj['status']=="new"){
576 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
577 $this->handle_post_events("add");
578 }
579 }
580 }
583 function PrepareForCopyPaste($source)
584 {
585 plugin::PrepareForCopyPaste($source);
587 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
588 */
589 $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
590 foreach($res as $obj){
592 /* Skip not relevant objects */
593 if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
595 $objects = array();
596 $objects['status'] = "edited";
597 $objects['dn'] = $obj['dn'];
598 $objects = $this->get_object_attributes($objects,$this->subAttributes);
599 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
600 $this->SubObjects[$objects['cn']] = $objects;
601 }
602 }
605 /*! \brief Used for copy & paste.
606 Returns a HTML input mask, which allows to change the cn of this entry.
607 @param Array Array containing current status && a HTML template.
608 */
609 function getCopyDialog()
610 {
611 $vars = array("cn");
612 $smarty = get_smarty();
613 $smarty->assign("cn", htmlentities($this->cn));
614 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
615 $ret = array();
616 $ret['string'] = $str;
617 $ret['status'] = "";
618 return($ret);
619 }
622 /*! \brief Used for copy & paste.
623 Some entries must be renamed to avaoid duplicate entries.
624 */
625 function saveCopyDialog()
626 {
627 if(isset($_POST['cn'])){
628 $this->cn = get_post('cn');
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 ?>