From 58d85f21da4f4a620ee5f90c170354561831bcf1 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 27 Apr 2022 22:02:58 +0100 Subject: [PATCH] MDL-74585 core: pass string identifier/component to network keepalive. Avoid passing potentially large amounts of data in the page AMD call, allow the `core/network` module to load actual string content itself. --- lib/amd/build/network.min.js | 2 +- lib/amd/build/network.min.js.map | 2 +- lib/amd/src/network.js | 7 ++++--- lib/classes/session/manager.php | 3 ++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/amd/build/network.min.js b/lib/amd/build/network.min.js index 3ffe5835695..d797fb65821 100644 --- a/lib/amd/build/network.min.js +++ b/lib/amd/build/network.min.js @@ -5,6 +5,6 @@ * @copyright 2019 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define("core/network",["jquery","core/ajax","core/config","core/notification","core/str"],(function($,Ajax,Config,Notification,Str){var started=!1,warningDisplayed=!1,keepAliveFrequency=0,requestTimeout=0,keepAliveMessage=!1,sessionTimeout=!1,checkFrequency=1e3*Math.min(Config.sessiontimeout/10,600),warningLimit=Config.sessiontimeoutwarning>0?1e3*Config.sessiontimeoutwarning:2*checkFrequency,firstWait=Config.sessiontimeoutwarning>0?Math.min(1e3*(Config.sessiontimeout-Config.sessiontimeoutwarning),5*checkFrequency):5*checkFrequency,timeoutSessionExpired=function(modal){sessionTimeout=!0,warningDisplayed=!1,closeModal(modal),displaySessionExpired()},closeModal=function(modal){modal.destroy()},displaySessionExpired=function(){return Ajax.call([{methodname:"core_session_time_remaining",args:{}}],!0,!0,!0)[0].then((function(args){return!(1e3*args.timeremaining>warningLimit)&&Str.get_strings([{key:"sessionexpired",component:"error"},{key:"sessionerroruser",component:"error"},{key:"loginagain",component:"moodle"},{key:"cancel",component:"moodle"}]).then((function(strings){return Notification.confirm(strings[0],strings[1],strings[2],strings[3],(function(){return location.reload(),!0})),!0})).catch(Notification.exception)}))},touchSession=function(){return sessionTimeout?displaySessionExpired():Ajax.call([{methodname:"core_session_touch",args:{}}],!0,!0,!1,requestTimeout)[0].then((function(){return keepAliveFrequency>0&&setTimeout(touchSession,keepAliveFrequency),!0})).catch((function(){Notification.alert("",keepAliveMessage)}))},checkSession=function(){return sessionTimeout=!1,Ajax.call([{methodname:"core_session_time_remaining",args:{}}],!0,!0,!0)[0].then((function(args){return!(args.userid<=0)&&(args.timeremaining<=0?displaySessionExpired():(1e3*args.timeremaining<=warningLimit&&!warningDisplayed?(warningDisplayed=!0,Str.get_strings([{key:"norecentactivity",component:"moodle"},{key:"sessiontimeoutsoon",component:"moodle"},{key:"extendsession",component:"moodle"},{key:"cancel",component:"moodle"}]).then((function(strings){return Notification.confirm(strings[0],strings[1],strings[2],strings[3],(function(){return touchSession(),warningDisplayed=!1,setTimeout(checkSession,firstWait),!0}),(function(){setTimeout(checkSession,checkFrequency)}))})).then((modal=>{setTimeout(timeoutSessionExpired,1e3*args.timeremaining,modal)})).catch(Notification.exception)):setTimeout(checkSession,checkFrequency),!0))}))},start=function(){keepAliveFrequency>0?setTimeout(touchSession,keepAliveFrequency):setTimeout(checkSession,firstWait)};const isMoodleIframe=function(){if(window.parent===window)return!1;let parentUrl;try{parentUrl=window.parent.location.href}catch(e){return!1}return parentUrl.startsWith(M.cfg.wwwroot)};return{keepalive:function(freq,timeout,message){started?window.console.warn("Ignoring session keep-alive. The core/network module was already initialised."):(started=!0,isMoodleIframe()?window.console.warn("Ignoring session keep-alive in this iframe inside another Moodle page."):(window.console.log("Starting Moodle session keep-alive."),keepAliveFrequency=1e3*freq,keepAliveMessage=message,requestTimeout=1e3*timeout,start()))},init:function(){started||(started=!0,isMoodleIframe()?window.console.log("Not starting Moodle session timeout warning in this iframe."):(window.console.log("Starting Moodle session timeout warning."),start()))}}})); +define("core/network",["jquery","core/ajax","core/config","core/notification","core/str"],(function($,Ajax,Config,Notification,Str){var started=!1,warningDisplayed=!1,keepAliveFrequency=0,requestTimeout=0,keepAliveMessage=!1,sessionTimeout=!1,checkFrequency=1e3*Math.min(Config.sessiontimeout/10,600),warningLimit=Config.sessiontimeoutwarning>0?1e3*Config.sessiontimeoutwarning:2*checkFrequency,firstWait=Config.sessiontimeoutwarning>0?Math.min(1e3*(Config.sessiontimeout-Config.sessiontimeoutwarning),5*checkFrequency):5*checkFrequency,timeoutSessionExpired=function(modal){sessionTimeout=!0,warningDisplayed=!1,closeModal(modal),displaySessionExpired()},closeModal=function(modal){modal.destroy()},displaySessionExpired=function(){return Ajax.call([{methodname:"core_session_time_remaining",args:{}}],!0,!0,!0)[0].then((function(args){return!(1e3*args.timeremaining>warningLimit)&&Str.get_strings([{key:"sessionexpired",component:"error"},{key:"sessionerroruser",component:"error"},{key:"loginagain",component:"moodle"},{key:"cancel",component:"moodle"}]).then((function(strings){return Notification.confirm(strings[0],strings[1],strings[2],strings[3],(function(){return location.reload(),!0})),!0})).catch(Notification.exception)}))},touchSession=function(){return sessionTimeout?displaySessionExpired():Ajax.call([{methodname:"core_session_touch",args:{}}],!0,!0,!1,requestTimeout)[0].then((function(){return keepAliveFrequency>0&&setTimeout(touchSession,keepAliveFrequency),!0})).catch((function(){Notification.alert("",keepAliveMessage)}))},checkSession=function(){return sessionTimeout=!1,Ajax.call([{methodname:"core_session_time_remaining",args:{}}],!0,!0,!0)[0].then((function(args){return!(args.userid<=0)&&(args.timeremaining<=0?displaySessionExpired():(1e3*args.timeremaining<=warningLimit&&!warningDisplayed?(warningDisplayed=!0,Str.get_strings([{key:"norecentactivity",component:"moodle"},{key:"sessiontimeoutsoon",component:"moodle"},{key:"extendsession",component:"moodle"},{key:"cancel",component:"moodle"}]).then((function(strings){return Notification.confirm(strings[0],strings[1],strings[2],strings[3],(function(){return touchSession(),warningDisplayed=!1,setTimeout(checkSession,firstWait),!0}),(function(){setTimeout(checkSession,checkFrequency)}))})).then((modal=>{setTimeout(timeoutSessionExpired,1e3*args.timeremaining,modal)})).catch(Notification.exception)):setTimeout(checkSession,checkFrequency),!0))}))},start=function(){keepAliveFrequency>0?setTimeout(touchSession,keepAliveFrequency):setTimeout(checkSession,firstWait)};const isMoodleIframe=function(){if(window.parent===window)return!1;let parentUrl;try{parentUrl=window.parent.location.href}catch(e){return!1}return parentUrl.startsWith(M.cfg.wwwroot)};return{keepalive:async function(freq,timeout,identifier,component){started?window.console.warn("Ignoring session keep-alive. The core/network module was already initialised."):(started=!0,isMoodleIframe()?window.console.warn("Ignoring session keep-alive in this iframe inside another Moodle page."):(window.console.log("Starting Moodle session keep-alive."),keepAliveFrequency=1e3*freq,keepAliveMessage=await Str.get_string(identifier,component),requestTimeout=1e3*timeout,start()))},init:function(){started||(started=!0,isMoodleIframe()?window.console.log("Not starting Moodle session timeout warning in this iframe."):(window.console.log("Starting Moodle session timeout warning."),start()))}}})); //# sourceMappingURL=network.min.js.map \ No newline at end of file diff --git a/lib/amd/build/network.min.js.map b/lib/amd/build/network.min.js.map index a1f9558e92b..3c57e409790 100644 --- a/lib/amd/build/network.min.js.map +++ b/lib/amd/build/network.min.js.map @@ -1 +1 @@ -{"version":3,"file":"network.min.js","sources":["../src/network.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Poll the server to keep the session alive.\n *\n * @module core/network\n * @copyright 2019 Damyon Wiese\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],\n function($, Ajax, Config, Notification, Str) {\n\n var started = false;\n var warningDisplayed = false;\n var keepAliveFrequency = 0;\n var requestTimeout = 0;\n var keepAliveMessage = false;\n var sessionTimeout = false;\n // 1/10 of session timeout, max of 10 minutes.\n var checkFrequency = Math.min((Config.sessiontimeout / 10), 600) * 1000;\n // Check if sessiontimeoutwarning is set or double the checkFrequency.\n var warningLimit = (Config.sessiontimeoutwarning > 0) ? (Config.sessiontimeoutwarning * 1000) : (checkFrequency * 2);\n // First wait is minimum of remaining time or half of the session timeout.\n var firstWait = (Config.sessiontimeoutwarning > 0) ?\n Math.min((Config.sessiontimeout - Config.sessiontimeoutwarning) * 1000, checkFrequency * 5) : checkFrequency * 5;\n /**\n * The session time has expired - we can't extend it now.\n * @param {Modal} modal\n */\n var timeoutSessionExpired = function(modal) {\n sessionTimeout = true;\n warningDisplayed = false;\n closeModal(modal);\n displaySessionExpired();\n };\n\n /**\n * Close modal - this relies on modal object passed from Notification.confirm.\n *\n * @param {Modal} modal\n */\n var closeModal = function(modal) {\n modal.destroy();\n };\n\n /**\n * The session time has expired - we can't extend it now.\n * @return {Promise}\n */\n var displaySessionExpired = function() {\n // Check again if its already extended before displaying session expired popup in case multiple tabs are open.\n var request = {\n methodname: 'core_session_time_remaining',\n args: { }\n };\n\n return Ajax.call([request], true, true, true)[0].then(function(args) {\n if (args.timeremaining * 1000 > warningLimit) {\n return false;\n } else {\n return Str.get_strings([\n {key: 'sessionexpired', component: 'error'},\n {key: 'sessionerroruser', component: 'error'},\n {key: 'loginagain', component: 'moodle'},\n {key: 'cancel', component: 'moodle'}\n ]).then(function(strings) {\n Notification.confirm(\n strings[0], // Title.\n strings[1], // Message.\n strings[2], // Login Again.\n strings[3], // Cancel.\n function() {\n location.reload();\n return true;\n }\n );\n return true;\n }).catch(Notification.exception);\n }\n });\n };\n\n /**\n * Ping the server to keep the session alive.\n *\n * @return {Promise}\n */\n var touchSession = function() {\n var request = {\n methodname: 'core_session_touch',\n args: { }\n };\n\n if (sessionTimeout) {\n // We timed out before we extended the session.\n return displaySessionExpired();\n } else {\n return Ajax.call([request], true, true, false, requestTimeout)[0].then(function() {\n if (keepAliveFrequency > 0) {\n setTimeout(touchSession, keepAliveFrequency);\n }\n return true;\n }).catch(function() {\n Notification.alert('', keepAliveMessage);\n });\n }\n };\n\n /**\n * Ask the server how much time is remaining in this session and\n * show confirm/cancel notifications if the session is about to run out.\n *\n * @return {Promise}\n */\n var checkSession = function() {\n var request = {\n methodname: 'core_session_time_remaining',\n args: { }\n };\n sessionTimeout = false;\n return Ajax.call([request], true, true, true)[0].then(function(args) {\n if (args.userid <= 0) {\n return false;\n }\n if (args.timeremaining <= 0) {\n return displaySessionExpired();\n } else if (args.timeremaining * 1000 <= warningLimit && !warningDisplayed) {\n warningDisplayed = true;\n Str.get_strings([\n {key: 'norecentactivity', component: 'moodle'},\n {key: 'sessiontimeoutsoon', component: 'moodle'},\n {key: 'extendsession', component: 'moodle'},\n {key: 'cancel', component: 'moodle'}\n ]).then(function(strings) {\n return Notification.confirm(\n strings[0], // Title.\n strings[1], // Message.\n strings[2], // Extend session.\n strings[3], // Cancel.\n function() {\n touchSession();\n warningDisplayed = false;\n // First wait is minimum of remaining time or half of the session timeout.\n setTimeout(checkSession, firstWait);\n return true;\n },\n function() {\n // User has cancelled notification.\n setTimeout(checkSession, checkFrequency);\n }\n );\n }).then(modal => {\n // If we don't extend the session before the timeout - warn.\n setTimeout(timeoutSessionExpired, args.timeremaining * 1000, modal);\n return;\n }).catch(Notification.exception);\n } else {\n setTimeout(checkSession, checkFrequency);\n }\n return true;\n });\n // We do not catch the fails from the above ajax call because they will fail when\n // we are not logged in - we don't need to take any action then.\n };\n\n /**\n * Start calling a function to check if the session is still alive.\n */\n var start = function() {\n if (keepAliveFrequency > 0) {\n setTimeout(touchSession, keepAliveFrequency);\n } else {\n // First wait is minimum of remaining time or half of the session timeout.\n setTimeout(checkSession, firstWait);\n }\n };\n\n /**\n * Are we in an iframe and the parent page is from the same Moodle site?\n *\n * @return {boolean} true if we are in an iframe in a page from this Moodle site.\n */\n const isMoodleIframe = function() {\n if (window.parent === window) {\n // Not in an iframe.\n return false;\n }\n\n // We are in an iframe. Is the parent from the same Moodle site?\n let parentUrl;\n try {\n parentUrl = window.parent.location.href;\n } catch (e) {\n // If we cannot access the URL of the parent page, it must be another site.\n return false;\n }\n\n return parentUrl.startsWith(M.cfg.wwwroot);\n };\n\n /**\n * Don't allow more than one of these polling loops in a single page.\n */\n var init = function() {\n // We only allow one concurrent instance of this checker.\n if (started) {\n return;\n }\n started = true;\n\n if (isMoodleIframe()) {\n window.console.log('Not starting Moodle session timeout warning in this iframe.');\n return;\n }\n\n window.console.log('Starting Moodle session timeout warning.');\n\n start();\n };\n\n /**\n * Start polling with more specific values for the frequency, timeout and message.\n *\n * @param {number} freq How ofter to poll the server.\n * @param {number} timeout The time to wait for each request to the server.\n * @param {string} message The message to display if the session is going to time out.\n */\n var keepalive = function(freq, timeout, message) {\n // We only allow one concurrent instance of this checker.\n if (started) {\n window.console.warn('Ignoring session keep-alive. The core/network module was already initialised.');\n return;\n }\n started = true;\n\n if (isMoodleIframe()) {\n window.console.warn('Ignoring session keep-alive in this iframe inside another Moodle page.');\n return;\n }\n\n window.console.log('Starting Moodle session keep-alive.');\n\n keepAliveFrequency = freq * 1000;\n keepAliveMessage = message;\n requestTimeout = timeout * 1000;\n start();\n };\n\n return {\n keepalive: keepalive,\n init: init\n };\n});\n"],"names":["define","$","Ajax","Config","Notification","Str","started","warningDisplayed","keepAliveFrequency","requestTimeout","keepAliveMessage","sessionTimeout","checkFrequency","Math","min","sessiontimeout","warningLimit","sessiontimeoutwarning","firstWait","timeoutSessionExpired","modal","closeModal","displaySessionExpired","destroy","call","methodname","args","then","timeremaining","get_strings","key","component","strings","confirm","location","reload","catch","exception","touchSession","setTimeout","alert","checkSession","userid","start","isMoodleIframe","window","parent","parentUrl","href","e","startsWith","M","cfg","wwwroot","keepalive","freq","timeout","message","console","warn","log","init"],"mappings":";;;;;;;AAsBAA,sBAAO,CAAC,SAAU,YAAa,cAAe,oBAAqB,aAC3D,SAASC,EAAGC,KAAMC,OAAQC,aAAcC,SAExCC,SA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MAAK,SAASD,cACvDA,KAAKgB,QAAU,KAGfhB,KAAKE,eAAiB,EACfN,yBACqB,IAArBI,KAAKE,eAAwBZ,eAAiBT,kBACrDA,kBAAmB,EACnBF,IAAIwB,YAAY,CACZ,CAACC,IAAK,mBAAoBC,UAAW,UACrC,CAACD,IAAK,qBAAsBC,UAAW,UACvC,CAACD,IAAK,gBAAiBC,UAAW,UAClC,CAACD,IAAK,SAAUC,UAAW,YAC5BJ,MAAK,SAASK,gBACL5B,aAAa6B,QACjBD,QAAQ,GACRA,QAAQ,GACRA,QAAQ,GACRA,QAAQ,IACR,kBACIM,eACA/B,kBAAmB,EAEnBgC,WAAWE,aAAcvB,YAClB,KAEX,WAEIqB,WAAWE,aAAc7B,sBAGlCe,MAAKP,QAEJmB,WAAWpB,sBAA4C,IAArBO,KAAKE,cAAsBR,UAE9DgB,MAAMhC,aAAaiC,YAEtBE,WAAWE,aAAc7B,iBAEtB,QASX+B,MAAQ,WACJnC,mBAAqB,EACrB+B,WAAWD,aAAc9B,oBAGzB+B,WAAWE,aAAcvB,kBAS3B0B,eAAiB,cACfC,OAAOC,SAAWD,cAEX,MAIPE,cAEAA,UAAYF,OAAOC,OAAOZ,SAASc,KACrC,MAAOC,UAEE,SAGJF,UAAUG,WAAWC,EAAEC,IAAIC,gBAmD/B,CACHC,UAtBY,SAASC,KAAMC,QAASC,SAEhCnD,QACAuC,OAAOa,QAAQC,KAAK,kFAGxBrD,SAAU,EAENsC,iBACAC,OAAOa,QAAQC,KAAK,2EAIxBd,OAAOa,QAAQE,IAAI,uCAEnBpD,mBAA4B,IAAP+C,KACrB7C,iBAAmB+C,QACnBhD,eAA2B,IAAV+C,QACjBb,WAKAkB,KA/CO,WAEHvD,UAGJA,SAAU,EAENsC,iBACAC,OAAOa,QAAQE,IAAI,gEAIvBf,OAAOa,QAAQE,IAAI,4CAEnBjB"} \ No newline at end of file +{"version":3,"file":"network.min.js","sources":["../src/network.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Poll the server to keep the session alive.\n *\n * @module core/network\n * @copyright 2019 Damyon Wiese\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],\n function($, Ajax, Config, Notification, Str) {\n\n var started = false;\n var warningDisplayed = false;\n var keepAliveFrequency = 0;\n var requestTimeout = 0;\n var keepAliveMessage = false;\n var sessionTimeout = false;\n // 1/10 of session timeout, max of 10 minutes.\n var checkFrequency = Math.min((Config.sessiontimeout / 10), 600) * 1000;\n // Check if sessiontimeoutwarning is set or double the checkFrequency.\n var warningLimit = (Config.sessiontimeoutwarning > 0) ? (Config.sessiontimeoutwarning * 1000) : (checkFrequency * 2);\n // First wait is minimum of remaining time or half of the session timeout.\n var firstWait = (Config.sessiontimeoutwarning > 0) ?\n Math.min((Config.sessiontimeout - Config.sessiontimeoutwarning) * 1000, checkFrequency * 5) : checkFrequency * 5;\n /**\n * The session time has expired - we can't extend it now.\n * @param {Modal} modal\n */\n var timeoutSessionExpired = function(modal) {\n sessionTimeout = true;\n warningDisplayed = false;\n closeModal(modal);\n displaySessionExpired();\n };\n\n /**\n * Close modal - this relies on modal object passed from Notification.confirm.\n *\n * @param {Modal} modal\n */\n var closeModal = function(modal) {\n modal.destroy();\n };\n\n /**\n * The session time has expired - we can't extend it now.\n * @return {Promise}\n */\n var displaySessionExpired = function() {\n // Check again if its already extended before displaying session expired popup in case multiple tabs are open.\n var request = {\n methodname: 'core_session_time_remaining',\n args: { }\n };\n\n return Ajax.call([request], true, true, true)[0].then(function(args) {\n if (args.timeremaining * 1000 > warningLimit) {\n return false;\n } else {\n return Str.get_strings([\n {key: 'sessionexpired', component: 'error'},\n {key: 'sessionerroruser', component: 'error'},\n {key: 'loginagain', component: 'moodle'},\n {key: 'cancel', component: 'moodle'}\n ]).then(function(strings) {\n Notification.confirm(\n strings[0], // Title.\n strings[1], // Message.\n strings[2], // Login Again.\n strings[3], // Cancel.\n function() {\n location.reload();\n return true;\n }\n );\n return true;\n }).catch(Notification.exception);\n }\n });\n };\n\n /**\n * Ping the server to keep the session alive.\n *\n * @return {Promise}\n */\n var touchSession = function() {\n var request = {\n methodname: 'core_session_touch',\n args: { }\n };\n\n if (sessionTimeout) {\n // We timed out before we extended the session.\n return displaySessionExpired();\n } else {\n return Ajax.call([request], true, true, false, requestTimeout)[0].then(function() {\n if (keepAliveFrequency > 0) {\n setTimeout(touchSession, keepAliveFrequency);\n }\n return true;\n }).catch(function() {\n Notification.alert('', keepAliveMessage);\n });\n }\n };\n\n /**\n * Ask the server how much time is remaining in this session and\n * show confirm/cancel notifications if the session is about to run out.\n *\n * @return {Promise}\n */\n var checkSession = function() {\n var request = {\n methodname: 'core_session_time_remaining',\n args: { }\n };\n sessionTimeout = false;\n return Ajax.call([request], true, true, true)[0].then(function(args) {\n if (args.userid <= 0) {\n return false;\n }\n if (args.timeremaining <= 0) {\n return displaySessionExpired();\n } else if (args.timeremaining * 1000 <= warningLimit && !warningDisplayed) {\n warningDisplayed = true;\n Str.get_strings([\n {key: 'norecentactivity', component: 'moodle'},\n {key: 'sessiontimeoutsoon', component: 'moodle'},\n {key: 'extendsession', component: 'moodle'},\n {key: 'cancel', component: 'moodle'}\n ]).then(function(strings) {\n return Notification.confirm(\n strings[0], // Title.\n strings[1], // Message.\n strings[2], // Extend session.\n strings[3], // Cancel.\n function() {\n touchSession();\n warningDisplayed = false;\n // First wait is minimum of remaining time or half of the session timeout.\n setTimeout(checkSession, firstWait);\n return true;\n },\n function() {\n // User has cancelled notification.\n setTimeout(checkSession, checkFrequency);\n }\n );\n }).then(modal => {\n // If we don't extend the session before the timeout - warn.\n setTimeout(timeoutSessionExpired, args.timeremaining * 1000, modal);\n return;\n }).catch(Notification.exception);\n } else {\n setTimeout(checkSession, checkFrequency);\n }\n return true;\n });\n // We do not catch the fails from the above ajax call because they will fail when\n // we are not logged in - we don't need to take any action then.\n };\n\n /**\n * Start calling a function to check if the session is still alive.\n */\n var start = function() {\n if (keepAliveFrequency > 0) {\n setTimeout(touchSession, keepAliveFrequency);\n } else {\n // First wait is minimum of remaining time or half of the session timeout.\n setTimeout(checkSession, firstWait);\n }\n };\n\n /**\n * Are we in an iframe and the parent page is from the same Moodle site?\n *\n * @return {boolean} true if we are in an iframe in a page from this Moodle site.\n */\n const isMoodleIframe = function() {\n if (window.parent === window) {\n // Not in an iframe.\n return false;\n }\n\n // We are in an iframe. Is the parent from the same Moodle site?\n let parentUrl;\n try {\n parentUrl = window.parent.location.href;\n } catch (e) {\n // If we cannot access the URL of the parent page, it must be another site.\n return false;\n }\n\n return parentUrl.startsWith(M.cfg.wwwroot);\n };\n\n /**\n * Don't allow more than one of these polling loops in a single page.\n */\n var init = function() {\n // We only allow one concurrent instance of this checker.\n if (started) {\n return;\n }\n started = true;\n\n if (isMoodleIframe()) {\n window.console.log('Not starting Moodle session timeout warning in this iframe.');\n return;\n }\n\n window.console.log('Starting Moodle session timeout warning.');\n\n start();\n };\n\n /**\n * Start polling with more specific values for the frequency, timeout and message.\n *\n * @param {number} freq How ofter to poll the server.\n * @param {number} timeout The time to wait for each request to the server.\n * @param {string} identifier The string identifier for the message to show if session is going to time out.\n * @param {string} component The string component for the message to show if session is going to time out.\n */\n var keepalive = async function(freq, timeout, identifier, component) {\n // We only allow one concurrent instance of this checker.\n if (started) {\n window.console.warn('Ignoring session keep-alive. The core/network module was already initialised.');\n return;\n }\n started = true;\n\n if (isMoodleIframe()) {\n window.console.warn('Ignoring session keep-alive in this iframe inside another Moodle page.');\n return;\n }\n\n window.console.log('Starting Moodle session keep-alive.');\n\n keepAliveFrequency = freq * 1000;\n keepAliveMessage = await Str.get_string(identifier, component);\n requestTimeout = timeout * 1000;\n start();\n };\n\n return {\n keepalive: keepalive,\n init: init\n };\n});\n"],"names":["define","$","Ajax","Config","Notification","Str","started","warningDisplayed","keepAliveFrequency","requestTimeout","keepAliveMessage","sessionTimeout","checkFrequency","Math","min","sessiontimeout","warningLimit","sessiontimeoutwarning","firstWait","timeoutSessionExpired","modal","closeModal","displaySessionExpired","destroy","call","methodname","args","then","timeremaining","get_strings","key","component","strings","confirm","location","reload","catch","exception","touchSession","setTimeout","alert","checkSession","userid","start","isMoodleIframe","window","parent","parentUrl","href","e","startsWith","M","cfg","wwwroot","keepalive","async","freq","timeout","identifier","console","warn","log","get_string","init"],"mappings":";;;;;;;AAsBAA,sBAAO,CAAC,SAAU,YAAa,cAAe,oBAAqB,aAC3D,SAASC,EAAGC,KAAMC,OAAQC,aAAcC,SAExCC,SAAU,EACVC,kBAAmB,EACnBC,mBAAqB,EACrBC,eAAiB,EACjBC,kBAAmB,EACnBC,gBAAiB,EAEjBC,eAA+D,IAA9CC,KAAKC,IAAKX,OAAOY,eAAiB,GAAK,KAExDC,aAAgBb,OAAOc,sBAAwB,EAAqC,IAA/Bd,OAAOc,sBAAkD,EAAjBL,eAE7FM,UAAaf,OAAOc,sBAAwB,EAC5CJ,KAAKC,IAA6D,KAAxDX,OAAOY,eAAiBZ,OAAOc,uBAAgD,EAAjBL,gBAAuC,EAAjBA,eAK9FO,sBAAwB,SAASC,OACjCT,gBAAiB,EACjBJ,kBAAmB,EACnBc,WAAWD,OACXE,yBAQAD,WAAa,SAASD,OACtBA,MAAMG,WAOND,sBAAwB,kBAOjBpB,KAAKsB,KAAK,CALH,CACVC,WAAY,8BACZC,KAAM,MAGkB,GAAM,GAAM,GAAM,GAAGC,MAAK,SAASD,cAClC,IAArBA,KAAKE,cAAuBZ,eAGrBX,IAAIwB,YAAY,CACnB,CAACC,I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,aAAc9B,oBAGzB+B,WAAWE,aAAcvB,kBAS3B0B,eAAiB,cACfC,OAAOC,SAAWD,cAEX,MAIPE,cAEAA,UAAYF,OAAOC,OAAOZ,SAASc,KACrC,MAAOC,UAEE,SAGJF,UAAUG,WAAWC,EAAEC,IAAIC,gBAoD/B,CACHC,UAtBYC,eAAeC,KAAMC,QAASC,WAAY3B,WAElDzB,QACAuC,OAAOc,QAAQC,KAAK,kFAGxBtD,SAAU,EAENsC,iBACAC,OAAOc,QAAQC,KAAK,2EAIxBf,OAAOc,QAAQE,IAAI,uCAEnBrD,mBAA4B,IAAPgD,KACrB9C,uBAAyBL,IAAIyD,WAAWJ,WAAY3B,WACpDtB,eAA2B,IAAVgD,QACjBd,WAKAoB,KAhDO,WAEHzD,UAGJA,SAAU,EAENsC,iBACAC,OAAOc,QAAQE,IAAI,gEAIvBhB,OAAOc,QAAQE,IAAI,4CAEnBlB"} \ No newline at end of file diff --git a/lib/amd/src/network.js b/lib/amd/src/network.js index ebc61917753..c8a7f86289f 100644 --- a/lib/amd/src/network.js +++ b/lib/amd/src/network.js @@ -236,9 +236,10 @@ define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'], * * @param {number} freq How ofter to poll the server. * @param {number} timeout The time to wait for each request to the server. - * @param {string} message The message to display if the session is going to time out. + * @param {string} identifier The string identifier for the message to show if session is going to time out. + * @param {string} component The string component for the message to show if session is going to time out. */ - var keepalive = function(freq, timeout, message) { + var keepalive = async function(freq, timeout, identifier, component) { // We only allow one concurrent instance of this checker. if (started) { window.console.warn('Ignoring session keep-alive. The core/network module was already initialised.'); @@ -254,7 +255,7 @@ define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'], window.console.log('Starting Moodle session keep-alive.'); keepAliveFrequency = freq * 1000; - keepAliveMessage = message; + keepAliveMessage = await Str.get_string(identifier, component); requestTimeout = timeout * 1000; start(); }; diff --git a/lib/classes/session/manager.php b/lib/classes/session/manager.php index e60a29d4298..07c8aef0894 100644 --- a/lib/classes/session/manager.php +++ b/lib/classes/session/manager.php @@ -1140,7 +1140,8 @@ class manager { $PAGE->requires->js_call_amd('core/network', 'keepalive', array( $frequency, $timeout, - get_string($identifier, $component) + $identifier, + $component )); }