Ejemplo n.º 1
0
<?php

global $wpdb;
$wp_prefix = $wpdb->prefix;
if (!is_super_admin()) {
    wp_die(__('Access denied!', 'wp_statistics'));
}
if (array_key_exists('populate', $_GET)) {
    if (intval($_GET['populate']) == 1) {
        require_once plugin_dir_path(__FILE__) . '../functions/geoip-populate.php';
        echo wp_statistics_populate_geoip_info();
    }
}
if (array_key_exists('hash-ips', $_GET)) {
    if (intval($_GET['hash-ips']) == 1) {
        // Generate a random salt
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < 50; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        // Get the rows from the Visitors table.
        $result = $wpdb->get_results("SELECT DISTINCT ip FROM {$wp_prefix}statistics_visitor");
        foreach ($result as $row) {
            if (substr($row->ip, 0, 6) != '#hash#') {
                $wpdb->update($wp_prefix . "statistics_visitor", array('ip' => '#hash#' . sha1($row->ip . $randomString)), array('ip' => $row->ip));
            }
        }
        echo "<div class='updated settings-error'><p><strong>" . __('IP Addresses replaced with hash values.', 'wp_statistics') . "</strong></p></div>";
    }
}
	function wp_statistics_download_geoip() {

		GLOBAL $WP_Statistics;

		// 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( 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 GeoIP is disabled, bail out.
		if( $WP_Statistics->get_option('geoip') == false ) { return '';}

		// This is the location of the file to download.
		$download_url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz';

		// 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/GeoLite2-Country.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 ) ) {
				$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;
		}
		
		// 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 ) ) {
			$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 {
			// 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 ) {
				$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 we failed to open the new file, throw and error and remove the temporary file.  Otherwise actually do the unzip.
				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 );
					}

					// Close the files.
					gzclose( $ZipHandle );
					fclose( $DBfh );

					// Delete the temporary file.
					unlink( $TempFile );

					// Display the success message.
					$result = "<div class='updated settings-error'><p><strong>" . __('GeoIP Database updated successfully!', 'wp_statistics') . "</strong></p></div>";

					// Update the options to reflect the new download.
					$WP_Statistics->update_option('last_geoip_dl', time());
					$WP_Statistics->update_option('update_geoip', false);

					// Populate any missing GeoIP information if the user has selected the option.
					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 );
		}

		// All of the messages displayed above are stored in a stirng, now it's time to actually output the messages.
		return $result;
	}