1
0

google.php 64 KB

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