This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* The sieve management class displays a list of sieve
* scripts for the given mail account.
* The account is identified by the parents uid attribute.
*
* $config The config object
* $dn The object edited
* $parent The parent object that provides the uid attribute
*/
class sieveManagement extends plugin
{
var $parent = NULL;
var $scripts= array();
var $uattrib = "uid";
var $current_script = -1;
var $current_handler = NULL;
var $script_to_delete =-1;
var $sieve_handle = NULL;
var $Script_Error = "";
var $Sieve_Error = "";
var $create_script = FALSE;
/* To add new elements we need to know
* Where to add the element -> add_new_id
* Whould we add above or below this id -> add_above_below
* What kind of element should we add -> add_element_type
*/
var $add_new_element = FALSE;
var $add_new_id = -1;
var $add_above_below = "below";
var $add_element_type = "sieve_comment";
/* If this variable is TRUE, this indicates that we have the
* import dialog opened.
*/
var $Import_Script = FALSE;
/* Initialize the class and load all sieve scripts
* try to parse them and display errors
*/
function sieveManagement(&$config,$dn,&$parent,$uattrib)
{
/* Check given parameter */
if(!isset($parent->$uattrib)){
trigger_error("Sieve Management implementation error. Parameter 4 (".$uattrib.") must be part of the given parent element (".get_class($parent).").");
}
$this->uattrib = $uattrib;
$this->parent = &$parent;
plugin::plugin($config,$dn);
/* Get sieve, if this fail abort class initialization */
if(!$this->sieve_handle = $this->get_sieve()){
return;
}
/* Get all sieve scripts names */
if($this->sieve_handle->sieve_listscripts()){
if (is_array($this->sieve_handle->response)){
foreach($this->sieve_handle->response as $key => $name){
$data = array();
$data['NAME'] = $name;
if($key == "ACTIVE" && $key === "ACTIVE"){
$data['ACTIVE'] = TRUE;
}else{
$data['ACTIVE'] = FALSE;
}
$this->scripts[] = $data;
}
}
}
/* Get script contents */
foreach($this->scripts as $key => $script){
$p = new My_Parser($this);
$this->sieve_handle->sieve_getscript($script['NAME']);
$script = "";
foreach($this->sieve_handle->response as $line){
$script.=$line;
}
$this->scripts[$key]['IS_NEW'] = FALSE;;
$this->scripts[$key]['SCRIPT'] = $script;
$this->scripts[$key]['ORIG_SCRIPT'] = $script;
$this->scripts[$key]['MSG'] = "";
$ret = $p->parse($script);
if(!$ret){
$this->scripts[$key]['STATUS'] = FALSE;
$this->scripts[$key]['MODE'] = "Source";
$this->scripts[$key]['MSG'] = _("Parse failed")."".$p->status_text."";
}else{
$this->scripts[$key]['STATUS'] = TRUE;
$this->scripts[$key]['MODE'] = "Structured";
$this->scripts[$key]['MSG'] = _("Parse successful");
}
$this->scripts[$key]['PARSER'] = $p;
$this->scripts[$key]['EDITED'] = FALSE;
}
$this->sieve_handle = $this->sieve_handle;
}
/* Return a sieve class handle,
* false if login fails
*/
function get_sieve()
{
/* Connect to sieve class and try to get all available sieve scripts */
if(isset($this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer])){
$cfg= $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
$this->Sieve_Error = "";
$uattrib = $this->uattrib;
/* Log into the mail server */
$this->sieve_handle= new sieve(
$cfg["sieve_server"],
$cfg["sieve_port"],
$this->parent->$uattrib,
$cfg["password"],
$cfg["admin"],
$cfg["sieve_option"]);
/* Try to login */
if (!@$this->sieve_handle->sieve_login()){
$this->Sieve_Error = $this->sieve_handle->error_raw;
return(FALSE);
}
return($this->sieve_handle);
}else{
$this->Sieve_Error = sprintf(_("The specified mail server '%s' does not exist within the GOsa configuration."),
$this->parent->gosaMailServer);
return(FALSE);
}
}
/* Handle sieve list
*/
function execute()
{
/***************
* Create a new Script
***************/
/* Force opening the add script dialog */
if(isset($_POST['create_new_script'])){
$this->create_script = TRUE;
}
/* Close add script dialog, without adding a new one */
if(isset($_POST['create_script_cancel'])){
$this->create_script = FALSE;
}
/* Display create script dialog
* handle posts, display warnings if specified
* name is not useable.
* Create a new script with given name
*/
if($this->create_script){
/* Set initial name or used posted name if available */
$name = "";
if(isset($_POST['NewScriptName'])){
$name = trim($_POST['NewScriptName']);
}
/* Check given name */
$err = false;
/* Is given name in lower case characters ? */
if(isset($_POST['create_script_save'])){
if(!strlen($name)){
$err =true;
msg_dialog::display(_("Error"), _("No script name specified!"), ERROR_DIALOG);
}
/* Is given name in lower case characters ? */
if($name != strtolower($name)){
$err =true;
msg_dialog::display(_("Error"), _("Please use only lowercase script names!"), ERROR_DIALOG);
}
/* Only chars are allowed here */
if(preg_match("/[^a-z]/i",$name)){
$err =true;
msg_dialog::display(_("Error"), _("Please use only alphabetical characters in script names!"), ERROR_DIALOG);
}
$tmp = $this->get_used_script_names();
if(in_array_ics($name,$tmp)){
$err =true;
msg_dialog::display(_("Error"), _("Script name already in use!"), ERROR_DIALOG);
}
}
/* Create script if everything is ok */
if($this->create_script && isset($_POST['create_script_save']) && !$err){
/* Close dialog */
$this->create_script = FALSE;
/* Script contents to use */
$script = "/*New script */".
"stop;";
/* Create a new parser and initialize default values */
$p = new My_Parser($this);
$ret = $p->parse($script);
$sc['SCRIPT'] = $script;
$sc['ORIG_SCRIPT'] = $script;
$sc['IS_NEW'] = TRUE;
$sc['MSG'] = "";
if(!$ret){
$sc['STATUS'] = FALSE;
$sc['MODE'] = "Source";
$sc['MSG'] = _("Parse failed")."".$p->status_text."";
}else{
$sc['STATUS'] = TRUE;
$sc['MODE'] = "Structured";
$sc['MSG'] = _("Parse successful");
}
$sc['PARSER'] = $p;
$sc['EDITED'] = TRUE;
$sc['ACTIVE'] = FALSE;
$sc['NAME'] = $name;
/* Add script */
$this->scripts[$name] = $sc;
}else{
/* Display dialog to enter new script name */
$smarty = get_smarty();
$smarty->assign("NewScriptName",$name);
return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
}
}
/*************
* Handle several posts
*************/
$once = TRUE;
foreach($_POST as $name => $value){
/* Edit script requested */
if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
$script = preg_replace("/^editscript_/","",$name);
$script = preg_replace("/_(x|y)/","",$script);
$once = FALSE;
$this->current_script = $script;
$this->current_handler = $this->scripts[$script]['PARSER'];
$this->scripts[$script]['SCRIPT_BACKUP'] = $this->scripts[$script]['SCRIPT'];
}
/* remove script requested */
if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^delscript_/",$name) && $once && !$this->current_handler){
$script = preg_replace("/^delscript_/","",$name);
$script = preg_replace("/_(x|y)/","",$script);
$once = FALSE;
$this->script_to_delete = $script;
}
/* Activate script */
if($this->parent->acl_is_writeable("sieveManagement") && preg_match("/^active_script_/",$name) && $once && !$this->current_handler){
$script = preg_replace("/^active_script_/","",$name);
$script = preg_replace("/_(x|y)/","",$script);
$once = FALSE;
/* We can only activate existing scripts */
if(!$this->scripts[$script]['IS_NEW']){
/* Get sieve */
if(!$this->sieve_handle = $this->get_sieve()){
msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '
'.to_string($this->Sieve_Error).''), ERROR_DIALOG);
}
/* Try to activate the given script and update
* class script array.
*/
if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '
'.to_string($this->sieve_handler->error_raw).''), ERROR_DIALOG);
}else{
foreach($this->scripts as $key => $data){
if($key == $script){
$this->scripts[$key]['ACTIVE'] = TRUE;
}else{
$this->scripts[$key]['ACTIVE'] = FALSE;
}
}
}
}
}
}
/*************
* Remove script handling
*************/
/* Remove aborted */
if(isset($_POST['delete_cancel'])){
$this->script_to_delete = -1;
}
/* Remove confirmed */
if($this->parent->acl_is_writeable("sieveManagement") && isset($_POST['delete_script_confirm'])){
$script = $this->scripts[$this->script_to_delete];
/* Just remove from array if it is a new script */
if($script['IS_NEW']){
unset($this->scripts[$this->script_to_delete]);
}else{
/* Get sieve */
if(!$this->sieve_handle = $this->get_sieve()){
msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '