PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.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-geolcation.php

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

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