google.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  1. <?php
  2. class google{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("google");
  8. }
  9. public function getfilters($page){
  10. $base = [
  11. "country" => [ // gl=<country> (image: cr=countryAF)
  12. "display" => "Country",
  13. "option" => [
  14. "any" => "Instance's country",
  15. "af" => "Afghanistan",
  16. "al" => "Albania",
  17. "dz" => "Algeria",
  18. "as" => "American Samoa",
  19. "ad" => "Andorra",
  20. "ao" => "Angola",
  21. "ai" => "Anguilla",
  22. "aq" => "Antarctica",
  23. "ag" => "Antigua and Barbuda",
  24. "ar" => "Argentina",
  25. "am" => "Armenia",
  26. "aw" => "Aruba",
  27. "au" => "Australia",
  28. "at" => "Austria",
  29. "az" => "Azerbaijan",
  30. "bs" => "Bahamas",
  31. "bh" => "Bahrain",
  32. "bd" => "Bangladesh",
  33. "bb" => "Barbados",
  34. "by" => "Belarus",
  35. "be" => "Belgium",
  36. "bz" => "Belize",
  37. "bj" => "Benin",
  38. "bm" => "Bermuda",
  39. "bt" => "Bhutan",
  40. "bo" => "Bolivia",
  41. "ba" => "Bosnia and Herzegovina",
  42. "bw" => "Botswana",
  43. "bv" => "Bouvet Island",
  44. "br" => "Brazil",
  45. "io" => "British Indian Ocean Territory",
  46. "bn" => "Brunei Darussalam",
  47. "bg" => "Bulgaria",
  48. "bf" => "Burkina Faso",
  49. "bi" => "Burundi",
  50. "kh" => "Cambodia",
  51. "cm" => "Cameroon",
  52. "ca" => "Canada",
  53. "cv" => "Cape Verde",
  54. "ky" => "Cayman Islands",
  55. "cf" => "Central African Republic",
  56. "td" => "Chad",
  57. "cl" => "Chile",
  58. "cn" => "China",
  59. "cx" => "Christmas Island",
  60. "cc" => "Cocos (Keeling) Islands",
  61. "co" => "Colombia",
  62. "km" => "Comoros",
  63. "cg" => "Congo",
  64. "cd" => "Congo, the Democratic Republic",
  65. "ck" => "Cook Islands",
  66. "cr" => "Costa Rica",
  67. "ci" => "Cote D'ivoire",
  68. "hr" => "Croatia",
  69. "cu" => "Cuba",
  70. "cy" => "Cyprus",
  71. "cz" => "Czech Republic",
  72. "dk" => "Denmark",
  73. "dj" => "Djibouti",
  74. "dm" => "Dominica",
  75. "do" => "Dominican Republic",
  76. "ec" => "Ecuador",
  77. "eg" => "Egypt",
  78. "sv" => "El Salvador",
  79. "gq" => "Equatorial Guinea",
  80. "er" => "Eritrea",
  81. "ee" => "Estonia",
  82. "et" => "Ethiopia",
  83. "fk" => "Falkland Islands (Malvinas)",
  84. "fo" => "Faroe Islands",
  85. "fj" => "Fiji",
  86. "fi" => "Finland",
  87. "fr" => "France",
  88. "gf" => "French Guiana",
  89. "pf" => "French Polynesia",
  90. "tf" => "French Southern Territories",
  91. "ga" => "Gabon",
  92. "gm" => "Gambia",
  93. "ge" => "Georgia",
  94. "de" => "Germany",
  95. "gh" => "Ghana",
  96. "gi" => "Gibraltar",
  97. "gr" => "Greece",
  98. "gl" => "Greenland",
  99. "gd" => "Grenada",
  100. "gp" => "Guadeloupe",
  101. "gu" => "Guam",
  102. "gt" => "Guatemala",
  103. "gn" => "Guinea",
  104. "gw" => "Guinea-Bissau",
  105. "gy" => "Guyana",
  106. "ht" => "Haiti",
  107. "hm" => "Heard Island and Mcdonald Islands",
  108. "va" => "Holy See (Vatican City State)",
  109. "hn" => "Honduras",
  110. "hk" => "Hong Kong",
  111. "hu" => "Hungary",
  112. "is" => "Iceland",
  113. "in" => "India",
  114. "id" => "Indonesia",
  115. "ir" => "Iran, Islamic Republic",
  116. "iq" => "Iraq",
  117. "ie" => "Ireland",
  118. "il" => "Israel",
  119. "it" => "Italy",
  120. "jm" => "Jamaica",
  121. "jp" => "Japan",
  122. "jo" => "Jordan",
  123. "kz" => "Kazakhstan",
  124. "ke" => "Kenya",
  125. "ki" => "Kiribati",
  126. "kp" => "Korea, Democratic People's Republic",
  127. "kr" => "Korea, Republic",
  128. "kw" => "Kuwait",
  129. "kg" => "Kyrgyzstan",
  130. "la" => "Lao People's Democratic Republic",
  131. "lv" => "Latvia",
  132. "lb" => "Lebanon",
  133. "ls" => "Lesotho",
  134. "lr" => "Liberia",
  135. "ly" => "Libyan Arab Jamahiriya",
  136. "li" => "Liechtenstein",
  137. "lt" => "Lithuania",
  138. "lu" => "Luxembourg",
  139. "mo" => "Macao",
  140. "mk" => "Macedonia, the Former Yugosalv Republic",
  141. "mg" => "Madagascar",
  142. "mw" => "Malawi",
  143. "my" => "Malaysia",
  144. "mv" => "Maldives",
  145. "ml" => "Mali",
  146. "mt" => "Malta",
  147. "mh" => "Marshall Islands",
  148. "mq" => "Martinique",
  149. "mr" => "Mauritania",
  150. "mu" => "Mauritius",
  151. "yt" => "Mayotte",
  152. "mx" => "Mexico",
  153. "fm" => "Micronesia, Federated States",
  154. "md" => "Moldova, Republic",
  155. "mc" => "Monaco",
  156. "mn" => "Mongolia",
  157. "ms" => "Montserrat",
  158. "ma" => "Morocco",
  159. "mz" => "Mozambique",
  160. "mm" => "Myanmar",
  161. "na" => "Namibia",
  162. "nr" => "Nauru",
  163. "np" => "Nepal",
  164. "nl" => "Netherlands",
  165. "an" => "Netherlands Antilles",
  166. "nc" => "New Caledonia",
  167. "nz" => "New Zealand",
  168. "ni" => "Nicaragua",
  169. "ne" => "Niger",
  170. "ng" => "Nigeria",
  171. "nu" => "Niue",
  172. "nf" => "Norfolk Island",
  173. "mp" => "Northern Mariana Islands",
  174. "no" => "Norway",
  175. "om" => "Oman",
  176. "pk" => "Pakistan",
  177. "pw" => "Palau",
  178. "ps" => "Palestinian Territory, Occupied",
  179. "pa" => "Panama",
  180. "pg" => "Papua New Guinea",
  181. "py" => "Paraguay",
  182. "pe" => "Peru",
  183. "ph" => "Philippines",
  184. "pn" => "Pitcairn",
  185. "pl" => "Poland",
  186. "pt" => "Portugal",
  187. "pr" => "Puerto Rico",
  188. "qa" => "Qatar",
  189. "re" => "Reunion",
  190. "ro" => "Romania",
  191. "ru" => "Russian Federation",
  192. "rw" => "Rwanda",
  193. "sh" => "Saint Helena",
  194. "kn" => "Saint Kitts and Nevis",
  195. "lc" => "Saint Lucia",
  196. "pm" => "Saint Pierre and Miquelon",
  197. "vc" => "Saint Vincent and the Grenadines",
  198. "ws" => "Samoa",
  199. "sm" => "San Marino",
  200. "st" => "Sao Tome and Principe",
  201. "sa" => "Saudi Arabia",
  202. "sn" => "Senegal",
  203. "cs" => "Serbia and Montenegro",
  204. "sc" => "Seychelles",
  205. "sl" => "Sierra Leone",
  206. "sg" => "Singapore",
  207. "sk" => "Slovakia",
  208. "si" => "Slovenia",
  209. "sb" => "Solomon Islands",
  210. "so" => "Somalia",
  211. "za" => "South Africa",
  212. "gs" => "South Georgia and the South Sandwich Islands",
  213. "es" => "Spain",
  214. "lk" => "Sri Lanka",
  215. "sd" => "Sudan",
  216. "sr" => "Suriname",
  217. "sj" => "Svalbard and Jan Mayen",
  218. "sz" => "Swaziland",
  219. "se" => "Sweden",
  220. "ch" => "Switzerland",
  221. "sy" => "Syrian Arab Republic",
  222. "tw" => "Taiwan, Province of China",
  223. "tj" => "Tajikistan",
  224. "tz" => "Tanzania, United Republic",
  225. "th" => "Thailand",
  226. "tl" => "Timor-Leste",
  227. "tg" => "Togo",
  228. "tk" => "Tokelau",
  229. "to" => "Tonga",
  230. "tt" => "Trinidad and Tobago",
  231. "tn" => "Tunisia",
  232. "tr" => "Turkey",
  233. "tm" => "Turkmenistan",
  234. "tc" => "Turks and Caicos Islands",
  235. "tv" => "Tuvalu",
  236. "ug" => "Uganda",
  237. "ua" => "Ukraine",
  238. "ae" => "United Arab Emirates",
  239. "uk" => "United Kingdom",
  240. "us" => "United States",
  241. "um" => "United States Minor Outlying Islands",
  242. "uy" => "Uruguay",
  243. "uz" => "Uzbekistan",
  244. "vu" => "Vanuatu",
  245. "ve" => "Venezuela",
  246. "vn" => "Viet Nam",
  247. "vg" => "Virgin Islands, British",
  248. "vi" => "Virgin Islands, U.S.",
  249. "wf" => "Wallis and Futuna",
  250. "eh" => "Western Sahara",
  251. "ye" => "Yemen",
  252. "zm" => "Zambia",
  253. "zw" => "Zimbabwe"
  254. ]
  255. ],
  256. "nsfw" => [
  257. "display" => "NSFW",
  258. "option" => [
  259. "yes" => "Yes", // safe=active
  260. "no" => "No" // safe=off
  261. ]
  262. ]
  263. ];
  264. switch($page){
  265. case "web":
  266. return array_merge(
  267. $base,
  268. [
  269. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  270. "display" => "Language",
  271. "option" => [
  272. "any" => "Any language",
  273. "ar" => "Arabic",
  274. "bg" => "Bulgarian",
  275. "ca" => "Catalan",
  276. "cs" => "Czech",
  277. "da" => "Danish",
  278. "de" => "German",
  279. "el" => "Greek",
  280. "en" => "English",
  281. "es" => "Spanish",
  282. "et" => "Estonian",
  283. "fi" => "Finnish",
  284. "fr" => "French",
  285. "hr" => "Croatian",
  286. "hu" => "Hungarian",
  287. "id" => "Indonesian",
  288. "is" => "Icelandic",
  289. "it" => "Italian",
  290. "iw" => "Hebrew",
  291. "ja" => "Japanese",
  292. "ko" => "Korean",
  293. "lt" => "Lithuanian",
  294. "lv" => "Latvian",
  295. "nl" => "Dutch",
  296. "no" => "Norwegian",
  297. "pl" => "Polish",
  298. "pt" => "Portuguese",
  299. "ro" => "Romanian",
  300. "ru" => "Russian",
  301. "sk" => "Slovak",
  302. "sl" => "Slovenian",
  303. "sr" => "Serbian",
  304. "sv" => "Swedish",
  305. "tr" => "Turkish",
  306. "zh-CN" => "Chinese (Simplified)",
  307. "zh-TW" => "Chinese (Traditional)"
  308. ]
  309. ],
  310. "newer" => [ // tbs
  311. "display" => "Newer than",
  312. "option" => "_DATE"
  313. ],
  314. "older" => [
  315. "display" => "Older than",
  316. "option" => "_DATE"
  317. ],
  318. "spellcheck" => [
  319. "display" => "Spellcheck",
  320. "option" => [
  321. "yes" => "Yes",
  322. "no" => "No"
  323. ]
  324. ]
  325. ]
  326. );
  327. break;
  328. case "images":
  329. return array_merge(
  330. $base,
  331. [
  332. "time" => [ // tbs=qdr:<time>
  333. "display" => "Time posted",
  334. "option" => [
  335. "any" => "Any time",
  336. "d" => "Past 24 hours",
  337. "w" => "Past week",
  338. "m" => "Past month",
  339. "y" => "Past year"
  340. ]
  341. ],
  342. "size" => [ // imgsz
  343. "display" => "Size",
  344. "option" => [
  345. "any" => "Any size",
  346. "l" => "Large",
  347. "m" => "Medium",
  348. "i" => "Icon",
  349. "qsvga" => "Larger than 400x300",
  350. "vga" => "Larger than 640x480",
  351. "svga" => "Larger than 800x600",
  352. "xga" => "Larger than 1024x768",
  353. "2mp" => "Larger than 2MP",
  354. "4mp" => "Larger than 4MP",
  355. "6mp" => "Larger than 6MP",
  356. "8mp" => "Larger than 8MP",
  357. "10mp" => "Larger than 10MP",
  358. "12mp" => "Larger than 12MP",
  359. "15mp" => "Larger than 15MP",
  360. "20mp" => "Larger than 20MP",
  361. "40mp" => "Larger than 40MP",
  362. "70mp" => "Larger than 70MP"
  363. ]
  364. ],
  365. "ratio" => [ // imgar
  366. "display" => "Aspect ratio",
  367. "option" => [
  368. "any" => "Any ratio",
  369. "t|xt" => "Tall",
  370. "s" => "Square",
  371. "w" => "Wide",
  372. "xw" => "Panoramic"
  373. ]
  374. ],
  375. "color" => [ // imgc
  376. "display" => "Color",
  377. "option" => [
  378. "any" => "Any color",
  379. "color" => "Full color",
  380. "bnw" => "Black & white",
  381. "trans" => "Transparent",
  382. // from here, imgcolor
  383. "red" => "Red",
  384. "orange" => "Orange",
  385. "yellow" => "Yellow",
  386. "green" => "Green",
  387. "teal" => "Teal",
  388. "blue" => "Blue",
  389. "purple" => "Purple",
  390. "pink" => "Pink",
  391. "white" => "White",
  392. "gray" => "Gray",
  393. "black" => "Black",
  394. "brown" => "Brown"
  395. ]
  396. ],
  397. "type" => [ // tbs=itp:<type>
  398. "display" => "Type",
  399. "option" => [
  400. "any" => "Any type",
  401. "clipart" => "Clip Art",
  402. "lineart" => "Line Drawing",
  403. "animated" => "Animated"
  404. ]
  405. ],
  406. "format" => [ // as_filetype
  407. "display" => "Format",
  408. "option" => [
  409. "any" => "Any format",
  410. "jpg" => "JPG",
  411. "gif" => "GIF",
  412. "png" => "PNG",
  413. "bmp" => "BMP",
  414. "svg" => "SVG",
  415. "webp" => "WEBP",
  416. "ico" => "ICO",
  417. "craw" => "RAW"
  418. ]
  419. ],
  420. "rights" => [ // tbs=sur:<rights>
  421. "display" => "Usage rights",
  422. "option" => [
  423. "any" => "Any license",
  424. "cl" => "Creative Commons licenses",
  425. "ol" => "Commercial & other licenses"
  426. ]
  427. ]
  428. ]
  429. );
  430. break;
  431. case "videos":
  432. return array_merge(
  433. $base,
  434. [
  435. "newer" => [ // tbs
  436. "display" => "Newer than",
  437. "option" => "_DATE"
  438. ],
  439. "older" => [
  440. "display" => "Older than",
  441. "option" => "_DATE"
  442. ],
  443. "duration" => [
  444. "display" => "Duration",
  445. "option" => [
  446. "any" => "Any duration",
  447. "s" => "Short (0-4min)", // tbs=dur:s
  448. "m" => "Medium (4-20min)", // tbs=dur:m
  449. "l" => "Long (20+ min)" // tbs=dur:l
  450. ]
  451. ],
  452. "quality" => [
  453. "display" => "Quality",
  454. "option" => [
  455. "any" => "Any quality",
  456. "h" => "High quality" // tbs=hq:h
  457. ]
  458. ],
  459. "captions" => [
  460. "display" => "Captions",
  461. "option" => [
  462. "any" => "No preference",
  463. "yes" => "Closed captioned" // tbs=cc:1
  464. ]
  465. ]
  466. ]
  467. );
  468. break;
  469. case "news":
  470. return array_merge(
  471. $base,
  472. [
  473. "newer" => [ // tbs
  474. "display" => "Newer than",
  475. "option" => "_DATE"
  476. ],
  477. "older" => [
  478. "display" => "Older than",
  479. "option" => "_DATE"
  480. ],
  481. "sort" => [
  482. "display" => "Sort",
  483. "option" => [
  484. "relevance" => "Relevance",
  485. "date" => "Date" // sbd:1
  486. ]
  487. ]
  488. ]
  489. );
  490. break;
  491. }
  492. }
  493. private function get($proxy, $url, $get = [], $container = null){
  494. if(config::FPLAY_EXTERNAL_ENDPOINT === null){
  495. throw new Exception("This scraper requires 4play, and the instance maintainer did not configure it. Please refer to the documentation for more information");
  496. }
  497. try{
  498. $curlproc = curl_init();
  499. if($get !== []){
  500. $get = http_build_query($get);
  501. $url .= "?" . $get;
  502. }
  503. if($container === null){
  504. $url =
  505. config::FPLAY_EXTERNAL_ENDPOINT .
  506. "?url=" . urlencode($url) .
  507. "&pw=" . urlencode(config::FPLAY_PASSWORD) .
  508. "&container=" . urlencode($container) .
  509. "&proxy=" . urlencode($proxy) .
  510. "&url_match=" . urlencode("&sei=") .
  511. "&fail_url_match=" . urlencode("\/sorry\/");
  512. }else{
  513. $url =
  514. config::FPLAY_EXTERNAL_ENDPOINT .
  515. "?url=" . urlencode($url) .
  516. "&pw=" . urlencode(config::FPLAY_PASSWORD) .
  517. "&container=" . urlencode($container) .
  518. "&proxy=" . urlencode($proxy);
  519. }
  520. $header_out = [];
  521. curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$header_out){
  522. $length = strlen($header);
  523. $header_out[] = trim($header);
  524. return $length;
  525. });
  526. curl_setopt($curlproc, CURLOPT_URL, $url);
  527. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  528. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  529. curl_setopt($curlproc, CURLOPT_HTTPHEADER, [
  530. "User-Agent: " . config::USER_AGENT_FRIENDLY,
  531. "Accept: text/plain"
  532. ]);
  533. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  534. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  535. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  536. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  537. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  538. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  539. //$this->backend->assign_proxy($curlproc, $proxy);
  540. $data = curl_exec($curlproc);
  541. if(curl_errno($curlproc)){
  542. throw new Exception(curl_error($curlproc));
  543. }
  544. curl_close($curlproc);
  545. }catch(Exception $error){
  546. throw new Exception("4play API is currently unreachable. Render server offline (probably) woopsies");
  547. }
  548. // check for failstate
  549. if(
  550. str_contains(
  551. $header_out[0],
  552. "400"
  553. )
  554. ){
  555. throw new Exception("Page renderer returned an error: " . $data);
  556. }
  557. // parse headers
  558. $container = null;
  559. foreach($header_out as $header){
  560. $header = explode(":", $header, 2);
  561. if(strtolower($header[0]) == "x-container-id"){
  562. $container = trim($header[1]);
  563. break;
  564. }
  565. }
  566. if($container === null){
  567. throw new Exception("The 4play API did not return a valid container ID. Make sure your configuration forwards X-Container-ID headers properly");
  568. }
  569. return [
  570. "data" => $data,
  571. "container" => $container
  572. ];
  573. }
  574. public function web($get){
  575. if($get["npt"]){
  576. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  577. $get = json_decode($get, true);
  578. $container = $get["c"];
  579. $get = $get["u"];
  580. $data =
  581. $this->get(
  582. $proxy,
  583. "https://www.google.com" . $get,
  584. [],
  585. $container
  586. );
  587. $html = &$data["data"];
  588. $container = &$data["container"];
  589. }else{
  590. $search = $get["s"];
  591. if(strlen($search) === 0){
  592. throw new Exception("Search term is empty!");
  593. }
  594. /*
  595. $session = $this->backend->session_get();
  596. if(config::FPLAY_ENABLE_API === false){
  597. throw new Exception("This scraper requires the 4play session generator. Check the documentation for more information");
  598. }
  599. if($session === false){
  600. throw new Exception("Session database is empty");
  601. }
  602. $proxy = &$session["proxy"];
  603. $headers = &$session["headers"];
  604. */
  605. $proxy = $this->backend->get_ip();
  606. //
  607. // Generate params
  608. //
  609. $params = [
  610. "q" => $get["s"],
  611. "hl" => "en",
  612. "udm" => 14
  613. ];
  614. // country
  615. if($get["country"] != "any"){
  616. $params["gl"] = $country;
  617. }
  618. // nsfw
  619. $params["safe"] = $get["nsfw"] == "yes" ? "off" : "active";
  620. // language
  621. if($get["lang"] != "any"){
  622. $params["lr"] = "lang_" . $lang;
  623. }
  624. // generate tbs
  625. $tbs = [];
  626. // get date
  627. $get["older"] = $get["older"] === false ? null : date("m/d/Y", $older);
  628. $get["newer"] = $get["newer"] === false ? null : date("m/d/Y", $newer);
  629. if(
  630. $get["older"] !== null ||
  631. $get["newer"] !== null
  632. ){
  633. $tbs["cdr"] = "1";
  634. $tbs["cd_min"] = $get["newer"];
  635. $tbs["cd_max"] = $get["older"];
  636. }
  637. // spellcheck filter
  638. if($get["spellcheck"] == "no"){
  639. $params["nfpr"] = "1";
  640. }
  641. if(count($tbs) !== 0){
  642. $params["tbs"] = "";
  643. foreach($tbs as $key => $value){
  644. $params["tbs"] .= $key . ":" . $value . ",";
  645. }
  646. $params["tbs"] = rtrim($params["tbs"], ",");
  647. }
  648. $data = $this->get(
  649. $proxy,
  650. "https://www.google.com/search",
  651. $params,
  652. null
  653. );
  654. $html = &$data["data"];
  655. $container = &$data["container"];
  656. //file_put_contents("scraper/google-jp.html", $html);
  657. //$html = file_get_contents("scraper/google-softcaptcha.html");
  658. }
  659. $out = [
  660. "status" => "ok",
  661. "spelling" => [
  662. "type" => "no_correction",
  663. "using" => null,
  664. "correction" => null
  665. ],
  666. "npt" => null,
  667. "answer" => [],
  668. "web" => [],
  669. "image" => [],
  670. "video" => [],
  671. "news" => [],
  672. "related" => []
  673. ];
  674. $this->fuckhtml->load($html);
  675. // init
  676. $this->parsestyles();
  677. $this->detect_sorry();
  678. $this->scrape_dimg($html);
  679. $this->scrape_imagearr($html);
  680. $content =
  681. $this->fuckhtml
  682. ->getElementById(
  683. "rso"
  684. );
  685. if($content === null){
  686. throw new Exception("Failed to grep search div");
  687. }
  688. // get encoded links
  689. $encoded_links =
  690. explode(
  691. "function(){var m={",
  692. $html,
  693. 2
  694. );
  695. if(count($encoded_links) === 2){
  696. $encoded_links =
  697. json_decode(
  698. $this->fuckhtml
  699. ->extract_json(
  700. "{" . $encoded_links[1]
  701. ),
  702. true
  703. );
  704. }else{
  705. $encoded_links = [];
  706. }
  707. // get the "did you mean" bullshit
  708. $taw =
  709. $this->fuckhtml
  710. ->getElementById("taw");
  711. if($taw !== null){
  712. $this->fuckhtml->load($taw);
  713. $as =
  714. $this->fuckhtml
  715. ->getElementsByTagName("a");
  716. $links = [];
  717. foreach($as as $a){
  718. $href =
  719. $this->fuckhtml
  720. ->getTextContent(
  721. $a["attributes"]["href"]
  722. );
  723. if(preg_match('/^\/search\?/', $href)){
  724. $links[] =
  725. $this->fuckhtml
  726. ->getTextContent(
  727. $a
  728. );
  729. }
  730. }
  731. switch(count($links)){
  732. case 1:
  733. $out["spelling"] = [
  734. "type" => "including",
  735. "using" => $params["q"],
  736. "correction" => $links[0]
  737. ];
  738. break;
  739. case 2:
  740. $out["spelling"] = [
  741. "type" => "including",
  742. "using" => $links[0],
  743. "correction" => $links[1]
  744. ];
  745. break;
  746. }
  747. // reset
  748. $this->fuckhtml->load($html);
  749. }
  750. $bottom =
  751. $this->fuckhtml
  752. ->getElementById("botstuff");
  753. if($bottom !== null){
  754. $this->fuckhtml->load($bottom);
  755. // get next page thingy
  756. $npt =
  757. $this->fuckhtml
  758. ->getElementById(
  759. "pnnext"
  760. );
  761. if($npt !== false){
  762. $out["npt"] =
  763. $this->backend->store(
  764. json_encode([
  765. "u" =>
  766. $this->fuckhtml
  767. ->getTextContent(
  768. $npt["attributes"]["href"]
  769. ),
  770. "c" => $container
  771. ]),
  772. "web",
  773. $proxy
  774. );
  775. }
  776. // get lumendatabase notices
  777. $notices = [];
  778. $as =
  779. $this->fuckhtml
  780. ->getElementsByTagName("a");
  781. foreach($as as $a){
  782. if(!isset($a["attributes"]["href"])){ continue; }
  783. $link =
  784. $this->fuckhtml
  785. ->getTextContent(
  786. $a["attributes"]["href"]
  787. );
  788. if(
  789. preg_match(
  790. '/^https?:\/\/lumendatabase\.org/',
  791. $link
  792. )
  793. ){
  794. $notices[] = $link;
  795. }
  796. }
  797. // push notices to dom
  798. if(count($notices) !== 0){
  799. $notices_dom = [];
  800. $i = 0;
  801. $c = count($notices);
  802. $notices_dom = [
  803. [
  804. "type" => "text",
  805. "value" => "Google removed results from this page, but you can get them anyways by consulting these takedown notices: "
  806. ]
  807. ];
  808. foreach($notices as $n){
  809. $i++;
  810. $notices_dom[] = [
  811. "type" => "link",
  812. "value" => "Notice #{$i}",
  813. "url" => $n
  814. ];
  815. if($i !== count($notices)){
  816. $notices_dom[] = [
  817. "type" => "text",
  818. "value" => ", "
  819. ];
  820. }
  821. }
  822. $out["answer"][] = [
  823. "title" => "LumenDB notices",
  824. "description" => $notices_dom,
  825. "url" => null,
  826. "thumb" => null,
  827. "table" => [],
  828. "sublink" => []
  829. ];
  830. }
  831. }
  832. $this->fuckhtml->load($content);
  833. // get search results
  834. $results =
  835. $this->fuckhtml
  836. ->getElementsByAttributeName(
  837. "data-rpos",
  838. "div"
  839. );
  840. foreach($results as $result){
  841. $this->fuckhtml->load($result);
  842. // get title
  843. $title =
  844. $this->fuckhtml
  845. ->getElementsByTagName("h3");
  846. if(count($title) === 0){
  847. // should not happen
  848. continue;
  849. }
  850. $title =
  851. $this->titledots(
  852. $this->fuckhtml
  853. ->getTextContent(
  854. $title[0]
  855. )
  856. );
  857. $url =
  858. $this->fuckhtml
  859. ->getElementsByTagName(
  860. "a"
  861. );
  862. if(
  863. count($url) === 0 ||
  864. !isset($url[0]["attributes"]["href"])
  865. ){
  866. continue;
  867. }
  868. $url =
  869. $this->fuckhtml
  870. ->getTextContent(
  871. $url[0]["attributes"]["href"]
  872. );
  873. if(
  874. preg_match(
  875. '/^\/goto\?url/',
  876. $url
  877. )
  878. ){
  879. // encrypted url detected
  880. // get reference div
  881. $refdivs =
  882. $this->fuckhtml
  883. ->getElementsByAttributeName(
  884. "id",
  885. "div"
  886. );
  887. foreach($refdivs as $r){
  888. if(
  889. preg_match(
  890. '/^atritem-/',
  891. $r["attributes"]["id"]
  892. ) &&
  893. isset($r["attributes"]["jsdata"])
  894. ){
  895. $ref = explode(";", $r["attributes"]["jsdata"]);
  896. $ref = $ref[count($ref) - 1];
  897. if(isset($encoded_links[$ref][32][3][0])){
  898. $url = $encoded_links[$ref][32][3][0];
  899. }
  900. break;
  901. }
  902. }
  903. }
  904. // get citation text. contains table elements
  905. $table = [];
  906. $date = null;
  907. $cite =
  908. $this->fuckhtml
  909. ->getElementsByTagName("cite");
  910. if(isset($cite[0])){
  911. $texts =
  912. explode(
  913. " · ",
  914. $this->fuckhtml
  915. ->getTextContent($cite[0])
  916. );
  917. foreach($texts as $text){
  918. $text = trim($text);
  919. preg_match(
  920. '/([0-9+kmb,.]+) +(likes?|views?|comments?|followers?|subscribers?|(?:hours?|days?|months?|years?) +ago)/i',
  921. $text,
  922. $probe
  923. );
  924. if(isset($probe[1])){
  925. // is it date?
  926. if(
  927. preg_match(
  928. '/ago$/i',
  929. $text
  930. )
  931. ){
  932. $time = strtotime($text);
  933. if($time !== false){ $date = $time; }
  934. continue;
  935. }
  936. // anything else?
  937. $table[ucfirst($probe[2])] = trim($probe[1]);
  938. }
  939. }
  940. }
  941. // get description/image wrapper
  942. $desc_img_wrappers =
  943. $this->fuckhtml
  944. ->getElementsByAttributeName(
  945. "data-sncf",
  946. "div"
  947. );
  948. $description = null;
  949. $thumb = [
  950. "url" => null,
  951. "ratio" => null
  952. ];
  953. foreach($desc_img_wrappers as $wrapper){
  954. $this->fuckhtml->load($wrapper);
  955. // probe for image
  956. $image =
  957. $this->fuckhtml
  958. ->getElementsByTagName(
  959. "img"
  960. );
  961. if(count($image) !== 0){
  962. // handle thumbnails
  963. $i = null;
  964. if(isset($image[0]["attributes"]["id"])){
  965. $i = $this->getdimg($image[0]["attributes"]["id"]);
  966. }
  967. if($i !== null){
  968. $thumb = [
  969. "ratio" => "1:1",
  970. "url" => $i
  971. ];
  972. }
  973. }else{
  974. if($description === null){
  975. // parse it as description
  976. $description =
  977. $this->titledots(
  978. $this->fuckhtml
  979. ->getTextContent(
  980. $wrapper
  981. )
  982. );
  983. }else{
  984. // parse table elements
  985. $ts =
  986. explode(
  987. " · ",
  988. $this->fuckhtml
  989. ->getTextContent(
  990. $wrapper
  991. )
  992. );
  993. foreach($ts as $t){
  994. $t = trim(html_entity_decode($t));
  995. if(preg_match("/\p{Sc}/u", $t)){
  996. $table["Price"] = $t;
  997. continue;
  998. }
  999. if(str_contains($t, "stock")){
  1000. $table["Stock"] = $t;
  1001. continue;
  1002. }
  1003. if(str_contains($t, "return")){
  1004. $table["Returns"] = $t;
  1005. continue;
  1006. }
  1007. if(str_contains($t, "delivery")){
  1008. $table["Delivery"] = $t;
  1009. continue;
  1010. }
  1011. // 5.0(590,004)
  1012. if(preg_match('/^([0-9kmb\/%.,]+) *(\([0-9kmb,.]+\))?/', $t, $match)){
  1013. if(isset($match[2])){
  1014. $table["Ratings"] = "{$match[1]} {$match[2]}";
  1015. continue;
  1016. }
  1017. }
  1018. if(str_contains($t, "rating")){
  1019. $table["Ratings"] = $t;
  1020. continue;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. }
  1026. // extract date from description
  1027. // Nov 6, 2017 — Keira Hagai from Jak and Daxter. I took off her belt/suspenders/goggles. Originally posted by DemonBoy on Models Resource, and edited and posted to Sketchfab
  1028. if($date === null){
  1029. $date_probe =
  1030. explode(
  1031. " — ",
  1032. $description,
  1033. 2
  1034. );
  1035. if(count($date_probe) === 2){
  1036. $time = strtotime($date_probe[0]);
  1037. if($time !== false){
  1038. $description = $date_probe[1];
  1039. $date = $time;
  1040. }
  1041. }
  1042. }
  1043. $sublinks = [];
  1044. $out["web"][] = [
  1045. "title" => $title,
  1046. "description" => $description,
  1047. "url" => $url,
  1048. "date" => $date,
  1049. "type" => "web",
  1050. "thumb" => $thumb,
  1051. "sublink" => $sublinks,
  1052. "table" => $table
  1053. ];
  1054. }
  1055. return $out;
  1056. }
  1057. public function image($get){
  1058. // generate parameters
  1059. if($get["npt"]){
  1060. [$params, $proxy] =
  1061. $this->backend->get(
  1062. $get["npt"],
  1063. "images"
  1064. );
  1065. $params = json_decode($params, true);
  1066. $container = $params["c"];
  1067. unset($params["c"]);
  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. "start" => 0
  1087. ];
  1088. // country (image search uses cr instead of gl)
  1089. if($country != "any"){
  1090. $params["cr"] = "country" . strtoupper($country);
  1091. }
  1092. // nsfw
  1093. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1094. // generate tbs
  1095. $tbs = [];
  1096. // time
  1097. if($time != "any"){
  1098. $tbs["qdr"] = $time;
  1099. }
  1100. // size
  1101. if($size != "any"){
  1102. $params["imgsz"] = $size;
  1103. }
  1104. // ratio
  1105. if($ratio != "any"){
  1106. $params["imgar"] = $ratio;
  1107. }
  1108. // color
  1109. if($color != "any"){
  1110. if(
  1111. $color == "color" ||
  1112. $color == "trans"
  1113. ){
  1114. $params["imgc"] = $color;
  1115. }elseif($color == "bnw"){
  1116. $params["imgc"] = "gray";
  1117. }else{
  1118. $tbs["ic"] = "specific";
  1119. $tbs["isc"] = $color;
  1120. }
  1121. }
  1122. // type
  1123. if($type != "any"){
  1124. $tbs["itp"] = $type;
  1125. }
  1126. // format
  1127. if($format != "any"){
  1128. $params["as_filetype"] = $format;
  1129. }
  1130. // rights (tbs)
  1131. if($rights != "any"){
  1132. $tbs["sur"] = $rights;
  1133. }
  1134. // append tbs
  1135. if(count($tbs) !== 0){
  1136. $params["tbs"] = "";
  1137. foreach($tbs as $key => $value){
  1138. $params["tbs"] .= $key . ":" . $value . ",";
  1139. }
  1140. $params["tbs"] = rtrim($params["tbs"], ",");
  1141. }
  1142. }
  1143. //$html = file_get_contents("scraper/google.html");
  1144. $data = $this->get(
  1145. $proxy,
  1146. "https://www.google.com/search",
  1147. $params
  1148. );
  1149. $html = &$data["data"];
  1150. $container = &$data["container"];
  1151. $this->fuckhtml->load($html);
  1152. // parse all <style> tags
  1153. $this->parsestyles();
  1154. $this->detect_sorry();
  1155. // get javascript images
  1156. $this->scrape_imagearr($html);
  1157. $out = [
  1158. "status" => "ok",
  1159. "npt" => null,
  1160. "image" => []
  1161. ];
  1162. $images =
  1163. $this->fuckhtml
  1164. ->getElementsByClassName(
  1165. "ivg-i",
  1166. "div"
  1167. );
  1168. foreach($images as $div){
  1169. $this->fuckhtml->load($div);
  1170. $image =
  1171. $this->fuckhtml
  1172. ->getElementsByTagName("img")[0];
  1173. // make sure we dont attempt to show an image we dont have data for
  1174. if(
  1175. isset($div["attributes"]["data-docid"]) &&
  1176. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1177. ){
  1178. $source =
  1179. $this->image_arr[
  1180. $div["attributes"]["data-docid"]
  1181. ];
  1182. }else{
  1183. continue;
  1184. }
  1185. $out["image"][] = [
  1186. "title" =>
  1187. $this->titledots(
  1188. $this->fuckhtml
  1189. ->getTextContent(
  1190. $image["attributes"]["alt"]
  1191. )
  1192. ),
  1193. "source" => $source,
  1194. "url" =>
  1195. $this->fuckhtml
  1196. ->getTextContent(
  1197. $div["attributes"]["data-lpage"]
  1198. )
  1199. ];
  1200. }
  1201. // as usual, no way to check if there is a next page reliably
  1202. if(count($out["image"]) > 50){
  1203. if(!isset($params["start"])){
  1204. $params["start"] = 10;
  1205. }else{
  1206. $params["start"] += 10;
  1207. }
  1208. $out["npt"] =
  1209. $this->backend
  1210. ->store(
  1211. json_encode($params),
  1212. "image",
  1213. $proxy
  1214. );
  1215. }
  1216. return $out;
  1217. }
  1218. public function video($get){
  1219. if($get["npt"]){
  1220. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1221. $params = json_decode($params, true);
  1222. $container = $params["c"];
  1223. unset($params["c"]);
  1224. }else{
  1225. $search = $get["s"];
  1226. $country = $get["country"];
  1227. $nsfw = $get["nsfw"];
  1228. $older = $get["older"];
  1229. $newer = $get["newer"];
  1230. $duration = $get["duration"];
  1231. $quality = $get["quality"];
  1232. $captions = $get["captions"];
  1233. $container = null;
  1234. $proxy = $this->backend->get_ip();
  1235. $params = [
  1236. "q" => $search,
  1237. "udm" => "7",
  1238. "hl" => "en"
  1239. ];
  1240. // country
  1241. if($country != "any"){
  1242. $params["gl"] = $country;
  1243. }
  1244. // nsfw
  1245. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1246. $tbs = [];
  1247. // get date
  1248. $older = $older === false ? null : date("m/d/Y", $older);
  1249. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1250. if(
  1251. $older !== null ||
  1252. $newer !== null
  1253. ){
  1254. $tbs["cdr"] = "1";
  1255. $tbs["cd_min"] = $newer;
  1256. $tbs["cd_max"] = $older;
  1257. }
  1258. // duration
  1259. if($duration != "any"){
  1260. $tbs[] = "dur:" . $duration;
  1261. }
  1262. // quality
  1263. if($quality != "any"){
  1264. $tbs[] = "hq:" . $quality;
  1265. }
  1266. // captions
  1267. if($captions != "any"){
  1268. $tbs[] = "cc:" . $captions;
  1269. }
  1270. // append tbs
  1271. if(count($tbs) !== 0){
  1272. $params["tbs"] =
  1273. implode(",", $tbs);
  1274. }
  1275. }
  1276. //$html = file_get_contents("scraper/google.html");
  1277. $data = $this->get(
  1278. $proxy,
  1279. "https://www.google.com/search",
  1280. $params,
  1281. $container
  1282. );
  1283. $html = &$data["data"];
  1284. $container = &$data["container"];
  1285. if(!isset($params["start"])){
  1286. $params["start"] = 0;
  1287. }
  1288. $params["start"] += 20;
  1289. $this->fuckhtml->load($html);
  1290. // parse all <style> tags
  1291. $this->parsestyles();
  1292. //
  1293. // Parse web video page
  1294. //
  1295. $this->detect_sorry();
  1296. // get javascript images
  1297. $this->scrape_dimg($html);
  1298. $this->scrape_imagearr($html);
  1299. $params["c"] = $container;
  1300. $out = [
  1301. "status" => "ok",
  1302. "npt" =>
  1303. $this->backend->store(
  1304. json_encode($params),
  1305. "videos",
  1306. $proxy
  1307. ),
  1308. "video" => [],
  1309. "author" => [],
  1310. "livestream" => [],
  1311. "playlist" => [],
  1312. "reel" => []
  1313. ];
  1314. $search_div =
  1315. $this->fuckhtml
  1316. ->getElementById(
  1317. "center_col"
  1318. );
  1319. if($search_div === false){
  1320. throw new Exception("Failed to grep search div");
  1321. }
  1322. $this->fuckhtml->load($search_div);
  1323. $results =
  1324. $this->fuckhtml
  1325. ->getElementsByClassName(
  1326. $this->getstyle([
  1327. "margin" => "0px 0px 30px"
  1328. ]),
  1329. "div"
  1330. );
  1331. foreach($results as $result){
  1332. $this->fuckhtml->load($result);
  1333. $url =
  1334. $this->fuckhtml
  1335. ->getElementsByTagName(
  1336. "a"
  1337. );
  1338. if(count($url) === 0){
  1339. // no url, weird, continue
  1340. continue;
  1341. }
  1342. $title =
  1343. $this->fuckhtml
  1344. ->getElementsByTagName(
  1345. "h3"
  1346. );
  1347. if(count($title) === 0){
  1348. // no title, weird, continue
  1349. continue;
  1350. }
  1351. // get description
  1352. $description =
  1353. $this->fuckhtml
  1354. ->getElementsByClassName(
  1355. $this->getstyle([
  1356. "-webkit-box-orient" => "vertical",
  1357. "display" => "-webkit-box",
  1358. "-webkit-line-clamp" => "2",
  1359. "overflow" => "hidden",
  1360. "word-break" => "break-word"
  1361. ]),
  1362. "div"
  1363. );
  1364. if(count($description) === 0){
  1365. $description = null;
  1366. }else{
  1367. $description =
  1368. html_entity_decode(
  1369. $this->titledots(
  1370. $this->fuckhtml
  1371. ->getTextContent(
  1372. $description[0]
  1373. )
  1374. )
  1375. );
  1376. }
  1377. // get author + date posted
  1378. $metadiv =
  1379. $this->fuckhtml
  1380. ->getElementsByClassName(
  1381. $this->getstyle([
  1382. "margin-top" => "12px"
  1383. ]),
  1384. "div"
  1385. );
  1386. $author = null;
  1387. $date = null;
  1388. if(count($metadiv) !== 0){
  1389. $metadiv =
  1390. explode(
  1391. "·",
  1392. $this->fuckhtml
  1393. ->getTextContent(
  1394. $metadiv[0]
  1395. )
  1396. );
  1397. if(count($metadiv) === 3){
  1398. $author = trim($metadiv[1]);
  1399. $date = strtotime(trim($metadiv[2]));
  1400. }elseif(count($metadiv) === 2){
  1401. $author = trim($metadiv[0]);
  1402. $date = strtotime(trim($metadiv[1]));
  1403. }
  1404. }
  1405. $thumb = [
  1406. "url" => null,
  1407. "ratio" => null
  1408. ];
  1409. $image =
  1410. $this->fuckhtml
  1411. ->getElementsByTagName(
  1412. "img"
  1413. );
  1414. $duration = null;
  1415. if(
  1416. count($image) !== 0 &&
  1417. isset($image[0]["attributes"]["id"])
  1418. ){
  1419. $thumb = [
  1420. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1421. "ratio" => "16:9"
  1422. ];
  1423. // get duration
  1424. $duration =
  1425. $this->fuckhtml
  1426. ->getElementsByClassName(
  1427. $this->getstyle([
  1428. "background-color" => "rgba(0,0,0,0.6)",
  1429. "color" => "#fff",
  1430. "fill" => "#fff"
  1431. ])
  1432. );
  1433. if(count($duration) !== 0){
  1434. $duration =
  1435. $this->hms2int(
  1436. $this->fuckhtml
  1437. ->getTextContent(
  1438. $duration[0]
  1439. ));
  1440. }else{
  1441. $duration = null;
  1442. }
  1443. }
  1444. $out["video"][] = [
  1445. "title" =>
  1446. $this->titledots(
  1447. $this->fuckhtml
  1448. ->getTextContent(
  1449. $title[0]
  1450. )
  1451. ),
  1452. "description" => $description,
  1453. "author" => [
  1454. "name" => $author,
  1455. "url" => null,
  1456. "avatar" => null
  1457. ],
  1458. "date" => $date,
  1459. "duration" => $duration,
  1460. "views" => null,
  1461. "thumb" => $thumb,
  1462. "url" =>
  1463. $this->fuckhtml
  1464. ->getTextContent(
  1465. $url[0]["attributes"]["href"]
  1466. )
  1467. ];
  1468. }
  1469. return $out;
  1470. }
  1471. public function news($get){
  1472. if($get["npt"]){
  1473. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1474. $req = json_decode($req, true);
  1475. $container = $req["c"];
  1476. $req = $req["u"];
  1477. $data = $this->get(
  1478. $proxy,
  1479. "https://www.google.com" . $req,
  1480. [],
  1481. $container
  1482. );
  1483. $html = &$data["data"];
  1484. $container = &$data["container"];
  1485. }else{
  1486. $search = $get["s"];
  1487. $country = $get["country"];
  1488. $nsfw = $get["nsfw"];
  1489. $older = $get["older"];
  1490. $newer = $get["newer"];
  1491. $sort = $get["sort"];
  1492. $container = null;
  1493. $proxy = $this->backend->get_ip();
  1494. $params = [
  1495. "q" => $search,
  1496. "tbm" => "nws",
  1497. "hl" => "en"
  1498. ];
  1499. // country
  1500. if($country != "any"){
  1501. $params["gl"] = $country;
  1502. }
  1503. // nsfw
  1504. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1505. $tbs = [];
  1506. // get date
  1507. $older = $older === false ? null : date("m/d/Y", $older);
  1508. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1509. if(
  1510. $older !== null ||
  1511. $newer !== null
  1512. ){
  1513. $tbs["cdr"] = "1";
  1514. $tbs["cd_min"] = $newer;
  1515. $tbs["cd_max"] = $older;
  1516. }
  1517. // relevance
  1518. if($sort == "date"){
  1519. $tbs["sbd"] = "1";
  1520. }
  1521. // append tbs
  1522. if(count($tbs) !== 0){
  1523. $params["tbs"] = "";
  1524. foreach($tbs as $key => $value){
  1525. $params["tbs"] .= $key . ":" . $value . ",";
  1526. }
  1527. $params["tbs"] = rtrim($params["tbs"], ",");
  1528. }
  1529. //$html = file_get_contents("scraper/google.html");
  1530. $data = $this->get(
  1531. $proxy,
  1532. "https://www.google.com/search",
  1533. $params
  1534. );
  1535. $html = &$data["data"];
  1536. $container = &$data["container"];
  1537. }
  1538. $out = [
  1539. "status" => "ok",
  1540. "npt" => null,
  1541. "news" => []
  1542. ];
  1543. $this->fuckhtml->load($html);
  1544. // parse styles
  1545. $this->parsestyles();
  1546. $this->detect_sorry();
  1547. // get images
  1548. $this->scrape_dimg($html);
  1549. $center_col =
  1550. $this->fuckhtml
  1551. ->getElementById(
  1552. "center_col",
  1553. "div"
  1554. );
  1555. if($center_col === null){
  1556. throw new Exception("Could not grep result div");
  1557. }
  1558. $this->fuckhtml->load($center_col);
  1559. // get next page
  1560. $npt =
  1561. $this->fuckhtml
  1562. ->getElementById(
  1563. "pnnext",
  1564. "a"
  1565. );
  1566. if($npt !== false){
  1567. $out["npt"] =
  1568. $this->backend->store(
  1569. json_encode([
  1570. "u" =>
  1571. $this->fuckhtml
  1572. ->getTextContent(
  1573. $npt["attributes"]
  1574. ["href"]
  1575. ),
  1576. "c" => $container
  1577. ]),
  1578. "news",
  1579. $proxy
  1580. );
  1581. }
  1582. $as =
  1583. $this->fuckhtml
  1584. ->getElementsByAttributeName(
  1585. "jsname",
  1586. "a"
  1587. );
  1588. foreach($as as $a){
  1589. $this->fuckhtml->load($a);
  1590. // get title
  1591. $title =
  1592. $this->fuckhtml
  1593. ->getElementsByAttributeValue(
  1594. "role",
  1595. "heading",
  1596. "div"
  1597. );
  1598. if(count($title) === 0){
  1599. continue;
  1600. }
  1601. $title =
  1602. $this->titledots(
  1603. $this->fuckhtml
  1604. ->getTextContent(
  1605. $title[0]
  1606. )
  1607. );
  1608. // get thumbnail
  1609. $image =
  1610. $this->fuckhtml
  1611. ->getElementsByAttributeName(
  1612. "id",
  1613. "img"
  1614. );
  1615. // check for padded title node, if found, we're inside a carousel
  1616. $probe =
  1617. $this->fuckhtml
  1618. ->getElementsByClassName(
  1619. $this->getstyle(
  1620. [
  1621. "padding" => "16px 16px 40px 16px"
  1622. ]
  1623. ),
  1624. "div"
  1625. );
  1626. if(count($probe) !== 0){
  1627. $probe = true;
  1628. }else{
  1629. $probe = false;
  1630. }
  1631. if(
  1632. count($image) !== 0 &&
  1633. !isset($image[0]["attributes"]["width"])
  1634. ){
  1635. $thumb = [
  1636. "url" =>
  1637. $this->getdimg(
  1638. $image[0]["attributes"]["id"]
  1639. ),
  1640. "ratio" => $probe === true ? "16:9" : "1:1"
  1641. ];
  1642. }else{
  1643. $thumb = [
  1644. "url" => null,
  1645. "ratio" => null
  1646. ];
  1647. }
  1648. $description = null;
  1649. if($probe === false){
  1650. $desc_divs =
  1651. $this->fuckhtml
  1652. ->getElementsByAttributeName(
  1653. "style",
  1654. "div"
  1655. );
  1656. foreach($desc_divs as $desc){
  1657. if(
  1658. strpos(
  1659. $desc["attributes"]["style"],
  1660. "margin-top:"
  1661. ) !== false
  1662. ){
  1663. $description =
  1664. $this->titledots(
  1665. $this->fuckhtml
  1666. ->getTextContent(
  1667. $desc
  1668. )
  1669. );
  1670. break;
  1671. }
  1672. }
  1673. }
  1674. // get author
  1675. $author =
  1676. $this->fuckhtml
  1677. ->getElementsByClassName(
  1678. $this->getstyle(
  1679. [
  1680. "overflow" => "hidden",
  1681. "text-align" => "left",
  1682. "text-overflow" => "ellipsis",
  1683. "white-space" => "nowrap",
  1684. "margin-bottom" => "8px"
  1685. ]
  1686. ),
  1687. "div"
  1688. );
  1689. if(count($author) !== 0){
  1690. $author =
  1691. $this->fuckhtml
  1692. ->getTextContent(
  1693. $author[0]
  1694. );
  1695. }else{
  1696. $author = null;
  1697. }
  1698. // get date
  1699. $date = null;
  1700. $date_div =
  1701. $this->fuckhtml
  1702. ->getElementsByAttributeName(
  1703. "style",
  1704. "div"
  1705. );
  1706. foreach($date_div as $d){
  1707. $this->fuckhtml->load($d);
  1708. $span =
  1709. $this->fuckhtml
  1710. ->getElementsByTagName(
  1711. "span"
  1712. );
  1713. if(
  1714. strpos(
  1715. $d["attributes"]["style"],
  1716. "bottom:"
  1717. ) !== false
  1718. ){
  1719. $date =
  1720. strtotime(
  1721. $this->fuckhtml
  1722. ->getTextContent(
  1723. $span[count($span) - 1]
  1724. )
  1725. );
  1726. break;
  1727. }
  1728. }
  1729. $out["news"][] = [
  1730. "title" => $title,
  1731. "author" => $author,
  1732. "description" => $description,
  1733. "date" => $date,
  1734. "thumb" => $thumb,
  1735. "url" =>
  1736. $this->unshiturl(
  1737. $a["attributes"]
  1738. ["href"]
  1739. )
  1740. ];
  1741. }
  1742. return $out;
  1743. }
  1744. private function scrape_dimg($html){
  1745. // get images loaded through javascript
  1746. $this->dimg = [];
  1747. preg_match_all(
  1748. '/function\(\){google\.ldi=({.*?});/',
  1749. $html,
  1750. $dimg
  1751. );
  1752. if(isset($dimg[1])){
  1753. foreach($dimg[1] as $i){
  1754. $tmp = json_decode($i, true);
  1755. foreach($tmp as $key => $value){
  1756. $this->dimg[$key] =
  1757. $this->unshit_thumb(
  1758. $value
  1759. );
  1760. }
  1761. }
  1762. }
  1763. // get additional javascript base64 images
  1764. preg_match_all(
  1765. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1766. $html,
  1767. $dimg
  1768. );
  1769. if(isset($dimg[1])){
  1770. for($i=0; $i<count($dimg[1]); $i++){
  1771. $delims = explode(",", $dimg[2][$i]);
  1772. $string =
  1773. $this->fuckhtml
  1774. ->parseJsString(
  1775. $dimg[1][$i]
  1776. );
  1777. foreach($delims as $delim){
  1778. $this->dimg[trim($delim, "'")] = $string;
  1779. }
  1780. }
  1781. }
  1782. }
  1783. private function scrape_imagearr($html){
  1784. // get image links arrays
  1785. preg_match_all(
  1786. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1787. $html,
  1788. $image_arr
  1789. );
  1790. $this->image_arr = [];
  1791. if(isset($image_arr[1])){
  1792. for($i=0; $i<count($image_arr[1]); $i++){
  1793. $original =
  1794. $this->fuckhtml
  1795. ->parseJsString(
  1796. $image_arr[5][$i]
  1797. );
  1798. if(
  1799. preg_match(
  1800. '/^x-raw-image/',
  1801. $original
  1802. )
  1803. ){
  1804. // only add thumbnail, google doesnt have OG resolution
  1805. $this->image_arr[$image_arr[1][$i]] = [
  1806. [
  1807. "url" =>
  1808. $this->unshit_thumb(
  1809. $this->fuckhtml
  1810. ->parseJsString(
  1811. $image_arr[2][$i]
  1812. )
  1813. ),
  1814. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1815. "height" => (int)$image_arr[6][$i]
  1816. ]
  1817. ];
  1818. continue;
  1819. }
  1820. $this->image_arr[$image_arr[1][$i]] =
  1821. [
  1822. [
  1823. "url" => $original,
  1824. "width" => (int)$image_arr[7][$i],
  1825. "height" => (int)$image_arr[6][$i]
  1826. ],
  1827. [
  1828. "url" =>
  1829. $this->unshit_thumb(
  1830. $this->fuckhtml
  1831. ->parseJsString(
  1832. $image_arr[2][$i]
  1833. )
  1834. ),
  1835. "width" => (int)$image_arr[4][$i],
  1836. "height" => (int)$image_arr[3][$i]
  1837. ]
  1838. ];
  1839. }
  1840. }
  1841. }
  1842. private function getdimg($dimg){
  1843. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1844. }
  1845. private function unshit_thumb($url, $get_bigger_res = false){
  1846. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1847. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1848. $parts = parse_url($url);
  1849. if(
  1850. isset($parts["host"]) &&
  1851. preg_match(
  1852. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1853. $parts["host"]
  1854. )
  1855. ){
  1856. parse_str($parts["query"], $params);
  1857. if(isset($params["q"])){
  1858. if($get_bigger_res){
  1859. // this method doesnt always work, but does work for wiki thumbnails
  1860. return
  1861. "https://" . $parts["host"] . "/images?q=tbn:" .
  1862. $this->base64url_encode(
  1863. substr(
  1864. $this->base64url_decode(
  1865. explode(
  1866. ":",
  1867. $params["q"])[1]
  1868. ),
  1869. 0,
  1870. 29
  1871. )
  1872. );
  1873. }else{
  1874. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1875. }
  1876. }
  1877. }
  1878. return $url;
  1879. }
  1880. private function parsestyles(){
  1881. $styles = [];
  1882. $style_div =
  1883. $this->fuckhtml
  1884. ->getElementsByTagName(
  1885. "style"
  1886. );
  1887. $raw_styles = "";
  1888. foreach($style_div as $style){
  1889. $raw_styles .= $style["innerHTML"];
  1890. }
  1891. // filter out media/keyframe queries
  1892. $raw_styles =
  1893. preg_replace(
  1894. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1895. "",
  1896. $raw_styles
  1897. );
  1898. // get styles
  1899. preg_match_all(
  1900. '/(.+?){([\S\s]*?)}/',
  1901. $raw_styles,
  1902. $matches
  1903. );
  1904. for($i=0; $i<count($matches[1]); $i++){
  1905. // get style values
  1906. preg_match_all(
  1907. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1908. $matches[2][$i],
  1909. $values_regex
  1910. );
  1911. $values = [];
  1912. for($k=0; $k<count($values_regex[1]); $k++){
  1913. $values[trim($values_regex[1][$k])] =
  1914. strtolower(trim($values_regex[2][$k]));
  1915. }
  1916. $names = explode(",", $matches[1][$i]);
  1917. // h1,h2,h3 will each get their own array index
  1918. foreach($names as $name){
  1919. $name = trim($name, "}\t\n\r\0\x0B");
  1920. foreach($values as $key => $value){
  1921. $styles[$name][$key] = $value;
  1922. }
  1923. }
  1924. }
  1925. foreach($styles as $key => $values){
  1926. $styles[$key]["_c"] = count($values);
  1927. }
  1928. $this->styles = $styles;
  1929. // get CSS colors
  1930. $this->css_colors = [];
  1931. if(isset($this->styles[":root"])){
  1932. foreach($this->styles[":root"] as $key => $value){
  1933. $this->css_colors[$value] = strtolower($key);
  1934. }
  1935. }
  1936. }
  1937. private function getstyle($styles){
  1938. $styles["_c"] = count($styles);
  1939. foreach($this->styles as $style_key => $style_values){
  1940. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1941. $style_key =
  1942. explode(" ", $style_key);
  1943. $style_key = $style_key[count($style_key) - 1];
  1944. return
  1945. ltrim(
  1946. str_replace(
  1947. [".", "#"],
  1948. " ",
  1949. $style_key
  1950. )
  1951. );
  1952. }
  1953. }
  1954. return false;
  1955. }
  1956. private function getcolorvar($color){
  1957. if(isset($this->css_colors[$color])){
  1958. return $this->css_colors[$color];
  1959. }
  1960. return null;
  1961. }
  1962. private function unshiturl($url, $return_size = false){
  1963. // decode
  1964. $url =
  1965. $this->fuckhtml
  1966. ->getTextContent(
  1967. $url
  1968. );
  1969. $url_parts = parse_url($url);
  1970. if(isset($url_parts["query"])){
  1971. parse_str($url_parts["query"], $query);
  1972. }else{
  1973. $query = [];
  1974. }
  1975. if(
  1976. !isset(
  1977. $url_parts["host"]
  1978. ) ||
  1979. stripos($url_parts["host"], "google.") !== false
  1980. ){
  1981. // no host, we have a tracking url
  1982. if(isset($query["imgurl"])){
  1983. $url = $query["imgurl"];
  1984. }
  1985. elseif(isset($query["q"])){
  1986. $url = $query["q"];
  1987. }
  1988. }
  1989. // rewrite URLs to remove extra tracking parameters
  1990. $domain = parse_url($url, PHP_URL_HOST);
  1991. if(
  1992. preg_match(
  1993. '/wikipedia.org$/',
  1994. $domain
  1995. )
  1996. ){
  1997. // rewrite wikipedia mobile URLs to desktop
  1998. $url =
  1999. $this->replacedomain(
  2000. $url,
  2001. preg_replace(
  2002. '/([a-z0-9]+)(\.m\.)/',
  2003. '$1.',
  2004. $domain
  2005. )
  2006. );
  2007. }
  2008. elseif(
  2009. preg_match(
  2010. '/imdb\.com$|youtube\.[^.]+$/',
  2011. $domain
  2012. )
  2013. ){
  2014. // rewrite imdb and youtube mobile URLs too
  2015. $url =
  2016. $this->replacedomain(
  2017. $url,
  2018. preg_replace(
  2019. '/^m\./',
  2020. "",
  2021. $domain
  2022. )
  2023. );
  2024. }
  2025. elseif(
  2026. preg_match(
  2027. '/play\.google\.[^.]+$/',
  2028. $domain
  2029. )
  2030. ){
  2031. // remove referrers from play.google.com
  2032. $u_query = parse_url($url, PHP_URL_QUERY);
  2033. if($u_query !== null){
  2034. parse_str($u_query, $u_query);
  2035. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2036. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2037. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2038. $query = http_build_query($query);
  2039. $url =
  2040. str_replace(
  2041. $u_query,
  2042. $u_query,
  2043. $url
  2044. );
  2045. }
  2046. }
  2047. elseif(
  2048. preg_match(
  2049. '/twitter\.com$/',
  2050. $domain
  2051. )
  2052. ){
  2053. // remove more referrers from twitter.com
  2054. $u_query = parse_url($url, PHP_URL_QUERY);
  2055. if($u_query !== null){
  2056. parse_str($u_query, $u_query);
  2057. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2058. $u_query = http_build_query($u_query);
  2059. $url =
  2060. str_replace(
  2061. $oldquery,
  2062. $u_query,
  2063. $url
  2064. );
  2065. }
  2066. }
  2067. elseif(
  2068. preg_match(
  2069. '/maps\.google\.[^.]+/',
  2070. $domain
  2071. )
  2072. ){
  2073. if(stripos($url, "maps?") !== false){
  2074. $u_query = parse_url($url, PHP_URL_QUERY);
  2075. if($u_query !== null){
  2076. parse_str($u_query, $u_query);
  2077. if(isset($u_query["daddr"])){
  2078. $url =
  2079. "https://maps.google.com/maps?daddr=" .
  2080. urlencode($u_query["daddr"]);
  2081. }
  2082. }
  2083. }
  2084. }
  2085. if($return_size){
  2086. return [
  2087. "url" => $url,
  2088. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2089. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2090. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2091. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2092. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2093. ];
  2094. }
  2095. return $url;
  2096. }
  2097. private function replacedomain($url, $domain){
  2098. return
  2099. preg_replace(
  2100. '/(https?:\/\/)([^\/]+)/',
  2101. '$1' . $domain,
  2102. $url
  2103. );
  2104. }
  2105. private function titledots($title){
  2106. return trim($title, " .\t\n\r\0\x0B");
  2107. }
  2108. private function hms2int($time){
  2109. $parts = explode(":", $time, 3);
  2110. $time = 0;
  2111. if(count($parts) === 3){
  2112. // hours
  2113. $time = $time + ((int)$parts[0] * 3600);
  2114. array_shift($parts);
  2115. }
  2116. if(count($parts) === 2){
  2117. // minutes
  2118. $time = $time + ((int)$parts[0] * 60);
  2119. array_shift($parts);
  2120. }
  2121. // seconds
  2122. $time = $time + (int)$parts[0];
  2123. return $time;
  2124. }
  2125. function base64url_decode($data){
  2126. $b64 = strtr($data, "-_", "+/");
  2127. $pad = strlen($b64) % 4;
  2128. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2129. return base64_decode($b64);
  2130. }
  2131. function base64url_encode($data){
  2132. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2133. }
  2134. private function detect_sorry(){
  2135. // detect recaptcha
  2136. $captcha_form =
  2137. $this->fuckhtml
  2138. ->getElementById(
  2139. "captcha-form",
  2140. "form"
  2141. );
  2142. if($captcha_form !== false){
  2143. throw new Exception("Google returned a captcha");
  2144. }
  2145. // detect JS challenge
  2146. $title =
  2147. $this->fuckhtml
  2148. ->getElementsByTagName(
  2149. "title"
  2150. );
  2151. if(
  2152. count($title) !== 0 &&
  2153. $this->fuckhtml
  2154. ->getTextContent(
  2155. $title[0]
  2156. ) == "Google Search"
  2157. ){
  2158. throw new Exception("Google returned a JS challenge");
  2159. }
  2160. // detect soft captcha
  2161. // "Verifying your request
  2162. // You'll be able to continue in a few seconds. Don't refresh this page."
  2163. // getting this mostly on mid-trust IPs
  2164. $softcaptcha =
  2165. $this->fuckhtml
  2166. ->getElementsByAttributeValue(
  2167. "role",
  2168. "progressbar",
  2169. "div"
  2170. );
  2171. if(count($softcaptcha) !== 0){
  2172. throw new Exception("Google returned a soft captcha");
  2173. }
  2174. }
  2175. }