BxAnalyticsRequest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Analytics Analytics
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * What the HTTP request of a hit tells about the visitor: address, country, privacy signals.
  13. * Reads a $_SERVER-like array, so tests can pass their own.
  14. */
  15. class BxAnalyticsRequest
  16. {
  17. /**
  18. * Headers that carry the client address when a proxy or CDN sits in front of the site
  19. * and there is no X-Forwarded-For, checked in this order.
  20. */
  21. protected static $_aIpHeaders = [
  22. 'HTTP_CF_CONNECTING_IP',
  23. 'HTTP_TRUE_CLIENT_IP',
  24. 'HTTP_X_REAL_IP',
  25. ];
  26. /**
  27. * The shared address space of carrier-grade NAT, where cloud load balancers (Railway)
  28. * connect from; PHP doesn't count it as private.
  29. */
  30. protected static $_aInternalRanges = ['100.64.0.0/10'];
  31. /**
  32. * Location headers set by CDNs and hosting edges, per edge: country, region, city and
  33. * coordinates. With no edge chosen, the first one found is read, in this order. An edge
  34. * sets its own headers and passes the others through, so a client can add another CDN's.
  35. */
  36. protected static $_aGeoHeaders = [
  37. 'cloudflare' => [
  38. 'country' => ['HTTP_CF_IPCOUNTRY'],
  39. 'region' => [],
  40. 'city' => ['HTTP_CF_IPCITY'],
  41. 'coordinates' => [['HTTP_CF_IPLATITUDE', 'HTTP_CF_IPLONGITUDE']],
  42. ],
  43. 'vercel' => [
  44. 'country' => ['HTTP_X_VERCEL_IP_COUNTRY'],
  45. 'region' => ['HTTP_X_VERCEL_IP_COUNTRY_REGION'],
  46. 'city' => ['HTTP_X_VERCEL_IP_CITY'],
  47. 'coordinates' => [['HTTP_X_VERCEL_IP_LATITUDE', 'HTTP_X_VERCEL_IP_LONGITUDE']],
  48. ],
  49. 'cloudfront' => [
  50. 'country' => ['HTTP_CLOUDFRONT_VIEWER_COUNTRY'],
  51. 'region' => ['HTTP_CLOUDFRONT_VIEWER_COUNTRY_REGION'],
  52. 'city' => ['HTTP_CLOUDFRONT_VIEWER_CITY'],
  53. 'coordinates' => [['HTTP_CLOUDFRONT_VIEWER_LATITUDE', 'HTTP_CLOUDFRONT_VIEWER_LONGITUDE']],
  54. ],
  55. '' => [
  56. 'country' => ['HTTP_CF_IPCOUNTRY', 'HTTP_X_VERCEL_IP_COUNTRY', 'HTTP_CLOUDFRONT_VIEWER_COUNTRY', 'HTTP_X_COUNTRY_CODE', 'HTTP_X_APPENGINE_COUNTRY'],
  57. 'region' => ['HTTP_X_VERCEL_IP_COUNTRY_REGION'],
  58. 'city' => ['HTTP_X_VERCEL_IP_CITY', 'HTTP_CF_IPCITY'],
  59. 'coordinates' => [['HTTP_X_VERCEL_IP_LATITUDE', 'HTTP_X_VERCEL_IP_LONGITUDE'], ['HTTP_CF_IPLATITUDE', 'HTTP_CF_IPLONGITUDE']],
  60. ],
  61. ];
  62. /**
  63. * A visitor id kept by a client (persistent identity mode), and the cookie the classic
  64. * tracker keeps it in for site requests.
  65. */
  66. const VISITOR_ID_PATTERN = '/^[a-zA-Z0-9_-]{16,64}$/';
  67. const VISITOR_ID_COOKIE = 'bx_analytics_vid';
  68. protected $_aServer;
  69. public function __construct($aServer = null)
  70. {
  71. $this->_aServer = is_array($aServer) ? $aServer : $_SERVER;
  72. }
  73. /**
  74. * Client address. Proxy headers count only when the request comes from a trusted proxy:
  75. * a private, loopback or shared (100.64.0.0/10) address (load balancers, the NEO server in
  76. * the same network), an address from the trusted list, or a request the caller has
  77. * already authenticated (the NEO server's API key).
  78. *
  79. * X-Forwarded-For is read from the right, as core's visitor address is: each proxy
  80. * appends the address it got the request from, so trusted proxies are skipped and the
  81. * first address that isn't one is the client. Whatever is left of it came from the
  82. * client, which could otherwise pick any address for each request: past the rate limits
  83. * and the excluded addresses. CF-Connecting-IP, True-Client-IP and X-Real-IP count only
  84. * without X-Forwarded-For, since a proxy that doesn't set them passes the client's own.
  85. *
  86. * @param array $aTrustedProxies addresses and CIDR ranges whose proxy headers are trusted
  87. * @param bool $bTrustedRequest the request carries a valid API key
  88. * @return string an IP address, or '' when there is none
  89. */
  90. public function getIp($aTrustedProxies = [], $bTrustedRequest = false)
  91. {
  92. $sRemote = $this->_valid($this->_get('REMOTE_ADDR'));
  93. if($sRemote === '' && !$bTrustedRequest)
  94. return '';
  95. if(!$bTrustedRequest && !self::isTrustedProxy($sRemote, $aTrustedProxies))
  96. return $sRemote;
  97. $sForwarded = $this->_get('HTTP_X_FORWARDED_FOR');
  98. if($sForwarded !== '') {
  99. $sIp = $sRemote;
  100. foreach(array_reverse(explode(',', $sForwarded)) as $sCandidate) {
  101. if(trim($sCandidate) === '')
  102. continue;
  103. // a hop that didn't know the address ("unknown"): nothing left of it is reliable
  104. $sCandidate = $this->_valid($sCandidate);
  105. if($sCandidate === '')
  106. break;
  107. $sIp = $sCandidate;
  108. if(!self::isTrustedProxy($sCandidate, $aTrustedProxies))
  109. break;
  110. }
  111. return $sIp;
  112. }
  113. foreach(self::$_aIpHeaders as $sHeader) {
  114. $sIp = $this->_valid($this->_get($sHeader));
  115. if($sIp !== '')
  116. return $sIp;
  117. }
  118. return $sRemote;
  119. }
  120. /**
  121. * Whether the proxy headers of a request from an address are trusted: a private,
  122. * loopback or shared address, or one in the trusted list.
  123. */
  124. public static function isTrustedProxy($sIp, $aTrustedProxies = [])
  125. {
  126. return self::isPrivate($sIp) || self::inList($sIp, self::$_aInternalRanges) || self::inList($sIp, $aTrustedProxies);
  127. }
  128. /**
  129. * The address the request came from, before any proxy header is read, or ''.
  130. */
  131. public function getRemoteAddr()
  132. {
  133. return $this->_valid($this->_get('REMOTE_ADDR'));
  134. }
  135. /**
  136. * ISO 3166-1 alpha-2 country from CDN headers, or ''. "XX" and "T1" (Tor) mean unknown.
  137. *
  138. * @param string $sEdge the CDN whose headers are read (cloudflare, vercel, cloudfront), '' for any
  139. */
  140. public function getCountry($sEdge = '')
  141. {
  142. $aCountry = $this->getCountryHeader($sEdge);
  143. return $aCountry ? $aCountry['country'] : '';
  144. }
  145. /**
  146. * The CDN header the country comes from and its value, or null when there is none.
  147. *
  148. * @param string $sEdge as for getCountry()
  149. * @return array|null header (as in $_SERVER) and country
  150. */
  151. public function getCountryHeader($sEdge = '')
  152. {
  153. foreach(self::_getGeoHeaders($sEdge, 'country') as $sHeader) {
  154. $sValue = mb_strtoupper(trim($this->_get($sHeader)));
  155. if(preg_match('/^[A-Z]{2}$/', $sValue) && !in_array($sValue, ['XX', 'T1', 'ZZ'], true))
  156. return ['header' => $sHeader, 'country' => $sValue];
  157. }
  158. return null;
  159. }
  160. /**
  161. * Region and city where the edge provides them, URL-decoded, or ''.
  162. *
  163. * @param string $sEdge as for getCountry()
  164. */
  165. public function getRegion($sEdge = '')
  166. {
  167. return $this->_clean(rawurldecode($this->_getFirst(self::_getGeoHeaders($sEdge, 'region'))), 64);
  168. }
  169. public function getCity($sEdge = '')
  170. {
  171. return $this->_clean(rawurldecode($this->_getFirst(self::_getGeoHeaders($sEdge, 'city'))), 64);
  172. }
  173. /**
  174. * Visitor coordinates from the edge (Vercel, CloudFront, or Cloudflare's visitor location
  175. * headers), rounded to one decimal (about 10 km), or null.
  176. *
  177. * @param string $sEdge as for getCountry()
  178. * @return array|null [lat, lng]
  179. */
  180. public function getCoordinates($sEdge = '')
  181. {
  182. foreach(self::_getGeoHeaders($sEdge, 'coordinates') as list($sLat, $sLng)) {
  183. $sLatValue = $this->_get($sLat);
  184. $sLngValue = $this->_get($sLng);
  185. if(!is_numeric($sLatValue) || !is_numeric($sLngValue))
  186. continue;
  187. $fLat = (float)$sLatValue;
  188. $fLng = (float)$sLngValue;
  189. if($fLat < -90 || $fLat > 90 || $fLng < -180 || $fLng > 180)
  190. continue;
  191. return [round($fLat, 1), round($fLng, 1)];
  192. }
  193. return null;
  194. }
  195. /**
  196. * The client of the current request, for actions recorded on the server.
  197. * - no request (cron, background jobs, command line): core
  198. * - a page or form of the classic UI: core-web
  199. * - NEO says which client it is in X-Neo-Platform (web, ios, android)
  200. * - otherwise the NEO web server is the request with the API key, and the native apps
  201. * are told apart by their HTTP stack (CFNetwork on iOS, okhttp on Android)
  202. * - any other API client: api
  203. *
  204. * @param bool $bApi the request came through api.php
  205. * @param bool $bApiKey it carries an API key api.php accepted
  206. */
  207. public function getPlatform($bApi, $bApiKey = false)
  208. {
  209. // cron and the command line have no request method
  210. if($this->_get('REQUEST_METHOD') === '')
  211. return BX_ANALYTICS_PLATFORM_CORE;
  212. if(!$bApi)
  213. return BX_ANALYTICS_PLATFORM_CORE_WEB;
  214. $aNeo = [
  215. 'web' => BX_ANALYTICS_PLATFORM_NEXT_WEB,
  216. 'ios' => BX_ANALYTICS_PLATFORM_RN_IOS,
  217. 'android' => BX_ANALYTICS_PLATFORM_RN_ANDROID,
  218. ];
  219. $sNeo = mb_strtolower($this->_get('HTTP_X_NEO_PLATFORM'));
  220. if(isset($aNeo[$sNeo]))
  221. return $aNeo[$sNeo];
  222. if($bApiKey)
  223. return BX_ANALYTICS_PLATFORM_NEXT_WEB;
  224. $sUserAgent = $this->_get('HTTP_USER_AGENT');
  225. if($this->_get('HTTP_ORIGIN') !== '') {
  226. if(preg_match('/CFNetwork|Darwin/i', $sUserAgent))
  227. return BX_ANALYTICS_PLATFORM_RN_IOS;
  228. if(preg_match('/okhttp|Dalvik/i', $sUserAgent))
  229. return BX_ANALYTICS_PLATFORM_RN_ANDROID;
  230. }
  231. return BX_ANALYTICS_PLATFORM_API;
  232. }
  233. /**
  234. * The visitor id the client keeps (persistent identity mode), on a site request rather
  235. * than in a tracker payload: the classic tracker's `bx_analytics_vid` cookie, or NEO's
  236. * X-Analytics-Vid header. '' when there is none or it isn't one.
  237. */
  238. public function getVisitorId()
  239. {
  240. $sId = $this->_get('HTTP_X_ANALYTICS_VID');
  241. if($sId === '')
  242. $sId = $this->_getCookie(self::VISITOR_ID_COOKIE);
  243. return self::isVisitorId($sId) ? $sId : '';
  244. }
  245. /**
  246. * Whether a string has the form of a client's visitor id.
  247. */
  248. public static function isVisitorId($s)
  249. {
  250. return is_string($s) && preg_match(self::VISITOR_ID_PATTERN, $s) === 1;
  251. }
  252. public function getUserAgent()
  253. {
  254. return $this->_clean($this->_get('HTTP_USER_AGENT'), 512);
  255. }
  256. public function getLanguage()
  257. {
  258. $sValue = $this->_get('HTTP_ACCEPT_LANGUAGE');
  259. if(!preg_match('/^\s*([a-zA-Z]{2,3}(?:-[a-zA-Z0-9]{2,4})?)/', $sValue, $aMatch))
  260. return '';
  261. return $aMatch[1];
  262. }
  263. /**
  264. * The browser asks not to be tracked: Do Not Track or Global Privacy Control.
  265. */
  266. public function isDoNotTrack()
  267. {
  268. return $this->_get('HTTP_DNT') === '1' || $this->_get('HTTP_SEC_GPC') === '1';
  269. }
  270. /**
  271. * The request comes from another site's page, which a same-origin tracker never does.
  272. * Browsers send Sec-Fetch-Site with fetch and sendBeacon; old ones send nothing, which passes.
  273. */
  274. public function isCrossSite()
  275. {
  276. $sSite = mb_strtolower($this->_get('HTTP_SEC_FETCH_SITE'));
  277. return $sSite === 'cross-site';
  278. }
  279. public static function isPrivate($sIp)
  280. {
  281. if(!filter_var($sIp, FILTER_VALIDATE_IP))
  282. return false;
  283. return filter_var($sIp, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false;
  284. }
  285. /**
  286. * Whether an address matches a list of addresses and CIDR ranges (IPv4 and IPv6).
  287. */
  288. public static function inList($sIp, $aList)
  289. {
  290. $sIpBin = @inet_pton($sIp);
  291. if($sIpBin === false)
  292. return false;
  293. foreach($aList as $sEntry) {
  294. $sEntry = trim($sEntry);
  295. if($sEntry === '')
  296. continue;
  297. $iBits = null;
  298. if(bx_mb_strpos($sEntry, '/') !== false)
  299. list($sEntry, $iBits) = explode('/', $sEntry, 2);
  300. $sNetBin = @inet_pton($sEntry);
  301. if($sNetBin === false || strlen($sNetBin) != strlen($sIpBin))
  302. continue;
  303. if($iBits === null || $iBits === '') {
  304. if($sNetBin === $sIpBin)
  305. return true;
  306. continue;
  307. }
  308. $iBits = (int)$iBits;
  309. $iBytes = intdiv($iBits, 8);
  310. if(substr($sNetBin, 0, $iBytes) !== substr($sIpBin, 0, $iBytes))
  311. continue;
  312. $iRest = $iBits % 8;
  313. if($iRest == 0)
  314. return true;
  315. $iMask = (0xFF << (8 - $iRest)) & 0xFF;
  316. if((ord($sNetBin[$iBytes]) & $iMask) === (ord($sIpBin[$iBytes]) & $iMask))
  317. return true;
  318. }
  319. return false;
  320. }
  321. protected function _get($sKey)
  322. {
  323. return isset($this->_aServer[$sKey]) && is_string($this->_aServer[$sKey]) ? trim($this->_aServer[$sKey]) : '';
  324. }
  325. /**
  326. * A cookie's value from the request's Cookie header, or ''.
  327. */
  328. protected function _getCookie($sName)
  329. {
  330. foreach(explode(';', $this->_get('HTTP_COOKIE')) as $sPair) {
  331. $aPair = explode('=', $sPair, 2);
  332. if(count($aPair) == 2 && trim($aPair[0]) === $sName)
  333. return trim(rawurldecode($aPair[1]));
  334. }
  335. return '';
  336. }
  337. /**
  338. * The first of some headers that isn't empty, or ''.
  339. */
  340. protected function _getFirst($aKeys)
  341. {
  342. foreach($aKeys as $sKey)
  343. if(($sValue = $this->_get($sKey)) !== '')
  344. return $sValue;
  345. return '';
  346. }
  347. /**
  348. * Location headers of an edge ('' for any) of a kind: country, region, city, coordinates.
  349. * An unknown edge has none.
  350. */
  351. protected static function _getGeoHeaders($sEdge, $sKind)
  352. {
  353. $sEdge = (string)$sEdge;
  354. return isset(self::$_aGeoHeaders[$sEdge][$sKind]) ? self::$_aGeoHeaders[$sEdge][$sKind] : [];
  355. }
  356. protected function _valid($sIp)
  357. {
  358. $sIp = trim((string)$sIp);
  359. // "1.2.3.4:5678" and "[::1]:5678" from some proxies
  360. if(preg_match('/^\[([0-9a-f:.]+)\](?::\d+)?$/i', $sIp, $aMatch))
  361. $sIp = $aMatch[1];
  362. elseif(preg_match('/^(\d{1,3}(?:\.\d{1,3}){3}):\d+$/', $sIp, $aMatch))
  363. $sIp = $aMatch[1];
  364. return filter_var($sIp, FILTER_VALIDATE_IP) ? $sIp : '';
  365. }
  366. protected function _clean($s, $iMaxLength)
  367. {
  368. $s = trim(preg_replace('/[\x00-\x1F\x7F]+/u', ' ', (string)$s));
  369. return get_mb_substr($s, 0, $iMaxLength);
  370. }
  371. }
  372. /** @} */