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();
34 var $uattrib = "uid";
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 add 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;
57 var $acl = "";
59 /* Initialize the class and load all sieve scripts
60 * try to parse them and display errors
61 */
62 function sieveManagement($config,$dn,$parent,$uattrib)
63 {
64 /* Use mail Acls here */
65 $ui = get_userinfo();
66 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
67 $this->acl= get_module_permission($acl, "mailAccount", $ui->dn);
69 /* Check given parameter */
70 if(!isset($parent->$uattrib)){
71 trigger_error("Sieve Management implementation error. Parameter 4 must be part of the given parent element.");
72 }
74 $this->uattrib = $uattrib;
75 $this->parent = $parent;
76 plugin::plugin($config,$dn);
78 /* Get sieve, if this fail abort class initialization */
79 if(!$this->sieve_handle = $this->get_sieve()){
80 return;
81 }
84 /* Get all sieve scripts names */
85 if($this->sieve_handle->sieve_listscripts()){
86 if (is_array($this->sieve_handle->response)){
87 foreach($this->sieve_handle->response as $key => $name){
89 $data = array();
90 $data['NAME'] = $name;
92 if($key == "ACTIVE" && $key === "ACTIVE"){
93 $data['ACTIVE'] = TRUE;
94 }else{
95 $data['ACTIVE'] = FALSE;
96 }
97 $this->scripts[] = $data;
98 }
99 }
100 }
102 /* Get script contents */
103 foreach($this->scripts as $key => $script){
104 $p = new My_Parser($this);
105 $this->sieve_handle->sieve_getscript($script['NAME']);
107 $script = "";
108 foreach($this->sieve_handle->response as $line){
109 $script.=$line;
110 }
112 $this->scripts[$key]['IS_NEW'] = FALSE;;
113 $this->scripts[$key]['SCRIPT'] = $script;
114 $this->scripts[$key]['ORIG_SCRIPT'] = $script;
115 $this->scripts[$key]['MSG'] = "";
116 $ret = $p->parse($script);
117 if(!$ret){
118 $this->scripts[$key]['STATUS'] = FALSE;
119 $this->scripts[$key]['MODE'] = "Source";
120 $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
121 }else{
122 $this->scripts[$key]['STATUS'] = TRUE;
123 $this->scripts[$key]['MODE'] = "Structured";
124 $this->scripts[$key]['MSG'] = _("Parse successful");
125 }
126 $this->scripts[$key]['PARSER'] = $p;
127 $this->scripts[$key]['EDITED'] = FALSE;
128 }
129 $this->sieve_handle = $this->sieve_handle;
130 }
133 /* Return a sieve class handle,
134 * false if login fails
135 */
136 function get_sieve()
137 {
139 /* Connect to sieve class and try to get all available sieve scripts */
140 if(isset($this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer])){
141 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
142 $this->Sieve_Error = "";
144 $uattrib = $this->uattrib;
146 /* Log into the mail server */
147 $this->sieve_handle= new sieve(
148 $cfg["sieve_server"],
149 $cfg["sieve_port"],
150 $this->parent->$uattrib,
151 $cfg["password"],
152 $cfg["admin"]);
154 /* Try to login */
155 if (!@$this->sieve_handle->sieve_login()){
156 $this->Sieve_Error = $this->sieve_handle->error_raw;
157 return(FALSE);
158 }
159 return($this->sieve_handle);
160 }else{
161 $this->Sieve_Error = sprintf(_("The specified mail server '%s' does not exist within the GOsa configuration."),
162 $this->parent->gosaMailServer);
163 return(FALSE);
164 }
165 }
168 /* Handle sieve list
169 */
170 function execute()
171 {
172 /***************
173 * Create a new Script
174 ***************/
176 /* Force opening the add script dialog */
177 if(isset($_POST['create_new_script'])){
178 $this->create_script = TRUE;
179 }
181 /* Close add script dialog, without adding a new one */
182 if(isset($_POST['create_script_cancel'])){
183 $this->create_script = FALSE;
184 }
186 /* Display create script dialog
187 * handle posts, display warnings if specified
188 * name is not useable.
189 * Create a new script with given name
190 */
191 if($this->create_script){
193 /* Set initial name or used posted name if available */
194 $name = "";
195 if(isset($_POST['NewScriptName'])){
196 $name = trim($_POST['NewScriptName']);
197 }
199 /* Check given name */
200 $err = false;
202 /* Is given name in lower case characters ? */
203 if(isset($_POST['create_script_save'])){
204 if(!strlen($name)){
205 $err =true;
206 print_red(_("You should specify a name for your new script."));
207 }
208 /* Is given name in lower case characters ? */
209 if($name != strtolower($name)){
210 $err =true;
211 print_red(_("Only lower case names are allowed."));
212 }
214 /* Only chars are allowed here */
215 if(preg_match("/[^a-z]/i",$name)){
216 $err =true;
217 print_red(_("Only alphabetical characters are allowed in script names."));
218 }
220 $tmp = $this->get_used_script_names();
221 if(in_array_ics($name,$tmp)){
222 $err =true;
223 print_red(_("The specified name is already in use."));
224 }
225 }
227 /* Create script if everything is ok */
228 if($this->create_script && isset($_POST['create_script_save']) && !$err){
230 /* Close dialog */
231 $this->create_script = FALSE;
233 /* Script contents to use */
234 $script = "/*New script */".
235 "stop;";
237 /* Create a new parser and initialize default values */
238 $p = new My_Parser($this);
239 $ret = $p->parse($script);
240 $sc['SCRIPT'] = $script;
241 $sc['ORIG_SCRIPT'] = $script;
242 $sc['IS_NEW'] = TRUE;
243 $sc['MSG'] = "";
244 if(!$ret){
245 $sc['STATUS'] = FALSE;
246 $sc['MODE'] = "Source";
247 $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
248 }else{
249 $sc['STATUS'] = TRUE;
250 $sc['MODE'] = "Structured";
251 $sc['MSG'] = _("Parse successful");
252 }
253 $sc['PARSER'] = $p;
254 $sc['EDITED'] = TRUE;
255 $sc['ACTIVE'] = FALSE;
256 $sc['NAME'] = $name;
258 /* Add script */
259 $this->scripts[$name] = $sc;
260 }else{
262 /* Display dialog to enter new script name */
263 $smarty = get_smarty();
264 $smarty->assign("NewScriptName",$name);
265 return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
266 }
267 }
270 /*************
271 * Handle several posts
272 *************/
274 $once = TRUE;
275 foreach($_POST as $name => $value){
277 /* Edit script requested */
278 if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
279 $script = preg_replace("/^editscript_/","",$name);
280 $script = preg_replace("/_(x|y)/","",$script);
281 $once = FALSE;
283 $this->current_script = $script;
284 $this->current_handler = $this->scripts[$script]['PARSER'];
285 $this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
286 }
288 /* remove script requested */
289 if(chkacl($this->acl,"sieveManagement") == "" && preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
290 $script = preg_replace("/^delscript_/","",$name);
291 $script = preg_replace("/_(x|y)/","",$script);
292 $once = FALSE;
293 $this->script_to_delete = $script;
294 }
296 /* Activate script */
297 if(chkacl($this->acl,"sieveManagement") == "" && preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
298 $script = preg_replace("/^active_script_/","",$name);
299 $script = preg_replace("/_(x|y)/","",$script);
300 $once = FALSE;
302 /* We can only activate existing scripts */
303 if(!$this->scripts[$script]['IS_NEW']){
305 /* Get sieve */
306 if(!$this->sieve_handle = $this->get_sieve()){
307 print_red(
308 sprintf(
309 _("Can't log into SIEVE server. Server says '%s'."),
310 to_string($this->Sieve_Error)));
311 }
313 /* Try to activate the given script and update
314 * class script array.
315 */
316 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
317 print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
318 }else{
319 foreach($this->scripts as $key => $data){
320 if($key == $script){
321 $this->scripts[$key]['ACTIVE'] = TRUE;
322 }else{
323 $this->scripts[$key]['ACTIVE'] = FALSE;
324 }
325 }
326 }
327 }
328 }
329 }
332 /*************
333 * Remove script handling
334 *************/
336 /* Remove aborted */
337 if(isset($_POST['delete_cancel'])){
338 $this->script_to_delete = -1;
339 }
341 /* Remove confirmed */
342 if(chkacl($this->acl,"sieveManagement") == "" && isset($_POST['delete_script_confirm'])){
344 $script = $this->scripts[$this->script_to_delete];
346 /* Just remove from array if it is a new script */
347 if($script['IS_NEW']){
348 unset($this->scripts[$this->script_to_delete]);
349 }else{
351 /* Get sieve */
352 if(!$this->sieve_handle = $this->get_sieve()){
353 print_red(
354 sprintf(
355 _("Can't log into SIEVE server. Server says '%s'."),
356 to_string($this->Sieve_Error)));
357 }
359 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
360 print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
361 }else{
362 unset($this->scripts[$this->script_to_delete]);
363 }
364 }
365 $this->script_to_delete = -1;
366 }
368 /* Display confirm dialog */
369 if($this->script_to_delete != -1){
370 $smarty = get_smarty();
371 $smarty->assign("Warning",
372 sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
373 $this->scripts[$this->script_to_delete]['NAME']));
374 return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
375 }
378 /**************
379 * Save script changes
380 **************/
382 /* Abort saving */
383 if(isset($_POST['cancel_sieve_changes'])){
384 $tmp = $this->scripts[$this->current_script]['SCRIPT_BACKUP'];
385 $this->scripts[$this->current_script]['SCRIPT'] = $tmp;
386 $this->scripts[$this->current_script]['PARSER']->parse($tmp);
387 $this->current_handler = NULL;
388 }
390 /* Save currently edited sieve script. */
391 if(chkacl($this->acl,"sieveManagement") == "" &&
392 isset($_POST['save_sieve_changes']) &&
393 is_object($this->current_handler)){
394 $chk = $this->current_handler->check();
395 if(!count($chk)){
397 $sc = $this->scripts[$this->current_script]['SCRIPT'];
398 $p = new My_Parser($this);
399 if($p -> parse($sc)){
401 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
402 $this->scripts[$this->current_script]['MODE'] = "Source";
403 }
405 $this->scripts[$this->current_script]['PARSER'] = $p;
406 $this->scripts[$this->current_script]['EDITED'] = TRUE;
407 $this->scripts[$this->current_script]['STATUS'] = TRUE;
408 $this->scripts[$this->current_script]['MSG'] = _("Edited");
409 $this->current_handler = NULL;
410 }else{
411 print_red($p->status_text);;
412 }
413 }else{
414 foreach($chk as $msgs){
415 print_red(sprintf(_("Please fix all errors before saving. Last error was: %s"),$msgs));
416 }
417 }
418 }
421 /*************
422 * Display edit dialog
423 *************/
425 /* Display edit dialog, depending on Mode display different uis
426 */
427 if($this->current_handler){
429 if(isset($_POST['Import_Script'])){
430 $this->Import_Script = TRUE;
431 }
433 if(isset($_POST['Import_Script_Cancel'])){
434 $this->Import_Script = FALSE;
435 }
437 if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
439 $file = $_FILES['Script_To_Import'];
441 if($file['size'] == 0){
442 print_red(_("Specified file seems to be empty."));
443 }elseif(!file_exists($file['tmp_name'])){
444 print_red(_("Upload failed. The temporary file can't be accessed."));
445 }elseif(!is_readable ($file['tmp_name'])){
446 print_red(sprintf(_("Can't open file '%s'."),$file['tmp_name']));
447 }else{
451 $contents = file_get_contents($file['tmp_name']);
453 $this->scripts[$this->current_script]['SCRIPT'] = $contents;
454 if(!$this->current_handler->parse($contents)){
455 $this->scripts[$this->current_script]['MODE'] = "Source";
456 }else{
457 $this->scripts[$this->current_script]['MODE'] = "Structured";
458 }
459 $this->Script_Error = "";
460 $this->Import_Script = FALSE;
461 }
462 }
464 if($this->Import_Script){
465 $smarty = get_smarty();
466 $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
467 return($str);
468 }
471 /* Create dump of current sieve script */
472 if(isset($_POST['Save_Copy'])){
474 /* force download dialog */
475 header("Content-type: application/tiff\n");
476 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
477 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
478 header('Content-Disposition: filename="dump.script"');
479 } else {
480 header('Content-Disposition: attachment; filename="dump.script"');
481 }
482 header("Content-transfer-encoding: binary\n");
483 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
484 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
485 header("Cache-Control: no-cache");
486 header("Pragma: no-cache");
487 header("Cache-Control: post-check=0, pre-check=0");
488 echo $this->scripts[$this->current_script]['SCRIPT'];
489 exit();
490 }
493 /****
494 * Add new element to ui
495 ****/
497 /* Abort add dialog */
498 if(isset($_POST['select_new_element_type_cancel'])){
499 $this->add_new_element = FALSE;
500 }
502 /* Add a new element */
503 if($this->add_new_element){
505 $element_types= array(
506 "sieve_keep" => _("Keep"),
507 "sieve_comment" => _("Comment"),
508 "sieve_fileinto" => _("File into"),
509 "sieve_keep" => _("Keep"),
510 "sieve_discard" => _("Discard"),
511 "sieve_redirect" => _("Redirect"),
512 "sieve_reject" => _("Reject"),
513 "sieve_require" => _("Require"),
514 "sieve_stop" => _("Stop"),
515 "sieve_vacation" => _("Vacation message"),
516 "sieve_if" => _("If"));
519 /* Element selected */
520 if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])
521 || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
522 $this->add_element_type = $_POST['element_type'];
523 }
525 /* Create new element and add it to
526 * the selected position
527 */
528 if(isset($_POST['select_new_element_type'])){
529 if($this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below)){
530 $this->add_new_element = FALSE;
531 }else{
532 print_red(_("Failed to add new element."));
533 }
534 }
535 }
537 /* Only display select dialog if it is necessary */
538 if($this->add_new_element){
539 $smarty = get_smarty();
541 $add_else_elsif = FALSE;
543 /* Check if we should add else/elsif to the select box
544 * or not. We can't add else twice!.
545 */
546 if($this->add_above_below == "below"){
548 /* Get posistion of the current element
549 */
550 foreach($this->current_handler->tree_->pap as $key => $obj){
552 if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
554 /* Get block start/end */
555 $end_id = $this->current_handler->tree_->get_block_end($key);
556 $else_found = FALSE;
557 $elsif_found = FALSE;
559 /* Check if there is already an else in this block
560 */
561 for($i = $key ; $i < $end_id ; $i ++){
562 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
563 $else_found = TRUE;
564 }
565 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
566 $elsif_found = TRUE;
567 }
568 }
570 /* Only allow adding 'else' if there is currently
571 * no 'else' statement. And don't allow adding
572 * 'else' before 'elseif'
573 */
574 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
575 $element_types['sieve_else'] = _("Else");
576 }
577 $element_types['sieve_elsif'] = _("Else If");
578 }
579 }
580 }
582 $smarty->assign("element_types",$element_types );
583 $smarty->assign("element_type",$this->add_element_type);
584 $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
585 return($str);
586 }
590 /****************
591 * Handle test posts
592 ****************/
594 /* handle some special posts from test elements
595 */
596 foreach($_POST as $name => $value){
597 if(preg_match("/^Add_Test_Object_/",$name)) {
598 $name = preg_replace("/^Add_Test_Object_/","",$name);
599 $name = preg_replace("/_(x|y)$/","",$name);
601 $test_types_to_add = array(
602 "address" =>_("Address"),
603 "header" =>_("Header"),
604 "envelope"=>_("Envelope"),
605 "size" =>_("Size"),
606 "exists" =>_("Exists"),
607 "allof" =>_("All of"),
608 "anyof" =>_("Any of"),
609 "true" =>_("True"),
610 "false" =>_("False"));
612 $smarty = get_smarty();
613 $smarty->assign("ID",$name);
614 $smarty->assign("test_types_to_add",$test_types_to_add);
615 $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
616 return($ret);
617 }
618 }
620 $current = $this->scripts[$this->current_script];
622 /* Create html results */
623 $smarty = get_smarty();
624 $smarty->assign("Mode",$current['MODE']);
625 if($current['MODE'] == "Structured"){
626 $smarty->assign("Contents",$this->current_handler->tree_->execute());
627 }else{
628 $smarty->assign("Contents",$current['SCRIPT']);
629 }
630 $smarty->assign("Script_Error",$this->Script_Error);
631 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
632 return($ret);
633 }
636 /* Create list of available sieve scripts
637 */
638 $List = new divSelectBox("sieveManagement");
639 foreach($this->scripts as $key => $script){
641 $edited = $script['EDITED'];
642 $active = $script['ACTIVE'];
644 $field1 = array("string" => " ",
645 "attach" => "style='width:20px;'");
646 if($active){
647 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'
648 title='"._("This script is marked as active")."'>",
649 "attach" => "style='width:20px;'");
650 }
651 $field2 = array("string" => $script['NAME']);
652 $field3 = array("string" => $script['MSG']);
653 $field4 = array("string" => _("Script length").": ".strlen($script['SCRIPT']));
655 if(chkacl($this->acl,"sieveManagement") == ""){
656 $del = "<input type='image' name='delscript_".$key."' src='images/edittrash.png'
657 title='"._("Remove script")."'>";
658 }else{
659 $del = "<img src='images/empty.png' alt=' '>";
660 }
662 if($active || $script['IS_NEW'] || chkacl($this->acl,"sieveManagement") != ""){
663 $activate = "<img src='images/empty.png' alt=' '>";
664 }else{
665 $activate = "<input type='image' name='active_script_".$key."' src='images/true.png'
666 title='"._("Activate script")."'>";
667 }
669 $field6 = array("string" => $activate."<input type='image' name='editscript_".$key."' src='images/edit.png'
670 title='"._("Edit script")."'>".$del,
671 "attach" => "style='border-right:0px; width:70px;'");
672 $List->AddEntry(array($field1,$field2,$field3,$field4,$field6));
673 }
675 $List->SetHeight(400);
677 /* If the uattrib is empty (Attribute to use for authentification with sieve)
678 * Display a message that the connection can't be established.
679 */
680 $uattrib = $this->uattrib;
681 $smarty = get_smarty();
683 if(!$this->get_sieve()){
684 $smarty->assign("Sieve_Error",sprintf(
685 _("Can't log into SIEVE server. Server says '%s'."),
686 to_string($this->Sieve_Error)));
687 }else{
688 $smarty->assign("Sieve_Error","");
689 }
691 $smarty->assign("uattrib_empty",empty($this->parent->$uattrib));
692 $smarty->assign("List",$List->DrawList());
693 return($smarty->fetch(get_template_path("templates/management.tpl",TRUE,dirname(__FILE__))));
694 }
697 /* Add a new element to the currently opened script editor.
698 * The insert position is specified by
699 */
700 function add_new_element_to_current_script($type,$id,$position)
701 {
702 /* Test given data */
703 if(!in_array_ics($position,array("above","below"))){
704 trigger_error("Can't add new element with \$position=".$position.". Only 'above','below' are allowed here.");
705 return(FALSE);
706 }
707 if(!is_numeric($id)){
708 trigger_error("Can't add new element, given id is not numeric.");
709 return(FALSE);
710 }
711 $tmp = get_declared_classes();
712 if(!in_array($type,$tmp)){
713 if(!empty($type)){
714 trigger_error("Can't add new element, given \$class=".$class." does not exists.");
715 }
716 return(FALSE);
717 }
719 if(!is_object($this->current_handler) || !preg_match("/My_Parser/i",get_class($this->current_handler))){
720 trigger_error("Can't add new element, there is no valid script editor opened.");
721 return(FALSE);
722 }
724 /* These element types are allowed to be added here */
725 $element_types= array(
726 "sieve_keep" => _("Keep"),
727 "sieve_comment" => _("Comment"),
728 "sieve_fileinto" => _("File into"),
729 "sieve_keep" => _("Keep"),
730 "sieve_discard" => _("Discard"),
731 "sieve_redirect" => _("Redirect"),
732 "sieve_reject" => _("Reject"),
733 "sieve_require" => _("Require"),
734 "sieve_stop" => _("Stop"),
735 "sieve_vacation" => _("Vacation message"),
736 "sieve_if" => _("If"));
738 /* Check if we should add else/elsif to the select box
739 * or not. We can't add else twice!.
740 */
742 /* Get posistion of the current element
743 */
744 foreach($this->current_handler->tree_->pap as $key => $obj){
746 if($obj->object_id == $id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
748 /* Get block start/end */
749 $end_id = $this->current_handler->tree_->get_block_end($key);
750 $else_found = FALSE;
751 $elsif_found = FALSE;
753 /* Check if there is already an else in this block
754 */
755 for($i = $key ; $i < $end_id ; $i ++){
756 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
757 $else_found = TRUE;
758 }
759 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
760 $elsif_found = TRUE;
761 }
762 }
764 if($this->add_above_below == "below"){
766 /* Only allow adding 'else' if there is currently
767 * no 'else' statement. And don't allow adding
768 * 'else' before 'elseif'
769 */
770 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
771 $element_types['sieve_else'] = _("Else");
772 }
773 $element_types['sieve_elsif'] = _("Else If");
774 }else{
776 /* Allow adding elsif above elsif */
777 if(in_array(get_class($obj),array("sieve_elsif"))){
778 $element_types['sieve_elsif'] = _("Else If");
779 }
780 }
781 }
782 }
784 if(!isset($element_types[$type])){
785 print_red(sprintf(_("Can't add the specified element at the given position.")));
786 return;
787 }
790 /* Create elements we should add
791 * -Some element require also surrounding block elements
792 */
793 $parent = $this->current_handler->tree_;
794 if($this->add_element_type == "sieve_if"){
795 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
796 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
797 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
798 }elseif($this->add_element_type == "sieve_else"){
799 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
800 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
801 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
802 }elseif($this->add_element_type == "sieve_elsif"){
803 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
804 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
805 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
806 }elseif($this->add_element_type == "sieve_vacation"){
808 /* Automatically add addresses to sieve alternate addresses */
809 $data = NULL;
810 $tmp = new $this->add_element_type($data, preg_replace("/[^0-9]/","",microtime()),$parent);
811 if(isset($this->parent->gosaMailAlternateAddress)){
812 $tmp->addresses = $this->parent->gosaMailAlternateAddress;
813 }
814 $ele[] = $tmp ;
815 }else{
816 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
817 }
819 /* PHP 4 automatic requirement add */
820 if(isset($_SESSION['add_require']) && count($_SESSION['add_require'])){
821 foreach($_SESSION['add_require'] as $reg){
822 $this->current_handler->tree_->add_require($reg,true);
823 }
824 }
826 /* Get index of the element identified by object_id == $id;
827 */
828 $index = -1;
829 $data = $this->current_handler->tree_->pap;
830 foreach($data as $key => $obj){
831 if($obj->object_id == $id && $index==-1){
832 $index = $key;
833 }
834 }
836 /* Tell to user that we couldn't find the given object
837 * so we can't add an element.
838 */
839 if($index == -1 ){
840 trigger_error("Can't add new element, specified \$id=".$id." could not be found in object tree.");
841 return(FALSE);
842 }
844 /* We have found the specified object_id
845 * and want to detect the next free position
846 * to insert the new element.
847 */
848 if($position == "above"){
849 $direction ="up";
850 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
851 }else{
852 $direction = "down";
853 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
854 }
855 /* This is extremly necessary, cause some objects
856 * updates the tree objects ... Somehow i should change this ...
857 */
858 $data = $this->current_handler->tree_->pap;
859 $start = $end = array();
861 if($position == "above"){
862 $start = array_slice($data,0,$next_free);
863 $end = array_slice($data,$next_free);
864 }else{
865 $start = array_slice($data,0,$next_free+1);
866 $end = array_slice($data,$next_free+1);
867 }
869 $new = array();
870 foreach($start as $obj){
871 $new[] = $obj;
872 }
873 foreach($ele as $el){
874 $new[] = $el;
875 }
876 foreach($end as $obj){
877 $new[] = $obj;
878 }
879 $data= $new;
880 $this->current_handler->tree_->pap = $data;
881 return(TRUE);
882 }
886 function save_object()
887 {
888 if($this->current_handler){
890 if(isset($_GET['Add_Object_Top_ID'])){
891 $this->add_new_element = TRUE;
892 $this->add_new_id = $_GET['Add_Object_Top_ID'];
893 $this->add_above_below = "above";
894 }
896 if(isset($_GET['Add_Object_Bottom_ID'])){
897 $this->add_new_element = TRUE;
898 $this->add_new_id = $_GET['Add_Object_Bottom_ID'];
899 $this->add_above_below = "below";
900 }
902 if(isset($_GET['Remove_Object_ID'])){
903 $found_id = -1;
904 foreach($this->current_handler->tree_->pap as $key => $element){
905 if($element->object_id == $_GET['Remove_Object_ID']){
906 $found_id = $key;
907 }
908 }
909 if($found_id != -1 ){
910 $this->current_handler->tree_->remove_object($found_id);
911 }
912 }
914 if(isset($_GET['Move_Up_Object_ID'])){
915 $found_id = -1;
916 foreach($this->current_handler->tree_->pap as $key => $element){
917 if($element->object_id == $_GET['Move_Up_Object_ID']){
918 $found_id = $key;
919 }
920 }
921 if($found_id != -1 ){
922 $this->current_handler->tree_->move_up_down($found_id,"up");
923 }
924 }
926 if(isset($_GET['Move_Down_Object_ID'])){
927 $found_id = -1;
928 foreach($this->current_handler->tree_->pap as $key => $element){
929 if($element->object_id == $_GET['Move_Down_Object_ID']){
930 $found_id = $key;
931 }
932 }
933 if($found_id != -1 ){
934 $this->current_handler->tree_->move_up_down($found_id,"down");
935 }
936 }
939 /* Check if there is an add object requested
940 */
941 $data = $this->current_handler->tree_->pap;
942 $once = TRUE;
943 foreach($_POST as $name => $value){
944 foreach($data as $key => $obj){
945 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
946 $once = FALSE;
947 $this->add_element_type = $_POST['element_type_'.$obj->object_id];
948 $this->add_new_element = FALSE;
949 $this->add_new_id = $obj->object_id;
950 $this->add_above_below = "above";
951 $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
952 }
953 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
954 $once = FALSE;
955 $this->add_element_type = $_POST['element_type_'.$obj->object_id];
956 $this->add_new_element = FALSE;
957 $this->add_new_id = $obj->object_id;
958 $this->add_above_below = "below";
959 $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
960 }
962 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
963 $once = FALSE;
964 $this->current_handler->tree_->remove_object($key);
965 }
966 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
967 $this->current_handler->tree_->move_up_down($key,"up");
968 $once = FALSE;
969 }
970 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
971 $this->current_handler->tree_->move_up_down($key,"down");
972 $once = FALSE;
973 }
974 }
975 }
977 /* Skip Mode changes and Parse tests
978 * if we are currently in a subdialog
979 */
981 $this->current_handler->save_object();
982 $Mode = $this->scripts[$this->current_script]['MODE'];
983 $skip_mode_change = false;
984 if(in_array($Mode,array("Source-Only","Source"))){
985 if(isset($_POST['script_contents'])){
986 $sc = stripslashes($_POST['script_contents']);
987 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
988 $p = new My_Parser($this);
989 if($p -> parse($sc)){
990 $this->Script_Error = "";
991 } else {
992 $this->Script_Error = $p->status_text;
993 $skip_mode_change = TRUE;
994 }
995 }
996 }
997 if(in_array($Mode,array("Structured"))){
998 $sc = $this->current_handler->get_sieve_script();
999 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
1000 $p = new My_Parser($this);
1001 if($p -> parse($sc)){
1002 $this->Script_Error = "";
1003 } else {
1004 $this->Script_Error = $p->status_text;
1005 $skip_mode_change = TRUE;
1006 }
1007 }
1008 if(!$skip_mode_change){
1009 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
1010 $old_mode = $this->scripts[$this->current_script]['MODE'];
1011 if(isset($_POST['View_Source'])){
1012 $this->scripts[$this->current_script]['MODE'] = "Source";
1013 }
1014 if(isset($_POST['View_Structured'])){
1015 $this->scripts[$this->current_script]['MODE'] = "Structured";
1016 }
1017 $new_mode = $this->scripts[$this->current_script]['MODE'];
1019 if($old_mode != $new_mode){
1021 $sc = $this->scripts[$this->current_script]['SCRIPT'];
1022 $p = new My_Parser($this);
1024 if($p -> parse($sc)){
1025 $this->current_handler->parse($sc);
1026 $this->Script_Error = "";
1027 } else {
1028 $this->Script_Error = $p->status_text;
1029 }
1030 }
1031 }
1032 }
1033 }
1034 }
1037 function get_used_script_names()
1038 {
1039 $ret = array();
1040 foreach($this->scripts as $script){
1041 $ret[] = $script['NAME'];
1042 }
1043 return($ret);
1044 }
1048 function save()
1049 {
1050 /* Get sieve */
1051 if(!$this->sieve_handle = $this->get_sieve()){
1052 print_red(
1053 sprintf(
1054 _("Can't log into SIEVE server. Server says '%s'."),
1055 to_string($this->Sieve_Error)));
1056 return;
1057 }
1059 $everything_went_fine = TRUE;
1061 foreach($this->scripts as $key => $script){
1062 if($script['EDITED']){
1063 $data = $this->scripts[$key]['SCRIPT'];
1064 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], addcslashes ($data,"\\"))){
1065 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
1066 $everything_went_fine = FALSE;
1067 print_red(to_string($this->sieve_handle->error_raw));
1068 $this->scripts[$key]['MSG'] = "<font color='red'>".
1069 _("Failed to save sieve script").": ".
1070 to_string($this->sieve_handle->error_raw).
1071 "</font>";
1072 }
1073 }
1074 }
1075 return($everything_went_fine);
1076 }
1077 }
1078 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1079 ?>