Skip to content

Commit

Permalink
[imp] always send system stats using AJAX
Browse files Browse the repository at this point in the history
  • Loading branch information
phproberto committed Nov 10, 2015
1 parent 7fe7473 commit 67fe07a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 50 deletions.
4 changes: 1 addition & 3 deletions administrator/language/en-GB/en-GB.plg_system_stats.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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."
25 changes: 11 additions & 14 deletions media/plg_system_stats/js/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'option' : 'com_ajax',
'group' : 'system',
'plugin' : 'renderStatsMessage',
'format' : 'json'
'format' : 'raw'
},
messageContainer = $('#system-message-container');

Expand Down Expand Up @@ -32,9 +32,7 @@
detailsContainer.remove();
ajaxData.plugin = 'sendAlways';

$.getJSON('index.php', ajaxData, function(response){

});
$.getJSON('index.php', ajaxData, function(response){});
e.preventDefault();
});

Expand All @@ -47,9 +45,7 @@

ajaxData.plugin = 'sendOnce';

$.getJSON('index.php', ajaxData, function(response){

});
$.getJSON('index.php', ajaxData, function(response){});
e.preventDefault();
});

Expand All @@ -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);
100 changes: 67 additions & 33 deletions plugins/system/stats/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}

Expand All @@ -103,92 +98,129 @@ 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())
{
throw new RuntimeException('Unable to save plugin settings', 500);
}

$this->sendStats();

echo json_encode(array('sent' => 1));
}

/**
Expand Down Expand Up @@ -321,7 +353,7 @@ private function isUpdateRequired()
}

// Never updated or debug enabled
if (!$last || !$interval || $this->isDebugEnabled())
if (!$last || $this->isDebugEnabled())
{
return true;
}
Expand All @@ -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';
}

/**
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 67fe07a

Please sign in to comment.