Code

Created a sieve script, saved it and it works !!!
[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; 
40   var $create_script = FALSE;
42   /* Initialize the class and load all sieve scripts 
43    *  try to parse them and display errors 
44    */ 
45   function sieveManagement($config,$dn,$parent)
46   {
47     $this->parent = $parent;
48     plugin::plugin($config,$dn);
51     /* Connect to sieve class and try to get all available sieve scripts */
52     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
53     
54     /* Log into the mail server */
55     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->parent->uid,
56         $cfg["password"], $cfg["admin"]);
58     /* Try to login */
59     if (!$sieve->sieve_login()){
60       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
61             to_string($sieve->error_raw)));
62       return;
63     }
65     /* Get all sieve scripts names */
66     if($sieve->sieve_listscripts()){
67       if (is_array($sieve->response)){
68         foreach($sieve->response as $key => $name){
70           $data = array();
71           $data['NAME'] = $name;
73           if($key == "ACTIVE" && $key === "ACTIVE"){
74             $data['ACTIVE'] = TRUE;
75           }else{
76             $data['ACTIVE'] = FALSE;
77           }
78           $this->scripts[] = $data;          
79         }
80       } 
81     }
83     /* Get script contents */
84     foreach($this->scripts as $key => $script){
85       $p = new My_Parser;
86       $sieve->sieve_getscript($script['NAME']);
88       $script = "";
89       foreach($sieve->response as $line){
90         $script.=$line;
91       }
93       $this->scripts[$key]['SCRIPT'] = $script;
94       $this->scripts[$key]['MSG']   = "";
95       $ret = $p->parse($script);
96       if(!$ret){
97         $this->scripts[$key]['MSG']   = "<font color='red'>".$p->status_text."</font>";
98         $this->scripts[$key]['STATUS'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
99       }else{
100         $this->scripts[$key]['STATUS'] = _("Parse successful");
101       }
102       $this->scripts[$key]['PARSER'] = $p;
103       $this->scripts[$key]['EDITED'] = FALSE;
104     }
105     $this->sieve_handle = $sieve;
106   }
109   /* Handle sieve list 
110    */
111   function execute()
112   {
113     if(isset($_POST['create_new_script'])){
114       $this->create_script = TRUE;
115     }
117     if(isset($_POST['create_script_cancel'])){
118       $this->create_script = FALSE;
119     }
121     if($this->create_script){
122       $smarty = get_smarty();
123     
124       $name = "";
125       if(isset($_POST['NewScriptName'])){
126         $name = $_POST['NewScriptName'];
127       }
128   
129       $err = "";
130       if($name != strtolower($name)){
131         $err = _("Only lower case names are allowed here.");
132       }
133   
134       if(preg_match("/[^a-z]/i",$name)){
135         $err = _("Only a-z are allowed in script names.");
136       }
138       if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
139         $this->create_script = FALSE;
141         $script = "/*New script */".
142                   "stop;";
143         $p = new My_Parser;
145         $sc['SCRIPT'] = $script;
146         $sc['MSG']   = "";
147   
148         $ret = $p->parse($script);
149         if(!$ret){
150           $sc['MSG']   = "<font color='red'>".$p->status_text."</font>";
151           $sc['STATUS'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
152         }else{
153           $sc['STATUS'] = _("Parse successful");
154         }
155         $sc['PARSER'] = $p;
156         $sc['EDITED'] = TRUE;
157         $sc['ACTIVE'] = FALSE;
158         $sc['NAME']   = $name;
159         $this->scripts[$name] = $sc;
160       }else{
161         $smarty->assign("NewScriptName",$name);
162         $smarty->assign("Error",$err);
163         return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
164       }
165     }
167     $once = TRUE;
168     foreach($_POST as $name => $value){
169       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
170         $script = preg_replace("/^editscript_/","",$name);
171         $script = preg_replace("/_(x|y)/","",$script);
172         $once = FALSE;
174         $this->current_script = $script;
175         $this->current_handler = $this->scripts[$script]['PARSER'];
176       }
177       if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
178         $script = preg_replace("/^delscript_/","",$name);
179         $script = preg_replace("/_(x|y)/","",$script);
180         $once = FALSE;
181  
182         $this->script_to_delete = $script;  
183       }
184       if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
185         $script = preg_replace("/^active_script_/","",$name);
186         $script = preg_replace("/_(x|y)/","",$script);
187         $once = FALSE;
188  
189         /* Connect to sieve class and try to get all available sieve scripts */
190         $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
191         $this->sieve_handle=
192           new sieve(  $cfg["sieve_server"],
193               $cfg["sieve_port"],
194               $this->parent->mail,
195               $cfg["password"],
196               $cfg["admin"]);
198         if (!$this->sieve_handle->sieve_login()){
199           print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
200           return;
201         }
203         if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
204           print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
205         }else{
207           foreach($this->scripts as $key => $data){
209             if($key == $script){
210               $this->scripts[$key]['ACTIVE'] = TRUE;
211             }else{
212               $this->scripts[$key]['ACTIVE'] = FALSE;
213             }
214           }
215         }
216       }
217     }
219     if(isset($_POST['delete_cancel'])){
220       $this->script_to_delete = -1;
221     }
223     if(isset($_POST['delete_script_confirm'])){
225       /* Connect to sieve class and try to get all available sieve scripts */
226       $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
227       $this->sieve_handle=
228         new sieve(  $cfg["sieve_server"],
229             $cfg["sieve_port"],
230             $this->parent->mail,
231             $cfg["password"],
232             $cfg["admin"]);
233       if (!$this->sieve_handle->sieve_login()){
234         print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
235         return;
236       }
238       if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
239         print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
240       }else{
241         unset($this->scripts[$this->script_to_delete]);
242       }
243       $this->script_to_delete = -1;
244     }
246     if($this->script_to_delete != -1){
247       $smarty = get_smarty();
248       $smarty->assign("Warning",
249           sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
250             $this->scripts[$this->script_to_delete]['NAME']));
252       return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
253     }
255     /* Abort saving */
256     if(isset($_POST['cancel_sieve_changes'])){
257       $this->current_handler = NULL;
258     }
260     /* Save currently edited sieve script. */
261     if(isset($_POST['save_sieve_changes'])){
262       $chk = $this->current_handler->check();
263       $chk =array();
264       if(!count($chk)){
265         $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
266         $this->scripts[$this->current_script]['EDITED'] = TRUE;
267         $this->current_handler = NULL; 
268       }else{
269         print_a($chk);
270         print_red(_("Please fix all errors before saving."));
271       }
272     }
274     /* Create output for currently opened sieve script */
275     if($this->current_handler){
276       $ret = $this->current_handler->execute();
277       return($ret);
278     }
280     /* Create list of available sieve scripts 
281      */
282     $List = new divSelectBox("sieveManagement");
283     foreach($this->scripts as $key => $script){
284   
285       $edited =  $script['EDITED'];
286       $active =  $script['ACTIVE'];
287       
288       $field1 = array("string" => "&nbsp;",
289                       "attach" => "style='width:20px;'");  
290       if($active){
291         $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
292                         "attach" => "style='width:20px;'");  
293       }
294       $field2 = array("string" => $script['NAME']);  
295       $field3 = array("string" => $script['STATUS']);
296       $field4 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
298       if($edited){
299         $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
300                         "attach" => "style='width:30px;'");
301       }else{
302         $field5 = array("string" => "",
303                         "attach" => "style='width:30px;'");
304       }
306       if($active){
307         $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
308                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
309                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
310       }else{
311         $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
312                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
313                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
314       }
315       $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6)); 
316     }
317   
318     $display ="<h2>Sieve script management</h2>";
319     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
320     $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
321     $display .=  $List->DrawList();
322     
323     $display .= "<p style=\"text-align:right\">\n";
324     $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
325     $display .= "&nbsp;\n";
326     $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
327     $display .= "</p>";
328     return($display);;
329   }
331   function save_object()
332   {
333     if($this->current_handler){
334       $this->current_handler->save_object();
335     }
336   }
339   function save()
340   {
341     /* Connect to sieve class and try to get all available sieve scripts */
342     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
344     $this->sieve_handle= 
345         new sieve(  $cfg["sieve_server"], 
346                     $cfg["sieve_port"], 
347                     $this->parent->mail,
348                     $cfg["password"], 
349                     $cfg["admin"]);
351     if (!$this->sieve_handle->sieve_login()){
352       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
353       return;
354     }
356     $everything_went_fine = TRUE;
358     foreach($this->scripts as $key => $script){
359       if($script['EDITED']){
360         $data = $script['PARSER']->get_sieve_script();
361         if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
362           gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
363           $everything_went_fine = FALSE;
364           print_red(to_string($this->sieve_handle->error_raw));
365           $this->scripts[$key]['STATUS'] = "<font color='red'>".
366                                            _("Failed to save sieve script").": ".
367                                            to_string($this->sieve_handle->error_raw).
368                                            "</font>";
369           echo nl2br($data);
370         }
371       }
372     }
373     return($everything_went_fine);
374   }
376 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
377 ?>