public_html/wp-content/plugins/wp-statistics/wps-updates.php
// This function downloads the GeoIP database from MaxMind.
This function downloads the GeoIP database from MaxMind.
This function downloads the GeoIP database from MaxMind .
function wp_statistics_download_geoip() {
		GLOBAL $WP_Statistics;
		if( false === function_exists( 'download_url' ) || false === function_exists( 'gzopen' ) ) { 
				$WP_Statistics->update_option('update_geoip', false);
				$result = "<div class='updated settings-error'><p><strong>" . __('Error the download_url() or gzopen() functions do not exist!', 'wp_statistics') . "</strong></p></div>";
				return $result;
		}
		if( $WP_Statistics->get_option('geoip') == false ) { return '';}
		$download_url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz';
		$upload_dir = wp_upload_dir();
		$DBFile = $upload_dir['basedir'] . '/wp-statistics/GeoLite2-Country.mmdb';
		if( !file_exists($upload_dir['basedir'] . '/wp-statistics') ) {
			if( !@mkdir($upload_dir['basedir'] . '/wp-statistics', 0755 ) ) {
				$WP_Statistics->update_option('update_geoip', false);
				$result = "<div class='updated settings-error'><p><strong>" . sprintf(__('Error creating GeoIP database directory, make sure your web server has permissions to create directories in : %s', 'wp_statistics'), $upload_dir['basedir'] ) . "</strong></p></div>";
				return $result;
			}
		}
		if( !is_writable( $upload_dir['basedir'] . '/wp-statistics' ) ) {
			$WP_Statistics->update_option('update_geoip', false);
			$result = "<div class='updated settings-error'><p><strong>" . 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'] ) . "</strong></p></div>";
			return $result;
		}
		$TempFile = download_url( $download_url );
		if (is_wp_error( $TempFile ) ) {
			$result = "<div class='updated settings-error'><p><strong>" . sprintf(__('Error downloading GeoIP database from: %s - %s', 'wp_statistics'), $download_url, $TempFile->get_error_message() ) . "</strong></p></div>";
		}
		else {
			$ZipHandle = gzopen( $TempFile, 'rb' );
			$DBfh = fopen( $DBFile, 'wb' );
			if( ! $ZipHandle ) {
				$result = "<div class='updated settings-error'><p><strong>" . sprintf(__('Error could not open downloaded GeoIP database for reading: %s', 'wp_statistics'), $TempFile) . "</strong></p></div>";
				unlink( $TempFile );
			}
			else {
				if( !$DBfh ) {
					$result = "<div class='updated settings-error'><p><strong>" . sprintf(__('Error could not open destination GeoIP database for writing %s', 'wp_statistics'), $DBFile) . "</strong></p></div>";
					unlink( $TempFile );
				}
				else {
					while( ( $data = gzread( $ZipHandle, 4096 ) ) != false ) {
						fwrite( $DBfh, $data );
					}
					gzclose( $ZipHandle );
					fclose( $DBfh );
					unlink( $TempFile );
					$result = "<div class='updated settings-error'><p><strong>" . __('GeoIP Database updated successfully!', 'wp_statistics') . "</strong></p></div>";
					$WP_Statistics->update_option('last_geoip_dl', time());
					$WP_Statistics->update_option('update_geoip', false);
					if( $WP_Statistics->get_option('geoip') && wp_statistics_geoip_supported() && $WP_Statistics->get_option('auto_pop')) {
						include_once dirname( __FILE__ ) . '/includes/functions/geoip-populate.php';
						$result .= wp_statistics_populate_geoip_info();
					}
				}
			}
		}
		if( $WP_Statistics->get_option('geoip_report') == true ) {
			$blogname = get_bloginfo('name');
			$blogemail = get_bloginfo('admin_email');
			$headers[] = "From: $blogname <$blogemail>";
			$headers[] = "MIME-Version: 1.0";
			$headers[] = "Content-type: text/html; charset=utf-8";
			if( $WP_Statistics->get_option('email_list') == '' ) { $WP_Statistics->update_option( 'email_list', $blogemail ); }
			wp_mail( $WP_Statistics->get_option('email_list'), __('GeoIP update on', 'wp_statistics') . ' ' . $blogname, $result, $headers );
		}
		return $result;
	}
function wp_statistics_download_geoip ( ) { GLOBAL $ WP_Statistics ; if ( false === function_exists ( 'download_url' ) || false === function_exists ( 'gzopen' ) ) { $ WP_Statistics -> update_option ( 'update_geoip' , false ) ; $ result = " <div class='updated settings-error'><p><strong> " . __ ( 'Error the download_url() or gzopen() functions do not exist!' , 'wp_statistics' ) . " </strong></p></div> " ; return $ result ; } if ( $ WP_Statistics -> get_option ( 'geoip' ) == false ) { return '' ; } $ download_url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz' ; $ upload_dir = wp_upload_dir ( ) ; $ DBFile = $ upload_dir [ 'basedir' ] . '/wp-statistics/GeoLite2-Country.mmdb' ; if ( ! file_exists ( $ upload_dir [ 'basedir' ] . '/wp-statistics' ) ) { if ( ! @ mkdir ( $ upload_dir [ 'basedir' ] . '/wp-statistics' , 0755 ) ) { $ WP_Statistics -> update_option ( 'update_geoip' , false ) ; $ result = " <div class='updated settings-error'><p><strong> " . sprintf ( __ ( 'Error creating GeoIP database directory, make sure your web server has permissions to create directories in : %s' , 'wp_statistics' ) , $ upload_dir [ 'basedir' ] ) . " </strong></p></div> " ; return $ result ; } } if ( ! is_writable ( $ upload_dir [ 'basedir' ] . '/wp-statistics' ) ) { $ WP_Statistics -> update_option ( 'update_geoip' , false ) ; $ result = " <div class='updated settings-error'><p><strong> " . 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' ] ) . " </strong></p></div> " ; return $ result ; } $ TempFile = download_url ( $ download_url ) ; if ( is_wp_error ( $ TempFile ) ) { $ result = " <div class='updated settings-error'><p><strong> " . sprintf ( __ ( 'Error downloading GeoIP database from: %s - %s' , 'wp_statistics' ) , $ download_url , $ TempFile -> get_error_message ( ) ) . " </strong></p></div> " ; } else { $ ZipHandle = gzopen ( $ TempFile , 'rb' ) ; $ DBfh = fopen ( $ DBFile , 'wb' ) ; if ( ! $ ZipHandle ) { $ result = " <div class='updated settings-error'><p><strong> " . sprintf ( __ ( 'Error could not open downloaded GeoIP database for reading: %s' , 'wp_statistics' ) , $ TempFile ) . " </strong></p></div> " ; unlink ( $ TempFile ) ; } else { if ( ! $ DBfh ) { $ result = " <div class='updated settings-error'><p><strong> " . sprintf ( __ ( 'Error could not open destination GeoIP database for writing %s' , 'wp_statistics' ) , $ DBFile ) . " </strong></p></div> " ; unlink ( $ TempFile ) ; } else { while ( ( $ data = gzread ( $ ZipHandle , 4096 ) ) != false ) { fwrite ( $ DBfh , $ data ) ; } gzclose ( $ ZipHandle ) ; fclose ( $ DBfh ) ; unlink ( $ TempFile ) ; $ result = " <div class='updated settings-error'><p><strong> " . __ ( 'GeoIP Database updated successfully!' , 'wp_statistics' ) . " </strong></p></div> " ; $ WP_Statistics -> update_option ( 'last_geoip_dl' , time ( ) ) ; $ WP_Statistics -> update_option ( 'update_geoip' , false ) ; if ( $ WP_Statistics -> get_option ( 'geoip' ) && wp_statistics_geoip_supported ( ) && $ WP_Statistics -> get_option ( 'auto_pop' ) ) { include_once dirname ( __FILE__ ) . '/includes/functions/geoip-populate.php' ; $ result .= wp_statistics_populate_geoip_info ( ) ; } } } } if ( $ WP_Statistics -> get_option ( 'geoip_report' ) == true ) { $ blogname = get_bloginfo ( 'name' ) ; $ blogemail = get_bloginfo ( 'admin_email' ) ; $ headers [ ] = " From:  $ blogname  < $ blogemail > " ; $ headers [ ] = " MIME-Version: 1.0 " ; $ headers [ ] = " Content-type: text/html; charset=utf-8 " ; if ( $ WP_Statistics -> get_option ( 'email_list' ) == '' ) { $ WP_Statistics -> update_option ( 'email_list' , $ blogemail ) ; } wp_mail ( $ WP_Statistics -> get_option ( 'email_list' ) , __ ( 'GeoIP update on' , 'wp_statistics' ) . ' ' . $ blogname , $ result , $ headers ) ; } return $ result ; }
This function downloads the GeoIP database from MaxMind.
This function downloads the GeoIP database from MaxMind .
// We need the download_url() and gzopen() functions, 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 GeoIP is disabled, bail out. // This is the location of the file to download. // Get the upload directory from WordPRess. // Create a variable with the name of the database file to download. // Check to see if the subdirectory we're going to upload to exists, if not create it. // Download the file from MaxMind, this places it in a temporary location. // If we failed, through a message, otherwise proceed. // Open the downloaded file to unzip it. // Create th new file to unzip to. // If we failed to open the downloaded file, through an error and remove the temporary file.  Otherwise do the actual unzip. // If we failed to open the new file, throw and error and remove the temporary file.  Otherwise actually do the unzip. // Close the files. // Delete the temporary file. // Display the success message. // Update the options to reflect the new download. // Populate any missing GeoIP information if the user has selected the option. // All of the messages displayed above are stored in a stirng, now it's time to actually output the messages.