1
0

google.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. <?php
  2. // @TODO check for consent.google.com page, if need be
  3. class google{
  4. public function __construct(){
  5. include "lib/fuckhtml.php";
  6. $this->fuckhtml = new fuckhtml();
  7. include "lib/backend.php";
  8. $this->backend = new backend("google");
  9. }
  10. public function getfilters($page){
  11. $base = [
  12. "country" => [ // gl=<country> (image: cr=countryAF)
  13. "display" => "Country",
  14. "option" => [
  15. "any" => "Instance's country",
  16. "af" => "Afghanistan",
  17. "al" => "Albania",
  18. "dz" => "Algeria",
  19. "as" => "American Samoa",
  20. "ad" => "Andorra",
  21. "ao" => "Angola",
  22. "ai" => "Anguilla",
  23. "aq" => "Antarctica",
  24. "ag" => "Antigua and Barbuda",
  25. "ar" => "Argentina",
  26. "am" => "Armenia",
  27. "aw" => "Aruba",
  28. "au" => "Australia",
  29. "at" => "Austria",
  30. "az" => "Azerbaijan",
  31. "bs" => "Bahamas",
  32. "bh" => "Bahrain",
  33. "bd" => "Bangladesh",
  34. "bb" => "Barbados",
  35. "by" => "Belarus",
  36. "be" => "Belgium",
  37. "bz" => "Belize",
  38. "bj" => "Benin",
  39. "bm" => "Bermuda",
  40. "bt" => "Bhutan",
  41. "bo" => "Bolivia",
  42. "ba" => "Bosnia and Herzegovina",
  43. "bw" => "Botswana",
  44. "bv" => "Bouvet Island",
  45. "br" => "Brazil",
  46. "io" => "British Indian Ocean Territory",
  47. "bn" => "Brunei Darussalam",
  48. "bg" => "Bulgaria",
  49. "bf" => "Burkina Faso",
  50. "bi" => "Burundi",
  51. "kh" => "Cambodia",
  52. "cm" => "Cameroon",
  53. "ca" => "Canada",
  54. "cv" => "Cape Verde",
  55. "ky" => "Cayman Islands",
  56. "cf" => "Central African Republic",
  57. "td" => "Chad",
  58. "cl" => "Chile",
  59. "cn" => "China",
  60. "cx" => "Christmas Island",
  61. "cc" => "Cocos (Keeling) Islands",
  62. "co" => "Colombia",
  63. "km" => "Comoros",
  64. "cg" => "Congo",
  65. "cd" => "Congo, the Democratic Republic",
  66. "ck" => "Cook Islands",
  67. "cr" => "Costa Rica",
  68. "ci" => "Cote D'ivoire",
  69. "hr" => "Croatia",
  70. "cu" => "Cuba",
  71. "cy" => "Cyprus",
  72. "cz" => "Czech Republic",
  73. "dk" => "Denmark",
  74. "dj" => "Djibouti",
  75. "dm" => "Dominica",
  76. "do" => "Dominican Republic",
  77. "ec" => "Ecuador",
  78. "eg" => "Egypt",
  79. "sv" => "El Salvador",
  80. "gq" => "Equatorial Guinea",
  81. "er" => "Eritrea",
  82. "ee" => "Estonia",
  83. "et" => "Ethiopia",
  84. "fk" => "Falkland Islands (Malvinas)",
  85. "fo" => "Faroe Islands",
  86. "fj" => "Fiji",
  87. "fi" => "Finland",
  88. "fr" => "France",
  89. "gf" => "French Guiana",
  90. "pf" => "French Polynesia",
  91. "tf" => "French Southern Territories",
  92. "ga" => "Gabon",
  93. "gm" => "Gambia",
  94. "ge" => "Georgia",
  95. "de" => "Germany",
  96. "gh" => "Ghana",
  97. "gi" => "Gibraltar",
  98. "gr" => "Greece",
  99. "gl" => "Greenland",
  100. "gd" => "Grenada",
  101. "gp" => "Guadeloupe",
  102. "gu" => "Guam",
  103. "gt" => "Guatemala",
  104. "gn" => "Guinea",
  105. "gw" => "Guinea-Bissau",
  106. "gy" => "Guyana",
  107. "ht" => "Haiti",
  108. "hm" => "Heard Island and Mcdonald Islands",
  109. "va" => "Holy See (Vatican City State)",
  110. "hn" => "Honduras",
  111. "hk" => "Hong Kong",
  112. "hu" => "Hungary",
  113. "is" => "Iceland",
  114. "in" => "India",
  115. "id" => "Indonesia",
  116. "ir" => "Iran, Islamic Republic",
  117. "iq" => "Iraq",
  118. "ie" => "Ireland",
  119. "il" => "Israel",
  120. "it" => "Italy",
  121. "jm" => "Jamaica",
  122. "jp" => "Japan",
  123. "jo" => "Jordan",
  124. "kz" => "Kazakhstan",
  125. "ke" => "Kenya",
  126. "ki" => "Kiribati",
  127. "kp" => "Korea, Democratic People's Republic",
  128. "kr" => "Korea, Republic",
  129. "kw" => "Kuwait",
  130. "kg" => "Kyrgyzstan",
  131. "la" => "Lao People's Democratic Republic",
  132. "lv" => "Latvia",
  133. "lb" => "Lebanon",
  134. "ls" => "Lesotho",
  135. "lr" => "Liberia",
  136. "ly" => "Libyan Arab Jamahiriya",
  137. "li" => "Liechtenstein",
  138. "lt" => "Lithuania",
  139. "lu" => "Luxembourg",
  140. "mo" => "Macao",
  141. "mk" => "Macedonia, the Former Yugosalv Republic",
  142. "mg" => "Madagascar",
  143. "mw" => "Malawi",
  144. "my" => "Malaysia",
  145. "mv" => "Maldives",
  146. "ml" => "Mali",
  147. "mt" => "Malta",
  148. "mh" => "Marshall Islands",
  149. "mq" => "Martinique",
  150. "mr" => "Mauritania",
  151. "mu" => "Mauritius",
  152. "yt" => "Mayotte",
  153. "mx" => "Mexico",
  154. "fm" => "Micronesia, Federated States",
  155. "md" => "Moldova, Republic",
  156. "mc" => "Monaco",
  157. "mn" => "Mongolia",
  158. "ms" => "Montserrat",
  159. "ma" => "Morocco",
  160. "mz" => "Mozambique",
  161. "mm" => "Myanmar",
  162. "na" => "Namibia",
  163. "nr" => "Nauru",
  164. "np" => "Nepal",
  165. "nl" => "Netherlands",
  166. "an" => "Netherlands Antilles",
  167. "nc" => "New Caledonia",
  168. "nz" => "New Zealand",
  169. "ni" => "Nicaragua",
  170. "ne" => "Niger",
  171. "ng" => "Nigeria",
  172. "nu" => "Niue",
  173. "nf" => "Norfolk Island",
  174. "mp" => "Northern Mariana Islands",
  175. "no" => "Norway",
  176. "om" => "Oman",
  177. "pk" => "Pakistan",
  178. "pw" => "Palau",
  179. "ps" => "Palestinian Territory, Occupied",
  180. "pa" => "Panama",
  181. "pg" => "Papua New Guinea",
  182. "py" => "Paraguay",
  183. "pe" => "Peru",
  184. "ph" => "Philippines",
  185. "pn" => "Pitcairn",
  186. "pl" => "Poland",
  187. "pt" => "Portugal",
  188. "pr" => "Puerto Rico",
  189. "qa" => "Qatar",
  190. "re" => "Reunion",
  191. "ro" => "Romania",
  192. "ru" => "Russian Federation",
  193. "rw" => "Rwanda",
  194. "sh" => "Saint Helena",
  195. "kn" => "Saint Kitts and Nevis",
  196. "lc" => "Saint Lucia",
  197. "pm" => "Saint Pierre and Miquelon",
  198. "vc" => "Saint Vincent and the Grenadines",
  199. "ws" => "Samoa",
  200. "sm" => "San Marino",
  201. "st" => "Sao Tome and Principe",
  202. "sa" => "Saudi Arabia",
  203. "sn" => "Senegal",
  204. "cs" => "Serbia and Montenegro",
  205. "sc" => "Seychelles",
  206. "sl" => "Sierra Leone",
  207. "sg" => "Singapore",
  208. "sk" => "Slovakia",
  209. "si" => "Slovenia",
  210. "sb" => "Solomon Islands",
  211. "so" => "Somalia",
  212. "za" => "South Africa",
  213. "gs" => "South Georgia and the South Sandwich Islands",
  214. "es" => "Spain",
  215. "lk" => "Sri Lanka",
  216. "sd" => "Sudan",
  217. "sr" => "Suriname",
  218. "sj" => "Svalbard and Jan Mayen",
  219. "sz" => "Swaziland",
  220. "se" => "Sweden",
  221. "ch" => "Switzerland",
  222. "sy" => "Syrian Arab Republic",
  223. "tw" => "Taiwan, Province of China",
  224. "tj" => "Tajikistan",
  225. "tz" => "Tanzania, United Republic",
  226. "th" => "Thailand",
  227. "tl" => "Timor-Leste",
  228. "tg" => "Togo",
  229. "tk" => "Tokelau",
  230. "to" => "Tonga",
  231. "tt" => "Trinidad and Tobago",
  232. "tn" => "Tunisia",
  233. "tr" => "Turkey",
  234. "tm" => "Turkmenistan",
  235. "tc" => "Turks and Caicos Islands",
  236. "tv" => "Tuvalu",
  237. "ug" => "Uganda",
  238. "ua" => "Ukraine",
  239. "ae" => "United Arab Emirates",
  240. "uk" => "United Kingdom",
  241. "us" => "United States",
  242. "um" => "United States Minor Outlying Islands",
  243. "uy" => "Uruguay",
  244. "uz" => "Uzbekistan",
  245. "vu" => "Vanuatu",
  246. "ve" => "Venezuela",
  247. "vn" => "Viet Nam",
  248. "vg" => "Virgin Islands, British",
  249. "vi" => "Virgin Islands, U.S.",
  250. "wf" => "Wallis and Futuna",
  251. "eh" => "Western Sahara",
  252. "ye" => "Yemen",
  253. "zm" => "Zambia",
  254. "zw" => "Zimbabwe"
  255. ]
  256. ],
  257. "nsfw" => [
  258. "display" => "NSFW",
  259. "option" => [
  260. "yes" => "Yes", // safe=active
  261. "no" => "No" // safe=off
  262. ]
  263. ]
  264. ];
  265. switch($page){
  266. case "web":
  267. return array_merge(
  268. $base,
  269. [
  270. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  271. "display" => "Language",
  272. "option" => [
  273. "any" => "Any language",
  274. "ar" => "Arabic",
  275. "bg" => "Bulgarian",
  276. "ca" => "Catalan",
  277. "cs" => "Czech",
  278. "da" => "Danish",
  279. "de" => "German",
  280. "el" => "Greek",
  281. "en" => "English",
  282. "es" => "Spanish",
  283. "et" => "Estonian",
  284. "fi" => "Finnish",
  285. "fr" => "French",
  286. "hr" => "Croatian",
  287. "hu" => "Hungarian",
  288. "id" => "Indonesian",
  289. "is" => "Icelandic",
  290. "it" => "Italian",
  291. "iw" => "Hebrew",
  292. "ja" => "Japanese",
  293. "ko" => "Korean",
  294. "lt" => "Lithuanian",
  295. "lv" => "Latvian",
  296. "nl" => "Dutch",
  297. "no" => "Norwegian",
  298. "pl" => "Polish",
  299. "pt" => "Portuguese",
  300. "ro" => "Romanian",
  301. "ru" => "Russian",
  302. "sk" => "Slovak",
  303. "sl" => "Slovenian",
  304. "sr" => "Serbian",
  305. "sv" => "Swedish",
  306. "tr" => "Turkish",
  307. "zh-CN" => "Chinese (Simplified)",
  308. "zh-TW" => "Chinese (Traditional)"
  309. ]
  310. ],
  311. "newer" => [ // tbs
  312. "display" => "Newer than",
  313. "option" => "_DATE"
  314. ],
  315. "older" => [
  316. "display" => "Older than",
  317. "option" => "_DATE"
  318. ],
  319. "spellcheck" => [
  320. "display" => "Spellcheck",
  321. "option" => [
  322. "yes" => "Yes",
  323. "no" => "No"
  324. ]
  325. ]
  326. ]
  327. );
  328. break;
  329. case "images":
  330. return array_merge(
  331. $base,
  332. [
  333. "time" => [ // tbs=qdr:<time>
  334. "display" => "Time posted",
  335. "option" => [
  336. "any" => "Any time",
  337. "d" => "Past 24 hours",
  338. "w" => "Past week",
  339. "m" => "Past month",
  340. "y" => "Past year"
  341. ]
  342. ],
  343. "size" => [ // imgsz
  344. "display" => "Size",
  345. "option" => [
  346. "any" => "Any size",
  347. "l" => "Large",
  348. "m" => "Medium",
  349. "i" => "Icon",
  350. "qsvga" => "Larger than 400x300",
  351. "vga" => "Larger than 640x480",
  352. "svga" => "Larger than 800x600",
  353. "xga" => "Larger than 1024x768",
  354. "2mp" => "Larger than 2MP",
  355. "4mp" => "Larger than 4MP",
  356. "6mp" => "Larger than 6MP",
  357. "8mp" => "Larger than 8MP",
  358. "10mp" => "Larger than 10MP",
  359. "12mp" => "Larger than 12MP",
  360. "15mp" => "Larger than 15MP",
  361. "20mp" => "Larger than 20MP",
  362. "40mp" => "Larger than 40MP",
  363. "70mp" => "Larger than 70MP"
  364. ]
  365. ],
  366. "ratio" => [ // imgar
  367. "display" => "Aspect ratio",
  368. "option" => [
  369. "any" => "Any ratio",
  370. "t|xt" => "Tall",
  371. "s" => "Square",
  372. "w" => "Wide",
  373. "xw" => "Panoramic"
  374. ]
  375. ],
  376. "color" => [ // imgc
  377. "display" => "Color",
  378. "option" => [
  379. "any" => "Any color",
  380. "color" => "Full color",
  381. "bnw" => "Black & white",
  382. "trans" => "Transparent",
  383. // from here, imgcolor
  384. "red" => "Red",
  385. "orange" => "Orange",
  386. "yellow" => "Yellow",
  387. "green" => "Green",
  388. "teal" => "Teal",
  389. "blue" => "Blue",
  390. "purple" => "Purple",
  391. "pink" => "Pink",
  392. "white" => "White",
  393. "gray" => "Gray",
  394. "black" => "Black",
  395. "brown" => "Brown"
  396. ]
  397. ],
  398. "type" => [ // tbs=itp:<type>
  399. "display" => "Type",
  400. "option" => [
  401. "any" => "Any type",
  402. "clipart" => "Clip Art",
  403. "lineart" => "Line Drawing",
  404. "animated" => "Animated"
  405. ]
  406. ],
  407. "format" => [ // as_filetype
  408. "display" => "Format",
  409. "option" => [
  410. "any" => "Any format",
  411. "jpg" => "JPG",
  412. "gif" => "GIF",
  413. "png" => "PNG",
  414. "bmp" => "BMP",
  415. "svg" => "SVG",
  416. "webp" => "WEBP",
  417. "ico" => "ICO",
  418. "craw" => "RAW"
  419. ]
  420. ],
  421. "rights" => [ // tbs=sur:<rights>
  422. "display" => "Usage rights",
  423. "option" => [
  424. "any" => "Any license",
  425. "cl" => "Creative Commons licenses",
  426. "ol" => "Commercial & other licenses"
  427. ]
  428. ]
  429. ]
  430. );
  431. break;
  432. case "videos":
  433. return array_merge(
  434. $base,
  435. [
  436. "newer" => [ // tbs
  437. "display" => "Newer than",
  438. "option" => "_DATE"
  439. ],
  440. "older" => [
  441. "display" => "Older than",
  442. "option" => "_DATE"
  443. ],
  444. "duration" => [
  445. "display" => "Duration",
  446. "option" => [
  447. "any" => "Any duration",
  448. "s" => "Short (0-4min)", // tbs=dur:s
  449. "m" => "Medium (4-20min)", // tbs=dur:m
  450. "l" => "Long (20+ min)" // tbs=dur:l
  451. ]
  452. ],
  453. "quality" => [
  454. "display" => "Quality",
  455. "option" => [
  456. "any" => "Any quality",
  457. "h" => "High quality" // tbs=hq:h
  458. ]
  459. ],
  460. "captions" => [
  461. "display" => "Captions",
  462. "option" => [
  463. "any" => "No preference",
  464. "yes" => "Closed captioned" // tbs=cc:1
  465. ]
  466. ]
  467. ]
  468. );
  469. break;
  470. case "news":
  471. return array_merge(
  472. $base,
  473. [
  474. "newer" => [ // tbs
  475. "display" => "Newer than",
  476. "option" => "_DATE"
  477. ],
  478. "older" => [
  479. "display" => "Older than",
  480. "option" => "_DATE"
  481. ],
  482. "sort" => [
  483. "display" => "Sort",
  484. "option" => [
  485. "relevance" => "Relevance",
  486. "date" => "Date" // sbd:1
  487. ]
  488. ]
  489. ]
  490. );
  491. break;
  492. }
  493. }
  494. private function get($proxy, $url, $get = []){
  495. $curlproc = curl_init();
  496. // use http2 in all cases
  497. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  498. if($get !== []){
  499. $get = http_build_query($get);
  500. $url .= "?" . $get;
  501. }
  502. curl_setopt($curlproc, CURLOPT_URL, $url);
  503. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  504. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  505. "User-Agent: " . config::USER_AGENT,
  506. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  507. "Accept-Language: en-US,en;q=0.5",
  508. "Accept-Encoding: gzip",
  509. "DNT: 1",
  510. "Connection: keep-alive",
  511. "Upgrade-Insecure-Requests: 1",
  512. "Sec-Fetch-Dest: document",
  513. "Sec-Fetch-Mode: navigate",
  514. "Sec-Fetch-Site: none",
  515. "Sec-Fetch-User: ?1",
  516. "Priority: u=1",
  517. "TE: trailers"
  518. ]);
  519. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  520. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  521. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  522. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  523. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  524. // follow redirects
  525. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  526. $this->backend->assign_proxy($curlproc, $proxy);
  527. $data = curl_exec($curlproc);
  528. if(curl_errno($curlproc)){
  529. throw new Exception(curl_error($curlproc));
  530. }
  531. curl_close($curlproc);
  532. return $data;
  533. }
  534. public function web($get){
  535. throw new Exception("Google made it impossible to scrape web results without a JavaScript runtime. In the meantime, use the Google API or the Google CSE scrapers.");
  536. }
  537. public function video($get){
  538. if($get["npt"]){
  539. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  540. $params = json_decode($params, true);
  541. }else{
  542. $search = $get["s"];
  543. $country = $get["country"];
  544. $nsfw = $get["nsfw"];
  545. $older = $get["older"];
  546. $newer = $get["newer"];
  547. $duration = $get["duration"];
  548. $quality = $get["quality"];
  549. $captions = $get["captions"];
  550. $proxy = $this->backend->get_ip();
  551. $params = [
  552. "q" => $search,
  553. "udm" => "7",
  554. "hl" => "en",
  555. "num" => 20
  556. ];
  557. // country
  558. if($country != "any"){
  559. $params["gl"] = $country;
  560. }
  561. // nsfw
  562. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  563. $tbs = [];
  564. // get date
  565. $older = $older === false ? null : date("m/d/Y", $older);
  566. $newer = $newer === false ? null : date("m/d/Y", $newer);
  567. if(
  568. $older !== null ||
  569. $newer !== null
  570. ){
  571. $tbs["cdr"] = "1";
  572. $tbs["cd_min"] = $newer;
  573. $tbs["cd_max"] = $older;
  574. }
  575. // duration
  576. if($duration != "any"){
  577. $tbs[] = "dur:" . $duration;
  578. }
  579. // quality
  580. if($quality != "any"){
  581. $tbs[] = "hq:" . $quality;
  582. }
  583. // captions
  584. if($captions != "any"){
  585. $tbs[] = "cc:" . $captions;
  586. }
  587. // append tbs
  588. if(count($tbs) !== 0){
  589. $params["tbs"] =
  590. implode(",", $tbs);
  591. }
  592. }
  593. try{
  594. $html =
  595. $this->get(
  596. $proxy,
  597. "https://www.google.com/search",
  598. $params
  599. );
  600. }catch(Exception $error){
  601. throw new Exception("Failed to get HTML");
  602. }
  603. if(!isset($params["start"])){
  604. $params["start"] = 0;
  605. }
  606. $params["start"] += 20;
  607. $this->fuckhtml->load($html);
  608. //
  609. // Parse web video page
  610. //
  611. $this->detect_sorry();
  612. // parse all <style> tags
  613. $this->parsestyles();
  614. // get javascript images
  615. $this->scrape_dimg($html);
  616. $this->scrape_imagearr($html);
  617. $out = [
  618. "status" => "ok",
  619. "npt" =>
  620. $this->backend->store(
  621. json_encode($params),
  622. "videos",
  623. $proxy
  624. ),
  625. "video" => [],
  626. "author" => [],
  627. "livestream" => [],
  628. "playlist" => [],
  629. "reel" => []
  630. ];
  631. $search_div =
  632. $this->fuckhtml
  633. ->getElementById(
  634. "center_col"
  635. );
  636. if($search_div === false){
  637. throw new Exception("Failed to grep search div");
  638. }
  639. $this->fuckhtml->load($search_div);
  640. $results =
  641. $this->fuckhtml
  642. ->getElementsByClassName(
  643. $this->getstyle([
  644. "margin" => "0px 0px 30px"
  645. ]),
  646. "div"
  647. );
  648. foreach($results as $result){
  649. $this->fuckhtml->load($result);
  650. $url =
  651. $this->fuckhtml
  652. ->getElementsByTagName(
  653. "a"
  654. );
  655. if(count($url) === 0){
  656. // no url, weird, continue
  657. continue;
  658. }
  659. $title =
  660. $this->fuckhtml
  661. ->getElementsByTagName(
  662. "h3"
  663. );
  664. if(count($title) === 0){
  665. // no title, weird, continue
  666. continue;
  667. }
  668. // get description
  669. $description =
  670. $this->fuckhtml
  671. ->getElementsByClassName(
  672. $this->getstyle([
  673. "-webkit-box-orient" => "vertical",
  674. "display" => "-webkit-box",
  675. "-webkit-line-clamp" => "2",
  676. "overflow" => "hidden",
  677. "word-break" => "break-word"
  678. ]),
  679. "div"
  680. );
  681. if(count($description) === 0){
  682. $description = null;
  683. }else{
  684. $description =
  685. html_entity_decode(
  686. $this->titledots(
  687. $this->fuckhtml
  688. ->getTextContent(
  689. $description[0]
  690. )
  691. )
  692. );
  693. }
  694. // get author + date posted
  695. $metadiv =
  696. $this->fuckhtml
  697. ->getElementsByClassName(
  698. $this->getstyle([
  699. "margin-top" => "12px"
  700. ]),
  701. "div"
  702. );
  703. $author = null;
  704. $date = null;
  705. if(count($metadiv) !== 0){
  706. $metadiv =
  707. explode(
  708. "·",
  709. $this->fuckhtml
  710. ->getTextContent(
  711. $metadiv[0]
  712. )
  713. );
  714. if(count($metadiv) === 3){
  715. $author = trim($metadiv[1]);
  716. $date = strtotime(trim($metadiv[2]));
  717. }elseif(count($metadiv) === 2){
  718. $author = trim($metadiv[0]);
  719. $date = strtotime(trim($metadiv[1]));
  720. }
  721. }
  722. $thumb = [
  723. "url" => null,
  724. "ratio" => null
  725. ];
  726. $image =
  727. $this->fuckhtml
  728. ->getElementsByTagName(
  729. "img"
  730. );
  731. $duration = null;
  732. if(
  733. count($image) !== 0 &&
  734. isset($image[0]["attributes"]["id"])
  735. ){
  736. $thumb = [
  737. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  738. "ratio" => "16:9"
  739. ];
  740. // get duration
  741. $duration =
  742. $this->fuckhtml
  743. ->getElementsByClassName(
  744. $this->getstyle([
  745. "background-color" => "rgba(0,0,0,0.6)",
  746. "color" => "#fff",
  747. "fill" => "#fff"
  748. ])
  749. );
  750. if(count($duration) !== 0){
  751. $duration =
  752. $this->hms2int(
  753. $this->fuckhtml
  754. ->getTextContent(
  755. $duration[0]
  756. ));
  757. }else{
  758. $duration = null;
  759. }
  760. }
  761. $out["video"][] = [
  762. "title" =>
  763. $this->titledots(
  764. $this->fuckhtml
  765. ->getTextContent(
  766. $title[0]
  767. )
  768. ),
  769. "description" => $description,
  770. "author" => [
  771. "name" => $author,
  772. "url" => null,
  773. "avatar" => null
  774. ],
  775. "date" => $date,
  776. "duration" => $duration,
  777. "views" => null,
  778. "thumb" => $thumb,
  779. "url" =>
  780. $this->fuckhtml
  781. ->getTextContent(
  782. $url[0]["attributes"]["href"]
  783. )
  784. ];
  785. }
  786. return $out;
  787. }
  788. public function news($get){
  789. if($get["npt"]){
  790. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  791. /*parse_str(
  792. parse_url($req, PHP_URL_QUERY),
  793. $search
  794. );*/
  795. try{
  796. $html =
  797. $this->get(
  798. $proxy,
  799. "https://www.google.com" . $req,
  800. []
  801. );
  802. }catch(Exception $error){
  803. throw new Exception("Failed to get HTML");
  804. }
  805. }else{
  806. $search = $get["s"];
  807. $country = $get["country"];
  808. $nsfw = $get["nsfw"];
  809. $older = $get["older"];
  810. $newer = $get["newer"];
  811. $sort = $get["sort"];
  812. $proxy = $this->backend->get_ip();
  813. $params = [
  814. "q" => $search,
  815. "tbm" => "nws",
  816. "hl" => "en",
  817. "num" => "20"
  818. ];
  819. // country
  820. if($country != "any"){
  821. $params["gl"] = $country;
  822. }
  823. // nsfw
  824. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  825. $tbs = [];
  826. // get date
  827. $older = $older === false ? null : date("m/d/Y", $older);
  828. $newer = $newer === false ? null : date("m/d/Y", $newer);
  829. if(
  830. $older !== null ||
  831. $newer !== null
  832. ){
  833. $tbs["cdr"] = "1";
  834. $tbs["cd_min"] = $newer;
  835. $tbs["cd_max"] = $older;
  836. }
  837. // relevance
  838. if($sort == "date"){
  839. $tbs["sbd"] = "1";
  840. }
  841. // append tbs
  842. if(count($tbs) !== 0){
  843. $params["tbs"] = "";
  844. foreach($tbs as $key => $value){
  845. $params["tbs"] .= $key . ":" . $value . ",";
  846. }
  847. $params["tbs"] = rtrim($params["tbs"], ",");
  848. }
  849. //$html = file_get_contents("scraper/google-news.html");
  850. $html =
  851. $this->get(
  852. $proxy,
  853. "https://www.google.com/search",
  854. $params
  855. );
  856. }
  857. $out = [
  858. "status" => "ok",
  859. "npt" => null,
  860. "news" => []
  861. ];
  862. $this->fuckhtml->load($html);
  863. $this->detect_sorry();
  864. // get images
  865. $this->scrape_dimg($html);
  866. // parse styles
  867. $this->parsestyles();
  868. $center_col =
  869. $this->fuckhtml
  870. ->getElementById(
  871. "center_col",
  872. "div"
  873. );
  874. if($center_col === null){
  875. throw new Exception("Could not grep result div");
  876. }
  877. $this->fuckhtml->load($center_col);
  878. // get next page
  879. $npt =
  880. $this->fuckhtml
  881. ->getElementById(
  882. "pnnext",
  883. "a"
  884. );
  885. if($npt !== false){
  886. $out["npt"] =
  887. $this->backend->store(
  888. $this->fuckhtml
  889. ->getTextContent(
  890. $npt["attributes"]
  891. ["href"]
  892. ),
  893. "news",
  894. $proxy
  895. );
  896. }
  897. $as =
  898. $this->fuckhtml
  899. ->getElementsByAttributeName(
  900. "jsname",
  901. "a"
  902. );
  903. foreach($as as $a){
  904. $this->fuckhtml->load($a);
  905. // get title
  906. $title =
  907. $this->fuckhtml
  908. ->getElementsByAttributeValue(
  909. "role",
  910. "heading",
  911. "div"
  912. );
  913. if(count($title) === 0){
  914. continue;
  915. }
  916. $title =
  917. $this->titledots(
  918. $this->fuckhtml
  919. ->getTextContent(
  920. $title[0]
  921. )
  922. );
  923. // get thumbnail
  924. $image =
  925. $this->fuckhtml
  926. ->getElementsByAttributeName(
  927. "id",
  928. "img"
  929. );
  930. // check for padded title node, if found, we're inside a carousel
  931. $probe =
  932. $this->fuckhtml
  933. ->getElementsByClassName(
  934. $this->getstyle(
  935. [
  936. "padding" => "16px 16px 40px 16px"
  937. ]
  938. ),
  939. "div"
  940. );
  941. if(count($probe) !== 0){
  942. $probe = true;
  943. }else{
  944. $probe = false;
  945. }
  946. if(
  947. count($image) !== 0 &&
  948. !isset($image[0]["attributes"]["width"])
  949. ){
  950. $thumb = [
  951. "url" =>
  952. $this->getdimg(
  953. $image[0]["attributes"]["id"]
  954. ),
  955. "ratio" => $probe === true ? "16:9" : "1:1"
  956. ];
  957. }else{
  958. $thumb = [
  959. "url" => null,
  960. "ratio" => null
  961. ];
  962. }
  963. $description = null;
  964. if($probe === false){
  965. $desc_divs =
  966. $this->fuckhtml
  967. ->getElementsByAttributeName(
  968. "style",
  969. "div"
  970. );
  971. foreach($desc_divs as $desc){
  972. if(
  973. strpos(
  974. $desc["attributes"]["style"],
  975. "margin-top:"
  976. ) !== false
  977. ){
  978. $description =
  979. $this->titledots(
  980. $this->fuckhtml
  981. ->getTextContent(
  982. $desc
  983. )
  984. );
  985. break;
  986. }
  987. }
  988. }
  989. // get author
  990. $author =
  991. $this->fuckhtml
  992. ->getElementsByClassName(
  993. $this->getstyle(
  994. [
  995. "overflow" => "hidden",
  996. "text-align" => "left",
  997. "text-overflow" => "ellipsis",
  998. "white-space" => "nowrap",
  999. "margin-bottom" => "8px"
  1000. ]
  1001. ),
  1002. "div"
  1003. );
  1004. if(count($author) !== 0){
  1005. $author =
  1006. $this->fuckhtml
  1007. ->getTextContent(
  1008. $author[0]
  1009. );
  1010. }else{
  1011. $author = null;
  1012. }
  1013. // get date
  1014. $date = null;
  1015. $date_div =
  1016. $this->fuckhtml
  1017. ->getElementsByAttributeName(
  1018. "style",
  1019. "div"
  1020. );
  1021. foreach($date_div as $d){
  1022. $this->fuckhtml->load($d);
  1023. $span =
  1024. $this->fuckhtml
  1025. ->getElementsByTagName(
  1026. "span"
  1027. );
  1028. if(
  1029. strpos(
  1030. $d["attributes"]["style"],
  1031. "bottom:"
  1032. ) !== false
  1033. ){
  1034. $date =
  1035. strtotime(
  1036. $this->fuckhtml
  1037. ->getTextContent(
  1038. $span[count($span) - 1]
  1039. )
  1040. );
  1041. break;
  1042. }
  1043. }
  1044. $out["news"][] = [
  1045. "title" => $title,
  1046. "author" => $author,
  1047. "description" => $description,
  1048. "date" => $date,
  1049. "thumb" => $thumb,
  1050. "url" =>
  1051. $this->unshiturl(
  1052. $a["attributes"]
  1053. ["href"]
  1054. )
  1055. ];
  1056. }
  1057. return $out;
  1058. }
  1059. public function image($get){
  1060. // generate parameters
  1061. if($get["npt"]){
  1062. [$params, $proxy] =
  1063. $this->backend->get(
  1064. $get["npt"],
  1065. "images"
  1066. );
  1067. $params = json_decode($params, true);
  1068. }else{
  1069. $search = $get["s"];
  1070. if(strlen($search) === 0){
  1071. throw new Exception("Search term is empty!");
  1072. }
  1073. $proxy = $this->backend->get_ip();
  1074. $country = $get["country"];
  1075. $nsfw = $get["nsfw"];
  1076. $time = $get["time"];
  1077. $size = $get["size"];
  1078. $ratio = $get["ratio"];
  1079. $color = $get["color"];
  1080. $type = $get["type"];
  1081. $format = $get["format"];
  1082. $rights = $get["rights"];
  1083. $params = [
  1084. "q" => $search,
  1085. "udm" => "2" // get images
  1086. ];
  1087. // country (image search uses cr instead of gl)
  1088. if($country != "any"){
  1089. $params["cr"] = "country" . strtoupper($country);
  1090. }
  1091. // nsfw
  1092. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1093. // generate tbs
  1094. $tbs = [];
  1095. // time
  1096. if($time != "any"){
  1097. $tbs["qdr"] = $time;
  1098. }
  1099. // size
  1100. if($size != "any"){
  1101. $params["imgsz"] = $size;
  1102. }
  1103. // ratio
  1104. if($ratio != "any"){
  1105. $params["imgar"] = $ratio;
  1106. }
  1107. // color
  1108. if($color != "any"){
  1109. if(
  1110. $color == "color" ||
  1111. $color == "trans"
  1112. ){
  1113. $params["imgc"] = $color;
  1114. }elseif($color == "bnw"){
  1115. $params["imgc"] = "gray";
  1116. }else{
  1117. $tbs["ic"] = "specific";
  1118. $tbs["isc"] = $color;
  1119. }
  1120. }
  1121. // type
  1122. if($type != "any"){
  1123. $tbs["itp"] = $type;
  1124. }
  1125. // format
  1126. if($format != "any"){
  1127. $params["as_filetype"] = $format;
  1128. }
  1129. // rights (tbs)
  1130. if($rights != "any"){
  1131. $tbs["sur"] = $rights;
  1132. }
  1133. // append tbs
  1134. if(count($tbs) !== 0){
  1135. $params["tbs"] = "";
  1136. foreach($tbs as $key => $value){
  1137. $params["tbs"] .= $key . ":" . $value . ",";
  1138. }
  1139. $params["tbs"] = rtrim($params["tbs"], ",");
  1140. }
  1141. }
  1142. /*
  1143. $handle = fopen("scraper/page.html", "r");
  1144. $html = fread($handle, filesize("scraper/page.html"));
  1145. fclose($handle);*/
  1146. try{
  1147. $html =
  1148. $this->get(
  1149. $proxy,
  1150. "https://www.google.com/search",
  1151. $params
  1152. );
  1153. }catch(Exception $error){
  1154. throw new Exception("Failed to get search page");
  1155. }
  1156. $this->fuckhtml->load($html);
  1157. $this->detect_sorry();
  1158. // get javascript images
  1159. $this->scrape_imagearr($html);
  1160. $out = [
  1161. "status" => "ok",
  1162. "npt" => null,
  1163. "image" => []
  1164. ];
  1165. $images =
  1166. $this->fuckhtml
  1167. ->getElementsByClassName(
  1168. "ivg-i",
  1169. "div"
  1170. );
  1171. foreach($images as $div){
  1172. $this->fuckhtml->load($div);
  1173. $image =
  1174. $this->fuckhtml
  1175. ->getElementsByTagName("img")[0];
  1176. // make sure we dont attempt to show an image we dont have data for
  1177. if(
  1178. isset($div["attributes"]["data-docid"]) &&
  1179. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1180. ){
  1181. $source =
  1182. $this->image_arr[
  1183. $div["attributes"]["data-docid"]
  1184. ];
  1185. }else{
  1186. continue;
  1187. }
  1188. $out["image"][] = [
  1189. "title" =>
  1190. $this->titledots(
  1191. $this->fuckhtml
  1192. ->getTextContent(
  1193. $image["attributes"]["alt"]
  1194. )
  1195. ),
  1196. "source" => $source,
  1197. "url" =>
  1198. $this->fuckhtml
  1199. ->getTextContent(
  1200. $div["attributes"]["data-lpage"]
  1201. )
  1202. ];
  1203. }
  1204. // as usual, no way to check if there is a next page reliably
  1205. if(count($out["image"]) > 50){
  1206. if(!isset($params["start"])){
  1207. $params["start"] = 10;
  1208. }else{
  1209. $params["start"] += 10;
  1210. }
  1211. $out["npt"] =
  1212. $this->backend
  1213. ->store(
  1214. json_encode($params),
  1215. "image",
  1216. $proxy
  1217. );
  1218. }
  1219. return $out;
  1220. }
  1221. private function scrape_dimg($html){
  1222. // get images loaded through javascript
  1223. $this->dimg = [];
  1224. preg_match_all(
  1225. '/function\(\){google\.ldi=({.*?});/',
  1226. $html,
  1227. $dimg
  1228. );
  1229. if(isset($dimg[1])){
  1230. foreach($dimg[1] as $i){
  1231. $tmp = json_decode($i, true);
  1232. foreach($tmp as $key => $value){
  1233. $this->dimg[$key] =
  1234. $this->unshit_thumb(
  1235. $value
  1236. );
  1237. }
  1238. }
  1239. }
  1240. // get additional javascript base64 images
  1241. preg_match_all(
  1242. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1243. $html,
  1244. $dimg
  1245. );
  1246. if(isset($dimg[1])){
  1247. for($i=0; $i<count($dimg[1]); $i++){
  1248. $delims = explode(",", $dimg[2][$i]);
  1249. $string =
  1250. $this->fuckhtml
  1251. ->parseJsString(
  1252. $dimg[1][$i]
  1253. );
  1254. foreach($delims as $delim){
  1255. $this->dimg[trim($delim, "'")] = $string;
  1256. }
  1257. }
  1258. }
  1259. }
  1260. private function scrape_imagearr($html){
  1261. // get image links arrays
  1262. preg_match_all(
  1263. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1264. $html,
  1265. $image_arr
  1266. );
  1267. $this->image_arr = [];
  1268. if(isset($image_arr[1])){
  1269. for($i=0; $i<count($image_arr[1]); $i++){
  1270. $original =
  1271. $this->fuckhtml
  1272. ->parseJsString(
  1273. $image_arr[5][$i]
  1274. );
  1275. if(
  1276. preg_match(
  1277. '/^x-raw-image/',
  1278. $original
  1279. )
  1280. ){
  1281. // only add thumbnail, google doesnt have OG resolution
  1282. $this->image_arr[$image_arr[1][$i]] = [
  1283. [
  1284. "url" =>
  1285. $this->unshit_thumb(
  1286. $this->fuckhtml
  1287. ->parseJsString(
  1288. $image_arr[2][$i]
  1289. )
  1290. ),
  1291. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1292. "height" => (int)$image_arr[6][$i]
  1293. ]
  1294. ];
  1295. continue;
  1296. }
  1297. $this->image_arr[$image_arr[1][$i]] =
  1298. [
  1299. [
  1300. "url" => $original,
  1301. "width" => (int)$image_arr[7][$i],
  1302. "height" => (int)$image_arr[6][$i]
  1303. ],
  1304. [
  1305. "url" =>
  1306. $this->unshit_thumb(
  1307. $this->fuckhtml
  1308. ->parseJsString(
  1309. $image_arr[2][$i]
  1310. )
  1311. ),
  1312. "width" => (int)$image_arr[4][$i],
  1313. "height" => (int)$image_arr[3][$i]
  1314. ]
  1315. ];
  1316. }
  1317. }
  1318. }
  1319. private function getdimg($dimg){
  1320. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1321. }
  1322. private function unshit_thumb($url, $get_bigger_res = false){
  1323. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1324. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1325. $parts = parse_url($url);
  1326. if(
  1327. isset($parts["host"]) &&
  1328. preg_match(
  1329. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1330. $parts["host"]
  1331. )
  1332. ){
  1333. parse_str($parts["query"], $params);
  1334. if(isset($params["q"])){
  1335. if($get_bigger_res){
  1336. // this method doesnt always work, but does work for wiki thumbnails
  1337. return
  1338. "https://" . $parts["host"] . "/images?q=tbn:" .
  1339. $this->base64url_encode(
  1340. substr(
  1341. $this->base64url_decode(
  1342. explode(
  1343. ":",
  1344. $params["q"])[1]
  1345. ),
  1346. 0,
  1347. 29
  1348. )
  1349. );
  1350. }else{
  1351. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1352. }
  1353. }
  1354. }
  1355. return $url;
  1356. }
  1357. private function parsestyles(){
  1358. $styles = [];
  1359. $style_div =
  1360. $this->fuckhtml
  1361. ->getElementsByTagName(
  1362. "style"
  1363. );
  1364. $raw_styles = "";
  1365. foreach($style_div as $style){
  1366. $raw_styles .= $style["innerHTML"];
  1367. }
  1368. // filter out media/keyframe queries
  1369. $raw_styles =
  1370. preg_replace(
  1371. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1372. "",
  1373. $raw_styles
  1374. );
  1375. // get styles
  1376. preg_match_all(
  1377. '/(.+?){([\S\s]*?)}/',
  1378. $raw_styles,
  1379. $matches
  1380. );
  1381. for($i=0; $i<count($matches[1]); $i++){
  1382. // get style values
  1383. preg_match_all(
  1384. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1385. $matches[2][$i],
  1386. $values_regex
  1387. );
  1388. $values = [];
  1389. for($k=0; $k<count($values_regex[1]); $k++){
  1390. $values[trim($values_regex[1][$k])] =
  1391. strtolower(trim($values_regex[2][$k]));
  1392. }
  1393. $names = explode(",", $matches[1][$i]);
  1394. // h1,h2,h3 will each get their own array index
  1395. foreach($names as $name){
  1396. $name = trim($name, "}\t\n\r\0\x0B");
  1397. foreach($values as $key => $value){
  1398. $styles[$name][$key] = $value;
  1399. }
  1400. }
  1401. }
  1402. foreach($styles as $key => $values){
  1403. $styles[$key]["_c"] = count($values);
  1404. }
  1405. $this->styles = $styles;
  1406. // get CSS colors
  1407. $this->css_colors = [];
  1408. if(isset($this->styles[":root"])){
  1409. foreach($this->styles[":root"] as $key => $value){
  1410. $this->css_colors[$value] = strtolower($key);
  1411. }
  1412. }
  1413. }
  1414. private function getstyle($styles){
  1415. $styles["_c"] = count($styles);
  1416. foreach($this->styles as $style_key => $style_values){
  1417. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1418. $style_key =
  1419. explode(" ", $style_key);
  1420. $style_key = $style_key[count($style_key) - 1];
  1421. return
  1422. ltrim(
  1423. str_replace(
  1424. [".", "#"],
  1425. " ",
  1426. $style_key
  1427. )
  1428. );
  1429. }
  1430. }
  1431. return false;
  1432. }
  1433. private function getcolorvar($color){
  1434. if(isset($this->css_colors[$color])){
  1435. return $this->css_colors[$color];
  1436. }
  1437. return null;
  1438. }
  1439. private function unshiturl($url, $return_size = false){
  1440. // decode
  1441. $url =
  1442. $this->fuckhtml
  1443. ->getTextContent(
  1444. $url
  1445. );
  1446. $url_parts = parse_url($url);
  1447. if(isset($url_parts["query"])){
  1448. parse_str($url_parts["query"], $query);
  1449. }else{
  1450. $query = [];
  1451. }
  1452. if(
  1453. !isset(
  1454. $url_parts["host"]
  1455. ) ||
  1456. stripos($url_parts["host"], "google.") !== false
  1457. ){
  1458. // no host, we have a tracking url
  1459. if(isset($query["imgurl"])){
  1460. $url = $query["imgurl"];
  1461. }
  1462. elseif(isset($query["q"])){
  1463. $url = $query["q"];
  1464. }
  1465. }
  1466. // rewrite URLs to remove extra tracking parameters
  1467. $domain = parse_url($url, PHP_URL_HOST);
  1468. if(
  1469. preg_match(
  1470. '/wikipedia.org$/',
  1471. $domain
  1472. )
  1473. ){
  1474. // rewrite wikipedia mobile URLs to desktop
  1475. $url =
  1476. $this->replacedomain(
  1477. $url,
  1478. preg_replace(
  1479. '/([a-z0-9]+)(\.m\.)/',
  1480. '$1.',
  1481. $domain
  1482. )
  1483. );
  1484. }
  1485. elseif(
  1486. preg_match(
  1487. '/imdb\.com$|youtube\.[^.]+$/',
  1488. $domain
  1489. )
  1490. ){
  1491. // rewrite imdb and youtube mobile URLs too
  1492. $url =
  1493. $this->replacedomain(
  1494. $url,
  1495. preg_replace(
  1496. '/^m\./',
  1497. "",
  1498. $domain
  1499. )
  1500. );
  1501. }
  1502. elseif(
  1503. preg_match(
  1504. '/play\.google\.[^.]+$/',
  1505. $domain
  1506. )
  1507. ){
  1508. // remove referrers from play.google.com
  1509. $u_query = parse_url($url, PHP_URL_QUERY);
  1510. if($u_query !== null){
  1511. parse_str($u_query, $u_query);
  1512. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  1513. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  1514. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  1515. $query = http_build_query($query);
  1516. $url =
  1517. str_replace(
  1518. $u_query,
  1519. $u_query,
  1520. $url
  1521. );
  1522. }
  1523. }
  1524. elseif(
  1525. preg_match(
  1526. '/twitter\.com$/',
  1527. $domain
  1528. )
  1529. ){
  1530. // remove more referrers from twitter.com
  1531. $u_query = parse_url($url, PHP_URL_QUERY);
  1532. if($u_query !== null){
  1533. parse_str($u_query, $u_query);
  1534. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  1535. $u_query = http_build_query($u_query);
  1536. $url =
  1537. str_replace(
  1538. $oldquery,
  1539. $u_query,
  1540. $url
  1541. );
  1542. }
  1543. }
  1544. elseif(
  1545. preg_match(
  1546. '/maps\.google\.[^.]+/',
  1547. $domain
  1548. )
  1549. ){
  1550. if(stripos($url, "maps?") !== false){
  1551. $u_query = parse_url($url, PHP_URL_QUERY);
  1552. if($u_query !== null){
  1553. parse_str($u_query, $u_query);
  1554. if(isset($u_query["daddr"])){
  1555. $url =
  1556. "https://maps.google.com/maps?daddr=" .
  1557. urlencode($u_query["daddr"]);
  1558. }
  1559. }
  1560. }
  1561. }
  1562. if($return_size){
  1563. return [
  1564. "url" => $url,
  1565. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  1566. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  1567. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  1568. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  1569. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  1570. ];
  1571. }
  1572. return $url;
  1573. }
  1574. private function replacedomain($url, $domain){
  1575. return
  1576. preg_replace(
  1577. '/(https?:\/\/)([^\/]+)/',
  1578. '$1' . $domain,
  1579. $url
  1580. );
  1581. }
  1582. private function titledots($title){
  1583. return trim($title, " .\t\n\r\0\x0B…");
  1584. }
  1585. private function hms2int($time){
  1586. $parts = explode(":", $time, 3);
  1587. $time = 0;
  1588. if(count($parts) === 3){
  1589. // hours
  1590. $time = $time + ((int)$parts[0] * 3600);
  1591. array_shift($parts);
  1592. }
  1593. if(count($parts) === 2){
  1594. // minutes
  1595. $time = $time + ((int)$parts[0] * 60);
  1596. array_shift($parts);
  1597. }
  1598. // seconds
  1599. $time = $time + (int)$parts[0];
  1600. return $time;
  1601. }
  1602. function base64url_decode($data){
  1603. $b64 = strtr($data, "-_", "+/");
  1604. $pad = strlen($b64) % 4;
  1605. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  1606. return base64_decode($b64);
  1607. }
  1608. function base64url_encode($data){
  1609. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  1610. }
  1611. private function detect_sorry(){
  1612. $captcha_form =
  1613. $this->fuckhtml
  1614. ->getElementById(
  1615. "captcha-form",
  1616. "form"
  1617. );
  1618. if($captcha_form !== false){
  1619. throw new Exception("Google returned a captcha");
  1620. }
  1621. }
  1622. }