stats.php
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php
/**
* @package Joomla.Plugin
* @subpackage System.stats
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
// Uncomment the following line to enable debug mode for testing purposes. Note: statistics will be sent on every page load
// define('PLG_SYSTEM_STATS_DEBUG', 1);
/**
* Statistics system plugin. This sends anonymous data back to the Joomla! Project about the
* PHP, SQL, Joomla and OS versions
*
* @since 3.5
*/
class PlgSystemStats extends JPlugin
{
/**
* Indicates sending statistics is always allowed.
*
* @var integer
* @since 3.5
*/
const MODE_ALLOW_ALWAYS = 1;
/**
* Indicates sending statistics is only allowed one time.
*
* @var integer
* @since 3.5
*/
const MODE_ALLOW_ONCE = 2;
/**
* Indicates sending statistics is never allowed.
*
* @var integer
* @since 3.5
*/
const MODE_ALLOW_NEVER = 3;
/**
* Application object
*
* @var JApplicationCms
* @since 3.5
*/
protected $app;
/**
* Database object
*
* @var JDatabaseDriver
* @since 3.5
*/
protected $db;
/**
* URL to send the statistics.
*
* @var string
* @since 3.5
*/
protected $serverUrl = 'https://developer.joomla.org/stats/submit';
/**
* Unique identifier for this site
*
* @var string
* @since 3.5
*/
protected $uniqueId;
/**
* Listener for the `onAfterInitialise` event
*
* @return void
*
* @since 3.5
*/
public function onAfterInitialise()
{
if (!$this->app->isClient('administrator') || !$this->isAllowedUser())
{
return;
}
if (!$this->isDebugEnabled() && !$this->isUpdateRequired())
{
return;
}
if (JUri::getInstance()->getVar('tmpl') === 'component')
{
return;
}
// Load plugin language files only when needed (ex: they are not needed in site client).
$this->loadLanguage();
JHtml::_('jquery.framework');
JHtml::_('script', 'plg_system_stats/stats.js', array('version' => 'auto', 'relative' => true));
}
/**
* User selected to always send data
*
* @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 onAjaxSendAlways()
{
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
{
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
}
$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 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 onAjaxSendNever()
{
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
{
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
}
$this->params->set('mode', static::MODE_ALLOW_NEVER);
if (!$this->saveParams())
{
throw new RuntimeException('Unable to save plugin settings', 500);
}
echo json_encode(array('sent' => 0));
}
/**
* 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 onAjaxSendOnce()
{
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
{
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
}
$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));
}
/**
* 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 onAjaxSendStats()
{
if (!$this->isAllowedUser() || !$this->isAjaxRequest())
{
throw new Exception(JText::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
}
// 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));
}
/**
* Get the data through events
*
* @param string $context Context where this will be called from
*
* @return array
*
* @since 3.5
*/
public function onGetStatsData($context)
{
return $this->getStatsData();
}
/**
* Debug a layout of this plugin
*
* @param string $layoutId Layout identifier
* @param array $data Optional data for the layout
*
* @return string
*
* @since 3.5
*/
public function debug($layoutId, $data = array())
{
$data = array_merge($this->getLayoutData(), $data);
return $this->getRenderer($layoutId)->debug($data);
}
/**
* Get the data for the layout
*
* @return array
*
* @since 3.5
*/
protected function getLayoutData()
{
return array(
'plugin' => $this,
'pluginParams' => $this->params,
'statsData' => $this->getStatsData()
);
}
/**
* Get the layout paths
*
* @return array
*
* @since 3.5
*/
protected function getLayoutPaths()
{
$template = JFactory::getApplication()->getTemplate();
return array(
JPATH_ADMINISTRATOR . '/templates/' . $template . '/html/layouts/plugins/' . $this->_type . '/' . $this->_name,
__DIR__ . '/layouts',
);
}
/**
* Get the plugin renderer
*
* @param string $layoutId Layout identifier
*
* @return JLayout
*
* @since 3.5
*/
protected function getRenderer($layoutId = 'default')
{
$renderer = new JLayoutFile($layoutId);
$renderer->setIncludePaths($this->getLayoutPaths());
return $renderer;
}
/**
* Get the data that will be sent to the stats server.
*
* @return array
*
* @since 3.5
*/
private function getStatsData()
{
$data = array(
'unique_id' => $this->getUniqueId(),
'php_version' => PHP_VERSION,
'db_type' => $this->db->name,
'db_version' => $this->db->getVersion(),
'cms_version' => JVERSION,
'server_os' => php_uname('s') . ' ' . php_uname('r')
);
// Check if we have a MariaDB version string and extract the proper version from it
if (preg_match('/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i', $data['db_version'], $versionParts))
{
$data['db_version'] = $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
}
return $data;
}
/**
* Get the unique id. Generates one if none is set.
*
* @return integer
*
* @since 3.5
*/
private function getUniqueId()
{
if (null === $this->uniqueId)
{
$this->uniqueId = $this->params->get('unique_id', hash('sha1', JUserHelper::genRandomPassword(28) . time()));
}
return $this->uniqueId;
}
/**
* Check if current user is allowed to send the data
*
* @return boolean
*
* @since 3.5
*/
private function isAllowedUser()
{
return JFactory::getUser()->authorise('core.admin');
}
/**
* Check if the debug is enabled
*
* @return boolean
*
* @since 3.5
*/
private function isDebugEnabled()
{
return defined('PLG_SYSTEM_STATS_DEBUG');
}
/**
* Check if last_run + interval > now
*
* @return boolean
*
* @since 3.5
*/
private function isUpdateRequired()
{
$last = (int) $this->params->get('lastrun', 0);
$interval = (int) $this->params->get('interval', 12);
$mode = (int) $this->params->get('mode', 0);
if ($mode === static::MODE_ALLOW_NEVER)
{
return false;
}
// Never updated or debug enabled
if (!$last || $this->isDebugEnabled())
{
return true;
}
return (abs(time() - $last) > $interval * 3600);
}
/**
* Check valid AJAX request
*
* @return boolean
*
* @since 3.5
*/
private function isAjaxRequest()
{
return strtolower($this->app->input->server->get('HTTP_X_REQUESTED_WITH', '')) === 'xmlhttprequest';
}
/**
* Render a layout of this plugin
*
* @param string $layoutId Layout identifier
* @param array $data Optional data for the layout
*
* @return string
*
* @since 3.5
*/
public function render($layoutId, $data = array())
{
$data = array_merge($this->getLayoutData(), $data);
return $this->getRenderer($layoutId)->render($data);
}
/**
* Save the plugin parameters
*
* @return boolean
*
* @since 3.5
*/
private function saveParams()
{
// Update params
$this->params->set('lastrun', time());
$this->params->set('unique_id', $this->getUniqueId());
$interval = (int) $this->params->get('interval', 12);
$this->params->set('interval', $interval ?: 12);
$query = $this->db->getQuery(true)
->update($this->db->quoteName('#__extensions'))
->set($this->db->quoteName('params') . ' = ' . $this->db->quote($this->params->toString('JSON')))
->where($this->db->quoteName('type') . ' = ' . $this->db->quote('plugin'))
->where($this->db->quoteName('folder') . ' = ' . $this->db->quote('system'))
->where($this->db->quoteName('element') . ' = ' . $this->db->quote('stats'));
try
{
// Lock the tables to prevent multiple plugin executions causing a race condition
$this->db->lockTable('#__extensions');
}
catch (Exception $e)
{
// If we can't lock the tables it's too risky to continue execution
return false;
}
try
{
// Update the plugin parameters
$result = $this->db->setQuery($query)->execute();
$this->clearCacheGroups(array('com_plugins'), array(0, 1));
}
catch (Exception $exc)
{
// If we failed to execute
$this->db->unlockTables();
$result = false;
}
try
{
// Unlock the tables after writing
$this->db->unlockTables();
}
catch (Exception $e)
{
// If we can't lock the tables assume we have somehow failed
$result = false;
}
return $result;
}
/**
* Send the stats to the stats server
*
* @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->serverUrl, $this->getStatsData(), null, 2);
}
catch (UnexpectedValueException $e)
{
// There was an error sending stats. Should we do anything?
throw new RuntimeException('Could not send site statistics to remote server: ' . $e->getMessage(), 500);
}
catch (RuntimeException $e)
{
// There was an error connecting to the server or in the post request
throw new RuntimeException('Could not connect to statistics server: ' . $e->getMessage(), 500);
}
catch (Exception $e)
{
// An unexpected error in processing; don't let this failure kill the site
throw new RuntimeException('Unexpected error connecting to statistics server: ' . $e->getMessage(), 500);
}
if ($response->code !== 200)
{
$data = json_decode($response->body);
throw new RuntimeException('Could not send site statistics to remote server: ' . $data->message, $response->code);
}
return true;
}
/**
* Clears cache groups. We use it to clear the plugins cache after we update the last run timestamp.
*
* @param array $clearGroups The cache groups to clean
* @param array $cacheClients The cache clients (site, admin) to clean
*
* @return void
*
* @since 3.5
*/
private function clearCacheGroups(array $clearGroups, array $cacheClients = array(0, 1))
{
foreach ($clearGroups as $group)
{
foreach ($cacheClients as $client_id)
{
try
{
$options = array(
'defaultgroup' => $group,
'cachebase' => $client_id ? JPATH_ADMINISTRATOR . '/cache' : $this->app->get('cache_path', JPATH_SITE . '/cache')
);
$cache = JCache::getInstance('callback', $options);
$cache->clean();
}
catch (Exception $e)
{
// Ignore it
}
}
}
}
}