Code

a406403bdfb161abdc4126e96998e71931909fec
[gosa.git] / include / 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();  
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   {
130     /* Connect to sieve class and try to get all available sieve scripts */
131     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
132     $this->Sieve_Error = "";
133   
134     /* Log into the mail server */
135     $this->sieve_handle= new sieve(
136         $cfg["sieve_server"], 
137         $cfg["sieve_port"], 
138         $this->parent->uid, 
139         $cfg["password"], 
140         $cfg["admin"]);
142     /* Try to login */
143     if (!$this->sieve_handle->sieve_login()){
144       $this->Sieve_Error = $this->sieve_handle->error_raw;
145       return(FALSE);
146     }
147     return($this->sieve_handle);
148   }
151   /* Handle sieve list 
152    */
153   function execute()
154   {
155     /***************
156      * Create a new Script 
157      ***************/
159     /* Force opening the add script dialog */
160     if(isset($_POST['create_new_script'])){
161       $this->create_script = TRUE;
162     }
164     /* Close add script dialog, without adding a new one */
165     if(isset($_POST['create_script_cancel'])){
166       $this->create_script = FALSE;
167     }
169     /* Display create script dialog 
170      *  handle posts, display warnings if specified 
171      *  name is not useable. 
172      * Create a new script with given name
173      */
174     if($this->create_script){
175     
176       /* Set initial name or used posted name if available */
177       $name = "";
178       if(isset($_POST['NewScriptName'])){
179         $name = trim($_POST['NewScriptName']);
180       }
181  
182       /* Check given name */ 
183       $err = "";
185       /* Is given name in lower case characters ? */
186       if(isset($_POST['create_script_save'])){
187         if(!strlen($name)){
188           $err = _("You should specify a name for your new script.");
189         }
190         /* Is given name in lower case characters ? */
191         if($name != strtolower($name)){
192           $err = _("Only lower case names are allowed here.");
193         }
195         /* Only chars are allowed here */
196         if(preg_match("/[^a-z]/i",$name)){
197           $err = _("Only a-z are allowed in script names.");
198         }
200         $tmp = $this->get_used_script_names();
201         if(in_array_ics($name,$tmp)){
202           $err =_("The specified name is already in use.");
203         }
204       }
206       /* Create script if everything is ok */
207       if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
209         /* Close dialog */
210         $this->create_script = FALSE;
212         /* Script contents to use */
213         $script = "/*New script */".
214                   "stop;";
216         /* Create a new parser and initialize default values */
217         $p = new My_Parser($this);
218         $ret = $p->parse($script);
219         $sc['SCRIPT'] = $script;
220         $sc['ORIG_SCRIPT'] = $script;
221         $sc['IS_NEW'] = TRUE;
222         $sc['MSG']   = "";
223         if(!$ret){
224           $sc['STATUS']   = FALSE;
225           $sc['MODE']    = "Source-Only";
226           $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
227         }else{
228           $sc['STATUS']   = TRUE;
229           $sc['MODE']    = "Strucktured";
230           $sc['MSG'] = _("Parse successful");
231         }
232         $sc['PARSER'] = $p;
233         $sc['EDITED'] = TRUE;
234         $sc['ACTIVE'] = FALSE;
235         $sc['NAME']   = $name;
236       
237         /* Add script */
238         $this->scripts[$name] = $sc;
239       }else{
240       
241         /* Display dialog to enter new script name */
242         $smarty = get_smarty();
243         $smarty->assign("NewScriptName",$name);
244         $smarty->assign("Error",$err);
245         return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
246       }
247     }
250     /*************
251      * Handle several posts 
252      *************/
254     $once = TRUE;
255     foreach($_POST as $name => $value){
257       /* Edit script requested */
258       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
259         $script = preg_replace("/^editscript_/","",$name);
260         $script = preg_replace("/_(x|y)/","",$script);
261         $once = FALSE;
263         $this->current_script = $script;
264         $this->current_handler = $this->scripts[$script]['PARSER'];
265       }
267       /* remove script requested */
268       if(preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
269         $script = preg_replace("/^delscript_/","",$name);
270         $script = preg_replace("/_(x|y)/","",$script);
271         $once = FALSE;
272  
273         $this->script_to_delete = $script;  
274       }
276       /* Activate script */
277       if(preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
278         $script = preg_replace("/^active_script_/","",$name);
279         $script = preg_replace("/_(x|y)/","",$script);
280         $once = FALSE;
282         /* We can only activate existing scripts */
283         if(!$this->scripts[$script]['IS_NEW']){
285           /* Get sieve */
286           if(!$this->sieve_handle = $this->get_sieve()){
287             print_red(
288                 sprintf(
289                   _("Can't log into SIEVE server. Server says '%s'."),
290                   to_string($this->Sieve_Error)));
291           }
293           /* Try to activate the given script and update 
294            *  class script array. 
295            */
296           if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
297             print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
298           }else{
299             foreach($this->scripts as $key => $data){
300               if($key == $script){
301                 $this->scripts[$key]['ACTIVE'] = TRUE;
302               }else{
303                 $this->scripts[$key]['ACTIVE'] = FALSE;
304               }
305             }
306           }
307         }
308       }
309     }
311     
312     /*************
313      * Remove script handling 
314      *************/
316     /* Remove aborted */
317     if(isset($_POST['delete_cancel'])){
318       $this->script_to_delete = -1;
319     }
321     /* Remove confirmed */
322     if(isset($_POST['delete_script_confirm'])){
324       $script = $this->scripts[$this->script_to_delete];
326       /* Just remove from array if it is a new script */
327       if($script['IS_NEW']){
328         unset($this->scripts[$this->script_to_delete]);
329       }else{
331         /* Get sieve */
332         if(!$this->sieve_handle = $this->get_sieve()){
333           print_red(
334               sprintf(
335                 _("Can't log into SIEVE server. Server says '%s'."),
336                 to_string($this->Sieve_Error)));
337         }
339         if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
340           print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
341         }else{
342           unset($this->scripts[$this->script_to_delete]);
343         }
344       }
345       $this->script_to_delete = -1;
346     }
348     /* Display confirm dialog */
349     if($this->script_to_delete != -1){
350       $smarty = get_smarty();
351       $smarty->assign("Warning",
352           sprintf(_("You are going to remove the sieve script '%s' from your mail server."),
353             $this->scripts[$this->script_to_delete]['NAME']));
354       return($smarty->fetch(get_template_path("templates/remove_script.tpl",TRUE,dirname(__FILE__))));
355     }
358     /**************
359      * Save script changes 
360      **************/
362     /* Abort saving */
363     if(isset($_POST['cancel_sieve_changes'])){
364       $this->current_handler = NULL;
365     }
367     /* Save currently edited sieve script. */
368     if(isset($_POST['save_sieve_changes'])){
369       $chk = $this->current_handler->check();
370       if(!count($chk)){
372         $sc = $this->scripts[$this->current_script]['SCRIPT'];
373         $p = new My_Parser($this);
374         if($p -> parse($sc)){
376           if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
377             $this->scripts[$this->current_script]['MODE'] = "Source";
378           }
379   
380           $this->scripts[$this->current_script]['PARSER'] = $p;
381           $this->scripts[$this->current_script]['EDITED'] = TRUE;
382           $this->scripts[$this->current_script]['STATUS'] = TRUE;
383           $this->scripts[$this->current_script]['MSG'] = _("Edited");
384           $this->current_handler = NULL;
385         }else{
386           print_red($p->status_text);;
387         }
388       }else{
389         print_red(_("Please fix all errors before saving."));
390       }
391     }
394     /*************
395      * Display edit dialog 
396      *************/
398     /* Display edit dialog, depending on Mode display different uis
399      */
400     if($this->current_handler){
402         if(isset($_POST['Import_Script'])){
403           $this->Import_Script = TRUE;
404         }
406         if(isset($_POST['Import_Script_Cancel'])){
407           $this->Import_Script = FALSE;
408         }
410         if(isset($_POST['Import_Script_Save']) && isset($_FILES['Script_To_Import'])){
412           $file     = $_FILES['Script_To_Import'];
414           if($file['size'] == 0){
415             print_red(_("Specified file seams to empty."));
416           }elseif(!file_exists($file['tmp_name'])){
417             print_red(_("Upload failed, somehow nothing was uploaded or the temporary file can't be accessed."));
418           }elseif(!is_readable ($file['tmp_name'])){
419             print_red(sprintf(_("Can't open file '%s' to read uploaded file contents."),$file['tmp_name']));
420           }else{
421             
422             
423  
424             $contents = file_get_contents($file['tmp_name']);
425            
426             $this->scripts[$this->current_script]['SCRIPT'] = $contents;
427             if(!$this->current_handler->parse($contents)){
428               $this->scripts[$this->current_script]['MODE'] = "Source";
429             }else{
430               $this->scripts[$this->current_script]['MODE'] = "Structured";
431             }
432             $this->Import_Script = FALSE;
433           }
434         }
436         if($this->Import_Script){
437           $smarty = get_smarty();
438           $str = $smarty->fetch(get_template_path("templates/import_script.tpl",TRUE,dirname(__FILE__)));
439           return($str);
440         }
441   
443         /* Create dump of current sieve script */
444         if(isset($_POST['Save_Copy'])){
446             /* force download dialog */
447             header("Content-type: application/tiff\n");
448             if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
449                     preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
450                 header('Content-Disposition: filename="dump.script"');
451             } else {
452                 header('Content-Disposition: attachment; filename="dump.script"');
453             }
454             header("Content-transfer-encoding: binary\n");
455             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
456             header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
457             header("Cache-Control: no-cache");
458             header("Pragma: no-cache");
459             header("Cache-Control: post-check=0, pre-check=0");
460             echo $this->scripts[$this->current_script]['SCRIPT'];
461             exit();
462         }
465       /****
466        * Add new element to ui
467        ****/
469       /* Abort add dialog */ 
470       if(isset($_POST['select_new_element_type_cancel'])){
471         $this->add_new_element = FALSE;
472       }
474       /* Add a new element */
475       if($this->add_new_element){
477         $element_types= array(
478             "sieve_keep"      => _("Keep"),
479             "sieve_comment"   => _("Comment"),
480             "sieve_fileinto"  => _("File into"),
481             "sieve_keep"      => _("Keep"),
482             "sieve_discard"   => _("Discard"),
483             "sieve_redirect"  => _("Redirect"),
484             "sieve_reject"    => _("Reject"),
485             "sieve_require"   => _("Require"),
486             "sieve_stop"      => _("Stop"),
487             "sieve_vacation"  => _("Vacation message"),
488             "sieve_if"        => _("If"));
491         /* Element selected */
492         if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']]) 
493            || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
494           $this->add_element_type = $_POST['element_type'];
495         }
497         /* Create new element and add it to
498          *  the selected position 
499          */
500         if(isset($_POST['select_new_element_type'])){
502           $this->add_new_id;
503           $data = $this->current_handler->tree_->pap;
505           /* Get index of the element identified by object_id == $this->add_new_id; */
506           $index = -1;
507           foreach($data as $key => $obj){
508             if($obj->object_id == $this->add_new_id && $index==-1){
509               $index = $key;
510             }
511           }
512  
513           /* We have found the specified object_id 
514            *  and want to detect the next free position 
515            *  to insert the new element.
516            */
517           if($index != -1){
518             if($this->add_above_below == "above"){
519               $direction ="up";
520               $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
521               $next_free ++;
522             }else{
523               $direction = "down";
524               $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
525             }
526             $this->add_new_id = $this->current_handler->tree_->pap[$next_free]->object_id;
527           }
529           $parent = $this->current_handler->tree_;
531           /* Create elements we should add */
532           if($this->add_element_type == "sieve_if"){
533             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
534             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
535             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
536           }elseif($this->add_element_type == "sieve_else"){
537             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
538             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
539             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
540           }elseif($this->add_element_type == "sieve_elsif"){
541             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
542             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
543             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()),$parent);
544           }else{
545             $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()),$parent);
546           }
548           $start = $end = array();
549           $found = false;
551           /* Add above current element*/
552           if($this->add_above_below == "above"){
553             foreach($data as $key => $obj){
554               if($obj->object_id == $this->add_new_id){
555                 $found = true;
556               }
557               if(!$found){
558                 $start[] = $obj;
559               }else{
560                 $end[] = $obj;
561               }
562             }
563           }else{
564             /* Add below current element */
565             foreach($data as $key => $obj){
566               if(!$found){
567                 $start[] = $obj;
568               }else{
569                 $end[] = $obj;
570               }
571               if($obj->object_id == $this->add_new_id){
572                 $found = true;
573               }
574             }
575           }
578           /* Only add, if current element could be located */
579           if($found){
580             $new = array();
581             foreach($start as $obj){
582               $new[] = $obj;
583             }
584             foreach($ele as $el){
585               $new[] = $el;
586             }
587             foreach($end as $obj){
588               $new[] = $obj;
589             }
590             $data= $new;
591             $this->current_handler->tree_->pap = $data;
592             $this->add_new_element = FALSE;
593           }else{
594             print_red(_("Something went wrong while adding a new entry."));
595           }
596         }
597       }
599       /* Only display select dialog if it is necessary */
600       if($this->add_new_element){
601         $smarty = get_smarty();
602     
603         $add_else_elsif = FALSE;
604         foreach($this->current_handler->tree_->pap as $obj){
605           if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
606             $add_else_elsif = TRUE;
607           }
608         }
610         if($add_else_elsif && $this->add_above_below == "below"){
611           $element_types['sieve_else'] = _("Else");
612           $element_types['sieve_elsif'] = _("Else if");
613         }
614   
615         $smarty->assign("element_types",$element_types );
616         $smarty->assign("element_type",$this->add_element_type);
617         $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
618         return($str);
619       }
623       /****************
624        * Handle test posts 
625        ****************/
627       /* handle some special posts from test elements 
628        */
629       foreach($_POST as $name => $value){
630         if(preg_match("/^Add_Test_Object_/",$name)) {
631           $name = preg_replace("/^Add_Test_Object_/","",$name);
632           $name = preg_replace("/_(x|y)$/","",$name);
634           $test_types_to_add = array(
635               "address" =>_("Address"),
636               "header"  =>_("Header"),
637               "envelope"=>_("Envelope"),
638               "size"    =>_("Size"),
639               "exists"  =>_("Exists"),
640               "allof"   =>_("All of"),
641               "anyof"   =>_("Any of"),
642               "true"    =>_("True"),
643               "false"   =>_("False"));
645           $smarty = get_smarty();
646           $smarty->assign("ID",$name);
647           $smarty->assign("test_types_to_add",$test_types_to_add);
648           $ret = $smarty->fetch(get_template_path("templates/select_test_type.tpl",TRUE,dirname(__FILE__)));
649           return($ret);
650         }
651       }
653       $current = $this->scripts[$this->current_script];
655       /* Create html results */
656       $smarty = get_smarty();
657       $smarty->assign("Mode",$current['MODE']);
658       if($current['MODE'] == "Structured"){
659         $smarty->assign("Contents",$this->current_handler->tree_->execute());
660       }else{
661         $smarty->assign("Contents",$current['SCRIPT']);
662       }
663       $smarty->assign("Script_Error",$this->Script_Error);
664       $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
665       return($ret);
666     }
669     /* Create list of available sieve scripts 
670      */
671     $List = new divSelectBox("sieveManagement");
672     foreach($this->scripts as $key => $script){
673   
674       $edited =  $script['EDITED'];
675       $active =  $script['ACTIVE'];
676       
677       $field1 = array("string" => "&nbsp;",
678                       "attach" => "style='width:20px;'");  
679       if($active){
680         $field1 = array("string" => "<img src='images/true.png' alt='"._("Active")."'>",
681                         "attach" => "style='width:20px;'");  
682       }
683       $field2 = array("string" => $script['NAME']);  
684       $field3 = array("string" => $script['MSG']);
685       $field4 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
687       if($edited){
688         $field5 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
689                         "attach" => "style='width:30px;'");
690       }else{
691         $field5 = array("string" => "",
692                         "attach" => "style='width:30px;'");
693       }
695       if($active || $script['IS_NEW']){
696         $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
697                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
698                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
699       }else{
700         $field6 = array("string" => "<input type='image' name='active_script_".$key."' src='images/true.png'>".
701                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
702                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
703       }
704       $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5,$field6)); 
705     }
706   
707     $display ="<h2>Sieve script management</h2>";
708     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
709     $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
710     $display .=  $List->DrawList();
711     
712     $display .= "<p style=\"text-align:right\">\n";
713     $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
714     $display .= "&nbsp;\n";
715     $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
716     $display .= "</p>";
717     return($display);;
718   }
720   function save_object()
721   {
722     if($this->current_handler){
724       /* Check if there is an add object requested 
725        */
726       $data = $this->current_handler->tree_->pap;
727       $once = TRUE;
728       foreach($_POST as $name => $value){
729         foreach($data as $key => $obj){
730           if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
731             $once = FALSE;
732             $this->add_new_element    = TRUE;
733             $this->add_new_id         = $obj->object_id;
734             $this->add_above_below    = "above";
735           }
736           if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
737             $once = FALSE;
738             $this->add_new_element    = TRUE;
739             $this->add_new_id         = $obj->object_id;
740             $this->add_above_below    = "below";
741           }
742         
743           if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
744             $once = FALSE;
745             $this->current_handler->tree_->remove_object($key);
746           }
747           if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
748             $this->current_handler->tree_->move_up_down($key,"up");
749             $once = FALSE;
750           }
751           if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
752             $this->current_handler->tree_->move_up_down($key,"down");
753             $once = FALSE;
754           }
755         }
756       }
758       /* Skip Mode changes and Parse tests 
759        *  if we are currently in a subdialog 
760        */
762       $this->current_handler->save_object();
763       $Mode = $this->scripts[$this->current_script]['MODE'];
764       $skip_mode_change = false;
765       if(in_array($Mode,array("Source-Only","Source"))){
766         if(isset($_POST['script_contents'])){
767           $sc = stripslashes($_POST['script_contents']);
768           $this->scripts[$this->current_script]['SCRIPT'] = $sc;
769           $p = new My_Parser($this);
770           if($p -> parse($sc)){
771             $this->Script_Error = "";
772           } else {
773             $this->Script_Error = $p->status_text;
774             $skip_mode_change = TRUE;
775           }
776         }
777       }
778       if(in_array($Mode,array("Structured"))){
779         $sc = $this->current_handler->get_sieve_script();
780         $this->scripts[$this->current_script]['SCRIPT'] = $sc;
781         $p = new My_Parser($this);
782         if($p -> parse($sc)){
783           $this->Script_Error = "";
784         } else {
785           $this->Script_Error = $p->status_text;
786           $skip_mode_change = TRUE;
787         }
788       }
789       if(!$skip_mode_change){
790         if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
791           $old_mode = $this->scripts[$this->current_script]['MODE'];
792           if(isset($_POST['View_Source'])){
793             $this->scripts[$this->current_script]['MODE'] = "Source";
794           }
795           if(isset($_POST['View_Structured'])){
796             $this->scripts[$this->current_script]['MODE'] = "Structured";
797           }
798           $new_mode = $this->scripts[$this->current_script]['MODE'];
800           if($old_mode != $new_mode){
802             $sc = $this->scripts[$this->current_script]['SCRIPT'];
803             $p = new My_Parser($this);
805             if($p -> parse($sc)){
806               $this->current_handler->parse($sc);
807               $this->Script_Error = "";
808             } else {
809               $this->Script_Error = $p->status_text;
810             }
811           } 
812         }
813       }
814     }
815   }
817   
818   function get_used_script_names()
819   {
820     $ret = array();
821     foreach($this->scripts as $script){
822       $ret[] = $script['NAME'];
823     }
824     return($ret);
825   }
829   function save()
830   {
831     /* Get sieve */
832     if(!$this->sieve_handle = $this->get_sieve()){
833       print_red(
834           sprintf(
835             _("Can't log into SIEVE server. Server says '%s'."),
836             to_string($this->Sieve_Error)));
837     }
839     $everything_went_fine = TRUE;
841     foreach($this->scripts as $key => $script){
842       if($script['EDITED']){
843         $data = $this->scripts[$key]['SCRIPT'];
844         if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
845           gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
846           $everything_went_fine = FALSE;
847           print_red(to_string($this->sieve_handle->error_raw));
848           $this->scripts[$key]['MSG'] = "<font color='red'>".
849                                            _("Failed to save sieve script").": ".
850                                            to_string($this->sieve_handle->error_raw).
851                                            "</font>";
852         }
853       }
854     }
855     return($everything_went_fine);
856   }
858 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
859 ?>