PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / integrations / class-maxmind-geolocation.php

class-maxmind-geolocation.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at src/integrations/class-maxmind-geolocation.php

134 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles interactions with Maxmind geolocation.
4 *
5 * @package WP_Defender\Integrations
6 */
7
8 namespace WP_Defender\Integrations;
9
10 use WP_Error;
11 use PharData;
12 use Exception;
13 use WP_Defender\Traits\IO;
14 use WP_Filesystem_Base;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 die;
18 }
19
20 /**
21 * Maxmind geolocation integration module.
22 *
23 * @since 2.7.1
24 */
25 class MaxMind_Geolocation {
26
27 use IO;
28
29 /**
30 * The name of the MaxMind database.
31 */
32 public const DB_NAME = 'GeoLite2-Country';
33
34 /**
35 * The extension of the MaxMind database.
36 */
37 public const DB_EXT = '.mmdb';
38
39 /**
40 * The name of the Maxmind directory
41 */
42 public const DB_DIRECTORY = 'maxmind';
43
44 /**
45 * Returns the full name of the database by concatenating the database name and extension.
46 *
47 * @return string The full name of the database.
48 */
49 public function get_db_full_name() {
50 return self::DB_NAME . self::DB_EXT;
51 }
52
53 /**
54 * Retrieves the URL for downloading the MaxMind database using the provided license key.
55 * Todo: extend the logic to handle different results.
56 *
57 * @param string $license_key The license key for the MaxMind database.
58 *
59 * @return string|WP_Error The URL for downloading the database, or a WP_Error object on failure.
60 */
61 public function get_downloaded_url( $license_key ) {
62 $url = add_query_arg(
63 array(
64 'edition_id' => self::DB_NAME,
65 'license_key' => rawurlencode( sanitize_text_field( $license_key ) ),
66 'suffix' => 'tar.gz',
67 ),
68 'https://download.maxmind.com/app/geoip_download'
69 );
70 if ( ! function_exists( 'download_url' ) ) {
71 require_once ABSPATH . 'wp-admin/includes/file.php';
72 }
73
74 return download_url( $url );
75 }
76
77 /**
78 * Get database base path.
79 *
80 * @return string
81 * @since 2.8.0
82 */
83 public function get_db_base_path() {
84 return $this->get_tmp_path( true ) . DIRECTORY_SEPARATOR . self::DB_DIRECTORY;
85 }
86
87 /**
88 * Extract downloaded database.
89 *
90 * @param string $temp_path The path to the temporary file.
91 *
92 * @return string|WP_Error Path to the database file or an error.
93 * @since 2.8.0
94 */
95 public function extract_db_archive( $temp_path ) {
96 try {
97 $phar = new PharData( $temp_path );
98
99 $base_path = $this->get_db_base_path();
100 if ( ! is_dir( $base_path ) ) {
101 wp_mkdir_p( $base_path );
102 }
103
104 $phar->extractTo( $base_path, null, true );
105 $geodb_path = $base_path . DIRECTORY_SEPARATOR . $phar->current()->getFileName() . DIRECTORY_SEPARATOR . $this->get_db_full_name();
106 } catch ( Exception $exception ) {
107 return new WP_Error( 'wpdef_maxmind_geolocation_database_archive', $exception->getMessage() );
108 } finally {
109 // Archive file is not needed.
110 wp_delete_file( $temp_path );
111 }
112
113 return $geodb_path;
114 }
115
116 /**
117 * Delete the database.
118 *
119 * @since 2.8.0
120 */
121 public function delete_database() {
122 global $wp_filesystem;
123 // Initialize the WP filesystem, no more using 'file-put-contents' function.
124 if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) {
125 require_once ABSPATH . '/wp-admin/includes/file.php';
126 WP_Filesystem();
127 }
128 $database_path = $this->get_db_base_path();
129 if ( $wp_filesystem->exists( $database_path ) ) {
130 $wp_filesystem->delete( $database_path, true );
131 }
132 }
133 }
134