slavkluev /
yii2-ip2location
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace slavkluev\Ip2Location; |
||
| 6 | |||
| 7 | use IP2Location\Database; |
||
| 8 | use Yii; |
||
| 9 | use yii\base\Component; |
||
| 10 | |||
| 11 | class Ip2Location extends Component |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Path to database file. |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | public $dbFile; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Unique download token to download IP2Location databases. |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $downloadToken; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Database code for downloading. |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | public $downloadCode = 'DB1LITEBIN'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Caching mode (one of FILE_IO, MEMORY_CACHE, or SHARED_MEMORY). |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | public $mode = Database::FILE_IO; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Field(s) to return. |
||
| 39 | * @var array|int |
||
| 40 | */ |
||
| 41 | public $defaultFields = Database::ALL; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Database |
||
| 45 | */ |
||
| 46 | protected $db; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @throws \Exception |
||
| 50 | */ |
||
| 51 | 9 | public function init() |
|
| 52 | { |
||
| 53 | 9 | parent::init(); |
|
| 54 | 9 | $this->loadDb(); |
|
| 55 | 9 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @throws \Exception |
||
| 59 | */ |
||
| 60 | 9 | public function loadDb() |
|
| 61 | { |
||
| 62 | 9 | $this->db = new Database($this->getDbFile(), $this->mode, $this->defaultFields); |
|
| 63 | 9 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * This function will look the given IP address up in the database and return the result(s) asked for. |
||
| 67 | * |
||
| 68 | * @param string|null $ip |
||
| 69 | * |
||
| 70 | * @return array|bool|mixed |
||
| 71 | */ |
||
| 72 | 6 | public function ip(?string $ip = null) |
|
| 73 | { |
||
| 74 | 6 | if ($ip === null) { |
|
| 75 | $ip = Yii::$app->request->getUserIP(); |
||
| 76 | } |
||
| 77 | |||
| 78 | 6 | return $this->db->lookup($ip); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Updates and reloads the database. |
||
| 83 | * |
||
| 84 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 85 | * @throws \Exception |
||
| 86 | */ |
||
| 87 | public function update() |
||
| 88 | { |
||
| 89 | $uploader = new Uploader(); |
||
| 90 | $uploader->update($this->downloadToken, $this->downloadCode, $this->getDbFile()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 91 | $this->loadDb(); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return Database |
||
| 96 | */ |
||
| 97 | 3 | public function getDb(): Database |
|
| 98 | { |
||
| 99 | 3 | return $this->db; |
|
| 100 | } |
||
| 101 | |||
| 102 | 9 | protected function getDbFile() |
|
| 103 | { |
||
| 104 | 9 | return $this->dbFile ?: Yii::getAlias('@vendor/slavkluev/yii2-ip2location/database/DB.BIN'); |
|
| 105 | } |
||
| 106 | } |
||
| 107 |