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;
39 var $Script_Error = "";
40 var $create_script = FALSE;
42 /* To add new elements we need to know
43 * Where to the element -> add_new_id
44 * Whould we add above or below this id -> add_above_below
45 * What kind of element should we add -> add_element_type
46 */
47 var $add_new_element = FALSE;
48 var $add_new_id = -1;
49 var $add_above_below = "below";
50 var $add_element_type = "sieve_comment";
53 /* Initialize the class and load all sieve scripts
54 * try to parse them and display errors
55 */
56 function sieveManagement($config,$dn,$parent)
57 {
58 $this->parent = $parent;
59 plugin::plugin($config,$dn);
62 /* Connect to sieve class and try to get all available sieve scripts */
63 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
65 /* Log into the mail server */
66 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->parent->uid,
67 $cfg["password"], $cfg["admin"]);
69 /* Try to login */
70 if (!$sieve->sieve_login()){
71 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
72 to_string($sieve->error_raw)));
73 return;
74 }
76 /* Get all sieve scripts names */
77 if($sieve->sieve_listscripts()){
78 if (is_array($sieve->response)){
79 foreach($sieve->response as $key => $name){
81 $data = array();
82 $data['NAME'] = $name;
84 if($key == "ACTIVE" && $key === "ACTIVE"){
85 $data['ACTIVE'] = TRUE;
86 }else{
87 $data['ACTIVE'] = FALSE;
88 }
89 $this->scripts[] = $data;
90 }
91 }
92 }
94 /* Get script contents */
95 foreach($this->scripts as $key => $script){
96 $p = new My_Parser;
97 $sieve->sieve_getscript($script['NAME']);
99 $script = "";
100 foreach($sieve->response as $line){
101 $script.=$line;
102 }
104 $this->scripts[$key]['IS_NEW'] = FALSE;;
105 $this->scripts[$key]['SCRIPT'] = $script;
106 $this->scripts[$key]['ORIG_SCRIPT'] = $script;
107 $this->scripts[$key]['MSG'] = "";
108 $ret = $p->parse($script);
109 if(!$ret){
110 $this->scripts[$key]['STATUS'] = FALSE;
111 $this->scripts[$key]['MODE'] = "Source-Only";
112 $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
113 }else{
114 $this->scripts[$key]['STATUS'] = TRUE;
115 $this->scripts[$key]['MODE'] = "Structured";
116 $this->scripts[$key]['MSG'] = _("Parse successful");
117 }
118 $this->scripts[$key]['PARSER'] = $p;
119 $this->scripts[$key]['EDITED'] = FALSE;
120 }
121 $this->sieve_handle = $sieve;
122 }
125 /* Handle sieve list
126 */
127 function execute()
128 {
129 /***************
130 * Create a new Script
131 ***************/
133 /* Force opening the add script dialog */
134 if(isset($_POST['create_new_script'])){
135 $this->create_script = TRUE;
136 }
138 /* Close add script dialog, without adding a new one */
139 if(isset($_POST['create_script_cancel'])){
140 $this->create_script = FALSE;
141 }
143 /* Display create script dialog
144 * handle posts, display warnings if specified
145 * name is not useable.
146 * Create a new script with given name
147 */
148 if($this->create_script){
150 /* Set initial name or used posted name if available */
151 $name = "";
152 if(isset($_POST['NewScriptName'])){
153 $name = trim($_POST['NewScriptName']);
154 }
156 /* Check given name */
157 $err = "";
159 /* Is given name in lower case characters ? */
160 if(isset($_POST['create_script_save'])){
161 if(!strlen($name)){
162 $err = _("You should specify a name for your new script.");
163 }
164 /* Is given name in lower case characters ? */
165 if($name != strtolower($name)){
166 $err = _("Only lower case names are allowed here.");
167 }
169 /* Only chars are allowed here */
170 if(preg_match("/[^a-z]/i",$name)){
171 $err = _("Only a-z are allowed in script names.");
172 }
173 }
175 /* Create script if everything is ok */
176 if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
178 /* Close dialog */
179 $this->create_script = FALSE;
181 /* Script contents to use */
182 $script = "/*New script */".
183 "stop;";
185 /* Create a new parser and initialize default values */
186 $p = new My_Parser;
187 $ret = $p->parse($script);
188 $sc['SCRIPT'] = $script;
189 $sc['ORIG_SCRIPT'] = $script;
190 $sc['IS_NEW'] = TRUE;
191 $sc['MSG'] = "";
192 if(!$ret){
193 $sc['STATUS'] = FALSE;
194 $sc['MODE'] = "Source-Only";
195 $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
196 }else{
197 $sc['STATUS'] = TRUE;
198 $sc['MODE'] = "Strucktured";
199 $sc['MSG'] = _("Parse successful");
200 }
201 $sc['PARSER'] = $p;
202 $sc['EDITED'] = TRUE;
203 $sc['ACTIVE'] = FALSE;
204 $sc['NAME'] = $name;
206 /* Add script */
207 $this->scripts[$name] = $sc;
208 }else{
210 /* Display dialog to enter new script name */
211 $smarty = get_smarty();
212 $smarty->assign("NewScriptName",$name);
213 $smarty->assign("Error",$err);
214 return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
215 }
216 }
219 /*************
220 * Handle several posts
221 *************/
223 $once = TRUE;
224 foreach($_POST as $name => $value){
226 /* Edit script requested */
227 if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
228 $script = preg_replace("/^editscript_/","",$name);
229 $script = preg_replace("/_(x|y)/","",$script);
230 $once = FALSE;
232 $this->current_script = $script;
233 $this->current_handler = $this->scripts[$script]['PARSER'];
234 }
236 /* remove script requested */
237 if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
238 $script = preg_replace("/^delscript_/","",$name);
239 $script = preg_replace("/_(x|y)/","",$script);
240 $once = FALSE;
242 $this->script_to_delete = $script;
243 }
245 /* Activate script */
246 if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
247 $script = preg_replace("/^active_script_/","",$name);
248 $script = preg_replace("/_(x|y)/","",$script);
249 $once = FALSE;
251 /* Connect to sieve class and try to get all available sieve scripts */
252 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
253 $this->sieve_handle=
254 new sieve( $cfg["sieve_server"],
255 $cfg["sieve_port"],
256 $this->parent->mail,
257 $cfg["password"],
258 $cfg["admin"]);
260 if (!$this->sieve_handle->sieve_login()){
261 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
262 return;
263 }
265 /* Try to activate the given script and update
266 * class script array.
267 */
268 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
269 print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
270 }else{
271 foreach($this->scripts as $key => $data){
272 if($key == $script){
273 $this->scripts[$key]['ACTIVE'] = TRUE;
274 }else{
275 $this->scripts[$key]['ACTIVE'] = FALSE;
276 }
277 }
278 }
279 }
280 }
283 /*************
284 * Remove script handling
285 *************/
287 /* Remove aborted */
288 if(isset($_POST['delete_cancel'])){
289 $this->script_to_delete = -1;
290 }
292 /* Remove confirmed */
293 if(isset($_POST['delete_script_confirm'])){
295 $script = $this->scripts[$this->script_to_delete];
297 /* Just remove from array if it is a new script */
298 if($script['IS_NEW']){
299 unset($this->scripts[$this->script_to_delete]);
300 }else{
302 /* Connect to sieve class and try to get all available sieve scripts */
303 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
304 $this->sieve_handle=
305 new sieve( $cfg["sieve_server"],
306 $cfg["sieve_port"],
307 $this->parent->mail,
308 $cfg["password"],
309 $cfg["admin"]);
310 if (!$this->sieve_handle->sieve_login()){
311 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
312 return;
313 }
315 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
316 print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
317 }else{
318 unset($this->scripts[$this->script_to_delete]);
319 }
320 }
321 $this->script_to_delete = -1;
322 }
324 /* Display confirm dialog */
325 if($this->script_to_delete != -1){
326 $smarty = get_smarty();
327 $smarty->assign("Warning",
328 sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
329 $this->scripts[$this->script_to_delete]['NAME']));
330 return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
331 }
334 /**************
335 * Save script changes
336 **************/
338 /* Abort saving */
339 if(isset($_POST['cancel_sieve_changes'])){
340 $this->current_handler = NULL;
341 }
343 /* Save currently edited sieve script. */
344 if(isset($_POST['save_sieve_changes'])){
345 $chk = $this->current_handler->check();
346 if(!count($chk)){
348 $sc = $this->scripts[$this->current_script]['SCRIPT'];
349 $p = new My_Parser;
350 if($p -> parse($sc)){
352 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
353 $this->scripts[$this->current_script]['MODE'] = "Source";
354 }
356 $this->scripts[$this->current_script]['PARSER'] = $p;
357 $this->scripts[$this->current_script]['EDITED'] = TRUE;
358 $this->scripts[$this->current_script]['STATUS'] = TRUE;
359 $this->scripts[$this->current_script]['MSG'] = _("Edited");
360 $this->current_handler = NULL;
361 }else{
362 print_red($p->status_text);;
363 }
364 }else{
365 print_red(_("Please fix all errors before saving."));
366 }
367 }
370 /*************
371 * Display edit dialog
372 *************/
374 /* Display edit dialog, depending on Mode display different uis
375 */
376 if($this->current_handler){
378 /****
379 * Add new element to ui
380 ****/
382 /* Abort add dialog */
383 if(isset($_POST['select_new_element_type_cancel'])){
384 $this->add_new_element = FALSE;
385 }
387 /* Add a new element */
388 if($this->add_new_element){
390 $element_types= array(
391 "sieve_keep" => _("Keep"),
392 "sieve_comment" => _("Comment"),
393 "sieve_fileinto" => _("File into"),
394 "sieve_keep" => _("Keep"),
395 "sieve_discard" => _("Discard"),
396 "sieve_redirect" => _("Redirect"),
397 "sieve_reject" => _("Reject"),
398 "sieve_require" => _("Require"),
399 "sieve_stop" => _("Stop"),
400 "sieve_vacation" => _("Vacation message"),
401 "sieve_if" => _("If"));
404 /* Element selected */
405 if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])){
406 $this->add_element_type = $_POST['element_type'];
407 }
409 /* Create new element and add it to
410 * the selected position
411 */
412 if(isset($_POST['select_new_element_type'])){
414 /* Create elements we should add */
415 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()));
416 if($this->add_element_type == "sieve_if"){
417 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()));
418 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()));
419 }
420 $start = $end = array();
421 $found = false;
422 $data = $this->current_handler->tree_->pap;
424 /* Add above current element*/
425 if($this->add_above_below == "above"){
426 foreach($data as $key => $obj){
427 if($obj->object_id == $this->add_new_id){
428 $found = true;
429 }
430 if(!$found){
431 $start[] = $obj;
432 }else{
433 $end[] = $obj;
434 }
435 }
436 }else{
437 /* Add below current element */
438 foreach($data as $key => $obj){
439 if(!$found){
440 $start[] = $obj;
441 }else{
442 $end[] = $obj;
443 }
444 if($obj->object_id == $this->add_new_id){
445 $found = true;
446 }
447 }
448 }
450 /* Only add, if current element could be located */
451 if($found){
452 $new = array();
453 foreach($start as $obj){
454 $new[] = $obj;
455 }
456 foreach($ele as $el){
457 $new[] = $el;
458 }
459 foreach($end as $obj){
460 $new[] = $obj;
461 }
462 $data= $new;
463 $this->current_handler->tree_->pap = $data;
464 $this->add_new_element = FALSE;
465 }else{
466 print_red(_("Something went wrong while adding a new entry."));
467 }
468 }
469 }
471 /* Only display select dialog if it is necessary */
472 if($this->add_new_element){
473 $smarty = get_smarty();
474 $smarty->assign("element_types",$element_types );
475 $smarty->assign("element_type",$this->add_element_type);
476 $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
477 return($str);
478 }
482 /****************
483 * Handle test posts
484 ****************/
486 /* handle some special posts from test elements
487 */
488 foreach($_POST as $name => $value){
489 if(preg_match("/^Add_Test_Object_/",$name)) {
490 $name = preg_replace("/^Add_Test_Object_/","",$name);
491 $name = preg_replace("/_(x|y)$/","",$name);
493 $test_types_to_add = array(
494 "address" =>_("Address"),
495 "header" =>_("Header"),
496 "envelope"=>_("Envelope"),
497 "size" =>_("Size"),
498 "exists" =>_("Exists"),
499 "allof" =>_("All of"),
500 "anyof" =>_("Any of"),
501 "true" =>_("True"),
502 "false" =>_("False"));
504 $smarty = get_smarty();
505 $smarty->assign("ID",$name);
506 $smarty->assign("test_types_to_add",$test_types_to_add);
507 $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
508 return($ret);
509 }
510 }
512 $current = $this->scripts[$this->current_script];
514 /* Create html results */
515 $smarty = get_smarty();
516 $smarty->assign("Mode",$current['MODE']);
517 if($current['MODE'] == "Structured"){
518 $smarty->assign("Contents",$this->current_handler->tree_->execute());
519 }else{
520 $smarty->assign("Contents",$current['SCRIPT']);
521 }
522 $smarty->assign("Script_Error",$this->Script_Error);
523 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
524 return($ret);
525 }
528 /* Create list of available sieve scripts
529 */
530 $List = new divSelectBox("sieveManagement");
531 foreach($this->scripts as $key => $script){
533 $edited = $script['EDITED'];
534 $active = $script['ACTIVE'];
536 $field1 = array("string" => " ",
537 "attach" => "style='width:20px;'");
538 if($active){
539 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
540 "attach" => "style='width:20px;'");
541 }
542 $field2 = array("string" => $script['NAME']);
543 $field3 = array("string" => $script['MSG']);
544 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
546 if($edited){
547 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
548 "attach" => "style='width:30px;'");
549 }else{
550 $field5 = array("string" => "",
551 "attach" => "style='width:30px;'");
552 }
554 if($active){
555 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
556 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
557 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
558 }else{
559 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
560 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
561 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
562 }
563 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
564 }
566 $display ="<h2>Sieve script management</h2>";
567 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
568 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
569 $display .= $List->DrawList();
571 $display .= "<p style=\"text-align:right\">\n";
572 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
573 $display .= " \n";
574 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
575 $display .= "</p>";
576 return($display);;
577 }
579 function save_object()
580 {
581 if($this->current_handler){
583 /* Check if there is an add object requested
584 */
585 $data = $this->current_handler->tree_->pap;
586 $once = TRUE;
587 foreach($_POST as $name => $value){
588 foreach($data as $key => $obj){
589 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
590 $once = FALSE;
591 $this->add_new_element = TRUE;
592 $this->add_new_id = $obj->object_id;
593 $this->add_above_below = "above";
594 }
595 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
596 $once = FALSE;
597 $this->add_new_element = TRUE;
598 $this->add_new_id = $obj->object_id;
599 $this->add_above_below = "below";
600 }
602 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
603 $once = FALSE;
604 $this->current_handler->tree_->remove_object($key);
605 }
606 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
607 $this->current_handler->tree_->move_up_down($key,"up");
608 $once = FALSE;
609 }
610 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
611 $this->current_handler->tree_->move_up_down($key,"down");
612 $once = FALSE;
613 }
614 }
615 }
617 /* Skip Mode changes and Parse tests
618 * if we are currently in a subdialog
619 */
620 if(!$this->add_new_element) {
622 $Mode = $this->scripts[$this->current_script]['MODE'];
623 $skip_mode_change = false;
624 if(in_array($Mode,array("Source-Only","Source"))){
625 if(isset($_POST['script_contents'])){
626 $sc = stripslashes($_POST['script_contents']);
627 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
628 $p = new My_Parser;
629 if($p -> parse($sc)){
630 $this->current_handler = $p;
631 $this->Script_Error = "";
632 } else {
633 $this->Script_Error = $p->status_text;
634 $skip_mode_change = TRUE;
635 }
636 }
637 }
638 if(in_array($Mode,array("Structured"))){
639 $this->current_handler->save_object();
640 $sc = $this->current_handler->get_sieve_script();
641 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
642 $p = new My_Parser;
643 if($p -> parse($sc)){
644 $this->current_handler = $p;
645 $this->Script_Error = "";
646 } else {
647 $this->Script_Error = $p->status_text;
648 $skip_mode_change = TRUE;
649 }
650 }
651 if(!$skip_mode_change){
652 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
653 if(isset($_POST['View_Source'])){
654 $this->scripts[$this->current_script]['MODE'] = "Source";
655 }
656 if(isset($_POST['View_Structured'])){
657 $this->scripts[$this->current_script]['MODE'] = "Structured";
658 }
659 }
660 }
661 }
662 $this->current_handler->save_object();
663 }
664 }
667 function save()
668 {
669 /* Connect to sieve class and try to get all available sieve scripts */
670 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
672 $this->sieve_handle=
673 new sieve( $cfg["sieve_server"],
674 $cfg["sieve_port"],
675 $this->parent->mail,
676 $cfg["password"],
677 $cfg["admin"]);
679 if (!$this->sieve_handle->sieve_login()){
680 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
681 return;
682 }
684 $everything_went_fine = TRUE;
686 foreach($this->scripts as $key => $script){
687 if($script['EDITED']){
688 $data = $this->scripts[$key]['SCRIPT'];
689 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
690 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
691 $everything_went_fine = FALSE;
692 print_red(to_string($this->sieve_handle->error_raw));
693 $this->scripts[$key]['MSG'] = "<font color='red'>".
694 _("Failed to save sieve script").": ".
695 to_string($this->sieve_handle->error_raw).
696 "</font>";
697 }
698 }
699 }
700 return($everything_went_fine);
701 }
702 }
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>