0.1.0 initial commit
This commit is contained in:
commit
a09ec7f5ed
9 changed files with 1016 additions and 0 deletions
328
src/Country.php
Normal file
328
src/Country.php
Normal file
|
|
@ -0,0 +1,328 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* (c) 2025 Sakuragasaki46
|
||||
*
|
||||
* This is an interface to the IP2Location Lite country IP database, CC BY-SA licensed
|
||||
* and accessible free of charge at <https://lite.ip2location.com> (registration required).
|
||||
* IP2Location Lite® is a registered trademark of Hexasoft Development Sdn Bhd.
|
||||
*/
|
||||
|
||||
namespace Yusurko\UserAgent;
|
||||
|
||||
use Yusurko\UserAgent\RegionBlock;
|
||||
|
||||
class Country {
|
||||
const NAMES = [
|
||||
'@' => 'Headspace',
|
||||
'+' => 'Inner Space',
|
||||
'-' => 'Outer Space',
|
||||
'AC' => 'Ascension (UK)',
|
||||
'AD' => 'Andorra',
|
||||
'AE' => 'United Arab Emirates',
|
||||
'AF' => 'Afghanistan',
|
||||
'AG' => 'Antigua & Barbuda',
|
||||
'AI' => 'Anguilla (UK)',
|
||||
'AL' => 'Albania',
|
||||
'AM' => 'Armenia',
|
||||
'AO' => 'Angola',
|
||||
'AQ' => 'Antarctica',
|
||||
'AR' => 'Argentina',
|
||||
'AS' => 'American Samoa',
|
||||
'AT' => 'Austria',
|
||||
'AU' => 'Australia',
|
||||
'AW' => 'Aruba (Netherlands)',
|
||||
'AX' => 'Åland (Finland)',
|
||||
'AZ' => 'Azerbaijan',
|
||||
'BA' => 'Bosnia & Herzegovina',
|
||||
'BB' => 'Barbados',
|
||||
'BD' => 'Bangladesh',
|
||||
'BE' => 'Belgium',
|
||||
'BF' => 'Burkina Faso',
|
||||
'BG' => 'Bulgaria',
|
||||
'BH' => 'Bahrain',
|
||||
'BI' => 'Burundi',
|
||||
'BJ' => 'Benin',
|
||||
'BL' => 'Saint Barthélemy (France)',
|
||||
'BM' => 'Bermuda (UK)',
|
||||
'BN' => 'Brunei',
|
||||
'BO' => 'Bolivia',
|
||||
'BQ' => 'Bonaire (Netherlands)',
|
||||
'BR' => 'Brazil',
|
||||
'BS' => 'Bahamas',
|
||||
'BT' => 'Bhutan',
|
||||
'BV' => 'Bouvet Island (Norway)',
|
||||
'BW' => 'Botswana',
|
||||
'BY' => 'Belarus',
|
||||
'BZ' => 'Belize',
|
||||
'CA' => 'Canada',
|
||||
'CC' => 'Cocos (Keeling) Islands (Australia)',
|
||||
'CD' => 'Democratic Republic of the Congo',
|
||||
'CF' => 'Central African Republic',
|
||||
'CG' => 'Republic of the Congo',
|
||||
'CH' => 'Switzerland',
|
||||
'CI' => 'Côte d\'Ivoire',
|
||||
'CK' => 'Cook Islands (UK)',
|
||||
'CL' => 'Chile',
|
||||
'CM' => 'Cameroon',
|
||||
'CN' => 'People\'s Republic of China',
|
||||
'CO' => 'Colombia',
|
||||
'CP' => 'France',
|
||||
'CQ' => 'Sark (UK)',
|
||||
'CR' => 'Costa Rica',
|
||||
'CU' => 'Cuba',
|
||||
'CV' => 'Cape Verde',
|
||||
'CW' => 'Curaçao (Netherlands)',
|
||||
'CX' => 'Christmas Island (Australia)',
|
||||
'CY' => 'Cyprus',
|
||||
'CZ' => 'Czechia',
|
||||
'DE' => 'Germany',
|
||||
'DG' => 'Chagos (Mauritius)',
|
||||
'DJ' => 'Djibouti',
|
||||
'DK' => 'Denmark',
|
||||
'DM' => 'Dominica',
|
||||
'DO' => 'Dominican Republic',
|
||||
'DZ' => 'Algeria',
|
||||
'EA' => 'Spain',
|
||||
'EC' => 'Ecuador',
|
||||
'EE' => 'Estonia',
|
||||
'EG' => 'Egypt',
|
||||
'EH' => 'Western Sahara',
|
||||
'ER' => 'Eritrea',
|
||||
'ES' => 'Spain',
|
||||
'ET' => 'Ethiopia',
|
||||
'EU' => 'European Union',
|
||||
'EZ' => 'Eurozone',
|
||||
'FI' => 'Finland',
|
||||
'FJ' => 'Fiji',
|
||||
'FK' => 'Falkland Islands (UK)',
|
||||
'FM' => 'Federated States of Micronesia',
|
||||
'FO' => 'Faroe Islands (Denmark)',
|
||||
'FR' => 'France',
|
||||
'FX' => 'France',
|
||||
'GA' => 'Gabon',
|
||||
'GB' => 'United Kingdom (wrong)',
|
||||
'GD' => 'Grenada',
|
||||
'GE' => 'Georgia',
|
||||
'GF' => 'French Guyane',
|
||||
'GG' => 'Guernsey (UK)',
|
||||
'GH' => 'Ghana',
|
||||
'GI' => 'Gibraltar (UK)',
|
||||
'GL' => 'Greenland (Denmark)',
|
||||
'GM' => 'Gambia',
|
||||
'GN' => 'Guinea',
|
||||
'GP' => 'Guadeloupe (France)',
|
||||
'GQ' => 'Equatorial Guinea',
|
||||
'GR' => 'Greece',
|
||||
'GS' => 'South Georgia & South Sandwich Islands (UK)',
|
||||
'GT' => 'Guatemala',
|
||||
'GU' => 'Guam (USA)',
|
||||
'GW' => 'Guinea-Bissau',
|
||||
'GY' => 'Guyana',
|
||||
'HK' => 'Hong Kong (PRC)',
|
||||
'HM' => 'Heard & McDonald Islands (Australia)',
|
||||
'HN' => 'Honduras',
|
||||
'HR' => 'Croatia',
|
||||
'HT' => 'Haïti',
|
||||
'HU' => 'Hungary',
|
||||
'IC' => 'Canary Islands (Spain)',
|
||||
'ID' => 'Indonesia',
|
||||
'IE' => 'Ireland',
|
||||
'IL' => 'Israel',
|
||||
'IM' => 'Isle of Man (UK)',
|
||||
'IN' => 'India (Bharat)',
|
||||
'IO' => 'Chagos (Mauritius)',
|
||||
'IQ' => 'Iraq',
|
||||
'IR' => 'Iran',
|
||||
'IS' => 'Iceland',
|
||||
'IT' => 'Italy',
|
||||
'JE' => 'Jersey (UK)',
|
||||
'JM' => 'Jamaica',
|
||||
'JO' => 'Jordan',
|
||||
'JP' => 'Japan',
|
||||
'KE' => 'Kenya',
|
||||
'KG' => 'Kyrgyzstan',
|
||||
'KH' => 'Cambodia',
|
||||
'KI' => 'Kiribati',
|
||||
'KM' => 'Comoros',
|
||||
'KN' => 'Saint Kitts & Nevis',
|
||||
'KP' => 'Democratic People\'s Republic of Korea',
|
||||
'KR' => 'Republic of Korea',
|
||||
'KW' => 'Kuwait',
|
||||
'KY' => 'Cayman Islands (UK)',
|
||||
'KZ' => 'Kazakhstan',
|
||||
'LA' => 'Laos',
|
||||
'LB' => 'Lebanon',
|
||||
'LC' => 'Saint Lucia',
|
||||
'LI' => 'Liechtenstein',
|
||||
'LK' => 'Sri Lanka',
|
||||
'LR' => 'Liberia',
|
||||
'LS' => 'Lesotho',
|
||||
'LT' => 'Lithuania',
|
||||
'LU' => 'Luxembourg',
|
||||
'LV' => 'Latvia',
|
||||
'LY' => 'Libya',
|
||||
'MA' => 'Morocco',
|
||||
'MC' => 'Monaco',
|
||||
'MD' => 'Moldova',
|
||||
'ME' => 'Montenegro',
|
||||
'MF' => 'Saint Martin (France)',
|
||||
'MG' => 'Madagascar',
|
||||
'MH' => 'Marshall Islands',
|
||||
'MK' => 'North Macedonia',
|
||||
'ML' => 'Mali',
|
||||
'MM' => 'Myanmar',
|
||||
'MN' => 'Mongolia',
|
||||
'MO' => 'Macao (PRC)',
|
||||
'MP' => 'Northern Mariana Islands (USA)',
|
||||
'MQ' => 'Martinique (France)',
|
||||
'MR' => 'Mauritania',
|
||||
'MS' => 'Montserrat (United Kingdom)',
|
||||
'MT' => 'Malta',
|
||||
'MU' => 'Mauritius',
|
||||
'MV' => 'Maldives',
|
||||
'MW' => 'Malawi',
|
||||
'MX' => 'Mexico',
|
||||
'MY' => 'Malaysia',
|
||||
'MZ' => 'Mozambique',
|
||||
'NA' => 'Namibia',
|
||||
'NC' => 'New Caledonia (France)',
|
||||
'NE' => 'Niger',
|
||||
'NF' => 'Norfolk Island (Australia)',
|
||||
'NG' => 'Nigeria',
|
||||
'NI' => 'Nicaragua',
|
||||
'NL' => 'Netherlands',
|
||||
'NO' => 'Norway',
|
||||
'NP' => 'Nepal',
|
||||
'NR' => 'Nauru',
|
||||
'NU' => 'Niue (NZ)',
|
||||
'NZ' => 'New Zealand',
|
||||
'OM' => 'Oman',
|
||||
'PA' => 'Panama',
|
||||
'PE' => 'Peru',
|
||||
'PF' => 'French Polynesia',
|
||||
'PG' => 'Papua New Guinea',
|
||||
'PH' => 'Philippines',
|
||||
'PK' => 'Pakistan',
|
||||
'PL' => 'Poland',
|
||||
'PM' => 'Saint Pierre & Miquelon (France)',
|
||||
'PN' => 'Pitcairn (UK)',
|
||||
'PR' => 'Puerto Rico (USA)',
|
||||
'PS' => 'Palestinian Authority',
|
||||
'PT' => 'Portugal',
|
||||
'PW' => 'Palau',
|
||||
'PY' => 'Paraguay',
|
||||
'QA' => 'Qatar',
|
||||
'QM' => 'Mars',
|
||||
'RE' => 'Réunion (France)',
|
||||
'RO' => 'Romania',
|
||||
'RS' => 'Serbia',
|
||||
'RU' => 'Russian Federation',
|
||||
'RW' => 'Rwanda',
|
||||
'SA' => 'Saudi Arabia',
|
||||
'SB' => 'Solomon Islands',
|
||||
'SC' => 'Seychelles',
|
||||
'SD' => 'Sudan',
|
||||
'SE' => 'Sweden',
|
||||
'SG' => 'Singapore',
|
||||
'SH' => 'Saint Helena (UK)',
|
||||
'SI' => 'Slovenia',
|
||||
'SJ' => 'Svalbard & Jan Mayen (Norway)',
|
||||
'SK' => 'Slovakia',
|
||||
'SL' => 'Sierra Leone',
|
||||
'SM' => 'San Marino',
|
||||
'SN' => 'Senegal',
|
||||
'SO' => 'Somalia',
|
||||
'SR' => 'Suriname',
|
||||
'SS' => 'South Sudan',
|
||||
'ST' => 'Sao Tome & Principe',
|
||||
'SU' => 'Soviet Union',
|
||||
'SV' => 'El Salvador',
|
||||
'SX' => 'Sint Maarten (Netherlands)',
|
||||
'SY' => 'Syria',
|
||||
'SZ' => 'eSwatini',
|
||||
'TA' => 'Tristan da Cunha (UK)',
|
||||
'TC' => 'Turks and Caicos Islands (UK)',
|
||||
'TD' => 'Chad',
|
||||
'TF' => 'French Southern Territories',
|
||||
'TG' => 'Togo',
|
||||
'TH' => 'Thailand',
|
||||
'TK' => 'Tokelau',
|
||||
'TL' => 'Timor-Leste',
|
||||
'TM' => 'Turkmenistan',
|
||||
'TN' => 'Tunisia',
|
||||
'TO' => 'Tonga',
|
||||
'TR' => 'Türkiye',
|
||||
'TT' => 'Trinidad & Tobago',
|
||||
'TV' => 'Tuvalu',
|
||||
'TW' => 'Republic of China (Taiwan)',
|
||||
'TZ' => 'Tanzania',
|
||||
'UA' => 'Ukraine',
|
||||
'UG' => 'Uganda',
|
||||
'UK' => 'United Kingdom',
|
||||
'UM' => 'USA Minor Outlying Islands',
|
||||
'UN' => 'United Nations',
|
||||
'US' => 'United States of America',
|
||||
'UY' => 'Uruguay',
|
||||
'UZ' => 'Uzbekistan',
|
||||
'VA' => 'Vatican City',
|
||||
'VC' => 'Saint Vincent & Grenadines',
|
||||
'VE' => 'Venezuela',
|
||||
'VG' => 'British Virgin Islands',
|
||||
'VI' => 'US Virgin Islands',
|
||||
'VN' => 'Vietnam',
|
||||
'VU' => 'Vanuatu',
|
||||
'WF' => 'Wallis & Futuna (France)',
|
||||
'WS' => 'Samoa',
|
||||
'XK' => 'Kosovo',
|
||||
'YE' => 'Yemen',
|
||||
'YT' => 'Mayotte (France)',
|
||||
'ZA' => 'South Africa',
|
||||
'ZM' => 'Zambia',
|
||||
'ZW' => 'Zimbabwe'
|
||||
];
|
||||
|
||||
const EUROPE = array (
|
||||
'AD', 'AL', 'AT', 'AX', 'BA', 'BE', 'BG', 'CH',
|
||||
'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FO',
|
||||
'FR', 'GE', 'GF', 'GG', 'GI', 'GL', 'GR',
|
||||
'HR', 'HU', 'IE', 'IM', 'IS', 'IT', 'LI', 'LT',
|
||||
'LU', 'LV', 'MC', 'MD', 'ME', 'MK', 'MQ', 'MT',
|
||||
'NL', 'NO', 'PL', 'PT', 'RO', 'SE', 'SI', 'SJ',
|
||||
'SK', 'SM', 'VA', '@', '+'
|
||||
);
|
||||
|
||||
protected $cc;
|
||||
|
||||
public function __construct ($cc) {
|
||||
$this->cc = $cc;
|
||||
}
|
||||
|
||||
public function getCode () {
|
||||
return $this->cc;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return self::NAMES[$this->cc] ?? $this->cc;
|
||||
}
|
||||
|
||||
public function isEurope() {
|
||||
return in_array($this->cc, self::EUROPE, true);
|
||||
}
|
||||
|
||||
public function isUS() {
|
||||
return in_array($this->cc, [ 'US', 'UM', 'QM' ], true);
|
||||
}
|
||||
|
||||
public function isBlocked() {
|
||||
return (new RegionBlock($this->cc))->isBlocked();
|
||||
}
|
||||
|
||||
public function isInnerSpace() {
|
||||
return in_array($this->cc, [ '@', '+' ], true);
|
||||
}
|
||||
|
||||
public static function outerSpace() {
|
||||
return new self("-");
|
||||
}
|
||||
}
|
||||
53
src/IpAddress.php
Normal file
53
src/IpAddress.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* (c) 2025 Sakuragasaki46
|
||||
*
|
||||
* This is an interface to the IP2Location Lite country IP database, CC BY-SA licensed
|
||||
* and accessible free of charge at <https://lite.ip2location.com> (registration required).
|
||||
* IP2Location Lite® is a registered trademark of Hexasoft Development Sdn Bhd.
|
||||
*/
|
||||
|
||||
namespace Yusurko\UserAgent;
|
||||
|
||||
use phpseclib3\Math\BigInteger;
|
||||
|
||||
class IpAddress extends BigInteger {
|
||||
public function __construct ($addr) {
|
||||
if (strpos($addr, ':') !== false) {
|
||||
// IPv6 //
|
||||
$dc = strpos($addr, '::');
|
||||
if ($dc === false) {
|
||||
$addrParts = explode(':', $addr);
|
||||
} else {
|
||||
[ $addr1, $addr2 ] = explode('::', $addr, 2);
|
||||
$addr1 = explode(':', $addr1);
|
||||
$addr2 = explode(':', $addr2);
|
||||
$addrParts = array_merge($addr1, array_fill(count($addr1), 8 - count($addr1) - count($addr2), '0'), $addr2);
|
||||
}
|
||||
|
||||
$hexAddr = '';
|
||||
foreach ($addrParts as $p) {
|
||||
$hexAddr .= str_pad($p, 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
//assert strlen($hexAddr) === 32;
|
||||
parent::__construct($hexAddr, 16);
|
||||
} elseif (strpos($addr, '.') !== false) {
|
||||
// IPv4 //
|
||||
$addrParts = explode('.', $addr);
|
||||
|
||||
parent::__construct((65535 << 32) | ($addrParts[0] << 24) | ($addrParts[1] << 16) | ($addrParts[2] << 8) | ($addrParts[3]));
|
||||
} else {
|
||||
// big-ass number //
|
||||
parent::__construct($addr);
|
||||
}
|
||||
}
|
||||
|
||||
public function toFullHex () {
|
||||
return str_pad($this->toHex(), 32, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
static public function fromHex($hs) {
|
||||
return new IpAddress((new BigInteger($hs, 16))->toString());
|
||||
}
|
||||
}
|
||||
115
src/IpLocator.php
Normal file
115
src/IpLocator.php
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
/**
|
||||
* (c) 2025 Sakuragasaki46
|
||||
*
|
||||
* This is an interface to the IP2Location Lite country IP database, CC BY-SA licensed
|
||||
* and accessible free of charge at <https://lite.ip2location.com> (registration required).
|
||||
* IP2Location Lite® is a registered trademark of Hexasoft Development Sdn Bhd.
|
||||
*/
|
||||
|
||||
namespace Yusurko\UserAgent;
|
||||
|
||||
|
||||
use Yusurko\UserAgent\IpAddress;
|
||||
|
||||
|
||||
class IpLocator {
|
||||
protected $conn, $source_file;
|
||||
|
||||
const SOURCE_FILE = '.err/data/IP2LOCATION-LITE-DB1.IPV6.CSV';
|
||||
|
||||
const LOCAL_RANGES = [
|
||||
["0", "1", '@'],
|
||||
["281470681743360", "281470698520575", '+'],
|
||||
["281470849515520", "281470866292735", "+"],
|
||||
["281472812449792", "281472829227007", "@"],
|
||||
["281473533739008", "281473533804543", "+"],
|
||||
["281473568473088", "281473569521663", "+"],
|
||||
["281473913978880", "281473914044415", "+"],
|
||||
["338288524927261089654018896841347694592", "338288524927261089672465640915057246207", "+"]
|
||||
];
|
||||
|
||||
public function __construct($conn, $source_file = self::SOURCE_FILE) {
|
||||
$this->conn = $conn;
|
||||
$this->source_file = $source_file;
|
||||
}
|
||||
|
||||
public function locate($addr){
|
||||
$addr = new IpAddress($addr);
|
||||
|
||||
if (!$this->isDbLoaded()) {
|
||||
$up_to_range = $this->getDbLoadedUpto();
|
||||
$this->loadCsvIntoDb($up_to_range);
|
||||
}
|
||||
|
||||
$country = $this->lookupDb($addr);
|
||||
|
||||
return new Country($country);
|
||||
}
|
||||
|
||||
private function isDbLoaded() {
|
||||
$ex = $this->conn->prepare("SELECT 1 FROM af_ip2location WHERE range_end = x'ffffffffffffffffffffffffffffffff'");
|
||||
$ex->execute();
|
||||
$res = $ex->get_result();
|
||||
|
||||
return ($res->num_rows > 0);
|
||||
}
|
||||
|
||||
private function getDbLoadedUpto(){
|
||||
$ex = $this->conn->prepare("SELECT hex(range_end) FROM af_ip2location ORDER BY id DESC LIMIT 1");
|
||||
$ex->execute();
|
||||
$ex->store_result();
|
||||
$ex->bind_result($range_end);
|
||||
$ex->fetch();
|
||||
|
||||
return IpAddress::fromHex($range_end);
|
||||
}
|
||||
|
||||
private function loadCsvIntoDb($start = false) {
|
||||
|
||||
$f = fopen($this->source_file, 'r');
|
||||
|
||||
while (($line = fgetcsv($f)) !== false) {
|
||||
$rs = new IpAddress($line[0]);
|
||||
$re = new IpAddress($line[1]);
|
||||
if ($rs->compare($start) <= 0) continue;
|
||||
try {
|
||||
$this->storeRangeIntoDb($rs, $re, $line[2]);
|
||||
} catch (\mysqli_sql_exception $ee) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function storeRangeIntoDb($range_start, $range_end, $country) {
|
||||
$range_start_x = $range_start->toFullHex();
|
||||
$range_end_x = $range_end->toFullHex();
|
||||
$ins = $this->conn->prepare('INSERT INTO af_ip2location (range_start, range_end, country) VALUES (unhex(?), unhex(?), ?)');
|
||||
$ins->bind_param('sss', $range_start_x, $range_end_x, $country);
|
||||
$ins->execute();
|
||||
$ins->store_result();
|
||||
}
|
||||
|
||||
private function lookupDb($addr) {
|
||||
// look up forbidden ranges first
|
||||
foreach (self::LOCAL_RANGES as $rang) {
|
||||
[ $rs, $re, $co ] = $rang;
|
||||
if ($addr->compare(new IpAddress($rs)) >= 0 && $addr->compare(new IpAddress($re)) <= 0) {
|
||||
return $co;
|
||||
}
|
||||
}
|
||||
|
||||
$addr_x = $addr->toFullHex();
|
||||
$st = $this->conn->prepare('SELECT country FROM af_ip2location WHERE range_end >= unhex(?) AND range_start <= unhex(?)');
|
||||
$st->bind_param('ss', $addr_x, $addr_x);
|
||||
$st->execute();
|
||||
$res = $st->get_result();
|
||||
if ($res->num_rows < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $res->fetch_assoc()['country'];
|
||||
}
|
||||
}
|
||||
47
src/MiniMD.php
Normal file
47
src/MiniMD.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Implements a subset of the Markdown formatting.
|
||||
*
|
||||
* (c) 2024-2025 Sakuragasaki46
|
||||
*/
|
||||
|
||||
namespace Yusurko\UserAgent;
|
||||
|
||||
class MiniMD {
|
||||
protected $inline;
|
||||
|
||||
/**
|
||||
* new MiniMD(false); => block MiniMD (wrap in <p> tags)
|
||||
* new MiniMD(true); => inline MiniMD (do not wrap in <p> tags)
|
||||
*/
|
||||
public function __construct($inline) {
|
||||
$this->inline = $inline;
|
||||
}
|
||||
|
||||
public function transform($text) {
|
||||
$text = preg_replace_callback('/\\\([!-\/:-@\[-`\{-~])/', function ($x) { return "\x05" . str_pad(dechex(ord($x[1])), 2, '0'); }, $text);
|
||||
$text = htmlspecialchars($text);
|
||||
$text = preg_replace('/\B\*\*(.*?)\*\*\B/u', '<b>\1</b>', $text);
|
||||
$text = preg_replace('/\B\*(.*?)\*\B/u', '<i>\1</i>', $text);
|
||||
$text = preg_replace('/\b__(.*?)__\b/u', '<u>\1</u>', $text);
|
||||
$text = preg_replace('/\B~~(.*?)~~\B/u', '<del>\1</del>', $text);
|
||||
$text = preg_replace('/\[(.*?)\]\((\/(?:[^\/\)].*?)?)\)/u', '<a href="\2">\1</a>', $text);
|
||||
$text = preg_replace('/<(https?:\/\/[^\s]+?)>/u', '<a href="\1">\1</a>', $text);
|
||||
$text = preg_replace('/\[(.*?)\]\((.*?)\)/u', '<a href="\2" rel="nofollow" target="_blank">\1</a>', $text);
|
||||
if ($this->inline) {
|
||||
$text = preg_replace_callback('/\x05([0-7][0-9a-f])/u', function ($x) { return htmlspecialchars(chr(hexdec($x[1]))); }, $text);
|
||||
return $text;
|
||||
}
|
||||
// from now on, block only
|
||||
$text = preg_replace('/ >> (.+)/u', '<blockquote>\1</blockquote>', $text);
|
||||
$text = preg_replace('/^\* (.*)$/mu', '<ul><li>\1</li></ul>', $text);
|
||||
$text = str_replace("</ul>\n<ul>", "\n", $text);
|
||||
$text = str_replace("\n\n", "</p><p>", $text);
|
||||
$text = preg_replace_callback('/\x05([0-7][0-9a-f])/u', function ($x) { return htmlspecialchars(chr(hexdec($x[1]))); }, $text);
|
||||
return '<p>' . $text . '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
78
src/RegionBlock.php
Normal file
78
src/RegionBlock.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Yusurko\UserAgent;
|
||||
|
||||
use Yusurko\UserAgent\Country;
|
||||
use Yusurko\UserAgent\MiniMD;
|
||||
|
||||
class RegionBlock {
|
||||
// no more lifted for the general public
|
||||
const BLOCKED_REGIONS = array(
|
||||
'US' => array(
|
||||
'reason' => 'recent legislation changes, namely the **Kids Online Safety Act** (KOSA), the **TAKE IT DOWN Act** (S. 146/H.R.633), the **SCREEN ACT**, the prosecution of LGBTQ+ people, and countless similar censorship bills, Presidential executive orders and high percentage of retards across the population',
|
||||
'min_age' => 18,
|
||||
'tags' => ['lgbt', 'xxx']
|
||||
),
|
||||
'IL' => array(
|
||||
'reason' => 'the ongoing disregard for human lives by your government. __Seriously, stop it__',
|
||||
'min_age' => 18,
|
||||
'tags' => ['lgbt', 'xxx']
|
||||
),
|
||||
'UK' => array(
|
||||
'reason' => 'the Online Safety Act',
|
||||
'min_age' => 18,
|
||||
'tags' => ['lgbt', 'xxx']
|
||||
),
|
||||
'GB' => array(
|
||||
'reason' => 'the Online Safety Act',
|
||||
'min_age' => 18,
|
||||
'tags' => ['lgbt', 'xxx']
|
||||
),
|
||||
);
|
||||
|
||||
protected string $country;
|
||||
|
||||
public function __construct($country) {
|
||||
$this->country = $country;
|
||||
}
|
||||
|
||||
public function isImmune() {
|
||||
return in_array($this->country, [ '@', '+' ], true);
|
||||
}
|
||||
|
||||
public function isBlocked() {
|
||||
return !$this->isImmune() && isset(self::BLOCKED_REGIONS[$this->country]);
|
||||
}
|
||||
|
||||
public function emitHtml() {
|
||||
$country = new Country($this->country);
|
||||
$country_name = $country->getName();
|
||||
$min_age = self::BLOCKED_REGIONS[$this->country]['min_age'] ?? 18;
|
||||
$reason = self::BLOCKED_REGIONS[$this->country]['reason'] ?? 'local law restrictions';
|
||||
$md = new MiniMD(true);
|
||||
|
||||
http_response_code(451);
|
||||
?>
|
||||
<article>
|
||||
<h1>Unavailable for Legal Reasons</h1>
|
||||
<p>This website is blocked in your country (<strong><?php echo htmlspecialchars($country_name); ?></strong>) pursuant to <?php
|
||||
echo $md->transform($reason); ?>.</p>
|
||||
<p>If you are over <?php echo htmlspecialchars($min_age); ?>, <a href="https://passport.yusur.moe/">log in</a> to access this website.</p>
|
||||
<p><a href="javascript:history.go(-1);">Go back</a></p>
|
||||
</article>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function emitText() {
|
||||
$country = new Country($this->country);
|
||||
$country_name = $country->getName();
|
||||
$min_age = self::BLOCKED_REGIONS[$this->country]['min_age'] ?? 18;
|
||||
$reason = self::BLOCKED_REGIONS[$this->country]['reason'] ?? 'local law restrictions';
|
||||
|
||||
echo <<< EOF
|
||||
This website is blocked in your country ($country_name) pursuant to $reason.
|
||||
|
||||
If you are over 18, log in at https://passport.yusur.moe/ to access this website.
|
||||
EOF;
|
||||
}
|
||||
}
|
||||
294
src/UserAgent.php
Normal file
294
src/UserAgent.php
Normal file
|
|
@ -0,0 +1,294 @@
|
|||
<?php
|
||||
|
||||
namespace Yusurko\UserAgent;
|
||||
|
||||
//use Sakuragasaki46\Datasec\DatabaseConnection;
|
||||
use Yusurko\UserAgent\IpLocator;
|
||||
use Yusurko\UserAgent\RegionBlock;
|
||||
use Yusurko\UserAgent\Country;
|
||||
|
||||
class UserAgent {
|
||||
protected string $ua, $accept, $addr, $prefLang;
|
||||
protected $locator = null, $country = null;
|
||||
static private UserAgent $instance;
|
||||
protected $langs;
|
||||
protected $conn;
|
||||
|
||||
const LOCAL_ADDRESSES = array(
|
||||
"", "::", "::1", "127.0.0.1"
|
||||
);
|
||||
const TRACKING_PARAMS = array(
|
||||
"fbclid", "nigger"
|
||||
);
|
||||
const BAD_STRINGS = array(
|
||||
'/^Go-http-client\//u',
|
||||
'/GPTBot/iu',
|
||||
'/iubenda-radar\//iu'
|
||||
);
|
||||
const TRUSTED_STRINGS = array(
|
||||
// '/ia_archiver/iu'
|
||||
);
|
||||
const SOCIAL_MEDIA = array(
|
||||
'threads.net',
|
||||
'instagram.com',
|
||||
'facebook.com',
|
||||
'x.com',
|
||||
't.co',
|
||||
'gab.com',
|
||||
'reddit.com',
|
||||
'bsky.app',
|
||||
'threads.com'
|
||||
);
|
||||
|
||||
private function __construct (string $ua, string $accept, string $addr){
|
||||
$this->ua = $ua;
|
||||
$this->accept = $accept;
|
||||
$this->addr = $addr;
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if (!isset(UserAgent::$instance)){
|
||||
UserAgent::$instance = new UserAgent(
|
||||
$_SERVER['HTTP_USER_AGENT'] ?? '',
|
||||
$_SERVER['HTTP_ACCEPT'] ?? '',
|
||||
$_SERVER['REMOTE_ADDR'] ?? ''
|
||||
);
|
||||
}
|
||||
return UserAgent::$instance;
|
||||
}
|
||||
|
||||
public static function errorPage($content, $extra = false){
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<?php if(isset($extra['refresh'])): ?>
|
||||
<meta http-equiv="Refresh" content="10; url=?<?php echo htmlspecialchars($extra['refresh']); ?>" />
|
||||
<?php endif ?>
|
||||
<style>
|
||||
body {font-family: system-ui,-apple-system,BlinkMacSystemFont,Roboto,'Segoe UI',sans-serif;font-size:24px; line-height: 1.5; text-align: center; background-color: #eee; color: #222}
|
||||
main {max-width: 800px; margin: auto;}
|
||||
a:link, a:visited {color: #39f}
|
||||
@media (prefers-color-scheme:dark) {body {background-color: #222; color: #eee}}
|
||||
</style>
|
||||
</head><body>
|
||||
<main>
|
||||
<?php echo $content ?>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function clean($dup = false){
|
||||
if ($dup === false) $dup = array();
|
||||
|
||||
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] !== '') {
|
||||
$hasTrackingParams = false;
|
||||
foreach (UserAgent::TRACKING_PARAMS as $x) if (isset($_GET[$x])) {
|
||||
$hasTrackingParams = true;
|
||||
break;
|
||||
}
|
||||
if ($hasTrackingParams) {
|
||||
$nQs = explode('&', $_SERVER['QUERY_STRING']);
|
||||
$nQs = array_filter($nQs, function ($x) use ($dup) {
|
||||
$xParts = explode('=', $x, 2);
|
||||
return !in_array($xParts[0], $dup);
|
||||
});
|
||||
$normQs = implode('&', $nQs);
|
||||
$nQs = array_filter($nQs, function ($x) {
|
||||
$xParts = explode('=', $x, 2);
|
||||
return !in_array($xParts[0], UserAgent::TRACKING_PARAMS);
|
||||
});
|
||||
$nQs = implode('&', $nQs);
|
||||
if ($nQs !== $normQs) {
|
||||
$platform = "a Meta social (i.e. Instagram or Threads)";
|
||||
if (strpos($normQs, 'fbclid=gab') !== false) {
|
||||
$platform = "Gab Social";
|
||||
}
|
||||
http_response_code(300);
|
||||
header("Connection: close");
|
||||
header("Cache-Control: no-cache, must-revalidate, no-store");
|
||||
header("Forensics: go away");
|
||||
ob_start(); ?>
|
||||
<p>You are landing onto <?php echo htmlspecialchars($_SERVER['HTTP_HOST']) ?> from <?php echo htmlspecialchars($platform) ?>. Please wait a few seconds before being redirected.</p>
|
||||
<p>Reminder (<i>if you missed it</i>): <u>usage of my content</u> (including website content, services, posts or comments) <u>for purposes of harming people is strictly prohibited</u>, no matter whether towards me or someone else.</p>
|
||||
<p>You are informed of the consequences of breach of this agreement, including but not limited to service ban.</p>
|
||||
<p><a href="<?php echo htmlspecialchars($nQs) ?>">Click here if you are not redirected</a></p>
|
||||
<?php $content = ob_get_clean();
|
||||
self::errorPage($content, [
|
||||
'refresh' => $nQs
|
||||
]);
|
||||
die;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ua = self::getInstance();
|
||||
|
||||
if ($ua->stringMatch(self::BAD_STRINGS)) {
|
||||
self::badUserAgent();
|
||||
}
|
||||
}
|
||||
|
||||
public function distress() {
|
||||
http_response_code(451);
|
||||
header("Connection: close");
|
||||
header("Forensics: there is nothing to see, go away");
|
||||
header("Every-Action: has a consequence, and every consequence, being an action itself, has a counterconsequence");
|
||||
ob_start(); ?>
|
||||
<p>Access to <?php echo htmlspecialchars($_SERVER['HTTP_HOST']) ?> is temporarily blocked from <?php echo $this->getCountry()->getName() ?> to protect the network's safety and integrity.</p>
|
||||
<p>Reminder (<i>if you missed it</i>): <u>usage of my content</u> (including website content, services, posts or comments) <u>for purposes of harming people is strictly prohibited</u>, no matter whether towards me or someone else.</p>
|
||||
<p>You are informed of the consequences of breach of this agreement, including but not limited to service ban.</p>
|
||||
<?php $content = ob_get_clean();
|
||||
self::errorPage($content);
|
||||
die;
|
||||
}
|
||||
|
||||
private function stringMatch($strings) {
|
||||
$myString = $this->getString();
|
||||
|
||||
foreach ($strings as $str) {
|
||||
if(preg_match($str, $myString)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isBrowser(){
|
||||
return substr($this->ua, 0, 11) === 'Mozilla/5.0';
|
||||
}
|
||||
|
||||
public function isMobile(){
|
||||
return strpos($this->ua, 'Mobile') !== false;
|
||||
}
|
||||
|
||||
public function getString(){
|
||||
return $this->ua;
|
||||
}
|
||||
|
||||
public function acceptsHtml(){
|
||||
return strpos($this->accept, 'text/html') !== 0 || strpos($this->accept, 'application/xhtml+xml') !== 0;
|
||||
}
|
||||
|
||||
public function acceptsJson(){
|
||||
return strpos($this->accept, 'application/json') !== 0;
|
||||
}
|
||||
|
||||
public function isBehindProxy(){
|
||||
return in_array($this->addr, UserAgent::LOCAL_ADDRESSES, true) && isset($_SERVER['HTTP_X_REAL_IP']);
|
||||
}
|
||||
|
||||
public function isLocal(){
|
||||
return in_array($this->addr, UserAgent::LOCAL_ADDRESSES, true) && !isset($_SERVER['HTTP_X_REAL_IP']);
|
||||
}
|
||||
|
||||
public function getAddr() {
|
||||
return ($this->isBehindProxy()? $_SERVER['HTTP_X_REAL_IP'] : $this->addr);
|
||||
}
|
||||
|
||||
public function getCountry() {
|
||||
if ($this->country === null) {
|
||||
if ($this->locator === null) $this->locator = $this->probeLocator();
|
||||
if ($this->locator === false) $this->country = Country::outerSpace();
|
||||
$this->country = $this->locator->locate($this->getAddr());
|
||||
}
|
||||
return $this->country;
|
||||
}
|
||||
|
||||
private function isTrusted() {
|
||||
return $this->isLocal() || $this->stringMatch(self::TRUSTED_STRINGS);
|
||||
}
|
||||
|
||||
public function getBlock() {
|
||||
if ($this->isTrusted()) return new RegionBlock('+');
|
||||
return new RegionBlock($this->getCountry()->getCode());
|
||||
}
|
||||
|
||||
public function getReferer() {
|
||||
return $_SERVER['HTTP_REFERER'] ?? $this->getLocation();
|
||||
}
|
||||
|
||||
public function getLocation() {
|
||||
return "http" . (($_SERVER['HTTPS'] ?? '')? 's' : '') . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function makeBeacon() {
|
||||
$now = time() / 3600;
|
||||
return hash('sha256', 'AF' . $this->addr . $this->ua . $now);
|
||||
}
|
||||
|
||||
private function probeLocator() {
|
||||
if (!$this->conn) return false;
|
||||
$loc = new IpLocator($this->conn);
|
||||
return $loc;
|
||||
}
|
||||
|
||||
public static function badUserAgent() {
|
||||
http_response_code(403);
|
||||
header("Content-Type: text/plain");
|
||||
header("Cache-Control: no-cache");
|
||||
header("X-Robots-Tag: nolawyers,noai,noimageai");
|
||||
header("Connection: close");
|
||||
|
||||
echo <<<EOF
|
||||
Your hacking attempt has been blocked.
|
||||
|
||||
If you believe you were blocked in error, change your user-agent string.
|
||||
|
||||
EOF;
|
||||
|
||||
die;
|
||||
}
|
||||
|
||||
public function isSocialMedia() {
|
||||
$ref = $this->getReferer();
|
||||
foreach (self::SOCIAL_MEDIA as $pf) {
|
||||
if (strpos($pf, $ref) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getLangs() {
|
||||
if ($this->langs === null) {
|
||||
$langsRaw = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
|
||||
$langsSc = explode(',', $langsRaw);
|
||||
$langs = array();
|
||||
if(isset($this->prefLang)){
|
||||
$langs[] = $this->prefLang;
|
||||
}
|
||||
foreach($langsSc as $l) {
|
||||
$lPart = explode(';', $l, 2)[0];
|
||||
$langs[] = $lPart;
|
||||
}
|
||||
$this->langs = $langs;
|
||||
}
|
||||
|
||||
return $this->langs;
|
||||
}
|
||||
|
||||
public function getLang() {
|
||||
return $this->getLangs()[0] ?? 'en';
|
||||
}
|
||||
|
||||
public function setPreferredLang($l) {
|
||||
$this->prefLang = $l;
|
||||
if (isset($this->langs)) {
|
||||
$this->langs = array_merge([$l], $this->langs);
|
||||
}
|
||||
}
|
||||
|
||||
public function setConnection($conn) {
|
||||
// e.g. DatabaseConnection::getDefault();
|
||||
$this->conn = $conn;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue