Skip to content

Commit 67fe07a

Browse files
committed
[imp] always send system stats using AJAX
1 parent 7fe7473 commit 67fe07a

File tree

3 files changed

+79
-50
lines changed

3 files changed

+79
-50
lines changed

administrator/language/en-GB/en-GB.plg_system_stats.ini

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PLG_SYSTEM_STATS="System - Joomla! Statistics"
77
PLG_SYSTEM_STATS_BTN_NEVER_SEND="Never"
88
PLG_SYSTEM_STATS_BTN_SEND_ALWAYS="Always"
99
PLG_SYSTEM_STATS_BTN_SEND_NOW="Once"
10-
PLG_SYSTEM_STATS_DEBUG_DESC="Enable debug (stats sent every single time)"
10+
PLG_SYSTEM_STATS_DEBUG_DESC="Enable debug for testing purposes. Stats sent every page load."
1111
PLG_SYSTEM_STATS_DEBUG_LABEL="Debug"
1212
PLG_SYSTEM_STATS_INTERVAL_DESC="Stats will be sent each X hours. Default is 12"
1313
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
2323
PLG_SYSTEM_STATS_RESET_UNIQUE_ID="Reset Unique Id"
2424
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."
2525
PLG_SYSTEM_STATS_UNIQUE_ID_LABEL="Unique ID"
26-
PLG_SYSTEM_STATS_URL_DESC="The official Joomla server url"
27-
PLG_SYSTEM_STATS_URL_LABEL="Url"
2826
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."

media/plg_system_stats/js/stats.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'option' : 'com_ajax',
55
'group' : 'system',
66
'plugin' : 'renderStatsMessage',
7-
'format' : 'json'
7+
'format' : 'raw'
88
},
99
messageContainer = $('#system-message-container');
1010

@@ -32,9 +32,7 @@
3232
detailsContainer.remove();
3333
ajaxData.plugin = 'sendAlways';
3434

35-
$.getJSON('index.php', ajaxData, function(response){
36-
37-
});
35+
$.getJSON('index.php', ajaxData, function(response){});
3836
e.preventDefault();
3937
});
4038

@@ -47,9 +45,7 @@
4745

4846
ajaxData.plugin = 'sendOnce';
4947

50-
$.getJSON('index.php', ajaxData, function(response){
51-
52-
});
48+
$.getJSON('index.php', ajaxData, function(response){});
5349
e.preventDefault();
5450
});
5551

@@ -62,20 +58,21 @@
6258

6359
ajaxData.plugin = 'sendNever';
6460

65-
$.getJSON('index.php', ajaxData, function(response){
66-
});
61+
$.getJSON('index.php', ajaxData, function(response){});
6762
e.preventDefault();
6863
});
6964
}
7065

71-
ajaxData.plugin = 'renderStatsMessage';
66+
ajaxData.plugin = 'sendStats';
7267

7368
$.getJSON('index.php', ajaxData, function(response){
74-
messageContainer
75-
.append(response.data[0].html)
76-
.find('.js-pstats-alert').show(200);
69+
if (response && response.html) {
70+
messageContainer
71+
.append(response.html)
72+
.find('.js-pstats-alert').show(200);
7773

78-
initStatsEvents();
74+
initStatsEvents();
75+
}
7976
});
8077
});
8178
})(jQuery);

plugins/system/stats/stats.php

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class PlgSystemStats extends JPlugin
5959
*/
6060
protected $db;
6161

62+
/**
63+
* Url to send the statistics.
64+
*
65+
* @var string
66+
*/
67+
protected $serverUrl = 'https://developer.joomla.org/stats/submit';
68+
6269
/**
6370
* Unique identifier for this site
6471
*
@@ -76,25 +83,13 @@ class PlgSystemStats extends JPlugin
7683
*/
7784
public function onAfterInitialise()
7885
{
79-
if (!$this->app->isAdmin() || !$this->isAllowedUser() || !$this->isUpdateRequired())
86+
if (!$this->app->isAdmin() || !$this->isAllowedUser())
8087
{
8188
return;
8289
}
8390

84-
$mode = (int) $this->params->get('mode');
85-
86-
// Plugin parameters are saved and send always enabled
87-
if ($mode === static::MODE_ALLOW_ALWAYS)
91+
if (!$this->isDebugEnabled() && !$this->isUpdateRequired())
8892
{
89-
try
90-
{
91-
$this->sendStats();
92-
}
93-
catch (Exception $e)
94-
{
95-
JLog::add($e->getMessage(), JLog::WARNING, 'stats');
96-
}
97-
9893
return;
9994
}
10095

@@ -103,92 +98,129 @@ public function onAfterInitialise()
10398
}
10499

105100
/**
106-
* Get the user message rendered
101+
* User selected to always send data
107102
*
108-
* @return array
103+
* @return void
109104
*
110105
* @since 3.5
106+
*
107+
* @throws Exception If user is not allowed.
108+
* @throws RuntimeException If there is an error saving the params or sending the data.
111109
*/
112-
public function onAjaxRenderStatsMessage()
110+
public function onAjaxSendAlways()
113111
{
114112
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
115113
{
116114
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
117115
}
118116

119-
return array(
120-
'html' => $this->getRenderer('message')->render($this->getLayoutData())
121-
);
117+
$this->params->set('mode', static::MODE_ALLOW_ALWAYS);
118+
119+
if (!$this->saveParams())
120+
{
121+
throw new RuntimeException('Unable to save plugin settings', 500);
122+
}
123+
124+
$this->sendStats();
125+
126+
echo json_encode(array('sent' => 1));
122127
}
123128

124129
/**
125-
* User selected to always send data
130+
* User selected to never send data.
126131
*
127132
* @return void
128133
*
129134
* @since 3.5
135+
*
136+
* @throws Exception If user is not allowed.
137+
* @throws RuntimeException If there is an error saving the params.
130138
*/
131-
public function onAjaxSendAlways()
139+
public function onAjaxSendNever()
132140
{
133141
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
134142
{
135143
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
136144
}
137145

138-
$this->params->set('mode', static::MODE_ALLOW_ALWAYS);
146+
$this->params->set('mode', static::MODE_ALLOW_NEVER);
139147

140148
if (!$this->saveParams())
141149
{
142150
throw new RuntimeException('Unable to save plugin settings', 500);
143151
}
144152

145-
$this->sendStats();
153+
echo json_encode(array('sent' => 0));
146154
}
147155

148156
/**
149-
* User selected to never send data.
157+
* User selected to send data once.
150158
*
151159
* @return void
152160
*
153161
* @since 3.5
162+
*
163+
* @throws Exception If user is not allowed.
164+
* @throws RuntimeException If there is an error saving the params or sending the data.
154165
*/
155-
public function onAjaxSendNever()
166+
public function onAjaxSendOnce()
156167
{
157168
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
158169
{
159170
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
160171
}
161172

162-
$this->params->set('mode', static::MODE_ALLOW_NEVER);
173+
$this->params->set('mode', static::MODE_ALLOW_ONCE);
163174

164175
if (!$this->saveParams())
165176
{
166177
throw new RuntimeException('Unable to save plugin settings', 500);
167178
}
179+
180+
$this->sendStats();
181+
182+
echo json_encode(array('sent' => 1));
168183
}
169184

170185
/**
171-
* User selected to send data once.
186+
* Send the stats to the server.
187+
* On first load | on demand mode it will show a message asking users to select mode.
172188
*
173189
* @return void
174190
*
175191
* @since 3.5
192+
*
193+
* @throws Exception If user is not allowed.
194+
* @throws RuntimeException If there is an error saving the params or sending the data.
176195
*/
177-
public function onAjaxSendOnce()
196+
public function onAjaxSendStats()
178197
{
179198
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
180199
{
181200
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
182201
}
183202

184-
$this->params->set('mode', static::MODE_ALLOW_ONCE);
203+
// User has not selected the mode. Show message.
204+
if ((int) $this->params->get('mode') !== static::MODE_ALLOW_ALWAYS)
205+
{
206+
$data = array(
207+
'sent' => 0,
208+
'html' => $this->getRenderer('message')->render($this->getLayoutData())
209+
);
210+
211+
echo json_encode($data);
212+
213+
return;
214+
}
185215

186216
if (!$this->saveParams())
187217
{
188218
throw new RuntimeException('Unable to save plugin settings', 500);
189219
}
190220

191221
$this->sendStats();
222+
223+
echo json_encode(array('sent' => 1));
192224
}
193225

194226
/**
@@ -321,7 +353,7 @@ private function isUpdateRequired()
321353
}
322354

323355
// Never updated or debug enabled
324-
if (!$last || !$interval || $this->isDebugEnabled())
356+
if (!$last || $this->isDebugEnabled())
325357
{
326358
return true;
327359
}
@@ -338,7 +370,7 @@ private function isUpdateRequired()
338370
*/
339371
private function isAjaxRequest()
340372
{
341-
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
373+
return strtolower($this->app->input->server->get('HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest';
342374
}
343375

344376
/**
@@ -408,13 +440,15 @@ private function saveParams()
408440
* @return boolean
409441
*
410442
* @since 3.5
443+
*
444+
* @throws RuntimeException If there is an error sending the data.
411445
*/
412446
private function sendStats()
413447
{
414448
try
415449
{
416450
// Don't let the request take longer than 2 seconds to avoid page timeout issues
417-
$response = JHttpFactory::getHttp()->post($this->params->get('url', 'https://developer.joomla.org/stats/submit'), $this->getStatsData(), null, 2);
451+
JHttpFactory::getHttp()->post($this->serverUrl, $this->getStatsData(), null, 2);
418452
}
419453
catch (UnexpectedValueException $e)
420454
{

0 commit comments

Comments
 (0)