Code

Added "relational" to require element when :value :contains is used within address...
[gosa.git] / 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();  
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 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)
62   {
63     $this->parent = $parent;
64     plugin::plugin($config,$dn);
66     /* Get sieve */
67     if(!$this->sieve_handle = $this->get_sieve()){
68       print_red(
69         sprintf(
70           _("Can't log into SIEVE server. Server says '%s'."),
71           to_string($this->Sieve_Error)));
72       return;
73     }
76     /* Get all sieve scripts names */
77     if($this->sieve_handle->sieve_listscripts()){
78       if (is_array($this->sieve_handle->response)){
79         foreach($this->sieve_handle->response as $key => $name){
81           $data = array();
82           $data['NAME'] = $name;
84           if($key == "ACTIVE" && $key === "ACTIVE"){
85             $data['ACTIVE'] = TRUE;
86           }else{
87             $data['ACTIVE'] = FALSE;
88           }
89           $this->scripts[] = $data;          
90         }
91       } 
92     }
94     /* Get script contents */
95     foreach($this->scripts as $key => $script){
96       $p = new My_Parser($this);
97       $this->sieve_handle->sieve_getscript($script['NAME']);
99       $script = "";
100       foreach($this->sieve_handle->response as $line){
101         $script.=$line;
102       }
104       $this->scripts[$key]['IS_NEW'] = FALSE;;
105       $this->scripts[$key]['SCRIPT'] = $script;
106       $this->scripts[$key]['ORIG_SCRIPT'] = $script;
107       $this->scripts[$key]['MSG']   = "";
108       $ret = $p->parse($script);
109       if(!$ret){
110         $this->scripts[$key]['STATUS']   = FALSE;
111         $this->scripts[$key]['MODE']    = "Source";
112         $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
113       }else{
114         $this->scripts[$key]['STATUS']   = TRUE;
115         $this->scripts[$key]['MODE']    = "Structured";
116         $this->scripts[$key]['MSG'] = _("Parse successful");
117       }
118       $this->scripts[$key]['PARSER'] = $p;
119       $this->scripts[$key]['EDITED'] = FALSE;
120     }
121     $this->sieve_handle = $this->sieve_handle;
122   }
125   /* Return a sieve class hanlde,
126    *  false if login fails
127    */
128   function get_sieve()
129   {
130     /* Connect to sieve class and try to get all available sieve scripts */
131     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
132     $this->Sieve_Error = "";
133   
134     /* Log into the mail server */
135     $this->sieve_handle= new sieve(
136         $cfg["sieve_server"], 
137         $cfg["sieve_port"], 
138         $this->parent->uid, 
139         $cfg["password"], 
140         $cfg["admin"]);
142     /* Try to login */
143     if (!$this->sieve_handle->sieve_login()){
144       $this->Sieve_Error = $this->sieve_handle->error_raw;
145       return(FALSE);
146     }
147     return($this->sieve_handle);
148   }
151   /* Handle sieve list 
152    */
153   function execute()
154   {
155     /***************
156      * Create a new Script 
157      ***************/
159     /* Force opening the add script dialog */
160     if(isset($_POST['create_new_script'])){
161       $this->create_script = TRUE;
162     }
164     /* Close add script dialog, without adding a new one */
165     if(isset($_POST['create_script_cancel'])){
166       $this->create_script = FALSE;
167     }
169     /* Display create script dialog 
170      *  handle posts, display warnings if specified 
171      *  name is not useable. 
172      * Create a new script with given name
173      */
174     if($this->create_script){
175     
176       /* Set initial name or used posted name if available */
177       $name = "";
178       if(isset($_POST['NewScriptName'])){
179         $name = trim($_POST['NewScriptName']);
180       }
181  
182       /* Check given name */ 
183       $err = "";
185       /* Is given name in lower case characters ? */
186       if(isset($_POST['create_script_save'])){
187         if(!strlen($name)){
188           $err = _("You should specify a name for your new script.");
189         }
190         /* Is given name in lower case characters ? */
191         if($name != strtolower($name)){
192           $err = _("Only lower case names are allowed here.");
193         }
195         /* Only chars are allowed here */
196         if(preg_match("/[^a-z]/i",$name)){
197           $err = _("Only a-z are allowed in script names.");
198         }
200         $tmp = $this->get_used_script_names();
201         if(in_array_ics($name,$tmp)){
202           $err =_("The specified name is already in use.");
203         }
204       }
206       /* Create script if everything is ok */
207       if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
209         /* Close dialog */
210         $this->create_script = FALSE;
212         /* Script contents to use */
213         $script = "/*New script */".
214                   "stop;";
216         /* Create a new parser and initialize default values */
217         $p = new My_Parser($this);
218         $ret = $p->parse($script);
219         $sc['SCRIPT'] = $script;
220         $sc['ORIG_SCRIPT'] = $script;
221         $sc['IS_NEW'] = TRUE;
222         $sc['MSG']   = "";
223         if(!$ret){
224           $sc['STATUS']   = FALSE;
225           $sc['MODE']    = "Source";
226           $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
227         }else{
228           $sc['STATUS']   = TRUE;
229           $sc['MODE']    = "Structured";
230           $sc['MSG'] = _("Parse successful");
231         }
232         $sc['PARSER'] = $p;
233         $sc['EDITED'] = TRUE;
234         $sc['ACTIVE'] = FALSE;
235         $sc['NAME']   = $name;
236       
237         /* Add script */
238         $this->scripts[$name] = $sc;
239       }else{
240       
241         /* Display dialog to enter new script name */
242         $smarty = get_smarty();
243         $smarty->assign("NewScriptName",$name);
244         $smarty->assign("Error",$err);
245         return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
246       }
247     }
250     /*************
251      * Handle several posts 
252      *************/
254     $once = TRUE;
255     foreach($_POST as $name => $value){
257       /* Edit script requested */
258       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
259         $script = preg_replace("/^editscript_/","",$name);
260         $script = preg_replace("/_(x|y)/","",$script);
261         $once = FALSE;
263         $this->current_script = $script;
264         $this->current_handler = $this->scripts[$script]['PARSER'];
265         $this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
266       }
268       /* remove script requested */
269       if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
270         $script = preg_replace("/^delscript_/","",$name);
271         $script = preg_replace("/_(x|y)/","",$script);
272         $once = FALSE;
273  
274         $this->script_to_delete = $script;  
275       }
277       /* Activate script */
278       if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
279         $script = preg_replace("/^active_script_/","",$name);
280         $script = preg_replace("/_(x|y)/","",$script);
281         $once = FALSE;
283         /* We can only activate existing scripts */
284         if(!$this->scripts[$script]['IS_NEW']){
286           /* Get sieve */
287           if(!$this->sieve_handle = $this->get_sieve()){
288             print_red(
289                 sprintf(
290                   _("Can't log into SIEVE server. Server says '%s'."),
291                   to_string($this->Sieve_Error)));
292           }
294           /* Try to activate the given script and update 
295            *  class script array. 
296            */
297           if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
298             print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
299           }else{
300             foreach($this->scripts as $key => $data){
301               if($key == $script){
302                 $this->scripts[$key]['ACTIVE'] = TRUE;
303               }else{
304                 $this->scripts[$key]['ACTIVE'] = FALSE;
305               }
306             }
307           }
308         }
309       }
310     }
312     
313     /*************
314      * Remove script handling 
315      *************/
317     /* Remove aborted */
318     if(isset($_POST['delete_cancel'])){
319       $this->script_to_delete = -1;
320     }
322     /* Remove confirmed */
323     if(isset($_POST['delete_script_confirm'])){
325       $script = $this->scripts[$this->script_to_delete];
327       /* Just remove from array if it is a new script */
328       if($script['IS_NEW']){
329         unset($this->scripts[$this->script_to_delete]);
330       }else{
332         /* Get sieve */
333         if(!$this->sieve_handle = $this->get_sieve()){
334           print_red(
335               sprintf(
336                 _("Can't log into SIEVE server. Server says '%s'."),
337                 to_string($this->Sieve_Error)));
338         }
340         if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
341           print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
342         }else{
343           unset($this->scripts[$this->script_to_delete]);
344         }
345       }
346       $this->script_to_delete = -1;
347     }
349     /* Display confirm dialog */
350     if($this->script_to_delete != -1){
351       $smarty = get_smarty();
352       $smarty->assign("Warning",
353           sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
354             $this->scripts[$this->script_to_delete]['NAME']));
355       return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
356     }
359     /**************
360      * Save script changes 
361      **************/
363     /* Abort saving */
364     if(isset($_POST['cancel_sieve_changes'])){
365       $tmp = $this->scripts[$this->current_script]['SCRIPT_BACKUP'];
366       $this->scripts[$this->current_script]['SCRIPT'] = $tmp;
367       $this->scripts[$this->current_script]['PARSER']->parse($tmp);
368       $this->current_handler = NULL;
369     }
371     /* Save currently edited sieve script. */
372     if(isset($_POST['save_sieve_changes'])){
373       $chk = $this->current_handler->check();
374       if(!count($chk)){
376         $sc = $this->scripts[$this->current_script]['SCRIPT'];
377         $p = new My_Parser($this);
378         if($p -> parse($sc)){
380           if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
381             $this->scripts[$this->current_script]['MODE'] = "Source";
382           }
383   
384           $this->scripts[$this->current_script]['PARSER'] = $p;
385           $this->scripts[$this->current_script]['EDITED'] = TRUE;
386           $this->scripts[$this->current_script]['STATUS'] = TRUE;
387           $this->scripts[$this->current_script]['MSG'] = _("Edited");
388           $this->current_handler = NULL;
389         }else{
390           print_red($p->status_text);;
391         }
392       }else{
393         print_red(_("Please fix all errors before saving."));
394       }
395     }
398     /*************
399      * Display edit dialog 
400      *************/
402     /* Display edit dialog, depending on Mode display different uis
403      */
404     if($this->current_handler){
406         if(isset($_POST['Import_Script'])){
407           $this->Import_Script = TRUE;
408         }
410         if(isset($_POST['Import_Script_Cancel'])){
411           $this->Import_Script = FALSE;
412         }
414         if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
416           $file     = $_FILES['Script_To_Import'];
418           if($file['size'] == 0){
419             print_red(_("Specified file seams to empty."));
420           }elseif(!file_exists($file['tmp_name'])){
421             print_red(_("Upload failed, somehow nothing was uploaded or the temporary file can't be accessed."));
422           }elseif(!is_readable ($file['tmp_name'])){
423             print_red(sprintf(_("Can't open file '%s' to read uploaded file contents."),$file['tmp_name']));
424           }else{
425             
426             
427  
428             $contents = file_get_contents($file['tmp_name']);
429            
430             $this->scripts[$this->current_script]['SCRIPT'] = $contents;
431             if(!$this->current_handler->parse($contents)){
432               $this->scripts[$this->current_script]['MODE'] = "Source";
433             }else{
434               $this->scripts[$this->current_script]['MODE'] = "Structured";
435             }
436             $this->Import_Script = FALSE;
437           }
438         }
440         if($this->Import_Script){
441           $smarty = get_smarty();
442           $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
443           return($str);
444         }
445   
447         /* Create dump of current sieve script */
448         if(isset($_POST['Save_Copy'])){
450             /* force download dialog */
451             header("Content-type: application/tiff\n");
452             if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
453                     preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
454                 header('Content-Disposition: filename="dump.script"');
455             } else {
456                 header('Content-Disposition: attachment; filename="dump.script"');
457             }
458             header("Content-transfer-encoding: binary\n");
459             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
460             header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
461             header("Cache-Control: no-cache");
462             header("Pragma: no-cache");
463             header("Cache-Control: post-check=0, pre-check=0");
464             echo $this->scripts[$this->current_script]['SCRIPT'];
465             exit();
466         }
469       /****
470        * Add new element to ui
471        ****/
473       /* Abort add dialog */ 
474       if(isset($_POST['select_new_element_type_cancel'])){
475         $this->add_new_element = FALSE;
476       }
478       /* Add a new element */
479       if($this->add_new_element){
481         $element_types= array(
482             "sieve_keep"      => _("Keep"),
483             "sieve_comment"   => _("Comment"),
484             "sieve_fileinto"  => _("File into"),
485             "sieve_keep"      => _("Keep"),
486             "sieve_discard"   => _("Discard"),
487             "sieve_redirect"  => _("Redirect"),
488             "sieve_reject"    => _("Reject"),
489             "sieve_require"   => _("Require"),
490             "sieve_stop"      => _("Stop"),
491             "sieve_vacation"  => _("Vacation message"),
492             "sieve_if"        => _("If"));
495         /* Element selected */
496         if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']]) 
497            || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
498           $this->add_element_type = $_POST['element_type'];
499         }
501         /* Create new element and add it to
502          *  the selected position 
503          */
504         if(isset($_POST['select_new_element_type'])){
506           $this->add_new_id;
507           $data = $this->current_handler->tree_->pap;
509           /* Get index of the element identified by object_id == $this->add_new_id; */
510           $index = -1;
511           foreach($data as $key => $obj){
512             if($obj->object_id == $this->add_new_id && $index==-1){
513               $index = $key;
514             }
515           }
516  
517           /* We have found the specified object_id 
518            *  and want to detect the next free position 
519            *  to insert the new element.
520            */
521           if($index != -1){
522             if($this->add_above_below == "above"){
523               $direction ="up";
524               $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
525               $next_free ++;
526             }else{
527               $direction = "down";
528               $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
529             }
530             $this->add_new_id = $this->current_handler->tree_->pap[$next_free]->object_id;
531           }
533           $parent = $this->current_handler->tree_;
535           /* Create elements we should add */
536           if($this->add_element_type == "sieve_if"){
537             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
538             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
539             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
540           }elseif($this->add_element_type == "sieve_else"){
541             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
542             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
543             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
544           }elseif($this->add_element_type == "sieve_elsif"){
545             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
546             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
547             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
548           }else{
549             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
550           }
552           $start = $end = array();
553           $found = false;
555           /* Add above current element*/
556           if($this->add_above_below == "above"){
557             foreach($data as $key => $obj){
558               if($obj->object_id == $this->add_new_id){
559                 $found = true;
560               }
561               if(!$found){
562                 $start[] = $obj;
563               }else{
564                 $end[] = $obj;
565               }
566             }
567           }else{
568             /* Add below current element */
569             foreach($data as $key => $obj){
570               if(!$found){
571                 $start[] = $obj;
572               }else{
573                 $end[] = $obj;
574               }
575               if($obj->object_id == $this->add_new_id){
576                 $found = true;
577               }
578             }
579           }
582           /* Only add, if current element could be located */
583           if($found){
584             $new = array();
585             foreach($start as $obj){
586               $new[] = $obj;
587             }
588             foreach($ele as $el){
589               $new[] = $el;
590             }
591             foreach($end as $obj){
592               $new[] = $obj;
593             }
594             $data= $new;
595             $this->current_handler->tree_->pap = $data;
596             $this->add_new_element = FALSE;
597           }else{
598             print_red(_("Something went wrong while adding a new entry."));
599           }
600         }
601       }
603       /* Only display select dialog if it is necessary */
604       if($this->add_new_element){
605         $smarty = get_smarty();
606     
607         $add_else_elsif = FALSE;
609         /* Check if we should add else/elsif to the select box 
610          *  or not. We can't add else twice!.
611          */
612         if($this->add_above_below == "below"){
614           /* Get posistion of the current element 
615            */
616           foreach($this->current_handler->tree_->pap as $key => $obj){
617         
618             if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
619   
620               /* Get block start/end */
621               $end_id = $this->current_handler->tree_->get_block_end($key);
622               $else_found = FALSE;
623               $elsif_found = FALSE;
624           
625               /* Check if there is already an else in this block 
626                */
627               for($i =  $key ; $i < $end_id ; $i ++){
628                 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
629                   $else_found = TRUE;
630                 }
631                 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
632                   $elsif_found = TRUE;
633                 }
634               }
635   
636               /* Only allow adding 'else' if there is currently 
637                *  no 'else' statement. And don't allow adding 
638                *  'else' before 'elseif'
639                */ 
640               if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
641                 $element_types['sieve_else'] = _("Else");
642               }
643               $element_types['sieve_elsif'] = _("Else if");
644             }
645           }
646         }
648         $smarty->assign("element_types",$element_types );
649         $smarty->assign("element_type",$this->add_element_type);
650         $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
651         return($str);
652       }
656       /****************
657        * Handle test posts 
658        ****************/
660       /* handle some special posts from test elements 
661        */
662       foreach($_POST as $name => $value){
663         if(preg_match("/^Add_Test_Object_/",$name)) {
664           $name = preg_replace("/^Add_Test_Object_/","",$name);
665           $name = preg_replace("/_(x|y)$/","",$name);
667           $test_types_to_add = array(
668               "address" =>_("Address"),
669               "header"  =>_("Header"),
670               "envelope"=>_("Envelope"),
671               "size"    =>_("Size"),
672               "exists"  =>_("Exists"),
673               "allof"   =>_("All of"),
674               "anyof"   =>_("Any of"),
675               "true"    =>_("True"),
676               "false"   =>_("False"));
678           $smarty = get_smarty();
679           $smarty->assign("ID",$name);
680           $smarty->assign("test_types_to_add",$test_types_to_add);
681           $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
682           return($ret);
683         }
684       }
686       $current = $this->scripts[$this->current_script];
688       /* Create html results */
689       $smarty = get_smarty();
690       $smarty->assign("Mode",$current['MODE']);
691       if($current['MODE'] == "Structured"){
692         $smarty->assign("Contents",$this->current_handler->tree_->execute());
693       }else{
694         $smarty->assign("Contents",$current['SCRIPT']);
695       }
696       $smarty->assign("Script_Error",$this->Script_Error);
697       $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
698       return($ret);
699     }
702     /* Create list of available sieve scripts 
703      */
704     $List = new divSelectBox("sieveManagement");
705     foreach($this->scripts as $key => $script){
706   
707       $edited =  $script['EDITED'];
708       $active =  $script['ACTIVE'];
709       
710       $field1 = array("string" => "&nbsp;",
711                       "attach" => "style='width:20px;'");  
712       if($active){
713         $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
714                         "attach" => "style='width:20px;'");  
715       }
716       $field2 = array("string" => $script['NAME']);  
717       $field3 = array("string" => $script['MSG']);
718       $field4 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
720       if($edited){
721         $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
722                         "attach" => "style='width:30px;'");
723       }else{
724         $field5 = array("string" => "",
725                         "attach" => "style='width:30px;'");
726       }
728       if($active || $script['IS_NEW']){
729         $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
730                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
731                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
732       }else{
733         $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
734                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
735                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
736       }
737       $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6)); 
738     }
739   
740     $display ="<h2>Sieve script management</h2>";
741     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
742     $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
743     $display .=  $List->DrawList();
744     
745     $display .= "<p style=\"text-align:right\">\n";
746     $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
747     $display .= "&nbsp;\n";
748     $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
749     $display .= "</p>";
750     return($display);;
751   }
753   function save_object()
754   {
755     if($this->current_handler){
757       /* Check if there is an add object requested 
758        */
759       $data = $this->current_handler->tree_->pap;
760       $once = TRUE;
761       foreach($_POST as $name => $value){
762         foreach($data as $key => $obj){
763           if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
764             $once = FALSE;
765             $this->add_new_element    = TRUE;
766             $this->add_new_id         = $obj->object_id;
767             $this->add_above_below    = "above";
768           }
769           if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
770             $once = FALSE;
771             $this->add_new_element    = TRUE;
772             $this->add_new_id         = $obj->object_id;
773             $this->add_above_below    = "below";
774           }
775         
776           if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
777             $once = FALSE;
778             $this->current_handler->tree_->remove_object($key);
779           }
780           if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
781             $this->current_handler->tree_->move_up_down($key,"up");
782             $once = FALSE;
783           }
784           if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
785             $this->current_handler->tree_->move_up_down($key,"down");
786             $once = FALSE;
787           }
788         }
789       }
791       /* Skip Mode changes and Parse tests 
792        *  if we are currently in a subdialog 
793        */
795       $this->current_handler->save_object();
796       $Mode = $this->scripts[$this->current_script]['MODE'];
797       $skip_mode_change = false;
798       if(in_array($Mode,array("Source-Only","Source"))){
799         if(isset($_POST['script_contents'])){
800           $sc = stripslashes($_POST['script_contents']);
801           $this->scripts[$this->current_script]['SCRIPT'] = $sc;
802           $p = new My_Parser($this);
803           if($p -> parse($sc)){
804             $this->Script_Error = "";
805           } else {
806             $this->Script_Error = $p->status_text;
807             $skip_mode_change = TRUE;
808           }
809         }
810       }
811       if(in_array($Mode,array("Structured"))){
812         $sc = $this->current_handler->get_sieve_script();
813         $this->scripts[$this->current_script]['SCRIPT'] = $sc;
814         $p = new My_Parser($this);
815         if($p -> parse($sc)){
816           $this->Script_Error = "";
817         } else {
818           $this->Script_Error = $p->status_text;
819           $skip_mode_change = TRUE;
820         }
821       }
822       if(!$skip_mode_change){
823         if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
824           $old_mode = $this->scripts[$this->current_script]['MODE'];
825           if(isset($_POST['View_Source'])){
826             $this->scripts[$this->current_script]['MODE'] = "Source";
827           }
828           if(isset($_POST['View_Structured'])){
829             $this->scripts[$this->current_script]['MODE'] = "Structured";
830           }
831           $new_mode = $this->scripts[$this->current_script]['MODE'];
833           if($old_mode != $new_mode){
835             $sc = $this->scripts[$this->current_script]['SCRIPT'];
836             $p = new My_Parser($this);
838             if($p -> parse($sc)){
839               $this->current_handler->parse($sc);
840               $this->Script_Error = "";
841             } else {
842               $this->Script_Error = $p->status_text;
843             }
844           } 
845         }
846       }
847     }
848   }
850   
851   function get_used_script_names()
852   {
853     $ret = array();
854     foreach($this->scripts as $script){
855       $ret[] = $script['NAME'];
856     }
857     return($ret);
858   }
862   function save()
863   {
864     /* Get sieve */
865     if(!$this->sieve_handle = $this->get_sieve()){
866       print_red(
867           sprintf(
868             _("Can't log into SIEVE server. Server says '%s'."),
869             to_string($this->Sieve_Error)));
870     }
872     $everything_went_fine = TRUE;
874     foreach($this->scripts as $key => $script){
875       if($script['EDITED']){
876         $data = $this->scripts[$key]['SCRIPT'];
877         if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
878           gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
879           $everything_went_fine = FALSE;
880           print_red(to_string($this->sieve_handle->error_raw));
881           $this->scripts[$key]['MSG'] = "<font color='red'>".
882                                            _("Failed to save sieve script").": ".
883                                            to_string($this->sieve_handle->error_raw).
884                                            "</font>";
885         }
886       }
887     }
888     return($everything_went_fine);
889   }
891 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
892 ?>