1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003-2007 - Fabian Hickert <hickert@gonicus.de>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
22 /* The sieve management class displays a list of sieve
23 * scripts for the given mail account.
24 * The account is identified by the parents uid attribute.
25 *
26 * $config The config object
27 * $dn The object edited
28 * $parent The parent object that provides the uid attribute
29 */
30 class sieveManagement extends plugin
31 {
32 var $parent = NULL;
33 var $scripts= array();
35 var $current_script = -1;
36 var $current_handler = NULL;
37 var $script_to_delete =-1;
38 var $sieve_handle = NULL;
39 var $Script_Error = "";
40 var $Sieve_Error = "";
41 var $create_script = FALSE;
43 /* To add new elements we need to know
44 * Where to the element -> add_new_id
45 * Whould we add above or below this id -> add_above_below
46 * What kind of element should we add -> add_element_type
47 */
48 var $add_new_element = FALSE;
49 var $add_new_id = -1;
50 var $add_above_below = "below";
51 var $add_element_type = "sieve_comment";
53 /* If this variable is TRUE, this indicates that we have the
54 * import dialog opened.
55 */
56 var $Import_Script = FALSE;
58 /* Initialize the class and load all sieve scripts
59 * try to parse them and display errors
60 */
61 function sieveManagement($config,$dn,$parent)
62 {
63 $this->parent = $parent;
64 plugin::plugin($config,$dn);
66 /* Get sieve */
67 if(!$this->sieve_handle = $this->get_sieve()){
68 print_red(
69 sprintf(
70 _("Can't log into SIEVE server. Server says '%s'."),
71 to_string($this->Sieve_Error)));
72 return;
73 }
76 /* Get all sieve scripts names */
77 if($this->sieve_handle->sieve_listscripts()){
78 if (is_array($this->sieve_handle->response)){
79 foreach($this->sieve_handle->response as $key => $name){
81 $data = array();
82 $data['NAME'] = $name;
84 if($key == "ACTIVE" && $key === "ACTIVE"){
85 $data['ACTIVE'] = TRUE;
86 }else{
87 $data['ACTIVE'] = FALSE;
88 }
89 $this->scripts[] = $data;
90 }
91 }
92 }
94 /* Get script contents */
95 foreach($this->scripts as $key => $script){
96 $p = new My_Parser($this);
97 $this->sieve_handle->sieve_getscript($script['NAME']);
99 $script = "";
100 foreach($this->sieve_handle->response as $line){
101 $script.=$line;
102 }
104 $this->scripts[$key]['IS_NEW'] = FALSE;;
105 $this->scripts[$key]['SCRIPT'] = $script;
106 $this->scripts[$key]['ORIG_SCRIPT'] = $script;
107 $this->scripts[$key]['MSG'] = "";
108 $ret = $p->parse($script);
109 if(!$ret){
110 $this->scripts[$key]['STATUS'] = FALSE;
111 $this->scripts[$key]['MODE'] = "Source";
112 $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
113 }else{
114 $this->scripts[$key]['STATUS'] = TRUE;
115 $this->scripts[$key]['MODE'] = "Structured";
116 $this->scripts[$key]['MSG'] = _("Parse successful");
117 }
118 $this->scripts[$key]['PARSER'] = $p;
119 $this->scripts[$key]['EDITED'] = FALSE;
120 }
121 $this->sieve_handle = $this->sieve_handle;
122 }
125 /* Return a sieve class hanlde,
126 * false if login fails
127 */
128 function get_sieve()
129 {
131 /* Connect to sieve class and try to get all available sieve scripts */
132 if(isset($this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer])){
133 $cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
134 $this->Sieve_Error = "";
136 /* Log into the mail server */
137 $this->sieve_handle= new sieve(
138 $cfg["sieve_server"],
139 $cfg["sieve_port"],
140 $this->parent->uid,
141 $cfg["password"],
142 $cfg["admin"]);
144 /* Try to login */
145 if (!$this->sieve_handle->sieve_login()){
146 $this->Sieve_Error = $this->sieve_handle->error_raw;
147 return(FALSE);
148 }
149 return($this->sieve_handle);
150 }else{
151 $this->Sieve_Error = sprintf(_("The specified mail server '%s' does not exist within the GOsa configuration."),
152 $this->parent->gosaMailServer);
153 return(FALSE);
154 }
155 }
158 /* Handle sieve list
159 */
160 function execute()
161 {
162 /***************
163 * Create a new Script
164 ***************/
166 /* Force opening the add script dialog */
167 if(isset($_POST['create_new_script'])){
168 $this->create_script = TRUE;
169 }
171 /* Close add script dialog, without adding a new one */
172 if(isset($_POST['create_script_cancel'])){
173 $this->create_script = FALSE;
174 }
176 /* Display create script dialog
177 * handle posts, display warnings if specified
178 * name is not useable.
179 * Create a new script with given name
180 */
181 if($this->create_script){
183 /* Set initial name or used posted name if available */
184 $name = "";
185 if(isset($_POST['NewScriptName'])){
186 $name = trim($_POST['NewScriptName']);
187 }
189 /* Check given name */
190 $err = "";
192 /* Is given name in lower case characters ? */
193 if(isset($_POST['create_script_save'])){
194 if(!strlen($name)){
195 $err = _("You should specify a name for your new script.");
196 }
197 /* Is given name in lower case characters ? */
198 if($name != strtolower($name)){
199 $err = _("Only lower case names are allowed here.");
200 }
202 /* Only chars are allowed here */
203 if(preg_match("/[^a-z]/i",$name)){
204 $err = _("Only a-z are allowed in script names.");
205 }
207 $tmp = $this->get_used_script_names();
208 if(in_array_ics($name,$tmp)){
209 $err =_("The specified name is already in use.");
210 }
211 }
213 /* Create script if everything is ok */
214 if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
216 /* Close dialog */
217 $this->create_script = FALSE;
219 /* Script contents to use */
220 $script = "/*New script */".
221 "stop;";
223 /* Create a new parser and initialize default values */
224 $p = new My_Parser($this);
225 $ret = $p->parse($script);
226 $sc['SCRIPT'] = $script;
227 $sc['ORIG_SCRIPT'] = $script;
228 $sc['IS_NEW'] = TRUE;
229 $sc['MSG'] = "";
230 if(!$ret){
231 $sc['STATUS'] = FALSE;
232 $sc['MODE'] = "Source";
233 $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
234 }else{
235 $sc['STATUS'] = TRUE;
236 $sc['MODE'] = "Structured";
237 $sc['MSG'] = _("Parse successful");
238 }
239 $sc['PARSER'] = $p;
240 $sc['EDITED'] = TRUE;
241 $sc['ACTIVE'] = FALSE;
242 $sc['NAME'] = $name;
244 /* Add script */
245 $this->scripts[$name] = $sc;
246 }else{
248 /* Display dialog to enter new script name */
249 $smarty = get_smarty();
250 $smarty->assign("NewScriptName",$name);
251 $smarty->assign("Error",$err);
252 return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
253 }
254 }
257 /*************
258 * Handle several posts
259 *************/
261 $once = TRUE;
262 foreach($_POST as $name => $value){
264 /* Edit script requested */
265 if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
266 $script = preg_replace("/^editscript_/","",$name);
267 $script = preg_replace("/_(x|y)/","",$script);
268 $once = FALSE;
270 $this->current_script = $script;
271 $this->current_handler = $this->scripts[$script]['PARSER'];
272 $this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
273 }
275 /* remove script requested */
276 if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
277 $script = preg_replace("/^delscript_/","",$name);
278 $script = preg_replace("/_(x|y)/","",$script);
279 $once = FALSE;
280 $this->script_to_delete = $script;
281 }
283 /* Activate script */
284 if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
285 $script = preg_replace("/^active_script_/","",$name);
286 $script = preg_replace("/_(x|y)/","",$script);
287 $once = FALSE;
289 /* We can only activate existing scripts */
290 if(!$this->scripts[$script]['IS_NEW']){
292 /* Get sieve */
293 if(!$this->sieve_handle = $this->get_sieve()){
294 print_red(
295 sprintf(
296 _("Can't log into SIEVE server. Server says '%s'."),
297 to_string($this->Sieve_Error)));
298 }
300 /* Try to activate the given script and update
301 * class script array.
302 */
303 if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
304 print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
305 }else{
306 foreach($this->scripts as $key => $data){
307 if($key == $script){
308 $this->scripts[$key]['ACTIVE'] = TRUE;
309 }else{
310 $this->scripts[$key]['ACTIVE'] = FALSE;
311 }
312 }
313 }
314 }
315 }
316 }
319 /*************
320 * Remove script handling
321 *************/
323 /* Remove aborted */
324 if(isset($_POST['delete_cancel'])){
325 $this->script_to_delete = -1;
326 }
328 /* Remove confirmed */
329 if($this->parent->acl_is_writeable("sieveManagement") && isset($_POST['delete_script_confirm'])){
331 $script = $this->scripts[$this->script_to_delete];
333 /* Just remove from array if it is a new script */
334 if($script['IS_NEW']){
335 unset($this->scripts[$this->script_to_delete]);
336 }else{
338 /* Get sieve */
339 if(!$this->sieve_handle = $this->get_sieve()){
340 print_red(
341 sprintf(
342 _("Can't log into SIEVE server. Server says '%s'."),
343 to_string($this->Sieve_Error)));
344 }
346 if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
347 print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
348 }else{
349 unset($this->scripts[$this->script_to_delete]);
350 }
351 }
352 $this->script_to_delete = -1;
353 }
355 /* Display confirm dialog */
356 if($this->script_to_delete != -1){
357 $smarty = get_smarty();
358 $smarty->assign("Warning",
359 sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
360 $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") && isset($_POST['save_sieve_changes'])){
379 $chk = $this->current_handler->check();
380 if(!count($chk)){
382 $sc = $this->scripts[$this->current_script]['SCRIPT'];
383 $p = new My_Parser($this);
384 if($p -> parse($sc)){
386 if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
387 $this->scripts[$this->current_script]['MODE'] = "Source";
388 }
390 $this->scripts[$this->current_script]['PARSER'] = $p;
391 $this->scripts[$this->current_script]['EDITED'] = TRUE;
392 $this->scripts[$this->current_script]['STATUS'] = TRUE;
393 $this->scripts[$this->current_script]['MSG'] = _("Edited");
394 $this->current_handler = NULL;
395 }else{
396 print_red($p->status_text);;
397 }
398 }else{
399 print_red(_("Please fix all errors before saving."));
400 }
401 }
404 /*************
405 * Display edit dialog
406 *************/
408 /* Display edit dialog, depending on Mode display different uis
409 */
410 if($this->current_handler){
412 if(isset($_POST['Import_Script'])){
413 $this->Import_Script = TRUE;
414 }
416 if(isset($_POST['Import_Script_Cancel'])){
417 $this->Import_Script = FALSE;
418 }
420 if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
422 $file = $_FILES['Script_To_Import'];
424 if($file['size'] == 0){
425 print_red(_("Specified file seams to empty."));
426 }elseif(!file_exists($file['tmp_name'])){
427 print_red(_("Upload failed, somehow nothing was uploaded or the temporary file can't be accessed."));
428 }elseif(!is_readable ($file['tmp_name'])){
429 print_red(sprintf(_("Can't open file '%s' to read uploaded file contents."),$file['tmp_name']));
430 }else{
434 $contents = file_get_contents($file['tmp_name']);
436 $this->scripts[$this->current_script]['SCRIPT'] = $contents;
437 if(!$this->current_handler->parse($contents)){
438 $this->scripts[$this->current_script]['MODE'] = "Source";
439 }else{
440 $this->scripts[$this->current_script]['MODE'] = "Structured";
441 }
442 $this->Import_Script = FALSE;
443 }
444 }
446 if($this->Import_Script){
447 $smarty = get_smarty();
448 $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
449 return($str);
450 }
453 /* Create dump of current sieve script */
454 if(isset($_POST['Save_Copy'])){
456 /* force download dialog */
457 header("Content-type: application/tiff\n");
458 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
459 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
460 header('Content-Disposition: filename="dump.script"');
461 } else {
462 header('Content-Disposition: attachment; filename="dump.script"');
463 }
464 header("Content-transfer-encoding: binary\n");
465 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
466 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
467 header("Cache-Control: no-cache");
468 header("Pragma: no-cache");
469 header("Cache-Control: post-check=0, pre-check=0");
470 echo $this->scripts[$this->current_script]['SCRIPT'];
471 exit();
472 }
475 /****
476 * Add new element to ui
477 ****/
479 /* Abort add dialog */
480 if(isset($_POST['select_new_element_type_cancel'])){
481 $this->add_new_element = FALSE;
482 }
484 /* Add a new element */
485 if($this->add_new_element){
487 $element_types= array(
488 "sieve_keep" => _("Keep"),
489 "sieve_comment" => _("Comment"),
490 "sieve_fileinto" => _("File into"),
491 "sieve_keep" => _("Keep"),
492 "sieve_discard" => _("Discard"),
493 "sieve_redirect" => _("Redirect"),
494 "sieve_reject" => _("Reject"),
495 "sieve_require" => _("Require"),
496 "sieve_stop" => _("Stop"),
497 "sieve_vacation" => _("Vacation message"),
498 "sieve_if" => _("If"));
501 /* Element selected */
502 if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])
503 || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
504 $this->add_element_type = $_POST['element_type'];
505 }
507 /* Create new element and add it to
508 * the selected position
509 */
510 if(isset($_POST['select_new_element_type'])){
511 if($this->add_new_element_to_current_script($this->add_element_type,$this->add_new_id,$this->add_above_below)){
512 $this->add_new_element = FALSE;
513 }else{
514 print_red(_("Failed to add new element."));
515 }
516 }
517 }
519 /* Only display select dialog if it is necessary */
520 if($this->add_new_element){
521 $smarty = get_smarty();
523 $add_else_elsif = FALSE;
525 /* Check if we should add else/elsif to the select box
526 * or not. We can't add else twice!.
527 */
528 if($this->add_above_below == "below"){
530 /* Get posistion of the current element
531 */
532 foreach($this->current_handler->tree_->pap as $key => $obj){
534 if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
536 /* Get block start/end */
537 $end_id = $this->current_handler->tree_->get_block_end($key);
538 $else_found = FALSE;
539 $elsif_found = FALSE;
541 /* Check if there is already an else in this block
542 */
543 for($i = $key ; $i < $end_id ; $i ++){
544 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_else"){
545 $else_found = TRUE;
546 }
547 if(get_class($this->current_handler->tree_->pap[$i]) == "sieve_elsif"){
548 $elsif_found = TRUE;
549 }
550 }
552 /* Only allow adding 'else' if there is currently
553 * no 'else' statement. And don't allow adding
554 * 'else' before 'elseif'
555 */
556 if(!$else_found && (!(get_class($obj) == "sieve_if" && $elsif_found))){
557 $element_types['sieve_else'] = _("Else");
558 }
559 $element_types['sieve_elsif'] = _("Else if");
560 }
561 }
562 }
564 $smarty->assign("element_types",$element_types );
565 $smarty->assign("element_type",$this->add_element_type);
566 $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
567 return($str);
568 }
572 /****************
573 * Handle test posts
574 ****************/
576 /* handle some special posts from test elements
577 */
578 foreach($_POST as $name => $value){
579 if(preg_match("/^Add_Test_Object_/",$name)) {
580 $name = preg_replace("/^Add_Test_Object_/","",$name);
581 $name = preg_replace("/_(x|y)$/","",$name);
583 $test_types_to_add = array(
584 "address" =>_("Address"),
585 "header" =>_("Header"),
586 "envelope"=>_("Envelope"),
587 "size" =>_("Size"),
588 "exists" =>_("Exists"),
589 "allof" =>_("All of"),
590 "anyof" =>_("Any of"),
591 "true" =>_("True"),
592 "false" =>_("False"));
594 $smarty = get_smarty();
595 $smarty->assign("ID",$name);
596 $smarty->assign("test_types_to_add",$test_types_to_add);
597 $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
598 return($ret);
599 }
600 }
602 $current = $this->scripts[$this->current_script];
604 /* Create html results */
605 $smarty = get_smarty();
606 $smarty->assign("Mode",$current['MODE']);
607 if($current['MODE'] == "Structured"){
608 $smarty->assign("Contents",$this->current_handler->tree_->execute());
609 }else{
610 $smarty->assign("Contents",$current['SCRIPT']);
611 }
612 $smarty->assign("Script_Error",$this->Script_Error);
613 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
614 return($ret);
615 }
618 /* Create list of available sieve scripts
619 */
620 $List = new divSelectBox("sieveManagement");
621 foreach($this->scripts as $key => $script){
623 $edited = $script['EDITED'];
624 $active = $script['ACTIVE'];
626 $field1 = array("string" => " ",
627 "attach" => "style='width:20px;'");
628 if($active){
629 $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
630 "attach" => "style='width:20px;'");
631 }
632 $field2 = array("string" => $script['NAME']);
633 $field3 = array("string" => $script['MSG']);
634 $field4 = array("string" => _("Script length")." : ".strlen($script['SCRIPT']));
636 if($edited){
637 $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
638 "attach" => "style='width:30px;'");
639 }else{
640 $field5 = array("string" => "",
641 "attach" => "style='width:30px;'");
642 }
644 if($this->parent->acl_is_writeable("sieveManagement")){
645 $del = "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>";
646 }else{
647 $del = "<img src='images/empty' alt=' '>";
648 }
650 if($active || $script['IS_NEW'] || !$this->parent->acl_is_writeable("sieveManagement")){
651 $activate = "<img src='images/empty' alt=' '>";
652 }else{
653 $activate = "<input type='image' name='active_script_".$key."' src='images/true.png'>";
654 }
656 $field6 = array("string" => $activate."<input type='image' name='editscript_".$key."' src='images/edit.png'>".$del);
657 $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6));
658 }
660 $display ="<h2>Sieve script management</h2>";
661 $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
662 $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
663 $display .= $List->DrawList();
665 $display .= "<p style=\"text-align:right\">\n";
666 $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
667 $display .= " \n";
668 $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
669 $display .= "</p>";
670 return($display);;
671 }
674 /* Add a new element to the currently opened script editor.
675 * The insert position is specified by
676 */
677 function add_new_element_to_current_script($type,$id,$position)
678 {
679 /* Test given data */
680 if(!in_array_ics($position,array("above","below"))){
681 trigger_error("Can't add new element with \$position=".$position.". Only 'above','below' are allowed here.");
682 return(FALSE);
683 }
684 if(!is_numeric($id)){
685 trigger_error("Can't add new element, given id is not numeric.");
686 return(FALSE);
687 }
688 $tmp = get_declared_classes();
689 if(!in_array($type,$tmp)){
690 trigger_error("Can't add new element, given \$class=".$class." does not exists.");
691 return(FALSE);
692 }
693 if(!is_object($this->current_handler) || get_class($this->current_handler) != "My_Parser"){
694 trigger_error("Can't add new element, there is no valid script editor opened.");
695 return(FALSE);
696 }
698 /* Create elements we should add
699 * -Some element require also surrounding block elements
700 */
701 $parent = $this->current_handler->tree_;
702 if($this->add_element_type == "sieve_if"){
703 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
704 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
705 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
706 }elseif($this->add_element_type == "sieve_else"){
707 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
708 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
709 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
710 }elseif($this->add_element_type == "sieve_elsif"){
711 $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
712 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
713 $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
714 }else{
715 $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
716 }
718 /* Get index of the element identified by object_id == $id;
719 */
720 $index = -1;
721 $data = $this->current_handler->tree_->pap;
722 foreach($data as $key => $obj){
723 if($obj->object_id == $id && $index==-1){
724 $index = $key;
725 }
726 }
728 /* Tell to user that we couldn't find the given object
729 * so we can't add an element.
730 */
731 if($index == -1 ){
732 trigger_error("Can't add new element, specified \$id=".$id." could not be found in object tree.");
733 return(FALSE);
734 }
736 /* We have found the specified object_id
737 * and want to detect the next free position
738 * to insert the new element.
739 */
740 if($position == "above"){
741 $direction ="up";
742 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
743 }else{
744 $direction = "down";
745 $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction,TRUE);
746 }
747 /* This is extremly necessary, cause some objects
748 * updates the tree objects ... Somehow i should change this ...
749 */
750 $data = $this->current_handler->tree_->pap;
751 $start = $end = array();
753 if($position == "above"){
754 $start = array_slice($data,0,$next_free);
755 $end = array_slice($data,$next_free);
756 }else{
757 $start = array_slice($data,0,$next_free+1);
758 $end = array_slice($data,$next_free+1);
759 }
761 $new = array();
762 foreach($start as $obj){
763 $new[] = $obj;
764 }
765 foreach($ele as $el){
766 $new[] = $el;
767 }
768 foreach($end as $obj){
769 $new[] = $obj;
770 }
771 $data= $new;
772 $this->current_handler->tree_->pap = $data;
773 return(TRUE);
774 }
778 function save_object()
779 {
780 if($this->current_handler){
782 /* Check if there is an add object requested
783 */
784 $data = $this->current_handler->tree_->pap;
785 $once = TRUE;
786 foreach($_POST as $name => $value){
787 foreach($data as $key => $obj){
788 if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
789 $once = FALSE;
790 $this->add_new_element = TRUE;
791 $this->add_new_id = $obj->object_id;
792 $this->add_above_below = "above";
793 }
794 if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
795 $once = FALSE;
796 $this->add_new_element = TRUE;
797 $this->add_new_id = $obj->object_id;
798 $this->add_above_below = "below";
799 }
801 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
802 $once = FALSE;
803 $this->current_handler->tree_->remove_object($key);
804 }
805 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
806 $this->current_handler->tree_->move_up_down($key,"up");
807 $once = FALSE;
808 }
809 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
810 $this->current_handler->tree_->move_up_down($key,"down");
811 $once = FALSE;
812 }
813 }
814 }
816 /* Skip Mode changes and Parse tests
817 * if we are currently in a subdialog
818 */
820 $this->current_handler->save_object();
821 $Mode = $this->scripts[$this->current_script]['MODE'];
822 $skip_mode_change = false;
823 if(in_array($Mode,array("Source-Only","Source"))){
824 if(isset($_POST['script_contents'])){
825 $sc = stripslashes($_POST['script_contents']);
826 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
827 $p = new My_Parser($this);
828 if($p -> parse($sc)){
829 $this->Script_Error = "";
830 } else {
831 $this->Script_Error = $p->status_text;
832 $skip_mode_change = TRUE;
833 }
834 }
835 }
836 if(in_array($Mode,array("Structured"))){
837 $sc = $this->current_handler->get_sieve_script();
838 $this->scripts[$this->current_script]['SCRIPT'] = $sc;
839 $p = new My_Parser($this);
840 if($p -> parse($sc)){
841 $this->Script_Error = "";
842 } else {
843 $this->Script_Error = $p->status_text;
844 $skip_mode_change = TRUE;
845 }
846 }
847 if(!$skip_mode_change){
848 if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
849 $old_mode = $this->scripts[$this->current_script]['MODE'];
850 if(isset($_POST['View_Source'])){
851 $this->scripts[$this->current_script]['MODE'] = "Source";
852 }
853 if(isset($_POST['View_Structured'])){
854 $this->scripts[$this->current_script]['MODE'] = "Structured";
855 }
856 $new_mode = $this->scripts[$this->current_script]['MODE'];
858 if($old_mode != $new_mode){
860 $sc = $this->scripts[$this->current_script]['SCRIPT'];
861 $p = new My_Parser($this);
863 if($p -> parse($sc)){
864 $this->current_handler->parse($sc);
865 $this->Script_Error = "";
866 } else {
867 $this->Script_Error = $p->status_text;
868 }
869 }
870 }
871 }
872 }
873 }
876 function get_used_script_names()
877 {
878 $ret = array();
879 foreach($this->scripts as $script){
880 $ret[] = $script['NAME'];
881 }
882 return($ret);
883 }
887 function save()
888 {
889 /* Get sieve */
890 if(!$this->sieve_handle = $this->get_sieve()){
891 print_red(
892 sprintf(
893 _("Can't log into SIEVE server. Server says '%s'."),
894 to_string($this->Sieve_Error)));
895 }
897 $everything_went_fine = TRUE;
899 foreach($this->scripts as $key => $script){
900 if($script['EDITED']){
901 $data = $this->scripts[$key]['SCRIPT'];
902 if(!$this->sieve_handle->sieve_sendscript($script['NAME'], addcslashes ($data,"\\"))){
903 gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
904 $everything_went_fine = FALSE;
905 print_red(to_string($this->sieve_handle->error_raw));
906 $this->scripts[$key]['MSG'] = "<font color='red'>".
907 _("Failed to save sieve script").": ".
908 to_string($this->sieve_handle->error_raw).
909 "</font>";
910 }
911 }
912 }
913 return($everything_went_fine);
914 }
915 }
916 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
917 ?>