AJAX
1 year ago
Admin_Page
1 year ago
Custom_WordPress_Columns
2 years ago
Data_Pruning
1 year ago
Date_Picker
1 year ago
Date_Range
1 year ago
Ecommerce
1 year ago
Email_Reports
1 year ago
Filter_Lists
2 years ago
Form_Submissions
1 year ago
Interval
2 years ago
Menu_Bar_Stats
1 year ago
Migrations
1 year ago
Models
1 year ago
Public_API
2 years ago
Rows
1 year ago
Statistics
1 year ago
Tables
1 year ago
Utils
1 year ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
1 year ago
Chart.php
1 year ago
Chart_Geo.php
1 year ago
Cron_Job.php
1 year ago
Cron_Job_Autoloader.php
1 year ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
1 year ago
Filters.php
1 year ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
1 year ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
1 year ago
Interrupt.php
1 year ago
Known_Referrers.php
2 years ago
Patch.php
1 year ago
Plugin_Conflict_Detector.php
1 year ago
Plugin_Group.php
1 year ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
1 year ago
Quick_Stats.php
2 years ago
REST_API.php
1 year ago
Real_Time.php
1 year ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
1 year ago
Resource_Identifier.php
1 year ago
Settings.php
1 year ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
1 year ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
Geo_Database_Manager.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use DateInterval; |
| 6 | use DateTime; |
| 7 | use IAWPSCOPED\Proper\Timezone; |
| 8 | use Throwable; |
| 9 | use ZipArchive; |
| 10 | /** @internal */ |
| 11 | class Geo_Database_Manager |
| 12 | { |
| 13 | // Updating the database? Read the Wiki page "Updating The Geo Database" |
| 14 | // https://github.com/andrewjmead/independent-analytics/wiki/Updating-The-Geo-Database |
| 15 | private $zip_download_url = 'https://assets.independentwp.com/iawp-geo-db-6.mmdb.zip'; |
| 16 | private $raw_download_url = 'https://assets.independentwp.com/iawp-geo-db-6.mmdb'; |
| 17 | private $database_checksum = '2213359f8d395c4f1a352007af9495ae'; |
| 18 | public function download() : void |
| 19 | { |
| 20 | if (!$this->should_download()) { |
| 21 | return; |
| 22 | } |
| 23 | \update_option('iawp_is_database_downloading', '1', \true); |
| 24 | $this->download_zip_database_and_extract(); |
| 25 | if (!$this->is_existing_database_valid()) { |
| 26 | $this->download_raw_database(); |
| 27 | } else { |
| 28 | } |
| 29 | \update_option('iawp_is_database_downloading', '0', \true); |
| 30 | $this->record_attempt(); |
| 31 | } |
| 32 | public function should_download() : bool |
| 33 | { |
| 34 | if (!$this->has_attempt_interval_elapsed()) { |
| 35 | return \false; |
| 36 | } |
| 37 | if (\get_option('iawp_is_database_downloading', '0') === '1') { |
| 38 | return \false; |
| 39 | } |
| 40 | if ($this->is_existing_database_valid()) { |
| 41 | $this->record_attempt(); |
| 42 | return \false; |
| 43 | } |
| 44 | return \true; |
| 45 | } |
| 46 | public function delete() : void |
| 47 | { |
| 48 | \wp_delete_file(self::path_to_database()); |
| 49 | } |
| 50 | private function download_zip_database_and_extract() : void |
| 51 | { |
| 52 | \wp_remote_get($this->zip_download_url, ['stream' => \true, 'filename' => $this->path_to_database_zip(), 'timeout' => 60]); |
| 53 | try { |
| 54 | $zip = new ZipArchive(); |
| 55 | if ($zip->open($this->path_to_database_zip()) === \true) { |
| 56 | $zip->extractTo(\IAWPSCOPED\iawp_upload_path_to('', \true)); |
| 57 | $zip->close(); |
| 58 | } |
| 59 | } catch (Throwable $e) { |
| 60 | // It's ok to fail |
| 61 | } |
| 62 | \wp_delete_file($this->path_to_database_zip()); |
| 63 | } |
| 64 | private function download_raw_database() : void |
| 65 | { |
| 66 | \wp_remote_get($this->raw_download_url, ['stream' => \true, 'filename' => self::path_to_database(), 'timeout' => 60]); |
| 67 | } |
| 68 | private function is_existing_database_valid() : bool |
| 69 | { |
| 70 | if (!\file_exists(self::path_to_database())) { |
| 71 | return \false; |
| 72 | } |
| 73 | try { |
| 74 | return \verify_file_md5(self::path_to_database(), $this->database_checksum); |
| 75 | } catch (Throwable $e) { |
| 76 | return \false; |
| 77 | } |
| 78 | } |
| 79 | private function has_attempt_interval_elapsed() : bool |
| 80 | { |
| 81 | $last_attempted_at = $this->last_attempted_at(); |
| 82 | $interval = new DateInterval('PT30M'); |
| 83 | if (\is_null($last_attempted_at)) { |
| 84 | return \true; |
| 85 | } |
| 86 | $is_past_interval_time = $last_attempted_at->add($interval) < new DateTime('now', Timezone::utc_timezone()); |
| 87 | if ($is_past_interval_time) { |
| 88 | return \true; |
| 89 | } |
| 90 | return \false; |
| 91 | } |
| 92 | private function last_attempted_at() : ?DateTime |
| 93 | { |
| 94 | $option_value = \get_option('iawp_geo_database_download_last_attempted_at', \false); |
| 95 | if (!$option_value) { |
| 96 | return null; |
| 97 | } |
| 98 | try { |
| 99 | return new DateTime($option_value, Timezone::utc_timezone()); |
| 100 | } catch (Throwable $e) { |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | private function record_attempt() : void |
| 105 | { |
| 106 | $now = new DateTime('now', Timezone::utc_timezone()); |
| 107 | $value = $now->format('Y-m-d\\TH:i:s'); |
| 108 | \update_option('iawp_geo_database_download_last_attempted_at', $value, \true); |
| 109 | } |
| 110 | private function path_to_database_zip() : string |
| 111 | { |
| 112 | return \IAWPSCOPED\iawp_upload_path_to('iawp-geo-db.zip', \true); |
| 113 | } |
| 114 | public static function path_to_database() : string |
| 115 | { |
| 116 | return \IAWPSCOPED\iawp_upload_path_to('iawp-geo-db.mmdb', \true); |
| 117 | } |
| 118 | } |
| 119 |