-
Notifications
You must be signed in to change notification settings - Fork 2
/
jstats.php
136 lines (117 loc) · 2.76 KB
/
jstats.php
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
<?php
/**
* @copyright Copyright (C) 2013 Don Gilbert. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
/**
* Class plgSystemJstats
*/
class plgSystemJstats extends JPlugin
{
/**
* @var JApplication
*
* @since 1.0
*/
protected $app;
/**
* Path to the cache file
*
* @var string
*
* @since 1.0
*/
protected $cacheFile;
/**
* @var JDatabaseDriver
*
* @since 1.0
*/
protected $db;
/**
* Flag for whether or not to perform the check.
*
* @var bool
*
* @since 1.0
*/
protected $doCheck = false;
/**
* Stats Plugin Constructor
*
* @param object $subject
* @param array $config
*
* @since 1.0
*/
public function __construct(&$subject, $config = array())
{
$this->app = JFactory::getApplication();
$this->db = JFactory::getDbo();
$this->cacheFile = JPATH_ROOT . '/cache/jstats.php';
parent::__construct($subject, $config);
}
public function onAfterInitialise()
{
if (is_readable($this->cacheFile))
{
$checkedTime = include $this->cacheFile;
if ($checkedTime < strtotime('-12 hours'))
{
$this->sendStats();
}
}
else
{
$this->sendStats();
}
}
protected function sendStats()
{
if (version_compare(JVERSION, '3.0', '<'))
{
JLoader::register('JHttp', dirname(__FILE__) . '/src/joomla/http/http.php');
JLoader::register('JHttpFactory', dirname(__FILE__) . '/src/joomla/http/factory.php');
JLoader::register('JHttpResponse', dirname(__FILE__) . '/src/joomla/http/response.php');
JLoader::register('JHttpTransport', dirname(__FILE__) . '/src/joomla/http/transport.php');
JLoader::register('JHttpTransportCurl', dirname(__FILE__) . '/src/joomla/http/transport/curl.php');
JLoader::register('JHttpTransportSocket', dirname(__FILE__) . '/src/joomla/http/transport/socket.php');
JLoader::register('JHttpTransportStream', dirname(__FILE__) . '/src/joomla/http/transport/stream.php');
}
$http = JHttpFactory::getHttp();
$data = array(
'unique_id' => $this->params->get('unique_id'),
'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')
);
$uri = new JUri('http://jstats.dongilbert.net/submit');
try
{
$status = $http->post($uri, $data);
if ($status->code === 200)
{
$this->writeCacheFile();
}
}
catch (UnexpectedValueException $e)
{
// There was an error sending stats. Should we do anything?g
}
}
protected function writeCacheFile()
{
if (is_readable($this->cacheFile))
{
unlink($this->cacheFile);
}
$now = time();
$php = <<<PHP
<?php defined('_JEXEC') or die;
return $now;
PHP;
file_put_contents($this->cacheFile, $php);
}
}