From d52f2adb2b43d5c5040c675c4e0394d7586b49de Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 11 Oct 2022 22:36:53 +0200 Subject: [PATCH] Draft code to run SnappyMail inside Nextcloud without iframe for --- .../lib/Controller/PageController.php | 61 ++++++++++++++++++- .../snappymail/lib/Util/SnappyMailHelper.php | 6 +- .../snappymail/templates/index_embed.php | 17 ++++++ plugins/nextcloud/index.php | 10 +-- 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100755 integrations/nextcloud/snappymail/templates/index_embed.php diff --git a/integrations/nextcloud/snappymail/lib/Controller/PageController.php b/integrations/nextcloud/snappymail/lib/Controller/PageController.php index ef51eebea..92854d357 100644 --- a/integrations/nextcloud/snappymail/lib/Controller/PageController.php +++ b/integrations/nextcloud/snappymail/lib/Controller/PageController.php @@ -16,6 +16,9 @@ class PageController extends Controller */ public function index() { + // Uncomment to test without using an iframe +// return static::index_embed(); + \OC::$server->getNavigationManager()->setActiveEntry('snappymail'); \OCP\Util::addStyle('snappymail', 'style'); @@ -72,5 +75,61 @@ class PageController extends Controller { SnappyMailHelper::startApp(); } -} + /** + * Draft code to run without using an iframe + */ + private static function index_embed() + { + \OC::$server->getNavigationManager()->setActiveEntry('snappymail'); + + \OCP\Util::addStyle('snappymail', 'style'); + + SnappyMailHelper::startApp(true); + $webPath = \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app'; + $oConfig = \RainLoop\Api::Config(); + $oActions = \RainLoop\Api::Actions(); + $oHttp = \MailSo\Base\Http::SingletonInstance(); + $oServiceActions = new \RainLoop\ServiceActions($oHttp, $oActions); + $sAppJsMin = $oConfig->Get('labs', 'use_app_debug_js', false) ? '' : '.min'; + $sAppCssMin = $oConfig->Get('labs', 'use_app_debug_css', false) ? '' : '.min'; + $sLanguage = $oActions->GetLanguage(false); + + $sScriptNonce = \OC::$server->getContentSecurityPolicyNonceManager()->getNonce(); +// $sScriptNonce = \SnappyMail\UUID::generate(); +// \RainLoop\Service::setCSP($sScriptNonce); + + $params = [ + 'LoadingDescriptionEsc' => \htmlspecialchars($oConfig->Get('webmail', 'loading_description', 'SnappyMail'), ENT_QUOTES|ENT_IGNORE, 'UTF-8'), + 'BaseTemplates' => \RainLoop\Utils::ClearHtmlOutput($oServiceActions->compileTemplates(false)), + 'BaseAppBootScript' => \str_replace( + 'loadScript(`./?/', + 'loadScript(`'.$webPath.'/?/', + \file_get_contents(APP_VERSION_ROOT_PATH.'static/js'.($sAppJsMin ? '/min' : '').'/boot'.$sAppJsMin.'.js') + ), + 'BaseAppBootScriptNonce' => $sScriptNonce, + 'BaseLanguage' => $oActions->compileLanguage($sLanguage, false), + ]; + + \OCP\Util::addHeader('style', ['id'=>'app-boot-css'], \file_get_contents(APP_VERSION_ROOT_PATH.'static/css/boot'.$sAppCssMin.'.css')); + \OCP\Util::addHeader('link', ['type'=>'text/css','rel'=>'stylesheet','href'=>\RainLoop\Utils::WebStaticPath('css/app'.$sAppCssMin.'.css')], ''); + \OCP\Util::addHeader('style', ['id'=>'app-theme-style','data-href'=>$oActions->ThemeLink(false)], + \preg_replace( + '/\\s*([:;{},]+)\\s*/s', + '$1', + $oActions->compileCss($oActions->GetTheme(false), false) + )); + + $response = new TemplateResponse('snappymail', 'index_embed', $params); + + $csp = new ContentSecurityPolicy(); + $csp->addAllowedScriptDomain("'self'"); + $csp->useStrictDynamic(true); + $csp->allowEvalScript(true); // $csp->addAllowedScriptDomain("'unsafe-eval'"); + $csp->addAllowedStyleDomain("'self'"); + $response->setContentSecurityPolicy($csp); + + return $response; + } + +} diff --git a/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php b/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php index 940eeb43a..b4d92b32f 100644 --- a/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php +++ b/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php @@ -36,14 +36,15 @@ class SnappyMailHelper if (!\class_exists('RainLoop\\Api')) { if ($api) { $_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true; - } else { - $_SERVER['SCRIPT_NAME'] = \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/index.php'; } $_ENV['SNAPPYMAIL_NEXTCLOUD'] = true; + $_SERVER['SCRIPT_NAME'] = \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/index.php'; + $sData = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/appdata_snappymail/'; if (\is_dir($sData)) { \define('APP_DATA_FOLDER_PATH', $sData); } + // Nextcloud the default spl_autoload_register() not working \spl_autoload_register(function($sClassName){ $file = RAINLOOP_APP_LIBRARIES_PATH . \strtolower(\strtr($sClassName, '\\', DIRECTORY_SEPARATOR)) . '.php'; @@ -51,6 +52,7 @@ class SnappyMailHelper include_once $file; } }); + require_once \OC::$server->getAppManager()->getAppPath('snappymail') . '/app/index.php'; } } diff --git a/integrations/nextcloud/snappymail/templates/index_embed.php b/integrations/nextcloud/snappymail/templates/index_embed.php new file mode 100755 index 000000000..3356099e0 --- /dev/null +++ b/integrations/nextcloud/snappymail/templates/index_embed.php @@ -0,0 +1,17 @@ +