From 8b842d920d39688936163c97b48a10419322df7c Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 11 Oct 2022 10:18:43 +0200 Subject: [PATCH] Nextcloud remove deprecated jQuery code and get things working properly #96 --- .../nextcloud/snappymail/css/style.css | 0 integrations/nextcloud/snappymail/js/admin.js | 12 +- .../nextcloud/snappymail/js/personal.js | 12 +- .../nextcloud/snappymail/js/rainloop.js | 114 ---------------- .../nextcloud/snappymail/js/resize.js | 32 ++--- .../nextcloud/snappymail/js/snappymail.js | 122 ++++++++++++++++++ .../lib/Controller/AjaxController.php | 6 +- .../lib/Controller/PageController.php | 26 +++- .../snappymail/lib/Util/SnappyMailHelper.php | 21 +-- .../snappymail/templates/admin-local.php | 2 +- .../templates/personal_settings.php | 2 +- 11 files changed, 169 insertions(+), 180 deletions(-) mode change 100755 => 100644 integrations/nextcloud/snappymail/css/style.css mode change 100755 => 100644 integrations/nextcloud/snappymail/js/admin.js mode change 100755 => 100644 integrations/nextcloud/snappymail/js/personal.js delete mode 100755 integrations/nextcloud/snappymail/js/rainloop.js mode change 100755 => 100644 integrations/nextcloud/snappymail/js/resize.js create mode 100644 integrations/nextcloud/snappymail/js/snappymail.js diff --git a/integrations/nextcloud/snappymail/css/style.css b/integrations/nextcloud/snappymail/css/style.css old mode 100755 new mode 100644 diff --git a/integrations/nextcloud/snappymail/js/admin.js b/integrations/nextcloud/snappymail/js/admin.js old mode 100755 new mode 100644 index e84b4a0c0..a57144112 --- a/integrations/nextcloud/snappymail/js/admin.js +++ b/integrations/nextcloud/snappymail/js/admin.js @@ -1,12 +1,2 @@ -/** - * Nextcloud - SnappyMail mail plugin - * - * @author SnappyMail Team, Nextgen-Networks (@nextgen-networks), Tab Fitts (@tabp0le), Pierre-Alain Bandinelli (@pierre-alain-b) - * - * Based initially on https://github.com/SnappyMail/snappymail-webmail/tree/master/build/owncloud - */ - -$(function() { - SnappyMailFormHelper('#mail-snappymail-admin-form', 'admin.php'); -}); +SnappyMailFormHelper('admin-form', 'admin.php'); diff --git a/integrations/nextcloud/snappymail/js/personal.js b/integrations/nextcloud/snappymail/js/personal.js old mode 100755 new mode 100644 index b2a7d7c3d..9d50805b2 --- a/integrations/nextcloud/snappymail/js/personal.js +++ b/integrations/nextcloud/snappymail/js/personal.js @@ -1,12 +1,2 @@ -/** - * Nextcloud - SnappyMail mail plugin - * - * @author SnappyMail Team, Nextgen-Networks (@nextgen-networks), Tab Fitts (@tabp0le), Pierre-Alain Bandinelli (@pierre-alain-b) - * - * Based initially on https://github.com/SnappyMail/snappymail-webmail/tree/master/build/owncloud - */ - -$(function() { - SnappyMailFormHelper('#mail-snappymail-personal-form', 'personal.php'); -}); +SnappyMailFormHelper('personal-form', 'personal.php'); diff --git a/integrations/nextcloud/snappymail/js/rainloop.js b/integrations/nextcloud/snappymail/js/rainloop.js deleted file mode 100755 index d8687f21b..000000000 --- a/integrations/nextcloud/snappymail/js/rainloop.js +++ /dev/null @@ -1,114 +0,0 @@ -// Do the following things once the document is fully loaded. -document.onreadystatechange = function () { - if (document.readyState === 'complete') { - watchIFrameTitle(); - } -} - -// The SnappyMail application is already configured to modify the element -// of its root document with the number of unread messages in the inbox. -// However, its document is the SnappyMail iframe. This function sets up a -// Mutation Observer to watch the <title> element of the iframe for changes in -// the unread message count and propagates that to the parent <title> element, -// allowing the unread message count to be displayed in the NC tab's text when -// the SnappyMail app is selected. -function watchIFrameTitle() { - iframe = document.getElementById('rliframe'); - if (!iframe) { - return; - } - target = iframe.contentDocument.getElementsByTagName('title')[0]; - config = { - characterData: true, - childList: true, - subtree: true - } - observer = new MutationObserver(function(mutations) { - title = mutations[0].target.innerText; - if (title) { - matches = title.match(/\(([0-9]+)\)/); - if (matches) { - document.title = '('+ matches[1] + ') ' + t('snappymail', 'Email') + ' - Nextcloud'; - } else { - document.title = t('snappymail', 'Email') + ' - Nextcloud'; - } - } - }); - observer.observe(target, config); -} - -function SnappyMailFormHelper(sID, sAjaxFile, fCallback) -{ - try - { - var - oForm = $(sID), - oSubmit = $('#snappymail-save-button', oForm), - sSubmitValue = oSubmit.val(), - oDesc = oForm.find('.snappymail-result-desc') - ; - - oSubmit.click(function (oEvent) { - - var oDefAjax = null; - - oEvent.preventDefault(); - - oForm - .addClass('snappymail-ajax') - .removeClass('snappymail-error') - .removeClass('snappymail-success') - ; - - oDesc.text(''); - oSubmit.val('...'); - - oDefAjax = $.ajax({ - 'type': 'POST', - 'async': true, - 'url': OC.filePath('snappymail', 'ajax', sAjaxFile), - 'data': oForm.serialize(), - 'dataType': 'json', - 'global': true - }); - - oDefAjax.always(function (oData) { - - var bResult = false; - - oForm.removeClass('snappymail-ajax'); - oSubmit.val(sSubmitValue); - - if (oData) - { - bResult = 'success' === oData['status']; - if (oData['Message']) - { - oDesc.text(t('snappymail', oData['Message'])); - } - } - - if (bResult) - { - oForm.addClass('snappymail-success'); - } - else - { - oForm.addClass('snappymail-error'); - if ('' === oDesc.text()) - { - oDesc.text(t('snappymail', 'Error')); - } - } - - if (fCallback) - { - fCallback(bResult, oData); - } - }); - - return false; - }); - } - catch(e) {} -} diff --git a/integrations/nextcloud/snappymail/js/resize.js b/integrations/nextcloud/snappymail/js/resize.js old mode 100755 new mode 100644 index 5c16f0022..bacff7aa0 --- a/integrations/nextcloud/snappymail/js/resize.js +++ b/integrations/nextcloud/snappymail/js/resize.js @@ -1,26 +1,18 @@ -$(function (window, document) { +(()=>{ - var + const buffer = 0, //was 5 but this was creating a white space of 5px at the bottom of the page - ifr = document.getElementById('rliframe') - ; - - - function pageY(elem) { - return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop; - } - - function resizeIframe() { - var height = document.documentElement.clientHeight; - height -= pageY(ifr) + buffer; - height = (height < 0) ? 0 : height; - ifr.style.height = height + 'px'; - } - - if (ifr) - { + ifr = document.getElementById('rliframe'), + pageY = elem => elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop, + resizeIframe = () => { + var height = document.documentElement.clientHeight; + height -= pageY(ifr) + buffer; + height = (height < 0) ? 0 : height; + ifr.style.height = height + 'px'; + }; + if (ifr) { ifr.onload = resizeIframe; window.onresize = resizeIframe; } -}(window, document)); +})(); diff --git a/integrations/nextcloud/snappymail/js/snappymail.js b/integrations/nextcloud/snappymail/js/snappymail.js new file mode 100644 index 000000000..b811ec868 --- /dev/null +++ b/integrations/nextcloud/snappymail/js/snappymail.js @@ -0,0 +1,122 @@ +/** + * Nextcloud - SnappyMail mail plugin + * + * @author RainLoop Team, Nextgen-Networks (@nextgen-networks), Tab Fitts (@tabp0le), Pierre-Alain Bandinelli (@pierre-alain-b), SnappyMail + * + * Based initially on https://github.com/RainLoop/rainloop-webmail/tree/master/build/owncloud/rainloop-app + */ + +// Do the following things once the document is fully loaded. +document.onreadystatechange = function () { + if (document.readyState === 'complete') { + watchIFrameTitle(); + } +} + +// The SnappyMail application is already configured to modify the <title> element +// of its root document with the number of unread messages in the inbox. +// However, its document is the SnappyMail iframe. This function sets up a +// Mutation Observer to watch the <title> element of the iframe for changes in +// the unread message count and propagates that to the parent <title> element, +// allowing the unread message count to be displayed in the NC tab's text when +// the SnappyMail app is selected. +function watchIFrameTitle() { + let iframe = document.getElementById('rliframe'); + if (!iframe) { + return; + } + let target = iframe.contentDocument.getElementsByTagName('title')[0]; + let config = { + characterData: true, + childList: true, + subtree: true + }; + let observer = new MutationObserver(function(mutations) { + let title = mutations[0].target.innerText; + if (title) { + let matches = title.match(/\(([0-9]+)\)/); + if (matches) { + document.title = '('+ matches[1] + ') ' + t('snappymail', 'Email') + ' - Nextcloud'; + } else { + document.title = t('snappymail', 'Email') + ' - Nextcloud'; + } + } + }); + observer.observe(target, config); +} + +function SnappyMailFormHelper(sID, sAjaxFile, fCallback) +{ + try + { + var + oForm = document.getElementById('mail-snappymail-' + sID), + oSubmit = document.getElementById('snappymail-save-button'), + sSubmitValue = oSubmit.textContent, + oDesc = oForm.querySelector('.snappymail-result-desc') + ; + + oForm.addEventListener('submit', (...args) => { + console.dir({args:args}); + return false + }); + + oSubmit.onclick = oEvent => { + + oEvent.preventDefault(); + + oForm.classList.add('snappymail-ajax') + oForm.classList.remove('snappymail-error') + oForm.classList.remove('snappymail-success') + + oDesc.textContent = ''; + oSubmit.textContent = '...'; + + fetch(OC.filePath('snappymail', 'ajax', sAjaxFile), { + mode: 'same-origin', + cache: 'no-cache', + redirect: 'error', + referrerPolicy: 'no-referrer', + credentials: 'same-origin', + method: 'POST', +/* + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + 'snappymail-email': document.getElementById('snappymail-email').value, + 'snappymail-password': document.getElementById('snappymail-password').value + }) +*/ + headers: {}, + body: new FormData(oForm) + }) + .then(response => response.json()) + .then(oData => { + let bResult = false; + oForm.classList.remove('snappymail-ajax'); + oSubmit.textContent = sSubmitValue; + if (oData) { + bResult = 'success' === oData.status; + if (oData.Message) { + oDesc.textContent = t('snappymail', oData.Message); + } + } + if (bResult) { + oForm.classList.add('snappymail-success'); + } else { + oForm.classList.add('snappymail-error'); + if ('' === oDesc.textContent) { + oDesc.textContent = t('snappymail', 'Error'); + } + } + if (fCallback) { + fCallback(bResult, oData); + } + }); + + return false; + }; + } + catch(e) { + console.error(e); + } +} diff --git a/integrations/nextcloud/snappymail/lib/Controller/AjaxController.php b/integrations/nextcloud/snappymail/lib/Controller/AjaxController.php index 8fbe5aeb2..d350e1f3c 100644 --- a/integrations/nextcloud/snappymail/lib/Controller/AjaxController.php +++ b/integrations/nextcloud/snappymail/lib/Controller/AjaxController.php @@ -53,9 +53,9 @@ class AjaxController extends Controller { } } - /** - * @NoAdminRequired - */ + /** + * @NoAdminRequired + */ public function setPersonal(): JSONResponse { try { diff --git a/integrations/nextcloud/snappymail/lib/Controller/PageController.php b/integrations/nextcloud/snappymail/lib/Controller/PageController.php index 48d3e0d18..ef51eebea 100644 --- a/integrations/nextcloud/snappymail/lib/Controller/PageController.php +++ b/integrations/nextcloud/snappymail/lib/Controller/PageController.php @@ -20,10 +20,30 @@ class PageController extends Controller \OCP\Util::addStyle('snappymail', 'style'); - $session = \OC::$server->getSession(); + $query = ''; + // If the user has set credentials for SnappyMail in their personal + // settings, override everything before and use those instead. + $config = \OC::$server->getConfig(); + $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']; + } + } + $params = [ - 'snappymail-iframe-url' => SnappyMailHelper::normalizeUrl(SnappyMailHelper::getAppUrl()) - . (empty($session['snappymail-sso-hash']) ? '' : '?sso&hash=' . $session['snappymail-sso-hash']) + 'snappymail-iframe-url' => SnappyMailHelper::normalizeUrl(SnappyMailHelper::getAppUrl()) . $query ]; $response = new TemplateResponse('snappymail', 'index', $params); diff --git a/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php b/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php index 16e49a66c..940eeb43a 100644 --- a/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php +++ b/integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php @@ -7,36 +7,25 @@ class SnappyMailHelper public function registerHooks() { - $config = \OC::$server->getConfig(); - $session = \OC::$server->getSession(); $userSession = \OC::$server->getUserSession(); $userSession->listen('\OC\User', 'postLogin', function($user, $loginName, $password, $isTokenLogin) { + $config = \OC::$server->getConfig(); $sEmail = ''; - // If the user has set credentials for SnappyMail in their personal - // settings, override everything before and use those instead. - $sIndividualEmail = $config->getUserValue($user->getUID(), 'snappymail', 'snappymail-email', ''); - if ($sIndividualEmail) { - $sEmail = $sIndividualEmail; - $password = SnappyMailHelper::decodePassword( - $this->config->getUserValue($sUser, 'snappymail', 'snappymail-password', ''), - \md5($sEmail) - ); - } // Only store the user's password in the current session if they have // enabled auto-login using Nextcloud username or email address. - else if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) { + if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) { $sEmail = $user->getUID(); } else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) { $sEmail = $config->getUserValue($user->getUID(), 'settings', 'email', ''); } if ($sEmail) { static::startApp(true); - $session['snappymail-sso-hash'] = \RainLoop\Api::CreateUserSsoHash($sEmail, $password/*, array $aAdditionalOptions = array(), bool $bUseTimeout = true*/); + \OC::$server->getSession()['snappymail-sso-hash'] = \RainLoop\Api::CreateUserSsoHash($sEmail, $password/*, array $aAdditionalOptions = array(), bool $bUseTimeout = true*/); } }); $userSession->listen('\OC\User', 'logout', function($user) { - $session['snappymail-sso-hash'] = ''; + \OC::$server->getSession()['snappymail-sso-hash'] = ''; static::startApp(true); \RainLoop\Api::LogoutCurrentLogginedUser(); }); @@ -51,7 +40,7 @@ class SnappyMailHelper $_SERVER['SCRIPT_NAME'] = \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/index.php'; } $_ENV['SNAPPYMAIL_NEXTCLOUD'] = true; - $sData = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/snappymail/'; + $sData = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/appdata_snappymail/'; if (\is_dir($sData)) { \define('APP_DATA_FOLDER_PATH', $sData); } diff --git a/integrations/nextcloud/snappymail/templates/admin-local.php b/integrations/nextcloud/snappymail/templates/admin-local.php index c7ee3c9a8..c1fc733e7 100755 --- a/integrations/nextcloud/snappymail/templates/admin-local.php +++ b/integrations/nextcloud/snappymail/templates/admin-local.php @@ -37,7 +37,7 @@ </div> <br /> <br /> - <input type="button" id="snappymail-save-button" name="snappymail-save-button" value="<?php echo($l->t('Save')); ?>" /> + <button id="snappymail-save-button" name="snappymail-save-button"><?php echo($l->t('Save')); ?></button>   <span class="snappymail-result-desc"></span> </p> </fieldset> diff --git a/integrations/nextcloud/snappymail/templates/personal_settings.php b/integrations/nextcloud/snappymail/templates/personal_settings.php index ec5ff5026..68c6f17f7 100755 --- a/integrations/nextcloud/snappymail/templates/personal_settings.php +++ b/integrations/nextcloud/snappymail/templates/personal_settings.php @@ -24,7 +24,7 @@ $links = [ <input type="password" id="snappymail-password" name="snappymail-password" value="<?php echo $_['snappymail-password']; ?>" placeholder="<?php echo($l->t('Password')); ?>" /> - <input type="button" id="snappymail-save-button" name="snappymail-save-button" value="<?php echo($l->t('Save')); ?>" /> + <button id="snappymail-save-button" name="snappymail-save-button"><?php echo($l->t('Save')); ?></button>   <span class="snappymail-result-desc"></span> </p> </fieldset>