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";
226 $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
227 }else{
228 $sc['STATUS'] = TRUE;
229 $sc['MODE'] = "Structured";
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 $this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
266 }
268 /* remove script requested */
269 if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
270 $script = preg_replace("/^delscript_/","",$name);
271 $script = preg_replace("/_(x|y)/","",$script);
272 $once = FALSE;
274 $this->script_to_delete = $script;
275 }
277 /* Activate script */
278 if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
279 $script = preg_replace("/^active_script_/","",$name);
280 $script = preg_replace("/_(x|y)/","",$script);
281 $once = FALSE;
283 /* We can only activate existing scripts */
284 if(!$this->scripts[$script]['IS_NEW']){
286 /* Get sieve */
287 if(!$this->sieve_handle = $this->get_sieve()){
288 print_red(
289 sprintf(
290 _("Can't log into SIEVE server. Server says '%s'."),
291 to_string($this->Sieve_Error)));
292 }
294 /* Try to activate the given script and update
295 * class script array.
296 */
297 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
298 print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
299 }else{
300 foreach($this->scripts as $key => $data){
301 if($key == $script){
302 $this->scripts[$key]['ACTIVE'] = TRUE;
303 }else{
304 $this->scripts[$key]['ACTIVE'] = FALSE;
305 }
306 }
307 }
308 }
309 }
310 }
313 /*************
314 * Remove script handling
315 *************/
317 /* Remove aborted */
318 if(isset($_POST['delete_cancel'])){
319 $this->script_to_delete = -1;
320 }
322 /* Remove confirmed */
323 if(isset($_POST['delete_script_confirm'])){
325 $script = $this->scripts[$this->script_to_delete];
327 /* Just remove from array if it is a new script */
328 if($script['IS_NEW']){
329 unset($this->scripts[$this->script_to_delete]);
330 }else{
332 /* Get sieve */
333 if(!$this->sieve_handle = $this->get_sieve()){
334 print_red(
335 sprintf(
336 _("Can't log into SIEVE server. Server says '%s'."),
337 to_string($this->Sieve_Error)));
338 }
340 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
341 print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
342 }else{
343 unset($this->scripts[$this->script_to_delete]);
344 }
345 }
346 $this->script_to_delete = -1;
347 }
349 /* Display confirm dialog */
350 if($this->script_to_delete != -1){
351 $smarty = get_smarty();
352 $smarty->assign("Warning",
353 sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
354 $this->scripts[$this->script_to_delete]['NAME']));
355 return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
356 }
359 /**************
360 * Save script changes
361 **************/
363 /* Abort saving */
364 if(isset($_POST['cancel_sieve_changes'])){
365 $tmp = $this->scripts[$this->current_script]['SCRIPT_BACKUP'];
366 $this->scripts[$this->current_script]['SCRIPT'] = $tmp;
367 $this->scripts[$this->current_script]['PARSER']->parse($tmp);
368 $this->current_handler = NULL;
369 }
371 /* Save currently edited sieve script. */
372 if(isset($_POST['save_sieve_changes'])){
373 $chk = $this->current_handler->check();
374 if(!count($chk)){
376 $sc = $this->scripts[$this->current_script]['SCRIPT'];
377 $p = new My_Parser($this);
378 if($p -> parse($sc)){
380 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
381 $this->scripts[$this->current_script]['MODE'] = "Source";
382 }
384 $this->scripts[$this->current_script]['PARSER'] = $p;
385 $this->scripts[$this->current_script]['EDITED'] = TRUE;
386 $this->scripts[$this->current_script]['STATUS'] = TRUE;
387 $this->scripts[$this->current_script]['MSG'] = _("Edited");
388 $this->current_handler = NULL;
389 }else{
390 print_red($p->status_text);;
391 }
392 }else{
393 print_red(_("Please fix all errors before saving."));
394 }
395 }
398 /*************
399 * Display edit dialog
400 *************/
402 /* Display edit dialog, depending on Mode display different uis
403 */
404 if($this->current_handler){
406 if(isset($_POST['Import_Script'])){
407 $this->Import_Script = TRUE;
408 }
410 if(isset($_POST['Import_Script_Cancel'])){
411 $this->Import_Script = FALSE;
412 }
414 if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
416 $file = $_FILES['Script_To_Import'];
418 if($file['size'] == 0){
419 print_red(_("Specified file seams to empty."));
420 }elseif(!file_exists($file['tmp_name'])){
421 print_red(_("Upload failed, somehow nothing was uploaded or the temporary file can't be accessed."));
422 }elseif(!is_readable ($file['tmp_name'])){
423 print_red(sprintf(_("Can't open file '%s' to read uploaded file contents."),$file['tmp_name']));
424 }else{
428 $contents = file_get_contents($file['tmp_name']);
430 $this->scripts[$this->current_script]['SCRIPT'] = $contents;
431 if(!$this->current_handler->parse($contents)){
432 $this->scripts[$this->current_script]['MODE'] = "Source";
433 }else{
434 $this->scripts[$this->current_script]['MODE'] = "Structured";
435 }
436 $this->Import_Script = FALSE;
437 }
438 }
440 if($this->Import_Script){
441 $smarty = get_smarty();
442 $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
443 return($str);
444 }
447 /* Create dump of current sieve script */
448 if(isset($_POST['Save_Copy'])){
450 /* force download dialog */
451 header("Content-type: application/tiff\n");
452 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
453 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
454 header('Content-Disposition: filename="dump.script"');
455 } else {
456 header('Content-Disposition: attachment; filename="dump.script"');
457 }
458 header("Content-transfer-encoding: binary\n");
459 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
460 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
461 header("Cache-Control: no-cache");
462 header("Pragma: no-cache");
463 header("Cache-Control: post-check=0, pre-check=0");
464 echo $this->scripts[$this->current_script]['SCRIPT'];
465 exit();
466 }
469 /****
470 * Add new element to ui
471 ****/
473 /* Abort add dialog */
474 if(isset($_POST['select_new_element_type_cancel'])){
475 $this->add_new_element = FALSE;
476 }
478 /* Add a new element */
479 if($this->add_new_element){
481 $element_types= array(
482 "sieve_keep" => _("Keep"),
483 "sieve_comment" => _("Comment"),
484 "sieve_fileinto" => _("File into"),
485 "sieve_keep" => _("Keep"),
486 "sieve_discard" => _("Discard"),
487 "sieve_redirect" => _("Redirect"),
488 "sieve_reject" => _("Reject"),
489 "sieve_require" => _("Require"),
490 "sieve_stop" => _("Stop"),
491 "sieve_vacation" => _("Vacation message"),
492 "sieve_if" => _("If"));
495 /* Element selected */
496 if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])
497 || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
498 $this->add_element_type = $_POST['element_type'];
499 }
501 /* Create new element and add it to
502 * the selected position
503 */
504 if(isset($_POST['select_new_element_type'])){
505 if($this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below)){
506 $this->add_new_element = FALSE;
507 }else{
508 print_red(_("Failed to add new element."));
509 }
510 }
511 }
513 /* Only display select dialog if it is necessary */
514 if($this->add_new_element){
515 $smarty = get_smarty();
517 $add_else_elsif = FALSE;
519 /* Check if we should add else/elsif to the select box
520 * or not. We can't add else twice!.
521 */
522 if($this->add_above_below == "below"){
524 /* Get posistion of the current element
525 */
526 foreach($this->current_handler->tree_->pap as $key => $obj){
528 if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
530 /* Get block start/end */
531 $end_id = $this->current_handler->tree_->get_block_end($key);
532 $else_found = FALSE;
533 $elsif_found = FALSE;
535 /* Check if there is already an else in this block
536 */
537 for($i = $key ; $i < $end_id ; $i ++){
538 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
539 $else_found = TRUE;
540 }
541 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
542 $elsif_found = TRUE;
543 }
544 }
546 /* Only allow adding 'else' if there is currently
547 * no 'else' statement. And don't allow adding
548 * 'else' before 'elseif'
549 */
550 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
551 $element_types['sieve_else'] = _("Else");
552 }
553 $element_types['sieve_elsif'] = _("Else if");
554 }
555 }
556 }
558 $smarty->assign("element_types",$element_types );
559 $smarty->assign("element_type",$this->add_element_type);
560 $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
561 return($str);
562 }
566 /****************
567 * Handle test posts
568 ****************/
570 /* handle some special posts from test elements
571 */
572 foreach($_POST as $name => $value){
573 if(preg_match("/^Add_Test_Object_/",$name)) {
574 $name = preg_replace("/^Add_Test_Object_/","",$name);
575 $name = preg_replace("/_(x|y)$/","",$name);
577 $test_types_to_add = array(
578 "address" =>_("Address"),
579 "header" =>_("Header"),
580 "envelope"=>_("Envelope"),
581 "size" =>_("Size"),
582 "exists" =>_("Exists"),
583 "allof" =>_("All of"),
584 "anyof" =>_("Any of"),
585 "true" =>_("True"),
586 "false" =>_("False"));
588 $smarty = get_smarty();
589 $smarty->assign("ID",$name);
590 $smarty->assign("test_types_to_add",$test_types_to_add);
591 $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
592 return($ret);
593 }
594 }
596 $current = $this->scripts[$this->current_script];
598 /* Create html results */
599 $smarty = get_smarty();
600 $smarty->assign("Mode",$current['MODE']);
601 if($current['MODE'] == "Structured"){
602 $smarty->assign("Contents",$this->current_handler->tree_->execute());
603 }else{
604 $smarty->assign("Contents",$current['SCRIPT']);
605 }
606 $smarty->assign("Script_Error",$this->Script_Error);
607 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
608 return($ret);
609 }
612 /* Create list of available sieve scripts
613 */
614 $List = new divSelectBox("sieveManagement");
615 foreach($this->scripts as $key => $script){
617 $edited = $script['EDITED'];
618 $active = $script['ACTIVE'];
620 $field1 = array("string" => " ",
621 "attach" => "style='width:20px;'");
622 if($active){
623 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
624 "attach" => "style='width:20px;'");
625 }
626 $field2 = array("string" => $script['NAME']);
627 $field3 = array("string" => $script['MSG']);
628 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
630 if($edited){
631 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
632 "attach" => "style='width:30px;'");
633 }else{
634 $field5 = array("string" => "",
635 "attach" => "style='width:30px;'");
636 }
638 if($active || $script['IS_NEW']){
639 $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
640 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
641 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
642 }else{
643 $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
644 "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
645 "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
646 }
647 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
648 }
650 $display ="<h2>Sieve script management</h2>";
651 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
652 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
653 $display .= $List->DrawList();
655 $display .= "<p style=\"text-align:right\">\n";
656 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
657 $display .= " \n";
658 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
659 $display .= "</p>";
660 return($display);;
661 }
664 /* Add a new element to the currently opened script editor.
665 * The insert position is specified by
666 */
667 function add_new_element_to_current_script($type,$id,$position)
668 {
669 /* Test given data */
670 if(!in_array_ics($position,array("above","below"))){
671 trigger_error("Can't add new element with \$position=".$position.". Only 'above','below' are allowed here.");
672 return(FALSE);
673 }
674 if(!is_numeric($id)){
675 trigger_error("Can't add new element, given id is not numeric.");
676 return(FALSE);
677 }
678 $tmp = get_declared_classes();
679 if(!in_array($type,$tmp)){
680 trigger_error("Can't add new element, given \$class=".$class." does not exists.");
681 return(FALSE);
682 }
683 if(!is_object($this->current_handler) || get_class($this->current_handler) != "My_Parser"){
684 trigger_error("Can't add new element, there is no valid script editor opened.");
685 return(FALSE);
686 }
688 /* Get index of the element identified by object_id == $id;
689 */
690 $index = -1;
691 $data = $this->current_handler->tree_->pap;
692 foreach($data as $key => $obj){
693 if($obj->object_id == $id && $index==-1){
694 $index = $key;
695 }
696 }
698 /* Tell to user that we couldn't find the given object
699 * so we can't add an element.
700 */
701 if($index == -1 ){
702 trigger_error("Can't add new element, specified \$id=".$id." could not be found in object tree.");
703 return(FALSE);
704 }
706 /* We have found the specified object_id
707 * and want to detect the next free position
708 * to insert the new element.
709 */
710 if($position == "above"){
711 $direction ="up";
712 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
713 }else{
714 $direction = "down";
715 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
716 }
718 /* Create elements we should add
719 * -Some element require also surrounding block elements
720 */
721 $parent = $this->current_handler->tree_;
722 if($this->add_element_type == "sieve_if"){
723 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
724 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
725 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
726 }elseif($this->add_element_type == "sieve_else"){
727 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
728 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
729 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
730 }elseif($this->add_element_type == "sieve_elsif"){
731 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
732 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
733 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
734 }else{
735 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
736 }
738 /* This is extremly necessary, cause some objects
739 * updates the tree objects ... Somehow i should change this ...
740 */
741 $data = $this->current_handler->tree_->pap;
742 $start = $end = array();
744 if($position == "above"){
745 $start = array_slice($data,0,$next_free);
746 $end = array_slice($data,$next_free);
747 }else{
748 $start = array_slice($data,0,$next_free+1);
749 $end = array_slice($data,$next_free+1);
750 }
752 $new = array();
753 foreach($start as $obj){
754 $new[] = $obj;
755 }
756 foreach($ele as $el){
757 $new[] = $el;
758 }
759 foreach($end as $obj){
760 $new[] = $obj;
761 }
762 $data= $new;
763 $this->current_handler->tree_->pap = $data;
764 return(TRUE);
765 }
769 function save_object()
770 {
771 if($this->current_handler){
773 /* Check if there is an add object requested
774 */
775 $data = $this->current_handler->tree_->pap;
776 $once = TRUE;
777 foreach($_POST as $name => $value){
778 foreach($data as $key => $obj){
779 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
780 $once = FALSE;
781 $this->add_new_element = TRUE;
782 $this->add_new_id = $obj->object_id;
783 $this->add_above_below = "above";
784 }
785 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
786 $once = FALSE;
787 $this->add_new_element = TRUE;
788 $this->add_new_id = $obj->object_id;
789 $this->add_above_below = "below";
790 }
792 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
793 $once = FALSE;
794 $this->current_handler->tree_->remove_object($key);
795 }
796 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
797 $this->current_handler->tree_->move_up_down($key,"up");
798 $once = FALSE;
799 }
800 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
801 $this->current_handler->tree_->move_up_down($key,"down");
802 $once = FALSE;
803 }
804 }
805 }
807 /* Skip Mode changes and Parse tests
808 * if we are currently in a subdialog
809 */
811 $this->current_handler->save_object();
812 $Mode = $this->scripts[$this->current_script]['MODE'];
813 $skip_mode_change = false;
814 if(in_array($Mode,array("Source-Only","Source"))){
815 if(isset($_POST['script_contents'])){
816 $sc = stripslashes($_POST['script_contents']);
817 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
818 $p = new My_Parser($this);
819 if($p -> parse($sc)){
820 $this->Script_Error = "";
821 } else {
822 $this->Script_Error = $p->status_text;
823 $skip_mode_change = TRUE;
824 }
825 }
826 }
827 if(in_array($Mode,array("Structured"))){
828 $sc = $this->current_handler->get_sieve_script();
829 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
830 $p = new My_Parser($this);
831 if($p -> parse($sc)){
832 $this->Script_Error = "";
833 } else {
834 $this->Script_Error = $p->status_text;
835 $skip_mode_change = TRUE;
836 }
837 }
838 if(!$skip_mode_change){
839 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
840 $old_mode = $this->scripts[$this->current_script]['MODE'];
841 if(isset($_POST['View_Source'])){
842 $this->scripts[$this->current_script]['MODE'] = "Source";
843 }
844 if(isset($_POST['View_Structured'])){
845 $this->scripts[$this->current_script]['MODE'] = "Structured";
846 }
847 $new_mode = $this->scripts[$this->current_script]['MODE'];
849 if($old_mode != $new_mode){
851 $sc = $this->scripts[$this->current_script]['SCRIPT'];
852 $p = new My_Parser($this);
854 if($p -> parse($sc)){
855 $this->current_handler->parse($sc);
856 $this->Script_Error = "";
857 } else {
858 $this->Script_Error = $p->status_text;
859 }
860 }
861 }
862 }
863 }
864 }
867 function get_used_script_names()
868 {
869 $ret = array();
870 foreach($this->scripts as $script){
871 $ret[] = $script['NAME'];
872 }
873 return($ret);
874 }
878 function save()
879 {
880 /* Get sieve */
881 if(!$this->sieve_handle = $this->get_sieve()){
882 print_red(
883 sprintf(
884 _("Can't log into SIEVE server. Server says '%s'."),
885 to_string($this->Sieve_Error)));
886 }
888 $everything_went_fine = TRUE;
890 foreach($this->scripts as $key => $script){
891 if($script['EDITED']){
892 $data = $this->scripts[$key]['SCRIPT'];
893 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
894 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
895 $everything_went_fine = FALSE;
896 print_red(to_string($this->sieve_handle->error_raw));
897 $this->scripts[$key]['MSG'] = "<font color='red'>".
898 _("Failed to save sieve script").": ".
899 to_string($this->sieve_handle->error_raw).
900 "</font>";
901 }
902 }
903 }
904 return($everything_went_fine);
905 }
906 }
907 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
908 ?>