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 $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){
277 if(isset($_POST['script_contents']) && $this->current_handler->tree_->Mode == "Source"){
278 $script = stripslashes($_POST['script_contents']);
280 $new_p = new My_Parser;
281 if($new_p->parse($script)){
282 $this->current_handler->parse($script);
283 $this->current_handler->tree_->Mode = "Source";
284 }else{
285 print_red($new_p->status_text);
286 $this->current_handler->tree_->Mode = "Source";
287 }
288 }
290 if(isset($_POST['View_Source'])){
291 $this->current_handler->tree_->Mode = "Source";
292 }
294 if(isset($_POST['View_Structured'])){
295 $this->current_handler->tree_->Mode = "Structured";
296 }
298 $ret = $this->current_handler->execute();
299 return($ret);
300 }
302 /* Create list of available sieve scripts
303 */
304 $List = new divSelectBox("sieveManagement");
305 foreach($this->scripts as $key => $script){
307 $edited = $script['EDITED'];
308 $active = $script['ACTIVE'];
310 $field1 = array("string" => " ",
311 "attach" => "style='width:20px;'");
312 if($active){
313 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
314 "attach" => "style='width:20px;'");
315 }
316 $field2 = array("string" => $script['NAME']);
317 $field3 = array("string" => $script['STATUS']);
318 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
320 if($edited){
321 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
322 "attach" => "style='width:30px;'");
323 }else{
324 $field5 = array("string" => "",
325 "attach" => "style='width:30px;'");
326 }
328 if($active){
329 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
330 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
331 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
332 }else{
333 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
334 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
335 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
336 }
337 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
338 }
340 $display ="<h2>Sieve script management</h2>";
341 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
342 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
343 $display .= $List->DrawList();
345 $display .= "<p style=\"text-align:right\">\n";
346 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
347 $display .= " \n";
348 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
349 $display .= "</p>";
350 return($display);;
351 }
353 function save_object()
354 {
355 if($this->current_handler){
356 $this->current_handler->save_object();
357 }
358 }
361 function save()
362 {
363 /* Connect to sieve class and try to get all available sieve scripts */
364 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
366 $this->sieve_handle=
367 new sieve( $cfg["sieve_server"],
368 $cfg["sieve_port"],
369 $this->parent->mail,
370 $cfg["password"],
371 $cfg["admin"]);
373 if (!$this->sieve_handle->sieve_login()){
374 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
375 return;
376 }
378 $everything_went_fine = TRUE;
380 foreach($this->scripts as $key => $script){
381 if($script['EDITED']){
382 $data = $script['PARSER']->get_sieve_script();
383 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
384 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
385 $everything_went_fine = FALSE;
386 print_red(to_string($this->sieve_handle->error_raw));
387 $this->scripts[$key]['STATUS'] = "<font color='red'>".
388 _("Failed to save sieve script").": ".
389 to_string($this->sieve_handle->error_raw).
390 "</font>";
391 echo nl2br($data);
392 }
393 }
394 }
395 return($everything_went_fine);
396 }
397 }
398 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
399 ?>