|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stats; |
|
4
|
|
|
|
|
5
|
|
|
use Joomla\Application\AbstractWebApplication; |
|
6
|
|
|
use Ramsey\Uuid\Uuid; |
|
7
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Analytics; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Web application for the stats server |
|
11
|
|
|
* |
|
12
|
|
|
* @since 1.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class WebApplication extends AbstractWebApplication |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Application analytics object. |
|
18
|
|
|
* |
|
19
|
|
|
* @var Analytics |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
private $analytics; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Response mime type. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
* @since 1.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
public $mimeType = 'application/json'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Application router. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Router |
|
36
|
|
|
* @since 1.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
private $router; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Method to run the application routines. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
* |
|
45
|
|
|
* @since 1.0 |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function doExecute() |
|
48
|
|
|
{ |
|
49
|
|
|
// On a GET request to the live domain, submit analytics data |
|
50
|
5 |
|
if ($this->input->getMethod() === 'GET' |
|
51
|
5 |
|
&& strpos($this->input->server->getString('HTTP_HOST', ''), 'developer.joomla.org') === 0 |
|
52
|
5 |
|
&& $this->analytics) |
|
53
|
5 |
|
{ |
|
54
|
|
|
$this->analytics->setAsyncRequest(true) |
|
55
|
|
|
->setProtocolVersion('1') |
|
56
|
|
|
->setTrackingId('UA-544070-16') |
|
57
|
|
|
->setClientId(Uuid::uuid4()->toString()) |
|
58
|
|
|
->setDocumentPath($this->get('uri.base.path')) |
|
59
|
|
|
->setIpOverride($this->input->server->getString('REMOTE_ADDR', '127.0.0.1')) |
|
60
|
|
|
->setUserAgentOverride($this->input->server->getString('HTTP_USER_AGENT', 'JoomlaStats/1.0')); |
|
61
|
|
|
|
|
62
|
|
|
// Don't allow sending Analytics data to cause a failure |
|
63
|
|
|
try |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->analytics->sendPageview(); |
|
66
|
|
|
} |
|
67
|
|
|
catch (\Exception $e) |
|
68
|
|
|
{ |
|
69
|
|
|
// Log the error for reference |
|
70
|
|
|
$this->getLogger()->error( |
|
71
|
|
|
'Error sending analytics data.', |
|
72
|
|
|
['exception' => $e] |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
try |
|
78
|
|
|
{ |
|
79
|
5 |
|
$this->router->getController($this->get('uri.route'))->execute(); |
|
80
|
|
|
} |
|
81
|
5 |
|
catch (\Exception $e) |
|
82
|
|
|
{ |
|
83
|
|
|
// Log the error for reference |
|
84
|
4 |
|
$this->getLogger()->error( |
|
85
|
4 |
|
sprintf('Uncaught Exception of type %s caught.', get_class($e)), |
|
86
|
4 |
|
['exception' => $e] |
|
87
|
4 |
|
); |
|
88
|
|
|
|
|
89
|
4 |
|
$this->setErrorHeader($e); |
|
90
|
|
|
|
|
91
|
|
|
$data = [ |
|
92
|
4 |
|
'error' => true, |
|
93
|
4 |
|
'message' => $e->getMessage(), |
|
94
|
4 |
|
]; |
|
95
|
|
|
|
|
96
|
4 |
|
$this->setBody(json_encode($data)); |
|
97
|
|
|
} |
|
98
|
5 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set the application's analytics object. |
|
102
|
|
|
* |
|
103
|
|
|
* @param Analytics $analytics Analytics object to set. |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
* |
|
107
|
|
|
* @since 1.0 |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setAnalytics(Analytics $analytics) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->analytics = $analytics; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set the HTTP Response Header for error conditions. |
|
118
|
|
|
* |
|
119
|
|
|
* @param \Exception $exception The Exception object to process. |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
* |
|
123
|
|
|
* @since 1.0 |
|
124
|
|
|
*/ |
|
125
|
4 |
|
private function setErrorHeader(\Exception $exception) |
|
126
|
|
|
{ |
|
127
|
4 |
|
switch ($exception->getCode()) |
|
128
|
|
|
{ |
|
129
|
4 |
|
case 401: |
|
130
|
1 |
|
$this->setHeader('HTTP/1.1 401 Unauthorized', 401, true); |
|
131
|
|
|
|
|
132
|
1 |
|
break; |
|
133
|
|
|
|
|
134
|
3 |
|
case 403: |
|
135
|
1 |
|
$this->setHeader('HTTP/1.1 403 Forbidden', 403, true); |
|
136
|
|
|
|
|
137
|
1 |
|
break; |
|
138
|
|
|
|
|
139
|
2 |
|
case 404: |
|
140
|
1 |
|
$this->setHeader('HTTP/1.1 404 Not Found', 404, true); |
|
141
|
|
|
|
|
142
|
2 |
|
break; |
|
143
|
|
|
|
|
144
|
1 |
|
case 500: |
|
145
|
1 |
|
default: |
|
146
|
1 |
|
$this->setHeader('HTTP/1.1 500 Internal Server Error', 500, true); |
|
147
|
|
|
|
|
148
|
1 |
|
break; |
|
149
|
4 |
|
} |
|
150
|
4 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Set the application's router. |
|
154
|
|
|
* |
|
155
|
|
|
* @param Router $router Router object to set. |
|
156
|
|
|
* |
|
157
|
|
|
* @return $this |
|
158
|
|
|
* |
|
159
|
|
|
* @since 1.0 |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function setRouter(Router $router) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->router = $router; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|