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