array( 'cdn' => 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz', 'github' => 'https://raw.githubusercontent.com/wp-statistics/GeoLite2-Country/master/GeoLite2-Country.mmdb.gz', 'file' => 'GeoLite2-Country', 'opt' => 'geoip' ), 'city' => array( 'cdn' => 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz', 'github' => 'https://raw.githubusercontent.com/wp-statistics/GeoLite2-City/master/GeoLite2-City.mmdb.gz', 'file' => 'GeoLite2-City', 'opt' => 'geoip_city' ) ); /** * Update option process. */ static function do_upgrade() { } /** * This function downloads the GeoIP database from MaxMind. * * @param $pack * @param string $type * * @return string */ static function download_geoip( $pack, $type = "enable" ) { GLOBAL $WP_Statistics; //Create Empty Return Function $result["status"] = false; // We need the download_url() function, it should exists on virtually all installs of PHP, but if it doesn't for some reason, bail out. if ( ! function_exists( 'download_url' ) ) { include( ABSPATH . 'wp-admin/includes/file.php' ); } // We need the wp_generate_password() function. if ( ! function_exists( 'wp_generate_password' ) ) { include( ABSPATH . 'wp-includes/pluggable.php' ); } // We need the gzopen() function, it should exists on virtually all installs of PHP, but if it doesn't for some reason, bail out. // Also stop trying to update the database as it just won't work :) if ( false === function_exists( 'gzopen' ) ) { if ( $type == "enable" ) { $WP_Statistics->update_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'], '' ); } $result["notice"] = __( 'Error the gzopen() function do not exist!', 'wp-statistics' ); WP_Statistics_Admin_Pages::set_admin_notice( $result["notice"], $type = 'error' ); return $result; } // If GeoIP is disabled, bail out. if ( $type == "update" and $WP_Statistics->get_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'] ) == '' ) { return ''; } // This is the location of the file to download. $download_url = WP_Statistics_Updates::$geoip[ $pack ]['cdn']; $response = wp_remote_get( $download_url ); // Change download url if the maxmind.com doesn't response. if ( wp_remote_retrieve_response_code( $response ) != '200' ) { $download_url = WP_Statistics_Updates::$geoip[ $pack ]['github']; } // Get the upload directory from WordPress. $upload_dir = wp_upload_dir(); // Create a variable with the name of the database file to download. $DBFile = $upload_dir['basedir'] . '/wp-statistics/' . WP_Statistics_Updates::$geoip[ $pack ]['file'] . '.mmdb'; // Check to see if the subdirectory we're going to upload to exists, if not create it. if ( ! file_exists( $upload_dir['basedir'] . '/wp-statistics' ) ) { if ( ! @mkdir( $upload_dir['basedir'] . '/wp-statistics', 0755 ) ) { if ( $type == "enable" ) { $WP_Statistics->update_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'], '' ); } $result["notice"] = sprintf( __( 'Error creating GeoIP database directory, make sure your web server has permissions to create directories in: %s', 'wp-statistics' ), $upload_dir['basedir'] ); WP_Statistics_Admin_Pages::set_admin_notice( $result["notice"], $type = 'error' ); return $result; } } if ( ! is_writable( $upload_dir['basedir'] . '/wp-statistics' ) ) { if ( $type == "enable" ) { $WP_Statistics->update_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'], '' ); } $result["notice"] = sprintf( __( 'Error setting permissions of the GeoIP database directory, make sure your web server has permissions to write to directories in : %s', 'wp-statistics' ), $upload_dir['basedir'] ); WP_Statistics_Admin_Pages::set_admin_notice( $result["notice"], $type = 'error' ); return $result; } // Download the file from MaxMind, this places it in a temporary location. $TempFile = download_url( $download_url ); // If we failed, through a message, otherwise proceed. if ( is_wp_error( $TempFile ) ) { if ( $type == "enable" ) { $WP_Statistics->update_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'], '' ); } $result["notice"] = sprintf( __( 'Error downloading GeoIP database from: %s - %s', 'wp-statistics' ), $download_url, $TempFile->get_error_message() ); WP_Statistics_Admin_Pages::set_admin_notice( $result["notice"], $type = 'error' ); } else { // Open the downloaded file to unzip it. $ZipHandle = gzopen( $TempFile, 'rb' ); // Create th new file to unzip to. $DBfh = fopen( $DBFile, 'wb' ); // If we failed to open the downloaded file, through an error and remove the temporary file. Otherwise do the actual unzip. if ( ! $ZipHandle ) { if ( $type == "enable" ) { $WP_Statistics->update_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'], '' ); } $result["notice"] = sprintf( __( 'Error could not open downloaded GeoIP database for reading: %s', 'wp-statistics' ), $TempFile ); WP_Statistics_Admin_Pages::set_admin_notice( $result["notice"], $type = 'error' ); unlink( $TempFile ); } else { // If we failed to open the new file, throw and error and remove the temporary file. Otherwise actually do the unzip. if ( ! $DBfh ) { if ( $type == "enable" ) { $WP_Statistics->update_option( WP_Statistics_Updates::$geoip[ $pack ]['opt'], '' ); } $result["notice"] = sprintf( __( 'Error could not open destination GeoIP database for writing %s', 'wp-statistics' ), $DBFile ); WP_Statistics_Admin_Pages::set_admin_notice( $result["notice"], $type = 'error' ); unlink( $TempFile ); } else { while ( ( $data = gzread( $ZipHandle, 4096 ) ) != false ) { fwrite( $DBfh, $data ); } // Close the files. gzclose( $ZipHandle ); fclose( $DBfh ); // Delete the temporary file. unlink( $TempFile ); // Display the success message. $result["status"] = true; $result["notice"] = "
" . __( 'GeoIP Database updated successfully!', 'wp-statistics' ) . "
" . sprintf( __( 'Updated %s GeoIP records in the visitors database.', 'wp-statistics' ), $count ) . "