htmlparse.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. //Wiby HTML Parser
  2. //Separates text from an HTML file
  3. //Remember to also set sql_mode = "NO_BACKSLASH_ESCAPES" in my.cnf
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #define window_len 100
  9. #define charset_len 100
  10. #define mysqlcharset_len 100
  11. #define title_len 152
  12. #define keywords_len 1024
  13. #define description_len 182
  14. #define robots_len 100
  15. #define body_len 81920
  16. #define urlList_len 102400
  17. #define strURL_len 102400
  18. FILE *bodyfile,*titlefile, *keywordsfile, *descriptionfile, *noindexfile, *nofollowfile, *charsetfile, *urlfile, *shuffledurlfile;
  19. static char filename[] = "page.out";
  20. char window[window_len],windowWithSpaces[window_len],charset[charset_len+1],mysqlcharset[mysqlcharset_len+1],title[title_len+1],keywords[keywords_len+1],description[description_len+1],robots[robots_len+1],body[body_len+1];
  21. char urlList[urlList_len+1],strURL[strURL_len+1],urlListShuffled[urlList_len+1],urlListHoldShuffled[urlList_len+1];
  22. char title_filtered[title_len+1], body_filtered[body_len+1], description_filtered[description_len+1];
  23. int titlefound=0,charsetfound=0,descriptionfound=0,keywordsfound=0,robotsfound=0,nofollow=0,noindex=0,scriptfound=0,stylefound=0,urlFound=0,urlTagFound=0,numURL=0,emptytitle=1,spaces=0,seeded=0,num_stylesheets=0,num_scripts=0,getURLs=1;
  24. long charsetsize=0,titlesize=0,keywordssize=0,descriptionsize=0,robotssize=0,bodysize=0;
  25. int matchMySQLcharset(int html_charset_length, char *html_charset, int html_match_length, char *html_lowercase_match, char *html_uppercase_match);
  26. int locateInWindow(char *window, char *birdLower, char *birdUpper, int length);
  27. int locateInURL(char *url, char *birdLower, char *birdUpper, int length, int urlSize);
  28. int canCrawl(int urlSize, char *urltocheck);
  29. void shuffleURLs(int iterations, long urlListSize);
  30. void sqlsafe();
  31. void charset2mysql();
  32. void filtervars();
  33. FILE *f;
  34. char *fileStr;
  35. char c;
  36. void htmlparse(){
  37. long urlListSize=0;
  38. numURL=0;
  39. int intag=0,incomment=0,inscript=0,instyle=0,inlink=0,putspace=0,spacecount=0;
  40. int urlSize=0,dqcount=0;
  41. titlefound=charsetfound=descriptionfound=keywordsfound=robotsfound=nofollow=noindex=scriptfound=stylefound=num_stylesheets=num_scripts=0;
  42. charsetsize=titlesize=keywordssize=descriptionsize=robotssize=bodysize=0;
  43. memset(window,'#',window_len);
  44. // window[window_len]=0;
  45. memset(windowWithSpaces,'#',window_len);
  46. // windowWithSpaces[window_len]=0;
  47. memset(charset,0,charset_len+1);
  48. memset(mysqlcharset,0,mysqlcharset_len+1);
  49. memset(title,0,title_len+1);
  50. memset(keywords,0,keywords_len+1);
  51. memset(description,0,description_len+1);
  52. memset(robots,0,robots_len+1);
  53. memset(body,0,body_len+1);
  54. memset(urlList,0,urlList_len+1);
  55. memset(strURL,0,strURL_len+1);
  56. memset(urlListShuffled,0,urlList_len+1);
  57. memset(urlListHoldShuffled,0,urlList_len+1);
  58. memset(title_filtered,0,title_len+1);
  59. memset(body_filtered,0,body_len+1);
  60. memset(description_filtered,0,description_len+1);
  61. printf("Parsing HTML... ");
  62. //open html file and load into memory
  63. f = fopen(filename, "rb");
  64. fseek(f, 0, SEEK_END);
  65. long fsize = ftell(f);
  66. fseek(f, 0, SEEK_SET); /* same as rewind(f); */
  67. fileStr = malloc(fsize + 1);
  68. if(fread(fileStr, 1, fsize, f)){};
  69. fclose(f);
  70. fileStr[fsize] = 0;
  71. //Locate the charset, title, description, keywords, robots, body
  72. //must accomodate human error in markup
  73. //must double all single quotes for mysql safety
  74. //dont allow extra whitespace, ignore cr/lf/tabs
  75. //complete it all in one pass
  76. for(int i=0;i<fsize;i++){
  77. c = fileStr[i];
  78. int skipchar = 0;
  79. if(c== 10 || c == 13 || c == 14 || c == 15 || c == 127 || c == 0 || c == 9){
  80. skipchar = 1;
  81. }
  82. //use a rolling window of 100 bytes to detect elements, ignore lf/cr/so/si/space/null/tab
  83. if(skipchar == 0 && c != 32){
  84. for(int j=0;j<window_len-1;j++){
  85. window[j] = window[j+1];
  86. }
  87. window[window_len-1] = c;
  88. }
  89. //use a rolling window of 100 bytes to detect elements, but permit space, ignore lf/cr/null/tab
  90. if(skipchar == 0){
  91. for(int j=0;j<window_len-1;j++){
  92. windowWithSpaces[j] = windowWithSpaces[j+1];
  93. }
  94. windowWithSpaces[window_len-1] = c;
  95. }
  96. //Get Title
  97. if(titlefound == 2){
  98. if(titlesize < (title_len-2) && skipchar == 0){
  99. title[titlesize]=c;
  100. titlesize++;
  101. if(c == 39){//check for single quotes and double them up for sql safety
  102. title[titlesize]=c;
  103. titlesize++;
  104. }
  105. if(c != 32 && skipchar == 0){//some titles are just a bunch of spaces or garbage, need to check for that
  106. emptytitle = 0;
  107. }
  108. }
  109. if(locateInWindow(window,"</title>","</TITLE>",8)==1){
  110. titlefound = 3;
  111. //remove </title> from end of title by inserting null at location of <
  112. titlesize -= 8;
  113. title[titlesize] = 0;
  114. //printf("\n%s",title);
  115. }
  116. }
  117. if(titlefound == 1 && c=='>')//in case of this situation: <title some_nonsense>
  118. titlefound=2;
  119. if(titlefound == 0 && locateInWindow(window,"<title","<TITLE",6)==1){
  120. titlefound = 1;
  121. }
  122. //Get Charset
  123. if(charsetfound == 1){
  124. if(c == '>' || c == '/'){
  125. charsetfound = 2;
  126. //printf("\n%s",charset);
  127. }
  128. if(charsetfound == 1 && charsetsize < charset_len && c != '"' && c != '\'' && skipchar == 0){
  129. charset[charsetsize]=c;
  130. charsetsize++;
  131. }
  132. }
  133. if(charsetfound == 0 && locateInWindow(window,"charset=","CHARSET=",8)==1){
  134. charsetfound = 1;
  135. }
  136. //Get Description
  137. if(descriptionfound == 1){
  138. if(c == '>' || c == '/'){
  139. descriptionfound = 2;
  140. //printf("\n%s",description);
  141. }
  142. if(descriptionfound == 1 && descriptionsize < (description_len-2) && c != '"' && skipchar == 0){
  143. description[descriptionsize]=c;
  144. descriptionsize++;
  145. if(c == 39){//check for single quotes and double them up for sql safety
  146. description[descriptionsize]=c;
  147. descriptionsize++;
  148. }
  149. }
  150. }
  151. if(descriptionfound == 0 && locateInWindow(window,"description\"content=","DESCRIPTION\"CONTENT=",20)==1){
  152. descriptionfound = 1;
  153. }
  154. //Get Keywords
  155. if(keywordsfound == 1){
  156. if(c == '>' || c == '/'){
  157. keywordsfound = 2;
  158. //printf("\n%s",keywords);
  159. }
  160. if(keywordsfound == 1 && keywordssize < (keywords_len-2) && c != '"' && skipchar == 0){
  161. keywords[keywordssize]=c;
  162. keywordssize++;
  163. if(c == 39){//check for single quotes and double them up for sql safety
  164. keywords[keywordssize]=c;
  165. keywordssize++;
  166. }
  167. }
  168. }
  169. if(keywordsfound == 0 && locateInWindow(window,"keywords\"content=","KEYWORDS\"CONTENT=",17)==1){
  170. keywordsfound = 1;
  171. }
  172. //Get Robots (nofollow, noindex)
  173. if(robotsfound == 1){
  174. if(c == '>' || c == '/'){
  175. robotsfound = 2;
  176. //printf("\n%s",robots);
  177. if(locateInWindow(window,"nofollow","NOFOLLOW",8)==1)
  178. nofollow=1;
  179. if(locateInWindow(window,"noindex","NOINDEX",7)==1 || locateInWindow(window,"none","NONE",4)==1)
  180. noindex=nofollow=1;
  181. }
  182. if(robotsfound == 1 && robotssize < robots_len && c != '"' && c != '\'' && skipchar == 0){
  183. robots[robotssize]=c;
  184. robotssize++;
  185. }
  186. }
  187. if(robotsfound == 0 && locateInWindow(window,"robots\"content=","ROBOTS\"CONTENT=",15)==1){
  188. robotsfound = 1;
  189. }
  190. if(titlefound != 2){
  191. //Ignore between scripts, styles, and remove all tags, repeated spaces, tabs, cr, lf, null, add a space at end of every tag
  192. if(c=='<'){
  193. intag = 1;
  194. }else if(c=='>'){
  195. intag = 0;
  196. putspace = 1;
  197. }
  198. if(locateInWindow(window,"<!--","<!--",4)==1){
  199. incomment = 1;
  200. }else if(locateInWindow(window,"-->","-->",3)==1){
  201. incomment = 0;
  202. }
  203. if(locateInWindow(window,"<script","<SCRIPT",7)==1 && c != ' ' && skipchar == 0){
  204. inscript = 1;
  205. num_scripts++;
  206. }else if(locateInWindow(window,"</script>","</SCRIPT>",9)==1){
  207. inscript = 0;
  208. }
  209. if(locateInWindow(window,"<style","<STYLE",6)==1 && c != ' ' && skipchar == 0){
  210. instyle = 1;
  211. num_stylesheets++;
  212. }else if(locateInWindow(window,"</style>","</STYLE>",8)==1){
  213. instyle = 0;
  214. }
  215. if(locateInWindow(window,"<link","<LINK",5)==1){
  216. inlink = 1;
  217. }else if(inlink==1 && locateInWindow(window,">",">",1)==1){
  218. inlink = 0;
  219. }
  220. if(inlink==1){
  221. if(locateInWindow(window,".css",".CSS",4)==1 && c != ' ' && skipchar == 0)
  222. num_stylesheets++;
  223. }
  224. //Get Body
  225. //exclude remaining tags, comments, scripts, styles, cr, lf, null, tab, add a space after a '>' but only allow one
  226. if(intag == 0 && incomment == 0 && inscript == 0 && instyle == 0 && inlink == 0 && skipchar == 0 && bodysize < (body_len-2)){
  227. if(putspace == 1){
  228. if(spacecount == 0){
  229. body[bodysize]=32;
  230. bodysize++;
  231. }
  232. spacecount++;
  233. putspace=0;
  234. }else{
  235. if(c==32)
  236. spacecount++;
  237. else spacecount = 0;
  238. if(spacecount < 2){
  239. body[bodysize]=c;
  240. bodysize++;
  241. if(c == 39){//check for single quotes and double them up for sql safety
  242. body[bodysize]=c;
  243. bodysize++;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. //Get URL's
  250. if(getURLs==1){
  251. if(urlFound == 1 && incomment==0 && instyle==0 && inscript==0 && inlink == 0){
  252. if(c=='"' || c=='\'')
  253. dqcount++;
  254. if((c == '#' && urlSize==0) || (dqcount == 2 && urlSize == 0) || (c == ' ' && urlSize == 0))
  255. urlFound=urlTagFound=dqcount=0;
  256. if((c == '>' || c == ' ') && urlFound == 1){
  257. if(canCrawl(urlSize,strURL)==0 || (urlSize+urlListSize) >= (urlList_len-1)){
  258. memset(strURL,0,strURL_len+1);
  259. }else{
  260. strcat(urlList,strURL);
  261. strcat(urlList,"\n");
  262. urlListSize+=urlSize+1;
  263. memset(strURL,0,strURL_len+1);
  264. numURL++;
  265. }
  266. urlFound = urlTagFound = urlSize = dqcount = 0;
  267. }
  268. if(urlFound == 1 && urlListSize < (urlList_len-2) && c != '"' && c != '\'' && urlSize < (strURL_len-2)){
  269. strURL[urlSize]=window[window_len-1];
  270. urlSize++;
  271. }
  272. if(urlSize==11){
  273. if(locateInWindow(window,"javascript:","JAVASCRIPT:",11)==1){
  274. urlFound=urlTagFound=urlSize=dqcount=0;
  275. memset(strURL,0,strURL_len+1);
  276. }
  277. }
  278. }
  279. if(urlFound == 0 && urlTagFound == 0 && incomment == 0 && instyle == 0 && inscript == 0 && inlink == 0 && locateInWindow(windowWithSpaces,"<a ","<A ",3)==1){//sometimes there is something between "<a" and "href"
  280. urlTagFound = 1;
  281. }
  282. if(urlFound == 0 && incomment == 0 && instyle == 0 && inscript == 0 && inlink == 0 && (locateInWindow(window,"ahref=","AHREF=",6)==1) || (urlTagFound == 1 && locateInWindow(window,"href=","HREF=",5)==1)){
  283. urlFound = 1;
  284. }
  285. }
  286. }
  287. //Convert charset to mysql equivalent
  288. charset2mysql();
  289. //Filter additional characters *if* required
  290. filtervars();
  291. //print body to file
  292. /* bodyfile = fopen("body.txt","wb");
  293. fputs(body,bodyfile);
  294. fclose(bodyfile);
  295. //print title to file
  296. titlefile = fopen("title.txt","wb");
  297. fputs(title,titlefile);
  298. fclose(titlefile);
  299. //print keywords to file
  300. keywordsfile = fopen("keywords.txt","wb");
  301. fputs(keywords,keywordsfile);
  302. fclose(keywordsfile);
  303. //print description to file
  304. descriptionfile = fopen("description.txt","wb");
  305. fputs(description,descriptionfile);
  306. fclose(descriptionfile);
  307. //print charset to file
  308. charsetfile = fopen("charset.txt","wb");
  309. fputs(mysqlcharset,charsetfile);
  310. fclose(charsetfile);
  311. //print noindex to file
  312. noindexfile = fopen("noindex.txt","wb");
  313. if(noindex==1)
  314. fputs("noindex",noindexfile);
  315. fclose(noindexfile);
  316. //print nofollow to file
  317. nofollowfile = fopen("nofollow.txt","wb");
  318. if(nofollow==1)
  319. fputs("nofollow",nofollowfile);
  320. fclose(nofollowfile);*/
  321. if(getURLs==1){
  322. //shuffle order of collected URLs list
  323. shuffleURLs(10,urlListSize);
  324. //printf("\n%s",urlList);
  325. /*//print URLs to file
  326. urlfile = fopen("url.txt","wb");
  327. fputs(urlList,urlfile);
  328. fclose(urlfile);
  329. //print shuffled URLs to file
  330. shuffledurlfile = fopen("urlshuffled.txt","wb");
  331. fputs(urlListShuffled,shuffledurlfile);
  332. fclose(shuffledurlfile); */
  333. }
  334. free(fileStr);
  335. printf("\nbody: %ld, title: %ld, charset: %ld, description: %ld, keywords: %ld, noindex: %d, nofollow: %d",bodysize,titlesize,charsetsize,descriptionsize,keywordssize,noindex,nofollow);
  336. }
  337. void shuffleURLs(int iterations, long urlListSize)
  338. {
  339. if(seeded==0){
  340. srand(time(NULL));
  341. seeded=1;
  342. }
  343. int r1,r2,r1to2;
  344. int urlCount,i,j,k,l;
  345. if(numURL>2){
  346. strcpy(urlListHoldShuffled,urlList);
  347. for(int loops=0;loops<iterations;loops++){
  348. r1 = r1to2 = (rand() % numURL) + 1;
  349. r2 = (rand() % numURL) + 1;
  350. if(r1>r2){
  351. r1=r2;
  352. r2=r1to2;
  353. }
  354. if(r1==r2){
  355. continue;
  356. }
  357. urlCount=i=j=k=l=0;
  358. //skip to url number r1
  359. while(urlCount < r1 /*&& i<urlList_len*/){
  360. if(urlListHoldShuffled[i]=='\n')
  361. urlCount++;
  362. i++;
  363. }
  364. j=i;
  365. //copy to urlListShuffled starting at j until reaching r2 location
  366. while(urlCount<r2 /*&& j<urlList_len*/){
  367. urlListShuffled[k]=urlListHoldShuffled[j];
  368. if(urlListHoldShuffled[j]=='\n')
  369. urlCount++;
  370. j++;
  371. k++;
  372. }
  373. //concat url's before i
  374. while(l<i /*&& k<urlList_len*/){
  375. urlListShuffled[k]=urlListHoldShuffled[l];
  376. l++;
  377. k++;
  378. }
  379. //concat url's after k
  380. while(k<urlListSize /*&& k<urlList_len*/){
  381. urlListShuffled[k]=urlListHoldShuffled[k];
  382. k++;
  383. }
  384. strcpy(urlListHoldShuffled,urlListShuffled);
  385. }
  386. }else{
  387. strcpy(urlListShuffled,urlList);
  388. }
  389. }
  390. void charset2mysql()
  391. {
  392. //if no charset specified, use utf8
  393. if(charsetsize == 0){
  394. strcpy(mysqlcharset,"SET CHARSET utf8;");
  395. printf("No Charset found. %s",mysqlcharset);
  396. }
  397. else{ //else, match charset with a proper mysql charset
  398. if(matchMySQLcharset(charsetsize,charset,5,"utf-8","UTF-8")==1){
  399. strcpy(mysqlcharset,"SET CHARSET utf8mb4;");
  400. printf("%s",mysqlcharset);
  401. }
  402. else if(matchMySQLcharset(charsetsize,charset,6,"latin1","LATIN1")==1){
  403. strcpy(mysqlcharset,"SET CHARSET latin1;");
  404. printf("%s",mysqlcharset);
  405. }
  406. else if(matchMySQLcharset(charsetsize,charset,9,"shift-jis","SHIFT-JIS")==1){
  407. strcpy(mysqlcharset,"SET CHARSET cp932;");
  408. printf("%s",mysqlcharset);
  409. }
  410. else if(matchMySQLcharset(charsetsize,charset,6,"x-sjis","X-SJIS")==1){
  411. strcpy(mysqlcharset,"SET CHARSET cp932;");
  412. printf("%s",mysqlcharset);
  413. }
  414. else if(matchMySQLcharset(charsetsize,charset,10,"iso-8859-1","ISO-8859-1")==1){
  415. strcpy(mysqlcharset,"SET CHARSET latin1;");
  416. printf("%s",mysqlcharset);
  417. }
  418. else if(matchMySQLcharset(charsetsize,charset,12,"windows-1252","WINDOWS-1252")==1){
  419. strcpy(mysqlcharset,"SET CHARSET latin1;");
  420. printf("%s",mysqlcharset);
  421. }
  422. else if(matchMySQLcharset(charsetsize,charset,12,"windows-1251","WINDOWS-1251")==1){
  423. strcpy(mysqlcharset,"SET CHARSET cp1251;");
  424. printf("%s",mysqlcharset);
  425. }
  426. else if(matchMySQLcharset(charsetsize,charset,6,"koi8-r","KOI8-R")==1){
  427. strcpy(mysqlcharset,"SET CHARSET cp1251;");
  428. printf("%s",mysqlcharset);
  429. }
  430. else if(matchMySQLcharset(charsetsize,charset,6,"euc-kr","EUC-KR")==1){
  431. strcpy(mysqlcharset,"SET CHARSET euckr;");
  432. printf("%s",mysqlcharset);
  433. }
  434. else if(matchMySQLcharset(charsetsize,charset,4,"big5","BIG5")==1){
  435. strcpy(mysqlcharset,"SET CHARSET big5;");
  436. printf("%s",mysqlcharset);
  437. }
  438. else{
  439. strcpy(mysqlcharset,"SET CHARSET utf8;");
  440. printf("Charset mismatch. %s",mysqlcharset);
  441. }
  442. }
  443. }
  444. int matchMySQLcharset(int html_charset_length, char *html_charset, int html_match_length, char *html_lowercase_match, char *html_uppercase_match)
  445. {
  446. int match = 0;
  447. int i=0;
  448. for(;i<html_match_length;i++){
  449. if(i > html_charset_length){
  450. return 0;
  451. }
  452. if(html_charset[i] != 95 && html_charset[i] != 45 && html_lowercase_match[i] != 95 && html_lowercase_match[i] != 45){ // _ or -
  453. if(html_lowercase_match[i] != html_charset[i] && html_uppercase_match[i] != html_charset[i]){
  454. return 0;
  455. }
  456. }
  457. match = 1;
  458. }
  459. return match;
  460. }
  461. int locateInWindow(char *window, char *birdLower, char *birdUpper, int length)
  462. {
  463. int start = window_len-length;
  464. for(int i=0;i<length;i++){
  465. if(window[start] != birdLower[i] && window[start] != birdUpper[i]){
  466. return 0;
  467. }
  468. start++;
  469. }
  470. return 1;
  471. }
  472. int locateInURL(char *url, char *birdLower, char *birdUpper, int length, int urlSize)
  473. {
  474. long start = urlSize-length;
  475. if(urlSize >= length){
  476. for(int i=0;i<length;i++){
  477. if(url[start] != birdLower[i] && window[start] != birdUpper[i]){
  478. return 0;
  479. }
  480. start++;
  481. }
  482. return 1;
  483. }else{
  484. return 0;
  485. }
  486. }
  487. //Check if url can be indexed (allow relative links for html and txt files. Removing this check will add to the queue everything listed including external links.
  488. int canCrawl(int urlSize, char *urltocheck){
  489. int numDots=0,numSlash=0;
  490. int slashpos=0,dotspos=0;
  491. int extfound=0,extlocation=0,prefixfound=0;
  492. for(int i=0;i<urlSize;i++){
  493. if(urlSize>5 && urltocheck[i]==':' && i>3){
  494. if((urltocheck[0]!='h' && urltocheck[0]!='H') || (urltocheck[1]!='t' && urltocheck[1]!='T') || (urltocheck[2]!='t' && urltocheck[2]!='T') || (urltocheck[3]!='p' && urltocheck[3]!='P') || (urltocheck[4]!='s' && urltocheck[4]!='S' && urltocheck[4]!=':') || (urltocheck[5]!=':' && urltocheck[5]!='/'))
  495. return 0;
  496. prefixfound=1;
  497. }
  498. if(urltocheck[i]=='?' || urltocheck[i]=='\\' || urltocheck[i] == '"' || urltocheck[i] == '\'' || urltocheck[i] == ' '){
  499. return 0;
  500. }
  501. if(urltocheck[i]=='.'){
  502. numDots++;
  503. }
  504. if(urltocheck[i]=='/'){
  505. numSlash++;
  506. }
  507. if(urltocheck[i]=='.' ){
  508. extfound=1;
  509. extlocation=i;
  510. }
  511. if(urltocheck[i]=='/' && extfound==1 && i>extlocation){
  512. extfound=0;
  513. }
  514. if(prefixfound==1 && numSlash-2<=0){
  515. extfound=0;
  516. }
  517. }
  518. if(numDots == 0){
  519. return 1;
  520. }
  521. //restrict file extensions to these
  522. if(extfound==1 && (locateInURL(urltocheck,".html",".HTML",5,urlSize)==1 || locateInURL(urltocheck,".htm",".HTM",4,urlSize)==1 || locateInURL(urltocheck,".txt",".TXT",4,urlSize)==1 || locateInURL(urltocheck,".php",".PHP",4,urlSize)==1 || locateInURL(urltocheck,".asp",".ASP",4,urlSize)==1 || locateInURL(urltocheck,".xhtml",".XHTML",6,urlSize)==1 || locateInURL(urltocheck,".shtml",".SHTML",6,urlSize)==1)){
  523. return 1;
  524. }
  525. if(extfound==0 )
  526. return 1;
  527. return 0;
  528. }
  529. void filtervars(){
  530. //Creates a copy of title, description, body variables with single-qutoes filtered out
  531. //will be used for the shard tables, but not on the primary 'windex' table
  532. //allows a more restrictive query to be used. Is agnostic to searches containing single-quotes as a compromise
  533. //filter title
  534. int j=0;
  535. for(int i=0;i<titlesize;i++){
  536. if(title[i]!=39){
  537. title_filtered[j]=title[i];
  538. j++;
  539. }
  540. }
  541. //filter description
  542. j=0;
  543. for(int i=0;i<descriptionsize;i++){
  544. if(description[i]!=39){
  545. description_filtered[j]=description[i];
  546. j++;
  547. }
  548. }
  549. //filter body
  550. j=0;
  551. for(int i=0;i<bodysize;i++){
  552. if(body[i]!=39){
  553. body_filtered[j]=body[i];
  554. j++;
  555. }
  556. }
  557. }