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;
58 /* Initialize the class and load all sieve scripts
59 * try to parse them and display errors
60 */
61 function sieveManagement(&$config,$dn,&$parent,$uattrib)
62 {
63 /* Check given parameter */
64 if(!isset($parent->$uattrib)){
65 trigger_error("Sieve Management implementation error. Parameter 4 (".$uattrib.") must be part of the given parent element (".get_class($parent).").");
66 }
68 $this->uattrib = $uattrib;
69 $this->parent = &$parent;
70 plugin::plugin($config,$dn);
72 /* Get sieve, if this fail abort class initialization */
73 if(!$this->sieve_handle = $this->get_sieve()){
74 return;
75 }
78 /* Get all sieve scripts names */
79 if($this->sieve_handle->sieve_listscripts()){
80 if (is_array($this->sieve_handle->response)){
81 foreach($this->sieve_handle->response as $key => $name){
83 $data = array();
84 $data['NAME'] = $name;
86 if($key == "ACTIVE" && $key === "ACTIVE"){
87 $data['ACTIVE'] = TRUE;
88 }else{
89 $data['ACTIVE'] = FALSE;
90 }
91 $this->scripts[] = $data;
92 }
93 }
94 }
96 /* Get script contents */
97 foreach($this->scripts as $key => $script){
98 $p = new My_Parser($this);
99 $this->sieve_handle->sieve_getscript($script['NAME']);
101 $script = "";
102 foreach($this->sieve_handle->response as $line){
103 $script.=$line;
104 }
106 $this->scripts[$key]['IS_NEW'] = FALSE;;
107 $this->scripts[$key]['SCRIPT'] = $script;
108 $this->scripts[$key]['ORIG_SCRIPT'] = $script;
109 $this->scripts[$key]['MSG'] = "";
110 $ret = $p->parse($script);
111 if(!$ret){
112 $this->scripts[$key]['STATUS'] = FALSE;
113 $this->scripts[$key]['MODE'] = "Source";
114 $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
115 }else{
116 $this->scripts[$key]['STATUS'] = TRUE;
117 $this->scripts[$key]['MODE'] = "Structured";
118 $this->scripts[$key]['MSG'] = _("Parse successful");
119 }
120 $this->scripts[$key]['PARSER'] = $p;
121 $this->scripts[$key]['EDITED'] = FALSE;
122 }
123 $this->sieve_handle = $this->sieve_handle;
124 }
127 /* Return a sieve class handle,
128 * false if login fails
129 */
130 function get_sieve()
131 {
133 /* Connect to sieve class and try to get all available sieve scripts */
134 if(isset($this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer])){
135 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
136 $this->Sieve_Error = "";
138 $uattrib = $this->uattrib;
140 /* Log into the mail server */
141 $this->sieve_handle= new sieve(
142 $cfg["sieve_server"],
143 $cfg["sieve_port"],
144 $this->parent->$uattrib,
145 $cfg["password"],
146 $cfg["admin"],
147 $cfg["sieve_option"]);
149 /* Try to login */
150 if (!@$this->sieve_handle->sieve_login()){
151 $this->Sieve_Error = $this->sieve_handle->error_raw;
152 return(FALSE);
153 }
154 return($this->sieve_handle);
155 }else{
156 $this->Sieve_Error = sprintf(_("The specified mail server '%s' does not exist within the GOsa configuration."),
157 $this->parent->gosaMailServer);
158 return(FALSE);
159 }
160 }
163 /* Handle sieve list
164 */
165 function execute()
166 {
167 /***************
168 * Create a new Script
169 ***************/
171 /* Force opening the add script dialog */
172 if(isset($_POST['create_new_script'])){
173 $this->create_script = TRUE;
174 }
176 /* Close add script dialog, without adding a new one */
177 if(isset($_POST['create_script_cancel'])){
178 $this->create_script = FALSE;
179 }
181 /* Display create script dialog
182 * handle posts, display warnings if specified
183 * name is not useable.
184 * Create a new script with given name
185 */
186 if($this->create_script){
188 /* Set initial name or used posted name if available */
189 $name = "";
190 if(isset($_POST['NewScriptName'])){
191 $name = trim($_POST['NewScriptName']);
192 }
194 /* Check given name */
195 $err = false;
197 /* Is given name in lower case characters ? */
198 if(isset($_POST['create_script_save'])){
199 if(!strlen($name)){
200 $err =true;
201 msg_dialog::display(_("Error"), _("No script name specified!"), ERROR_DIALOG);
202 }
203 /* Is given name in lower case characters ? */
204 if($name != strtolower($name)){
205 $err =true;
206 msg_dialog::display(_("Error"), _("Please use only lowercase script names!"), ERROR_DIALOG);
207 }
209 /* Only chars are allowed here */
210 if(preg_match("/[^a-z]/i",$name)){
211 $err =true;
212 msg_dialog::display(_("Error"), _("Please use only alphabetical characters in script names!"), ERROR_DIALOG);
213 }
215 $tmp = $this->get_used_script_names();
216 if(in_array_ics($name,$tmp)){
217 $err =true;
218 msg_dialog::display(_("Error"), _("Script name already in use!"), ERROR_DIALOG);
219 }
220 }
222 /* Create script if everything is ok */
223 if($this->create_script && isset($_POST['create_script_save']) && !$err){
225 /* Close dialog */
226 $this->create_script = FALSE;
228 /* Script contents to use */
229 $script = "/*New script */".
230 "stop;";
232 /* Create a new parser and initialize default values */
233 $p = new My_Parser($this);
234 $ret = $p->parse($script);
235 $sc['SCRIPT'] = $script;
236 $sc['ORIG_SCRIPT'] = $script;
237 $sc['IS_NEW'] = TRUE;
238 $sc['MSG'] = "";
239 if(!$ret){
240 $sc['STATUS'] = FALSE;
241 $sc['MODE'] = "Source";
242 $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
243 }else{
244 $sc['STATUS'] = TRUE;
245 $sc['MODE'] = "Structured";
246 $sc['MSG'] = _("Parse successful");
247 }
248 $sc['PARSER'] = $p;
249 $sc['EDITED'] = TRUE;
250 $sc['ACTIVE'] = FALSE;
251 $sc['NAME'] = $name;
253 /* Add script */
254 $this->scripts[$name] = $sc;
255 }else{
257 /* Display dialog to enter new script name */
258 $smarty = get_smarty();
259 $smarty->assign("NewScriptName",$name);
260 return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
261 }
262 }
265 /*************
266 * Handle several posts
267 *************/
269 $once = TRUE;
270 foreach($_POST as $name => $value){
272 /* Edit script requested */
273 if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
274 $script = preg_replace("/^editscript_/","",$name);
275 $script = preg_replace("/_(x|y)/","",$script);
276 $once = FALSE;
278 $this->current_script = $script;
279 $this->current_handler = $this->scripts[$script]['PARSER'];
280 $this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
281 }
283 /* remove script requested */
284 if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
285 $script = preg_replace("/^delscript_/","",$name);
286 $script = preg_replace("/_(x|y)/","",$script);
287 $once = FALSE;
288 $this->script_to_delete = $script;
289 }
291 /* Activate script */
292 if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
293 $script = preg_replace("/^active_script_/","",$name);
294 $script = preg_replace("/_(x|y)/","",$script);
295 $once = FALSE;
297 /* We can only activate existing scripts */
298 if(!$this->scripts[$script]['IS_NEW']){
300 /* Get sieve */
301 if(!$this->sieve_handle = $this->get_sieve()){
302 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($this->Sieve_Error).'</i>'), ERROR_DIALOG);
303 }
305 /* Try to activate the given script and update
306 * class script array.
307 */
308 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
309 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '<br><br><i>'.to_string($this->sieve_handler->error_raw).'</i>'), ERROR_DIALOG);
310 }else{
311 foreach($this->scripts as $key => $data){
312 if($key == $script){
313 $this->scripts[$key]['ACTIVE'] = TRUE;
314 }else{
315 $this->scripts[$key]['ACTIVE'] = FALSE;
316 }
317 }
318 }
319 }
320 }
321 }
324 /*************
325 * Remove script handling
326 *************/
328 /* Remove aborted */
329 if(isset($_POST['delete_cancel'])){
330 $this->script_to_delete = -1;
331 }
333 /* Remove confirmed */
334 if($this->parent->acl_is_writeable("sieveManagement") && isset($_POST['delete_script_confirm'])){
336 $script = $this->scripts[$this->script_to_delete];
338 /* Just remove from array if it is a new script */
339 if($script['IS_NEW']){
340 unset($this->scripts[$this->script_to_delete]);
341 }else{
343 /* Get sieve */
344 if(!$this->sieve_handle = $this->get_sieve()){
345 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($this->Sieve_Error).'</i>'), ERROR_DIALOG);
346 }
348 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
349 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot remove SIEVE script: %s"), '<br><br><i>'.to_string($this->sieve_handler->error_raw).'</i>'), ERROR_DIALOG);
350 }else{
351 unset($this->scripts[$this->script_to_delete]);
352 }
353 }
354 $this->script_to_delete = -1;
355 }
357 /* Display confirm dialog */
358 if($this->script_to_delete != -1){
359 $smarty = get_smarty();
360 $smarty->assign("Warning",msgPool::deleteInfo($this->scripts[$this->script_to_delete]['NAME']));
361 return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
362 }
365 /**************
366 * Save script changes
367 **************/
369 /* Abort saving */
370 if(isset($_POST['cancel_sieve_changes'])){
371 $tmp = $this->scripts[$this->current_script]['SCRIPT_BACKUP'];
372 $this->scripts[$this->current_script]['SCRIPT'] = $tmp;
373 $this->scripts[$this->current_script]['PARSER']->parse($tmp);
374 $this->current_handler = NULL;
375 }
377 /* Save currently edited sieve script. */
378 if($this->parent->acl_is_writeable("sieveManagement") &&
379 isset($_POST['save_sieve_changes']) &&
380 is_object($this->current_handler)){
381 $chk = $this->current_handler->check();
382 if(!count($chk)){
384 $sc = $this->scripts[$this->current_script]['SCRIPT'];
385 $p = new My_Parser($this);
386 if($p -> parse($sc)){
388 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
389 $this->scripts[$this->current_script]['MODE'] = "Source";
390 }
392 $this->scripts[$this->current_script]['PARSER'] = $p;
393 $this->scripts[$this->current_script]['EDITED'] = TRUE;
394 $this->scripts[$this->current_script]['STATUS'] = TRUE;
395 $this->scripts[$this->current_script]['MSG'] = _("Edited");
396 $this->current_handler = NULL;
397 }else{
398 msg_dialog::display(_("SIEVE error"), $p->status_text, ERROR_DIALOG);
399 }
400 }else{
401 foreach($chk as $msgs){
402 msg_dialog::display(_("SIEVE error"), $msgs, ERROR_DIALOG);
403 }
404 }
405 }
408 /*************
409 * Display edit dialog
410 *************/
412 /* Display edit dialog, depending on Mode display different uis
413 */
414 if($this->current_handler){
416 if(isset($_POST['Import_Script'])){
417 $this->Import_Script = TRUE;
418 }
420 if(isset($_POST['Import_Script_Cancel'])){
421 $this->Import_Script = FALSE;
422 }
424 if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
426 $file = $_FILES['Script_To_Import'];
428 if($file['size'] == 0){
429 msg_dialog::display(_("Error"), _("Uploaded script is empty!"), ERROR_DIALOG);
430 }elseif(!file_exists($file['tmp_name'])){
431 msg_dialog::display(_("Internal error"), sprintf(_("Cannot access temporary file '%s'!"), $file['tmp_name']), ERROR_DIALOG);
432 }elseif(!is_readable ($file['tmp_name'])){
433 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot open temporary file '%s'!"), $file['tmp_name']), ERROR_DIALOG);
434 }else{
438 $contents = file_get_contents($file['tmp_name']);
440 $this->scripts[$this->current_script]['SCRIPT'] = $contents;
441 if(!$this->current_handler->parse($contents)){
442 $this->scripts[$this->current_script]['MODE'] = "Source";
443 }else{
444 $this->scripts[$this->current_script]['MODE'] = "Structured";
445 }
446 $this->Script_Error = "";
447 $this->Import_Script = FALSE;
448 }
449 }
451 if($this->Import_Script){
452 $smarty = get_smarty();
453 $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
454 return($str);
455 }
458 /* Create dump of current sieve script */
459 if(isset($_POST['Save_Copy'])){
461 /* force download dialog */
462 header("Content-type: application/tiff\n");
463 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
464 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
465 header('Content-Disposition: filename="dump.script"');
466 } else {
467 header('Content-Disposition: attachment; filename="dump.script"');
468 }
469 header("Content-transfer-encoding: binary\n");
470 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
471 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
472 header("Cache-Control: no-cache");
473 header("Pragma: no-cache");
474 header("Cache-Control: post-check=0, pre-check=0");
475 echo $this->scripts[$this->current_script]['SCRIPT'];
476 exit();
477 }
480 /****
481 * Add new element to ui
482 ****/
484 /* Abort add dialog */
485 if(isset($_POST['select_new_element_type_cancel'])){
486 $this->add_new_element = FALSE;
487 }
489 /* Add a new element */
490 if($this->add_new_element){
492 $element_types= array(
493 "sieve_keep" => _("Keep"),
494 "sieve_comment" => _("Comment"),
495 "sieve_fileinto" => _("File into"),
496 "sieve_keep" => _("Keep"),
497 "sieve_discard" => _("Discard"),
498 "sieve_redirect" => _("Redirect"),
499 "sieve_reject" => _("Reject"),
500 "sieve_require" => _("Require"),
501 "sieve_stop" => _("Stop"),
502 "sieve_vacation" => _("Vacation message"),
503 "sieve_if" => _("If"));
506 /* Element selected */
507 if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])
508 || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
509 $this->add_element_type = $_POST['element_type'];
510 }
512 /* Create new element and add it to
513 * the selected position
514 */
515 if(isset($_POST['select_new_element_type'])){
516 if($this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below)){
517 $this->add_new_element = FALSE;
518 }else{
519 msg_dialog::display(_("SIEVE error"), _("Cannot add new element!") , ERROR_DIALOG);
520 }
521 }
522 }
524 /* Only display select dialog if it is necessary */
525 if($this->add_new_element){
526 $smarty = get_smarty();
528 $add_else_elsif = FALSE;
530 /* Check if we should add else/elsif to the select box
531 * or not. We can't add else twice!.
532 */
533 if($this->add_above_below == "below"){
535 /* Get posistion of the current element
536 */
537 foreach($this->current_handler->tree_->pap as $key => $obj){
539 if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
541 /* Get block start/end */
542 $end_id = $this->current_handler->tree_->get_block_end($key);
543 $else_found = FALSE;
544 $elsif_found = FALSE;
546 /* Check if there is already an else in this block
547 */
548 for($i = $key ; $i < $end_id ; $i ++){
549 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
550 $else_found = TRUE;
551 }
552 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
553 $elsif_found = TRUE;
554 }
555 }
557 /* Only allow adding 'else' if there is currently
558 * no 'else' statement. And don't allow adding
559 * 'else' before 'elseif'
560 */
561 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
562 $element_types['sieve_else'] = _("Else");
563 }
564 $element_types['sieve_elsif'] = _("Else If");
565 }
566 }
567 }
569 $smarty->assign("element_types",$element_types );
570 $smarty->assign("element_type",$this->add_element_type);
571 $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
572 return($str);
573 }
577 /****************
578 * Handle test posts
579 ****************/
581 /* handle some special posts from test elements
582 */
583 foreach($_POST as $name => $value){
584 if(preg_match("/^Add_Test_Object_/",$name)) {
585 $name = preg_replace("/^Add_Test_Object_/","",$name);
586 $name = preg_replace("/_(x|y)$/","",$name);
588 $test_types_to_add = array(
589 "address" =>_("Address"),
590 "header" =>_("Header"),
591 "envelope"=>_("Envelope"),
592 "size" =>_("Size"),
593 "exists" =>_("Exists"),
594 "allof" =>_("All of"),
595 "anyof" =>_("Any of"),
596 "true" =>_("True"),
597 "false" =>_("False"));
599 $smarty = get_smarty();
600 $smarty->assign("ID",$name);
601 $smarty->assign("test_types_to_add",$test_types_to_add);
602 $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
603 return($ret);
604 }
605 }
607 $current = $this->scripts[$this->current_script];
609 /* Create html results */
610 $smarty = get_smarty();
611 $smarty->assign("Mode",$current['MODE']);
612 if($current['MODE'] == "Structured"){
613 $smarty->assign("Contents",$this->current_handler->tree_->execute());
614 }else{
615 $smarty->assign("Contents",$current['SCRIPT']);
616 }
617 $smarty->assign("Script_Error",$this->Script_Error);
618 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
619 return($ret);
620 }
623 /* Create list of available sieve scripts
624 */
625 $List = new divSelectBox("sieveManagement");
626 foreach($this->scripts as $key => $script){
628 $edited = $script['EDITED'];
629 $active = $script['ACTIVE'];
631 $field1 = array("string" => " ",
632 "attach" => "style='width:20px;'");
633 if($active){
634 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'
635 title='"._("This script is marked as active")."'>",
636 "attach" => "style='width:20px;'");
637 }
638 $field2 = array("string" => $script['NAME']);
639 $field3 = array("string" => $script['MSG']);
640 $field4 = array("string" => _("Script length").": ".strlen($script['SCRIPT']));
642 if($this->parent->acl_is_writeable("sieveManagement")){
643 $del = "<input type='image' name='delscript_".$key."' src='images/lists/trash.png'
644 title='"._("Remove script")."'>";
645 }else{
646 $del = "<img src='images/empty.png' alt=' '>";
647 }
649 if($active || $script['IS_NEW'] || !$this->parent->acl_is_writeable("sieveManagement")){
650 $activate = "<img src='images/empty.png' alt=' '>";
651 }else{
652 $activate = "<input type='image' name='active_script_".$key."' src='images/true.png'
653 title='"._("Activate script")."'>";
654 }
656 $field6 = array("string" => $activate."<input type='image' name='editscript_".$key."' src='images/lists/edit.png'
657 title='"._("Edit script")."'>".$del,
658 "attach" => "style='border-right:0px; width:70px;'");
659 $List->AddEntry(array($field1,$field2,$field3,$field4,$field6));
660 }
662 $List->SetHeight(400);
664 /* If the uattrib is empty (Attribute to use for authentification with sieve)
665 * Display a message that the connection can't be established.
666 */
667 $uattrib = $this->uattrib;
668 $smarty = get_smarty();
670 if(!$this->get_sieve()){
671 $smarty->assign("Sieve_Error",sprintf(
672 _("Can't log into SIEVE server. Server says '%s'."),
673 to_string($this->Sieve_Error)));
674 }else{
675 $smarty->assign("Sieve_Error","");
676 }
678 $smarty->assign("uattrib_empty",empty($this->parent->$uattrib));
679 $smarty->assign("List",$List->DrawList());
680 return($smarty->fetch(get_template_path("templates/management.tpl",TRUE,dirname(__FILE__))));
681 }
684 /* Add a new element to the currently opened script editor.
685 * The insert position is specified by
686 */
687 function add_new_element_to_current_script($type,$id,$position)
688 {
689 /* Test given data */
690 if(!in_array_ics($position,array("above","below"))){
691 trigger_error("Can't add new element with \$position=".$position.". Only 'above','below' are allowed here.");
692 return(FALSE);
693 }
694 if(!is_numeric($id)){
695 trigger_error("Can't add new element, given id is not numeric.");
696 return(FALSE);
697 }
698 if(!class_available($type)){
699 if(!empty($type)){
700 trigger_error("Can't add new element, given \$class=".$class." does not exists.");
701 }
702 return(FALSE);
703 }
704 if(!is_object($this->current_handler) || get_class($this->current_handler) != "My_Parser"){
705 trigger_error("Can't add new element, there is no valid script editor opened.");
706 return(FALSE);
707 }
709 /* These element types are allowed to be added here */
710 $element_types= array(
711 "sieve_keep" => _("Keep"),
712 "sieve_comment" => _("Comment"),
713 "sieve_fileinto" => _("File into"),
714 "sieve_keep" => _("Keep"),
715 "sieve_discard" => _("Discard"),
716 "sieve_redirect" => _("Redirect"),
717 "sieve_reject" => _("Reject"),
718 "sieve_require" => _("Require"),
719 "sieve_stop" => _("Stop"),
720 "sieve_vacation" => _("Vacation message"),
721 "sieve_if" => _("If"));
723 /* Check if we should add else/elsif to the select box
724 * or not. We can't add else twice!.
725 */
727 /* Get posistion of the current element
728 */
729 foreach($this->current_handler->tree_->pap as $key => $obj){
731 if($obj->object_id == $id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
733 /* Get block start/end */
734 $end_id = $this->current_handler->tree_->get_block_end($key);
735 $else_found = FALSE;
736 $elsif_found = FALSE;
738 /* Check if there is already an else in this block
739 */
740 for($i = $key ; $i < $end_id ; $i ++){
741 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
742 $else_found = TRUE;
743 }
744 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
745 $elsif_found = TRUE;
746 }
747 }
749 if($this->add_above_below == "below"){
751 /* Only allow adding 'else' if there is currently
752 * no 'else' statement. And don't allow adding
753 * 'else' before 'elseif'
754 */
755 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
756 $element_types['sieve_else'] = _("Else");
757 }
758 $element_types['sieve_elsif'] = _("Else If");
759 }else{
761 /* Allow adding elsif above elsif */
762 if(in_array(get_class($obj),array("sieve_elsif"))){
763 $element_types['sieve_elsif'] = _("Else If");
764 }
765 }
766 }
767 }
769 if(!isset($element_types[$type])){
770 msg_dialog::display(_("SIEVE error"), _("Cannot insert element at the requested position!") , ERROR_DIALOG);
771 return;
772 }
775 /* Create elements we should add
776 * -Some element require also surrounding block elements
777 */
778 $parent = $this->current_handler->tree_;
779 if($this->add_element_type == "sieve_if"){
780 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
781 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
782 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
783 }elseif($this->add_element_type == "sieve_else"){
784 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
785 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
786 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
787 }elseif($this->add_element_type == "sieve_elsif"){
788 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
789 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
790 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
791 }elseif($this->add_element_type == "sieve_vacation"){
793 /* Automatically add addresses to sieve alternate addresses */
794 $data = NULL;
795 $tmp = new $this->add_element_type($data, preg_replace("/[^0-9]/","",microtime()),$parent);
796 if(isset($this->parent->gosaMailAlternateAddress)){
797 $tmp->addresses = $this->parent->gosaMailAlternateAddress;
798 }
799 $ele[] = $tmp ;
800 }else{
801 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
802 }
804 /* Get index of the element identified by object_id == $id;
805 */
806 $index = -1;
807 $data = $this->current_handler->tree_->pap;
808 foreach($data as $key => $obj){
809 if($obj->object_id == $id && $index==-1){
810 $index = $key;
811 }
812 }
814 /* Tell to user that we couldn't find the given object
815 * so we can't add an element.
816 */
817 if($index == -1 ){
818 trigger_error("Can't add new element, specified \$id=".$id." could not be found in object tree.");
819 return(FALSE);
820 }
822 /* We have found the specified object_id
823 * and want to detect the next free position
824 * to insert the new element.
825 */
826 if($position == "above"){
827 $direction ="up";
828 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
829 }else{
830 $direction = "down";
831 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
832 }
833 /* This is extremly necessary, cause some objects
834 * updates the tree objects ... Somehow i should change this ...
835 */
836 $data = $this->current_handler->tree_->pap;
837 $start = $end = array();
839 if($position == "above"){
840 $start = array_slice($data,0,$next_free);
841 $end = array_slice($data,$next_free);
842 }else{
843 $start = array_slice($data,0,$next_free+1);
844 $end = array_slice($data,$next_free+1);
845 }
847 $new = array();
848 foreach($start as $obj){
849 $new[] = $obj;
850 }
851 foreach($ele as $el){
852 $new[] = $el;
853 }
854 foreach($end as $obj){
855 $new[] = $obj;
856 }
857 $data= $new;
858 $this->current_handler->tree_->pap = $data;
859 return(TRUE);
860 }
864 function save_object()
865 {
866 if($this->current_handler){
868 if(isset($_GET['Add_Object_Top_ID'])){
869 $this->add_new_element = TRUE;
870 $this->add_new_id = $_GET['Add_Object_Top_ID'];
871 $this->add_above_below = "above";
872 }
874 if(isset($_GET['Add_Object_Bottom_ID'])){
875 $this->add_new_element = TRUE;
876 $this->add_new_id = $_GET['Add_Object_Bottom_ID'];
877 $this->add_above_below = "below";
878 }
880 if(isset($_GET['Remove_Object_ID'])){
881 $found_id = -1;
882 foreach($this->current_handler->tree_->pap as $key => $element){
883 if($element->object_id == $_GET['Remove_Object_ID']){
884 $found_id = $key;
885 }
886 }
887 if($found_id != -1 ){
888 $this->current_handler->tree_->remove_object($found_id);
889 }
890 }
892 if(isset($_GET['Move_Up_Object_ID'])){
893 $found_id = -1;
894 foreach($this->current_handler->tree_->pap as $key => $element){
895 if($element->object_id == $_GET['Move_Up_Object_ID']){
896 $found_id = $key;
897 }
898 }
899 if($found_id != -1 ){
900 $this->current_handler->tree_->move_up_down($found_id,"up");
901 }
902 }
904 if(isset($_GET['Move_Down_Object_ID'])){
905 $found_id = -1;
906 foreach($this->current_handler->tree_->pap as $key => $element){
907 if($element->object_id == $_GET['Move_Down_Object_ID']){
908 $found_id = $key;
909 }
910 }
911 if($found_id != -1 ){
912 $this->current_handler->tree_->move_up_down($found_id,"down");
913 }
914 }
917 /* Check if there is an add object requested
918 */
919 $data = $this->current_handler->tree_->pap;
920 $once = TRUE;
921 foreach($_POST as $name => $value){
922 foreach($data as $key => $obj){
923 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
924 $once = FALSE;
925 $this->add_element_type = $_POST['element_type_'.$obj->object_id];
926 $this->add_new_element = FALSE;
927 $this->add_new_id = $obj->object_id;
928 $this->add_above_below = "above";
929 $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
930 }
931 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
932 $once = FALSE;
933 $this->add_element_type = $_POST['element_type_'.$obj->object_id];
934 $this->add_new_element = FALSE;
935 $this->add_new_id = $obj->object_id;
936 $this->add_above_below = "below";
937 $this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below);
938 }
940 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
941 $once = FALSE;
942 $this->current_handler->tree_->remove_object($key);
943 }
944 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
945 $this->current_handler->tree_->move_up_down($key,"up");
946 $once = FALSE;
947 }
948 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
949 $this->current_handler->tree_->move_up_down($key,"down");
950 $once = FALSE;
951 }
952 }
953 }
955 /* Skip Mode changes and Parse tests
956 * if we are currently in a subdialog
957 */
959 $this->current_handler->save_object();
960 $Mode = $this->scripts[$this->current_script]['MODE'];
961 $skip_mode_change = false;
962 if(in_array($Mode,array("Source-Only","Source"))){
963 if(isset($_POST['script_contents'])){
964 $sc = stripslashes($_POST['script_contents']);
965 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
966 $p = new My_Parser($this);
967 if($p -> parse($sc)){
968 $this->Script_Error = "";
969 } else {
970 $this->Script_Error = $p->status_text;
971 $skip_mode_change = TRUE;
972 }
973 }
974 }
975 if(in_array($Mode,array("Structured"))){
976 $sc = $this->current_handler->get_sieve_script();
977 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
978 $p = new My_Parser($this);
979 if($p -> parse($sc)){
980 $this->Script_Error = "";
981 } else {
982 $this->Script_Error = $p->status_text;
983 $skip_mode_change = TRUE;
984 }
985 }
986 if(!$skip_mode_change){
987 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
988 $old_mode = $this->scripts[$this->current_script]['MODE'];
989 if(isset($_POST['View_Source'])){
990 $this->scripts[$this->current_script]['MODE'] = "Source";
991 }
992 if(isset($_POST['View_Structured'])){
993 $this->scripts[$this->current_script]['MODE'] = "Structured";
994 }
995 $new_mode = $this->scripts[$this->current_script]['MODE'];
997 if($old_mode != $new_mode){
999 $sc = $this->scripts[$this->current_script]['SCRIPT'];
1000 $p = new My_Parser($this);
1002 if($p -> parse($sc)){
1003 $this->current_handler->parse($sc);
1004 $this->Script_Error = "";
1005 } else {
1006 $this->Script_Error = $p->status_text;
1007 }
1008 }
1009 }
1010 }
1011 }
1012 }
1015 function get_used_script_names()
1016 {
1017 $ret = array();
1018 foreach($this->scripts as $script){
1019 $ret[] = $script['NAME'];
1020 }
1021 return($ret);
1022 }
1026 function save()
1027 {
1028 /* Get sieve */
1029 if(!$this->sieve_handle = $this->get_sieve()){
1030 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($this->Sieve_Error).'</i>'), ERROR_DIALOG);
1031 return;
1032 }
1034 $everything_went_fine = TRUE;
1036 foreach($this->scripts as $key => $script){
1037 if($script['EDITED']){
1038 $data = $this->scripts[$key]['SCRIPT'];
1039 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], addcslashes ($data,"\\"))){
1040 new log("modify","users/mailAccount".get_class($this),$script['NAME'],"Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
1042 $everything_went_fine = FALSE;
1043 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot store SIEVE script: %s"), '<br><br><i>'.to_string($this->sieve_handle->error_raw).'</i>'), ERROR_DIALOG);
1044 $this->scripts[$key]['MSG'] = "<font color='red'>".
1045 _("Failed to save sieve script").": ".
1046 to_string($this->sieve_handle->error_raw).
1047 "</font>";
1048 }
1049 }
1050 }
1051 return($everything_went_fine);
1052 }
1053 }
1054 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1055 ?>