Nextcloud 34 changes #2055

This commit is contained in:
the-djmaze 2026-10-30 15:05:39 +01:00
parent 8afb1049f7
commit 49ddcc7dfd
14 changed files with 150 additions and 113 deletions

View file

@ -29,6 +29,7 @@
*/
if (class_exists('OC')) {
define('APP_DATA_FOLDER_PATH', \rtrim(\trim(
// \OCA\SnappyMail\Util\SnappyMailHelper::getSystemConfigValue('datadirectory', '')
\method_exists(\OC::$server, 'getSystemConfig')
? \OC::$server->getSystemConfig()->getValue('datadirectory', '')
// Nextcloud 34+

View file

@ -72,22 +72,21 @@ class Application extends App implements IBootstrap
public function boot(IBootContext $context): void
{
if (!\is_dir(\rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/') . '/appdata_snappymail')) {
if (!\is_dir(\rtrim(\trim(SnappyMailHelper::getSystemConfigValue('datadirectory', '')), '\\/') . '/appdata_snappymail')) {
return;
}
$dispatcher = $context->getAppContainer()->query('OCP\EventDispatcher\IEventDispatcher');
$dispatcher->addListener(PostLoginEvent::class, function (PostLoginEvent $Event) {
/*
$config = \OC::$server->getConfig();
// 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)
|| $config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
if (SnappyMailHelper::getAppConfigValue('snappymail-autologin', false)
|| SnappyMailHelper::getAppConfigValue('snappymail-autologin-with-email', false)) {
*/
$sUID = $Event->getUser()->getUID();
\OC::$server->getSession()['snappymail-nc-uid'] = $sUID;
\OC::$server->getSession()['snappymail-passphrase'] = SnappyMailHelper::encodePassword($Event->getPassword(), $sUID);
SnappyMailHelper::setSessionValue('snappymail-nc-uid', $sUID);
SnappyMailHelper::setSessionValue('snappymail-passphrase', SnappyMailHelper::encodePassword($Event->getPassword(), $sUID));
/*
}
*/
@ -95,7 +94,7 @@ class Application extends App implements IBootstrap
$dispatcher->addListener(BeforeUserLoggedOutEvent::class, function (BeforeUserLoggedOutEvent $Event) {
// https://github.com/nextcloud/server/issues/36083#issuecomment-1387370634
// \OC::$server->getSession()['snappymail-passphrase'] = '';
// SnappyMailHelper::setSessionValue('snappymail-passphrase', '');
SnappyMailHelper::loadApp();
// \RainLoop\Api::Actions()->Logout(true);
\RainLoop\Api::Actions()->DoLogout();
@ -106,12 +105,12 @@ class Application extends App implements IBootstrap
$class = 'OCA\Impersonate\Events\BeginImpersonateEvent';
if (\class_exists($class)) {
$dispatcher->addListener($class, function ($Event) {
\OC::$server->getSession()['snappymail-passphrase'] = '';
SnappyMailHelper::setSessionValue('snappymail-passphrase', '');
SnappyMailHelper::loadApp();
\RainLoop\Api::Actions()->Logout(true);
});
$dispatcher->addListener('OCA\Impersonate\Events\EndImpersonateEvent', function ($Event) {
\OC::$server->getSession()['snappymail-passphrase'] = '';
SnappyMailHelper::setSessionValue('snappymail-passphrase', '');
SnappyMailHelper::loadApp();
\RainLoop\Api::Actions()->Logout(true);
});

View file

@ -40,7 +40,7 @@ class ContentSecurityPolicy extends \OCP\AppFramework\Http\ContentSecurityPolicy
$cspManager = (\method_exists(\OC::$server, 'getContentSecurityPolicyNonceManager')
? \OC::$server->getContentSecurityPolicyNonceManager()
// Nextcloud 34+
: \OC::$server->get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class)
: \OCP\Server::get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class)
);
$sNonce = $cspManager->getNonce() ?: \SnappyMail\UUID::generate();
if (\method_exists($cspManager, 'browserSupportsCspV3') && !$cspManager->browserSupportsCspV3()) {

View file

@ -104,7 +104,7 @@ class FetchController extends Controller {
try {
$sEmail = '';
if (isset($_POST['appname'], $_POST['snappymail-password'], $_POST['snappymail-email']) && 'snappymail' === $_POST['appname']) {
$sUser = \OC::$server->getUserSession()->getUser()->getUID();
$sUser = SnappyMailHelper::getUID();
$sEmail = $_POST['snappymail-email'];
$this->config->setUserValue($sUser, 'snappymail', 'snappymail-email', $sEmail);

View file

@ -16,8 +16,6 @@ class PageController extends Controller
*/
public function index()
{
$config = \OC::$server->getConfig();
$bAdmin = false;
if (!empty($_SERVER['QUERY_STRING'])) {
SnappyMailHelper::loadApp();
@ -27,7 +25,7 @@ class PageController extends Controller
}
}
if (!$bAdmin && $config->getAppValue('snappymail', 'snappymail-no-embed')) {
if (!$bAdmin && SnappyMailHelper::getAppConfigValue('snappymail-no-embed')) {
\OC::$server->getNavigationManager()->setActiveEntry('snappymail');
\OCP\Util::addScript('snappymail', 'snappymail');
\OCP\Util::addStyle('snappymail', 'style');

View file

@ -91,8 +91,7 @@ class Provider implements IProvider
$MessageCollection = $oMailClient->MessageList($oParams);
$baseURL = $this->urlGenerator->linkToRoute('snappymail.page.index');
// $config = \OC::$server->getConfig();
// if ($config->getAppValue('snappymail', 'snappymail-no-embed')) {
// if (SnappyMailHelper::getAppConfigValue('snappymail-no-embed')) {
// $baseURL .= '?target=';
// } else {
$baseURL .= '#';

View file

@ -17,7 +17,7 @@ class AdminSettings implements ISettings
public function getForm()
{
\OCA\SnappyMail\Util\SnappyMailHelper::loadApp();
SnappyMailHelper::loadApp();
$keys = [
'snappymail-autologin',
@ -27,10 +27,9 @@ class AdminSettings implements ISettings
];
$parameters = [];
foreach ($keys as $k) {
$v = $this->config->getAppValue('snappymail', $k);
$parameters[$k] = $v;
$parameters[$k] = $this->config->getAppValue('snappymail', $k);
}
$uid = \OC::$server->getUserSession()->getUser()->getUID();
$uid = SnappyMailHelper::getUID();
if (\OC_User::isAdminUser($uid)) {
// $parameters['snappymail-admin-panel-link'] = SnappyMailHelper::getAppUrl().'?admin';
SnappyMailHelper::loadApp();
@ -49,7 +48,7 @@ class AdminSettings implements ISettings
$parameters['snappymail-admin-password'] = $sPassword;
$parameters['can-import-rainloop'] = $sPassword && \is_dir(
\rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/')
\rtrim(\trim(SnappyMailHelper::getSystemConfigValue('datadirectory', '')), '\\/')
. '/rainloop-storage'
);

View file

@ -1,6 +1,7 @@
<?php
namespace OCA\SnappyMail\Settings;
use OCA\SnappyMail\Util\SnappyMailHelper;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\Settings\ISettings;
@ -16,7 +17,7 @@ class PersonalSettings implements ISettings
public function getForm()
{
$uid = \OC::$server->getUserSession()->getUser()->getUID();
$uid = SnappyMailHelper::getUID();
$sEmail = $this->config->getUserValue($uid, 'snappymail', 'snappymail-email');
if ($sPass = $this->config->getUserValue($uid, 'snappymail', 'snappymail-password')) {
$this->config->deleteUserValue($uid, 'snappymail', 'snappymail-password');

View file

@ -13,9 +13,9 @@ class RainLoop
* skips: /data/rainloop-storage/INSTALLED
* skips: /data/rainloop-storage/index.php
*/
public static function import() : array
public static function import(): array
{
$dir = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/');
$dir = \rtrim(\trim(SnappyMailHelper::getSystemConfigValue('datadirectory', '')), '\\/');
$dir_snappy = $dir . '/appdata_snappymail/';
$dir_rainloop = $dir . '/rainloop-storage';
$result = [];

View file

@ -15,10 +15,85 @@ class SnappyMailResponse extends \OCP\AppFramework\Http\Response
}
}
/**
* Nextcloud 34+
* @CarlSchwan 2026-03-10
* refactor: remove long deprecated IServerContainer methods
*
* https://github.com/nextcloud/server/commit/39c14c383b698467711a2de09870d7c032d92f63
*/
class SnappyMailHelper
{
public static function getConfig(): object
{
return \method_exists(\OC::$server, 'getConfig')
? \OC::$server->getConfig()
// Nextcloud 34+
: \OCP\Server::get(\OCP\IConfig::class);
}
public static function loadApp() : void
public static function getAppConfigValue(string $key, mixed $default = null): object
{
return (\method_exists(\OC::$server, 'getConfig')
? \OC::$server->getConfig()
// Nextcloud 34+
: \OCP\Server::get(\OCP\IAppConfig::class)
)->getAppValue('snappymail', $key, $default);
}
public static function getSystemConfigValue(string $key, mixed $default = null): mixed
{
return \method_exists(\OC::$server, 'getSystemConfig')
? \OC::$server->getSystemConfig()->getValue($key, $default)
// Nextcloud 34+
: \OCP\Server::get(\OCP\IConfig::class)->getSystemValue($key, $default);
}
public static function getUID(): mixed
{
return static::getUser()->getUID();
}
public static function getUser(): object
{
return static::getUserSession()->getUser();
}
public static function getUserSession(): object
{
return (\method_exists(\OC::$server, 'getUserSession')
? \OC::$server->getUserSession()
// Nextcloud 34+
: \OCP\Server::get(\OCP\IUserSession::class)
);
}
public static function getSession(): object
{
return (\method_exists(\OC::$server, 'getSession')
? \OC::$server->getSession()
// Nextcloud 34+
: \OCP\Server::get(\OCP\ISession::class)
);
}
public static function getSessionValue(string $key): mixed
{
return static::getSession()->get($key);
}
public static function setSessionValue(string $key, mixed $value): void
{
if (\method_exists(\OC::$server, 'getSession')) {
\OC::$server->getSession()[$key] = $value;
} else {
// Nextcloud 34+
\OCP\Server::get(\OCP\ISession::class)->set($key, $value);
}
}
public static function loadApp(): void
{
if (\class_exists('RainLoop\\Api')) {
return;
@ -37,13 +112,13 @@ class SnappyMailHelper
// define('APP_VERSION', '0.0.0');
// define('APP_INDEX_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
// include APP_INDEX_ROOT_PATH.'snappymail/v/'.APP_VERSION.'/include.php';
// define('APP_DATA_FOLDER_PATH', \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/appdata_snappymail/');
// define('APP_DATA_FOLDER_PATH', \rtrim(\trim(static::getSystemConfigValue('datadirectory', '')), '\\/').'/appdata_snappymail/');
$app_dir = \dirname(\dirname(__DIR__)) . '/app';
require_once $app_dir . '/index.php';
}
public static function startApp(bool $handle = false)
public static function startApp(bool $handle = false): void
{
static::loadApp();
@ -57,7 +132,7 @@ class SnappyMailHelper
$oActions = \RainLoop\Api::Actions();
if (isset($_GET[$oConfig->Get('security', 'admin_panel_key', 'admin')])) {
if ($oConfig->Get('security', 'allow_admin_panel', true)
&& \OC_User::isAdminUser(\OC::$server->getUserSession()->getUser()->getUID())
&& \OC_User::isAdminUser(static::getUID())
&& !$oActions->IsAdminLoggined(false)
) {
$sRand = \MailSo\Base\Utils::Sha1Rand();
@ -79,7 +154,7 @@ class SnappyMailHelper
// file_get_contents(\OC::$SERVERROOT . 'config/config.php');
// $CONFIG['version']
if (24 < $OC_Version[0]) {
$ocSession = \OC::$server->getSession();
$ocSession = static::getSession();
$ocSession->reopen();
if (!$doLogin && $ocSession['snappymail-uid'] && $ocSession['snappymail-uid'] != $aCredentials[0]) {
// UID changed, Impersonate plugin probably active
@ -92,7 +167,6 @@ class SnappyMailHelper
if ($doLogin && $aCredentials[1] && $aCredentials[2]) {
$isOIDC = \str_starts_with($aCredentials[2], 'oidc_login|');
try {
$ocSession = \OC::$server->getSession();
$oAccount = $oActions->LoginProcess($aCredentials[1], $aCredentials[2]);
if (!$isOIDC && $oAccount
&& $oConfig->Get('login', 'sign_me_auto', \RainLoop\Enumerations\SignMeType::DefaultOff) === \RainLoop\Enumerations\SignMeType::DefaultOn
@ -102,9 +176,9 @@ class SnappyMailHelper
} catch (\Throwable $e) {
// Login failure, reset password to prevent more attempts
if (!$isOIDC) {
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
\OC::$server->getSession()['snappymail-passphrase'] = '';
\OC::$server->getConfig()->setUserValue($sUID, 'snappymail', 'passphrase', '');
$sUID = static::getUID();
static::setSessionValue('snappymail-passphrase', '');
static::getConfig()->setUserValue($sUID, 'snappymail', 'passphrase', '');
\SnappyMail\Log::error('Nextcloud', $e->getMessage());
}
}
@ -125,14 +199,13 @@ class SnappyMailHelper
// Check if OpenID Connect (OIDC) is enabled and used for login
// https://apps.nextcloud.com/apps/oidc_login
public static function isOIDCLogin() : bool
public static function isOIDCLogin(): bool
{
$config = \OC::$server->getConfig();
if ($config->getAppValue('snappymail', 'snappymail-autologin-oidc', false)) {
if (static::getAppConfigValue('snappymail-autologin-oidc', false)) {
// Check if the OIDC Login app is enabled
if (\OC::$server->getAppManager()->isEnabledForUser('oidc_login')) {
// Check if session is an OIDC Login
$ocSession = \OC::$server->getSession();
$ocSession = static::getSession();
if ($ocSession->get('is_oidc')) {
// IToken->getPassword() ???
if ($ocSession->get('oidc_access_token')) {
@ -149,11 +222,11 @@ class SnappyMailHelper
return false;
}
private static function getLoginCredentials() : array
private static function getLoginCredentials(): array
{
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
$config = \OC::$server->getConfig();
$ocSession = \OC::$server->getSession();
$sUID = static::getUID();
$config = static::getConfig();
$ocSession = static::getSession();
// If the user has set credentials for SnappyMail in their personal settings,
// this has the first priority.
@ -203,12 +276,12 @@ class SnappyMailHelper
return [$sUID, '', ''];
}
public static function getAppUrl() : string
public static function getAppUrl(): string
{
return \OC::$server->getURLGenerator()->linkToRoute('snappymail.page.appGet');
}
public static function normalizeUrl(string $sUrl) : string
public static function normalizeUrl(string $sUrl): string
{
$sUrl = \rtrim(\trim($sUrl), '/\\');
if ('.php' !== \strtolower(\substr($sUrl, -4))) {
@ -218,13 +291,13 @@ class SnappyMailHelper
return $sUrl;
}
public static function encodePassword(string $sPassword, string $sSalt) : string
public static function encodePassword(string $sPassword, string $sSalt): string
{
static::loadApp();
return \SnappyMail\Crypt::EncryptUrlSafe($sPassword, $sSalt);
}
public static function decodePassword(string $sPassword, string $sSalt) : ?\SnappyMail\SensitiveString
public static function decodePassword(string $sPassword, string $sSalt): ?\SnappyMail\SensitiveString
{
static::loadApp();
$result = \SnappyMail\Crypt::DecryptUrlSafe($sPassword, $sSalt);