1
0

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. /*
  649. $data = $this->get(
  650. $proxy,
  651. "https://www.google.com/search",
  652. $params,
  653. null
  654. );*/
  655. $html = &$data["data"];
  656. $container = &$data["container"];
  657. //file_put_contents("scraper/google-jp.html", $html);
  658. //$html = file_get_contents("scraper/google-softcaptcha.html");
  659. }
  660. $out = [
  661. "status" => "ok",
  662. "spelling" => [
  663. "type" => "no_correction",
  664. "using" => null,
  665. "correction" => null
  666. ],
  667. "npt" => null,
  668. "answer" => [],
  669. "web" => [],
  670. "image" => [],
  671. "video" => [],
  672. "news" => [],
  673. "related" => []
  674. ];
  675. $this->fuckhtml->load($html);
  676. // init
  677. $this->parsestyles();
  678. $this->detect_sorry();
  679. $this->scrape_dimg($html);
  680. $this->scrape_imagearr($html);
  681. $content =
  682. $this->fuckhtml
  683. ->getElementById(
  684. "rso"
  685. );
  686. if($content === null){
  687. throw new Exception("Failed to grep search div");
  688. }
  689. // get encoded links
  690. $encoded_links =
  691. explode(
  692. "function(){var m={",
  693. $html,
  694. 2
  695. );
  696. if(count($encoded_links) === 2){
  697. $encoded_links =
  698. json_decode(
  699. $this->fuckhtml
  700. ->extract_json(
  701. "{" . $encoded_links[1]
  702. ),
  703. true
  704. );
  705. }else{
  706. $encoded_links = [];
  707. }
  708. // get the "did you mean" bullshit
  709. $taw =
  710. $this->fuckhtml
  711. ->getElementById("taw");
  712. if($taw !== null){
  713. $this->fuckhtml->load($taw);
  714. $as =
  715. $this->fuckhtml
  716. ->getElementsByTagName("a");
  717. $links = [];
  718. foreach($as as $a){
  719. $href =
  720. $this->fuckhtml
  721. ->getTextContent(
  722. $a["attributes"]["href"]
  723. );
  724. if(preg_match('/^\/search\?/', $href)){
  725. $links[] =
  726. $this->fuckhtml
  727. ->getTextContent(
  728. $a
  729. );
  730. }
  731. }
  732. switch(count($links)){
  733. case 1:
  734. $out["spelling"] = [
  735. "type" => "including",
  736. "using" => $params["q"],
  737. "correction" => $links[0]
  738. ];
  739. break;
  740. case 2:
  741. $out["spelling"] = [
  742. "type" => "including",
  743. "using" => $links[0],
  744. "correction" => $links[1]
  745. ];
  746. break;
  747. }
  748. // reset
  749. $this->fuckhtml->load($html);
  750. }
  751. $bottom =
  752. $this->fuckhtml
  753. ->getElementById("botstuff");
  754. if($bottom !== null){
  755. $this->fuckhtml->load($bottom);
  756. // get next page thingy
  757. $npt =
  758. $this->fuckhtml
  759. ->getElementById(
  760. "pnnext"
  761. );
  762. if($npt !== false){
  763. $out["npt"] =
  764. $this->backend->store(
  765. json_encode([
  766. "u" =>
  767. $this->fuckhtml
  768. ->getTextContent(
  769. $npt["attributes"]["href"]
  770. ),
  771. "c" => $container
  772. ]),
  773. "web",
  774. $proxy
  775. );
  776. }
  777. // get lumendatabase notices
  778. $notices = [];
  779. $as =
  780. $this->fuckhtml
  781. ->getElementsByTagName("a");
  782. foreach($as as $a){
  783. if(!isset($a["attributes"]["href"])){ continue; }
  784. $link =
  785. $this->fuckhtml
  786. ->getTextContent(
  787. $a["attributes"]["href"]
  788. );
  789. if(
  790. preg_match(
  791. '/^https?:\/\/lumendatabase\.org/',
  792. $link
  793. )
  794. ){
  795. $notices[] = $link;
  796. }
  797. }
  798. // push notices to dom
  799. if(count($notices) !== 0){
  800. $notices_dom = [];
  801. $i = 0;
  802. $c = count($notices);
  803. $notices_dom = [
  804. [
  805. "type" => "text",
  806. "value" => "Google removed results from this page, but you can get them anyways by consulting these takedown notices: "
  807. ]
  808. ];
  809. foreach($notices as $n){
  810. $i++;
  811. $notices_dom[] = [
  812. "type" => "link",
  813. "value" => "Notice #{$i}",
  814. "url" => $n
  815. ];
  816. if($i !== count($notices)){
  817. $notices_dom[] = [
  818. "type" => "text",
  819. "value" => ", "
  820. ];
  821. }
  822. }
  823. $out["answer"][] = [
  824. "title" => "LumenDB notices",
  825. "description" => $notices_dom,
  826. "url" => null,
  827. "thumb" => null,
  828. "table" => [],
  829. "sublink" => []
  830. ];
  831. }
  832. }
  833. $this->fuckhtml->load($content);
  834. // get search results
  835. $results =
  836. $this->fuckhtml
  837. ->getElementsByAttributeName(
  838. "data-rpos",
  839. "div"
  840. );
  841. foreach($results as $result){
  842. $this->fuckhtml->load($result);
  843. // get title
  844. $title =
  845. $this->fuckhtml
  846. ->getElementsByTagName("h3");
  847. if(count($title) === 0){
  848. // should not happen
  849. continue;
  850. }
  851. $title =
  852. $this->titledots(
  853. $this->fuckhtml
  854. ->getTextContent(
  855. $title[0]
  856. )
  857. );
  858. $url =
  859. $this->fuckhtml
  860. ->getElementsByTagName(
  861. "a"
  862. );
  863. if(
  864. count($url) === 0 ||
  865. !isset($url[0]["attributes"]["href"])
  866. ){
  867. continue;
  868. }
  869. $url =
  870. $this->fuckhtml
  871. ->getTextContent(
  872. $url[0]["attributes"]["href"]
  873. );
  874. if(
  875. preg_match(
  876. '/^\/goto\?url/',
  877. $url
  878. )
  879. ){
  880. // encrypted url detected
  881. // get reference div
  882. $refdivs =
  883. $this->fuckhtml
  884. ->getElementsByAttributeName(
  885. "id",
  886. "div"
  887. );
  888. foreach($refdivs as $r){
  889. if(
  890. preg_match(
  891. '/^atritem-/',
  892. $r["attributes"]["id"]
  893. ) &&
  894. isset($r["attributes"]["jsdata"])
  895. ){
  896. $ref = explode(";", $r["attributes"]["jsdata"]);
  897. $ref = $ref[count($ref) - 1];
  898. if(isset($encoded_links[$ref][32][3][0])){
  899. $url = $encoded_links[$ref][32][3][0];
  900. }
  901. break;
  902. }
  903. }
  904. }
  905. // get citation text. contains table elements
  906. $table = [];
  907. $date = null;
  908. $cite =
  909. $this->fuckhtml
  910. ->getElementsByTagName("cite");
  911. if(isset($cite[0])){
  912. $texts =
  913. explode(
  914. " · ",
  915. $this->fuckhtml
  916. ->getTextContent($cite[0])
  917. );
  918. foreach($texts as $text){
  919. $text = trim($text);
  920. preg_match(
  921. '/([0-9+kmb,.]+) +(likes?|views?|comments?|followers?|subscribers?|(?:hours?|days?|months?|years?) +ago)/i',
  922. $text,
  923. $probe
  924. );
  925. if(isset($probe[1])){
  926. // is it date?
  927. if(
  928. preg_match(
  929. '/ago$/i',
  930. $text
  931. )
  932. ){
  933. $time = strtotime($text);
  934. if($time !== false){ $date = $time; }
  935. continue;
  936. }
  937. // anything else?
  938. $table[ucfirst($probe[2])] = trim($probe[1]);
  939. }
  940. }
  941. }
  942. // get description/image wrapper
  943. $desc_img_wrappers =
  944. $this->fuckhtml
  945. ->getElementsByAttributeName(
  946. "data-sncf",
  947. "div"
  948. );
  949. $description = null;
  950. $thumb = [
  951. "url" => null,
  952. "ratio" => null
  953. ];
  954. foreach($desc_img_wrappers as $wrapper){
  955. $this->fuckhtml->load($wrapper);
  956. // probe for image
  957. $image =
  958. $this->fuckhtml
  959. ->getElementsByTagName(
  960. "img"
  961. );
  962. if(count($image) !== 0){
  963. // handle thumbnails
  964. $i = null;
  965. if(isset($image[0]["attributes"]["id"])){
  966. $i = $this->getdimg($image[0]["attributes"]["id"]);
  967. }
  968. if($i !== null){
  969. $thumb = [
  970. "ratio" => "1:1",
  971. "url" => $i
  972. ];
  973. }
  974. }else{
  975. if($description === null){
  976. // parse it as description
  977. $description =
  978. $this->titledots(
  979. $this->fuckhtml
  980. ->getTextContent(
  981. $wrapper
  982. )
  983. );
  984. }else{
  985. // parse table elements
  986. $ts =
  987. explode(
  988. " · ",
  989. $this->fuckhtml
  990. ->getTextContent(
  991. $wrapper
  992. )
  993. );
  994. foreach($ts as $t){
  995. $t = trim(html_entity_decode($t));
  996. if(preg_match("/\p{Sc}/u", $t)){
  997. $table["Price"] = $t;
  998. continue;
  999. }
  1000. if(str_contains($t, "stock")){
  1001. $table["Stock"] = $t;
  1002. continue;
  1003. }
  1004. if(str_contains($t, "return")){
  1005. $table["Returns"] = $t;
  1006. continue;
  1007. }
  1008. if(str_contains($t, "delivery")){
  1009. $table["Delivery"] = $t;
  1010. continue;
  1011. }
  1012. // 5.0(590,004)
  1013. if(preg_match('/^([0-9kmb\/%.,]+) *(\([0-9kmb,.]+\))?/', $t, $match)){
  1014. if(isset($match[2])){
  1015. $table["Ratings"] = "{$match[1]} {$match[2]}";
  1016. continue;
  1017. }
  1018. }
  1019. if(str_contains($t, "rating")){
  1020. $table["Ratings"] = $t;
  1021. continue;
  1022. }
  1023. }
  1024. }
  1025. }
  1026. }
  1027. // extract date from description
  1028. // 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
  1029. if($date === null){
  1030. $date_probe =
  1031. explode(
  1032. " — ",
  1033. $description,
  1034. 2
  1035. );
  1036. if(count($date_probe) === 2){
  1037. $time = strtotime($date_probe[0]);
  1038. if($time !== false){
  1039. $description = $date_probe[1];
  1040. $date = $time;
  1041. }
  1042. }
  1043. }
  1044. $sublinks = [];
  1045. $out["web"][] = [
  1046. "title" => $title,
  1047. "description" => $description,
  1048. "url" => $url,
  1049. "date" => $date,
  1050. "type" => "web",
  1051. "thumb" => $thumb,
  1052. "sublink" => $sublinks,
  1053. "table" => $table
  1054. ];
  1055. }
  1056. return $out;
  1057. }
  1058. public function image($get){
  1059. // generate parameters
  1060. if($get["npt"]){
  1061. [$params, $proxy] =
  1062. $this->backend->get(
  1063. $get["npt"],
  1064. "images"
  1065. );
  1066. $params = json_decode($params, true);
  1067. $container = $params["c"];
  1068. unset($params["c"]);
  1069. }else{
  1070. $search = $get["s"];
  1071. if(strlen($search) === 0){
  1072. throw new Exception("Search term is empty!");
  1073. }
  1074. $proxy = $this->backend->get_ip();
  1075. $country = $get["country"];
  1076. $nsfw = $get["nsfw"];
  1077. $time = $get["time"];
  1078. $size = $get["size"];
  1079. $ratio = $get["ratio"];
  1080. $color = $get["color"];
  1081. $type = $get["type"];
  1082. $format = $get["format"];
  1083. $rights = $get["rights"];
  1084. $params = [
  1085. "q" => $search,
  1086. "udm" => "2", // get images
  1087. "start" => 0
  1088. ];
  1089. // country (image search uses cr instead of gl)
  1090. if($country != "any"){
  1091. $params["cr"] = "country" . strtoupper($country);
  1092. }
  1093. // nsfw
  1094. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1095. // generate tbs
  1096. $tbs = [];
  1097. // time
  1098. if($time != "any"){
  1099. $tbs["qdr"] = $time;
  1100. }
  1101. // size
  1102. if($size != "any"){
  1103. $params["imgsz"] = $size;
  1104. }
  1105. // ratio
  1106. if($ratio != "any"){
  1107. $params["imgar"] = $ratio;
  1108. }
  1109. // color
  1110. if($color != "any"){
  1111. if(
  1112. $color == "color" ||
  1113. $color == "trans"
  1114. ){
  1115. $params["imgc"] = $color;
  1116. }elseif($color == "bnw"){
  1117. $params["imgc"] = "gray";
  1118. }else{
  1119. $tbs["ic"] = "specific";
  1120. $tbs["isc"] = $color;
  1121. }
  1122. }
  1123. // type
  1124. if($type != "any"){
  1125. $tbs["itp"] = $type;
  1126. }
  1127. // format
  1128. if($format != "any"){
  1129. $params["as_filetype"] = $format;
  1130. }
  1131. // rights (tbs)
  1132. if($rights != "any"){
  1133. $tbs["sur"] = $rights;
  1134. }
  1135. // append tbs
  1136. if(count($tbs) !== 0){
  1137. $params["tbs"] = "";
  1138. foreach($tbs as $key => $value){
  1139. $params["tbs"] .= $key . ":" . $value . ",";
  1140. }
  1141. $params["tbs"] = rtrim($params["tbs"], ",");
  1142. }
  1143. }
  1144. //$html = file_get_contents("scraper/google.html");
  1145. $data = $this->get(
  1146. $proxy,
  1147. "https://www.google.com/search",
  1148. $params
  1149. );
  1150. $html = &$data["data"];
  1151. $container = &$data["container"];
  1152. $this->fuckhtml->load($html);
  1153. // parse all <style> tags
  1154. $this->parsestyles();
  1155. $this->detect_sorry();
  1156. // get javascript images
  1157. $this->scrape_imagearr($html);
  1158. $out = [
  1159. "status" => "ok",
  1160. "npt" => null,
  1161. "image" => []
  1162. ];
  1163. $images =
  1164. $this->fuckhtml
  1165. ->getElementsByClassName(
  1166. "ivg-i",
  1167. "div"
  1168. );
  1169. foreach($images as $div){
  1170. $this->fuckhtml->load($div);
  1171. $image =
  1172. $this->fuckhtml
  1173. ->getElementsByTagName("img")[0];
  1174. // make sure we dont attempt to show an image we dont have data for
  1175. if(
  1176. isset($div["attributes"]["data-docid"]) &&
  1177. isset($this->image_arr[$div["attributes"]["data-docid"]])
  1178. ){
  1179. $source =
  1180. $this->image_arr[
  1181. $div["attributes"]["data-docid"]
  1182. ];
  1183. }else{
  1184. continue;
  1185. }
  1186. $out["image"][] = [
  1187. "title" =>
  1188. $this->titledots(
  1189. $this->fuckhtml
  1190. ->getTextContent(
  1191. $image["attributes"]["alt"]
  1192. )
  1193. ),
  1194. "source" => $source,
  1195. "url" =>
  1196. $this->fuckhtml
  1197. ->getTextContent(
  1198. $div["attributes"]["data-lpage"]
  1199. )
  1200. ];
  1201. }
  1202. // as usual, no way to check if there is a next page reliably
  1203. if(count($out["image"]) > 50){
  1204. if(!isset($params["start"])){
  1205. $params["start"] = 10;
  1206. }else{
  1207. $params["start"] += 10;
  1208. }
  1209. $out["npt"] =
  1210. $this->backend
  1211. ->store(
  1212. json_encode($params),
  1213. "image",
  1214. $proxy
  1215. );
  1216. }
  1217. return $out;
  1218. }
  1219. public function video($get){
  1220. if($get["npt"]){
  1221. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1222. $params = json_decode($params, true);
  1223. $container = $params["c"];
  1224. unset($params["c"]);
  1225. }else{
  1226. $search = $get["s"];
  1227. $country = $get["country"];
  1228. $nsfw = $get["nsfw"];
  1229. $older = $get["older"];
  1230. $newer = $get["newer"];
  1231. $duration = $get["duration"];
  1232. $quality = $get["quality"];
  1233. $captions = $get["captions"];
  1234. $container = null;
  1235. $proxy = $this->backend->get_ip();
  1236. $params = [
  1237. "q" => $search,
  1238. "udm" => "7",
  1239. "hl" => "en"
  1240. ];
  1241. // country
  1242. if($country != "any"){
  1243. $params["gl"] = $country;
  1244. }
  1245. // nsfw
  1246. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1247. $tbs = [];
  1248. // get date
  1249. $older = $older === false ? null : date("m/d/Y", $older);
  1250. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1251. if(
  1252. $older !== null ||
  1253. $newer !== null
  1254. ){
  1255. $tbs["cdr"] = "1";
  1256. $tbs["cd_min"] = $newer;
  1257. $tbs["cd_max"] = $older;
  1258. }
  1259. // duration
  1260. if($duration != "any"){
  1261. $tbs[] = "dur:" . $duration;
  1262. }
  1263. // quality
  1264. if($quality != "any"){
  1265. $tbs[] = "hq:" . $quality;
  1266. }
  1267. // captions
  1268. if($captions != "any"){
  1269. $tbs[] = "cc:" . $captions;
  1270. }
  1271. // append tbs
  1272. if(count($tbs) !== 0){
  1273. $params["tbs"] =
  1274. implode(",", $tbs);
  1275. }
  1276. }
  1277. //$html = file_get_contents("scraper/google.html");
  1278. $data = $this->get(
  1279. $proxy,
  1280. "https://www.google.com/search",
  1281. $params,
  1282. $container
  1283. );
  1284. $html = &$data["data"];
  1285. $container = &$data["container"];
  1286. if(!isset($params["start"])){
  1287. $params["start"] = 0;
  1288. }
  1289. $params["start"] += 20;
  1290. $this->fuckhtml->load($html);
  1291. // parse all <style> tags
  1292. $this->parsestyles();
  1293. //
  1294. // Parse web video page
  1295. //
  1296. $this->detect_sorry();
  1297. // get javascript images
  1298. $this->scrape_dimg($html);
  1299. $this->scrape_imagearr($html);
  1300. $params["c"] = $container;
  1301. $out = [
  1302. "status" => "ok",
  1303. "npt" =>
  1304. $this->backend->store(
  1305. json_encode($params),
  1306. "videos",
  1307. $proxy
  1308. ),
  1309. "video" => [],
  1310. "author" => [],
  1311. "livestream" => [],
  1312. "playlist" => [],
  1313. "reel" => []
  1314. ];
  1315. $search_div =
  1316. $this->fuckhtml
  1317. ->getElementById(
  1318. "center_col"
  1319. );
  1320. if($search_div === false){
  1321. throw new Exception("Failed to grep search div");
  1322. }
  1323. $this->fuckhtml->load($search_div);
  1324. $results =
  1325. $this->fuckhtml
  1326. ->getElementsByClassName(
  1327. $this->getstyle([
  1328. "margin" => "0px 0px 30px"
  1329. ]),
  1330. "div"
  1331. );
  1332. foreach($results as $result){
  1333. $this->fuckhtml->load($result);
  1334. $url =
  1335. $this->fuckhtml
  1336. ->getElementsByTagName(
  1337. "a"
  1338. );
  1339. if(count($url) === 0){
  1340. // no url, weird, continue
  1341. continue;
  1342. }
  1343. $title =
  1344. $this->fuckhtml
  1345. ->getElementsByTagName(
  1346. "h3"
  1347. );
  1348. if(count($title) === 0){
  1349. // no title, weird, continue
  1350. continue;
  1351. }
  1352. // get description
  1353. $description =
  1354. $this->fuckhtml
  1355. ->getElementsByClassName(
  1356. $this->getstyle([
  1357. "-webkit-box-orient" => "vertical",
  1358. "display" => "-webkit-box",
  1359. "-webkit-line-clamp" => "2",
  1360. "overflow" => "hidden",
  1361. "word-break" => "break-word"
  1362. ]),
  1363. "div"
  1364. );
  1365. if(count($description) === 0){
  1366. $description = null;
  1367. }else{
  1368. $description =
  1369. html_entity_decode(
  1370. $this->titledots(
  1371. $this->fuckhtml
  1372. ->getTextContent(
  1373. $description[0]
  1374. )
  1375. )
  1376. );
  1377. }
  1378. // get author + date posted
  1379. $metadiv =
  1380. $this->fuckhtml
  1381. ->getElementsByClassName(
  1382. $this->getstyle([
  1383. "margin-top" => "12px"
  1384. ]),
  1385. "div"
  1386. );
  1387. $author = null;
  1388. $date = null;
  1389. if(count($metadiv) !== 0){
  1390. $metadiv =
  1391. explode(
  1392. "·",
  1393. $this->fuckhtml
  1394. ->getTextContent(
  1395. $metadiv[0]
  1396. )
  1397. );
  1398. if(count($metadiv) === 3){
  1399. $author = trim($metadiv[1]);
  1400. $date = strtotime(trim($metadiv[2]));
  1401. }elseif(count($metadiv) === 2){
  1402. $author = trim($metadiv[0]);
  1403. $date = strtotime(trim($metadiv[1]));
  1404. }
  1405. }
  1406. $thumb = [
  1407. "url" => null,
  1408. "ratio" => null
  1409. ];
  1410. $image =
  1411. $this->fuckhtml
  1412. ->getElementsByTagName(
  1413. "img"
  1414. );
  1415. $duration = null;
  1416. if(
  1417. count($image) !== 0 &&
  1418. isset($image[0]["attributes"]["id"])
  1419. ){
  1420. $thumb = [
  1421. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1422. "ratio" => "16:9"
  1423. ];
  1424. // get duration
  1425. $duration =
  1426. $this->fuckhtml
  1427. ->getElementsByClassName(
  1428. $this->getstyle([
  1429. "background-color" => "rgba(0,0,0,0.6)",
  1430. "color" => "#fff",
  1431. "fill" => "#fff"
  1432. ])
  1433. );
  1434. if(count($duration) !== 0){
  1435. $duration =
  1436. $this->hms2int(
  1437. $this->fuckhtml
  1438. ->getTextContent(
  1439. $duration[0]
  1440. ));
  1441. }else{
  1442. $duration = null;
  1443. }
  1444. }
  1445. $out["video"][] = [
  1446. "title" =>
  1447. $this->titledots(
  1448. $this->fuckhtml
  1449. ->getTextContent(
  1450. $title[0]
  1451. )
  1452. ),
  1453. "description" => $description,
  1454. "author" => [
  1455. "name" => $author,
  1456. "url" => null,
  1457. "avatar" => null
  1458. ],
  1459. "date" => $date,
  1460. "duration" => $duration,
  1461. "views" => null,
  1462. "thumb" => $thumb,
  1463. "url" =>
  1464. $this->fuckhtml
  1465. ->getTextContent(
  1466. $url[0]["attributes"]["href"]
  1467. )
  1468. ];
  1469. }
  1470. return $out;
  1471. }
  1472. public function news($get){
  1473. if($get["npt"]){
  1474. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1475. $req = json_decode($req, true);
  1476. $container = $req["c"];
  1477. $req = $req["u"];
  1478. $data = $this->get(
  1479. $proxy,
  1480. "https://www.google.com" . $req,
  1481. [],
  1482. $container
  1483. );
  1484. $html = &$data["data"];
  1485. $container = &$data["container"];
  1486. }else{
  1487. $search = $get["s"];
  1488. $country = $get["country"];
  1489. $nsfw = $get["nsfw"];
  1490. $older = $get["older"];
  1491. $newer = $get["newer"];
  1492. $sort = $get["sort"];
  1493. $container = null;
  1494. $proxy = $this->backend->get_ip();
  1495. $params = [
  1496. "q" => $search,
  1497. "tbm" => "nws",
  1498. "hl" => "en"
  1499. ];
  1500. // country
  1501. if($country != "any"){
  1502. $params["gl"] = $country;
  1503. }
  1504. // nsfw
  1505. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1506. $tbs = [];
  1507. // get date
  1508. $older = $older === false ? null : date("m/d/Y", $older);
  1509. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1510. if(
  1511. $older !== null ||
  1512. $newer !== null
  1513. ){
  1514. $tbs["cdr"] = "1";
  1515. $tbs["cd_min"] = $newer;
  1516. $tbs["cd_max"] = $older;
  1517. }
  1518. // relevance
  1519. if($sort == "date"){
  1520. $tbs["sbd"] = "1";
  1521. }
  1522. // append tbs
  1523. if(count($tbs) !== 0){
  1524. $params["tbs"] = "";
  1525. foreach($tbs as $key => $value){
  1526. $params["tbs"] .= $key . ":" . $value . ",";
  1527. }
  1528. $params["tbs"] = rtrim($params["tbs"], ",");
  1529. }
  1530. //$html = file_get_contents("scraper/google.html");
  1531. $data = $this->get(
  1532. $proxy,
  1533. "https://www.google.com/search",
  1534. $params
  1535. );
  1536. $html = &$data["data"];
  1537. $container = &$data["container"];
  1538. }
  1539. $out = [
  1540. "status" => "ok",
  1541. "npt" => null,
  1542. "news" => []
  1543. ];
  1544. $this->fuckhtml->load($html);
  1545. // parse styles
  1546. $this->parsestyles();
  1547. $this->detect_sorry();
  1548. // get images
  1549. $this->scrape_dimg($html);
  1550. $center_col =
  1551. $this->fuckhtml
  1552. ->getElementById(
  1553. "center_col",
  1554. "div"
  1555. );
  1556. if($center_col === null){
  1557. throw new Exception("Could not grep result div");
  1558. }
  1559. $this->fuckhtml->load($center_col);
  1560. // get next page
  1561. $npt =
  1562. $this->fuckhtml
  1563. ->getElementById(
  1564. "pnnext",
  1565. "a"
  1566. );
  1567. if($npt !== false){
  1568. $out["npt"] =
  1569. $this->backend->store(
  1570. json_encode([
  1571. "u" =>
  1572. $this->fuckhtml
  1573. ->getTextContent(
  1574. $npt["attributes"]
  1575. ["href"]
  1576. ),
  1577. "c" => $container
  1578. ]),
  1579. "news",
  1580. $proxy
  1581. );
  1582. }
  1583. $as =
  1584. $this->fuckhtml
  1585. ->getElementsByAttributeName(
  1586. "jsname",
  1587. "a"
  1588. );
  1589. foreach($as as $a){
  1590. $this->fuckhtml->load($a);
  1591. // get title
  1592. $title =
  1593. $this->fuckhtml
  1594. ->getElementsByAttributeValue(
  1595. "role",
  1596. "heading",
  1597. "div"
  1598. );
  1599. if(count($title) === 0){
  1600. continue;
  1601. }
  1602. $title =
  1603. $this->titledots(
  1604. $this->fuckhtml
  1605. ->getTextContent(
  1606. $title[0]
  1607. )
  1608. );
  1609. // get thumbnail
  1610. $image =
  1611. $this->fuckhtml
  1612. ->getElementsByAttributeName(
  1613. "id",
  1614. "img"
  1615. );
  1616. // check for padded title node, if found, we're inside a carousel
  1617. $probe =
  1618. $this->fuckhtml
  1619. ->getElementsByClassName(
  1620. $this->getstyle(
  1621. [
  1622. "padding" => "16px 16px 40px 16px"
  1623. ]
  1624. ),
  1625. "div"
  1626. );
  1627. if(count($probe) !== 0){
  1628. $probe = true;
  1629. }else{
  1630. $probe = false;
  1631. }
  1632. if(
  1633. count($image) !== 0 &&
  1634. !isset($image[0]["attributes"]["width"])
  1635. ){
  1636. $thumb = [
  1637. "url" =>
  1638. $this->getdimg(
  1639. $image[0]["attributes"]["id"]
  1640. ),
  1641. "ratio" => $probe === true ? "16:9" : "1:1"
  1642. ];
  1643. }else{
  1644. $thumb = [
  1645. "url" => null,
  1646. "ratio" => null
  1647. ];
  1648. }
  1649. $description = null;
  1650. if($probe === false){
  1651. $desc_divs =
  1652. $this->fuckhtml
  1653. ->getElementsByAttributeName(
  1654. "style",
  1655. "div"
  1656. );
  1657. foreach($desc_divs as $desc){
  1658. if(
  1659. strpos(
  1660. $desc["attributes"]["style"],
  1661. "margin-top:"
  1662. ) !== false
  1663. ){
  1664. $description =
  1665. $this->titledots(
  1666. $this->fuckhtml
  1667. ->getTextContent(
  1668. $desc
  1669. )
  1670. );
  1671. break;
  1672. }
  1673. }
  1674. }
  1675. // get author
  1676. $author =
  1677. $this->fuckhtml
  1678. ->getElementsByClassName(
  1679. $this->getstyle(
  1680. [
  1681. "overflow" => "hidden",
  1682. "text-align" => "left",
  1683. "text-overflow" => "ellipsis",
  1684. "white-space" => "nowrap",
  1685. "margin-bottom" => "8px"
  1686. ]
  1687. ),
  1688. "div"
  1689. );
  1690. if(count($author) !== 0){
  1691. $author =
  1692. $this->fuckhtml
  1693. ->getTextContent(
  1694. $author[0]
  1695. );
  1696. }else{
  1697. $author = null;
  1698. }
  1699. // get date
  1700. $date = null;
  1701. $date_div =
  1702. $this->fuckhtml
  1703. ->getElementsByAttributeName(
  1704. "style",
  1705. "div"
  1706. );
  1707. foreach($date_div as $d){
  1708. $this->fuckhtml->load($d);
  1709. $span =
  1710. $this->fuckhtml
  1711. ->getElementsByTagName(
  1712. "span"
  1713. );
  1714. if(
  1715. strpos(
  1716. $d["attributes"]["style"],
  1717. "bottom:"
  1718. ) !== false
  1719. ){
  1720. $date =
  1721. strtotime(
  1722. $this->fuckhtml
  1723. ->getTextContent(
  1724. $span[count($span) - 1]
  1725. )
  1726. );
  1727. break;
  1728. }
  1729. }
  1730. $out["news"][] = [
  1731. "title" => $title,
  1732. "author" => $author,
  1733. "description" => $description,
  1734. "date" => $date,
  1735. "thumb" => $thumb,
  1736. "url" =>
  1737. $this->unshiturl(
  1738. $a["attributes"]
  1739. ["href"]
  1740. )
  1741. ];
  1742. }
  1743. return $out;
  1744. }
  1745. private function scrape_dimg($html){
  1746. // get images loaded through javascript
  1747. $this->dimg = [];
  1748. preg_match_all(
  1749. '/function\(\){google\.ldi=({.*?});/',
  1750. $html,
  1751. $dimg
  1752. );
  1753. if(isset($dimg[1])){
  1754. foreach($dimg[1] as $i){
  1755. $tmp = json_decode($i, true);
  1756. foreach($tmp as $key => $value){
  1757. $this->dimg[$key] =
  1758. $this->unshit_thumb(
  1759. $value
  1760. );
  1761. }
  1762. }
  1763. }
  1764. // get additional javascript base64 images
  1765. preg_match_all(
  1766. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  1767. $html,
  1768. $dimg
  1769. );
  1770. if(isset($dimg[1])){
  1771. for($i=0; $i<count($dimg[1]); $i++){
  1772. $delims = explode(",", $dimg[2][$i]);
  1773. $string =
  1774. $this->fuckhtml
  1775. ->parseJsString(
  1776. $dimg[1][$i]
  1777. );
  1778. foreach($delims as $delim){
  1779. $this->dimg[trim($delim, "'")] = $string;
  1780. }
  1781. }
  1782. }
  1783. }
  1784. private function scrape_imagearr($html){
  1785. // get image links arrays
  1786. preg_match_all(
  1787. '/\[[0-9]+,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  1788. $html,
  1789. $image_arr
  1790. );
  1791. $this->image_arr = [];
  1792. if(isset($image_arr[1])){
  1793. for($i=0; $i<count($image_arr[1]); $i++){
  1794. $original =
  1795. $this->fuckhtml
  1796. ->parseJsString(
  1797. $image_arr[5][$i]
  1798. );
  1799. if(
  1800. preg_match(
  1801. '/^x-raw-image/',
  1802. $original
  1803. )
  1804. ){
  1805. // only add thumbnail, google doesnt have OG resolution
  1806. $this->image_arr[$image_arr[1][$i]] = [
  1807. [
  1808. "url" =>
  1809. $this->unshit_thumb(
  1810. $this->fuckhtml
  1811. ->parseJsString(
  1812. $image_arr[2][$i]
  1813. )
  1814. ),
  1815. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  1816. "height" => (int)$image_arr[6][$i]
  1817. ]
  1818. ];
  1819. continue;
  1820. }
  1821. $this->image_arr[$image_arr[1][$i]] =
  1822. [
  1823. [
  1824. "url" => $original,
  1825. "width" => (int)$image_arr[7][$i],
  1826. "height" => (int)$image_arr[6][$i]
  1827. ],
  1828. [
  1829. "url" =>
  1830. $this->unshit_thumb(
  1831. $this->fuckhtml
  1832. ->parseJsString(
  1833. $image_arr[2][$i]
  1834. )
  1835. ),
  1836. "width" => (int)$image_arr[4][$i],
  1837. "height" => (int)$image_arr[3][$i]
  1838. ]
  1839. ];
  1840. }
  1841. }
  1842. }
  1843. private function getdimg($dimg){
  1844. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  1845. }
  1846. private function unshit_thumb($url, $get_bigger_res = false){
  1847. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  1848. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  1849. $parts = parse_url($url);
  1850. if(
  1851. isset($parts["host"]) &&
  1852. preg_match(
  1853. '/(?:encrypted-)?tbn.*\.gstatic\.com/',
  1854. $parts["host"]
  1855. )
  1856. ){
  1857. parse_str($parts["query"], $params);
  1858. if(isset($params["q"])){
  1859. if($get_bigger_res){
  1860. // this method doesnt always work, but does work for wiki thumbnails
  1861. return
  1862. "https://" . $parts["host"] . "/images?q=tbn:" .
  1863. $this->base64url_encode(
  1864. substr(
  1865. $this->base64url_decode(
  1866. explode(
  1867. ":",
  1868. $params["q"])[1]
  1869. ),
  1870. 0,
  1871. 29
  1872. )
  1873. );
  1874. }else{
  1875. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  1876. }
  1877. }
  1878. }
  1879. return $url;
  1880. }
  1881. private function parsestyles(){
  1882. $styles = [];
  1883. $style_div =
  1884. $this->fuckhtml
  1885. ->getElementsByTagName(
  1886. "style"
  1887. );
  1888. $raw_styles = "";
  1889. foreach($style_div as $style){
  1890. $raw_styles .= $style["innerHTML"];
  1891. }
  1892. // filter out media/keyframe queries
  1893. $raw_styles =
  1894. preg_replace(
  1895. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  1896. "",
  1897. $raw_styles
  1898. );
  1899. // get styles
  1900. preg_match_all(
  1901. '/(.+?){([\S\s]*?)}/',
  1902. $raw_styles,
  1903. $matches
  1904. );
  1905. for($i=0; $i<count($matches[1]); $i++){
  1906. // get style values
  1907. preg_match_all(
  1908. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  1909. $matches[2][$i],
  1910. $values_regex
  1911. );
  1912. $values = [];
  1913. for($k=0; $k<count($values_regex[1]); $k++){
  1914. $values[trim($values_regex[1][$k])] =
  1915. strtolower(trim($values_regex[2][$k]));
  1916. }
  1917. $names = explode(",", $matches[1][$i]);
  1918. // h1,h2,h3 will each get their own array index
  1919. foreach($names as $name){
  1920. $name = trim($name, "}\t\n\r\0\x0B");
  1921. foreach($values as $key => $value){
  1922. $styles[$name][$key] = $value;
  1923. }
  1924. }
  1925. }
  1926. foreach($styles as $key => $values){
  1927. $styles[$key]["_c"] = count($values);
  1928. }
  1929. $this->styles = $styles;
  1930. // get CSS colors
  1931. $this->css_colors = [];
  1932. if(isset($this->styles[":root"])){
  1933. foreach($this->styles[":root"] as $key => $value){
  1934. $this->css_colors[$value] = strtolower($key);
  1935. }
  1936. }
  1937. }
  1938. private function getstyle($styles){
  1939. $styles["_c"] = count($styles);
  1940. foreach($this->styles as $style_key => $style_values){
  1941. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  1942. $style_key =
  1943. explode(" ", $style_key);
  1944. $style_key = $style_key[count($style_key) - 1];
  1945. return
  1946. ltrim(
  1947. str_replace(
  1948. [".", "#"],
  1949. " ",
  1950. $style_key
  1951. )
  1952. );
  1953. }
  1954. }
  1955. return false;
  1956. }
  1957. private function getcolorvar($color){
  1958. if(isset($this->css_colors[$color])){
  1959. return $this->css_colors[$color];
  1960. }
  1961. return null;
  1962. }
  1963. private function unshiturl($url, $return_size = false){
  1964. // decode
  1965. $url =
  1966. $this->fuckhtml
  1967. ->getTextContent(
  1968. $url
  1969. );
  1970. $url_parts = parse_url($url);
  1971. if(isset($url_parts["query"])){
  1972. parse_str($url_parts["query"], $query);
  1973. }else{
  1974. $query = [];
  1975. }
  1976. if(
  1977. !isset(
  1978. $url_parts["host"]
  1979. ) ||
  1980. stripos($url_parts["host"], "google.") !== false
  1981. ){
  1982. // no host, we have a tracking url
  1983. if(isset($query["imgurl"])){
  1984. $url = $query["imgurl"];
  1985. }
  1986. elseif(isset($query["q"])){
  1987. $url = $query["q"];
  1988. }
  1989. }
  1990. // rewrite URLs to remove extra tracking parameters
  1991. $domain = parse_url($url, PHP_URL_HOST);
  1992. if(
  1993. preg_match(
  1994. '/wikipedia.org$/',
  1995. $domain
  1996. )
  1997. ){
  1998. // rewrite wikipedia mobile URLs to desktop
  1999. $url =
  2000. $this->replacedomain(
  2001. $url,
  2002. preg_replace(
  2003. '/([a-z0-9]+)(\.m\.)/',
  2004. '$1.',
  2005. $domain
  2006. )
  2007. );
  2008. }
  2009. elseif(
  2010. preg_match(
  2011. '/imdb\.com$|youtube\.[^.]+$/',
  2012. $domain
  2013. )
  2014. ){
  2015. // rewrite imdb and youtube mobile URLs too
  2016. $url =
  2017. $this->replacedomain(
  2018. $url,
  2019. preg_replace(
  2020. '/^m\./',
  2021. "",
  2022. $domain
  2023. )
  2024. );
  2025. }
  2026. elseif(
  2027. preg_match(
  2028. '/play\.google\.[^.]+$/',
  2029. $domain
  2030. )
  2031. ){
  2032. // remove referrers from play.google.com
  2033. $u_query = parse_url($url, PHP_URL_QUERY);
  2034. if($u_query !== null){
  2035. parse_str($u_query, $u_query);
  2036. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2037. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2038. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2039. $query = http_build_query($query);
  2040. $url =
  2041. str_replace(
  2042. $u_query,
  2043. $u_query,
  2044. $url
  2045. );
  2046. }
  2047. }
  2048. elseif(
  2049. preg_match(
  2050. '/twitter\.com$/',
  2051. $domain
  2052. )
  2053. ){
  2054. // remove more referrers from twitter.com
  2055. $u_query = parse_url($url, PHP_URL_QUERY);
  2056. if($u_query !== null){
  2057. parse_str($u_query, $u_query);
  2058. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2059. $u_query = http_build_query($u_query);
  2060. $url =
  2061. str_replace(
  2062. $oldquery,
  2063. $u_query,
  2064. $url
  2065. );
  2066. }
  2067. }
  2068. elseif(
  2069. preg_match(
  2070. '/maps\.google\.[^.]+/',
  2071. $domain
  2072. )
  2073. ){
  2074. if(stripos($url, "maps?") !== false){
  2075. $u_query = parse_url($url, PHP_URL_QUERY);
  2076. if($u_query !== null){
  2077. parse_str($u_query, $u_query);
  2078. if(isset($u_query["daddr"])){
  2079. $url =
  2080. "https://maps.google.com/maps?daddr=" .
  2081. urlencode($u_query["daddr"]);
  2082. }
  2083. }
  2084. }
  2085. }
  2086. if($return_size){
  2087. return [
  2088. "url" => $url,
  2089. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2090. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2091. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2092. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2093. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2094. ];
  2095. }
  2096. return $url;
  2097. }
  2098. private function replacedomain($url, $domain){
  2099. return
  2100. preg_replace(
  2101. '/(https?:\/\/)([^\/]+)/',
  2102. '$1' . $domain,
  2103. $url
  2104. );
  2105. }
  2106. private function titledots($title){
  2107. return trim($title, " .\t\n\r\0\x0B");
  2108. }
  2109. private function hms2int($time){
  2110. $parts = explode(":", $time, 3);
  2111. $time = 0;
  2112. if(count($parts) === 3){
  2113. // hours
  2114. $time = $time + ((int)$parts[0] * 3600);
  2115. array_shift($parts);
  2116. }
  2117. if(count($parts) === 2){
  2118. // minutes
  2119. $time = $time + ((int)$parts[0] * 60);
  2120. array_shift($parts);
  2121. }
  2122. // seconds
  2123. $time = $time + (int)$parts[0];
  2124. return $time;
  2125. }
  2126. function base64url_decode($data){
  2127. $b64 = strtr($data, "-_", "+/");
  2128. $pad = strlen($b64) % 4;
  2129. if ($pad) $b64 .= str_repeat("=", 4 - $pad);
  2130. return base64_decode($b64);
  2131. }
  2132. function base64url_encode($data){
  2133. return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
  2134. }
  2135. private function detect_sorry(){
  2136. // detect recaptcha
  2137. $captcha_form =
  2138. $this->fuckhtml
  2139. ->getElementById(
  2140. "captcha-form",
  2141. "form"
  2142. );
  2143. if($captcha_form !== false){
  2144. throw new Exception("Google returned a captcha");
  2145. }
  2146. // detect JS challenge
  2147. $title =
  2148. $this->fuckhtml
  2149. ->getElementsByTagName(
  2150. "title"
  2151. );
  2152. if(
  2153. count($title) !== 0 &&
  2154. $this->fuckhtml
  2155. ->getTextContent(
  2156. $title[0]
  2157. ) == "Google Search"
  2158. ){
  2159. throw new Exception("Google returned a JS challenge");
  2160. }
  2161. // detect soft captcha
  2162. // "Verifying your request
  2163. // You'll be able to continue in a few seconds. Don't refresh this page."
  2164. // getting this mostly on mid-trust IPs
  2165. $softcaptcha =
  2166. $this->fuckhtml
  2167. ->getElementsByAttributeValue(
  2168. "role",
  2169. "progressbar",
  2170. "div"
  2171. );
  2172. if(count($softcaptcha) !== 0){
  2173. throw new Exception("Google returned a soft captcha");
  2174. }
  2175. }
  2176. }