|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | /*
|
4 |
| - * Copyright (C) 2005-2019 IP2Location.com |
| 4 | + * Copyright (C) 2005-2020 IP2Location.com |
5 | 5 | * All Rights Reserved
|
6 | 6 | *
|
7 | 7 | * This library is free software: you can redistribute it and/or
|
@@ -31,7 +31,7 @@ class Database
|
31 | 31 | *
|
32 | 32 | * @var string
|
33 | 33 | */
|
34 |
| - public const VERSION = '2.1.0'; |
| 34 | + public const VERSION = '2.2.0'; |
35 | 35 |
|
36 | 36 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
37 | 37 | // Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
@@ -481,9 +481,9 @@ class Database
|
481 | 481 | private $year;
|
482 | 482 | private $month;
|
483 | 483 | private $day;
|
484 |
| - |
| 484 | + |
485 | 485 | // This variable will be used to hold the raw row of columns's positions
|
486 |
| - private $raw_positions_row; |
| 486 | + private $raw_positions_row; |
487 | 487 |
|
488 | 488 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
489 | 489 | // Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
@@ -858,13 +858,13 @@ protected function lookup($ip, $fields = null, $asNamed = true)
|
858 | 858 | if ($fields === null) {
|
859 | 859 | $fields = $this->defaultFields;
|
860 | 860 | }
|
861 |
| - |
| 861 | + |
862 | 862 | // Get the entire row based on the pointer value.
|
863 | 863 | // The length of the row differs based on the IP version.
|
864 |
| - if (4 === $ipVersion) { |
865 |
| - $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4); |
866 |
| - } elseif (6 === $ipVersion) { |
867 |
| - $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]); |
| 864 | + if ($ipVersion === 4) { |
| 865 | + $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4); |
| 866 | + } elseif ($ipVersion === 6) { |
| 867 | + $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]); |
868 | 868 | }
|
869 | 869 |
|
870 | 870 | // turn fields into an array in case it wasn't already
|
@@ -1353,7 +1353,7 @@ private function read($pos, $len)
|
1353 | 1353 | * @return string
|
1354 | 1354 | */
|
1355 | 1355 | private function readString($pos, $additional = 0)
|
1356 |
| - { |
| 1356 | + { |
1357 | 1357 | // Get the actual pointer to the string's head by extract from raw row data.
|
1358 | 1358 | $spos = unpack('V', substr($this->raw_positions_row, $pos, 4))[1] + $additional;
|
1359 | 1359 |
|
@@ -1749,3 +1749,139 @@ private function binSearch($version, $ipNumber)
|
1749 | 1749 | return false;
|
1750 | 1750 | }
|
1751 | 1751 | }
|
| 1752 | + |
| 1753 | +/** |
| 1754 | + * IP2Proxy web service class. |
| 1755 | + */ |
| 1756 | +class WebService |
| 1757 | +{ |
| 1758 | + /** |
| 1759 | + * No cURL extension found. |
| 1760 | + * |
| 1761 | + * @var int |
| 1762 | + */ |
| 1763 | + public const EXCEPTION_NO_CURL = 10001; |
| 1764 | + |
| 1765 | + /** |
| 1766 | + * Invalid API key format. |
| 1767 | + * |
| 1768 | + * @var int |
| 1769 | + */ |
| 1770 | + public const EXCEPTION_INVALID_API_KEY = 10002; |
| 1771 | + |
| 1772 | + /** |
| 1773 | + * Web service error. |
| 1774 | + * |
| 1775 | + * @var int |
| 1776 | + */ |
| 1777 | + public const EXCEPTION_WEB_SERVICE_ERROR = 10003; |
| 1778 | + |
| 1779 | + /** |
| 1780 | + * Constructor. |
| 1781 | + * |
| 1782 | + * @param string $apiKey API key of your IP2Proxy web service |
| 1783 | + * @param string $package Supported IP2Proxy package from PX1 to PX8 |
| 1784 | + * @param bool $useSsl Enable or disabled HTTPS connection. HTTP is faster but less secure. |
| 1785 | + * |
| 1786 | + * @throws \Exception |
| 1787 | + */ |
| 1788 | + public function __construct($apiKey, $package = 'PX1', $useSsl = false) |
| 1789 | + { |
| 1790 | + if (!\extension_loaded('curl')) { |
| 1791 | + throw new \Exception(__CLASS__ . ": Please make sure your PHP setup has the 'curl' extension enabled.", self::EXCEPTION_NO_CURL); |
| 1792 | + } |
| 1793 | + |
| 1794 | + if (!preg_match('/^[0-9A-Z]{10}$/', $apiKey) && $apiKey != 'demo') { |
| 1795 | + throw new \Exception(__CLASS__ . ': Please provide a valid IP2Proxy web service API key.', self::EXCEPTION_INVALID_API_KEY); |
| 1796 | + } |
| 1797 | + |
| 1798 | + if (!preg_match('/^PX[0-9]+$/', $package)) { |
| 1799 | + $package = 'PX1'; |
| 1800 | + } |
| 1801 | + |
| 1802 | + $this->apiKey = $apiKey; |
| 1803 | + $this->package = $package; |
| 1804 | + $this->useSsl = $useSsl; |
| 1805 | + } |
| 1806 | + |
| 1807 | + /** |
| 1808 | + * This function will look the given IP address up in IP2Proxy web service. |
| 1809 | + * |
| 1810 | + * @param string $ip IP address to look up |
| 1811 | + * |
| 1812 | + * @throws \Exception |
| 1813 | + * |
| 1814 | + * @return array|false |
| 1815 | + */ |
| 1816 | + public function lookup($ip) |
| 1817 | + { |
| 1818 | + $response = $this->httpRequest('http://api.ip2proxy.com/?' . http_build_query([ |
| 1819 | + 'key' => $this->apiKey, |
| 1820 | + 'ip' => $ip, |
| 1821 | + 'package' => $this->package, |
| 1822 | + ])); |
| 1823 | + |
| 1824 | + if (($data = json_decode($response, true)) === null) { |
| 1825 | + return false; |
| 1826 | + } |
| 1827 | + |
| 1828 | + if ($data['response'] != 'OK') { |
| 1829 | + throw new \Exception(__CLASS__ . ': ' . $data['response'], self::EXCEPTION_WEB_SERVICE_ERROR); |
| 1830 | + } |
| 1831 | + |
| 1832 | + return $data; |
| 1833 | + } |
| 1834 | + |
| 1835 | + /** |
| 1836 | + * Get the remaing credit in your IP2Proxy web service account. |
| 1837 | + * |
| 1838 | + * @return int |
| 1839 | + */ |
| 1840 | + public function getCredit() |
| 1841 | + { |
| 1842 | + $response = $this->httpRequest('http://api.ip2proxy.com/?' . http_build_query([ |
| 1843 | + 'key' => $this->apiKey, |
| 1844 | + 'check' => true, |
| 1845 | + ])); |
| 1846 | + |
| 1847 | + if (($data = json_decode($response, true)) === null) { |
| 1848 | + return 0; |
| 1849 | + } |
| 1850 | + |
| 1851 | + if (!isset($data['response'])) { |
| 1852 | + return 0; |
| 1853 | + } |
| 1854 | + |
| 1855 | + return $data['response']; |
| 1856 | + } |
| 1857 | + |
| 1858 | + /** |
| 1859 | + * Open a remote web address. |
| 1860 | + * |
| 1861 | + * @param string $url Website URL |
| 1862 | + * |
| 1863 | + * @return bool|string |
| 1864 | + */ |
| 1865 | + private function httpRequest($url) |
| 1866 | + { |
| 1867 | + $ch = curl_init(); |
| 1868 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 1869 | + curl_setopt($ch, CURLOPT_FAILONERROR, 1); |
| 1870 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
| 1871 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 1872 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 1873 | + curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
| 1874 | + |
| 1875 | + $response = curl_exec($ch); |
| 1876 | + |
| 1877 | + if (!curl_errno($ch)) { |
| 1878 | + curl_close($ch); |
| 1879 | + |
| 1880 | + return $response; |
| 1881 | + } |
| 1882 | + |
| 1883 | + curl_close($ch); |
| 1884 | + |
| 1885 | + return false; |
| 1886 | + } |
| 1887 | +} |
0 commit comments