Code

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