Code

Several sieve filter updates
[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  
38   /* Initialize the class and load all sieve scripts 
39    *  try to parse them and display errors 
40    */ 
41   function sieveManagement($config,$dn,$parent)
42   {
43     $this->parent = $parent;
44     plugin::plugin($config,$dn);
47     /* Connect to sieve class and try to get all available sieve scripts */
48     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
49     
50     /* Log into the mail server */
51     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->parent->uid,
52         $cfg["password"], $cfg["admin"]);
54     /* Try to login */
55     if (!$sieve->sieve_login()){
56       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
57             to_string($sieve->error_raw)));
58       return;
59     }
61     /* Get all sieve scripts names */
62     if($sieve->sieve_listscripts()){
63       if (is_array($sieve->response)){
64         foreach($sieve->response as $key => $name){
65           $this->scripts[$key]['NAME'] = $name;
66         }
67       } 
68     }
70     /* Get script contents */
71     foreach($this->scripts as $key => $script){
72       $p = new My_Parser;
73       $sieve->sieve_getscript($script['NAME']);
75       $script = "";
76       foreach($sieve->response as $line){
77         $script.=$line;
78       }
80       $this->scripts[$key]['SCRIPT'] = $script;
81       $this->scripts[$key]['MSG']   = "";
82       $ret = $p->parse($script);
83       if(!$ret){
84         $this->scripts[$key]['MSG']   = "<font color='red'>".$p->status_text."</font>";
85         $this->scripts[$key]['STATUS'] = FALSE;
86       }else{
87         $this->scripts[$key]['STATUS'] = TRUE;
88       }
89       $this->scripts[$key]['PARSER'] = $p;
90     }
91   }
94   /* Handle sieve list 
95    */
96   function execute()
97   {
98     $once = TRUE;
99     foreach($_POST as $name => $value){
100       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
101         $script = preg_replace("/^editscript_/","",$name);
102         $script = preg_replace("/_(x|y)/","",$script);
103         $once = FALSE;
105         $this->current_script = $script;
106         $this->current_handler = $this->scripts[$script]['PARSER'];
107       }
108     }
110     /* Abort saving */
111     if(isset($_POST['cancel_sieve_changes'])){
112       $this->current_handler = NULL;
113     }
115     /* Save currently edited sieve script. */
116     if(isset($_POST['save_sieve_changes'])){
117       $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
118       $this->current_handler = NULL; 
119     }
121     /* Create output for currently opened sieve script */
122     if($this->current_handler){
123       $ret = $this->current_handler->execute();
124       $ret .= "<div class='seperator' style='text-align:right; width:100%;'>
125         <input type='submit' name='save_sieve_changes' value='"._("Save")."'>
126         &nbsp;
127       <input type='submit' name='cancel_sieve_changes' value='"._("Cancel")."'>
128         </div>";
129       return($ret);
130     }
132     /* Create list of available sieve scripts 
133      */
134     $List = new divSelectBox("sieveManagement");
135     foreach($this->scripts as $key => $script){
136       $field1 = array("string" => $script['NAME']);  
137       if($script['STATUS']){
138         $field2 = array("string" => _("Parse successful"));
139       }else{
140         $field2 = array("string" => _("Parse failed") .$script['MSG']);
141       }
142       $field3 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
143       $field4 = array("string" => "<input type='image' name='editscript_".$key."' src='images/edit.png'>");
144       $List ->AddEntry(array($field1,$field2,$field3,$field4)); 
145     }
146   
147     $display ="<h2>Sieve script management</h2>";
148     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
149     $display .=  $List->DrawList();
150     
151     $display .= "<p style=\"text-align:right\">\n";
152     $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
153     $display .= "&nbsp;\n";
154     $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
155     $display .= "</p>";
156     return($display);;
157   }
159   function save_object()
160   {
161     if($this->current_handler){
162       $this->current_handler->save_object();
163     }
164   }
167   function save()
168   {
169     $ret = "<textarea style='width:100%;height:400px;'>";
170     foreach($this->scripts as $script){
171       $ret .= ($script['PARSER']->get_sieve_script());
172     }
173     $ret .= "</textarea>";
174     echo $ret;
175   }
177 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
178 ?>