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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\UserCountry;
use Exception;
use Piwik\Common;
use Piwik\DataTable\Renderer\Json;
use Piwik\Http;
use Piwik\IP;
use Piwik\Piwik;
use Piwik\Plugins\UserCountry\LocationProvider\GeoIp\ServerBased;
use Piwik\Plugins\UserCountry\LocationProvider\GeoIp;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
use Piwik\Plugins\UserCountry\LocationProvider\GeoIp\Pecl;
use Piwik\Plugins\UserCountry\Reports\GetCity;
use Piwik\Plugins\UserCountry\Reports\GetContinent;
use Piwik\Plugins\UserCountry\Reports\GetCountry;
use Piwik\Plugins\UserCountry\Reports\GetRegion;
use Piwik\View;
/**
*
*/
class Controller extends \Piwik\Plugin\ControllerAdmin
{
public function index()
{
$view = new View('@UserCountry/index');
$view->urlSparklineCountries = $this->getUrlSparkline('getLastDistinctCountriesGraph');
$view->numberDistinctCountries = $this->getNumberOfDistinctCountries(true);
$view->dataTableCountry = $this->renderReport(new GetCountry());
$view->dataTableContinent = $this->renderReport(new GetContinent());
$view->dataTableRegion = $this->renderReport(new GetRegion());
$view->dataTableCity = $this->renderReport(new GetCity());
return $view->render();
}
public function adminIndex()
{
$this->dieIfGeolocationAdminIsDisabled();
Piwik::checkUserHasSuperUserAccess();
$view = new View('@UserCountry/adminIndex');
$allProviderInfo = LocationProvider::getAllProviderInfo($newline = '<br/>', $includeExtra = true);
$view->locationProviders = $allProviderInfo;
$view->currentProviderId = LocationProvider::getCurrentProviderId();
$view->thisIP = IP::getIpFromHeader();
$geoIPDatabasesInstalled = GeoIp::isDatabaseInstalled();
$view->geoIPDatabasesInstalled = $geoIPDatabasesInstalled;
// check if there is a working provider (that isn't the default one)
$isThereWorkingProvider = false;
foreach ($allProviderInfo as $id => $provider) {
if ($id != DefaultProvider::ID
&& $provider['status'] == LocationProvider::INSTALLED
) {
$isThereWorkingProvider = true;
break;
}
}
$view->isThereWorkingProvider = $isThereWorkingProvider;
// if using either the Apache or PECL module, they are working and there are no databases
// in misc, then the databases are located outside of Piwik, so we cannot update them
$view->showGeoIPUpdateSection = true;
$currentProviderId = LocationProvider::getCurrentProviderId();
if (!$geoIPDatabasesInstalled
&& ($currentProviderId == ServerBased::ID
|| $currentProviderId == Pecl::ID)
&& $allProviderInfo[$currentProviderId]['status'] == LocationProvider::INSTALLED
) {
$view->showGeoIPUpdateSection = false;
}
$this->setUpdaterManageVars($view);
$this->setBasicVariablesView($view);
$this->setBasicVariablesAdminView($view);
return $view->render();
}
/**
* Starts or continues download of GeoLiteCity.dat.
*
* To avoid a server/PHP timeout & to show progress of the download to the user, we
* use the HTTP Range header to download one chunk of the file at a time. After each
* chunk, it is the browser's responsibility to call the method again to continue the download.
*
* Input:
* 'continue' query param - if set to 1, will assume we are currently downloading & use
* Range: HTTP header to get another chunk of the file.
*
* Output (in JSON):
* 'current_size' - Current size of the partially downloaded file on disk.
* 'expected_file_size' - The expected finished file size as returned by the HTTP server.
* 'next_screen' - When the download finishes, this is the next screen that should be shown.
* 'error' - When an error occurs, the message is returned in this property.
*/
public function downloadFreeGeoIPDB()
{
$this->dieIfGeolocationAdminIsDisabled();
Piwik::checkUserHasSuperUserAccess();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$this->checkTokenInUrl();
Json::sendHeaderJSON();
$outputPath = GeoIp::getPathForGeoIpDatabase('GeoIPCity.dat') . '.gz';
try {
$result = Http::downloadChunk(
$url = GeoIp::GEO_LITE_URL,
$outputPath,
$continue = Common::getRequestVar('continue', true, 'int')
);
// if the file is done
if ($result['current_size'] >= $result['expected_file_size']) {
GeoIPAutoUpdater::unzipDownloadedFile($outputPath, $unlink = true);
// setup the auto updater
GeoIPAutoUpdater::setUpdaterOptions(array(
'loc_db' => GeoIp::GEO_LITE_URL,
'period' => GeoIPAutoUpdater::SCHEDULE_PERIOD_MONTHLY,
));
// make sure to echo out the geoip updater management screen
$result['next_screen'] = $this->getGeoIpUpdaterManageScreen();
}
return Common::json_encode($result);
} catch (Exception $ex) {
return Common::json_encode(array('error' => $ex->getMessage()));
}
}
}
/**
* Renders and returns the HTML that manages the GeoIP auto-updater.
*
* @return string
*/
private function getGeoIpUpdaterManageScreen()
{
$view = new View('@UserCountry/getGeoIpUpdaterManageScreen');
$view->geoIPDatabasesInstalled = true;
$this->setUpdaterManageVars($view);
return $view->render();
}
/**
* Sets some variables needed by the _updaterManage.twig template.
*
* @param View $view
*/
private function setUpdaterManageVars($view)
{
$urls = GeoIPAutoUpdater::getConfiguredUrls();
$view->geoIPLocUrl = $urls['loc'];
$view->geoIPIspUrl = $urls['isp'];
$view->geoIPOrgUrl = $urls['org'];
$view->geoIPUpdatePeriod = GeoIPAutoUpdater::getSchedulePeriod();
$view->geoLiteUrl = GeoIp::GEO_LITE_URL;
$lastRunTime = GeoIPAutoUpdater::getLastRunTime();
if ($lastRunTime !== false) {
$view->lastTimeUpdaterRun = '<strong><em>' . $lastRunTime->toString() . '</em></strong>';
}
$view->nextRunTime = GeoIPAutoUpdater::getNextRunTime();
}
/**
* Sets the URLs used to download new versions of the installed GeoIP databases.
*
* Input (query params):
* 'loc_db' - URL for a GeoIP location database.
* 'isp_db' - URL for a GeoIP ISP database (optional).
* 'org_db' - URL for a GeoIP Org database (optional).
* 'period' - 'weekly' or 'monthly'. Determines how often update is run.
*
* Output (json):
* 'error' - if an error occurs its message is set as the resulting JSON object's
* 'error' property.
*/
public function updateGeoIPLinks()
{
$this->dieIfGeolocationAdminIsDisabled();
Piwik::checkUserHasSuperUserAccess();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
Json::sendHeaderJSON();
try {
$this->checkTokenInUrl();
GeoIPAutoUpdater::setUpdaterOptionsFromUrl();
// if there is a updater URL for a database, but its missing from the misc dir, tell
// the browser so it can download it next
$info = $this->getNextMissingDbUrlInfo();
if ($info !== false) {
return Common::json_encode($info);
} else {
$view = new View("@UserCountry/_updaterNextRunTime");
$view->nextRunTime = GeoIPAutoUpdater::getNextRunTime();
$nextRunTimeHtml = $view->render();
return Common::json_encode(array('nextRunTime' => $nextRunTimeHtml));
}
} catch (Exception $ex) {
return Common::json_encode(array('error' => $ex->getMessage()));
}
}
}
/**
* Starts or continues a download for a missing GeoIP database. A database is missing if
* it has an update URL configured, but the actual database is not available in the misc
* directory.
*
* Input:
* 'url' - The URL to download the database from.
* 'continue' - 1 if we're continuing a download, 0 if we're starting one.
*
* Output:
* 'error' - If an error occurs this describes the error.
* 'to_download' - The URL of a missing database that should be downloaded next (if any).
* 'to_download_label' - The label to use w/ the progress bar that describes what we're
* downloading.
* 'current_size' - Size of the current file on disk.
* 'expected_file_size' - Size of the completely downloaded file.
*/
public function downloadMissingGeoIpDb()
{
$this->dieIfGeolocationAdminIsDisabled();
Piwik::checkUserHasSuperUserAccess();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
try {
$this->checkTokenInUrl();
Json::sendHeaderJSON();
// based on the database type (provided by the 'key' query param) determine the
// url & output file name
$key = Common::getRequestVar('key', null, 'string');
$url = GeoIPAutoUpdater::getConfiguredUrl($key);
$ext = GeoIPAutoUpdater::getGeoIPUrlExtension($url);
$filename = GeoIp::$dbNames[$key][0] . '.' . $ext;
if (substr($filename, 0, 15) == 'GeoLiteCity.dat') {
$filename = 'GeoIPCity.dat' . substr($filename, 15);
}
$outputPath = GeoIp::getPathForGeoIpDatabase($filename);
// download part of the file
$result = Http::downloadChunk(
$url, $outputPath, Common::getRequestVar('continue', true, 'int'));
// if the file is done
if ($result['current_size'] >= $result['expected_file_size']) {
GeoIPAutoUpdater::unzipDownloadedFile($outputPath, $unlink = true);
$info = $this->getNextMissingDbUrlInfo();
if ($info !== false) {
return Common::json_encode($info);
}
}
return Common::json_encode($result);
} catch (Exception $ex) {
return Common::json_encode(array('error' => $ex->getMessage()));
}
}
}
/**
* Sets the current LocationProvider type.
*
* Input:
* Requires the 'id' query parameter to be set to the desired LocationProvider's ID.
*
* Output:
* Nothing.
*/
public function setCurrentLocationProvider()
{
$this->dieIfGeolocationAdminIsDisabled();
Piwik::checkUserHasSuperUserAccess();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$this->checkTokenInUrl();
$providerId = Common::getRequestVar('id');
$provider = LocationProvider::setCurrentProvider($providerId);
if ($provider === false) {
throw new Exception("Invalid provider ID: '$providerId'.");
}
return 1;
}
}
/**
* Echo's a pretty formatted location using a specific LocationProvider.
*
* Input:
* The 'id' query parameter must be set to the ID of the LocationProvider to use.
*
* Output:
* The pretty formatted location that was obtained. Will be HTML.
*/
public function getLocationUsingProvider()
{
$providerId = Common::getRequestVar('id');
$provider = $provider = LocationProvider::getProviderById($providerId);
if ($provider === false) {
throw new Exception("Invalid provider ID: '$providerId'.");
}
$location = $provider->getLocation(array('ip' => IP::getIpFromHeader(),
'lang' => Common::getBrowserLanguage(),
'disable_fallbacks' => true));
$location = LocationProvider::prettyFormatLocation(
$location, $newline = '<br/>', $includeExtra = true);
return $location;
}
public function getNumberOfDistinctCountries()
{
return $this->getNumericValue('UserCountry.getNumberOfDistinctCountries');
}
public function getLastDistinctCountriesGraph()
{
$view = $this->getLastUnitGraph('UserCountry', __FUNCTION__, "UserCountry.getNumberOfDistinctCountries");
$view->config->columns_to_display = array('UserCountry_distinctCountries');
return $this->renderView($view);
}
/**
* Gets information for the first missing GeoIP database (if any).
*
* @return bool
*/
private function getNextMissingDbUrlInfo()
{
$missingDbs = GeoIPAutoUpdater::getMissingDatabases();
if (!empty($missingDbs)) {
$missingDbKey = $missingDbs[0];
$missingDbName = GeoIp::$dbNames[$missingDbKey][0];
$url = GeoIPAutoUpdater::getConfiguredUrl($missingDbKey);
$link = '<a href="' . $url . '">' . $missingDbName . '</a>';
return array(
'to_download' => $missingDbKey,
'to_download_label' => Piwik::translate('UserCountry_DownloadingDb', $link) . '...',
);
}
return false;
}
private function dieIfGeolocationAdminIsDisabled()
{
if (!UserCountry::isGeoLocationAdminEnabled()) {
throw new \Exception('Geo location setting page has been disabled.');
}
}
}
|