c4b8d338aaca97a7a99cb0706f85617568d05501
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]['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();
124 $name = "";
125 if(isset($_POST['NewScriptName'])){
126 $name = $_POST['NewScriptName'];
127 }
129 $err = "";
130 if($name != strtolower($name)){
131 $err = _("Only lower case names are allowed here.");
132 }
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'] = "";
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;
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;
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 if(!count($chk)){
264 $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
265 $this->scripts[$this->current_script]['EDITED'] = TRUE;
266 $this->current_handler = NULL;
267 }else{
268 print_a($chk);
269 print_red(_("Please fix all errors before saving."));
270 }
271 }
273 /* Create output for currently opened sieve script */
274 if($this->current_handler){
275 $ret = $this->current_handler->execute();
276 return($ret);
277 }
279 /* Create list of available sieve scripts
280 */
281 $List = new divSelectBox("sieveManagement");
282 foreach($this->scripts as $key => $script){
284 $edited = $script['EDITED'];
285 $active = $script['ACTIVE'];
287 $field1 = array("string" => " ",
288 "attach" => "style='width:20px;'");
289 if($active){
290 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
291 "attach" => "style='width:20px;'");
292 }
293 $field2 = array("string" => $script['NAME']);
294 $field3 = array("string" => $script['STATUS']);
295 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
297 if($edited){
298 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
299 "attach" => "style='width:30px;'");
300 }else{
301 $field5 = array("string" => "",
302 "attach" => "style='width:30px;'");
303 }
305 if($active){
306 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
307 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
308 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
309 }else{
310 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
311 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
312 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
313 }
314 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
315 }
317 $display ="<h2>Sieve script management</h2>";
318 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
319 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
320 $display .= $List->DrawList();
322 $display .= "<p style=\"text-align:right\">\n";
323 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
324 $display .= " \n";
325 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
326 $display .= "</p>";
327 return($display);;
328 }
330 function save_object()
331 {
332 if($this->current_handler){
333 $this->current_handler->save_object();
334 }
335 }
338 function save()
339 {
340 /* Connect to sieve class and try to get all available sieve scripts */
341 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
343 $this->sieve_handle=
344 new sieve( $cfg["sieve_server"],
345 $cfg["sieve_port"],
346 $this->parent->mail,
347 $cfg["password"],
348 $cfg["admin"]);
350 if (!$this->sieve_handle->sieve_login()){
351 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
352 return;
353 }
355 $everything_went_fine = TRUE;
357 foreach($this->scripts as $key => $script){
358 if($script['EDITED']){
359 $data = $script['PARSER']->get_sieve_script();
360 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
361 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
362 $everything_went_fine = FALSE;
363 print_red(to_string($this->sieve_handle->error_raw));
364 $this->scripts[$key]['STATUS'] = "<font color='red'>".
365 _("Failed to save sieve script").": ".
366 to_string($this->sieve_handle->error_raw).
367 "</font>";
368 echo nl2br($data);
369 }
370 }
371 }
372 return($everything_went_fine);
373 }
374 }
375 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
376 ?>