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);
68 while($object = $ldap->fetch()){
70 /* Skip objects, that are tagged as removed */
71 if(isset($object['FAIstate'][0])){
72 if(preg_match("/removed$/",$object['FAIstate'][0])){
73 continue;
74 }
75 }
77 /* Set status for save management */
78 $objects = array();
79 $objects['status'] = "FreshLoaded";
80 $objects['dn'] = $object['dn'];
81 $objects = $this->get_object_attributes($objects,$this->subAttributes);
82 $this->SubObjects[$objects['cn']] = $objects;
83 }
85 }
86 $this->ui = get_userinfo();
87 }
90 /* Reload some attributes */
91 function get_object_attributes($object,$attributes)
92 {
93 $ldap = $this->config->get_ldap_link();
94 $ldap->cd($this->config->current['BASE']);
95 $ldap->cat($object['dn'],$attributes);
96 $tmp = $ldap->fetch();
98 foreach($attributes as $attrs){
99 if(isset($tmp[$attrs][0])){
100 $var = $tmp[$attrs][0];
102 /* Check if we must decode some attributes */
103 if(in_array_ics($attrs,$this->sub64coded)){
104 $var = base64_decode($var);
105 }
107 /* check if this is a binary entry */
108 if(in_array_ics($attrs,$this->subBinary)){
109 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110 }
112 /* Fix slashes */
113 $var = addslashes($var);
114 $object[$attrs] = $var;
115 }
116 }
117 return($object);
118 }
121 /* Return a valid dn to fetch acls. Because 'new' will not work. */
122 function acl_base_for_current_object($dn)
123 {
124 if($dn == "new"){
125 if($this->dn == "new"){
126 $dn= "cn=dummy,".session::get('CurrentMainBase');
127 }else{
128 $dn = $this->dn;
129 }
130 }
131 return($dn);
132 }
135 function execute()
136 {
137 /* Call parent execute */
138 plugin::execute();
140 if($this->is_account && !$this->view_logged){
141 $this->view_logged = TRUE;
142 new log("view","fai/".get_class($this),$this->dn);
143 }
145 /* Fill templating stuff */
146 $smarty= get_smarty();
147 $display= "";
149 /* Add new sub object */
150 if(isset($_POST['AddSubObject'])){
151 $this->dialog= new $this->subClassName($this->config,"new");
152 $this->dialog->set_acl_base($this->acl_base);
153 $this->dialog->set_acl_category("fai");
154 $this->dialog->parent = &$this;
155 $this->is_dialog=true;
156 }
158 if($this->dn != "new"){
159 session::set('objectinfo',$this->dn);
160 }
162 /* File download requested */
163 if(isset($_GET['getFAIscript'])){
164 if(isset($this->SubObjects[$_GET['getFAIscript']])){
165 $obj = $this->SubObjects[$_GET['getFAIscript']];
166 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
167 send_binary_content($obj['FAIscript'],$obj['cn'].".FAIscript");
168 }
169 }
171 /* Handle posts */
172 $s_action = $s_entry = "";
173 foreach($_POST as $name => $value){
175 /* Edit script posted */
176 if(preg_match("/^editscript_/",$name)){
177 $s_action = "edit";
178 $s_entry = preg_replace("/^editscript_/","",$name);
179 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
180 break;
181 }
183 /* Delete script requested */
184 if(preg_match("/^deletescript_/",$name)){
185 $s_action = "remove";
186 $s_entry = preg_replace("/^deletescript_/","",$name);
187 $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
188 break;
189 }
190 }
192 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
193 $s_entry = $_GET['id'];
194 if(isset($this->SubObjects[$s_entry])){
195 $s_action = "edit";
196 }
197 }
199 if($s_action =="edit" && isset($this->SubObjects[$s_entry])){
201 /* Get object, and load missing entry values */
202 $obj = $this->SubObjects[$s_entry];
203 if($obj['status'] == "FreshLoaded"){
204 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
205 }
207 /* Create new dialog and set acl attributes */
208 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
209 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
210 $this->dialog->set_acl_category("fai");
212 /* Assign some additional dialog informations like headline and parent */
213 session::set('objectinfo',$obj['dn']);
214 $this->dialog->parent = &$this;
215 $this->is_dialog=true;
216 }
218 /* Check acls, are we allowed to delete an entry */
219 if($s_action == "remove" && isset($this->SubObjects[$s_entry])){
220 $entry = $this->SubObjects[$s_entry];
221 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry") ;
222 if(preg_match("/d/",$acl)){
223 $status = $entry['status'];
224 if($status == "edited" || $status == "FreshLoaded"){
225 $this->SubObjects[$s_entry]['status']= "delete";
226 }else{
227 unset($this->SubObjects[$s_entry]);
228 }
229 }
230 }
232 /* Save the edited entry */
233 if(isset($_POST['SaveSubObject'])){
235 /* Check if there are still errors remaining that must be fixed before saving */
236 $this->dialog->save_object();
237 $msgs = $this->dialog->check();
238 if(count($msgs)>0){
239 foreach($msgs as $msg){
240 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
241 }
242 }else{
244 /* Get return object */
245 $obj = $this->dialog->save();
247 /* If we have renamed the script entry, we must remove the old entry */
248 if(isset($obj['remove'])){
250 /* Get old entry values */
251 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
253 /* Depending on status, set new status */
254 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
255 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
256 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
257 unset($this->SubObjects[$obj['remove']['from']]);
258 }
260 /* Append the new entry */
261 $obj['status'] = "new";
262 $this->SubObjects[$obj['remove']['to']] = $obj;
263 unset($this->SubObjects[$obj['remove']['to']]['remove']);
264 }else{
266 /* Set new status and append the entry */
267 if($obj['status'] == "FreshLoaded"){
268 $obj['status'] = "edited";
269 }
270 $this->SubObjects[$obj['cn']]=$obj;
271 }
272 $this->is_dialog=false;
273 unset($this->dialog);
274 $this->dialog=FALSE;
276 }
277 }
279 /* Sort entries */
280 $tmp = $keys = array();
282 if($this->sort_by == "name"){
283 foreach($this->SubObjects as $key => $entry){
284 $keys[$key]=$entry['cn'];
285 }
286 }elseif($this->sort_by == "priority"){
287 foreach($this->SubObjects as $key => $entry){
288 $keys[$key]=$entry['FAIpriority'];
289 }
290 }
292 natcasesort($keys);
294 if($this->sort_order == "down"){
295 $keys =array_reverse($keys);
296 }
298 foreach($keys as $key => $order_var){
299 $tmp[$key]=$this->SubObjects[$key];
300 }
301 $this->SubObjects = $tmp;
303 /* Cancel Dialog */
304 if(isset($_POST['CancelSubObject'])){
305 $this->is_dialog=false;
306 unset($this->dialog);
307 $this->dialog=FALSE;
308 }
310 /* Print dialog if $this->dialog is set */
311 if(is_object($this->dialog)){
312 $this->dialog->save_object();
313 $display = $this->dialog->execute();
314 return($display);
315 }
317 /* Divlist added 23.02.2006
318 Containing FAIscripts
319 */
320 $divlist = new divlist("FAIscripts");
321 $divlist->SetEntriesPerPage(0);
322 $plug = $_GET['plug'];
324 if($this->sort_order == "up"){
325 $dir = "<img src='images/sort_up.png' title='"._("Sort direction")."' alt='\/' border=0>";
326 }else{
327 $dir = "<img src='images/sort_down.png' title='"._("Sort direction")."' alt='/\' border=0>";
328 }
330 if($this->sort_by == "name"){
331 $sort_name = $dir;
332 $sort_prio = "";
333 }else{
334 $sort_name = "";
335 $sort_prio = $dir;
336 }
338 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name").$sort_name."</a>"),
339 array("string"=>"<a href='?plug=".$plug."&sort=priority'>".$sort_prio._("Priority")."</a>",
340 "attach"=>"style='width:100px;'"),
341 array("string"=>_("Download"),
342 "attach"=>"style='width:100px;'"),
343 array("string"=>_("Action"),
344 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
345 $divlist->SetHeight(300);
346 $divlist->SetWidth("100%");
347 foreach($this->getList(true) as $key => $name){
349 $dn= $this->acl_base_for_current_object($name['dn']);
350 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
351 $act = "";
353 /* Hide delete icon if this object is freezed */
354 if($this->FAIstate == "freeze"){
355 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
356 }else{
357 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
358 if(preg_match("/d/",$acl)){
359 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
360 }
361 }
363 /* Check acls for download icon */
364 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
365 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
366 $down = "";
367 }else{
368 $down = "<a href='?plug=".$_GET['plug']."&getFAIscript=".$key."'>
369 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
370 </a>";
371 }
373 /* Check if we are allowed to view this object */
374 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
375 if(preg_match("/r/",$s_acl)){
376 $divlist->AddEntry(array( array("string"=>"<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name['name']."</a>"),
377 array("string"=>$name['FAIpriority'] , "attach" => "style='width:100px;'"),
378 array("string"=>$down , "attach" => "style='width:100px;'"),
379 array("string"=>str_replace("%s",base64_encode($key),$act),
380 "attach"=>"style='border-right: 0px;width:100px;text-align:right;'")));
381 }
382 }
383 $smarty->assign("Entry_divlist",$divlist->DrawList());
386 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
387 * If we post the escaped strings they will be escaped again
388 */
389 foreach($this->attributes as $attrs){
390 if(get_magic_quotes_gpc()){
391 $smarty->assign($attrs,stripslashes($this->$attrs));
392 }else{
393 $smarty->assign($attrs,($this->$attrs));
394 }
395 }
397 $dn = $this->acl_base_for_current_object($this->dn);
398 $smarty->assign("sub_object_is_addable",
399 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
400 !preg_match("/freeze/",$this->FAIstate));
402 $tmp = $this->plInfo();
403 foreach($tmp['plProvidedAcls'] as $name => $translated){
404 $smarty->assign($name."ACL",$this->getacl($name));
405 }
407 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
408 return($display);
409 }
411 /* Generate listbox friendly SubObject list
412 */
413 function getList($use_dns=false){
414 $a_return=array();
415 foreach($this->SubObjects as $obj){
416 if($obj['status'] != "delete"){
417 if($use_dns){
418 if((isset($obj['description']))&&(!empty($obj['description']))){
419 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
420 }else{
421 $a_return[$obj['cn']]['name']= $obj['cn'];
422 }
423 $a_return[$obj['cn']]['dn']= $obj['dn'];
424 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
425 }else{
426 if((isset($obj['description']))&&(!empty($obj['description']))){
427 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
428 }else{
429 $a_return[$obj['cn']]= $obj['cn'];
430 }
431 }
432 }
433 }
434 return($a_return);
435 }
437 /* Delete me, and all my subtrees
438 */
439 function remove_from_parent()
440 {
441 if($this->acl_is_removeable()){
442 $ldap = $this->config->get_ldap_link();
443 $ldap->cd ($this->dn);
444 $faifilter = session::get('faifilter');
445 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
446 if($faifilter['branch'] == "main"){
447 $use_dn = $this->dn;
448 }
450 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
452 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
454 foreach($this->SubObjects as $name => $obj){
455 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
456 if($faifilter['branch'] == "main"){
457 $use_dn = $obj['dn'];
458 }
459 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
460 }
461 $this->handle_post_events("remove");
462 }
463 }
466 /* Save data to object
467 */
468 function save_object()
469 {
470 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
471 plugin::save_object();
472 foreach($this->attributes as $attrs){
473 if(isset($_POST[$attrs])){
474 $this->$attrs = $_POST[$attrs];
475 }
476 }
477 }
479 /* Get sort order */
480 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
481 if($this->sort_by == $_GET['sort']){
482 if($this->sort_order == "up"){
483 $this->sort_order = "down";
484 }elseif($this->sort_order == "down"){
485 $this->sort_order = "up";
486 }
487 }
488 $this->sort_by = $_GET['sort'];
489 }
490 }
493 /* Check supplied data */
494 function check()
495 {
496 /* Call common method to give check the hook */
497 $message= plugin::check();
499 return ($message);
500 }
503 /* Save to LDAP */
504 function save()
505 {
506 plugin::save();
508 $ldap = $this->config->get_ldap_link();
510 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
511 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
513 if($this->initially_was_account){
514 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
515 }else{
516 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
517 }
519 /* Prepare FAIscriptEntry to write it to ldap
520 * First sort array.
521 * Because we must delete old entries first.
522 * After deletion, we perform add and modify
523 */
524 $Objects = array();
526 /* We do not need to save untouched objects */
527 foreach($this->SubObjects as $name => $obj){
528 if($obj['status'] == "FreshLoaded"){
529 unset($this->SubObjects[$name]);
530 }
531 }
533 foreach($this->SubObjects as $name => $obj){
534 if($obj['status'] == "delete"){
535 $Objects[$name] = $obj;
536 }
537 }
538 foreach($this->SubObjects as $name => $obj){
539 if($obj['status'] != "delete"){
540 $Objects[$name] = $obj;
541 }
542 }
544 foreach($Objects as $name => $obj){
546 foreach($this->sub64coded as $codeIt){
547 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
548 }
550 $tmp = array();
551 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
552 foreach($attributes as $attrs){
554 if(empty($obj[$attrs])){
555 $obj[$attrs] = array();
556 }
557 if(!is_array($obj[$attrs])){
558 $tmp[$attrs] = stripslashes($obj[$attrs]);
559 }else{
560 $tmp[$attrs] = $obj[$attrs];
561 }
562 }
564 $tmp['objectClass'] = $this->subClasses;
566 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
568 if($obj['status']=="new"){
569 $ldap->cat($sub_dn,array("objectClass"));
570 if($ldap->count()){
571 $obj['status']="edited";
572 }
573 }
575 if(empty($tmp['FAIpriority'])){
576 $tmp['FAIpriority'] ="0";
577 }
579 /* Tag object */
580 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
582 if($obj['status'] == "delete"){
583 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
584 $this->handle_post_events("remove");
585 }elseif($obj['status'] == "edited"){
586 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
587 $this->handle_post_events("modify");
588 }elseif($obj['status']=="new"){
589 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
590 $this->handle_post_events("add");
591 }
592 }
593 }
596 function PrepareForCopyPaste($source)
597 {
598 plugin::PrepareForCopyPaste($source);
600 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
601 */
602 $ldap = $this->config->get_ldap_link();
603 $ldap->cd ($source['dn']);
605 $attrs_to_search = $this->subAttributes;
606 $attrs_to_search[] = "FAIstate";
607 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
609 while($object = $ldap->fetch()){
611 /* Skip objects, that are tagged as removed */
612 if(isset($object['FAIstate'][0])){
613 if(preg_match("/removed$/",$object['FAIstate'][0])){
614 continue;
615 }
616 }
618 /* Set status for save management */
619 $objects = array();
620 $objects['status'] = "edited";
621 $objects['dn'] = $object['dn'];
622 $objects = $this->get_object_attributes($objects,$this->subAttributes);
623 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
625 $this->SubObjects[$objects['cn']] = $objects;
626 }
627 }
630 /* Return plugin informations for acl handling */
631 static function plInfo()
632 {
633 return (array(
634 "plShortName" => _("Script"),
635 "plDescription" => _("FAI script"),
636 "plSelfModify" => FALSE,
637 "plDepends" => array(),
638 "plPriority" => 18,
639 "plSection" => array("administration"),
640 "plCategory" => array("fai"),
641 "plProvidedAcls" => array(
642 "cn" => _("Name")." ("._("Readonly").")",
643 "description" => _("Description"))
644 ));
645 }
646 }
648 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
649 ?>