snappymail/plugins/demo-account/index.php

128 lines
4 KiB
PHP
Raw Normal View History

<?php
class DemoAccountPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'Demo Account Extension',
CATEGORY = 'Login',
2022-03-21 20:00:10 +08:00
REQUIRED = '2.14.0',
DESCRIPTION = 'Extension to enable a demo account';
/**
* @return void
*/
2020-08-31 00:04:54 +08:00
public function Init() : void
{
$this->addHook('filter.app-data', 'FilterAppData');
$this->addHook('filter.action-params', 'FilterActionParams');
$this->addHook('json.action-pre-call', 'JsonActionPreCall');
$this->addHook('filter.send-message', 'FilterSendMessage');
2015-02-13 03:56:06 +08:00
$this->addHook('main.fabrica', 'MainFabrica');
}
/**
* @return array
*/
2020-08-31 00:04:54 +08:00
protected function configMapping() : array
{
return array(
\RainLoop\Plugins\Property::NewInstance('email')->SetLabel('Demo Email')
->SetDefaultValue('demo@domain.com'),
\RainLoop\Plugins\Property::NewInstance('password')->SetLabel('Demo Password')
2022-02-28 23:13:41 +08:00
->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD),
\RainLoop\Plugins\Property::NewInstance('recipient_delimiter')->SetLabel('recipient_delimiter')
->SetDefaultValue(''),
);
}
/**
* @return void
*/
public function FilterAppData($bAdmin, &$aResult)
{
if (!$bAdmin && \is_array($aResult) && isset($aResult['Auth']) && !$aResult['Auth']) {
$aResult['DevEmail'] = $this->Config()->Get('plugin', 'email', $aResult['DevEmail']);
$aResult['DevPassword'] = APP_DUMMY;
}
}
/**
* @return void
*/
public function FilterActionParams($sMethodName, &$aActionParams)
{
if ('DoLogin' === $sMethodName
&& isset($aActionParams['Email'])
&& isset($aActionParams['Password'])
&& $this->Config()->Get('plugin', 'email') === $aActionParams['Email']) {
$aActionParams['Password'] = $this->Config()->Get('plugin', 'password');
}
2022-05-21 06:40:07 +08:00
else if ('DoFolderCreate' === $sMethodName || 'DoFolderRename' === $sMethodName) {
// Block spam https://github.com/the-djmaze/snappymail/issues/371
$latin = transliterator_transliterate('Any-Latin; Latin-ASCII; Lower()', $aActionParams['Folder']);
if (false !== \strpos($latin, 'nigger')) {
\error_log("blocked {$sMethodName} {$aActionParams['Folder']}");
exit;
}
}
}
/**
* @param \RainLoop\Model\Account $oAccount
*
* @return bool
*/
private function isDemoAccount()
{
$oAccount = $this->Manager()->Actions()->GetAccount();
return ($oAccount && $oAccount->Email() === $this->Config()->Get('plugin', 'email'));
}
public function JsonActionPreCall($sAction)
{
if ('AccountSetup' === $sAction && $this->isDemoAccount()) {
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoAccountError);
}
}
2021-08-18 18:25:17 +08:00
public function FilterSendMessage($oMessage)
{
if ($oMessage && $this->isDemoAccount()) {
2022-02-28 23:13:41 +08:00
$recipient_delimiter = $this->Config()->Get('plugin', 'recipient_delimiter');
$regex = '/^' . \preg_quote($this->Config()->Get('plugin', 'email')) . '$/D';
if ($recipient_delimiter) {
$regex = \str_replace('@', '('.\preg_quote($recipient_delimiter).'.+)?@', $regex);
}
foreach ($oMessage->GetTo() as $oEmail) {
2022-02-28 23:13:41 +08:00
if (!\preg_match($regex, $oEmail->GetEmail())) {
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoSendMessageError);
}
}
foreach ($oMessage->GetCc() ?: [] as $oEmail) {
2022-02-28 23:13:41 +08:00
if (!\preg_match($regex, $oEmail->GetEmail())) {
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoSendMessageError);
}
}
foreach ($oMessage->GetBcc() ?: [] as $oEmail) {
2022-02-28 23:13:41 +08:00
if (!\preg_match($regex, $oEmail->GetEmail())) {
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoSendMessageError);
}
}
// throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoSendMessageError);
}
}
/**
* @param string $sName
* @param mixed $oDriver
*/
2015-02-13 03:56:06 +08:00
public function MainFabrica($sName, &$oDriver)
{
2021-11-09 00:40:21 +08:00
if ('storage' === $sName || 'storage-local' === $sName) {
require_once __DIR__ . '/storage.php';
$oDriver = new \DemoStorage(APP_PRIVATE_DATA.'storage', $sName === 'storage-local');
$oDriver->setDemoEmail($this->Config()->Get('plugin', 'email'));
}
}
}