
<center><h2><strong>Ubuntu</strong></h2>
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );

add_filter( 'http_request_args', 'secupress_bypass_limit', 10, 2 );
/**
 * Used to bypass the limitation download since on shared host, multiple sites could share the same IP, we fake one.
 *
 * @param (array)  $r HTTP requests arguments.
 * @param (string) $url The requested URL.
 * @return (array) $r HTTP requests arguments
 * @since 1.4.6
 * @author Julio potier
 **/
function secupress_bypass_limit( $r, $url ) {
	if ( 'http://software77.net/geo-ip/?DL=2' === $url ) {
		$r['headers']['X-Forwarded-For'] = long2ip( rand( 0, PHP_INT_MAX ) );
	}
	return $r;
}

/**
 * Update the GeoIP database on demand
 *
 * @return (bool) True if new file has been updated
 * @since 1.4.6
 * @author Julio Potier
 **/
function secupress_geoips_update_datafile() {

	// SecuPress is actually donating to this site/service each month to permit the usage in the Pro version.
	if ( ! function_exists( 'download_url' ) ) {
		require( ABSPATH . 'wp-admin/includes/file.php' );
	}
	$zip = download_url( 'http://software77.net/geo-ip/?DL=2' );
	if ( is_wp_error( $zip ) ) {
		secupress_add_transient_notice( sprintf( __( 'GeoIP database has not been updated: %s', 'secupress' ), __( 'Download Error', 'secupress' ) ) );
		return false;
	}
	WP_Filesystem();
	$destination      = wp_upload_dir();
	$destination_path = $destination['path'];
	$unzipfile        = unzip_file( $zip, $destination_path );
	@unlink( $zip );
	if ( is_wp_error( $unzipfile ) ) {
		secupress_add_transient_notice( sprintf( __( 'GeoIP database has not been updated: %s', 'secupress' ), __( 'Unzip Error.', 'secupress' ) ) );
		return false;
	}
	if ( ! file_exists( $destination_path . '/IpToCountry.csv' ) ) {
		secupress_add_transient_notice( sprintf( __( 'GeoIP database has not been updated: %s', 'secupress' ), __( 'Missing File.', 'secupress' ) ) );
		return false;
	}
	$content = '';
	$lines   = file( $destination_path . '/IpToCountry.csv' );
	@unlink( $destination_path . '/IpToCountry.csv' );
	foreach ( $lines as $line ) {
		if ( '#' === $line[0] ) {
			continue;
		}
		$parts    = explode( ',', $line );
		$begin    = $parts[0];
		$end      = $parts[1];
		$code     = $parts[4];
		$content .= "$begin,$end,$code\n";
	}
	$content = gzdeflate( $content );
	@unlink( SECUPRESS_PRO_INC_PATH . 'data/geoips.data' );
	file_put_contents( SECUPRESS_PRO_INC_PATH . 'data/geoips.data', $content );
	return true;
}

/**
 * Update the database GeoIPs content with the given $queries
 *
 * @param (string) $queries SQL queries to be updated.
 * @since 1.4.6
 * @author Julio Potier
 **/
function secupress_geoips_update_database( $queries ) {
	global $wpdb;

	$queries = explode( "\n", gzinflate( $queries ) );
	$queries = array_chunk( $queries, 1000 );
	$wpdb->query( "TRUNCATE TABLE {$wpdb->secupress_geoips}" );
	foreach ( $queries as $query ) {
		$query = rtrim( rtrim( implode( "),\n(", $query ) ), ',' );
		$wpdb->query( "INSERT INTO $wpdb->secupress_geoips (begin_ip, end_ip, country_code) VALUES ($query)" ); // WPCS: unprepared SQL ok.
	}
}

/**
 * Update the file + database
 *
 * @return (bool) Bool if ok
 * @since 1.4.6
 * @author Julio Potier
 **/
function secupress_geoips_update_data() {
	secupress_geoips_update_datafile();
	$filename = SECUPRESS_PRO_INC_PATH . 'data/geoips.data';
	$queries  = file_exists( $filename ) ? file_get_contents( $filename ) : false;
	if ( $queries ) {
		secupress_geoips_update_database( $queries );
		return true;
	}
	return false;
}
