72e93211867c49dc082b637816fc148f0a6c9204
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, somehow nothing was uploaded or the temporary file can't be accessed."));
445 }elseif(!is_readable ($file['tmp_name'])){
446 print_red(sprintf(_("Can't open file '%s' to read uploaded file contents."),$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' alt=' '>";
660 }
662 if($active || $script['IS_NEW'] || chkacl($this->acl,"sieveManagement") != ""){
663 $activate = "<img src='images/empty' 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 }else{
807 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
808 }
810 /* Get index of the element identified by object_id == $id;
811 */
812 $index = -1;
813 $data = $this->current_handler->tree_->pap;
814 foreach($data as $key => $obj){
815 if($obj->object_id == $id && $index==-1){
816 $index = $key;
817 }
818 }
820 /* Tell to user that we couldn't find the given object
821 * so we can't add an element.
822 */
823 if($index == -1 ){
824 trigger_error("Can't add new element, specified \$id=".$id." could not be found in object tree.");
825 return(FALSE);
826 }
828 /* We have found the specified object_id
829 * and want to detect the next free position
830 * to insert the new element.
831 */
832 if($position == "above"){
833 $direction ="up";
834 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
835 }else{
836 $direction = "down";
837 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
838 }
839 /* This is extremly necessary, cause some objects
840 * updates the tree objects ... Somehow i should change this ...
841 */
842 $data = $this->current_handler->tree_->pap;
843 $start = $end = array();
845 if($position == "above"){
846 $start = array_slice($data,0,$next_free);
847 $end = array_slice($data,$next_free);
848 }else{
849 $start = array_slice($data,0,$next_free+1);
850 $end = array_slice($data,$next_free+1);
851 }
853 $new = array();
854 foreach($start as $obj){
855 $new[] = $obj;
856 }
857 foreach($ele as $el){
858 $new[] = $el;
859 }
860 foreach($end as $obj){
861 $new[] = $obj;
862 }
863 $data= $new;
864 $this->current_handler->tree_->pap = $data;
865 return(TRUE);
866 }
870 function save_object()
871 {
872 if($this->current_handler){
874 if(isset($_GET['Add_Object_Top_ID'])){
875 $this->add_new_element = TRUE;
876 $this->add_new_id = $_GET['Add_Object_Top_ID'];
877 $this->add_above_below = "above";
878 }
880 if(isset($_GET['Add_Object_Bottom_ID'])){
881 $this->add_new_element = TRUE;
882 $this->add_new_id = $_GET['Add_Object_Bottom_ID'];
883 $this->add_above_below = "below";
884 }
886 if(isset($_GET['Remove_Object_ID'])){
887 $found_id = -1;
888 foreach($this->current_handler->tree_->pap as $key => $element){
889 if($element->object_id == $_GET['Remove_Object_ID']){
890 $found_id = $key;
891 }
892 }
893 if($found_id != -1 ){
894 $this->current_handler->tree_->remove_object($found_id);
895 }
896 }
898 if(isset($_GET['Move_Up_Object_ID'])){
899 $found_id = -1;
900 foreach($this->current_handler->tree_->pap as $key => $element){
901 if($element->object_id == $_GET['Move_Up_Object_ID']){
902 $found_id = $key;
903 }
904 }
905 if($found_id != -1 ){
906 $this->current_handler->tree_->move_up_down($found_id,"up");
907 }
908 }
910 if(isset($_GET['Move_Down_Object_ID'])){
911 $found_id = -1;
912 foreach($this->current_handler->tree_->pap as $key => $element){
913 if($element->object_id == $_GET['Move_Down_Object_ID']){
914 $found_id = $key;
915 }
916 }
917 if($found_id != -1 ){
918 $this->current_handler->tree_->move_up_down($found_id,"down");
919 }
920 }
923 /* Check if there is an add object requested
924 */
925 $data = $this->current_handler->tree_->pap;
926 $once = TRUE;
927 foreach($_POST as $name => $value){
928 foreach($data as $key => $obj){
929 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
930 $once = FALSE;
931 $this->add_element_type = $_POST['element_type_'.$obj->object_id];
932 $this->add_new_element = FALSE;
933 $this->add_new_id = $obj->object_id;
934 $this->add_above_below = "above";
935 $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
936 }
937 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
938 $once = FALSE;
939 $this->add_element_type = $_POST['element_type_'.$obj->object_id];
940 $this->add_new_element = FALSE;
941 $this->add_new_id = $obj->object_id;
942 $this->add_above_below = "below";
943 $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
944 }
946 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
947 $once = FALSE;
948 $this->current_handler->tree_->remove_object($key);
949 }
950 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
951 $this->current_handler->tree_->move_up_down($key,"up");
952 $once = FALSE;
953 }
954 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
955 $this->current_handler->tree_->move_up_down($key,"down");
956 $once = FALSE;
957 }
958 }
959 }
961 /* Skip Mode changes and Parse tests
962 * if we are currently in a subdialog
963 */
965 $this->current_handler->save_object();
966 $Mode = $this->scripts[$this->current_script]['MODE'];
967 $skip_mode_change = false;
968 if(in_array($Mode,array("Source-Only","Source"))){
969 if(isset($_POST['script_contents'])){
970 $sc = stripslashes($_POST['script_contents']);
971 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
972 $p = new My_Parser($this);
973 if($p -> parse($sc)){
974 $this->Script_Error = "";
975 } else {
976 $this->Script_Error = $p->status_text;
977 $skip_mode_change = TRUE;
978 }
979 }
980 }
981 if(in_array($Mode,array("Structured"))){
982 $sc = $this->current_handler->get_sieve_script();
983 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
984 $p = new My_Parser($this);
985 if($p -> parse($sc)){
986 $this->Script_Error = "";
987 } else {
988 $this->Script_Error = $p->status_text;
989 $skip_mode_change = TRUE;
990 }
991 }
992 if(!$skip_mode_change){
993 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
994 $old_mode = $this->scripts[$this->current_script]['MODE'];
995 if(isset($_POST['View_Source'])){
996 $this->scripts[$this->current_script]['MODE'] = "Source";
997 }
998 if(isset($_POST['View_Structured'])){
999 $this->scripts[$this->current_script]['MODE'] = "Structured";
1000 }
1001 $new_mode = $this->scripts[$this->current_script]['MODE'];
1003 if($old_mode != $new_mode){
1005 $sc = $this->scripts[$this->current_script]['SCRIPT'];
1006 $p = new My_Parser($this);
1008 if($p -> parse($sc)){
1009 $this->current_handler->parse($sc);
1010 $this->Script_Error = "";
1011 } else {
1012 $this->Script_Error = $p->status_text;
1013 }
1014 }
1015 }
1016 }
1017 }
1018 }
1021 function get_used_script_names()
1022 {
1023 $ret = array();
1024 foreach($this->scripts as $script){
1025 $ret[] = $script['NAME'];
1026 }
1027 return($ret);
1028 }
1032 function save()
1033 {
1034 /* Get sieve */
1035 if(!$this->sieve_handle = $this->get_sieve()){
1036 print_red(
1037 sprintf(
1038 _("Can't log into SIEVE server. Server says '%s'."),
1039 to_string($this->Sieve_Error)));
1040 return;
1041 }
1043 $everything_went_fine = TRUE;
1045 foreach($this->scripts as $key => $script){
1046 if($script['EDITED']){
1047 $data = $this->scripts[$key]['SCRIPT'];
1048 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], addcslashes ($data,"\\"))){
1049 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
1050 $everything_went_fine = FALSE;
1051 print_red(to_string($this->sieve_handle->error_raw));
1052 $this->scripts[$key]['MSG'] = "<font color='red'>".
1053 _("Failed to save sieve script").": ".
1054 to_string($this->sieve_handle->error_raw).
1055 "</font>";
1056 }
1057 }
1058 }
1059 return($everything_went_fine);
1060 }
1061 }
1062 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1063 ?>