Code

e16bbdce6bab75192520240447e5b0c21dbc3a3b
[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]['ORIG_SCRIPT'] = $script;
95       $this->scripts[$key]['MSG']   = "";
96       $ret = $p->parse($script);
97       if(!$ret){
98         $this->scripts[$key]['STATUS']   = FALSE;
99         $this->scripts[$key]['MODE']    = "Source-Only";
100         $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
101       }else{
102         $this->scripts[$key]['STATUS']   = TRUE;
103         $this->scripts[$key]['MODE']    = "Structured";
104         $this->scripts[$key]['MSG'] = _("Parse successful");
105       }
106       $this->scripts[$key]['PARSER'] = $p;
107       $this->scripts[$key]['EDITED'] = FALSE;
108     }
109     $this->sieve_handle = $sieve;
110   }
113   /* Handle sieve list 
114    */
115   function execute()
116   {
117     if(isset($_POST['create_new_script'])){
118       $this->create_script = TRUE;
119     }
121     if(isset($_POST['create_script_cancel'])){
122       $this->create_script = FALSE;
123     }
125     if($this->create_script){
126       $smarty = get_smarty();
127     
128       $name = "";
129       if(isset($_POST['NewScriptName'])){
130         $name = $_POST['NewScriptName'];
131       }
132   
133       $err = "";
134       if($name != strtolower($name)){
135         $err = _("Only lower case names are allowed here.");
136       }
137   
138       if(preg_match("/[^a-z]/i",$name)){
139         $err = _("Only a-z are allowed in script names.");
140       }
142       if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
143         $this->create_script = FALSE;
145         $script = "/*New script */".
146                   "stop;";
147         $p = new My_Parser;
149         $sc['SCRIPT'] = $script;
150         $sc['ORIG_SCRIPT'] = $script;
151         $sc['MSG']   = "";
152   
153         $ret = $p->parse($script);
154         if(!$ret){
155           $sc['STATUS']   = FALSE;
156           $sc['MODE']    = "Source-Only";
157           $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
158         }else{
159           $sc['STATUS']   = TRUE;
160           $sc['MODE']    = "Strucktured";
161           $sc['MSG'] = _("Parse successful");
162         }
163         $sc['PARSER'] = $p;
164         $sc['EDITED'] = TRUE;
165         $sc['ACTIVE'] = FALSE;
166         $sc['NAME']   = $name;
167         $this->scripts[$name] = $sc;
168       }else{
169         $smarty->assign("NewScriptName",$name);
170         $smarty->assign("Error",$err);
171         return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
172       }
173     }
175     $once = TRUE;
176     foreach($_POST as $name => $value){
177       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
178         $script = preg_replace("/^editscript_/","",$name);
179         $script = preg_replace("/_(x|y)/","",$script);
180         $once = FALSE;
182         $this->current_script = $script;
183         $this->current_handler = $this->scripts[$script]['PARSER'];
184       }
185       if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
186         $script = preg_replace("/^delscript_/","",$name);
187         $script = preg_replace("/_(x|y)/","",$script);
188         $once = FALSE;
189  
190         $this->script_to_delete = $script;  
191       }
192       if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
193         $script = preg_replace("/^active_script_/","",$name);
194         $script = preg_replace("/_(x|y)/","",$script);
195         $once = FALSE;
196  
197         /* Connect to sieve class and try to get all available sieve scripts */
198         $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
199         $this->sieve_handle=
200           new sieve(  $cfg["sieve_server"],
201               $cfg["sieve_port"],
202               $this->parent->mail,
203               $cfg["password"],
204               $cfg["admin"]);
206         if (!$this->sieve_handle->sieve_login()){
207           print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
208           return;
209         }
211         if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
212           print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
213         }else{
215           foreach($this->scripts as $key => $data){
217             if($key == $script){
218               $this->scripts[$key]['ACTIVE'] = TRUE;
219             }else{
220               $this->scripts[$key]['ACTIVE'] = FALSE;
221             }
222           }
223         }
224       }
225     }
227     if(isset($_POST['delete_cancel'])){
228       $this->script_to_delete = -1;
229     }
231     if(isset($_POST['delete_script_confirm'])){
233       /* Connect to sieve class and try to get all available sieve scripts */
234       $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
235       $this->sieve_handle=
236         new sieve(  $cfg["sieve_server"],
237             $cfg["sieve_port"],
238             $this->parent->mail,
239             $cfg["password"],
240             $cfg["admin"]);
241       if (!$this->sieve_handle->sieve_login()){
242         print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
243         return;
244       }
246       if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
247         print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
248       }else{
249         unset($this->scripts[$this->script_to_delete]);
250       }
251       $this->script_to_delete = -1;
252     }
254     if($this->script_to_delete != -1){
255       $smarty = get_smarty();
256       $smarty->assign("Warning",
257           sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
258             $this->scripts[$this->script_to_delete]['NAME']));
260       return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
261     }
263     /* Abort saving */
264     if(isset($_POST['cancel_sieve_changes'])){
265       $this->current_handler = NULL;
266     }
268     /* Save currently edited sieve script. */
269     if(isset($_POST['save_sieve_changes'])){
270       $chk = $this->current_handler->check();
271       $chk =array();
272       if(!count($chk)){
274         $sc = $this->scripts[$this->current_script]['SCRIPT'];
275         $p = new My_Parser;
276         if($p -> parse($sc)){
278           if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
279             $this->scripts[$this->current_script]['MODE'] = "Source";
280           }
281   
282           $this->scripts[$this->current_script]['PARSER'] = $p;
283           $this->scripts[$this->current_script]['EDITED'] = TRUE;
284           $this->scripts[$this->current_script]['STATUS'] = TRUE;
285           $this->scripts[$this->current_script]['MSG'] = _("Edited");
286           $this->current_handler = NULL;
287         }else{
288           print_red($p->status_text);;
289         }
290       }else{
291         print_red(_("Please fix all errors before saving."));
292       }
293     }
296     /*************
297      * Display edit dialog 
298      *************/
300     /* Display edit dialog, depending on Mode display different uis
301      */
302     if($this->current_handler){
304       $current = $this->scripts[$this->current_script];
306       /* Create html results */
307       $smarty = get_smarty();
308       $smarty->assign("Mode",$current['MODE']);
309       if($current['MODE'] == "Structured"){
310         $smarty->assign("Contents",$this->current_handler->tree_->execute());
311       }else{
312         $smarty->assign("Contents",$current['SCRIPT']);
313       }
314       $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
315       return($ret);
316     }
319     /* Create list of available sieve scripts 
320      */
321     $List = new divSelectBox("sieveManagement");
322     foreach($this->scripts as $key => $script){
323   
324       $edited =  $script['EDITED'];
325       $active =  $script['ACTIVE'];
326       
327       $field1 = array("string" => "&nbsp;",
328                       "attach" => "style='width:20px;'");  
329       if($active){
330         $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
331                         "attach" => "style='width:20px;'");  
332       }
333       $field2 = array("string" => $script['NAME']);  
334       $field3 = array("string" => $script['MSG']);
335       $field4 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
337       if($edited){
338         $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
339                         "attach" => "style='width:30px;'");
340       }else{
341         $field5 = array("string" => "",
342                         "attach" => "style='width:30px;'");
343       }
345       if($active){
346         $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
347                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
348                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
349       }else{
350         $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
351                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
352                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
353       }
354       $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6)); 
355     }
356   
357     $display ="<h2>Sieve script management</h2>";
358     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
359     $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
360     $display .=  $List->DrawList();
361     
362     $display .= "<p style=\"text-align:right\">\n";
363     $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
364     $display .= "&nbsp;\n";
365     $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
366     $display .= "</p>";
367     return($display);;
368   }
370   function save_object()
371   {
372     if($this->current_handler){
374       $Mode = $this->scripts[$this->current_script]['MODE'];
375       $skip_mode_change = false;
376       if(in_array($Mode,array("Source-Only","Source"))){
377         if(isset($_POST['script_contents'])){
378           $sc = stripslashes($_POST['script_contents']);
379           $this->scripts[$this->current_script]['SCRIPT'] = $sc;
380           $p = new My_Parser;
381           if($p -> parse($sc)){
382             $this->current_handler = $p;
383           } else {
384             print_red($p->status_text);
385             $skip_mode_change = TRUE;
386           }
387         }
388       }
389       if(!$skip_mode_change){
390         if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
391           if(isset($_POST['View_Source'])){
392             $this->scripts[$this->current_script]['MODE'] = "Source";
393           }
394           if(isset($_POST['View_Structured'])){
395             $this->scripts[$this->current_script]['MODE'] = "Structured";
396           }
397         }
398       }
399       $this->current_handler->save_object();
400     }
401   }
404   function save()
405   {
406     /* Connect to sieve class and try to get all available sieve scripts */
407     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
409     $this->sieve_handle= 
410         new sieve(  $cfg["sieve_server"], 
411                     $cfg["sieve_port"], 
412                     $this->parent->mail,
413                     $cfg["password"], 
414                     $cfg["admin"]);
416     if (!$this->sieve_handle->sieve_login()){
417       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
418       return;
419     }
421     $everything_went_fine = TRUE;
423     foreach($this->scripts as $key => $script){
424       if($script['EDITED']){
425         $data = $this->scripts[$key]['SCRIPT'];
426         if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
427           gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
428           $everything_went_fine = FALSE;
429           print_red(to_string($this->sieve_handle->error_raw));
430           $this->scripts[$key]['MSG'] = "<font color='red'>".
431                                            _("Failed to save sieve script").": ".
432                                            to_string($this->sieve_handle->error_raw).
433                                            "</font>";
434         }
435       }
436     }
437     return($everything_went_fine);
438   }
440 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
441 ?>