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];
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();
128 $name = "";
129 if(isset($_POST['NewScriptName'])){
130 $name = $_POST['NewScriptName'];
131 }
133 $err = "";
134 if($name != strtolower($name)){
135 $err = _("Only lower case names are allowed here.");
136 }
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'] = "";
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;
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;
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 if(!count($chk)){
273 $sc = $this->scripts[$this->current_script]['SCRIPT'];
274 $p = new My_Parser;
275 if($p -> parse($sc)){
277 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
278 $this->scripts[$this->current_script]['MODE'] = "Source";
279 }
281 $this->scripts[$this->current_script]['PARSER'] = $p;
282 $this->scripts[$this->current_script]['EDITED'] = TRUE;
283 $this->scripts[$this->current_script]['STATUS'] = TRUE;
284 $this->scripts[$this->current_script]['MSG'] = _("Edited");
285 $this->current_handler = NULL;
286 }else{
287 print_red($p->status_text);;
288 }
289 }else{
290 print_red(_("Please fix all errors before saving."));
291 }
292 }
295 /*************
296 * Display edit dialog
297 *************/
299 /* Display edit dialog, depending on Mode display different uis
300 */
301 if($this->current_handler){
303 $current = $this->scripts[$this->current_script];
305 /* Create html results */
306 $smarty = get_smarty();
307 $smarty->assign("Mode",$current['MODE']);
308 if($current['MODE'] == "Structured"){
309 $smarty->assign("Contents",$this->current_handler->tree_->execute());
310 }else{
311 $smarty->assign("Contents",$current['SCRIPT']);
312 }
313 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
314 return($ret);
315 }
318 /* Create list of available sieve scripts
319 */
320 $List = new divSelectBox("sieveManagement");
321 foreach($this->scripts as $key => $script){
323 $edited = $script['EDITED'];
324 $active = $script['ACTIVE'];
326 $field1 = array("string" => " ",
327 "attach" => "style='width:20px;'");
328 if($active){
329 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
330 "attach" => "style='width:20px;'");
331 }
332 $field2 = array("string" => $script['NAME']);
333 $field3 = array("string" => $script['MSG']);
334 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
336 if($edited){
337 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
338 "attach" => "style='width:30px;'");
339 }else{
340 $field5 = array("string" => "",
341 "attach" => "style='width:30px;'");
342 }
344 if($active){
345 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
346 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
347 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
348 }else{
349 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
350 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
351 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
352 }
353 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
354 }
356 $display ="<h2>Sieve script management</h2>";
357 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
358 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
359 $display .= $List->DrawList();
361 $display .= "<p style=\"text-align:right\">\n";
362 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
363 $display .= " \n";
364 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
365 $display .= "</p>";
366 return($display);;
367 }
369 function save_object()
370 {
371 if($this->current_handler){
373 $Mode = $this->scripts[$this->current_script]['MODE'];
374 $skip_mode_change = false;
375 if(in_array($Mode,array("Source-Only","Source"))){
376 if(isset($_POST['script_contents'])){
377 $sc = stripslashes($_POST['script_contents']);
378 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
379 $p = new My_Parser;
380 if($p -> parse($sc)){
381 $this->current_handler = $p;
382 } else {
383 print_red($p->status_text);
384 $skip_mode_change = TRUE;
385 }
386 }
387 }
388 if(in_array($Mode,array("Structured"))){
389 $this->current_handler->save_object();
390 $sc = $this->current_handler->get_sieve_script();
391 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
392 $p = new My_Parser;
393 if($p -> parse($sc)){
394 $this->current_handler = $p;
395 } else {
396 print_red($p->status_text);
397 $skip_mode_change = TRUE;
398 }
399 }
400 if(!$skip_mode_change){
401 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
402 if(isset($_POST['View_Source'])){
403 $this->scripts[$this->current_script]['MODE'] = "Source";
404 }
405 if(isset($_POST['View_Structured'])){
406 $this->scripts[$this->current_script]['MODE'] = "Structured";
407 }
408 }
409 }
410 $this->current_handler->save_object();
411 }
412 }
415 function save()
416 {
417 /* Connect to sieve class and try to get all available sieve scripts */
418 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
420 $this->sieve_handle=
421 new sieve( $cfg["sieve_server"],
422 $cfg["sieve_port"],
423 $this->parent->mail,
424 $cfg["password"],
425 $cfg["admin"]);
427 if (!$this->sieve_handle->sieve_login()){
428 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
429 return;
430 }
432 $everything_went_fine = TRUE;
434 foreach($this->scripts as $key => $script){
435 if($script['EDITED']){
436 $data = $this->scripts[$key]['SCRIPT'];
437 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
438 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
439 $everything_went_fine = FALSE;
440 print_red(to_string($this->sieve_handle->error_raw));
441 $this->scripts[$key]['MSG'] = "<font color='red'>".
442 _("Failed to save sieve script").": ".
443 to_string($this->sieve_handle->error_raw).
444 "</font>";
445 }
446 }
447 }
448 return($everything_went_fine);
449 }
450 }
451 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
452 ?>