egroupware_official/mail/inc/class.mail_sieve.inc.php

1179 lines
29 KiB
PHP
Raw Normal View History

2013-07-25 17:48:18 +02:00
<?php
/**
* EGroupware - Mail - interface class
*
* @link http://www.egroupware.org
* @package mail
* @author Hadi Nategh [hn@stylite.de]
* @copyright (c) 2013 by Hadi Nategh <hn-AT-stylite.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
2014-04-03 14:31:52 +02:00
* @version $Id$
2013-07-25 17:48:18 +02:00
*/
include_once(EGW_INCLUDE_ROOT.'/etemplate/inc/class.etemplate.inc.php');
class mail_sieve
{
var $public_functions = array
(
'editVacation' => True,
'index' => True,
'edit' => True,
2013-08-09 19:47:53 +02:00
'editEmailNotification'=> True, // Added email notifications
2013-07-25 17:48:18 +02:00
);
/**
* Flag if we can do a timed vaction message, requires Cyrus Admin User/Pw to enable/disable via async service
*
* @var boolean
*/
var $timed_vacation;
//var $scriptName = 'felamimail';
/**
* @var emailadmin_sieve
*/
var $bosieve;
var $errorStack;
/**
* etemplate object for sieve index
*
* @var object
*/
2013-07-25 17:48:18 +02:00
var $tmpl;
/**
* etemplate object for sieve edit popup
*
* @var object
*/
2013-08-09 19:47:53 +02:00
var $etmpl;
/**
* etemplate object for Vacation
*
* @var object
*/
2013-08-09 19:47:53 +02:00
var $vtmpl;
/**
* etemplate object for email notification
*
* @var object
*/
2013-08-09 19:47:53 +02:00
var $eNotitmpl;
/**
* mail_bo object
*
* @var object
*/
2013-07-25 17:48:18 +02:00
var $mailbo;
2013-08-09 19:47:53 +02:00
var $extraAddr;
/**
* Current Identitiy
*
* @var String
*/
var $currentIdentity;
/**
* emailamdin_smpt object
*
* @var object
*/
var $smtp;
2013-07-25 17:48:18 +02:00
/**
* Constructor
*
*
2013-07-25 17:48:18 +02:00
*/
function __construct()
{
$this->displayCharset = translation::charset();
2013-07-25 17:48:18 +02:00
$profileID = 0;
$this->smtp = new emailadmin_smtp();
2013-07-25 17:48:18 +02:00
if (isset($GLOBALS['egw_info']['user']['preferences']['mail']['ActiveProfileID']))
{
$profileID = (int) $GLOBALS['egw_info']['user']['preferences']['mail']['ActiveProfileID'];
}
$this->mailbo = mail_bo::getInstance(false, $profileID, false,false);
2013-07-25 17:48:18 +02:00
$this->mailPreferences =& $this->mailbo->mailPreferences;
$this->mailConfig = config::read('mail');
$allIdentities = mail_bo::getAllIdentities();
$defaultIdentity = $this->mailbo->getDefaultIdentity();
$this->currentIdentity = $allIdentities[$defaultIdentity];
$this->currentIdentity['identity_string'] = mail_bo::generateIdentityString($allIdentities[$defaultIdentity],true);
2013-07-25 17:48:18 +02:00
$this->restoreSessionData();
$this->bosieve = $this->mailbo->icServer;
$this->timed_vacation = $this->bosieve->enableSieve && $this->bosieve->acc_imap_administration;
2013-07-25 17:48:18 +02:00
}
/**
* Sieve rules list
*
* @param {array} $content=null
* @param {string} $msg=null
2013-07-25 17:48:18 +02:00
*/
function index(array $content=null,$msg=null)
{
//Instantiate an etemplate_new object
$tmpl = new etemplate_new('mail.sieve.index');
2013-08-09 19:47:53 +02:00
if ($msg)
{
$content['msg'] = $msg;
}
if ($this->mailbo->icServer->enableSieve)
{
//Initializes the Grid contents
$content['rg']= $this->get_rows();
2013-07-25 17:48:18 +02:00
// Set content-menu actions
$tmpl->set_cell_attribute('rg', 'actions',$this->get_actions());
2013-07-25 17:48:18 +02:00
$sel_options = array(
'status' => array(
'ENABLED' => lang('Enabled'),
'DISABLED' => lang('Disabled'),
)
);
}
else
{
$content['msg'] = lang('error').':'.lang('Serverside Filterrules (Sieve) are not activated').'. '.lang('Please contact your Administrator to validate if your Server supports Serverside Filterrules, and how to enable them in EGroupware for your active Account (%1) with ID:%2.',$this->currentIdentity['identity_string'],$this->mailbo->profileID);
$content['hideIfSieveDisabled']='mail_DisplayNone';
}
$tmpl->exec('mail.mail_sieve.index',$content,$sel_options,array());
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
/**
* Email Notification Edit
*
*
* @param {array} $content
* @param {string} $msg
2013-08-09 19:47:53 +02:00
*/
function editEmailNotification($content=null, $msg='')
{
//Instantiate an etemplate_new object, representing sieve.emailNotification
$eNotitmpl = new etemplate_new('mail.sieve.emailNotification');
if ($this->mailbo->icServer->enableSieve)
2013-08-09 19:47:53 +02:00
{
$eNotification = $this->getEmailNotification();
2013-08-09 19:47:53 +02:00
if (!is_array($content))
2013-08-09 19:47:53 +02:00
{
$content = $eNotification;
2013-08-09 19:47:53 +02:00
if (!empty($eNotification['externalEmail']))
{
$content['externalEmail'] = explode(",",$eNotification['externalEmail']);
}
}
else
2013-08-09 19:47:53 +02:00
{
$this->restoreSessionData();
list($button) = @each($content['button']);
unset ($content['button']);
switch($button)
{
case 'save':
case 'apply':
if (isset($content['status']))
2013-08-09 19:47:53 +02:00
{
$newEmailNotification = $content;
2014-05-13 09:54:42 +02:00
if (empty($this->mailPreferences['prefpreventforwarding']) ||
$this->mailPreferences['prefpreventforwarding'] == 0 )
2013-08-09 19:47:53 +02:00
{
if (is_array($content['externalEmail']) && !empty($content['externalEmail']))
{
$newEmailNotification['externalEmail'] = implode(",",$content['externalEmail']);
}
2013-08-09 19:47:53 +02:00
}
}
if (isset($content['externalEmail']) && !empty($content['externalEmail']))
2013-08-09 19:47:53 +02:00
{
if (!$this->bosieve->setEmailNotification($this->scriptName, $newEmailNotification))
{
$msg = lang("email notification update failed")."<br />";
break;
}
else
{
$msg .= lang("email notification successfully updated!");
}
2013-08-09 19:47:53 +02:00
}
else
{
$msg .= lang('email notification update failed! You need to set an email address!');
break;
2013-08-09 19:47:53 +02:00
}
egw_framework::refresh_opener($msg, 'mail');
2014-05-13 09:54:42 +02:00
if ($button === 'apply')
{
break;
}
2013-08-09 19:47:53 +02:00
case 'cancel':
egw_framework::window_close();
common::egw_exit();
}
$this->saveSessionData();
2013-08-09 19:47:53 +02:00
}
$sel_options = array(
'status' => array(
'on' => lang('Active'),
'off' => lang('Deactive'),
),
'displaySubject' => array(
0 => lang('No'),
1 => lang('Yes'),
),
);
$content['msg'] = $msg;
}
else
{
$content['msg'] = lang('error').':'.lang('Serverside Filterrules (Sieve) are not activated').'. '.lang('Please contact your Administrator to validate if your Server supports Serverside Filterrules, and how to enable them in EGroupware for your active Account (%1) with ID:%2.',$this->currentIdentity['identity_string'],$this->mailbo->profileID);
$content['hideIfSieveDisabled']='mail_DisplayNone';
}
2013-08-09 19:47:53 +02:00
$eNotitmpl->exec('mail.mail_sieve.editEmailNotification', $content,$sel_options);
}
2013-07-25 17:48:18 +02:00
/**
* Sieve rules edit
*
*
* @param {array} $content=null
2013-07-25 17:48:18 +02:00
*/
function edit ($content=null)
{
//Instantiate an etemplate_new object, representing sieve.edit template
$etmpl = new etemplate_new('mail.sieve.edit');
2013-07-25 17:48:18 +02:00
if (!is_array($content))
{
if ( $this->getRules($_GET['ruleID']) && isset($_GET['ruleID']))
{
$rules = $this->rulesByID;
2013-08-09 19:47:53 +02:00
$content= $rules;
2013-07-25 17:48:18 +02:00
switch ($rules['action'])
{
case 'folder':
$content['action_folder_text'][] = translation::convert($rules['action_arg'],'utf-8','utf7-imap');
2013-08-09 19:47:53 +02:00
2013-07-25 17:48:18 +02:00
break;
case 'address':
2013-08-09 19:47:53 +02:00
$content['action_address_text'][] = $rules['action_arg'];
2013-07-25 17:48:18 +02:00
break;
case 'reject':
$content['action_reject_text'] = $rules['action_arg'];
}
}
else // Adding new rule
{
2013-08-09 19:47:53 +02:00
$this->getRules(null);
2013-07-25 17:48:18 +02:00
$newRulePriority = count($this->rules)*2+1;
$newRules ['priority'] = $newRulePriority;
$newRules ['status'] = 'ENABLED';
2013-08-09 19:47:53 +02:00
$readonlys = array(
'button[delete]' => 'true',
);
2013-07-25 17:48:18 +02:00
$this->rulesByID = $newRules;
2013-08-09 19:47:53 +02:00
$content = $this->rulesByID;
2013-07-25 17:48:18 +02:00
}
$this->saveSessionData();
}
else
{
$this->restoreSessionData();
list($button) = @each($content['button']);
2013-08-09 19:47:53 +02:00
//$ruleID is calculated by priority from the selected rule and is an unique ID
$ruleID = ($this->rulesByID['priority'] -1) / 2;
$error = 0;
2013-07-25 17:48:18 +02:00
switch ($button)
{
2013-08-09 19:47:53 +02:00
2013-07-25 17:48:18 +02:00
case 'save':
2013-08-09 19:47:53 +02:00
case 'apply':
2013-07-25 17:48:18 +02:00
if($content)
{
unset($content['button']);
2013-08-09 19:47:53 +02:00
2013-07-25 17:48:18 +02:00
$newRule = $content;
$newRule['priority'] = $this->rulesByID['priority'];
$newRule['status'] = $this->rulesByID['status'];
switch ($content['action'])
2013-07-25 17:48:18 +02:00
{
case 'folder':
$newRule['action_arg'] = translation::convert(implode($content['action_folder_text']), 'utf7-imap', 'utf-8');
2013-07-25 17:48:18 +02:00
break;
case 'address':
$newRule['action_arg'] = implode($content['action_address_text']);
//error_log(__METHOD__. '() newRules_address '. array2string($newRule['action_arg']));
2013-07-25 17:48:18 +02:00
break;
case 'reject':
$newRule['action_arg'] = $content['action_reject_text'];
}
unset($newRule['action_folder_text']);
unset($newRule['action_address_text']);
unset($newRule['action_reject_text']);
$newRule['flg'] = 0 ;
if( $newRule['continue'] ) { $newRule['flg'] += 1; }
if( $newRule['gthan'] ) { $newRule['flg'] += 2; }
if( $newRule['anyof'] ) { $newRule['flg'] += 4; }
if( $newRule['keep'] ) { $newRule['flg'] += 8; }
if( $newRule['regexp'] ) { $newRule['flg'] += 128; }
2013-07-25 17:48:18 +02:00
if($newRule['action'] && $this->rulesByID['priority'])
{
$this->rules[$ruleID] = $newRule;
$ret = $this->bosieve->setRules($this->scriptName, $this->rules);
if (!$ret && !empty($this->bosieve->error))
{
$msg .= lang("Saving the rule failed:")."<br />".$this->bosieve->error."<br />";
}
2013-08-09 19:47:53 +02:00
else
{
$msg .= lang("The rule with priority %1 successfully saved!",$ruleID);
}
2013-07-25 17:48:18 +02:00
$this->saveSessionData();
}
else
{
$msg .= "\n".lang("Error: Could not save rule").' '.lang("No action defined!");
$error++;
}
}
else
{
$msg .= "\n".lang("Error: Could not save rule").' '.lang("No action defined!");
$error++;
}
egw_framework::refresh_opener($msg, 'mail');
2014-05-13 09:54:42 +02:00
if ($button == "apply")
{
break;
}
2013-08-09 19:47:53 +02:00
//fall through
2013-07-25 17:48:18 +02:00
case 'delete':
2013-08-09 19:47:53 +02:00
if ($button == "delete")
2014-05-13 09:54:42 +02:00
{
$msg = $this->ajax_action(false,$button, $ruleID, $msg);
}
egw_framework::refresh_opener($msg, 'mail');
2013-08-09 19:47:53 +02:00
case 'cancel':
egw_framework::window_close();
2013-08-09 19:47:53 +02:00
common::egw_exit();
2013-07-25 17:48:18 +02:00
}
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
$sel_options = array(//array_merge($sel_options,array(
2013-07-25 17:48:18 +02:00
'anyof' => array(
0 => lang('all of'),
1 => lang('any of'),
),
'gthan' => array(
0 => lang('less than'),
1 => lang('greater than'),
),
'bodytransform' => array(
0 => 'raw',
1 => 'text',
),
'ctype' => emailadmin_script::$btransform_ctype_array,
);
//Set the preselect_options for mail/folders as we are not allow free entry for folder taglist
$mailCompose = new mail_compose();
$sel_options['action_folder_text'] = $mailCompose->ajax_searchFolder(0,true);
2013-07-25 17:48:18 +02:00
return $etmpl->exec('mail.mail_sieve.edit',$content,$sel_options,$readonlys,array(),2);
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
/**
* Read email notification script from the sieve script from server
*
*
2013-08-09 19:47:53 +02:00
* @return type, returns array of email notification data, and in case of failure returns false
*/
function getEmailNotification()
2013-07-25 17:48:18 +02:00
{
2014-05-13 09:54:42 +02:00
if(!(empty($this->mailPreferences['prefpreventnotificationformailviaemail']) || $this->mailPreferences['prefpreventnotificationformailviaemail'] == 0))
{
throw new egw_exception_no_permission();
}
2013-07-25 17:48:18 +02:00
2013-08-09 19:47:53 +02:00
if($this->bosieve->getScript($this->scriptName))
{
if(PEAR::isError($error = $this->bosieve->retrieveRules($this->scriptName)) )
{
$rules = array();
$emailNotification = array();
}
else
{
$rules = $this->bosieve->getRules($this->scriptName);
$emailNotification = $this->bosieve->getEmailNotification($this->scriptName);
}
}
else
{
// something went wrong
error_log(__METHOD__.__LINE__.' failed');
return false;
}
return $emailNotification;
}
/**
* Fetch Vacation rules and predefined Addresses from mailserver
*
* @param {array} $vacation
* @param {string} $msg
*
* @return {array} return multi-dimentional array of vacation and aliases
2013-08-09 19:47:53 +02:00
*/
function getVacation(&$vacation,&$msg)
{
//$response->call('app.mail.sieve_vac_response_addresses');
2014-05-13 09:54:42 +02:00
if(!(empty($this->mailPreferences['prefpreventabsentnotice']) || $this->mailPreferences['prefpreventabsentnotice'] == 0))
2013-07-25 17:48:18 +02:00
{
2014-05-13 09:54:42 +02:00
throw new egw_exception_no_permission();
2013-07-25 17:48:18 +02:00
}
2013-07-25 17:48:18 +02:00
if($this->bosieve->getScript($this->scriptName))
{
if(PEAR::isError($error = $this->bosieve->retrieveRules($this->scriptName)) )
{
$vacation = array();
}
else
{
$vacation = $this->bosieve->getVacation($this->scriptName);
}
}
else
{
// something went wrong
2013-08-09 19:47:53 +02:00
$msg = lang('Unable to fetch vacation!');
2013-07-25 17:48:18 +02:00
}
$allIdentities = mail_bo::getAllIdentities();
$accAllIdentities = $this->smtp->getAccountEmailAddress($GLOBALS['egw_info']['user']['account_lid']);
$accEmailAddresses = array();
foreach ($accAllIdentities as &$arrVal)
{
if ($arrVal['type'] !='default')
{
array_push($accEmailAddresses, $arrVal['address']);
}
}
2014-05-13 09:54:42 +02:00
foreach($allIdentities as &$singleIdentity)
2013-08-09 19:47:53 +02:00
{
$predefinedAddresses[$singleIdentity['ident_email']] = $singleIdentity['ident_email'];
2013-07-25 17:48:18 +02:00
}
$allAliases = array_unique(array_merge($predefinedAddresses,$accEmailAddresses));
asort($allAliases);
2013-08-09 19:47:53 +02:00
return array(
'vacation' =>$vacation,
'aliases' => array_values($allAliases),
'defaultProfile' => $allIdentities[$this->mailbo->getDefaultIdentity()]
);
2013-08-09 19:47:53 +02:00
}
/**
* Vacation edit
*
* @param {array} $content
* @param {string} $msg
2013-08-09 19:47:53 +02:00
*/
function editVacation($content=null, $msg='')
{
//Instantiate an etemplate_new object, representing the sieve.vacation template
$vtmpl = new etemplate_new('mail.sieve.vacation');
2014-05-13 09:54:42 +02:00
$vacation = array();
if ($this->mailbo->icServer->enableSieve)
2013-07-25 17:48:18 +02:00
{
$vacRules = $this->getVacation($vacation,$msg);
//$this->timed_vacation=true;//this could force the timed vacation thingy even if not supported ;-)
if ($this->timed_vacation)
{
$ByDate = array('by_date' => lang('By date'));
}
if (!is_array($content))
2013-07-25 17:48:18 +02:00
{
$content = $vacation = $vacRules['vacation'];
2014-05-13 09:54:42 +02:00
if (empty($vacation['addresses']))
{
$content['addresses']='';
}
if (!empty($vacation['forwards']))
{
$content['forwards'] = explode(",",$vacation['forwards']);
}
else
{
$content['forwards'] = '';
}
//Set default value for days new entry
if (empty($content['days']))
{
$content['days'] = '3';
}
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
else
2013-07-25 17:48:18 +02:00
{
$this->restoreSessionData();
list($button) = @each($content['button']);
unset ($content['button']);
2013-08-09 19:47:53 +02:00
switch($button)
{
case 'save':
case 'apply':
if ($GLOBALS['egw_info']['user']['apps']['admin'])
2013-08-09 19:47:53 +02:00
{
// store text as default
if ($content['set_as_default'] == 1)
{
config::save_value('default_vacation_text', $content['text'], 'mail');
}
2013-08-09 19:47:53 +02:00
}
if (isset($content['status']))
2013-08-09 19:47:53 +02:00
{
//error_log(__METHOD__. 'content:' . array2string($content));
$newVacation = $content;
2014-05-13 09:54:42 +02:00
if (empty($this->mailPreferences['prefpreventforwarding']) ||
$this->mailPreferences['prefpreventforwarding'] == 0 )
2013-08-09 19:47:53 +02:00
{
if (is_array($content['forwards']) && !empty($content['forwards']))
{
$newVacation['forwards'] = implode(",",$content['forwards']);
}
else
{
$newVacation ['forwards'] = '';
}
2013-08-09 19:47:53 +02:00
}
else
{
unset($newVacation ['forwards']);
2013-08-09 19:47:53 +02:00
}
2014-05-13 09:54:42 +02:00
if (!in_array($newVacation['status'],array('on','off','by_date')))
{
$newVacation['status'] = 'off';
}
2013-08-09 19:47:53 +02:00
$checkAddresses = (isset($content['check_mail_sent_to']) && ($content['check_mail_sent_to']) != 0) ? true: false;
if ($content['addresses'])
{
$newVacation ['addresses'] = $content['addresses'];
}
else
{
2013-08-09 19:47:53 +02:00
}
2013-08-09 19:47:53 +02:00
if($this->checkRule($newVacation,$checkAddresses))
2013-08-09 19:47:53 +02:00
{
if (!$this->bosieve->setVacation($this->scriptName, $newVacation))
{
$msg = lang('vacation update failed') . "\n" . lang('Vacation notice update failed') . ":" . $this->bosieve->error;
break;
}
else
{
2014-05-13 09:54:42 +02:00
if (!isset($newVacation['scriptName']) || empty($newVacation['scriptName']))
{
$newVacation['scriptName'] = $this->scriptName;
}
$this->bosieve->setAsyncJob($newVacation);
$msg = lang('Vacation notice sucessfully updated.');
}
2013-08-09 19:47:53 +02:00
}
else
{
$msg .= implode("\n",$this->errorStack);
2013-08-09 19:47:53 +02:00
}
// refresh vacationNotice on index
$response = egw_json_response::get();
$response->call('app.mail.mail_callRefreshVacationNotice',$this->mailbo->profileID);
2013-12-17 18:10:02 +01:00
egw_framework::refresh_opener($msg, 'mail','edit');
2014-05-13 09:54:42 +02:00
if ($button === 'apply' || $this->bosieve->error !=="")
{
break;
}
2013-08-09 19:47:53 +02:00
}
case 'cancel':
2013-12-17 18:10:02 +01:00
egw_framework::window_close();
}
}
2013-07-25 17:48:18 +02:00
$sel_options = array(
'status' => array(
'on' => lang('Active'),
'off' => lang('Deactive'),
),
'addresses' => array_combine($vacRules['aliases'],$vacRules['aliases']),
);
if (!empty($ByDate))
{
$sel_options['status'] += $ByDate;
}
$content['msg'] = $msg;
}
else
{
$content['msg'] = lang('error').':'.lang('Serverside Filterrules (Sieve) are not activated').'. '.lang('Please contact your Administrator to validate if your Server supports Serverside Filterrules, and how to enable them in EGroupware for your active Account (%1) with ID:%2.',$this->currentIdentity['identity_string'],$this->mailbo->profileID);
$content['hideIfSieveDisabled']='mail_DisplayNone';
}
$vtmpl->exec('mail.mail_sieve.editVacation',$content,$sel_options,array(),array(),2);
2013-08-09 19:47:53 +02:00
}
/**
* Checking vaction validation
*
* @param {array} $_vacation
* @param {boolean} $_checkAddresses
*
2013-08-09 19:47:53 +02:00
* @return boolean
*/
function checkRule($_vacation,$_checkAddresses=true)
{
$this->errorStack = array();
if (!$_vacation['text'])
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
$this->errorStack['text'] = lang('Please supply the message to send with auto-responses').'! ';
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
if (!$_vacation['days'])
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
$this->errorStack['days'] = lang('Please select the number of days to wait between responses').'!';
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
if(is_array($_vacation['addresses']) && !empty($_vacation['addresses']))
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
$regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
foreach ($_vacation['addresses'] as $addr)
{
if (!preg_match($regexp,$addr) && $_checkAddresses)
{
$this->errorStack['addresses'] = lang('One address is not valid').'!';
}
}
2013-07-25 17:48:18 +02:00
}
else
{
2013-08-09 19:47:53 +02:00
$this->errorStack['addresses'] = lang('Please select a address').'!';
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
if ($_vacation['status'] == 'by_date')
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
if (!$_vacation['start_date'] || !$_vacation['end_date'])
{
$this->errorStack['status'] = lang('Activating by date requires a start- AND end-date!');
}
elseif($_vacation['start_date'] > $_vacation['end_date'])
{
$this->errorStack['status'] = lang('Vacation start-date must be BEFORE the end-date!');
}
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
if ($_vacation['forwards'])
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
foreach(preg_split('/, ?/',$_vacation['forwards']) as $addr)
{
if (!preg_match($regexp,$addr) && $_checkAddresses)
{
$this->errorStack['forwards'] = lang('One address is not valid'.'!');
}
}
2013-07-25 17:48:18 +02:00
}
//error_log(__METHOD__. array2string($this->errorStack));
2013-08-09 19:47:53 +02:00
if(count($this->errorStack) == 0)
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
return true;
2013-07-25 17:48:18 +02:00
}
else
{
2013-08-09 19:47:53 +02:00
$this->errorStack['message'] = lang('Vacation notice is not saved yet! (But we filled in some defaults to cover some of the above errors. Please correct and check your settings and save again.)');
return false;
2013-07-25 17:48:18 +02:00
}
}
/**
* Move rule to an other position in list
*
* @param {array} $orders
2013-07-25 17:48:18 +02:00
*/
function ajax_moveRule($orders)
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
2014-05-13 09:54:42 +02:00
foreach ($orders as $keys => $val)
{
$orders[$keys] = $val -1;
2014-05-13 09:54:42 +02:00
}
2013-07-25 17:48:18 +02:00
$this->getRules(null);
$newrules = $this->rules;
2014-05-13 09:54:42 +02:00
2013-07-25 17:48:18 +02:00
foreach($orders as $keys => $ruleID)
{
$newrules[$keys] = $this->rules[$ruleID];
}
2013-08-09 19:47:53 +02:00
$this->rules = $newrules;
2013-07-25 17:48:18 +02:00
$this->updateScript();
$this->saveSessionData();
2013-08-09 19:47:53 +02:00
//Calling to referesh after move action
$response = egw_json_response::get();
2014-05-13 09:54:42 +02:00
$response->call('app.mail.sieve_refresh');
2013-08-09 19:47:53 +02:00
}
/**
* Ajax function to handle the server side content for refreshing the form
*
* @param type $execId,
*
*/
function ajax_sieve_egw_refresh($execId)
2013-08-09 19:47:53 +02:00
{
//Need to read the template to use for refreshing the form
$response = egw_json_response::get();
$response->generic('assign',array(
'etemplate_exec_id' => $execId,
'id' => 'rg',
'key' => 'value',
2014-05-13 09:54:42 +02:00
'value' => array('content' => $this->get_rows())
2013-08-09 19:47:53 +02:00
));
//error_log(__METHOD__. "RESPONSE".array2string($response));
2013-08-09 19:47:53 +02:00
}
/**
* Ajax function to handle actions over sieve rules list on gd
2013-07-25 17:48:18 +02:00
*
* @param {int|boolean} $exec_id template unique Id | false if we only wants to use serverside actions
* @param {string} $actions name of action
* @param {string} $checked the selected rule id
* @param {string} $msg containing the message comming from the client-side
*
2013-07-25 17:48:18 +02:00
*/
function ajax_action($exec_id, $action,$checked,$msg)
2013-07-25 17:48:18 +02:00
{
2013-08-09 19:47:53 +02:00
$this->getRules(null);
2013-07-25 17:48:18 +02:00
switch ($action)
{
case 'delete':
2013-08-09 19:47:53 +02:00
if ($checked === count($this->rules)-1)
{
$msg = lang('rule with priority ') . $checked . lang(' deleted!');
}else
{
$msg = lang('rule with priority ') . $checked . lang(' deleted!') . lang(' And the rule with priority %1, now got the priority %2',$checked+1,$checked);
}
2013-07-25 17:48:18 +02:00
unset($this->rules[$checked]);
$this->rules = array_values($this->rules);
break;
case 'enable':
2013-08-09 19:47:53 +02:00
$msg = lang('rule with priority ') . $checked . lang(' enabled!');
2013-07-25 17:48:18 +02:00
$this->rules[$checked][status] = 'ENABLED';
break;
case 'disable':
2013-08-09 19:47:53 +02:00
$msg = lang('rule with priority ') . $checked . lang(' disabled!');
2013-07-25 17:48:18 +02:00
$this->rules[$checked][status] = 'DISABLED';
break;
2013-08-09 19:47:53 +02:00
case 'move':
break;
2013-07-25 17:48:18 +02:00
}
2013-08-09 19:47:53 +02:00
ob_start();
$result = $this->updateScript();
if($result)
{
egw_json_response::get()->error($result);
return;
}
2013-07-25 17:48:18 +02:00
$this->saveSessionData();
//Refresh the grid
if ($exec_id)
{
$this->ajax_sieve_egw_refresh($exec_id,$msg);
}
else
{
return $msg;
}
2013-08-09 19:47:53 +02:00
}
2013-07-25 17:48:18 +02:00
/**
2013-08-09 19:47:53 +02:00
* Convert an script seive format rule to human readable format
2013-07-25 17:48:18 +02:00
*
* @param {array} $rule Array of rules
* @return {string} return the rule as a string.
2013-07-25 17:48:18 +02:00
*/
function buildRule($rule)
{
$andor = ' '. lang('and') .' ';
$started = 0;
2014-05-13 09:54:42 +02:00
if ($rule['anyof'])
{
$andor = ' '. lang('or') .' ';
}
2013-07-25 17:48:18 +02:00
$complete = lang('IF').' ';
2014-05-13 09:54:42 +02:00
if ($rule['unconditional'])
{
$complete = "[Unconditional] ";
}
2013-07-25 17:48:18 +02:00
if ($rule['from'])
{
$match = $this->setMatchType($rule['from'],$rule['regexp']);
$complete .= "'From:' " . $match . " '" . $rule['from'] . "'";
$started = 1;
}
if ($rule['to'])
{
2014-05-13 09:54:42 +02:00
if ($started)
{
$complete .= $andor;
}
2013-07-25 17:48:18 +02:00
$match = $this->setMatchType($rule['to'],$rule['regexp']);
$complete .= "'To:' " . $match . " '" . $rule['to'] . "'";
$started = 1;
}
if ($rule['subject'])
{
2014-05-13 09:54:42 +02:00
if ($started)
{
$complete .= $andor;
}
2013-07-25 17:48:18 +02:00
$match = $this->setMatchType($rule['subject'],$rule['regexp']);
$complete .= "'Subject:' " . $match . " '" . $rule['subject'] . "'";
$started = 1;
}
if ($rule['field'] && $rule['field_val'])
{
2014-05-13 09:54:42 +02:00
if ($started)
{
$complete .= $andor;
}
2013-07-25 17:48:18 +02:00
$match = $this->setMatchType($rule['field_val'],$rule['regexp']);
$complete .= "'" . $rule['field'] . "' " . $match . " '" . $rule['field_val'] . "'";
$started = 1;
}
if ($rule['size'])
{
$xthan = " less than '";
2014-05-13 09:54:42 +02:00
if ($rule['gthan'])
{
$xthan = " greater than '";
}
if ($started)
{
$complete .= $andor;
}
2013-07-25 17:48:18 +02:00
$complete .= "message " . $xthan . $rule['size'] . "KB'";
$started = 1;
}
if (!empty($rule['field_bodytransform']))
2013-08-09 19:47:53 +02:00
{
2014-05-13 09:54:42 +02:00
if ($started)
{
$newruletext .= ", ";
}
2013-08-09 19:47:53 +02:00
$btransform = " :raw ";
$match = ' :contains';
2014-05-13 09:54:42 +02:00
if ($rule['bodytransform'])
{
$btransform = " :text ";
}
if (preg_match("/\*|\?/", $rule['field_bodytransform']))
{
$match = ':matches';
}
if ($rule['regexp'])
{
$match = ':regex';
}
2013-08-09 19:47:53 +02:00
$complete .= " body " . $btransform . $match . " \"" . $rule['field_bodytransform'] . "\"";
$started = 1;
}
if ($rule['ctype']!= '0' && !empty($rule['ctype']))
2013-08-09 19:47:53 +02:00
{
2014-05-13 09:54:42 +02:00
if ($started)
{
$newruletext .= ", ";
}
2013-08-09 19:47:53 +02:00
$btransform_ctype = emailadmin_script::$btransform_ctype_array[$rule['ctype']];
$ctype_subtype = "";
2014-05-13 09:54:42 +02:00
if ($rule['field_ctype_val'])
{
$ctype_subtype = "/";
}
2013-08-09 19:47:53 +02:00
$complete .= " body :content " . " \"" . $btransform_ctype . $ctype_subtype . $rule['field_ctype_val'] . "\"" . " :contains \"\"";
$started = 1;
//error_log(__CLASS__."::".__METHOD__.array2string(emailadmin_script::$btransform_ctype_array));
}
2014-05-13 09:54:42 +02:00
if (!$rule['unconditional'])
{
$complete .= ' '.lang('THEN').' ';
}
2013-07-25 17:48:18 +02:00
if (preg_match("/folder/i",$rule['action']))
2014-05-13 09:54:42 +02:00
{
2013-07-25 17:48:18 +02:00
$complete .= lang('file into')." '" . $rule['action_arg'] . "';";
2014-05-13 09:54:42 +02:00
}
2013-07-25 17:48:18 +02:00
if (preg_match("/reject/i",$rule['action']))
2014-05-13 09:54:42 +02:00
{
2013-07-25 17:48:18 +02:00
$complete .= lang('reject with')." '" . $rule['action_arg'] . "'.";
2014-05-13 09:54:42 +02:00
}
2013-07-25 17:48:18 +02:00
if (preg_match("/address/i",$rule['action']))
2014-05-13 09:54:42 +02:00
{
2013-07-25 17:48:18 +02:00
$complete .= lang('forward to').' ' . $rule['action_arg'] .'.';
2014-05-13 09:54:42 +02:00
}
2013-07-25 17:48:18 +02:00
if (preg_match("/discard/i",$rule['action']))
2014-05-13 09:54:42 +02:00
{
2013-07-25 17:48:18 +02:00
$complete .= lang('discard').'.';
2014-05-13 09:54:42 +02:00
}
if ($rule['continue'])
{
$complete .= " [Continue]";
}
if ($rule['keep'])
{
$complete .= " [Keep a copy]";
}
2013-07-25 17:48:18 +02:00
return $complete;
}
/**
* Helper function to find the type of content
2013-07-25 17:48:18 +02:00
*
* @param {string} $matchstr string that should be compared
* @param {string} $regex regular expresion as pattern to be matched
* @return {string} return the type
2013-07-25 17:48:18 +02:00
*/
function setMatchType (&$matchstr, $regex = false)
{
$match = lang('contains');
if (preg_match("/^\s*!/", $matchstr))
2014-05-13 09:54:42 +02:00
{
$match = lang('does not contain');
}
if (preg_match("/\*|\?/", $matchstr))
{
$match = lang('matches');
if (preg_match("/^\s*!/", $matchstr))
{
2014-05-13 09:54:42 +02:00
$match = lang('does not match');
}
2014-05-13 09:54:42 +02:00
}
if ($regex)
2013-07-25 17:48:18 +02:00
{
2014-05-13 09:54:42 +02:00
$match = lang('matches regexp');
if (preg_match("/^\s*!/", $matchstr))
2013-07-25 17:48:18 +02:00
{
2014-05-13 09:54:42 +02:00
$match = lang('does not match regexp');
2013-07-25 17:48:18 +02:00
}
}
2014-05-13 09:54:42 +02:00
if ($regex && preg_match("/^\s*\\\\!/", $matchstr))
{
$matchstr = preg_replace("/^\s*\\\\!/","!",$matchstr);
}
else
{
$matchstr = preg_replace("/^\s*!/","",$matchstr);
}
return $match;
2013-07-25 17:48:18 +02:00
}
/**
2013-08-09 19:47:53 +02:00
* Save session data
*
*
2013-07-25 17:48:18 +02:00
*/
function saveSessionData()
{
$sessionData['sieve_rules'] = $this->rules;
$sessionData['sieve_rulesByID'] = $this->rulesByID;
$sessionData['sieve_scriptToEdit'] = $this->scriptToEdit;
$GLOBALS['egw']->session->appsession('sieve_session_data','',$sessionData);
}
/**
2013-08-09 19:47:53 +02:00
* Update the sieve script on mail server
*
*
2013-07-25 17:48:18 +02:00
*/
function updateScript()
{
if (!$this->bosieve->setRules($this->scriptToEdit, $this->rules))
{
return "update failed";
2013-07-25 17:48:18 +02:00
}
}
/**
* getRules()
* Fetched rules save on array()rules.
*
* @param {string} $ruleID Numeric Id of the rule if specify return the specitic rule| otherwise ruleByID would be null
*
* @return {boolean} returns false in case of failure and true in case of success.
2013-07-25 17:48:18 +02:00
*/
function getRules($ruleID = null)
2013-07-25 17:48:18 +02:00
{
if(($script = $this->bosieve->getScript($this->scriptName)))
2013-07-25 17:48:18 +02:00
{
$this->scriptToEdit = $this->scriptName;
if(PEAR::isError($error = $this->bosieve->retrieveRules($this->scriptName)) )
{
error_log(__METHOD__.__LINE__.$error->message);
$this->rules = array();
$this->rulesByID = array();
$this->vacation = array();
}
else
{
$this->rules = $this->bosieve->getRules($this->scriptName);
$this->rulesByID = $this->rules[$ruleID];
$this->vacation = $this->bosieve->getVacation($this->scriptName);
}
return true;
}
else
{
// something went wrong
error_log(__METHOD__.__LINE__.' failed');
return false;
}
//error_log(__METHOD__.array2string( $script));
}
/**
2013-08-09 19:47:53 +02:00
* Restore session data
*
2013-07-25 17:48:18 +02:00
*/
function restoreSessionData()
{
$sessionData = $GLOBALS['egw']->session->appsession('sieve_session_data');
$this->rules = $sessionData['sieve_rules'];
$this->rulesByID = $sessionData['sieve_rulesByID'];
$this->scriptToEdit = $sessionData['sieve_scriptToEdit'];
}
/**
*
2013-08-09 19:47:53 +02:00
* Get the data for iterating the rows on rules list grid
*
* @return {boolean|array} Array of rows | false if failed
2013-07-25 17:48:18 +02:00
*/
function get_rows()
2013-07-25 17:48:18 +02:00
{
$rows = array();
$this->getRules(null);
2013-07-25 17:48:18 +02:00
if (is_array($this->rules) && !empty($this->rules) )
{
$rows = $this->rules;
foreach ($rows as &$row )
{
$row['rules'] = $this->buildRule($row);
$row['ruleID'] =(string)(($row['priority'] -1) / 2 );
if ($row ['status'] === 'DISABLED')
2013-07-25 17:48:18 +02:00
{
$row['class'] = 'mail_sieve_DISABLED';
2013-07-25 17:48:18 +02:00
}
}
2013-07-25 17:48:18 +02:00
}else
{
//error_log(__METHOD__.'There are no rules or something is went wrong at getRules()!');
return false;
2013-07-25 17:48:18 +02:00
}
// Shift one down, because in grid the first row is reserved for header
2013-07-25 17:48:18 +02:00
array_unshift($rows,array(''=> ''));
return $rows;
}
/**
* Get actions / context menu for index
*
* @return {array} returns defined actions as an array
2013-07-25 17:48:18 +02:00
*/
private function get_actions()
2013-07-25 17:48:18 +02:00
{
$actions =array(
'edit' => array(
'caption' => 'Edit',
'default' => true,
'onExecute' => 'javaScript:app.mail.action',
'disableClass' => 'th'
2013-07-25 17:48:18 +02:00
),
'add' => array(
'caption' => 'Add',
'onExecute' => 'javaScript:app.mail.action'
),
'enable' => array(
'caption' => 'Enable',
'onExecute' => 'javaScript:app.mail.action',
'enableClass' => 'mail_sieve_DISABLED',
'hideOnDisabled' => true
2013-07-25 17:48:18 +02:00
),
'disable' => array(
'caption' => 'Disable',
'onExecute' => 'javaScript:app.mail.action',
'disableClass' => 'mail_sieve_DISABLED',
'hideOnDisabled' => true
2013-07-25 17:48:18 +02:00
),
'delete' => array(
'caption' => 'Delete',
'onExecute' => 'javaScript:app.mail.action'
),
);
return $actions;
}
}
2014-05-13 09:54:42 +02:00