Split Nextcloud SnappyMailHelper

This commit is contained in:
the-djmaze 2022-10-19 16:27:15 +02:00
parent bdea9c6833
commit 04d55be625
4 changed files with 42 additions and 38 deletions

View file

@ -93,7 +93,7 @@ class Application extends App implements IBootstrap
$userSession->listen('\OC\User', 'logout', function($user) { $userSession->listen('\OC\User', 'logout', function($user) {
$oSession = \OC::$server->getSession()['snappymail-password'] = ''; $oSession = \OC::$server->getSession()['snappymail-password'] = '';
SnappyMailHelper::startApp(); SnappyMailHelper::loadApp();
\RainLoop\Api::LogoutCurrentLogginedUser(); \RainLoop\Api::LogoutCurrentLogginedUser();
}); });
} }

View file

@ -25,7 +25,7 @@ class FetchController extends Controller {
public function upgrade(): JSONResponse { public function upgrade(): JSONResponse {
$error = 'Upgrade failed'; $error = 'Upgrade failed';
try { try {
SnappyMailHelper::startApp(); SnappyMailHelper::loadApp();
if (SnappyMail\Upgrade::core()) { if (SnappyMail\Upgrade::core()) {
return new JSONResponse([ return new JSONResponse([
'status' => 'success', 'status' => 'success',
@ -59,7 +59,7 @@ class FetchController extends Controller {
]); ]);
} }
\OCA\SnappyMail\Util\SnappyMailHelper::startApp(); SnappyMailHelper::loadApp();
$debug = !empty($_POST['snappymail-debug']); $debug = !empty($_POST['snappymail-debug']);
$oConfig = \RainLoop\Api::Config(); $oConfig = \RainLoop\Api::Config();
if ($debug != $oConfig->Get('debug', 'enable', false)) { if ($debug != $oConfig->Get('debug', 'enable', false)) {

View file

@ -32,7 +32,7 @@ class AdminSettings implements ISettings
$parameters['snappymail-admin-panel-link'] = SnappyMailHelper::getAppUrl().'?admin'; $parameters['snappymail-admin-panel-link'] = SnappyMailHelper::getAppUrl().'?admin';
} }
\OCA\SnappyMail\Util\SnappyMailHelper::startApp(); \OCA\SnappyMail\Util\SnappyMailHelper::loadApp();
$oConfig = \RainLoop\Api::Config(); $oConfig = \RainLoop\Api::Config();
$passfile = APP_PRIVATE_DATA . 'admin_password.txt'; $passfile = APP_PRIVATE_DATA . 'admin_password.txt';
$sPassword = $oConfig->Get('security', 'admin_password', ''); $sPassword = $oConfig->Get('security', 'admin_password', '');

View file

@ -5,11 +5,9 @@ namespace OCA\SnappyMail\Util;
class SnappyMailHelper class SnappyMailHelper
{ {
public static function startApp(bool $handle = false) : void public static function loadApp() : void
{ {
if (!\class_exists('RainLoop\\Api')) { if (!\class_exists('RainLoop\\Api')) {
$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true;
// Nextcloud the default spl_autoload_register() not working // Nextcloud the default spl_autoload_register() not working
\spl_autoload_register(function($sClassName){ \spl_autoload_register(function($sClassName){
$file = RAINLOOP_APP_LIBRARIES_PATH . \strtolower(\strtr($sClassName, '\\', DIRECTORY_SEPARATOR)) . '.php'; $file = RAINLOOP_APP_LIBRARIES_PATH . \strtolower(\strtr($sClassName, '\\', DIRECTORY_SEPARATOR)) . '.php';
@ -18,43 +16,49 @@ class SnappyMailHelper
} }
}); });
$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true;
$_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true; $_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true;
require_once \dirname(\dirname(__DIR__)) . '/app/index.php'; require_once \dirname(\dirname(__DIR__)) . '/app/index.php';
}
$oConfig = \RainLoop\Api::Config(); $oConfig = \RainLoop\Api::Config();
$bSave = false; $bSave = false;
if (!$oConfig->Get('webmail', 'app_path')) { if (!$oConfig->Get('webmail', 'app_path')) {
$oConfig->Set('webmail', 'app_path', \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/'); $oConfig->Set('webmail', 'app_path', \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/');
$bSave = true; $bSave = true;
} }
if (!\is_dir(APP_PLUGINS_PATH . 'nextcloud')) { if (!\is_dir(APP_PLUGINS_PATH . 'nextcloud')) {
\SnappyMail\Repository::installPackage('plugin', 'nextcloud'); \SnappyMail\Repository::installPackage('plugin', 'nextcloud');
$oConfig->Set('plugins', 'enable', true); $oConfig->Set('plugins', 'enable', true);
$aList = \SnappyMail\Repository::getEnabledPackagesNames(); $aList = \SnappyMail\Repository::getEnabledPackagesNames();
$aList[] = 'nextcloud'; $aList[] = 'nextcloud';
$oConfig->Set('plugins', 'enabled_list', \implode(',', \array_unique($aList))); $oConfig->Set('plugins', 'enabled_list', \implode(',', \array_unique($aList)));
$oConfig->Set('webmail', 'theme', 'Nextcloud@custom'); $oConfig->Set('webmail', 'theme', 'Nextcloud@custom');
$bSave = true; $bSave = true;
} }
$bSave && $oConfig->Save(); $bSave && $oConfig->Save();
}
try { public static function startApp(bool $handle = false) : void
$oActions = \RainLoop\Api::Actions(); {
if (!$oActions->getMainAccountFromToken(false)) { static::loadApp();
$aCredentials = SnappyMailHelper::getLoginCredentials();
if ($aCredentials[0] && $aCredentials[1]) { try {
$oActions->Logger()->AddSecret($aCredentials[1]); $oActions = \RainLoop\Api::Actions();
$oAccount = $oActions->LoginProcess($aCredentials[0], $aCredentials[1], false); if (!$oActions->getMainAccountFromToken(false)) {
if ($oAccount) { $aCredentials = SnappyMailHelper::getLoginCredentials();
$oActions->Plugins()->RunHook('login.success', array($oAccount)); if ($aCredentials[0] && $aCredentials[1]) {
$oActions->SetAuthToken($oAccount); $oActions->Logger()->AddSecret($aCredentials[1]);
} $oAccount = $oActions->LoginProcess($aCredentials[0], $aCredentials[1], false);
if ($oAccount) {
$oActions->Plugins()->RunHook('login.success', array($oAccount));
$oActions->SetAuthToken($oAccount);
} }
} }
} catch (\Throwable $e) {
// Ignore login failure
} }
} catch (\Throwable $e) {
// Ignore login failure
} }
if ($handle) { if ($handle) {
@ -106,13 +110,13 @@ class SnappyMailHelper
public static function encodePassword(string $sPassword, string $sSalt) : string public static function encodePassword(string $sPassword, string $sSalt) : string
{ {
static::startApp(); static::loadApp();
return \SnappyMail\Crypt::EncryptUrlSafe($sPassword, $sSalt); return \SnappyMail\Crypt::EncryptUrlSafe($sPassword, $sSalt);
} }
public static function decodePassword(string $sPassword, string $sSalt)/* : mixed */ public static function decodePassword(string $sPassword, string $sSalt)/* : mixed */
{ {
static::startApp(); static::loadApp();
return \SnappyMail\Crypt::DecryptUrlSafe($sPassword, $sSalt); return \SnappyMail\Crypt::DecryptUrlSafe($sPassword, $sSalt);
} }
} }