Code

Added save method.
[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;
38   var $sieve_handle = NULL; 
41   /* Initialize the class and load all sieve scripts 
42    *  try to parse them and display errors 
43    */ 
44   function sieveManagement($config,$dn,$parent)
45   {
46     $this->parent = $parent;
47     plugin::plugin($config,$dn);
50     /* Connect to sieve class and try to get all available sieve scripts */
51     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
52     
53     /* Log into the mail server */
54     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->parent->uid,
55         $cfg["password"], $cfg["admin"]);
57     /* Try to login */
58     if (!$sieve->sieve_login()){
59       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
60             to_string($sieve->error_raw)));
61       return;
62     }
64     /* Get all sieve scripts names */
65     if($sieve->sieve_listscripts()){
66       if (is_array($sieve->response)){
67         foreach($sieve->response as $key => $name){
68           $this->scripts[$key]['NAME'] = $name;
69         }
70       } 
71     }
73     /* Get script contents */
74     foreach($this->scripts as $key => $script){
75       $p = new My_Parser;
76       $sieve->sieve_getscript($script['NAME']);
78       $script = "";
79       foreach($sieve->response as $line){
80         $script.=$line;
81       }
83       $this->scripts[$key]['SCRIPT'] = $script;
84       $this->scripts[$key]['MSG']   = "";
85       $ret = $p->parse($script);
86       if(!$ret){
87         $this->scripts[$key]['MSG']   = "<font color='red'>".$p->status_text."</font>";
88         $this->scripts[$key]['STATUS'] = _("Parse failed");
89       }else{
90         $this->scripts[$key]['STATUS'] = _("Parse successful");
91       }
92       $this->scripts[$key]['PARSER'] = $p;
93       $this->scripts[$key]['EDITED'] = FALSE;
94     }
95     $this->sieve_handle = $sieve;
96   }
99   /* Handle sieve list 
100    */
101   function execute()
102   {
103     $once = TRUE;
104     foreach($_POST as $name => $value){
105       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
106         $script = preg_replace("/^editscript_/","",$name);
107         $script = preg_replace("/_(x|y)/","",$script);
108         $once = FALSE;
110         $this->current_script = $script;
111         $this->current_handler = $this->scripts[$script]['PARSER'];
112       }
113     }
115     /* Abort saving */
116     if(isset($_POST['cancel_sieve_changes'])){
117       $this->current_handler = NULL;
118     }
120     /* Save currently edited sieve script. */
121     if(isset($_POST['save_sieve_changes'])){
122       $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
123       $this->scripts[$this->current_script]['EDITED'] = TRUE;
124       $this->current_handler = NULL; 
125     }
127     /* Create output for currently opened sieve script */
128     if($this->current_handler){
129       $ret = $this->current_handler->execute();
130       $ret .= "<div class='seperator' style='text-align:right; width:100%;'>
131         <input type='submit' name='save_sieve_changes' value='"._("Save")."'>
132         &nbsp;
133       <input type='submit' name='cancel_sieve_changes' value='"._("Cancel")."'>
134         </div>";
135       return($ret);
136     }
138     /* Create list of available sieve scripts 
139      */
140     $List = new divSelectBox("sieveManagement");
141     foreach($this->scripts as $key => $script){
142   
143       $edited =  $script['EDITED'];
145       $field1 = array("string" => $script['NAME']);  
146       $field2 = array("string" => $script['STATUS']);
147       $field3 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
149       if($edited){
150         $field4 = array("string" => "<img src='images/fai_new_hook.png' alt='"._("Edited")."'>",
151                         "attach" => "style='width:30px;'");
152       }else{
153         $field4 = array("string" => "",
154                         "attach" => "style='width:30px;'");
155       }
157       $field5 = array("string" => "<input type='image' name='editscript_".$key."' src='images/edit.png'>");
158       $List ->AddEntry(array($field1,$field2,$field3,$field4,$field5)); 
159     }
160   
161     $display ="<h2>Sieve script management</h2>";
162     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
163     $display .=  $List->DrawList();
164     
165     $display .= "<p style=\"text-align:right\">\n";
166     $display .= "<input type=submit name=\"sieve_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
167     $display .= "&nbsp;\n";
168     $display .= "<input type=submit name=\"sieve_cancel\" value=\""._("Cancel")."\">\n";
169     $display .= "</p>";
170     return($display);;
171   }
173   function save_object()
174   {
175     if($this->current_handler){
176       $this->current_handler->save_object();
177     }
178   }
181   function save()
182   {
183     /* Connect to sieve class and try to get all available sieve scripts */
184     $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
186     $this->sieve_handle= 
187         new sieve(  $cfg["sieve_server"], 
188                     $cfg["sieve_port"], 
189                     $this->parent->mail,
190                     $cfg["password"], 
191                     $cfg["admin"]);
193     if (!$this->sieve_handle->sieve_login()){
194       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
195       return;
196     }
198     $everything_went_fine = TRUE;
200     foreach($this->scripts as $key => $script){
201       if($script['EDITED']){
202         $data = $script['PARSER']->get_sieve_script();
203         if(!$this->sieve_handle->sieve_sendscript($script['NAME']."1", $data)){
204           gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
205           $everything_went_fine = FALSE;
206           print_red(to_string($this->sieve_handle->error_raw));
207           $this->scripts[$key]['STATUS'] = "<font color='red'>".
208                                            _("Failed to save sieve script").": ".
209                                            to_string($this->sieve_handle->error_raw).
210                                            "</font>";
211         }
212       }
213     }
214     return($everything_went_fine);
215   }
217 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
218 ?>