mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-11-03 19:08:50 +03:00
Split collectionToFileString() from SieveStorage to Sieve
Converted SieveStorage fileStringToCollection() to JavaScript sieveScriptToFilters() Drop the old filtersSave()
This commit is contained in:
parent
d9118fbf90
commit
dd9f277ccf
7 changed files with 509 additions and 554 deletions
|
|
@ -41,41 +41,6 @@ trait Filters
|
|||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFiltersSave() : array
|
||||
{
|
||||
$oAccount = $this->getAccountFromToken();
|
||||
|
||||
if (!$this->GetCapa(false, false, Capa::FILTERS, $oAccount))
|
||||
{
|
||||
return $this->FalseResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
$aIncFilters = $this->GetActionParam('Filters', array());
|
||||
|
||||
$sRaw = $this->GetActionParam('Raw', '');
|
||||
$bRawIsActive = '1' === (string) $this->GetActionParam('RawIsActive', '0');
|
||||
|
||||
$aFilters = array();
|
||||
foreach ($aIncFilters as $aFilter)
|
||||
{
|
||||
if (\is_array($aFilter))
|
||||
{
|
||||
$oFilter = new \RainLoop\Providers\Filters\Classes\Filter();
|
||||
if ($oFilter->FromJSON($aFilter))
|
||||
{
|
||||
$aFilters[] = $oFilter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->Plugins()
|
||||
->RunHook('filter.filters-save', array($oAccount, &$aFilters, &$sRaw, &$bRawIsActive))
|
||||
;
|
||||
|
||||
return $this->DefaultResponse(__FUNCTION__, $this->FiltersProvider()->Save($oAccount,
|
||||
$aFilters, $sRaw, $bRawIsActive));
|
||||
}
|
||||
|
||||
public function DoFiltersScriptSave() : array
|
||||
{
|
||||
$oAccount = $this->getAccountFromToken();
|
||||
|
|
@ -108,6 +73,9 @@ trait Filters
|
|||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFiltersScriptActivate() : array
|
||||
{
|
||||
$oAccount = $this->getAccountFromToken();
|
||||
|
|
@ -121,6 +89,9 @@ trait Filters
|
|||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFiltersScriptDelete() : array
|
||||
{
|
||||
$oAccount = $this->getAccountFromToken();
|
||||
|
|
|
|||
|
|
@ -179,25 +179,25 @@ class Filter implements \JsonSerializable
|
|||
|
||||
public function FromJSON(array $aFilter) : bool
|
||||
{
|
||||
$this->sID = isset($aFilter['ID']) ? $aFilter['ID'] : '';
|
||||
$this->sName = isset($aFilter['Name']) ? $aFilter['Name'] : '';
|
||||
$this->sID = $aFilter['ID'] ?? '';
|
||||
$this->sName = $aFilter['Name'] ?? '';
|
||||
|
||||
$this->bEnabled = isset($aFilter['Enabled']) ? '1' === (string) $aFilter['Enabled'] : true;
|
||||
$this->bEnabled = !isset($aFilter['Enabled']) || !empty($aFilter['Enabled']);
|
||||
|
||||
$this->sConditionsType = isset($aFilter['ConditionsType']) ? $aFilter['ConditionsType'] :
|
||||
\RainLoop\Providers\Filters\Enumerations\ConditionsType::ANY;
|
||||
$this->sConditionsType = $aFilter['ConditionsType']
|
||||
?? \RainLoop\Providers\Filters\Enumerations\ConditionsType::ANY;
|
||||
|
||||
$this->sActionType = isset($aFilter['ActionType']) ? $aFilter['ActionType'] :
|
||||
\RainLoop\Providers\Filters\Enumerations\ActionType::MOVE_TO;
|
||||
$this->sActionType = $aFilter['ActionType']
|
||||
?? \RainLoop\Providers\Filters\Enumerations\ActionType::MOVE_TO;
|
||||
|
||||
$this->sActionValue = isset($aFilter['ActionValue']) ? $aFilter['ActionValue'] : '';
|
||||
$this->sActionValueSecond = isset($aFilter['ActionValueSecond']) ? $aFilter['ActionValueSecond'] : '';
|
||||
$this->sActionValueThird = isset($aFilter['ActionValueThird']) ? $aFilter['ActionValueThird'] : '';
|
||||
$this->sActionValueFourth = isset($aFilter['ActionValueFourth']) ? $aFilter['ActionValueFourth'] : '';
|
||||
$this->sActionValue = $aFilter['ActionValue'] ?? '';
|
||||
$this->sActionValueSecond = $aFilter['ActionValueSecond'] ?? '';
|
||||
$this->sActionValueThird = $aFilter['ActionValueThird'] ?? '';
|
||||
$this->sActionValueFourth = $aFilter['ActionValueFourth'] ?? '';
|
||||
|
||||
$this->bKeep = isset($aFilter['Keep']) ? '1' === (string) $aFilter['Keep'] : true;
|
||||
$this->bStop = isset($aFilter['Stop']) ? '1' === (string) $aFilter['Stop'] : true;
|
||||
$this->bMarkAsRead = isset($aFilter['MarkAsRead']) ? '1' === (string) $aFilter['MarkAsRead'] : false;
|
||||
$this->bKeep = !isset($aFilter['Keep']) || !empty($aFilter['Keep']);
|
||||
$this->bStop = !isset($aFilter['Stop']) || !empty($aFilter['Stop']);
|
||||
$this->bMarkAsRead = !isset($aFilter['MarkAsRead']) || !empty($aFilter['MarkAsRead']);
|
||||
|
||||
$this->aConditions = empty($aFilter['Conditions']) ? array() : FilterCondition::CollectionFromJSON($aFilter['Conditions']);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,393 @@
|
|||
<?php
|
||||
|
||||
namespace RainLoop\Providers\Filters;
|
||||
|
||||
class Sieve
|
||||
{
|
||||
const NEW_LINE = "\r\n";
|
||||
|
||||
public static $bUtf8FolderName = true;
|
||||
|
||||
public static function collectionToFileString(array $aFilters) : string
|
||||
{
|
||||
$sNL = static::NEW_LINE;
|
||||
|
||||
$aCapa = array();
|
||||
$aParts = [
|
||||
'# This is SnappyMail sieve script.',
|
||||
'# Please don\'t change anything here.',
|
||||
'# RAINLOOP:SIEVE',
|
||||
''
|
||||
];
|
||||
|
||||
foreach ($aFilters as /* @var $oItem \RainLoop\Providers\Filters\Classes\Filter */ $oItem)
|
||||
{
|
||||
$aParts[] = \implode($sNL, [
|
||||
'/*',
|
||||
'BEGIN:FILTER:'.$oItem->ID(),
|
||||
'BEGIN:HEADER',
|
||||
\chunk_split(\base64_encode(\json_encode($oItem)), 74, $sNL).'END:HEADER',
|
||||
'*/',
|
||||
$oItem->Enabled() ? '' : '/* @Filter is disabled ',
|
||||
static::filterToSieveScript($oItem, $aCapa),
|
||||
$oItem->Enabled() ? '' : '*/',
|
||||
'/* END:FILTER */',
|
||||
''
|
||||
]);
|
||||
}
|
||||
|
||||
$aCapa = \array_keys($aCapa);
|
||||
$sCapa = \count($aCapa)
|
||||
? $sNL . 'require ' . \str_replace('","', '", "', \json_encode($aCapa)).';' . $sNL
|
||||
: '';
|
||||
|
||||
return $sCapa . $sNL . \implode($sNL, $aParts);
|
||||
}
|
||||
|
||||
public static function fileStringToCollection(string $sFileString) : array
|
||||
{
|
||||
$aResult = array();
|
||||
if (!empty($sFileString) && false !== \strpos($sFileString, 'RAINLOOP:SIEVE'))
|
||||
{
|
||||
$aMatch = array();
|
||||
if (\preg_match_all('/BEGIN:FILTER(.+?)BEGIN:HEADER(.+?)END:HEADER/s', $sFileString, $aMatch) &&
|
||||
isset($aMatch[2]) && \is_array($aMatch[2]))
|
||||
{
|
||||
foreach ($aMatch[2] as $sEncodedLine)
|
||||
{
|
||||
if (!empty($sEncodedLine))
|
||||
{
|
||||
$sDecodedLine = \base64_decode(\preg_replace('/\\s+/s', '', $sEncodedLine));
|
||||
if (!empty($sDecodedLine))
|
||||
{
|
||||
$oItem = new Classes\Filter();
|
||||
if ($oItem && $oItem->unserializeFromJson($sDecodedLine))
|
||||
{
|
||||
$aResult[] = $oItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
private static function conditionToSieveScript(Classes\FilterCondition $oCondition, array &$aCapa) : string
|
||||
{
|
||||
$sResult = '';
|
||||
$sTypeWord = '';
|
||||
$bTrue = true;
|
||||
|
||||
$sValue = \trim($oCondition->Value());
|
||||
$sValueSecond = \trim($oCondition->ValueSecond());
|
||||
|
||||
if (0 < \strlen($sValue) ||
|
||||
(0 < \strlen($sValue) && 0 < \strlen($sValueSecond) &&
|
||||
Enumerations\ConditionField::HEADER === $oCondition->Field()))
|
||||
{
|
||||
switch ($oCondition->Type())
|
||||
{
|
||||
case Enumerations\ConditionType::TEXT:
|
||||
case Enumerations\ConditionType::RAW:
|
||||
case Enumerations\ConditionType::OVER:
|
||||
case Enumerations\ConditionType::UNDER:
|
||||
$sTypeWord = ':' . \strtolower($oCondition->Type());
|
||||
break;
|
||||
case Enumerations\ConditionType::NOT_EQUAL_TO:
|
||||
$sResult .= 'not ';
|
||||
case Enumerations\ConditionType::EQUAL_TO:
|
||||
$sTypeWord = ':is';
|
||||
break;
|
||||
case Enumerations\ConditionType::NOT_CONTAINS:
|
||||
$sResult .= 'not ';
|
||||
case Enumerations\ConditionType::CONTAINS:
|
||||
$sTypeWord = ':contains';
|
||||
break;
|
||||
case Enumerations\ConditionType::REGEX:
|
||||
$sTypeWord = ':regex';
|
||||
$aCapa['regex'] = true;
|
||||
break;
|
||||
default:
|
||||
$bTrue = false;
|
||||
$sResult = '/* @Error: unknown type value */ false';
|
||||
break;
|
||||
}
|
||||
|
||||
switch ($oCondition->Field())
|
||||
{
|
||||
case Enumerations\ConditionField::FROM:
|
||||
$sResult .= 'header '.$sTypeWord.' ["From"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::RECIPIENT:
|
||||
$sResult .= 'header '.$sTypeWord.' ["To", "CC"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::SUBJECT:
|
||||
$sResult .= 'header '.$sTypeWord.' ["Subject"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::HEADER:
|
||||
$sResult .= 'header '.$sTypeWord.' ["'.static::quote($sValueSecond).'"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::BODY:
|
||||
// :text :raw :content
|
||||
$sResult .= 'body '.$sTypeWord.' :contains';
|
||||
$aCapa['body'] = true;
|
||||
break;
|
||||
case Enumerations\ConditionField::SIZE:
|
||||
$sResult .= 'size '.$sTypeWord;
|
||||
break;
|
||||
default:
|
||||
$bTrue = false;
|
||||
$sResult = '/* @Error: unknown field value */ false';
|
||||
break;
|
||||
}
|
||||
|
||||
if ($bTrue)
|
||||
{
|
||||
if (\in_array($oCondition->Field(), array(
|
||||
Enumerations\ConditionField::FROM,
|
||||
Enumerations\ConditionField::RECIPIENT
|
||||
)) && false !== \strpos($sValue, ','))
|
||||
{
|
||||
$aValue = \array_map(function ($sValue) use ($self) {
|
||||
return '"'.static::quote(\trim($sValue)).'"';
|
||||
}, \explode(',', $sValue));
|
||||
|
||||
$sResult .= ' ['.\trim(\implode(', ', $aValue)).']';
|
||||
}
|
||||
else if (Enumerations\ConditionField::SIZE === $oCondition->Field())
|
||||
{
|
||||
$sResult .= ' '.static::quote($sValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
$sResult .= ' "'.static::quote($sValue).'"';
|
||||
}
|
||||
|
||||
$sResult = \MailSo\Base\Utils::StripSpaces($sResult);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$sResult = '/* @Error: empty condition value */ false';
|
||||
}
|
||||
|
||||
return $sResult;
|
||||
}
|
||||
|
||||
private static function filterToSieveScript(Classes\Filter $oFilter, array &$aCapa) : string
|
||||
{
|
||||
$sNL = static::NEW_LINE;
|
||||
$sTab = ' ';
|
||||
|
||||
$bBlock = true;
|
||||
$aResult = array();
|
||||
|
||||
// Conditions
|
||||
$aConditions = $oFilter->Conditions();
|
||||
if (1 < \count($aConditions))
|
||||
{
|
||||
if (Enumerations\ConditionsType::ANY ===
|
||||
$oFilter->ConditionsType())
|
||||
{
|
||||
$aResult[] = 'if anyof(';
|
||||
|
||||
$bTrim = false;
|
||||
foreach ($aConditions as $oCond)
|
||||
{
|
||||
$bTrim = true;
|
||||
$sCons = static::conditionToSieveScript($oCond, $aCapa);
|
||||
if (!empty($sCons))
|
||||
{
|
||||
$aResult[] = $sTab.$sCons.',';
|
||||
}
|
||||
}
|
||||
if ($bTrim)
|
||||
{
|
||||
$aResult[\count($aResult) - 1] = \rtrim($aResult[\count($aResult) - 1], ',');
|
||||
}
|
||||
|
||||
$aResult[] = ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = 'if allof(';
|
||||
foreach ($aConditions as $oCond)
|
||||
{
|
||||
$aResult[] = $sTab . static::conditionToSieveScript($oCond, $aCapa) . ',';
|
||||
}
|
||||
|
||||
$aResult[\count($aResult) - 1] = \rtrim($aResult[\count($aResult) - 1], ',');
|
||||
$aResult[] = ')';
|
||||
}
|
||||
}
|
||||
else if (1 === \count($aConditions))
|
||||
{
|
||||
$aResult[] = 'if ' . static::conditionToSieveScript($aConditions[0], $aCapa);
|
||||
}
|
||||
else
|
||||
{
|
||||
$bBlock = false;
|
||||
}
|
||||
|
||||
// actions
|
||||
if ($bBlock)
|
||||
{
|
||||
$aResult[] = '{';
|
||||
}
|
||||
else
|
||||
{
|
||||
$sTab = '';
|
||||
}
|
||||
|
||||
if ($oFilter->MarkAsRead() && \in_array($oFilter->ActionType(), array(
|
||||
Enumerations\ActionType::NONE,
|
||||
Enumerations\ActionType::MOVE_TO,
|
||||
Enumerations\ActionType::FORWARD
|
||||
)))
|
||||
{
|
||||
$aCapa['imap4flags'] = true;
|
||||
$aResult[] = $sTab.'addflag "\\\\Seen";';
|
||||
}
|
||||
|
||||
switch ($oFilter->ActionType())
|
||||
{
|
||||
case Enumerations\ActionType::NONE:
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::DISCARD:
|
||||
$aResult[] = $sTab.'discard;';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::VACATION:
|
||||
$sValue = \trim($oFilter->ActionValue());
|
||||
$sValueSecond = \trim($oFilter->ActionValueSecond());
|
||||
$sValueThird = \trim($oFilter->ActionValueThird());
|
||||
$sValueFourth = \trim($oFilter->ActionValueFourth());
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
$aCapa['vacation'] = true;
|
||||
|
||||
$iDays = 1;
|
||||
$sSubject = '';
|
||||
if (0 < \strlen($sValueSecond))
|
||||
{
|
||||
$sSubject = ':subject "'.
|
||||
static::quote(\MailSo\Base\Utils::StripSpaces($sValueSecond)).'" ';
|
||||
}
|
||||
|
||||
if (0 < \strlen($sValueThird) && \is_numeric($sValueThird) && 1 < (int) $sValueThird)
|
||||
{
|
||||
$iDays = (int) $sValueThird;
|
||||
}
|
||||
|
||||
$sAddresses = '';
|
||||
if (0 < \strlen($sValueFourth))
|
||||
{
|
||||
$aAddresses = \explode(',', $sValueFourth);
|
||||
$aAddresses = \array_filter(\array_map(function ($sEmail) use ($self) {
|
||||
$sEmail = \trim($sEmail);
|
||||
return 0 < \strlen($sEmail) ? '"'.static::quote($sEmail).'"' : '';
|
||||
}, $aAddresses), 'strlen');
|
||||
|
||||
if (0 < \count($aAddresses))
|
||||
{
|
||||
$sAddresses = ':addresses ['.\implode(', ', $aAddresses).'] ';
|
||||
}
|
||||
}
|
||||
|
||||
$aResult[] = $sTab.'vacation :days '.$iDays.' '.$sAddresses.$sSubject.'"'.static::quote($sValue).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (vacation): empty action value';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::REJECT:
|
||||
$sValue = \trim($oFilter->ActionValue());
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
$aCapa['reject'] = true;
|
||||
|
||||
$aResult[] = $sTab.'reject "'.static::quote($sValue).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (reject): empty action value';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::FORWARD:
|
||||
$sValue = $oFilter->ActionValue();
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
if ($oFilter->Keep())
|
||||
{
|
||||
$aCapa['fileinto'] = true;
|
||||
$aResult[] = $sTab.'fileinto "INBOX";';
|
||||
}
|
||||
|
||||
$aResult[] = $sTab.'redirect "'.static::quote($sValue).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (redirect): empty action value';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::MOVE_TO:
|
||||
$sValue = $oFilter->ActionValue();
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
$sFolderName = $sValue; // utf7-imap
|
||||
if (static::$bUtf8FolderName) // to utf-8
|
||||
{
|
||||
$sFolderName = \MailSo\Base\Utils::ConvertEncoding($sFolderName,
|
||||
\MailSo\Base\Enumerations\Charset::UTF_7_IMAP,
|
||||
\MailSo\Base\Enumerations\Charset::UTF_8);
|
||||
}
|
||||
|
||||
$aCapa['fileinto'] = true;
|
||||
$aResult[] = $sTab.'fileinto "'.static::quote($sFolderName).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (fileinto): empty action value';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if ($bBlock)
|
||||
{
|
||||
$aResult[] = '}';
|
||||
}
|
||||
|
||||
return \implode($sNL, $aResult);
|
||||
}
|
||||
|
||||
private static function quote(string $sValue) : string
|
||||
{
|
||||
return \str_replace(array('\\', '"'), array('\\\\', '\\"'), \trim($sValue));
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,6 @@ namespace RainLoop\Providers\Filters;
|
|||
|
||||
class SieveStorage implements FiltersInterface
|
||||
{
|
||||
const NEW_LINE = "\r\n";
|
||||
|
||||
const SIEVE_FILE_NAME = 'rainloop.user';
|
||||
const SIEVE_FILE_NAME_RAW = 'rainloop.user.raw';
|
||||
|
||||
|
|
@ -24,19 +22,12 @@ class SieveStorage implements FiltersInterface
|
|||
*/
|
||||
private $oConfig;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $bUtf8FolderName;
|
||||
|
||||
public function __construct($oPlugins, $oConfig)
|
||||
{
|
||||
$this->oLogger = null;
|
||||
|
||||
$this->oPlugins = $oPlugins;
|
||||
$this->oConfig = $oConfig;
|
||||
|
||||
$this->bUtf8FolderName = !!$this->oConfig->Get('labs', 'sieve_utf8_folder_name', true);
|
||||
}
|
||||
|
||||
protected function getConnection(\RainLoop\Model\Account $oAccount) : ?\MailSo\Sieve\ManageSieveClient
|
||||
|
|
@ -62,26 +53,12 @@ class SieveStorage implements FiltersInterface
|
|||
$aList = $oSieveClient->ListScripts();
|
||||
|
||||
foreach ($aList as $name => $active) {
|
||||
if ($name != self::SIEVE_FILE_NAME) {
|
||||
if ($bAllowRaw) {
|
||||
$aScripts[$name] = array(
|
||||
'@Object' => 'Object/SieveScript',
|
||||
'name' => $name,
|
||||
'active' => $active,
|
||||
'body' => $oSieveClient->GetScript($name) // \trim() ?
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$sS = $oSieveClient->GetScript(self::SIEVE_FILE_NAME);
|
||||
if ($sS) {
|
||||
$aFilters = $this->fileStringToCollection($sS);
|
||||
}
|
||||
if ($bAllowRaw || $name == self::SIEVE_FILE_NAME) {
|
||||
$aScripts[$name] = array(
|
||||
'@Object' => 'Object/SieveScript',
|
||||
'name' => $name,
|
||||
'active' => $active,
|
||||
'body' => $oSieveClient->GetScript($name), // \trim() ?
|
||||
'filters' => $aFilters
|
||||
'body' => $oSieveClient->GetScript($name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -119,7 +96,8 @@ class SieveStorage implements FiltersInterface
|
|||
public function Save(\RainLoop\Model\Account $oAccount, string $sScriptName, array $aFilters, string $sRaw = '') : bool
|
||||
{
|
||||
if (self::SIEVE_FILE_NAME === $sScriptName) {
|
||||
$sRaw = $this->collectionToFileString($aFilters);
|
||||
Sieve::$bUtf8FolderName = !!$this->oConfig->Get('labs', 'sieve_utf8_folder_name', true);
|
||||
$sRaw = Sieve::collectionToFileString($aFilters);
|
||||
}
|
||||
$oSieveClient = $this->getConnection($oAccount);
|
||||
if ($oSieveClient) {
|
||||
|
|
@ -149,7 +127,17 @@ class SieveStorage implements FiltersInterface
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
public function Check(\RainLoop\Model\Account $oAccount, string $sScript) : bool
|
||||
{
|
||||
$oSieveClient = $this->getConnection($oAccount);
|
||||
if ($oSieveClient) {
|
||||
$oSieveClient->CheckScript($sScript);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
public function Delete(\RainLoop\Model\Account $oAccount, string $sScriptName) : bool
|
||||
{
|
||||
$oSieveClient = $this->getConnection($oAccount);
|
||||
|
|
@ -160,392 +148,6 @@ class SieveStorage implements FiltersInterface
|
|||
return false;
|
||||
}
|
||||
|
||||
private function conditionToSieveScript(Classes\FilterCondition $oCondition, array &$aCapa) : string
|
||||
{
|
||||
$sResult = '';
|
||||
$sTypeWord = '';
|
||||
$bTrue = true;
|
||||
|
||||
$sValue = \trim($oCondition->Value());
|
||||
$sValueSecond = \trim($oCondition->ValueSecond());
|
||||
|
||||
if (0 < \strlen($sValue) ||
|
||||
(0 < \strlen($sValue) && 0 < \strlen($sValueSecond) &&
|
||||
Enumerations\ConditionField::HEADER === $oCondition->Field()))
|
||||
{
|
||||
switch ($oCondition->Type())
|
||||
{
|
||||
case Enumerations\ConditionType::TEXT:
|
||||
case Enumerations\ConditionType::RAW:
|
||||
case Enumerations\ConditionType::OVER:
|
||||
case Enumerations\ConditionType::UNDER:
|
||||
$sTypeWord = ':' . \strtolower($oCondition->Type());
|
||||
break;
|
||||
case Enumerations\ConditionType::NOT_EQUAL_TO:
|
||||
$sResult .= 'not ';
|
||||
case Enumerations\ConditionType::EQUAL_TO:
|
||||
$sTypeWord = ':is';
|
||||
break;
|
||||
case Enumerations\ConditionType::NOT_CONTAINS:
|
||||
$sResult .= 'not ';
|
||||
case Enumerations\ConditionType::CONTAINS:
|
||||
$sTypeWord = ':contains';
|
||||
break;
|
||||
case Enumerations\ConditionType::REGEX:
|
||||
$sTypeWord = ':regex';
|
||||
$aCapa['regex'] = true;
|
||||
break;
|
||||
default:
|
||||
$bTrue = false;
|
||||
$sResult = '/* @Error: unknown type value */ false';
|
||||
break;
|
||||
}
|
||||
|
||||
switch ($oCondition->Field())
|
||||
{
|
||||
case Enumerations\ConditionField::FROM:
|
||||
$sResult .= 'header '.$sTypeWord.' ["From"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::RECIPIENT:
|
||||
$sResult .= 'header '.$sTypeWord.' ["To", "CC"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::SUBJECT:
|
||||
$sResult .= 'header '.$sTypeWord.' ["Subject"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::HEADER:
|
||||
$sResult .= 'header '.$sTypeWord.' ["'.$this->quote($sValueSecond).'"]';
|
||||
break;
|
||||
case Enumerations\ConditionField::BODY:
|
||||
// :text :raw :content
|
||||
$sResult .= 'body '.$sTypeWord.' :contains';
|
||||
$aCapa['body'] = true;
|
||||
break;
|
||||
case Enumerations\ConditionField::SIZE:
|
||||
$sResult .= 'size '.$sTypeWord;
|
||||
break;
|
||||
default:
|
||||
$bTrue = false;
|
||||
$sResult = '/* @Error: unknown field value */ false';
|
||||
break;
|
||||
}
|
||||
|
||||
if ($bTrue)
|
||||
{
|
||||
if (\in_array($oCondition->Field(), array(
|
||||
Enumerations\ConditionField::FROM,
|
||||
Enumerations\ConditionField::RECIPIENT
|
||||
)) && false !== \strpos($sValue, ','))
|
||||
{
|
||||
$self = $this;
|
||||
$aValue = \array_map(function ($sValue) use ($self) {
|
||||
return '"'.$self->quote(\trim($sValue)).'"';
|
||||
}, \explode(',', $sValue));
|
||||
|
||||
$sResult .= ' ['.\trim(\implode(', ', $aValue)).']';
|
||||
}
|
||||
else if (Enumerations\ConditionField::SIZE === $oCondition->Field())
|
||||
{
|
||||
$sResult .= ' '.$this->quote($sValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
$sResult .= ' "'.$this->quote($sValue).'"';
|
||||
}
|
||||
|
||||
$sResult = \MailSo\Base\Utils::StripSpaces($sResult);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$sResult = '/* @Error: empty condition value */ false';
|
||||
}
|
||||
|
||||
return $sResult;
|
||||
}
|
||||
|
||||
private function filterToSieveScript(Classes\Filter $oFilter, array &$aCapa) : string
|
||||
{
|
||||
$sNL = static::NEW_LINE;
|
||||
$sTab = ' ';
|
||||
|
||||
$bAll = false;
|
||||
$aResult = array();
|
||||
|
||||
// Conditions
|
||||
$aConditions = $oFilter->Conditions();
|
||||
if (1 < \count($aConditions))
|
||||
{
|
||||
if (Enumerations\ConditionsType::ANY ===
|
||||
$oFilter->ConditionsType())
|
||||
{
|
||||
$aResult[] = 'if anyof(';
|
||||
|
||||
$bTrim = false;
|
||||
foreach ($aConditions as $oCond)
|
||||
{
|
||||
$bTrim = true;
|
||||
$sCons = $this->conditionToSieveScript($oCond, $aCapa);
|
||||
if (!empty($sCons))
|
||||
{
|
||||
$aResult[] = $sTab.$sCons.',';
|
||||
}
|
||||
}
|
||||
if ($bTrim)
|
||||
{
|
||||
$aResult[\count($aResult) - 1] = \rtrim($aResult[\count($aResult) - 1], ',');
|
||||
}
|
||||
|
||||
$aResult[] = ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = 'if allof(';
|
||||
foreach ($aConditions as $oCond)
|
||||
{
|
||||
$aResult[] = $sTab.$this->conditionToSieveScript($oCond, $aCapa).',';
|
||||
}
|
||||
|
||||
$aResult[\count($aResult) - 1] = \rtrim($aResult[\count($aResult) - 1], ',');
|
||||
$aResult[] = ')';
|
||||
}
|
||||
}
|
||||
else if (1 === \count($aConditions))
|
||||
{
|
||||
$aResult[] = 'if '.$this->conditionToSieveScript($aConditions[0], $aCapa).'';
|
||||
}
|
||||
else
|
||||
{
|
||||
$bAll = true;
|
||||
}
|
||||
|
||||
// actions
|
||||
if (!$bAll)
|
||||
{
|
||||
$aResult[] = '{';
|
||||
}
|
||||
else
|
||||
{
|
||||
$sTab = '';
|
||||
}
|
||||
|
||||
if ($oFilter->MarkAsRead() && \in_array($oFilter->ActionType(), array(
|
||||
Enumerations\ActionType::NONE,
|
||||
Enumerations\ActionType::MOVE_TO,
|
||||
Enumerations\ActionType::FORWARD
|
||||
)))
|
||||
{
|
||||
$aCapa['imap4flags'] = true;
|
||||
$aResult[] = $sTab.'addflag "\\\\Seen";';
|
||||
}
|
||||
|
||||
switch ($oFilter->ActionType())
|
||||
{
|
||||
case Enumerations\ActionType::NONE:
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::DISCARD:
|
||||
$aResult[] = $sTab.'discard;';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::VACATION:
|
||||
$sValue = \trim($oFilter->ActionValue());
|
||||
$sValueSecond = \trim($oFilter->ActionValueSecond());
|
||||
$sValueThird = \trim($oFilter->ActionValueThird());
|
||||
$sValueFourth = \trim($oFilter->ActionValueFourth());
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
$aCapa['vacation'] = true;
|
||||
|
||||
$iDays = 1;
|
||||
$sSubject = '';
|
||||
if (0 < \strlen($sValueSecond))
|
||||
{
|
||||
$sSubject = ':subject "'.
|
||||
$this->quote(\MailSo\Base\Utils::StripSpaces($sValueSecond)).'" ';
|
||||
}
|
||||
|
||||
if (0 < \strlen($sValueThird) && \is_numeric($sValueThird) && 1 < (int) $sValueThird)
|
||||
{
|
||||
$iDays = (int) $sValueThird;
|
||||
}
|
||||
|
||||
$sAddresses = '';
|
||||
if (0 < \strlen($sValueFourth))
|
||||
{
|
||||
$self = $this;
|
||||
|
||||
$aAddresses = \explode(',', $sValueFourth);
|
||||
$aAddresses = \array_filter(\array_map(function ($sEmail) use ($self) {
|
||||
$sEmail = \trim($sEmail);
|
||||
return 0 < \strlen($sEmail) ? '"'.$self->quote($sEmail).'"' : '';
|
||||
}, $aAddresses), 'strlen');
|
||||
|
||||
if (0 < \count($aAddresses))
|
||||
{
|
||||
$sAddresses = ':addresses ['.\implode(', ', $aAddresses).'] ';
|
||||
}
|
||||
}
|
||||
|
||||
$aResult[] = $sTab.'vacation :days '.$iDays.' '.$sAddresses.$sSubject.'"'.$this->quote($sValue).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (vacation): empty action value';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::REJECT:
|
||||
$sValue = \trim($oFilter->ActionValue());
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
$aCapa['reject'] = true;
|
||||
|
||||
$aResult[] = $sTab.'reject "'.$this->quote($sValue).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (reject): empty action value';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::FORWARD:
|
||||
$sValue = $oFilter->ActionValue();
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
if ($oFilter->Keep())
|
||||
{
|
||||
$aCapa['fileinto'] = true;
|
||||
$aResult[] = $sTab.'fileinto "INBOX";';
|
||||
}
|
||||
|
||||
$aResult[] = $sTab.'redirect "'.$this->quote($sValue).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (redirect): empty action value';
|
||||
}
|
||||
break;
|
||||
case Enumerations\ActionType::MOVE_TO:
|
||||
$sValue = $oFilter->ActionValue();
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
$sFolderName = $sValue; // utf7-imap
|
||||
if ($this->bUtf8FolderName) // to utf-8
|
||||
{
|
||||
$sFolderName = \MailSo\Base\Utils::ConvertEncoding($sFolderName,
|
||||
\MailSo\Base\Enumerations\Charset::UTF_7_IMAP,
|
||||
\MailSo\Base\Enumerations\Charset::UTF_8);
|
||||
}
|
||||
|
||||
$aCapa['fileinto'] = true;
|
||||
$aResult[] = $sTab.'fileinto "'.$this->quote($sFolderName).'";';
|
||||
if ($oFilter->Stop())
|
||||
{
|
||||
$aResult[] = $sTab.'stop;';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aResult[] = $sTab.'# @Error (fileinto): empty action value';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$bAll)
|
||||
{
|
||||
$aResult[] = '}';
|
||||
}
|
||||
|
||||
return \implode($sNL, $aResult);
|
||||
}
|
||||
|
||||
private function collectionToFileString(array $aFilters) : string
|
||||
{
|
||||
$sNL = static::NEW_LINE;
|
||||
|
||||
$aCapa = array();
|
||||
$aParts = array();
|
||||
|
||||
$aParts[] = '# This is SnappyMail sieve script.';
|
||||
$aParts[] = '# Please don\'t change anything here.';
|
||||
$aParts[] = '# RAINLOOP:SIEVE';
|
||||
$aParts[] = '';
|
||||
|
||||
foreach ($aFilters as /* @var $oItem \RainLoop\Providers\Filters\Classes\Filter */ $oItem)
|
||||
{
|
||||
$aData = array();
|
||||
$aData[] = '/*';
|
||||
$aData[] = 'BEGIN:FILTER:'.$oItem->ID();
|
||||
$aData[] = 'BEGIN:HEADER';
|
||||
$aData[] = \chunk_split(\base64_encode(\json_encode($oItem)), 74, $sNL).'END:HEADER';
|
||||
$aData[] = '*/';
|
||||
$aData[] = $oItem->Enabled() ? '' : '/* @Filter is disabled ';
|
||||
$aData[] = $this->filterToSieveScript($oItem, $aCapa);
|
||||
$aData[] = $oItem->Enabled() ? '' : '*/';
|
||||
$aData[] = '/* END:FILTER */';
|
||||
$aData[] = '';
|
||||
|
||||
$aParts[] = \implode($sNL, $aData);
|
||||
}
|
||||
|
||||
$aCapa = \array_keys($aCapa);
|
||||
$sCapa = 0 < \count($aCapa) ? $sNL.'require '.
|
||||
\str_replace('","', '", "', \json_encode($aCapa)).';'.$sNL : '';
|
||||
|
||||
return $sCapa.$sNL.\implode($sNL, $aParts).$sNL;
|
||||
}
|
||||
|
||||
private function fileStringToCollection(string $sFileString) : array
|
||||
{
|
||||
$aResult = array();
|
||||
if (!empty($sFileString) && false !== \strpos($sFileString, 'RAINLOOP:SIEVE'))
|
||||
{
|
||||
$aMatch = array();
|
||||
if (\preg_match_all('/BEGIN:FILTER(.+?)BEGIN:HEADER(.+?)END:HEADER/s', $sFileString, $aMatch) &&
|
||||
isset($aMatch[2]) && \is_array($aMatch[2]))
|
||||
{
|
||||
foreach ($aMatch[2] as $sEncodedLine)
|
||||
{
|
||||
if (!empty($sEncodedLine))
|
||||
{
|
||||
$sDecodedLine = \base64_decode(\preg_replace('/\\s+/s', '', $sEncodedLine));
|
||||
if (!empty($sDecodedLine))
|
||||
{
|
||||
$oItem = new Classes\Filter();
|
||||
if ($oItem && $oItem->unserializeFromJson($sDecodedLine))
|
||||
{
|
||||
$aResult[] = $oItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
public function quote(string $sValue) : string
|
||||
{
|
||||
return \str_replace(array('\\', '"'), array('\\\\', '\\"'), \trim($sValue));
|
||||
}
|
||||
|
||||
public function SetLogger(?\MailSo\Log\Logger $oLogger)
|
||||
{
|
||||
$this->oLogger = $oLogger;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue