PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/IP.php

https://github.com/CodeYellowBV/piwik
PHP | 431 lines | 219 code | 48 blank | 164 comment | 58 complexity | dec6791fe3477560fa3462757f190fc7 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. /**
  11. * Contains IP address helper functions (for both IPv4 and IPv6).
  12. *
  13. * As of Piwik 1.3, IP addresses are stored in the DB has VARBINARY(16),
  14. * and passed around in network address format which has the advantage of
  15. * being in big-endian byte order. This allows for binary-safe string
  16. * comparison of addresses (of the same length), even on Intel x86.
  17. *
  18. * As a matter of naming convention, we use `$ip` for the network address format
  19. * and `$ipString` for the presentation format (i.e., human-readable form).
  20. *
  21. * We're not using the network address format (in_addr) for socket functions,
  22. * so we don't have to worry about incompatibility with Windows UNICODE
  23. * and inetPtonW().
  24. *
  25. * @api
  26. */
  27. class IP
  28. {
  29. const MAPPED_IPv4_START = '::ffff:';
  30. /**
  31. * Removes the port and the last portion of a CIDR IP address.
  32. *
  33. * @param string $ipString The IP address to sanitize.
  34. * @return string
  35. */
  36. public static function sanitizeIp($ipString)
  37. {
  38. $ipString = trim($ipString);
  39. // CIDR notation, A.B.C.D/E
  40. $posSlash = strrpos($ipString, '/');
  41. if ($posSlash !== false) {
  42. $ipString = substr($ipString, 0, $posSlash);
  43. }
  44. $posColon = strrpos($ipString, ':');
  45. $posDot = strrpos($ipString, '.');
  46. if ($posColon !== false) {
  47. // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE
  48. $posRBrac = strrpos($ipString, ']');
  49. if ($posRBrac !== false && $ipString[0] == '[') {
  50. $ipString = substr($ipString, 1, $posRBrac - 1);
  51. }
  52. if ($posDot !== false) {
  53. // IPv4 address with port, A.B.C.D:EEEE
  54. if ($posColon > $posDot) {
  55. $ipString = substr($ipString, 0, $posColon);
  56. }
  57. // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J
  58. } else if (strpos($ipString, ':') === $posColon) {
  59. $ipString = substr($ipString, 0, $posColon);
  60. }
  61. // else: IPv6 address, A:B:C:D:E:F:G:H
  62. }
  63. // else: IPv4 address, A.B.C.D
  64. return $ipString;
  65. }
  66. /**
  67. * Sanitize human-readable (user-supplied) IP address range.
  68. *
  69. * Accepts the following formats for $ipRange:
  70. * - single IPv4 address, e.g., 127.0.0.1
  71. * - single IPv6 address, e.g., ::1/128
  72. * - IPv4 block using CIDR notation, e.g., 192.168.0.0/22 represents the IPv4 addresses from 192.168.0.0 to 192.168.3.255
  73. * - IPv6 block using CIDR notation, e.g., 2001:DB8::/48 represents the IPv6 addresses from 2001:DB8:0:0:0:0:0:0 to 2001:DB8:0:FFFF:FFFF:FFFF:FFFF:FFFF
  74. * - wildcards, e.g., 192.168.0.*
  75. *
  76. * @param string $ipRangeString IP address range
  77. * @return string|bool IP address range in CIDR notation OR false
  78. */
  79. public static function sanitizeIpRange($ipRangeString)
  80. {
  81. $ipRangeString = trim($ipRangeString);
  82. if (empty($ipRangeString)) {
  83. return false;
  84. }
  85. // IPv4 address with wildcards '*'
  86. if (strpos($ipRangeString, '*') !== false) {
  87. if (preg_match('~(^|\.)\*\.\d+(\.|$)~D', $ipRangeString)) {
  88. return false;
  89. }
  90. $bits = 32 - 8 * substr_count($ipRangeString, '*');
  91. $ipRangeString = str_replace('*', '0', $ipRangeString);
  92. }
  93. // CIDR
  94. if (($pos = strpos($ipRangeString, '/')) !== false) {
  95. $bits = substr($ipRangeString, $pos + 1);
  96. $ipRangeString = substr($ipRangeString, 0, $pos);
  97. }
  98. // single IP
  99. if (($ip = @inet_pton($ipRangeString)) === false)
  100. return false;
  101. $maxbits = strlen($ip) * 8;
  102. if (!isset($bits))
  103. $bits = $maxbits;
  104. if ($bits < 0 || $bits > $maxbits) {
  105. return false;
  106. }
  107. return "$ipRangeString/$bits";
  108. }
  109. /**
  110. * Converts an IP address in presentation format to network address format.
  111. *
  112. * @param string $ipString IP address, either IPv4 or IPv6, e.g., `"127.0.0.1"`.
  113. * @return string Binary-safe string, e.g., `"\x7F\x00\x00\x01"`.
  114. */
  115. public static function P2N($ipString)
  116. {
  117. // use @inet_pton() because it throws an exception and E_WARNING on invalid input
  118. $ip = @inet_pton($ipString);
  119. return $ip === false ? "\x00\x00\x00\x00" : $ip;
  120. }
  121. /**
  122. * Convert network address format to presentation format.
  123. *
  124. * See also {@link prettyPrint()}.
  125. *
  126. * @param string $ip IP address in network address format.
  127. * @return string IP address in presentation format.
  128. */
  129. public static function N2P($ip)
  130. {
  131. // use @inet_ntop() because it throws an exception and E_WARNING on invalid input
  132. $ipStr = @inet_ntop($ip);
  133. return $ipStr === false ? '0.0.0.0' : $ipStr;
  134. }
  135. /**
  136. * Alias for {@link N2P()}.
  137. *
  138. * @param string $ip IP address in network address format.
  139. * @return string IP address in presentation format.
  140. */
  141. public static function prettyPrint($ip)
  142. {
  143. return self::N2P($ip);
  144. }
  145. /**
  146. * Returns true if `$ip` is an IPv4, IPv4-compat, or IPv4-mapped address, false
  147. * if otherwise.
  148. *
  149. * @param string $ip IP address in network address format.
  150. * @return bool True if IPv4, else false.
  151. */
  152. public static function isIPv4($ip)
  153. {
  154. // in case mbstring overloads strlen and substr functions
  155. $strlen = function_exists('mb_orig_strlen') ? 'mb_orig_strlen' : 'strlen';
  156. $substr = function_exists('mb_orig_substr') ? 'mb_orig_substr' : 'substr';
  157. // IPv4
  158. if ($strlen($ip) == 4) {
  159. return true;
  160. }
  161. // IPv6 - transitional address?
  162. if ($strlen($ip) == 16) {
  163. if (substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 0, 12) === 0
  164. || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0
  165. ) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. /**
  172. * Converts an IP address (in network address format) to presentation format.
  173. * This is a backward compatibility function for code that only expects
  174. * IPv4 addresses (i.e., doesn't support IPv6).
  175. *
  176. * This function does not support the long (or its string representation)
  177. * returned by the built-in ip2long() function, from Piwik 1.3 and earlier.
  178. *
  179. * @param string $ip IPv4 address in network address format.
  180. * @return string IP address in presentation format.
  181. */
  182. public static function long2ip($ip)
  183. {
  184. // IPv4
  185. if (strlen($ip) == 4) {
  186. return self::N2P($ip);
  187. }
  188. // IPv6 - transitional address?
  189. if (strlen($ip) == 16) {
  190. if (substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 0, 12) === 0
  191. || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0
  192. ) {
  193. // remap 128-bit IPv4-mapped and IPv4-compat addresses
  194. return self::N2P(substr($ip, 12));
  195. }
  196. }
  197. return '0.0.0.0';
  198. }
  199. /**
  200. * Returns true if $ip is an IPv6 address, false if otherwise. This function does
  201. * a naive check. It assumes that whatever format $ip is in, it is well-formed.
  202. *
  203. * @param string $ip
  204. * @return bool
  205. */
  206. public static function isIPv6($ip)
  207. {
  208. return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
  209. }
  210. /**
  211. * Returns true if $ip is a IPv4 mapped address, false if otherwise.
  212. *
  213. * @param string $ip
  214. * @return bool
  215. */
  216. public static function isMappedIPv4($ip)
  217. {
  218. return substr($ip, 0, strlen(self::MAPPED_IPv4_START)) === self::MAPPED_IPv4_START;
  219. }
  220. /**
  221. * Returns an IPv4 address from a 'mapped' IPv6 address.
  222. *
  223. * @param string $ip eg, `'::ffff:192.0.2.128'`
  224. * @return string eg, `'192.0.2.128'`
  225. */
  226. public static function getIPv4FromMappedIPv6($ip)
  227. {
  228. return substr($ip, strlen(self::MAPPED_IPv4_START));
  229. }
  230. /**
  231. * Get low and high IP addresses for a specified range.
  232. *
  233. * @param array $ipRange An IP address range in presentation format.
  234. * @return array|bool Array `array($lowIp, $highIp)` in network address format, or false on failure.
  235. */
  236. public static function getIpsForRange($ipRange)
  237. {
  238. if (strpos($ipRange, '/') === false) {
  239. $ipRange = self::sanitizeIpRange($ipRange);
  240. }
  241. $pos = strpos($ipRange, '/');
  242. $bits = substr($ipRange, $pos + 1);
  243. $range = substr($ipRange, 0, $pos);
  244. $high = $low = @inet_pton($range);
  245. if ($low === false) {
  246. return false;
  247. }
  248. $lowLen = strlen($low);
  249. $i = $lowLen - 1;
  250. $bits = $lowLen * 8 - $bits;
  251. for ($n = (int)($bits / 8); $n > 0; $n--, $i--) {
  252. $low[$i] = chr(0);
  253. $high[$i] = chr(255);
  254. }
  255. $n = $bits % 8;
  256. if ($n) {
  257. $low[$i] = chr(ord($low[$i]) & ~((1 << $n) - 1));
  258. $high[$i] = chr(ord($high[$i]) | ((1 << $n) - 1));
  259. }
  260. return array($low, $high);
  261. }
  262. /**
  263. * Determines if an IP address is in a specified IP address range.
  264. *
  265. * An IPv4-mapped address should be range checked with an IPv4-mapped address range.
  266. *
  267. * @param string $ip IP address in network address format
  268. * @param array $ipRanges List of IP address ranges
  269. * @return bool True if in any of the specified IP address ranges; else false.
  270. */
  271. public static function isIpInRange($ip, $ipRanges)
  272. {
  273. $ipLen = strlen($ip);
  274. if (empty($ip) || empty($ipRanges) || ($ipLen != 4 && $ipLen != 16)) {
  275. return false;
  276. }
  277. foreach ($ipRanges as $range) {
  278. if (is_array($range)) {
  279. // already split into low/high IP addresses
  280. $range[0] = self::P2N($range[0]);
  281. $range[1] = self::P2N($range[1]);
  282. } else {
  283. // expect CIDR format but handle some variations
  284. $range = self::getIpsForRange($range);
  285. }
  286. if ($range === false) {
  287. continue;
  288. }
  289. $low = $range[0];
  290. $high = $range[1];
  291. if (strlen($low) != $ipLen) {
  292. continue;
  293. }
  294. // binary-safe string comparison
  295. if ($ip >= $low && $ip <= $high) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. /**
  302. * Returns the most accurate IP address availble for the current user, in
  303. * IPv4 format. This could be the proxy client's IP address.
  304. *
  305. * @return string IP address in presentation format.
  306. */
  307. public static function getIpFromHeader()
  308. {
  309. $clientHeaders = @Config::getInstance()->General['proxy_client_headers'];
  310. if (!is_array($clientHeaders)) {
  311. $clientHeaders = array();
  312. }
  313. $default = '0.0.0.0';
  314. if (isset($_SERVER['REMOTE_ADDR'])) {
  315. $default = $_SERVER['REMOTE_ADDR'];
  316. }
  317. $ipString = self::getNonProxyIpFromHeader($default, $clientHeaders);
  318. return self::sanitizeIp($ipString);
  319. }
  320. /**
  321. * Returns a non-proxy IP address from header.
  322. *
  323. * @param string $default Default value to return if there no matching proxy header.
  324. * @param array $proxyHeaders List of proxy headers.
  325. * @return string
  326. */
  327. public static function getNonProxyIpFromHeader($default, $proxyHeaders)
  328. {
  329. $proxyIps = array();
  330. $config = Config::getInstance()->General;
  331. if(isset($config['proxy_ips'])) {
  332. $proxyIps = $config['proxy_ips'];
  333. }
  334. if (!is_array($proxyIps)) {
  335. $proxyIps = array();
  336. }
  337. $proxyIps[] = $default;
  338. // examine proxy headers
  339. foreach ($proxyHeaders as $proxyHeader) {
  340. if (!empty($_SERVER[$proxyHeader])) {
  341. $proxyIp = self::getLastIpFromList($_SERVER[$proxyHeader], $proxyIps);
  342. if (strlen($proxyIp) && stripos($proxyIp, 'unknown') === false) {
  343. return $proxyIp;
  344. }
  345. }
  346. }
  347. return $default;
  348. }
  349. /**
  350. * Returns the last IP address in a comma separated list, subject to an optional exclusion list.
  351. *
  352. * @param string $csv Comma separated list of elements.
  353. * @param array $excludedIps Optional list of excluded IP addresses (or IP address ranges).
  354. * @return string Last (non-excluded) IP address in the list.
  355. */
  356. public static function getLastIpFromList($csv, $excludedIps = null)
  357. {
  358. $p = strrpos($csv, ',');
  359. if ($p !== false) {
  360. $elements = explode(',', $csv);
  361. for ($i = count($elements); $i--;) {
  362. $element = trim(Common::sanitizeInputValue($elements[$i]));
  363. if (empty($excludedIps) || (!in_array($element, $excludedIps) && !self::isIpInRange(self::P2N(self::sanitizeIp($element)), $excludedIps))) {
  364. return $element;
  365. }
  366. }
  367. }
  368. return trim(Common::sanitizeInputValue($csv));
  369. }
  370. /**
  371. * Retirms the hostname for a given IP address.
  372. *
  373. * @param string $ipStr Human-readable IP address.
  374. * @return string The hostname or unmodified $ipStr on failure.
  375. */
  376. public static function getHostByAddr($ipStr)
  377. {
  378. // PHP's reverse lookup supports ipv4 and ipv6
  379. // except on Windows before PHP 5.3
  380. $host = strtolower(@gethostbyaddr($ipStr));
  381. return $host === '' ? $ipStr : $host;
  382. }
  383. }