Migrations
3 years ago
ajax
3 years ago
models
3 years ago
queries
3 years ago
sql
3 years ago
tables
3 years ago
utils
3 years ago
campaign_builder.php
3 years ago
capability_manager.php
3 years ago
chart.php
3 years ago
chart_geo.php
3 years ago
chart_svg.php
3 years ago
dashboard_options.php
3 years ago
dashboard_widget.php
3 years ago
email_reports.php
3 years ago
filters.php
3 years ago
freemius.php
3 years ago
geo_database.php
3 years ago
geo_database_download_job.php
3 years ago
health_check.php
3 years ago
independent_analytics.php
3 years ago
known_referrers.php
3 years ago
pdf.php
3 years ago
query.php
3 years ago
quick_stats.php
3 years ago
real_time.php
3 years ago
rest_api.php
3 years ago
settings.php
3 years ago
track_resource_changes.php
3 years ago
view_counter.php
3 years ago
geo_database.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class Geo_Database |
| 6 | { |
| 7 | private $database_url = 'https://d12l9frnj72cgq.cloudfront.net/iawp-geo-db.zip'; |
| 8 | private $zip_file_name = 'iawp-geo-db.zip'; |
| 9 | private $database_file_name = 'iawp-geo-db.mmdb'; |
| 10 | |
| 11 | public function __construct() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | public function ip_to_geo($ip) |
| 16 | { |
| 17 | try { |
| 18 | $reader = new MaxMind\Db\Reader($this->database_path()); |
| 19 | $geo = $reader->get($ip); |
| 20 | $reader->close(); |
| 21 | |
| 22 | return $geo; |
| 23 | } catch (\Exception $e) { |
| 24 | return null; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public function maybe_download() |
| 29 | { |
| 30 | $is_downloading = get_option('iawp_is_database_downloading') === '1'; |
| 31 | |
| 32 | if (!file_exists($this->database_path()) && !$is_downloading) { |
| 33 | $download_job = new Geo_Database_Download_Job(); |
| 34 | $download_job->dispatch(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function download() |
| 39 | { |
| 40 | $is_downloading = get_option('iawp_is_database_downloading') === '1'; |
| 41 | |
| 42 | if (!file_exists($this->database_path()) && !$is_downloading) { |
| 43 | update_option('iawp_is_database_downloading', '1'); |
| 44 | |
| 45 | try { |
| 46 | $response = wp_remote_get($this->database_url, [ |
| 47 | 'stream' => true, |
| 48 | 'filename' => $this->zip_path(), |
| 49 | 'timeout' => 60, |
| 50 | ]); |
| 51 | |
| 52 | $zip = new \ZipArchive; |
| 53 | if ($zip->open($this->zip_path()) === true) { |
| 54 | $zip->extractTo($this->upload_path()); |
| 55 | $zip->close(); |
| 56 | } else { |
| 57 | } |
| 58 | |
| 59 | wp_delete_file($this->zip_path()); |
| 60 | } catch (\Exception $e) { |
| 61 | } |
| 62 | |
| 63 | delete_option('iawp_is_database_downloading'); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private function upload_path() |
| 68 | { |
| 69 | return trailingslashit(wp_upload_dir()['basedir']); |
| 70 | } |
| 71 | |
| 72 | private function zip_path() |
| 73 | { |
| 74 | return trailingslashit(wp_upload_dir()['basedir']) . $this->zip_file_name; |
| 75 | } |
| 76 | |
| 77 | private function database_path() |
| 78 | { |
| 79 | return trailingslashit(wp_upload_dir()['basedir']) . $this->database_file_name; |
| 80 | } |
| 81 | } |
| 82 |