Code

Closes #310 Moved copy_FAI_resource_recursive to faiManagement. It is only used there.
[gosa.git] / gosa-core / include / sieve / class_sieveManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2007 - Fabian Hickert <hickert@gonicus.de>
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 /* The sieve management class displays a list of sieve 
23  *  scripts for the given mail account. 
24  * The account is identified by the parents uid attribute. 
25  *
26  *  $config       The config object
27  *  $dn           The object edited 
28  *  $parent       The parent object that provides the uid attribute 
29  */
30 class sieveManagement extends plugin
31 {
32   var $parent = NULL;
33   var $scripts= array();  
34   var $uattrib = "uid";
35   var $current_script  = -1;
36   var $current_handler = NULL;
37   var $script_to_delete =-1;
38   var $sieve_handle = NULL; 
39   var $Script_Error = "";
40   var $Sieve_Error = "";
41   var $create_script = FALSE;
43   /* To add new elements we need to know 
44    *  Where to add the element              -> add_new_id
45    *  Whould we add above or below this id  -> add_above_below
46    *  What kind of element should we add    -> add_element_type
47    */
48   var $add_new_element    = FALSE;
49   var $add_new_id         = -1;
50   var $add_above_below    = "below";
51   var $add_element_type   = "sieve_comment";
53   /* If this variable is TRUE, this indicates that we have the 
54    *  import dialog opened. 
55    */
56   var $Import_Script = FALSE;
58   /* Initialize the class and load all sieve scripts 
59    *  try to parse them and display errors 
60    */ 
61   function sieveManagement(&$config,$dn,&$parent,$uattrib)
62   {
63     /* Check given parameter */
64     if(!isset($parent->$uattrib)){
65       trigger_error("Sieve Management implementation error. Parameter 4 (".$uattrib.") must be part of the given parent element (".get_class($parent).").");
66     }
68     $this->uattrib = $uattrib;
69     $this->parent = &$parent;
70     plugin::plugin($config,$dn);
72     /* Get sieve, if this fail abort class initialization */
73     if(!$this->sieve_handle = $this->get_sieve()){
74       return;
75     }
78     /* Get all sieve scripts names */
79     if($this->sieve_handle->sieve_listscripts()){
80       if (is_array($this->sieve_handle->response)){
81         foreach($this->sieve_handle->response as $key => $name){
83           $data = array();
84           $data['NAME'] = $name;
86           if($key == "ACTIVE" && $key === "ACTIVE"){
87             $data['ACTIVE'] = TRUE;
88           }else{
89             $data['ACTIVE'] = FALSE;
90           }
91           $this->scripts[] = $data;          
92         }
93       } 
94     }
96     /* Get script contents */
97     foreach($this->scripts as $key => $script){
98       $p = new My_Parser($this);
99       $this->sieve_handle->sieve_getscript($script['NAME']);
101       $script = "";
102       foreach($this->sieve_handle->response as $line){
103         $script.=$line;
104       }
106       $this->scripts[$key]['IS_NEW'] = FALSE;;
107       $this->scripts[$key]['SCRIPT'] = $script;
108       $this->scripts[$key]['ORIG_SCRIPT'] = $script;
109       $this->scripts[$key]['MSG']   = "";
110       $ret = $p->parse($script);
111       if(!$ret){
112         $this->scripts[$key]['STATUS']   = FALSE;
113         $this->scripts[$key]['MODE']    = "Source";
114         $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
115       }else{
116         $this->scripts[$key]['STATUS']   = TRUE;
117         $this->scripts[$key]['MODE']    = "Structured";
118         $this->scripts[$key]['MSG'] = _("Parse successful");
119       }
120       $this->scripts[$key]['PARSER'] = $p;
121       $this->scripts[$key]['EDITED'] = FALSE;
122     }
123     $this->sieve_handle = $this->sieve_handle;
124   }
127   /* Return a sieve class handle,
128    *  false if login fails
129    */
130   function get_sieve()
131   {
132     
133     /* Connect to sieve class and try to get all available sieve scripts */
134     if(isset($this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer])){
135       $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
136       $this->Sieve_Error = "";
138       $uattrib = $this->uattrib;
140       /* Log into the mail server */
141       $this->sieve_handle= new sieve(
142           $cfg["sieve_server"], 
143           $cfg["sieve_port"], 
144           $this->parent->$uattrib, 
145           $cfg["password"], 
146           $cfg["admin"]);
148       /* Try to login */
149       if (!@$this->sieve_handle->sieve_login()){
150         $this->Sieve_Error = $this->sieve_handle->error_raw;
151         return(FALSE);
152       }
153       return($this->sieve_handle);
154     }else{
155       $this->Sieve_Error = sprintf(_("The specified mail server '%s' does not exist within the GOsa configuration."),
156         $this->parent->gosaMailServer);
157       return(FALSE);
158     }
159   }
162   /* Handle sieve list 
163    */
164   function execute()
165   {
166     /***************
167      * Create a new Script 
168      ***************/
170     /* Force opening the add script dialog */
171     if(isset($_POST['create_new_script'])){
172       $this->create_script = TRUE;
173     }
175     /* Close add script dialog, without adding a new one */
176     if(isset($_POST['create_script_cancel'])){
177       $this->create_script = FALSE;
178     }
180     /* Display create script dialog 
181      *  handle posts, display warnings if specified 
182      *  name is not useable. 
183      * Create a new script with given name
184      */
185     if($this->create_script){
186     
187       /* Set initial name or used posted name if available */
188       $name = "";
189       if(isset($_POST['NewScriptName'])){
190         $name = trim($_POST['NewScriptName']);
191       }
192  
193       /* Check given name */ 
194       $err = false;
196       /* Is given name in lower case characters ? */
197       if(isset($_POST['create_script_save'])){
198         if(!strlen($name)){
199           $err =true;
200           msg_dialog::display(_("Error"), _("No script name specified!"), ERROR_DIALOG);
201         }
202         /* Is given name in lower case characters ? */
203         if($name != strtolower($name)){
204           $err =true;
205           msg_dialog::display(_("Error"), _("Please use only lowercase script names!"), ERROR_DIALOG);
206         }
208         /* Only chars are allowed here */
209         if(preg_match("/[^a-z]/i",$name)){
210           $err =true;
211           msg_dialog::display(_("Error"), _("Please use only alphabetical characters in script names!"), ERROR_DIALOG);
212         }
214         $tmp = $this->get_used_script_names();
215         if(in_array_ics($name,$tmp)){
216           $err =true;
217           msg_dialog::display(_("Error"), _("Script name already in use!"), ERROR_DIALOG);
218         }
219       }
221       /* Create script if everything is ok */
222       if($this->create_script && isset($_POST['create_script_save']) && !$err){
224         /* Close dialog */
225         $this->create_script = FALSE;
227         /* Script contents to use */
228         $script = "/*New script */".
229                   "stop;";
231         /* Create a new parser and initialize default values */
232         $p = new My_Parser($this);
233         $ret = $p->parse($script);
234         $sc['SCRIPT'] = $script;
235         $sc['ORIG_SCRIPT'] = $script;
236         $sc['IS_NEW'] = TRUE;
237         $sc['MSG']   = "";
238         if(!$ret){
239           $sc['STATUS']   = FALSE;
240           $sc['MODE']    = "Source";
241           $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
242         }else{
243           $sc['STATUS']   = TRUE;
244           $sc['MODE']    = "Structured";
245           $sc['MSG'] = _("Parse successful");
246         }
247         $sc['PARSER'] = $p;
248         $sc['EDITED'] = TRUE;
249         $sc['ACTIVE'] = FALSE;
250         $sc['NAME']   = $name;
251       
252         /* Add script */
253         $this->scripts[$name] = $sc;
254       }else{
255       
256         /* Display dialog to enter new script name */
257         $smarty = get_smarty();
258         $smarty->assign("NewScriptName",$name);
259         return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
260       }
261     }
264     /*************
265      * Handle several posts 
266      *************/
268     $once = TRUE;
269     foreach($_POST as $name => $value){
271       /* Edit script requested */
272       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
273         $script = preg_replace("/^editscript_/","",$name);
274         $script = preg_replace("/_(x|y)/","",$script);
275         $once = FALSE;
277         $this->current_script = $script;
278         $this->current_handler = $this->scripts[$script]['PARSER'];
279         $this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
280       }
282       /* remove script requested */
283       if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
284         $script = preg_replace("/^delscript_/","",$name);
285         $script = preg_replace("/_(x|y)/","",$script);
286         $once = FALSE;
287         $this->script_to_delete = $script;  
288       }
290       /* Activate script */
291       if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
292         $script = preg_replace("/^active_script_/","",$name);
293         $script = preg_replace("/_(x|y)/","",$script);
294         $once = FALSE;
296         /* We can only activate existing scripts */
297         if(!$this->scripts[$script]['IS_NEW']){
299           /* Get sieve */
300           if(!$this->sieve_handle = $this->get_sieve()){
301             msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($this->Sieve_Error).'</i>'), ERROR_DIALOG);
302           }
304           /* Try to activate the given script and update 
305            *  class script array. 
306            */
307           if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
308             msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '<br><br><i>'.to_string($this->sieve_handler->error_raw).'</i>'), ERROR_DIALOG);
309           }else{
310             foreach($this->scripts as $key => $data){
311               if($key == $script){
312                 $this->scripts[$key]['ACTIVE'] = TRUE;
313               }else{
314                 $this->scripts[$key]['ACTIVE'] = FALSE;
315               }
316             }
317           }
318         }
319       }
320     }
322     
323     /*************
324      * Remove script handling 
325      *************/
327     /* Remove aborted */
328     if(isset($_POST['delete_cancel'])){
329       $this->script_to_delete = -1;
330     }
332     /* Remove confirmed */
333     if($this->parent->acl_is_writeable("sieveManagement") && isset($_POST['delete_script_confirm'])){
335       $script = $this->scripts[$this->script_to_delete];
337       /* Just remove from array if it is a new script */
338       if($script['IS_NEW']){
339         unset($this->scripts[$this->script_to_delete]);
340       }else{
342         /* Get sieve */
343         if(!$this->sieve_handle = $this->get_sieve()){
344           msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($this->Sieve_Error).'</i>'), ERROR_DIALOG);
345         }
347         if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
348           msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot remove SIEVE script: %s"), '<br><br><i>'.to_string($this->sieve_handler->error_raw).'</i>'), ERROR_DIALOG);
349         }else{
350           unset($this->scripts[$this->script_to_delete]);
351         }
352       }
353       $this->script_to_delete = -1;
354     }
356     /* Display confirm dialog */
357     if($this->script_to_delete != -1){
358       $smarty = get_smarty();
359       $smarty->assign("Warning",
360           sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
361             $this->scripts[$this->script_to_delete]['NAME']));
362       return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
363     }
366     /**************
367      * Save script changes 
368      **************/
370     /* Abort saving */
371     if(isset($_POST['cancel_sieve_changes'])){
372       $tmp = $this->scripts[$this->current_script]['SCRIPT_BACKUP'];
373       $this->scripts[$this->current_script]['SCRIPT'] = $tmp;
374       $this->scripts[$this->current_script]['PARSER']->parse($tmp);
375       $this->current_handler = NULL;
376     }
378     /* Save currently edited sieve script. */
379     if($this->parent->acl_is_writeable("sieveManagement") && 
380        isset($_POST['save_sieve_changes']) && 
381        is_object($this->current_handler)){
382       $chk = $this->current_handler->check();
383       if(!count($chk)){
385         $sc = $this->scripts[$this->current_script]['SCRIPT'];
386         $p = new My_Parser($this);
387         if($p -> parse($sc)){
389           if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
390             $this->scripts[$this->current_script]['MODE'] = "Source";
391           }
392   
393           $this->scripts[$this->current_script]['PARSER'] = $p;
394           $this->scripts[$this->current_script]['EDITED'] = TRUE;
395           $this->scripts[$this->current_script]['STATUS'] = TRUE;
396           $this->scripts[$this->current_script]['MSG'] = _("Edited");
397           $this->current_handler = NULL;
398         }else{
399           msg_dialog::display(_("SIEVE error"), $p->status_text, ERROR_DIALOG);
400         }
401       }else{
402         foreach($chk as $msgs){
403           msg_dialog::display(_("SIEVE error"), $msgs, ERROR_DIALOG);
404         }
405       }
406     }
409     /*************
410      * Display edit dialog 
411      *************/
413     /* Display edit dialog, depending on Mode display different uis
414      */
415     if($this->current_handler){
417         if(isset($_POST['Import_Script'])){
418           $this->Import_Script = TRUE;
419         }
421         if(isset($_POST['Import_Script_Cancel'])){
422           $this->Import_Script = FALSE;
423         }
425         if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
427           $file     = $_FILES['Script_To_Import'];
429           if($file['size'] == 0){
430             msg_dialog::display(_("Error"), _("Uploaded script is empty!"), ERROR_DIALOG);
431           }elseif(!file_exists($file['tmp_name'])){
432             msg_dialog::display(_("Internal error"), sprintf(_("Cannot access temporary file '%s'!"), $file['tmp_name']), ERROR_DIALOG);
433           }elseif(!is_readable ($file['tmp_name'])){
434             msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot open temporary file '%s'!"), $file['tmp_name']), ERROR_DIALOG);
435           }else{
436             
437             
438  
439             $contents = file_get_contents($file['tmp_name']);
440            
441             $this->scripts[$this->current_script]['SCRIPT'] = $contents;
442             if(!$this->current_handler->parse($contents)){
443               $this->scripts[$this->current_script]['MODE'] = "Source";
444             }else{
445               $this->scripts[$this->current_script]['MODE'] = "Structured";
446             }
447             $this->Script_Error = "";
448             $this->Import_Script = FALSE;
449           }
450         }
452         if($this->Import_Script){
453           $smarty = get_smarty();
454           $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
455           return($str);
456         }
457   
459         /* Create dump of current sieve script */
460         if(isset($_POST['Save_Copy'])){
462             /* force download dialog */
463             header("Content-type: application/tiff\n");
464             if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
465                     preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
466                 header('Content-Disposition: filename="dump.script"');
467             } else {
468                 header('Content-Disposition: attachment; filename="dump.script"');
469             }
470             header("Content-transfer-encoding: binary\n");
471             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
472             header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
473             header("Cache-Control: no-cache");
474             header("Pragma: no-cache");
475             header("Cache-Control: post-check=0, pre-check=0");
476             echo $this->scripts[$this->current_script]['SCRIPT'];
477             exit();
478         }
481       /****
482        * Add new element to ui
483        ****/
485       /* Abort add dialog */ 
486       if(isset($_POST['select_new_element_type_cancel'])){
487         $this->add_new_element = FALSE;
488       }
490       /* Add a new element */
491       if($this->add_new_element){
493         $element_types= array(
494             "sieve_keep"      => _("Keep"),
495             "sieve_comment"   => _("Comment"),
496             "sieve_fileinto"  => _("File into"),
497             "sieve_keep"      => _("Keep"),
498             "sieve_discard"   => _("Discard"),
499             "sieve_redirect"  => _("Redirect"),
500             "sieve_reject"    => _("Reject"),
501             "sieve_require"   => _("Require"),
502             "sieve_stop"      => _("Stop"),
503             "sieve_vacation"  => _("Vacation message"),
504             "sieve_if"        => _("If"));
507         /* Element selected */
508         if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']]) 
509            || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
510           $this->add_element_type = $_POST['element_type'];
511         }
513         /* Create new element and add it to
514          *  the selected position 
515          */
516         if(isset($_POST['select_new_element_type'])){
517           if($this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below)){
518             $this->add_new_element = FALSE;
519           }else{
520             msg_dialog::display(_("SIEVE error"), _("Cannot add new element!") , ERROR_DIALOG);
521           }
522         }
523       }
525       /* Only display select dialog if it is necessary */
526       if($this->add_new_element){
527         $smarty = get_smarty();
528     
529         $add_else_elsif = FALSE;
531         /* Check if we should add else/elsif to the select box 
532          *  or not. We can't add else twice!.
533          */
534         if($this->add_above_below == "below"){
536           /* Get posistion of the current element 
537            */
538           foreach($this->current_handler->tree_->pap as $key => $obj){
539         
540             if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
541   
542               /* Get block start/end */
543               $end_id = $this->current_handler->tree_->get_block_end($key);
544               $else_found = FALSE;
545               $elsif_found = FALSE;
546           
547               /* Check if there is already an else in this block 
548                */
549               for($i =  $key ; $i < $end_id ; $i ++){
550                 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
551                   $else_found = TRUE;
552                 }
553                 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
554                   $elsif_found = TRUE;
555                 }
556               }
557   
558               /* Only allow adding 'else' if there is currently 
559                *  no 'else' statement. And don't allow adding 
560                *  'else' before 'elseif'
561                */ 
562               if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
563                 $element_types['sieve_else'] = _("Else");
564               }
565               $element_types['sieve_elsif'] = _("Else If");
566             }
567           }
568         }
570         $smarty->assign("element_types",$element_types );
571         $smarty->assign("element_type",$this->add_element_type);
572         $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
573         return($str);
574       }
578       /****************
579        * Handle test posts 
580        ****************/
582       /* handle some special posts from test elements 
583        */
584       foreach($_POST as $name => $value){
585         if(preg_match("/^Add_Test_Object_/",$name)) {
586           $name = preg_replace("/^Add_Test_Object_/","",$name);
587           $name = preg_replace("/_(x|y)$/","",$name);
589           $test_types_to_add = array(
590               "address" =>_("Address"),
591               "header"  =>_("Header"),
592               "envelope"=>_("Envelope"),
593               "size"    =>_("Size"),
594               "exists"  =>_("Exists"),
595               "allof"   =>_("All of"),
596               "anyof"   =>_("Any of"),
597               "true"    =>_("True"),
598               "false"   =>_("False"));
600           $smarty = get_smarty();
601           $smarty->assign("ID",$name);
602           $smarty->assign("test_types_to_add",$test_types_to_add);
603           $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
604           return($ret);
605         }
606       }
608       $current = $this->scripts[$this->current_script];
610       /* Create html results */
611       $smarty = get_smarty();
612       $smarty->assign("Mode",$current['MODE']);
613       if($current['MODE'] == "Structured"){
614         $smarty->assign("Contents",$this->current_handler->tree_->execute());
615       }else{
616         $smarty->assign("Contents",$current['SCRIPT']);
617       }
618       $smarty->assign("Script_Error",$this->Script_Error);
619       $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
620       return($ret);
621     }
624     /* Create list of available sieve scripts 
625      */
626     $List = new divSelectBox("sieveManagement");
627     foreach($this->scripts as $key => $script){
628   
629       $edited =  $script['EDITED'];
630       $active =  $script['ACTIVE'];
631       
632       $field1 = array("string" => "&nbsp;",
633                       "attach" => "style='width:20px;'");  
634       if($active){
635         $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."' 
636                                       title='"._("This script is marked as active")."'>",
637                         "attach" => "style='width:20px;'");  
638       }
639       $field2 = array("string" => $script['NAME']);  
640       $field3 = array("string" => $script['MSG']);
641       $field4 = array("string" => _("Script length").":&nbsp;".strlen($script['SCRIPT']));
643       if($this->parent->acl_is_writeable("sieveManagement")){
644         $del = "<input type='image' name='delscript_".$key."' src='images/edittrash.png'
645                   title='"._("Remove script")."'>";
646       }else{
647         $del = "<img src='images/empty.png' alt=' '>";
648       }
650       if($active || $script['IS_NEW'] || !$this->parent->acl_is_writeable("sieveManagement")){
651         $activate = "<img src='images/empty.png' alt=' '>";
652       }else{
653         $activate = "<input type='image' name='active_script_".$key."' src='images/true.png'
654                        title='"._("Activate script")."'>";
655       }
657       $field6 = array("string" => $activate."<input type='image' name='editscript_".$key."' src='images/edit.png'
658                         title='"._("Edit script")."'>".$del,
659                       "attach" => "style='border-right:0px; width:70px;'");
660       $List->AddEntry(array($field1,$field2,$field3,$field4,$field6)); 
661     }
663     $List->SetHeight(400);
664  
665     /* If the uattrib is empty   (Attribute to use for authentification with sieve)
666      *  Display a message that the connection can't be established.
667      */
668     $uattrib = $this->uattrib;
669     $smarty = get_smarty();
671     if(!$this->get_sieve()){
672       $smarty->assign("Sieve_Error",sprintf(
673         _("Can't log into SIEVE server. Server says '%s'."),
674           to_string($this->Sieve_Error)));
675     }else{
676       $smarty->assign("Sieve_Error","");
677     }
679     $smarty->assign("uattrib_empty",empty($this->parent->$uattrib));
680     $smarty->assign("List",$List->DrawList());
681     return($smarty->fetch(get_template_path("templates/management.tpl",TRUE,dirname(__FILE__))));
682   }
685   /* Add a new element to the currently opened script editor.
686    * The insert position is specified by 
687    */
688   function add_new_element_to_current_script($type,$id,$position)
689   {
690     /* Test given data */
691     if(!in_array_ics($position,array("above","below"))){
692       trigger_error("Can't add new element with \$position=".$position.". Only 'above','below' are allowed here.");
693       return(FALSE);
694     }
695     if(!is_numeric($id)){
696       trigger_error("Can't add new element, given id is not numeric.");
697       return(FALSE);
698     }
699     $tmp = get_declared_classes();  
700     if(!in_array($type,$tmp)){
701       if(!empty($type)){
702         trigger_error("Can't add new element, given \$class=".$class." does not exists.");
703       }
704       return(FALSE);
705     }
706     if(!is_object($this->current_handler) || get_class($this->current_handler) != "My_Parser"){
707       trigger_error("Can't add new element, there is no valid script editor opened.");
708       return(FALSE);
709     }
711     /* These element types are allowed to be added here */
712     $element_types= array(
713         "sieve_keep"      => _("Keep"),
714         "sieve_comment"   => _("Comment"),
715         "sieve_fileinto"  => _("File into"),
716         "sieve_keep"      => _("Keep"),
717         "sieve_discard"   => _("Discard"),
718         "sieve_redirect"  => _("Redirect"),
719         "sieve_reject"    => _("Reject"),
720         "sieve_require"   => _("Require"),
721         "sieve_stop"      => _("Stop"),
722         "sieve_vacation"  => _("Vacation message"),
723         "sieve_if"        => _("If"));
725     /* Check if we should add else/elsif to the select box
726      *  or not. We can't add else twice!.
727      */
729     /* Get posistion of the current element
730      */
731     foreach($this->current_handler->tree_->pap as $key => $obj){
733       if($obj->object_id == $id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
735         /* Get block start/end */
736         $end_id = $this->current_handler->tree_->get_block_end($key);
737         $else_found = FALSE;
738         $elsif_found = FALSE;
740         /* Check if there is already an else in this block
741          */
742         for($i =  $key ; $i < $end_id ; $i ++){
743           if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
744             $else_found = TRUE;
745           }
746           if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
747             $elsif_found = TRUE;
748           }
749         }
751         if($this->add_above_below == "below"){
753           /* Only allow adding 'else' if there is currently
754            *  no 'else' statement. And don't allow adding
755            *  'else' before 'elseif'
756            */
757           if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
758             $element_types['sieve_else'] = _("Else");
759           }
760           $element_types['sieve_elsif'] = _("Else If");
761         }else{
762          
763           /* Allow adding elsif above elsif */ 
764           if(in_array(get_class($obj),array("sieve_elsif"))){
765             $element_types['sieve_elsif'] = _("Else If");
766           }
767         }
768       }
769     }
771     if(!isset($element_types[$type])){
772       msg_dialog::display(_("SIEVE error"), _("Cannot insert element at the requested position!") , ERROR_DIALOG);
773       return;
774     }
777     /* Create elements we should add 
778      * -Some element require also surrounding block elements
779      */
780     $parent = $this->current_handler->tree_;
781     if($this->add_element_type == "sieve_if"){
782       $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
783       $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
784       $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
785     }elseif($this->add_element_type == "sieve_else"){
786       $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
787       $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
788       $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
789     }elseif($this->add_element_type == "sieve_elsif"){
790       $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
791       $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
792       $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
793     }elseif($this->add_element_type == "sieve_vacation"){
795       /* Automatically add addresses to sieve alternate addresses */
796       $data = NULL;
797       $tmp = new $this->add_element_type($data, preg_replace("/[^0-9]/","",microtime()),$parent);
798       if(isset($this->parent->gosaMailAlternateAddress)){
799         $tmp->addresses = $this->parent->gosaMailAlternateAddress;
800       }
801       $ele[] = $tmp ;
802     }else{
803       $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
804     }
806     /* Get index of the element identified by object_id == $id; 
807      */
808     $index = -1;
809     $data = $this->current_handler->tree_->pap;
810     foreach($data as $key => $obj){
811       if($obj->object_id == $id && $index==-1){
812         $index = $key;
813       }
814     }
816     /* Tell to user that we couldn't find the given object 
817      *  so we can't add an element. 
818      */
819     if($index == -1 ){
820       trigger_error("Can't add new element, specified \$id=".$id." could not be found in object tree.");
821       return(FALSE);
822     }
824     /* We have found the specified object_id 
825      *  and want to detect the next free position 
826      *  to insert the new element.
827      */
828     if($position == "above"){
829       $direction ="up";
830       $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
831     }else{
832       $direction = "down";
833       $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
834     }
835     /* This is extremly necessary, cause some objects 
836      *  updates the tree objects ... Somehow i should change this ... 
837      */
838     $data = $this->current_handler->tree_->pap;
839     $start = $end = array();
841     if($position == "above"){
842       $start = array_slice($data,0,$next_free);
843       $end   = array_slice($data,$next_free);
844     }else{
845       $start = array_slice($data,0,$next_free+1);
846       $end   = array_slice($data,$next_free+1);
847     }
849     $new = array();
850     foreach($start as $obj){
851       $new[] = $obj;
852     }
853     foreach($ele as $el){
854       $new[] = $el;
855     }
856     foreach($end as $obj){
857       $new[] = $obj;
858     }
859     $data= $new;
860     $this->current_handler->tree_->pap = $data;
861     return(TRUE);
862   }
866   function save_object()
867   {
868     if($this->current_handler){
870       if(isset($_GET['Add_Object_Top_ID'])){
871         $this->add_new_element    = TRUE;
872         $this->add_new_id         = $_GET['Add_Object_Top_ID'];
873         $this->add_above_below    = "above";
874       }  
876       if(isset($_GET['Add_Object_Bottom_ID'])){
877         $this->add_new_element    = TRUE;
878         $this->add_new_id         = $_GET['Add_Object_Bottom_ID'];
879         $this->add_above_below    = "below";
880       }  
882       if(isset($_GET['Remove_Object_ID'])){
883         $found_id = -1;
884         foreach($this->current_handler->tree_->pap as $key => $element){
885           if($element->object_id == $_GET['Remove_Object_ID']){
886             $found_id = $key;
887           }
888         }
889         if($found_id != -1 ){
890           $this->current_handler->tree_->remove_object($found_id);  
891         }
892       }  
893  
894       if(isset($_GET['Move_Up_Object_ID'])){
895         $found_id = -1;
896         foreach($this->current_handler->tree_->pap as $key => $element){
897           if($element->object_id == $_GET['Move_Up_Object_ID']){
898             $found_id = $key;
899           }
900         }
901         if($found_id != -1 ){
902           $this->current_handler->tree_->move_up_down($found_id,"up");
903         }
904       }  
905  
906       if(isset($_GET['Move_Down_Object_ID'])){
907         $found_id = -1;
908         foreach($this->current_handler->tree_->pap as $key => $element){
909           if($element->object_id == $_GET['Move_Down_Object_ID']){
910             $found_id = $key;
911           }
912         }
913         if($found_id != -1 ){
914           $this->current_handler->tree_->move_up_down($found_id,"down");
915         }
916       }  
917   
919       /* Check if there is an add object requested 
920        */
921       $data = $this->current_handler->tree_->pap;
922       $once = TRUE;
923       foreach($_POST as $name => $value){
924         foreach($data as $key => $obj){
925           if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
926             $once = FALSE;
927             $this->add_element_type   =  $_POST['element_type_'.$obj->object_id];
928             $this->add_new_element    = FALSE;
929             $this->add_new_id         = $obj->object_id;
930             $this->add_above_below    = "above";
931             $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
932           }
933           if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
934             $once = FALSE;
935             $this->add_element_type   =  $_POST['element_type_'.$obj->object_id];
936             $this->add_new_element    = FALSE;
937             $this->add_new_id         = $obj->object_id;
938             $this->add_above_below    = "below";
939             $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
940           }
941         
942           if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
943             $once = FALSE;
944             $this->current_handler->tree_->remove_object($key);
945           }
946           if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
947             $this->current_handler->tree_->move_up_down($key,"up");
948             $once = FALSE;
949           }
950           if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
951             $this->current_handler->tree_->move_up_down($key,"down");
952             $once = FALSE;
953           }
954         }
955       }
957       /* Skip Mode changes and Parse tests 
958        *  if we are currently in a subdialog 
959        */
961       $this->current_handler->save_object();
962       $Mode = $this->scripts[$this->current_script]['MODE'];
963       $skip_mode_change = false;
964       if(in_array($Mode,array("Source-Only","Source"))){
965         if(isset($_POST['script_contents'])){
966           $sc = stripslashes($_POST['script_contents']);
967           $this->scripts[$this->current_script]['SCRIPT'] = $sc;
968           $p = new My_Parser($this);
969           if($p -> parse($sc)){
970             $this->Script_Error = "";
971           } else {
972             $this->Script_Error = $p->status_text;
973             $skip_mode_change = TRUE;
974           }
975         }
976       }
977       if(in_array($Mode,array("Structured"))){
978         $sc = $this->current_handler->get_sieve_script();
979         $this->scripts[$this->current_script]['SCRIPT'] = $sc;
980         $p = new My_Parser($this);
981         if($p -> parse($sc)){
982           $this->Script_Error = "";
983         } else {
984           $this->Script_Error = $p->status_text;
985           $skip_mode_change = TRUE;
986         }
987       }
988       if(!$skip_mode_change){
989         if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
990           $old_mode = $this->scripts[$this->current_script]['MODE'];
991           if(isset($_POST['View_Source'])){
992             $this->scripts[$this->current_script]['MODE'] = "Source";
993           }
994           if(isset($_POST['View_Structured'])){
995             $this->scripts[$this->current_script]['MODE'] = "Structured";
996           }
997           $new_mode = $this->scripts[$this->current_script]['MODE'];
999           if($old_mode != $new_mode){
1001             $sc = $this->scripts[$this->current_script]['SCRIPT'];
1002             $p = new My_Parser($this);
1004             if($p -> parse($sc)){
1005               $this->current_handler->parse($sc);
1006               $this->Script_Error = "";
1007             } else {
1008               $this->Script_Error = $p->status_text;
1009             }
1010           } 
1011         }
1012       }
1013     }
1014   }
1016   
1017   function get_used_script_names()
1018   {
1019     $ret = array();
1020     foreach($this->scripts as $script){
1021       $ret[] = $script['NAME'];
1022     }
1023     return($ret);
1024   }
1028   function save()
1029   {
1030     /* Get sieve */
1031     if(!$this->sieve_handle = $this->get_sieve()){
1032       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($this->Sieve_Error).'</i>'), ERROR_DIALOG);
1033       return;
1034     }
1036     $everything_went_fine = TRUE;
1038     foreach($this->scripts as $key => $script){
1039       if($script['EDITED']){
1040         $data = $this->scripts[$key]['SCRIPT'];
1041         if(!$this->sieve_handle->sieve_sendscript($script['NAME'], addcslashes ($data,"\\"))){
1042           new log("modify","users/mailAccount".get_class($this),$script['NAME'],"Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
1044           $everything_went_fine = FALSE;
1045           msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot store SIEVE script: %s"), '<br><br><i>'.to_string($this->sieve_handle->error_raw).'</i>'), ERROR_DIALOG);
1046           $this->scripts[$key]['MSG'] = "<font color='red'>".
1047                                            _("Failed to save sieve script").": ".
1048                                            to_string($this->sieve_handle->error_raw).
1049                                            "</font>";
1050         }
1051       }
1052     }
1053     return($everything_went_fine);
1054   }
1056 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1057 ?>