PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.3.1
Defender Security – Malware Scanner, Login Security & Firewall v5.3.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.3.1, at src/integrations/class-maxmind-geolocation.php

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