crawler.php 14 KB

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