Giter VIP home page Giter VIP logo

Comments (2)

gor3a avatar gor3a commented on June 2, 2024

`
class Updater
{
const GEOLITE2_URL_BASE = 'GeoLite2-City';

protected $databaseFileGzipped;

protected $databaseFile;

protected $md5File;

protected $messages = [];

/**
 * Add a message.
 *
 * @param $string
 */
private function addMessage($string)
{
    $this->messages[] = $string;
}

protected function databaseIsUpdated($geoDbFileUrl, $geoDbMd5Url, $destinationPath)
{
    $destinationGeoDbFile = $this->removeGzipExtension($destinationPath . DIRECTORY_SEPARATOR . basename($geoDbFileUrl));
    $this->md5File = base_path("config\geoip\GeoLite2-City.md5");
    if (!file_exists($destinationGeoDbFile)) {
        return false;
    }

    if ($updated = file_get_contents($this->md5File) == md5_file($destinationGeoDbFile)) {
        $this->addMessage('Database is already updated.');
    }

    return $updated;
}

/**
 * Download gzipped database, unzip and check md5.
 *
 * @param $destinationPath
 * @param $geoDbUrl
 * @return bool
 */
protected function downloadGzipped($destinationPath, $geoDbUrl)
{
    if (!$this->databaseFileGzipped = $this->getHTTPFile($geoDbUrl, ($destination = $destinationPath . DIRECTORY_SEPARATOR))) {
        $this->addMessage("Unable to download file {$geoDbUrl} to {$destination}.");
    }

    $this->databaseFile = $this->dezipGzFile($destinationPath . DIRECTORY_SEPARATOR . basename($geoDbUrl));

    return $this->md5Match();
}

private function getDbFileName($geoDbUrl)
{
    return $geoDbUrl ?: static::GEOLITE2_URL_BASE . '.mmdb.gz';
}

private function getMd5FileName($geoDbMd5Url)
{
    return $geoDbMd5Url ?: static::GEOLITE2_URL_BASE . '.md5';
}

/**
 * Get messages.
 *
 * @return mixed
 */
public function getMessages()
{
    return $this->messages;
}

/**
 * Make directory.
 *
 * @param $destinationPath
 * @return bool
 */
protected function makeDir($destinationPath)
{
    return file_exists($destinationPath) || mkdir($destinationPath, 0770, true);
}

/**
 * Compare MD5s.
 *
 * @return bool
 */
private function md5Match()
{
    try {
        if (!$match = md5_file($this->databaseFile) == file_get_contents($this->md5File)) {
            $this->addMessage("MD5 is not matching for {$this->databaseFile} and {$this->md5File}.");

            return false;
        }
    } catch (Exception $e) {
        echo $e;
    }

    $this->addMessage("Database successfully downloaded to {$this->databaseFile}.");

    return true;
}

/**
 * Remove .gzip extension from file.
 *
 * @param $filePath
 * @return mixed
 */
protected function removeGzipExtension($filePath)
{
    return str_replace('.gz', '', $filePath);
}

/**
 * Download and update GeoIp database.
 *
 * @param $destinationPath
 * @param null $geoDbUrl
 * @param null $geoDbMd5Url
 * @return bool
 */
public function updateGeoIpFiles($destinationPath, $geoDbUrl = null, $geoDbMd5Url = null)
{
    if ($this->databaseIsUpdated($geoDbUrl = $this->getDbFileName($geoDbUrl), $this->getMd5FileName($geoDbMd5Url), $destinationPath)) {
        return true;
    }

    if ($this->downloadGzipped($destinationPath, $geoDbUrl)) {
        return true;
    }

    $this->addMessage("Unknown error downloading {$geoDbUrl}.");

    return false;
}

/**
 * Read url to file.
 *
 * @param $uri
 * @param $destinationPath
 * @return bool|string
 */
protected function getHTTPFile($uri, $destinationPath)
{
    set_time_limit(360);

    if (!$this->makeDir($destinationPath)) {
        return false;
    }

    $fileWriteName = $destinationPath . basename($uri);

    if (($fileRead = @fopen($uri, "rb")) === false || ($fileWrite = @fopen($fileWriteName, 'wb')) === false) {
        $this->addMessage("Unable to open {$uri} (read) or {$fileWriteName} (write).");

        return false;
    }

    while (!feof($fileRead)) {
        $content = @fread($fileRead, 1024 * 16);

        $success = fwrite($fileWrite, $content);

        if ($success === false) {
            $this->addMessage("Error downloading file {$uri} to {$fileWriteName}.");

            return false;
        }
    }

    fclose($fileWrite);

    fclose($fileRead);

    return $fileWriteName;
}

/**
 * Extract gzip file.
 *
 * @param $filePath
 * @return bool|mixed
 */
protected function dezipGzFile($filePath)
{
    $buffer_size = 8192; // read 8kb at a time

    $out_file_name = $this->removeGzipExtension($filePath);

    $fileRead = gzopen($filePath, 'rb');

    $fileWrite = fopen($out_file_name, 'wb');

    if ($fileRead === false || $fileWrite === false) {
        $this->addMessage("Unable to extract gzip file {$filePath} to {$out_file_name}.");

        return false;
    }

    while (!gzeof($fileRead)) {
        $success = fwrite($fileWrite, gzread($fileRead, $buffer_size));

        if ($success === false) {
            $this->addMessage("Error degzipping file {$filePath} to {$out_file_name}.");

            return false;
        }
    }

    // Files are done, close files
    fclose($fileWrite);

    gzclose($fileRead);

    return $out_file_name;
}

`
Change Updater.php file with this code,
the problem because they try to download a file called "GeoLite2-City" and extract that and check md5,
but the file can't download it now so it get error

from tracker.

ak-rocksdev avatar ak-rocksdev commented on June 2, 2024

Same issue here, been tried @gor3a 's Solution, but still, there's an issue. Check out my image below:

Error tracker geoip

Any solution?

FYI: That GeoLite2-City.tar.gz file I add is just in case the original file is broken, I get a mirror version from the original link inside updater.php file

from tracker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.