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;
41 /* Initialize the class and load all sieve scripts
42 * try to parse them and display errors
43 */
44 function sieveManagement($config,$dn,$parent)
45 {
46 $this->parent = $parent;
47 plugin::plugin($config,$dn);
50 /* Connect to sieve class and try to get all available sieve scripts */
51 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
53 /* Log into the mail server */
54 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->parent->uid,
55 $cfg["password"], $cfg["admin"]);
57 /* Try to login */
58 if (!$sieve->sieve_login()){
59 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
60 to_string($sieve->error_raw)));
61 return;
62 }
64 /* Get all sieve scripts names */
65 if($sieve->sieve_listscripts()){
66 if (is_array($sieve->response)){
67 foreach($sieve->response as $key => $name){
69 $data = array();
70 $data['NAME'] = $name;
72 if($key == "ACTIVE" && $key === "ACTIVE"){
73 $data['ACTIVE'] = TRUE;
74 }else{
75 $data['ACTIVE'] = FALSE;
76 }
77 $this->scripts[] = $data;
78 }
79 }
80 }
82 /* Get script contents */
83 foreach($this->scripts as $key => $script){
84 $p = new My_Parser;
85 $sieve->sieve_getscript($script['NAME']);
87 $script = "";
88 foreach($sieve->response as $line){
89 $script.=$line;
90 }
92 $this->scripts[$key]['SCRIPT'] = $script;
93 $this->scripts[$key]['MSG'] = "";
94 $ret = $p->parse($script);
95 if(!$ret){
96 $this->scripts[$key]['MSG'] = "<font color='red'>".$p->status_text."</font>";
97 $this->scripts[$key]['STATUS'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
98 }else{
99 $this->scripts[$key]['STATUS'] = _("Parse successful");
100 }
101 $this->scripts[$key]['PARSER'] = $p;
102 $this->scripts[$key]['EDITED'] = FALSE;
103 }
104 $this->sieve_handle = $sieve;
105 }
108 /* Handle sieve list
109 */
110 function execute()
111 {
112 $once = TRUE;
113 foreach($_POST as $name => $value){
114 if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
115 $script = preg_replace("/^editscript_/","",$name);
116 $script = preg_replace("/_(x|y)/","",$script);
117 $once = FALSE;
119 $this->current_script = $script;
120 $this->current_handler = $this->scripts[$script]['PARSER'];
121 }
122 if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
123 $script = preg_replace("/^delscript_/","",$name);
124 $script = preg_replace("/_(x|y)/","",$script);
125 $once = FALSE;
127 $this->script_to_delete = $script;
128 }
129 if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
130 $script = preg_replace("/^active_script_/","",$name);
131 $script = preg_replace("/_(x|y)/","",$script);
132 $once = FALSE;
134 /* Connect to sieve class and try to get all available sieve scripts */
135 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
136 $this->sieve_handle=
137 new sieve( $cfg["sieve_server"],
138 $cfg["sieve_port"],
139 $this->parent->mail,
140 $cfg["password"],
141 $cfg["admin"]);
143 if (!$this->sieve_handle->sieve_login()){
144 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
145 return;
146 }
148 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
149 print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
150 }else{
152 foreach($this->scripts as $key => $data){
154 if($key == $script){
155 $this->scripts[$key]['ACTIVE'] = TRUE;
156 }else{
157 $this->scripts[$key]['ACTIVE'] = FALSE;
158 }
159 }
160 }
161 }
162 }
164 if(isset($_POST['delete_cancel'])){
165 $this->script_to_delete = -1;
166 }
168 if(isset($_POST['delete_script_confirm'])){
170 /* Connect to sieve class and try to get all available sieve scripts */
171 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
172 $this->sieve_handle=
173 new sieve( $cfg["sieve_server"],
174 $cfg["sieve_port"],
175 $this->parent->mail,
176 $cfg["password"],
177 $cfg["admin"]);
178 if (!$this->sieve_handle->sieve_login()){
179 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
180 return;
181 }
183 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
184 print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
185 }else{
186 unset($this->scripts[$this->script_to_delete]);
187 }
188 $this->script_to_delete = -1;
189 }
191 if($this->script_to_delete != -1){
192 $smarty = get_smarty();
193 $smarty->assign("Warning",
194 sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
195 $this->scripts[$this->script_to_delete]['NAME']));
197 return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
198 }
200 /* Abort saving */
201 if(isset($_POST['cancel_sieve_changes'])){
202 $this->current_handler = NULL;
203 }
205 /* Save currently edited sieve script. */
206 if(isset($_POST['save_sieve_changes'])){
207 $chk = $this->current_handler->check();
208 if(!count($chk)){
209 $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
210 $this->scripts[$this->current_script]['EDITED'] = TRUE;
211 $this->current_handler = NULL;
212 }else{
213 print_a($chk);
214 print_red(_("Please fix all errors before saving."));
215 }
216 }
218 /* Create output for currently opened sieve script */
219 if($this->current_handler){
220 $ret = $this->current_handler->execute();
221 $ret .= "<div class='seperator' style='text-align:right; width:100%;'>
222 <input type='submit' name='save_sieve_changes' value='"._("Save")."'>
223
224 <input type='submit' name='cancel_sieve_changes' value='"._("Cancel")."'>
225 </div>";
226 return($ret);
227 }
229 /* Create list of available sieve scripts
230 */
231 $List = new divSelectBox("sieveManagement");
232 foreach($this->scripts as $key => $script){
234 $edited = $script['EDITED'];
235 $active = $script['ACTIVE'];
237 $field1 = array("string" => " ",
238 "attach" => "style='width:20px;'");
239 if($active){
240 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
241 "attach" => "style='width:20px;'");
242 }
243 $field2 = array("string" => $script['NAME']);
244 $field3 = array("string" => $script['STATUS']);
245 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
247 if($edited){
248 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
249 "attach" => "style='width:30px;'");
250 }else{
251 $field5 = array("string" => "",
252 "attach" => "style='width:30px;'");
253 }
255 if($active){
256 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
257 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
258 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
259 }else{
260 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
261 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
262 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
263 }
264 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
265 }
267 $display ="<h2>Sieve script management</h2>";
268 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
269 $display .= $List->DrawList();
271 $display .= "<p style=\"text-align:right\">\n";
272 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
273 $display .= " \n";
274 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
275 $display .= "</p>";
276 return($display);;
277 }
279 function save_object()
280 {
281 if($this->current_handler){
282 $this->current_handler->save_object();
283 }
284 }
287 function save()
288 {
289 /* Connect to sieve class and try to get all available sieve scripts */
290 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
292 $this->sieve_handle=
293 new sieve( $cfg["sieve_server"],
294 $cfg["sieve_port"],
295 $this->parent->mail,
296 $cfg["password"],
297 $cfg["admin"]);
299 if (!$this->sieve_handle->sieve_login()){
300 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
301 return;
302 }
304 $everything_went_fine = TRUE;
306 foreach($this->scripts as $key => $script){
307 if($script['EDITED']){
308 $data = $script['PARSER']->get_sieve_script();
309 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
310 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
311 $everything_went_fine = FALSE;
312 print_red(to_string($this->sieve_handle->error_raw));
313 $this->scripts[$key]['STATUS'] = "<font color='red'>".
314 _("Failed to save sieve script").": ".
315 to_string($this->sieve_handle->error_raw).
316 "</font>";
317 # echo nl2br($data);
318 }
319 }
320 }
321 return($everything_went_fine);
322 }
323 }
324 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
325 ?>