ea355b2e3937a2c08108555cc388d385942807ab
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 $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
208 $this->scripts[$this->current_script]['EDITED'] = TRUE;
209 $this->current_handler = NULL;
210 }
212 /* Create output for currently opened sieve script */
213 if($this->current_handler){
214 $ret = $this->current_handler->execute();
215 $ret .= "<div class='seperator' style='text-align:right; width:100%;'>
216 <input type='submit' name='save_sieve_changes' value='"._("Save")."'>
217
218 <input type='submit' name='cancel_sieve_changes' value='"._("Cancel")."'>
219 </div>";
220 return($ret);
221 }
223 /* Create list of available sieve scripts
224 */
225 $List = new divSelectBox("sieveManagement");
226 foreach($this->scripts as $key => $script){
228 $edited = $script['EDITED'];
229 $active = $script['ACTIVE'];
231 $field1 = array("string" => " ",
232 "attach" => "style='width:20px;'");
233 if($active){
234 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
235 "attach" => "style='width:20px;'");
236 }
237 $field2 = array("string" => $script['NAME']);
238 $field3 = array("string" => $script['STATUS']);
239 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
241 if($edited){
242 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
243 "attach" => "style='width:30px;'");
244 }else{
245 $field5 = array("string" => "",
246 "attach" => "style='width:30px;'");
247 }
249 if($active){
250 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
251 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
252 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
253 }else{
254 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
255 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
256 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
257 }
258 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
259 }
261 $display ="<h2>Sieve script management</h2>";
262 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
263 $display .= $List->DrawList();
265 $display .= "<p style=\"text-align:right\">\n";
266 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
267 $display .= " \n";
268 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
269 $display .= "</p>";
270 return($display);;
271 }
273 function save_object()
274 {
275 if($this->current_handler){
276 $this->current_handler->save_object();
277 }
278 }
281 function save()
282 {
283 /* Connect to sieve class and try to get all available sieve scripts */
284 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
286 $this->sieve_handle=
287 new sieve( $cfg["sieve_server"],
288 $cfg["sieve_port"],
289 $this->parent->mail,
290 $cfg["password"],
291 $cfg["admin"]);
293 if (!$this->sieve_handle->sieve_login()){
294 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
295 return;
296 }
298 $everything_went_fine = TRUE;
300 foreach($this->scripts as $key => $script){
301 if($script['EDITED']){
302 $data = $script['PARSER']->get_sieve_script();
303 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
304 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
305 $everything_went_fine = FALSE;
306 print_red(to_string($this->sieve_handle->error_raw));
307 $this->scripts[$key]['STATUS'] = "<font color='red'>".
308 _("Failed to save sieve script").": ".
309 to_string($this->sieve_handle->error_raw).
310 "</font>";
311 }
312 }
313 }
314 return($everything_went_fine);
315 }
316 }
317 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
318 ?>