From 67fe07a4f0b663b33f23b8fe2e9102ed5ffacf8d Mon Sep 17 00:00:00 2001 From: Roberto Segura - phproberto Date: Tue, 10 Nov 2015 10:43:32 +0100 Subject: [PATCH] [imp] always send system stats using AJAX --- .../language/en-GB/en-GB.plg_system_stats.ini | 4 +- media/plg_system_stats/js/stats.js | 25 ++--- plugins/system/stats/stats.php | 100 ++++++++++++------ 3 files changed, 79 insertions(+), 50 deletions(-) diff --git a/administrator/language/en-GB/en-GB.plg_system_stats.ini b/administrator/language/en-GB/en-GB.plg_system_stats.ini index f140e881df99d..c9995e85d74c8 100644 --- a/administrator/language/en-GB/en-GB.plg_system_stats.ini +++ b/administrator/language/en-GB/en-GB.plg_system_stats.ini @@ -7,7 +7,7 @@ PLG_SYSTEM_STATS="System - Joomla! Statistics" PLG_SYSTEM_STATS_BTN_NEVER_SEND="Never" PLG_SYSTEM_STATS_BTN_SEND_ALWAYS="Always" PLG_SYSTEM_STATS_BTN_SEND_NOW="Once" -PLG_SYSTEM_STATS_DEBUG_DESC="Enable debug (stats sent every single time)" +PLG_SYSTEM_STATS_DEBUG_DESC="Enable debug for testing purposes. Stats sent every page load." PLG_SYSTEM_STATS_DEBUG_LABEL="Debug" PLG_SYSTEM_STATS_INTERVAL_DESC="Stats will be sent each X hours. Default is 12" PLG_SYSTEM_STATS_INTERVAL_LABEL="Interval (hours)" @@ -23,6 +23,4 @@ PLG_SYSTEM_STATS_MSG_WHAT_DATA_WILL_BE_SENT="Click here to see which information PLG_SYSTEM_STATS_RESET_UNIQUE_ID="Reset Unique Id" PLG_SYSTEM_STATS_UNIQUE_ID_DESC="An identifier that allows the Joomla! project to count unique installs of the plugin. This is sent with the statistics back to the server." PLG_SYSTEM_STATS_UNIQUE_ID_LABEL="Unique ID" -PLG_SYSTEM_STATS_URL_DESC="The official Joomla server url" -PLG_SYSTEM_STATS_URL_LABEL="Url" PLG_SYSTEM_STATS_XML_DESCRIPTION="System Plugin that sends environment statistics to a server controlled by the Joomla! project for statistical analysis. Statistics sent include PHP version, CMS version, Database type, Database version and Server type." diff --git a/media/plg_system_stats/js/stats.js b/media/plg_system_stats/js/stats.js index a3e8ec634a7c7..6604bfe84bcdc 100644 --- a/media/plg_system_stats/js/stats.js +++ b/media/plg_system_stats/js/stats.js @@ -4,7 +4,7 @@ 'option' : 'com_ajax', 'group' : 'system', 'plugin' : 'renderStatsMessage', - 'format' : 'json' + 'format' : 'raw' }, messageContainer = $('#system-message-container'); @@ -32,9 +32,7 @@ detailsContainer.remove(); ajaxData.plugin = 'sendAlways'; - $.getJSON('index.php', ajaxData, function(response){ - - }); + $.getJSON('index.php', ajaxData, function(response){}); e.preventDefault(); }); @@ -47,9 +45,7 @@ ajaxData.plugin = 'sendOnce'; - $.getJSON('index.php', ajaxData, function(response){ - - }); + $.getJSON('index.php', ajaxData, function(response){}); e.preventDefault(); }); @@ -62,20 +58,21 @@ ajaxData.plugin = 'sendNever'; - $.getJSON('index.php', ajaxData, function(response){ - }); + $.getJSON('index.php', ajaxData, function(response){}); e.preventDefault(); }); } - ajaxData.plugin = 'renderStatsMessage'; + ajaxData.plugin = 'sendStats'; $.getJSON('index.php', ajaxData, function(response){ - messageContainer - .append(response.data[0].html) - .find('.js-pstats-alert').show(200); + if (response && response.html) { + messageContainer + .append(response.html) + .find('.js-pstats-alert').show(200); - initStatsEvents(); + initStatsEvents(); + } }); }); })(jQuery); diff --git a/plugins/system/stats/stats.php b/plugins/system/stats/stats.php index b04976f3de40f..718f868fc527b 100644 --- a/plugins/system/stats/stats.php +++ b/plugins/system/stats/stats.php @@ -59,6 +59,13 @@ class PlgSystemStats extends JPlugin */ protected $db; + /** + * Url to send the statistics. + * + * @var string + */ + protected $serverUrl = 'https://developer.joomla.org/stats/submit'; + /** * Unique identifier for this site * @@ -76,25 +83,13 @@ class PlgSystemStats extends JPlugin */ public function onAfterInitialise() { - if (!$this->app->isAdmin() || !$this->isAllowedUser() || !$this->isUpdateRequired()) + if (!$this->app->isAdmin() || !$this->isAllowedUser()) { return; } - $mode = (int) $this->params->get('mode'); - - // Plugin parameters are saved and send always enabled - if ($mode === static::MODE_ALLOW_ALWAYS) + if (!$this->isDebugEnabled() && !$this->isUpdateRequired()) { - try - { - $this->sendStats(); - } - catch (Exception $e) - { - JLog::add($e->getMessage(), JLog::WARNING, 'stats'); - } - return; } @@ -103,85 +98,120 @@ public function onAfterInitialise() } /** - * Get the user message rendered + * User selected to always send data * - * @return array + * @return void * * @since 3.5 + * + * @throws Exception If user is not allowed. + * @throws RuntimeException If there is an error saving the params or sending the data. */ - public function onAjaxRenderStatsMessage() + public function onAjaxSendAlways() { if (!$this->isAllowedUser() || !$this->isAjaxRequest()) { throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403); } - return array( - 'html' => $this->getRenderer('message')->render($this->getLayoutData()) - ); + $this->params->set('mode', static::MODE_ALLOW_ALWAYS); + + if (!$this->saveParams()) + { + throw new RuntimeException('Unable to save plugin settings', 500); + } + + $this->sendStats(); + + echo json_encode(array('sent' => 1)); } /** - * User selected to always send data + * User selected to never send data. * * @return void * * @since 3.5 + * + * @throws Exception If user is not allowed. + * @throws RuntimeException If there is an error saving the params. */ - public function onAjaxSendAlways() + public function onAjaxSendNever() { if (!$this->isAllowedUser() || !$this->isAjaxRequest()) { throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403); } - $this->params->set('mode', static::MODE_ALLOW_ALWAYS); + $this->params->set('mode', static::MODE_ALLOW_NEVER); if (!$this->saveParams()) { throw new RuntimeException('Unable to save plugin settings', 500); } - $this->sendStats(); + echo json_encode(array('sent' => 0)); } /** - * User selected to never send data. + * User selected to send data once. * * @return void * * @since 3.5 + * + * @throws Exception If user is not allowed. + * @throws RuntimeException If there is an error saving the params or sending the data. */ - public function onAjaxSendNever() + public function onAjaxSendOnce() { if (!$this->isAllowedUser() || !$this->isAjaxRequest()) { throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403); } - $this->params->set('mode', static::MODE_ALLOW_NEVER); + $this->params->set('mode', static::MODE_ALLOW_ONCE); if (!$this->saveParams()) { throw new RuntimeException('Unable to save plugin settings', 500); } + + $this->sendStats(); + + echo json_encode(array('sent' => 1)); } /** - * User selected to send data once. + * Send the stats to the server. + * On first load | on demand mode it will show a message asking users to select mode. * * @return void * * @since 3.5 + * + * @throws Exception If user is not allowed. + * @throws RuntimeException If there is an error saving the params or sending the data. */ - public function onAjaxSendOnce() + public function onAjaxSendStats() { if (!$this->isAllowedUser() || !$this->isAjaxRequest()) { throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403); } - $this->params->set('mode', static::MODE_ALLOW_ONCE); + // User has not selected the mode. Show message. + if ((int) $this->params->get('mode') !== static::MODE_ALLOW_ALWAYS) + { + $data = array( + 'sent' => 0, + 'html' => $this->getRenderer('message')->render($this->getLayoutData()) + ); + + echo json_encode($data); + + return; + } if (!$this->saveParams()) { @@ -189,6 +219,8 @@ public function onAjaxSendOnce() } $this->sendStats(); + + echo json_encode(array('sent' => 1)); } /** @@ -321,7 +353,7 @@ private function isUpdateRequired() } // Never updated or debug enabled - if (!$last || !$interval || $this->isDebugEnabled()) + if (!$last || $this->isDebugEnabled()) { return true; } @@ -338,7 +370,7 @@ private function isUpdateRequired() */ private function isAjaxRequest() { - return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); + return strtolower($this->app->input->server->get('HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest'; } /** @@ -408,13 +440,15 @@ private function saveParams() * @return boolean * * @since 3.5 + * + * @throws RuntimeException If there is an error sending the data. */ private function sendStats() { try { // Don't let the request take longer than 2 seconds to avoid page timeout issues - $response = JHttpFactory::getHttp()->post($this->params->get('url', 'https://developer.joomla.org/stats/submit'), $this->getStatsData(), null, 2); + JHttpFactory::getHttp()->post($this->serverUrl, $this->getStatsData(), null, 2); } catch (UnexpectedValueException $e) {