solofield.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <?php
  2. class solofield{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("solofield");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. public function getfilters($page){
  10. return [
  11. "nsfw" => [
  12. "display" => "NSFW",
  13. "option" => [
  14. "yes" => "Yes",
  15. "no" => "No",
  16. ]
  17. ]
  18. ];
  19. }
  20. private function get($proxy, $url, $get = []){
  21. $curlproc = curl_init();
  22. if($get !== []){
  23. $get = http_build_query($get);
  24. $url .= "?" . $get;
  25. }
  26. curl_setopt($curlproc, CURLOPT_URL, $url);
  27. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  28. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  29. ["User-Agent: " . config::USER_AGENT,
  30. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  31. "Accept-Language: en-US,en;q=0.5",
  32. "Accept-Encoding: gzip",
  33. "Referer: https://solofield.net",
  34. "DNT: 1",
  35. "Connection: keep-alive",
  36. "Cookie: cross-site-cookie=name; lno=35842050",
  37. "Upgrade-Insecure-Requests: 1",
  38. "Sec-Fetch-Dest: document",
  39. "Sec-Fetch-Mode: navigate",
  40. "Sec-Fetch-Site: same-origin",
  41. "Sec-Fetch-User: ?1"]
  42. );
  43. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  45. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  46. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  47. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  48. $this->backend->assign_proxy($curlproc, $proxy);
  49. $data = curl_exec($curlproc);
  50. if(curl_errno($curlproc)){
  51. throw new Exception(curl_error($curlproc));
  52. }
  53. curl_close($curlproc);
  54. return $data;
  55. }
  56. public function web($get){
  57. if($get["npt"]){
  58. [$query, $proxy] = $this->backend->get($get["npt"], "web");
  59. try{
  60. $html =
  61. $this->get(
  62. $proxy,
  63. "https://solofield.net/search?" . $query,
  64. []
  65. );
  66. }catch(Exception $error){
  67. throw new Exception("Failed to fetch search page");
  68. }
  69. }else{
  70. $proxy = $this->backend->get_ip();
  71. try{
  72. $html =
  73. $this->get(
  74. $proxy,
  75. "https://solofield.net/search",
  76. [
  77. "q" => $get["s"],
  78. "ie" => "UTF-8",
  79. "oe" => "UTF-8",
  80. "hl" => "ja", // changing this doesnt do anything
  81. "lr" => "lang_ja", // same here
  82. //"ls" => "", // ??
  83. "f" => ($get["nsfw"] == "yes" ? "off" : "on")
  84. ]
  85. );
  86. }catch(Exception $error){
  87. throw new Exception("Failed to fetch search page");
  88. }
  89. }
  90. $out = [
  91. "status" => "ok",
  92. "spelling" => [
  93. "type" => "no_correction",
  94. "using" => null,
  95. "correction" => null
  96. ],
  97. "npt" => null,
  98. "answer" => [],
  99. "web" => [],
  100. "image" => [],
  101. "video" => [],
  102. "news" => [],
  103. "related" => []
  104. ];
  105. // check for errors and load the result div
  106. if($this->error_and_load($html)){
  107. return $out;
  108. }
  109. $items =
  110. $this->fuckhtml
  111. ->getElementsByClassName(
  112. "g0",
  113. "li"
  114. );
  115. foreach($items as $item){
  116. $this->fuckhtml->load($item);
  117. $title_tag =
  118. $this->fuckhtml
  119. ->getElementsByClassName(
  120. "r",
  121. "h3"
  122. );
  123. if(count($title_tag) === 0){
  124. continue;
  125. }
  126. $this->fuckhtml->load($title_tag[0]);
  127. $link =
  128. $this->fuckhtml
  129. ->getTextContent(
  130. $this->fuckhtml
  131. ->getElementsByTagName(
  132. "a"
  133. )[0]
  134. ["attributes"]
  135. ["href"]
  136. );
  137. $this->fuckhtml->load($item);
  138. $thumb =
  139. $this->fuckhtml
  140. ->getElementsByClassName(
  141. "webshot",
  142. "img"
  143. );
  144. if(count($thumb) !== 0){
  145. $uri =
  146. $this->fuckhtml
  147. ->getTextContent(
  148. $thumb[0]
  149. ["attributes"]
  150. ["src"]
  151. );
  152. if(stripos($uri, "now_printing") === false){
  153. $thumb = [
  154. "ratio" => "1:1",
  155. "url" =>
  156. "https://solofield.net" .
  157. $this->fuckhtml
  158. ->getTextContent(
  159. $thumb[0]
  160. ["attributes"]
  161. ["src"]
  162. )
  163. ];
  164. }else{
  165. $thumb = [
  166. "ratio" => null,
  167. "url" => null
  168. ];
  169. }
  170. }else{
  171. $thumb = [
  172. "ratio" => null,
  173. "url" => null
  174. ];
  175. }
  176. $out["web"][] = [
  177. "title" =>
  178. $this->fuckhtml
  179. ->getTextContent(
  180. $title_tag[0]
  181. ),
  182. "description" =>
  183. $this->fuckhtml
  184. ->getTextContent(
  185. $this->fuckhtml
  186. ->getElementsByClassName(
  187. "s",
  188. "div"
  189. )[0]
  190. ),
  191. "url" => $link,
  192. "date" => null,
  193. "type" => "web",
  194. "thumb" => $thumb,
  195. "sublink" => [],
  196. "table" => []
  197. ];
  198. }
  199. // get next page
  200. $this->get_npt($html, $proxy, $out, "web");
  201. return $out;
  202. }
  203. public function image($get){
  204. // no pagination
  205. $html =
  206. $this->get(
  207. $this->backend->get_ip(),
  208. "https://solofield.net/isearch",
  209. [
  210. "q" => $get["s"],
  211. "ie" => "UTF-8",
  212. "oe" => "UTF-8",
  213. "hl" => "ja", // changing this doesnt do anything
  214. //"lr" => "lang_ja", // same here
  215. "ls" => "", // ??
  216. "f" => ($get["nsfw"] == "yes" ? "off" : "on")
  217. ]
  218. );
  219. $out = [
  220. "status" => "ok",
  221. "npt" => null,
  222. "image" => []
  223. ];
  224. // check for errors and load the result div
  225. if($this->error_and_load($html)){
  226. return $out;
  227. }
  228. $images =
  229. $this->fuckhtml
  230. ->getElementsByTagName(
  231. "li"
  232. );
  233. foreach($images as $image){
  234. $this->fuckhtml->load($image);
  235. $img =
  236. $this->fuckhtml
  237. ->getElementsByTagName(
  238. "img"
  239. );
  240. if(count($img) === 0){
  241. // ?? invalid
  242. continue;
  243. }
  244. $img = $img[0];
  245. $size =
  246. explode(
  247. "x",
  248. $this->fuckhtml
  249. ->getTextContent(
  250. $image
  251. ),
  252. 2
  253. );
  254. $size = [
  255. (int)trim($size[0]), // width
  256. (int)trim($size[1]) // height
  257. ];
  258. $out["image"][] = [
  259. "title" => null,
  260. "source" => [
  261. [
  262. "url" =>
  263. "https://solofield.net/" .
  264. $this->fuckhtml
  265. ->getTextContent(
  266. $img["attributes"]["src"]
  267. ),
  268. "width" => $size[0],
  269. "height" => $size[1]
  270. ]
  271. ],
  272. "url" =>
  273. $this->fuckhtml
  274. ->getTextContent(
  275. $this->fuckhtml
  276. ->getElementsByTagName(
  277. "a"
  278. )[0]
  279. ["attributes"]
  280. ["href"]
  281. )
  282. ];
  283. }
  284. return $out;
  285. }
  286. public function video($get){
  287. if($get["npt"]){
  288. [$query, $proxy] = $this->backend->get($get["npt"], "videos");
  289. try{
  290. $html =
  291. $this->get(
  292. $proxy,
  293. "https://solofield.net/vsearch?" . $query,
  294. []
  295. );
  296. }catch(Exception $error){
  297. throw new Exception("Failed to fetch search page");
  298. }
  299. }else{
  300. $proxy = $this->backend->get_ip();
  301. try{
  302. $html =
  303. $this->get(
  304. $proxy,
  305. "https://solofield.net/vsearch",
  306. [
  307. "q" => $get["s"],
  308. "ie" => "UTF-8",
  309. "oe" => "UTF-8",
  310. "hl" => "ja", // changing this doesnt do anything
  311. //"lr" => "lang_ja", // same here
  312. "ls" => "", // ??
  313. "f" => ($get["nsfw"] == "yes" ? "off" : "on")
  314. ]
  315. );
  316. }catch(Exception $error){
  317. throw new Exception("Failed to fetch search page");
  318. }
  319. }
  320. $out = [
  321. "status" => "ok",
  322. "npt" => null,
  323. "video" => [],
  324. "author" => [],
  325. "livestream" => [],
  326. "playlist" => [],
  327. "reel" => []
  328. ];
  329. // check for errors and load the result div
  330. if($this->error_and_load($html)){
  331. return $out;
  332. }
  333. $items =
  334. $this->fuckhtml
  335. ->getElementsByTagName(
  336. "li"
  337. );
  338. foreach($items as $item){
  339. $this->fuckhtml->load($item);
  340. $as =
  341. $this->fuckhtml
  342. ->getElementsByTagName(
  343. "a"
  344. );
  345. if(count($as) === 0){
  346. continue;
  347. }
  348. $thumb =
  349. $this->fuckhtml
  350. ->getElementsByTagName(
  351. "img"
  352. );
  353. if(count($thumb) !== 0){
  354. $thumb = [
  355. "ratio" => "16:9",
  356. "url" =>
  357. "https://solofield.net/" .
  358. $thumb[0]
  359. ["attributes"]
  360. ["src"]
  361. ];
  362. }else{
  363. $thumb = [
  364. "ratio" => null,
  365. "url" => null
  366. ];
  367. }
  368. $date =
  369. $this->fuckhtml
  370. ->getElementsByAttributeValue(
  371. "style",
  372. "font-size: 10px;",
  373. "span"
  374. );
  375. if(count($date) !== 0){
  376. $date =
  377. $this->unfuckdate(
  378. $this->fuckhtml
  379. ->getTextContent(
  380. $date[0]
  381. )
  382. );
  383. }else{
  384. $date = null;
  385. }
  386. $center_td =
  387. $this->fuckhtml
  388. ->getElementsByAttributeValue(
  389. "align",
  390. "center",
  391. "td"
  392. );
  393. if(count($center_td) === 2){
  394. $duration =
  395. $this->fuckhtml
  396. ->getTextContent(
  397. $this->hms2int(
  398. $center_td[0]
  399. )
  400. );
  401. }else{
  402. $duration = null;
  403. }
  404. $out["video"][] = [
  405. "title" =>
  406. $this->fuckhtml
  407. ->getTextContent(
  408. $as[1]
  409. ),
  410. "description" => null,
  411. "author" => [
  412. "name" => null,
  413. "url" => null,
  414. "avatar" => null
  415. ],
  416. "date" => $date,
  417. "duration" => $duration,
  418. "views" => null,
  419. "thumb" => $thumb,
  420. "url" =>
  421. $this->fuckhtml
  422. ->getTextContent(
  423. $as[0]
  424. ["attributes"]
  425. ["href"]
  426. )
  427. ];
  428. }
  429. // get next page
  430. $this->get_npt($html, $proxy, $out, "videos");
  431. return $out;
  432. }
  433. private function get_npt($html, $proxy, &$out, $type){
  434. // get next page
  435. $this->fuckhtml->load($html);
  436. $pjs =
  437. $this->fuckhtml
  438. ->getElementById(
  439. "pjs"
  440. );
  441. if($pjs){
  442. $alnk =
  443. $this->fuckhtml
  444. ->getElementsByClassName(
  445. "alnk",
  446. "span"
  447. );
  448. foreach($alnk as $lnk){
  449. if(
  450. stripos(
  451. $this->fuckhtml
  452. ->getTextContent(
  453. $lnk
  454. ),
  455. "Next"
  456. ) !== false
  457. ){
  458. $this->fuckhtml->load($lnk);
  459. $out["npt"] =
  460. $this->backend->store(
  461. parse_url(
  462. $this->fuckhtml
  463. ->getElementsByTagName(
  464. "a"
  465. )[0]
  466. ["attributes"]
  467. ["href"],
  468. PHP_URL_QUERY
  469. ),
  470. $type,
  471. $proxy
  472. );
  473. }
  474. }
  475. }
  476. }
  477. private function error_and_load($html){
  478. if(strlen($html) === 0){
  479. throw new Exception("Solofield blocked the request IP");
  480. }
  481. $this->fuckhtml->load($html);
  482. $list =
  483. $this->fuckhtml
  484. ->getElementById(
  485. "list",
  486. "div"
  487. );
  488. if($list === false){
  489. $nosearch =
  490. $this->fuckhtml
  491. ->getElementById(
  492. "nosearch",
  493. "div"
  494. );
  495. if($nosearch){
  496. return true;
  497. }
  498. throw new Exception("Failed to grep search list");
  499. }
  500. $this->fuckhtml->load($list);
  501. return false;
  502. }
  503. private function unfuckdate($date){
  504. return
  505. strtotime(
  506. rtrim(
  507. preg_replace(
  508. '/[^0-9]+/',
  509. "-",
  510. explode(
  511. ":",
  512. $date,
  513. 2
  514. )[1]
  515. ),
  516. "-"
  517. )
  518. );
  519. }
  520. private function hms2int($time){
  521. $parts = explode(":", $time, 3);
  522. $time = 0;
  523. if(count($parts) === 3){
  524. // hours
  525. $time = $time + ((int)$parts[0] * 3600);
  526. array_shift($parts);
  527. }
  528. if(count($parts) === 2){
  529. // minutes
  530. $time = $time + ((int)$parts[0] * 60);
  531. array_shift($parts);
  532. }
  533. // seconds
  534. $time = $time + (int)$parts[0];
  535. return $time;
  536. }
  537. }