diff --git a/integrations/nextcloud/snappymail/lib/AppInfo/Application.php b/integrations/nextcloud/snappymail/lib/AppInfo/Application.php index e59cdbc7a..d3cd604e6 100644 --- a/integrations/nextcloud/snappymail/lib/AppInfo/Application.php +++ b/integrations/nextcloud/snappymail/lib/AppInfo/Application.php @@ -87,14 +87,13 @@ class Application extends App implements IBootstrap $sEmail = $config->getUserValue($user->getUID(), 'settings', 'email', ''); } if ($sEmail) { - SnappyMailHelper::startApp(true); - \OC::$server->getSession()['snappymail-sso-hash'] = \RainLoop\Api::CreateUserSsoHash($sEmail, $password/*, array $aAdditionalOptions = array(), bool $bUseTimeout = true*/); + \OC::$server->getSession()['snappymail-password'] = SnappyMailHelper::encodePassword($password, \md5($sEmail)); } }); $userSession->listen('\OC\User', 'logout', function($user) { - \OC::$server->getSession()['snappymail-sso-hash'] = ''; - SnappyMailHelper::startApp(true); + $oSession = \OC::$server->getSession()['snappymail-password'] = ''; + SnappyMailHelper::startApp(); \RainLoop\Api::LogoutCurrentLogginedUser(); }); } diff --git a/integrations/nextcloud/snappymail/lib/Controller/PageController.php b/integrations/nextcloud/snappymail/lib/Controller/PageController.php index 0951ee9e7..e1d1d2b27 100644 --- a/integrations/nextcloud/snappymail/lib/Controller/PageController.php +++ b/integrations/nextcloud/snappymail/lib/Controller/PageController.php @@ -28,24 +28,9 @@ class PageController extends Controller \OCP\Util::addStyle('snappymail', 'style'); $query = ''; - // If the user has set credentials for SnappyMail in their personal - // settings, override everything before and use those instead. - $sUID = \OC::$server->getUserSession()->getUser()->getUID(); - $sEmail = $config->getUserValue($sUID, 'snappymail', 'snappymail-email', ''); - if ($sEmail) { - $password = SnappyMailHelper::decodePassword( - $config->getUserValue($sUID, 'snappymail', 'snappymail-password', ''), - \md5($sEmail) - ); - if ($password) { - $query = '?sso&hash=' . \RainLoop\Api::CreateUserSsoHash($sEmail, $password); - } - } - if (!$query) { - $session = \OC::$server->getSession(); - if (!empty($session['snappymail-sso-hash'])) { - $query = '?sso&hash=' . $session['snappymail-sso-hash']; - } + $aCredentials = SnappyMailHelper::getLoginCredentials(); + if ($aCredentials[0] && $aCredentials[1]) { + $query = '?sso&hash=' . \RainLoop\Api::CreateUserSsoHash($aCredentials[0] && $aCredentials[1]); } $params = [ @@ -68,6 +53,8 @@ class PageController extends Controller public function appGet() { SnappyMailHelper::startApp(); + \RainLoop\Service::Handle(); + exit; } /** @@ -76,7 +63,7 @@ class PageController extends Controller */ public function appPost() { - SnappyMailHelper::startApp(); + SnappyMailHelper::startApp(true); } /** @@ -85,7 +72,7 @@ class PageController extends Controller */ public function indexPost() { - SnappyMailHelper::startApp(); + SnappyMailHelper::startApp(true); } /** @@ -94,15 +81,14 @@ class PageController extends Controller private static function index_embed() { if (!empty($_SERVER['QUERY_STRING'])) { - SnappyMailHelper::startApp(); - return; + SnappyMailHelper::startApp(true); } \OC::$server->getNavigationManager()->setActiveEntry('snappymail'); \OCP\Util::addStyle('snappymail', 'embed'); - SnappyMailHelper::startApp(true); + SnappyMailHelper::startApp(); $oConfig = \RainLoop\Api::Config(); $oActions = \RainLoop\Api::Actions(); $oHttp = \MailSo\Base\Http::SingletonInstance(); diff --git a/integrations/nextcloud/snappymail/lib/Search/Provider.php b/integrations/nextcloud/snappymail/lib/Search/Provider.php index 95b8cd1fe..9888ff25e 100644 --- a/integrations/nextcloud/snappymail/lib/Search/Provider.php +++ b/integrations/nextcloud/snappymail/lib/Search/Provider.php @@ -43,9 +43,10 @@ class Provider implements IProvider public function search(IUser $user, ISearchQuery $query): SearchResult { $result = []; - SnappyMailHelper::startApp(true); + SnappyMailHelper::startApp(); $oActions = \RainLoop\Api::Actions(); - $oAccount = $oActions->getAccountFromToken(false); + $oAccount = $oActions->getMainAccountFromToken(false); +// $oAccount = $oActions->getAccountFromToken(false); $iCursor = (int) $query->getCursor(); $iLimit = $query->getLimit(); if ($oAccount) { diff --git a/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php b/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php index fa10fc967..089ecb05a 100644 --- a/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php +++ b/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php @@ -5,7 +5,7 @@ namespace OCA\SnappyMail\Util; class SnappyMailHelper { - public static function startApp(bool $api = false) + public static function startApp(bool $handle = false) : void { if (!\class_exists('RainLoop\\Api')) { $_ENV['SNAPPYMAIL_NEXTCLOUD'] = true; @@ -18,47 +18,77 @@ class SnappyMailHelper } }); - if ($api) { - $_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true; - } + $_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true; require_once \dirname(\dirname(__DIR__)) . '/app/index.php'; - if ($api) { - $oConfig = \RainLoop\Api::Config(); - $bSave = false; - if (!$oConfig->Get('webmail', 'app_path')) { - $oConfig->Set('webmail', 'app_path', \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/'); - $bSave = true; - } - if (!\is_dir(APP_PLUGINS_PATH . 'nextcloud')) { - \SnappyMail\Repository::installPackage('plugin', 'nextcloud'); - $oConfig->Set('plugins', 'enable', true); - $aList = \SnappyMail\Repository::getEnabledPackagesNames(); - $aList[] = 'nextcloud'; - $oConfig->Set('plugins', 'enabled_list', \implode(',', \array_unique($aList))); - $oConfig->Set('webmail', 'theme', 'Nextcloud@custom'); - $bSave = true; - } - $bSave && $oConfig->Save(); + $oConfig = \RainLoop\Api::Config(); + $bSave = false; + if (!$oConfig->Get('webmail', 'app_path')) { + $oConfig->Set('webmail', 'app_path', \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/'); + $bSave = true; } + if (!\is_dir(APP_PLUGINS_PATH . 'nextcloud')) { + \SnappyMail\Repository::installPackage('plugin', 'nextcloud'); + $oConfig->Set('plugins', 'enable', true); + $aList = \SnappyMail\Repository::getEnabledPackagesNames(); + $aList[] = 'nextcloud'; + $oConfig->Set('plugins', 'enabled_list', \implode(',', \array_unique($aList))); + $oConfig->Set('webmail', 'theme', 'Nextcloud@custom'); + $bSave = true; + } + $bSave && $oConfig->Save(); + + $oActions = \RainLoop\Api::Actions(); + if (!$oActions->getMainAccountFromToken(false)) { + $aCredentials = SnappyMailHelper::getLoginCredentials(); + if ($aCredentials[0] && $aCredentials[1]) { + $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); + } + } + } + } + + if ($handle) { + \RainLoop\Service::Handle(); + exit; } } - /** - * @return string - */ - public static function getAppUrl() + public static function getLoginCredentials() : array + { + $config = \OC::$server->getConfig(); + $sEmail = ''; + $sPassword = ''; + $sUID = \OC::$server->getUserSession()->getUser()->getUID(); + // Only store the user's password in the current session if they have + // enabled auto-login using Nextcloud username or email address. + if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) { + $sEmail = $sUID; + $sPassword = \OC::$server->getSession()['snappymail-password']; + } else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) { + $sEmail = $config->getUserValue($sUID, 'settings', 'email', ''); + $sPassword = \OC::$server->getSession()['snappymail-password']; + } + // If the user has set credentials for SnappyMail in their personal + // settings, override everything before and use those instead. + $sEmail = $config->getUserValue($sUID, 'snappymail', 'snappymail-email', ''); + if ($sEmail) { + $sPassword = $config->getUserValue($sUID, 'snappymail', 'snappymail-password', ''); + } + return [$sEmail, $sPassword ? SnappyMailHelper::decodePassword($sPassword, \md5($sEmail)) : '']; + } + + public static function getAppUrl() : string { return \OC::$server->getURLGenerator()->linkToRoute('snappymail.page.appGet'); } - /** - * @param string $sUrl - * - * @return string - */ - public static function normalizeUrl($sUrl) + public static function normalizeUrl(string $sUrl) : string { $sUrl = \rtrim(\trim($sUrl), '/\\'); if ('.php' !== \strtolower(\substr($sUrl, -4))) { @@ -68,27 +98,15 @@ class SnappyMailHelper return $sUrl; } - /** - * @param string $sPassword - * @param string $sSalt - * - * @return string - */ - public static function encodePassword($sPassword, $sSalt) + public static function encodePassword(string $sPassword, string $sSalt) : string { - static::startApp(true); + static::startApp(); return \SnappyMail\Crypt::EncryptUrlSafe($sPassword, $sSalt); } - /** - * @param string $sPassword - * @param string $sSalt - * - * @return string - */ - public static function decodePassword($sPassword, $sSalt) + public static function decodePassword(string $sPassword, string $sSalt)/* : mixed */ { - static::startApp(true); + static::startApp(); return \SnappyMail\Crypt::DecryptUrlSafe($sPassword, $sSalt); } }