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 $Sieve_Error = "";
41 var $create_script = FALSE;
43 /* To add new elements we need to know
44 * Where to the element -> add_new_id
45 * Whould we add above or below this id -> add_above_below
46 * What kind of element should we add -> add_element_type
47 */
48 var $add_new_element = FALSE;
49 var $add_new_id = -1;
50 var $add_above_below = "below";
51 var $add_element_type = "sieve_comment";
53 /* If this variable is TRUE, this indicates that we have the
54 * import dialog opened.
55 */
56 var $Import_Script = FALSE;
58 /* Initialize the class and load all sieve scripts
59 * try to parse them and display errors
60 */
61 function sieveManagement($config,$dn,$parent)
62 {
63 $this->parent = $parent;
64 plugin::plugin($config,$dn);
66 /* Get sieve */
67 if(!$this->sieve_handle = $this->get_sieve()){
68 print_red(
69 sprintf(
70 _("Can't log into SIEVE server. Server says '%s'."),
71 to_string($this->Sieve_Error)));
72 return;
73 }
76 /* Get all sieve scripts names */
77 if($this->sieve_handle->sieve_listscripts()){
78 if (is_array($this->sieve_handle->response)){
79 foreach($this->sieve_handle->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($this);
97 $this->sieve_handle->sieve_getscript($script['NAME']);
99 $script = "";
100 foreach($this->sieve_handle->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";
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 = $this->sieve_handle;
122 }
125 /* Return a sieve class hanlde,
126 * false if login fails
127 */
128 function get_sieve()
129 {
130 /* Connect to sieve class and try to get all available sieve scripts */
131 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
132 $this->Sieve_Error = "";
134 /* Log into the mail server */
135 $this->sieve_handle= new sieve(
136 $cfg["sieve_server"],
137 $cfg["sieve_port"],
138 $this->parent->uid,
139 $cfg["password"],
140 $cfg["admin"]);
142 /* Try to login */
143 if (!$this->sieve_handle->sieve_login()){
144 $this->Sieve_Error = $this->sieve_handle->error_raw;
145 return(FALSE);
146 }
147 return($this->sieve_handle);
148 }
151 /* Handle sieve list
152 */
153 function execute()
154 {
155 /***************
156 * Create a new Script
157 ***************/
159 /* Force opening the add script dialog */
160 if(isset($_POST['create_new_script'])){
161 $this->create_script = TRUE;
162 }
164 /* Close add script dialog, without adding a new one */
165 if(isset($_POST['create_script_cancel'])){
166 $this->create_script = FALSE;
167 }
169 /* Display create script dialog
170 * handle posts, display warnings if specified
171 * name is not useable.
172 * Create a new script with given name
173 */
174 if($this->create_script){
176 /* Set initial name or used posted name if available */
177 $name = "";
178 if(isset($_POST['NewScriptName'])){
179 $name = trim($_POST['NewScriptName']);
180 }
182 /* Check given name */
183 $err = "";
185 /* Is given name in lower case characters ? */
186 if(isset($_POST['create_script_save'])){
187 if(!strlen($name)){
188 $err = _("You should specify a name for your new script.");
189 }
190 /* Is given name in lower case characters ? */
191 if($name != strtolower($name)){
192 $err = _("Only lower case names are allowed here.");
193 }
195 /* Only chars are allowed here */
196 if(preg_match("/[^a-z]/i",$name)){
197 $err = _("Only a-z are allowed in script names.");
198 }
200 $tmp = $this->get_used_script_names();
201 if(in_array_ics($name,$tmp)){
202 $err =_("The specified name is already in use.");
203 }
204 }
206 /* Create script if everything is ok */
207 if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
209 /* Close dialog */
210 $this->create_script = FALSE;
212 /* Script contents to use */
213 $script = "/*New script */".
214 "stop;";
216 /* Create a new parser and initialize default values */
217 $p = new My_Parser($this);
218 $ret = $p->parse($script);
219 $sc['SCRIPT'] = $script;
220 $sc['ORIG_SCRIPT'] = $script;
221 $sc['IS_NEW'] = TRUE;
222 $sc['MSG'] = "";
223 if(!$ret){
224 $sc['STATUS'] = FALSE;
225 $sc['MODE'] = "Source-Only";
226 $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
227 }else{
228 $sc['STATUS'] = TRUE;
229 $sc['MODE'] = "Strucktured";
230 $sc['MSG'] = _("Parse successful");
231 }
232 $sc['PARSER'] = $p;
233 $sc['EDITED'] = TRUE;
234 $sc['ACTIVE'] = FALSE;
235 $sc['NAME'] = $name;
237 /* Add script */
238 $this->scripts[$name] = $sc;
239 }else{
241 /* Display dialog to enter new script name */
242 $smarty = get_smarty();
243 $smarty->assign("NewScriptName",$name);
244 $smarty->assign("Error",$err);
245 return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
246 }
247 }
250 /*************
251 * Handle several posts
252 *************/
254 $once = TRUE;
255 foreach($_POST as $name => $value){
257 /* Edit script requested */
258 if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
259 $script = preg_replace("/^editscript_/","",$name);
260 $script = preg_replace("/_(x|y)/","",$script);
261 $once = FALSE;
263 $this->current_script = $script;
264 $this->current_handler = $this->scripts[$script]['PARSER'];
265 }
267 /* remove script requested */
268 if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
269 $script = preg_replace("/^delscript_/","",$name);
270 $script = preg_replace("/_(x|y)/","",$script);
271 $once = FALSE;
273 $this->script_to_delete = $script;
274 }
276 /* Activate script */
277 if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
278 $script = preg_replace("/^active_script_/","",$name);
279 $script = preg_replace("/_(x|y)/","",$script);
280 $once = FALSE;
282 /* We can only activate existing scripts */
283 if(!$this->scripts[$script]['IS_NEW']){
285 /* Get sieve */
286 if(!$this->sieve_handle = $this->get_sieve()){
287 print_red(
288 sprintf(
289 _("Can't log into SIEVE server. Server says '%s'."),
290 to_string($this->Sieve_Error)));
291 }
293 /* Try to activate the given script and update
294 * class script array.
295 */
296 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
297 print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
298 }else{
299 foreach($this->scripts as $key => $data){
300 if($key == $script){
301 $this->scripts[$key]['ACTIVE'] = TRUE;
302 }else{
303 $this->scripts[$key]['ACTIVE'] = FALSE;
304 }
305 }
306 }
307 }
308 }
309 }
312 /*************
313 * Remove script handling
314 *************/
316 /* Remove aborted */
317 if(isset($_POST['delete_cancel'])){
318 $this->script_to_delete = -1;
319 }
321 /* Remove confirmed */
322 if(isset($_POST['delete_script_confirm'])){
324 $script = $this->scripts[$this->script_to_delete];
326 /* Just remove from array if it is a new script */
327 if($script['IS_NEW']){
328 unset($this->scripts[$this->script_to_delete]);
329 }else{
331 /* Get sieve */
332 if(!$this->sieve_handle = $this->get_sieve()){
333 print_red(
334 sprintf(
335 _("Can't log into SIEVE server. Server says '%s'."),
336 to_string($this->Sieve_Error)));
337 }
339 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
340 print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
341 }else{
342 unset($this->scripts[$this->script_to_delete]);
343 }
344 }
345 $this->script_to_delete = -1;
346 }
348 /* Display confirm dialog */
349 if($this->script_to_delete != -1){
350 $smarty = get_smarty();
351 $smarty->assign("Warning",
352 sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
353 $this->scripts[$this->script_to_delete]['NAME']));
354 return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
355 }
358 /**************
359 * Save script changes
360 **************/
362 /* Abort saving */
363 if(isset($_POST['cancel_sieve_changes'])){
364 $this->current_handler = NULL;
365 }
367 /* Save currently edited sieve script. */
368 if(isset($_POST['save_sieve_changes'])){
369 $chk = $this->current_handler->check();
370 if(!count($chk)){
372 $sc = $this->scripts[$this->current_script]['SCRIPT'];
373 $p = new My_Parser($this);
374 if($p -> parse($sc)){
376 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
377 $this->scripts[$this->current_script]['MODE'] = "Source";
378 }
380 $this->scripts[$this->current_script]['PARSER'] = $p;
381 $this->scripts[$this->current_script]['EDITED'] = TRUE;
382 $this->scripts[$this->current_script]['STATUS'] = TRUE;
383 $this->scripts[$this->current_script]['MSG'] = _("Edited");
384 $this->current_handler = NULL;
385 }else{
386 print_red($p->status_text);;
387 }
388 }else{
389 print_red(_("Please fix all errors before saving."));
390 }
391 }
394 /*************
395 * Display edit dialog
396 *************/
398 /* Display edit dialog, depending on Mode display different uis
399 */
400 if($this->current_handler){
402 if(isset($_POST['Import_Script'])){
403 $this->Import_Script = TRUE;
404 }
406 if(isset($_POST['Import_Script_Cancel'])){
407 $this->Import_Script = FALSE;
408 }
410 if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
412 $file = $_FILES['Script_To_Import'];
414 if($file['size'] == 0){
415 print_red(_("Specified file seams to empty."));
416 }elseif(!file_exists($file['tmp_name'])){
417 print_red(_("Upload failed, somehow nothing was uploaded or the temporary file can't be accessed."));
418 }elseif(!is_readable ($file['tmp_name'])){
419 print_red(sprintf(_("Can't open file '%s' to read uploaded file contents."),$file['tmp_name']));
420 }else{
424 $contents = file_get_contents($file['tmp_name']);
426 $this->scripts[$this->current_script]['SCRIPT'] = $contents;
427 if(!$this->current_handler->parse($contents)){
428 $this->scripts[$this->current_script]['MODE'] = "Source";
429 }else{
430 $this->scripts[$this->current_script]['MODE'] = "Structured";
431 }
432 $this->Import_Script = FALSE;
433 }
434 }
436 if($this->Import_Script){
437 $smarty = get_smarty();
438 $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
439 return($str);
440 }
443 /* Create dump of current sieve script */
444 if(isset($_POST['Save_Copy'])){
446 /* force download dialog */
447 header("Content-type: application/tiff\n");
448 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
449 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
450 header('Content-Disposition: filename="dump.script"');
451 } else {
452 header('Content-Disposition: attachment; filename="dump.script"');
453 }
454 header("Content-transfer-encoding: binary\n");
455 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
456 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
457 header("Cache-Control: no-cache");
458 header("Pragma: no-cache");
459 header("Cache-Control: post-check=0, pre-check=0");
460 echo $this->scripts[$this->current_script]['SCRIPT'];
461 exit();
462 }
465 /****
466 * Add new element to ui
467 ****/
469 /* Abort add dialog */
470 if(isset($_POST['select_new_element_type_cancel'])){
471 $this->add_new_element = FALSE;
472 }
474 /* Add a new element */
475 if($this->add_new_element){
477 $element_types= array(
478 "sieve_keep" => _("Keep"),
479 "sieve_comment" => _("Comment"),
480 "sieve_fileinto" => _("File into"),
481 "sieve_keep" => _("Keep"),
482 "sieve_discard" => _("Discard"),
483 "sieve_redirect" => _("Redirect"),
484 "sieve_reject" => _("Reject"),
485 "sieve_require" => _("Require"),
486 "sieve_stop" => _("Stop"),
487 "sieve_vacation" => _("Vacation message"),
488 "sieve_if" => _("If"));
491 /* Element selected */
492 if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])
493 || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
494 $this->add_element_type = $_POST['element_type'];
495 }
497 /* Create new element and add it to
498 * the selected position
499 */
500 if(isset($_POST['select_new_element_type'])){
502 $this->add_new_id;
503 $data = $this->current_handler->tree_->pap;
505 /* Get index of the element identified by object_id == $this->add_new_id; */
506 $index = -1;
507 foreach($data as $key => $obj){
508 if($obj->object_id == $this->add_new_id && $index==-1){
509 $index = $key;
510 }
511 }
513 /* We have found the specified object_id
514 * and want to detect the next free position
515 * to insert the new element.
516 */
517 if($index != -1){
518 if($this->add_above_below == "above"){
519 $direction ="up";
520 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
521 $next_free ++;
522 }else{
523 $direction = "down";
524 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
525 }
526 $this->add_new_id = $this->current_handler->tree_->pap[$next_free]->object_id;
527 }
529 $parent = $this->current_handler->tree_;
531 /* Create elements we should add */
532 if($this->add_element_type == "sieve_if"){
533 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
534 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
535 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
536 }elseif($this->add_element_type == "sieve_else"){
537 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
538 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
539 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
540 }elseif($this->add_element_type == "sieve_elsif"){
541 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
542 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
543 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
544 }else{
545 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
546 }
548 $start = $end = array();
549 $found = false;
551 /* Add above current element*/
552 if($this->add_above_below == "above"){
553 foreach($data as $key => $obj){
554 if($obj->object_id == $this->add_new_id){
555 $found = true;
556 }
557 if(!$found){
558 $start[] = $obj;
559 }else{
560 $end[] = $obj;
561 }
562 }
563 }else{
564 /* Add below current element */
565 foreach($data as $key => $obj){
566 if(!$found){
567 $start[] = $obj;
568 }else{
569 $end[] = $obj;
570 }
571 if($obj->object_id == $this->add_new_id){
572 $found = true;
573 }
574 }
575 }
578 /* Only add, if current element could be located */
579 if($found){
580 $new = array();
581 foreach($start as $obj){
582 $new[] = $obj;
583 }
584 foreach($ele as $el){
585 $new[] = $el;
586 }
587 foreach($end as $obj){
588 $new[] = $obj;
589 }
590 $data= $new;
591 $this->current_handler->tree_->pap = $data;
592 $this->add_new_element = FALSE;
593 }else{
594 print_red(_("Something went wrong while adding a new entry."));
595 }
596 }
597 }
599 /* Only display select dialog if it is necessary */
600 if($this->add_new_element){
601 $smarty = get_smarty();
603 $add_else_elsif = FALSE;
605 /* Check if we should add else/elsif to the select box
606 * or not. We can't add else twice!.
607 */
608 if($this->add_above_below == "below"){
610 /* Get posistion of the current element
611 */
612 foreach($this->current_handler->tree_->pap as $key => $obj){
614 if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
616 /* Get block start/end */
617 $end_id = $this->current_handler->tree_->get_block_end($key);
618 $else_found = FALSE;
619 $elsif_found = FALSE;
621 /* Check if there is already an else in this block
622 */
623 for($i = $key ; $i < $end_id ; $i ++){
624 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
625 $else_found = TRUE;
626 }
627 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
628 $elsif_found = TRUE;
629 }
630 }
632 /* Only allow adding 'else' if there is currently
633 * no 'else' statement. And don't allow adding
634 * 'else' before 'elseif'
635 */
636 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
637 $element_types['sieve_else'] = _("Else");
638 }
639 $element_types['sieve_elsif'] = _("Else if");
640 }
641 }
642 }
644 $smarty->assign("element_types",$element_types );
645 $smarty->assign("element_type",$this->add_element_type);
646 $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
647 return($str);
648 }
652 /****************
653 * Handle test posts
654 ****************/
656 /* handle some special posts from test elements
657 */
658 foreach($_POST as $name => $value){
659 if(preg_match("/^Add_Test_Object_/",$name)) {
660 $name = preg_replace("/^Add_Test_Object_/","",$name);
661 $name = preg_replace("/_(x|y)$/","",$name);
663 $test_types_to_add = array(
664 "address" =>_("Address"),
665 "header" =>_("Header"),
666 "envelope"=>_("Envelope"),
667 "size" =>_("Size"),
668 "exists" =>_("Exists"),
669 "allof" =>_("All of"),
670 "anyof" =>_("Any of"),
671 "true" =>_("True"),
672 "false" =>_("False"));
674 $smarty = get_smarty();
675 $smarty->assign("ID",$name);
676 $smarty->assign("test_types_to_add",$test_types_to_add);
677 $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
678 return($ret);
679 }
680 }
682 $current = $this->scripts[$this->current_script];
684 /* Create html results */
685 $smarty = get_smarty();
686 $smarty->assign("Mode",$current['MODE']);
687 if($current['MODE'] == "Structured"){
688 $smarty->assign("Contents",$this->current_handler->tree_->execute());
689 }else{
690 $smarty->assign("Contents",$current['SCRIPT']);
691 }
692 $smarty->assign("Script_Error",$this->Script_Error);
693 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
694 return($ret);
695 }
698 /* Create list of available sieve scripts
699 */
700 $List = new divSelectBox("sieveManagement");
701 foreach($this->scripts as $key => $script){
703 $edited = $script['EDITED'];
704 $active = $script['ACTIVE'];
706 $field1 = array("string" => " ",
707 "attach" => "style='width:20px;'");
708 if($active){
709 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
710 "attach" => "style='width:20px;'");
711 }
712 $field2 = array("string" => $script['NAME']);
713 $field3 = array("string" => $script['MSG']);
714 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
716 if($edited){
717 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
718 "attach" => "style='width:30px;'");
719 }else{
720 $field5 = array("string" => "",
721 "attach" => "style='width:30px;'");
722 }
724 if($active || $script['IS_NEW']){
725 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
726 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
727 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
728 }else{
729 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
730 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
731 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
732 }
733 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
734 }
736 $display ="<h2>Sieve script management</h2>";
737 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
738 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
739 $display .= $List->DrawList();
741 $display .= "<p style=\"text-align:right\">\n";
742 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
743 $display .= " \n";
744 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
745 $display .= "</p>";
746 return($display);;
747 }
749 function save_object()
750 {
751 if($this->current_handler){
753 /* Check if there is an add object requested
754 */
755 $data = $this->current_handler->tree_->pap;
756 $once = TRUE;
757 foreach($_POST as $name => $value){
758 foreach($data as $key => $obj){
759 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
760 $once = FALSE;
761 $this->add_new_element = TRUE;
762 $this->add_new_id = $obj->object_id;
763 $this->add_above_below = "above";
764 }
765 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
766 $once = FALSE;
767 $this->add_new_element = TRUE;
768 $this->add_new_id = $obj->object_id;
769 $this->add_above_below = "below";
770 }
772 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
773 $once = FALSE;
774 $this->current_handler->tree_->remove_object($key);
775 }
776 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
777 $this->current_handler->tree_->move_up_down($key,"up");
778 $once = FALSE;
779 }
780 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
781 $this->current_handler->tree_->move_up_down($key,"down");
782 $once = FALSE;
783 }
784 }
785 }
787 /* Skip Mode changes and Parse tests
788 * if we are currently in a subdialog
789 */
791 $this->current_handler->save_object();
792 $Mode = $this->scripts[$this->current_script]['MODE'];
793 $skip_mode_change = false;
794 if(in_array($Mode,array("Source-Only","Source"))){
795 if(isset($_POST['script_contents'])){
796 $sc = stripslashes($_POST['script_contents']);
797 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
798 $p = new My_Parser($this);
799 if($p -> parse($sc)){
800 $this->Script_Error = "";
801 } else {
802 $this->Script_Error = $p->status_text;
803 $skip_mode_change = TRUE;
804 }
805 }
806 }
807 if(in_array($Mode,array("Structured"))){
808 $sc = $this->current_handler->get_sieve_script();
809 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
810 $p = new My_Parser($this);
811 if($p -> parse($sc)){
812 $this->Script_Error = "";
813 } else {
814 $this->Script_Error = $p->status_text;
815 $skip_mode_change = TRUE;
816 }
817 }
818 if(!$skip_mode_change){
819 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
820 $old_mode = $this->scripts[$this->current_script]['MODE'];
821 if(isset($_POST['View_Source'])){
822 $this->scripts[$this->current_script]['MODE'] = "Source";
823 }
824 if(isset($_POST['View_Structured'])){
825 $this->scripts[$this->current_script]['MODE'] = "Structured";
826 }
827 $new_mode = $this->scripts[$this->current_script]['MODE'];
829 if($old_mode != $new_mode){
831 $sc = $this->scripts[$this->current_script]['SCRIPT'];
832 $p = new My_Parser($this);
834 if($p -> parse($sc)){
835 $this->current_handler->parse($sc);
836 $this->Script_Error = "";
837 } else {
838 $this->Script_Error = $p->status_text;
839 }
840 }
841 }
842 }
843 }
844 }
847 function get_used_script_names()
848 {
849 $ret = array();
850 foreach($this->scripts as $script){
851 $ret[] = $script['NAME'];
852 }
853 return($ret);
854 }
858 function save()
859 {
860 /* Get sieve */
861 if(!$this->sieve_handle = $this->get_sieve()){
862 print_red(
863 sprintf(
864 _("Can't log into SIEVE server. Server says '%s'."),
865 to_string($this->Sieve_Error)));
866 }
868 $everything_went_fine = TRUE;
870 foreach($this->scripts as $key => $script){
871 if($script['EDITED']){
872 $data = $this->scripts[$key]['SCRIPT'];
873 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
874 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
875 $everything_went_fine = FALSE;
876 print_red(to_string($this->sieve_handle->error_raw));
877 $this->scripts[$key]['MSG'] = "<font color='red'>".
878 _("Failed to save sieve script").": ".
879 to_string($this->sieve_handle->error_raw).
880 "</font>";
881 }
882 }
883 }
884 return($everything_went_fine);
885 }
886 }
887 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
888 ?>