crawler.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?PHP
  2. /*
  3. * Mbfier, the gallery bot
  4. * Copyright (C) 2021 Daniele Bonini
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. header("Content-Type: text/javascript");
  21. $host1 = filter_input(INPUT_GET, "h", FILTER_SANITIZE_STRING);
  22. if ($host1 == "mbfier.com") {
  23. $host = "mbfier.com";
  24. } else if ($host1 === "mbfy.it") {
  25. $host = "mbfy.it";
  26. } else {
  27. echo("hots parameter error.");
  28. exit(0);
  29. }
  30. $targetDomain = filter_input(INPUT_GET, "td", FILTER_SANITIZE_STRING);
  31. if (substr($targetDomain, 0, 4) == "www.") {
  32. $cleanTargetDomain = substr($targetDomain, 4);
  33. } else {
  34. $cleanTargetDomain = $targetDomain;
  35. }
  36. $cleanTargetDomain = ucfirst($cleanTargetDomain);
  37. $ipos = mb_strripos("~" . $cleanTargetDomain, ".");
  38. if ($ipos) {
  39. $galTitle = strtoupper(substr($cleanTargetDomain, 0, $ipos-1));
  40. } else {
  41. $targetDomain = $targetDomain . ".com";
  42. echo("window.open('http://" . $targetDomain . "." . $host . "','_self');");
  43. exit(0);
  44. }
  45. $output1 = filter_input(INPUT_GET, "out", FILTER_SANITIZE_STRING);
  46. $output1 = strtolower($output1);
  47. if ($output1 == "json") {
  48. $output = 2; // set flag for json output
  49. } else if ($output1 === "std") {
  50. $output = 1; // set flag for standard output
  51. } else {
  52. echo("out parameter error.");
  53. exit(0);
  54. }
  55. $verbose1 = filter_input(INPUT_GET, "v", FILTER_SANITIZE_STRING);
  56. if ($verbose1 == "0") {
  57. $verbose = 0; // set flag for defalt app verbosity
  58. } else if ($verbose1 === "1") {
  59. $verbose = 1; // set flag for quite verbosity
  60. } else {
  61. echo("verbose parameter error.");
  62. exit(0);
  63. }
  64. ?>
  65. /*
  66. * Mbfier, the gallery bot
  67. * Copyright (C) 2021 Daniele Bonini
  68. *
  69. * This program is free software: you can redistribute it and/or modify
  70. * it under the terms of the GNU General Public License as published by
  71. * the Free Software Foundation, either version 3 of the License, or
  72. * (at your option) any later version.
  73. *
  74. * This program is distributed in the hope that it will be useful,
  75. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  76. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  77. * GNU General Public License for more details.
  78. *
  79. * You should have received a copy of the GNU General Public License
  80. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  81. *
  82. */
  83. var recNum = 0;
  84. var nImgProcessed = 0;
  85. var output = <?PHP echo($output); ?>;
  86. var verbose = <?PHP echo($verbose); ?>;
  87. var host = "<?PHP echo($host); ?>";
  88. var targetDomain = "<?PHP echo($targetDomain);?>";
  89. var galTitle = "<?PHP echo($galTitle);?>";
  90. var landingPage = "<?PHP echo($cleanTargetDomain);?>";
  91. var crawlres = [];
  92. var logoFound = false;
  93. function loadDoc() {
  94. recNum++;
  95. bConnectionOK = false;
  96. if (verbose===0 && recNum===1) {
  97. document.write("Copyrights (C) 2021 Daniele Bonini. GNU General Public License ver3<br>");
  98. document.write("<br>");
  99. document.write("This is a bit of doc:<br>");
  100. document.write("- out: std (for standard output) or json<br>");
  101. document.write("- v: 0 (for standard verbosity) or 1 (for quite output)<br>");
  102. document.write("<br>");
  103. document.write("The service is running and trying to connect to the target server.<br>");
  104. document.write("The connection to the target web server can eventually fail.<br>");
  105. document.write("Cause could be found among the following reasons:<br>");
  106. document.write("- Internet connection lost.<br>");
  107. document.write("- DNS failure.<br>");
  108. document.write("- Target server is down.<br>");
  109. document.write("- Access-Control-Allow-Origin header missing on the target server.<br>");
  110. document.write("<br>");
  111. }
  112. var xhttp = new XMLHttpRequest();
  113. if (host == "mbfier.com") {
  114. xhttp.open("GET", "http://"+targetDomain+".mbfier.com/upload.php?url=http://"+targetDomain, true);
  115. } else {
  116. xhttp.open("GET", "http://"+targetDomain+".mbfy.it/upload.php?url=http://"+targetDomain, true);
  117. }
  118. xhttp.send();
  119. xhttp.onreadystatechange = function() {
  120. if (this.readyState == 4 && this.status == 200) {
  121. bConnectionOK = true;
  122. if (verbose===0) {
  123. document.write("Connection succeeded.<br><br>")
  124. }
  125. try {
  126. document.getElementById("grausi").value = this.responseText;
  127. }
  128. catch (error) {
  129. //console.error(error);
  130. if (!document.getElementById("grausi")) {
  131. document.write("<input id='grausi' type='hidden' value=''>");
  132. document.getElementById("grausi").value = this.responseText;
  133. }
  134. }
  135. finally {
  136. document.getElementById("grausi").addEventListener("change", launchParsing(), true);
  137. }
  138. } else {
  139. if ((this.readyState == 4 && this.status == 0) && (!bConnectionOK)) {
  140. if (verbose===0) {
  141. document.write("Final result:<br>");
  142. document.write("Connection problems detected.<br><br>");
  143. }
  144. }
  145. }
  146. }
  147. }
  148. function writeHeader() {
  149. //TITLE BAR
  150. if ((output == 1 && verbose == 1) && (nImgProcessed==0 && recNum==1)) {
  151. divTitle = "<div style='position:relative;margin-top:8px;top:0px;left:0px;width:100%'>";
  152. document.write(divTitle);
  153. divTitle = "<div style='height:50px;text-align:center;opacity:1.0;width:350px;clear:both;margin:auto;vertical-align:middle;border:0px solid green;background: rgba(3, 169, 245, 0.7);color:#FFFFFF;'><span style='position:relative;top:-15px;font-size:25px;font-weight:900;'><br>"+galTitle+"<br><br></span></div></div>";
  154. document.write(divTitle);
  155. }
  156. // END TITLE
  157. }
  158. function crawlImages() {
  159. var bTitle = false;
  160. var found = "||";
  161. var z = 0;
  162. var str = document.getElementById('grausi').value;
  163. str = str.replace("/\n/"," ");
  164. //var regexp = /<img.*?src="[^*?"<>|]+".*?>/gi;
  165. //var regexp = /("|')([^*?"<>|]+\.(png|gif|jpg|jpeg))("|')/gi;
  166. //var regexp = /<img\n?.*src=("|')?([^:*?"<>|]+\.(png|gif|jpg|jpeg))("|')?.*?\n?\/?>/gis;
  167. //var regexp = /<img.*\n?.*src=("|')?(?!(cid:|data:))([^*?"<>|]+\.(png|gif|jpg|jpeg))("|')?.*?\n?[^*?"<>|]*\/?>/gi;
  168. var regexp = /<img\n?.*?\n?\/?>/gis;
  169. var x = str.match(regexp);
  170. if (x) {
  171. for (i=0;i<x.length;i++) {
  172. str2 = x[i]; //.toLowerCase();
  173. str3 = str2.toLowerCase();
  174. if (str2.length <= 4) {
  175. continue;
  176. }
  177. if ((str2.indexOf("blank.gif")>-1 || str2.indexOf("pixel.gif")>-1 || str2.indexOf("pxl.gif")>-1 || str2.indexOf("pix.gif")>-1) && str2.indexOf("data-src")==-1) {
  178. continue;
  179. }
  180. if (str2.indexOf(" src=\"data:")>-1 || str2.indexOf(" src='data:")>-1 || str2.indexOf(" src=data:")>-1) {
  181. str2 = str2.replace(" src=", " zzz=");
  182. str2 = str2.replace(" data-src=", " src=");
  183. }
  184. if (str3.indexOf("credit-card")>-1 || str3.indexOf("loyalty")>-1) {
  185. continue;
  186. }
  187. oo = str2.indexOf(" src=");
  188. ii = str2.indexOf(" data-src=");
  189. jj = str2.indexOf(".jpg");
  190. yy = str2.indexOf(".jpeg");
  191. kk = str2.indexOf(".png");
  192. ww = str2.indexOf(".gif");
  193. if (((oo==-1) && (oo<ii)) && ii>-1 && ((jj>ii) || (yy>ii) || (kk>ii) || (ww>ii))) {
  194. //alert(str2);
  195. //var regexp3 = /<img\n?.*data-src=("|')([^*?"<>|]+\.(png|gif|jpg|jpeg))("|').*?\n?\/?>/gis;
  196. //var zz = str2.match(regexp3);
  197. //if (zz[0]) {
  198. //str3 = zz[0];
  199. str2 = str2.replace(" src=", " zzz=");
  200. str2 = str2.replace(" data-src=", " src=");
  201. //str2 = str3;
  202. //}
  203. }
  204. str2 = str2.trim();
  205. if (str2 !== "") {
  206. newurl = str2;
  207. //var regexp2 = /([\w\.-]+\.[a-z\.]{2,8})/gi;
  208. //var regexp2 = /([\w\.-]+\.(png|jpg|jpeg))/gi;
  209. //var regexp2 = /src="([\w\:\/\.-]+\.(png|jpg|jpeg))"/gi;
  210. //var regexp2 = /<img.*?src="[^*?"<>|]+".*?>/gi;
  211. //var regexp2 = /("|')([^*?"<>|]+\.(png|gif|jpg|jpeg))("|')/gi;
  212. //var regexp2 = /<img\n?.*src=("|')([^*?"<>|]+\.(png|gif|jpg|jpeg))("|').*?>/gi;
  213. var regexp2 = /<img\n?.*?\n?\/?>/gis;
  214. var y = newurl.match(regexp2);
  215. if (y[0]) {
  216. if (found.indexOf("|" + y[0] + "|") == -1) {
  217. crawlres[z] = y[0];
  218. z++;
  219. found = found + "|" + y[0] + "|";
  220. }
  221. }
  222. }
  223. }
  224. }
  225. /*
  226. * DUMP
  227. */
  228. dump = false;
  229. if (dump || (verbose == 0) || ((output==2) && (verbose == 1))) {
  230. for (i=0;i<crawlres.length;i++) {
  231. str = crawlres[i];
  232. str = str.replace("<", "&lt;");
  233. str = str.replace(">", "&gt;");
  234. crawlres[i] = str;
  235. }
  236. }
  237. if (output === 1) {
  238. if (verbose === 0) {
  239. document.write("Image list:<br>");
  240. }
  241. /*
  242. * PATH FIX
  243. */
  244. for (i=0;i<crawlres.length;i++) {
  245. str = crawlres[i];
  246. ipos = str.indexOf("src=//");
  247. if (ipos>-1) {
  248. str = str.replace("src=//", "src=http://");
  249. crawlres[i] = str;
  250. }
  251. ipos = str.indexOf("src='//");
  252. if (ipos>-1) {
  253. str = str.replace("src='//", "src='http://");
  254. crawlres[i] = str;
  255. }
  256. ipos = str.indexOf("src=\"//");
  257. if (ipos>-1) {
  258. str = str.replace("src=\"//", "src=\"http://");
  259. crawlres[i] = str;
  260. }
  261. ipos = str.indexOf("src=/");
  262. if (ipos>-1) {
  263. str = str.replace("src=/", "src=http://"+targetDomain+"/");
  264. crawlres[i] = str;
  265. }
  266. ipos = str.indexOf("src='/");
  267. if (ipos>-1) {
  268. str = str.replace("src='/", "src='http://"+targetDomain+"/");
  269. crawlres[i] = str;
  270. }
  271. ipos = str.indexOf("src=\"/");
  272. if (ipos>-1) {
  273. str = str.replace("src=\"/", "src=\"http://"+targetDomain+"/");
  274. crawlres[i] = str;
  275. }
  276. ipos = str.indexOf("src=http");
  277. if (ipos>-1) {
  278. continue;
  279. }
  280. ipos = str.indexOf("src='http");
  281. if (ipos>-1) {
  282. continue;
  283. }
  284. ipos = str.indexOf("src=\"http");
  285. if (ipos>-1) {
  286. continue;
  287. }
  288. str = str.replace("src=", "src=http://"+targetDomain+"/");
  289. crawlres[i] = str;
  290. str = str.replace("src='", "src='http://"+targetDomain+"/");
  291. crawlres[i] = str;
  292. str = str.replace("src=\"", "src=\"http://"+targetDomain+"/");
  293. crawlres[i] = str;
  294. }
  295. /*
  296. * SEARCH FOR LOGO
  297. */
  298. var newcrawlres = [];
  299. var found = -1;
  300. for (i=0;i<crawlres.length;i++) {
  301. str = crawlres[i];
  302. if (str.indexOf("\"logo\"")>-1 || str.indexOf("'logo'")>-1 || str.indexOf("logo.png")>-1 || str.indexOf("logo.gif")>-1 || str.indexOf("logo.jpg")>-1 || str.indexOf("logo.jpeg")>-1) {
  303. found=i;
  304. //alert("found="+found);
  305. break;
  306. }
  307. }
  308. if (found>-1) {
  309. newcrawlres[0] = crawlres[found];
  310. //alert(newcrawlres[0]);
  311. var j=1;
  312. for (i=0;i<found;i++) {
  313. newcrawlres[j] = crawlres[i];
  314. j++;
  315. }
  316. for (i=found+1;i<crawlres.length;i++) {
  317. newcrawlres[j] = crawlres[i];
  318. j++;
  319. }
  320. crawlres = newcrawlres;
  321. logoFound = true;
  322. //alert(newcrawlres[0]);
  323. }
  324. // END SEARCH
  325. for (i=0;i<crawlres.length;i++) {
  326. document.write(crawlres[i]);
  327. if (verbose === 0) {
  328. document.write("<br>");
  329. }
  330. }
  331. } else {
  332. document.write(JSON.stringify(crawlres));
  333. }
  334. }
  335. function postCrawling() {
  336. var colImages = document.images;
  337. var j = 0;
  338. var logoHeight = "300px";
  339. for(var i = 0; i < colImages.length; i++) {
  340. var oriWidth = colImages[i].width;
  341. var oriHeight = colImages[i].height;
  342. var d = oriWidth / oriHeight;
  343. if((parseInt(colImages[i].width) < 200) || (j>4) || colImages[i].style.visibility == "hidden" || colImages[i].style.display == "none") {
  344. colImages[i].style.display = "none";
  345. colImages[i].style.visibility = "hidden";
  346. } else {
  347. colImages[i].style.width = "100%";
  348. newWidth = colImages[i].width;
  349. newHeight = parseInt(newWidth / d);
  350. colImages[i].style.maxWidth = "";
  351. colImages[i].style.float = "";
  352. colImages[i].style.height = "auto"; //newHeight + "px";
  353. colImages[i].style.position = "";
  354. colImages[i].style.top = "";
  355. colImages[i].style.left = "";
  356. colImages[i].style.margin = "";
  357. colImages[i].style.marginTop = "";
  358. colImages[i].style.marginBottom = "";
  359. colImages[i].style.marginLeft = "";
  360. colImages[i].style.marginRight = "";
  361. colImages[i].style.padding = "";
  362. colImages[i].style.paddingTop = "";
  363. colImages[i].style.paddingBottom = "";
  364. colImages[i].style.paddingLeft = "";
  365. colImages[i].style.paddingRight = "";
  366. colImages[i].style.border = "";
  367. //for transp pictures
  368. colImages[i].style.backgroundColor = "lightgray";
  369. if (i==0) {
  370. logoHeight = newHeight + "px";
  371. }
  372. j++;
  373. }
  374. }
  375. return j;
  376. }
  377. function writeFooter() {
  378. if (output == 1 && verbose == 1) {
  379. str = "<div style='text-align:center;font-size:9px'>Trademarks and brands are property of their respective owners.<br>.<br></div><div style='background-color:#03a9f5;height:105px;text-align:center;vetical-align:middle'><br><br><a href='/gr.php?to="+landingPage+"' target='_blank' style='text-decoration:none;font-weight:900;font-size:3.2vw;color:#FFFFFF;'>Go to "+landingPage+"</a></div>";
  380. document.write(str);
  381. }
  382. }
  383. window.addEventListener("load", function() {
  384. if (!document.getElementById("grausi")) {
  385. document.write("<input id='grausi' type='hidden' value=''>");
  386. }
  387. loadDoc();
  388. }, true);
  389. function checkHttpResponse() {
  390. ret = false;
  391. str = document.getElementById("grausi").value;
  392. if (str.indexOf("<img")>-1) {
  393. ret = true;
  394. }
  395. return ret;
  396. }
  397. function launchParsing() {
  398. writeHeader();
  399. if (checkHttpResponse()) {
  400. crawlImages();
  401. setTimeout(nImgProcessed=postCrawling(), 1500);
  402. if (nImgProcessed==0 && recNum<3 && verbose!=0 && output!=2) {
  403. //document.body.innerHtml = "";
  404. loadDoc();
  405. return;
  406. }
  407. } else {
  408. if (recNum<3 && verbose!=0 && output!=2) {
  409. loadDoc();
  410. }
  411. }
  412. setTimeout(writeFooter(), 1700);
  413. }