/** * 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 json_encode($result); } catch (Exception $ex) { return json_encode(array('error' => $ex->getMessage())); } } }