search_function.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <?php
  2. /*
  3. ----------------------------------------------------------------------------------
  4. PhpDig Version 1.8.x - See the config file for the full version number.
  5. This program is provided WITHOUT warranty under the GNU/GPL license.
  6. See the LICENSE file for more information about the GNU/GPL license.
  7. Contributors are listed in the CREDITS and CHANGELOG files in this package.
  8. Developer from inception to and including PhpDig v.1.6.2: Antoine Bajolet
  9. Developer from PhpDig v.1.6.3 to and including current version: Charter
  10. Copyright (C) 2001 - 2003, Antoine Bajolet, http://www.toiletoine.net/
  11. Copyright (C) 2003 - current, Charter, http://www.phpdig.net/
  12. Contributors hold Copyright (C) to their code submissions.
  13. Do NOT edit or remove this copyright or licence information upon redistribution.
  14. If you modify code and redistribute, you may ADD your copyright to this notice.
  15. ----------------------------------------------------------------------------------
  16. */
  17. //===============================================
  18. // do the search and display the results
  19. // can be called in any page
  20. function phpdigSearch($id_connect, $query_string, $option='start', $refine=0,
  21. $refine_url='', $lim_start=0, $limite=10, $browse=0,
  22. $site=0, $path='', $relative_script_path = '.', $template='', $adlog_flag=0, $rssdf='', $template_demo='')
  23. {
  24. // check input
  25. // $id_connect set in connect.php file
  26. // $query_string cleaned in $query_to_parse in search_function.php file
  27. if (($option != "start") && ($option != "any") && ($option != "exact")) { $option = SEARCH_DEFAULT_MODE; }
  28. if (($refine != 0) && ($refine != 1)) { $refine = 0; }
  29. // $refine_url set in search_function.php file
  30. // $lim_start set in search_function.php file
  31. settype($limite,'integer');
  32. if (($limite != 10) && ($limite != 30) && ($limite != 100)) { $limite = SEARCH_DEFAULT_LIMIT; }
  33. settype($limit_start,'integer');
  34. if (isset($limit_start)) { $limit_start = $limite * floor($limit_start / $limite); }
  35. if (($browse != 0) && ($browse != 1)) { $browse = 0; }
  36. if (eregi("^[0-9]+[,]",$site)) { $tempbust = explode(",",$site); $site = $tempbust[0]; $path = $tempbust[1]; }
  37. settype($site,'integer'); // now set to integer
  38. settype($path,'string'); // make sure set to string
  39. if (!get_magic_quotes_gpc()) { $my_path = addslashes($path); }
  40. else { $my_path = addslashes(stripslashes($path)); $path = stripslashes($path); }
  41. if (empty($site) && empty($path)) { $refine = 0; } else { $refine = 1; }
  42. if ($path == "-###-") { $site = 0; $path = ""; $refine = 0; }
  43. // $relative_script_path set in search.php file
  44. // $template set in config.php file
  45. // $adlog_flag set in search.php file
  46. // $rssdf set in search.php file
  47. // $template_demo set in config.php
  48. $timer = new phpdigTimer('html');
  49. $timer->start('All');
  50. // init variables
  51. global $phpdig_words_chars;
  52. settype($maxweight,'integer');
  53. $ignore = '';
  54. $ignore_common = '';
  55. $ignore_message = '';
  56. $ignore_commess = '';
  57. $wheresite = '';
  58. $wherepath = '';
  59. $table_results = '';
  60. $final_result = '';
  61. $search_time = 0;
  62. $strings = '';
  63. $num_tot = 0;
  64. $leven_final = "";
  65. $exclude = array();
  66. $nav_bar = '';
  67. $pages_bar = '';
  68. $mtime = explode(' ',microtime());
  69. $start_time = $mtime[0]+$mtime[1];
  70. $timer->start('All backend');
  71. $timer->start('parsing strings');
  72. if (!$option) {
  73. $option = SEARCH_DEFAULT_MODE;
  74. }
  75. if (!in_array($option,array('start','any','exact'))) {
  76. return 0;
  77. }
  78. // the query was filled
  79. if ($query_string) {
  80. $common_words = phpdigComWords("$relative_script_path/includes/common_words.txt");
  81. $like_start = array( "start" => "", // is empty
  82. "any" => "", // was a percent
  83. "exact" => "" // is empty
  84. );
  85. $like_end = array( "start" => "%", // is a percent
  86. "any" => "%", // is a percent
  87. "exact" => "%" // was empty
  88. );
  89. $like_operator = array( "start" => "like", // is a like
  90. "any" => "like", // is a like
  91. "exact" => "like" // was an =
  92. );
  93. if ($refine) {
  94. $wheresite = "AND spider.site_id = $site ";
  95. if (($path) && (strlen($path) > 0)) {
  96. $wherepath = "AND spider.path like '$my_path' ";
  97. }
  98. $refine_url = "&amp;refine=1&amp;site=$site&amp;path=".urlencode($path);
  99. }
  100. else {
  101. $refine_url = "";
  102. }
  103. settype ($lim_start,"integer");
  104. if ($lim_start < 0) {
  105. $lim_start = 0;
  106. }
  107. $n_words = count(explode(" ",$query_string));
  108. $ncrit = 0;
  109. $tin = "0";
  110. if (!get_magic_quotes_gpc()) {
  111. $query_to_parse = addslashes($query_string);
  112. }
  113. else {
  114. $query_to_parse = $query_string;
  115. }
  116. $my_query_string_link = stripslashes($query_to_parse);
  117. $query_to_parse = str_replace('_','\_',$query_to_parse); // avoid '_' in the query
  118. $query_to_parse = str_replace('%','\%',$query_to_parse); // avoid '%' in the query
  119. $query_to_parse = str_replace('\"',' ',$query_to_parse); // avoid '"' in the query
  120. $query_to_parse = phpdigStripAccents(strtolower($query_to_parse)); //made all lowercase
  121. // RH query_chars = "[^".$phpdig_words_chars[PHPDIG_ENCODING]." \'.\_~@#$:&\%/;,=-]+"; // epure chars \'._~@#$:&%/;,=-
  122. $what_query_chars = "[^".$phpdig_words_chars[PHPDIG_ENCODING]." \'._~@#$&%/=-]+"; // epure chars \'._~@#$&%/=-
  123. if (eregi($what_query_chars,$query_to_parse)) {
  124. $query_to_parse = eregi_replace($what_query_chars," ",$query_to_parse);
  125. }
  126. $query_to_parse = ereg_replace('(['.$phpdig_words_chars[PHPDIG_ENCODING].'])[\'.\_~@#$:&\%/;,=-]+($|[[:space:]]$|[[:space:]]['.$phpdig_words_chars[PHPDIG_ENCODING].'])','\1 \2',$query_to_parse);
  127. $query_to_parse = trim(ereg_replace(" +"," ",$query_to_parse)); // no more than 1 blank
  128. $query_for_strings = $query_to_parse;
  129. $query_for_phrase = $query_to_parse;
  130. $test_short = $query_to_parse;
  131. $query_to_parse2 = explode(" ",$query_to_parse);
  132. usort($query_to_parse2, "phpdigByLength");
  133. $query_to_parse = implode(" ",$query_to_parse2);
  134. if (isset($query_to_parse2)) { unset($query_to_parse2); }
  135. if (SMALL_WORDS_SIZE >= 1) {
  136. $ignore_short_flag = 0;
  137. $test_short_counter = 0;
  138. $test_short2 = explode(" ",$test_short);
  139. for ($i=0; $i<count($test_short2); $i++) {
  140. $test_short2[$i] = trim($test_short2[$i]);
  141. }
  142. $test_short2 = array_unique($test_short2);
  143. sort($test_short2);
  144. $test_short3 = array();
  145. for ($i=0; $i<count($test_short2); $i++) {
  146. if ((strlen($test_short2[$i]) <= SMALL_WORDS_SIZE) && (strlen($test_short2[$i]) > 0)) {
  147. $test_short2[$i].=" ";
  148. $test_short_counter++;
  149. $test_short3[] = $test_short2[$i];
  150. }
  151. }
  152. $test_short = implode(" ",$test_short3);
  153. if (isset($test_short2)) { unset($test_short2); }
  154. if (isset($test_short3)) { unset($test_short3); }
  155. $regs = array(); // for use with ereg()
  156. while (ereg('( [^ ]{1,'.SMALL_WORDS_SIZE.'} )|( [^ ]{1,'.SMALL_WORDS_SIZE.'})$|^([^ ]{1,'.SMALL_WORDS_SIZE.'} )',$test_short,$regs)) {
  157. for ($n=1; $n<=3; $n++) {
  158. if (($regs[$n]) || ($regs[$n] == 0)) {
  159. $ignore_short_flag++;
  160. if (!eregi("\"".trim(stripslashes($regs[$n]))."\", ",$ignore)) {
  161. $ignore .= "\"".trim(stripslashes($regs[$n]))."\", ";
  162. }
  163. $test_short = trim(str_replace($regs[$n],"",$test_short));
  164. }
  165. }
  166. }
  167. if (strlen($test_short) <= SMALL_WORDS_SIZE) {
  168. if (!eregi("\"".$test_short."\", ",$ignore)) {
  169. $ignore_short_flag++;
  170. $ignore .= "\"".stripslashes($test_short)."\", ";
  171. }
  172. $test_short = trim(str_replace($test_short,"",$test_short));
  173. }
  174. }
  175. $ignore = str_replace("\"\", ","",$ignore);
  176. if ($option != "exact") {
  177. if (($ignore) && ($ignore_short_flag > 1) && ($test_short_counter > 1)) {
  178. $ignore_message = $ignore.' '.phpdigMsg('w_short_plur');
  179. }
  180. elseif ($ignore) {
  181. $ignore_message = $ignore.' '.phpdigMsg('w_short_sing');
  182. }
  183. }
  184. $ignore_common_flag = 0;
  185. while (ereg("(-)?([^ ]{".(SMALL_WORDS_SIZE+1).",}).*",$query_for_strings,$regs)) {
  186. $query_for_strings = trim(str_replace($regs[2],"",$query_for_strings));
  187. if (!isset($common_words[stripslashes($regs[2])])) {
  188. if ($regs[1] == '-') {
  189. $exclude[$ncrit] = $regs[2];
  190. $query_for_phrase = trim(str_replace("-".$regs[2],"",$query_for_phrase));
  191. }
  192. else {
  193. $strings[$ncrit] = $regs[2];
  194. }
  195. $kconds[$ncrit] = '';
  196. if ($option != 'any') {
  197. $kconds[$ncrit] .= " AND k.twoletters = '".addslashes(substr(str_replace('\\','',$regs[2]),0,2))."' ";
  198. }
  199. $kconds[$ncrit] .= " AND k.keyword ".$like_operator[$option]." '".$like_start[$option].$regs[2].$like_end[$option]."' ";
  200. $ncrit++;
  201. }
  202. else {
  203. $ignore_common_flag++;
  204. $ignore_common .= "\"".stripslashes($regs[2])."\", ";
  205. }
  206. }
  207. if ($option != "exact") {
  208. if (($ignore_common) && ($ignore_common_flag > 1)) {
  209. $ignore_commess = $ignore_common.' '.phpdigMsg('w_common_plur');
  210. }
  211. elseif ($ignore_common) {
  212. $ignore_commess = $ignore_common.' '.phpdigMsg('w_common_sing');
  213. }
  214. }
  215. $timer->stop('parsing strings');
  216. if ($ncrit && is_array($strings)) {
  217. $query = "SET OPTION SQL_BIG_SELECTS = 1";
  218. mysql_query($query,$id_connect);
  219. $my_spider2site_array = array();
  220. $my_sitecount_array = array();
  221. for ($n = 0; $n < $ncrit; $n++) {
  222. $timer->start('spider queries');
  223. $query = "SELECT spider.spider_id,sum(weight) as weight, spider.site_id
  224. FROM ".PHPDIG_DB_PREFIX."keywords as k,".PHPDIG_DB_PREFIX."engine as engine, ".PHPDIG_DB_PREFIX."spider as spider
  225. WHERE engine.key_id = k.key_id
  226. ".$kconds[$n]."
  227. AND engine.spider_id = spider.spider_id $wheresite $wherepath
  228. GROUP BY spider.spider_id,spider.site_id ";
  229. $result = mysql_query($query,$id_connect);
  230. $num_res_temp = mysql_num_rows($result);
  231. $timer->stop('spider queries');
  232. $timer->start('spider fills');
  233. if ($num_res_temp > 0) {
  234. if (!isset($exclude[$n])) {
  235. $num_res[$n] = $num_res_temp;
  236. while (list($spider_id,$weight,$site_id) = mysql_fetch_array($result)) {
  237. $s_weight[$n][$spider_id] = $weight;
  238. $my_spider2site_array[$spider_id] = $site_id;
  239. $my_sitecount_array[$site_id] = 0;
  240. }
  241. }
  242. else {
  243. $num_exclude[$n] = $num_res_temp;
  244. while (list($spider_id,$weight) = mysql_fetch_array($result)) {
  245. $s_exclude[$n][$spider_id] = 1;
  246. }
  247. mysql_free_result($result);
  248. }
  249. }
  250. elseif (!isset($exclude[$n])) {
  251. $num_res[$n] = 0;
  252. $s_weight[$n][0] = 0;
  253. }
  254. $timer->stop('spider fills');
  255. }
  256. $timer->start('reorder results');
  257. if ($option != "any") {
  258. if (is_array($num_res)) {
  259. asort ($num_res);
  260. list($id_most) = each($num_res);
  261. reset ($s_weight[$id_most]);
  262. while (list($spider_id,$weight) = each($s_weight[$id_most])) {
  263. $weight_tot = 1;
  264. reset ($num_res);
  265. while(list($n) = each($num_res)) {
  266. settype($s_weight[$n][$spider_id],'integer');
  267. $weight_tot *= sqrt($s_weight[$n][$spider_id]);
  268. }
  269. if ($weight_tot > 0) {
  270. $final_result[$spider_id]=$weight_tot;
  271. }
  272. }
  273. }
  274. }
  275. else {
  276. if (is_array($num_res)) {
  277. asort ($num_res);
  278. while (list($spider_id,$site_id) = each($my_spider2site_array)) {
  279. $weight_tot = 0;
  280. reset ($num_res);
  281. while(list($n) = each($num_res)) {
  282. settype($s_weight[$n][$spider_id],'integer');
  283. $weight_tot += sqrt($s_weight[$n][$spider_id]);
  284. }
  285. if ($weight_tot > 0) {
  286. $final_result[$spider_id]=$weight_tot;
  287. }
  288. }
  289. }
  290. }
  291. if (isset($num_exclude) && is_array($num_exclude)) {
  292. while (list($id) = each($num_exclude)) {
  293. while(list($spider_id) = each($s_exclude[$id])) {
  294. if (isset($final_result[$spider_id])) { unset($final_result[$spider_id]); }
  295. }
  296. }
  297. }
  298. if ($option == "exact") {
  299. if ((is_array($final_result)) && (count($final_result) > 0)) {
  300. $exact_phrase_flag = 0;
  301. arsort($final_result);
  302. reset($final_result);
  303. $query_for_phrase_array = explode(" ",$query_for_phrase);
  304. $reg_strings = str_replace('@#@',' ',phpdigPregQuotes(str_replace('\\','',implode('@#@',$query_for_phrase_array))));
  305. $stop_regs = "[][(){}[:blank:]=&?!&#%\$£*@+%:;,/\.'\"]";
  306. $reg_strings = "($stop_regs{1}|^)($reg_strings)($stop_regs{1}|\$)";
  307. while (list($spider_id,$weight) = each($final_result)) {
  308. $content_file = $relative_script_path.'/'.TEXT_CONTENT_PATH.$spider_id.'.txt';
  309. if (is_file($content_file)) {
  310. $f_handler = fopen($content_file,'r');
  311. $extract_content = preg_replace("/([ ]{2,}|\n|\r|\r\n)/"," ",fread($f_handler,filesize($content_file)));
  312. if(!eregi($reg_strings,$extract_content)) {
  313. $exact_phrase_flag = 1;
  314. }
  315. fclose($f_handler);
  316. }
  317. if ($exact_phrase_flag == 1) {
  318. if (isset($final_result[$spider_id])) { unset($final_result[$spider_id]); }
  319. $exact_phrase_flag = 0;
  320. }
  321. }
  322. }
  323. }
  324. if((!$refine) && (NUMBER_OF_RESULTS_PER_SITE != -1)) {
  325. if ((is_array($final_result)) && (count($final_result) > 0)) {
  326. arsort($final_result);
  327. reset($final_result);
  328. while (list($spider_id,$weight) = each($final_result)) {
  329. $site_id = $my_spider2site_array[$spider_id];
  330. $current_site_counter = $my_sitecount_array[$site_id];
  331. if ($current_site_counter < NUMBER_OF_RESULTS_PER_SITE) {
  332. $my_sitecount_array[$site_id]++;
  333. }
  334. else {
  335. if (isset($final_result[$spider_id])) { unset($final_result[$spider_id]); }
  336. }
  337. }
  338. }
  339. }
  340. $timer->stop('reorder results');
  341. }
  342. $timer->stop('All backend');
  343. $timer->start('All display');
  344. if ((is_array($final_result)) && (count($final_result) > 0)) {
  345. arsort($final_result);
  346. $lim_start = max(0, $lim_start-($lim_start % $limite));
  347. $n_start = $lim_start+1;
  348. $num_tot = count($final_result);
  349. if ($n_start+$limite-1 < $num_tot) {
  350. $n_end = ($lim_start+$limite);
  351. $more_results = 1;
  352. }
  353. else {
  354. $n_end = $num_tot;
  355. $more_results = 0;
  356. }
  357. if ($n_start > $n_end) {
  358. $n_start = 1;
  359. $n_end = min($num_tot,$limite);
  360. $lim_start = 0;
  361. if ($n_end < $num_tot) {
  362. $more_results = 1;
  363. }
  364. }
  365. // ereg for text snippets and highlighting
  366. if ($option == "exact") {
  367. $reg_strings = str_replace('@#@',' ',phpdigPregQuotes(str_replace('\\','',implode('@#@',$query_for_phrase_array))));
  368. }
  369. else {
  370. $reg_strings = str_replace('@#@','|',phpdigPregQuotes(str_replace('\\','',implode('@#@',$strings))));
  371. }
  372. $stop_regs = "[][(){}[:blank:]=&?!&#%\$£*@+%:;,/\.'\"]";
  373. switch($option) {
  374. case 'any':
  375. $reg_strings = "($stop_regs{1}|^)($reg_strings)()";
  376. break;
  377. case 'exact':
  378. $reg_strings = "($stop_regs{1}|^)($reg_strings)($stop_regs{1}|\$)";
  379. break;
  380. default:
  381. $reg_strings = "($stop_regs{1}|^)($reg_strings)()";
  382. }
  383. $timer->start('Result table');
  384. //fill the results table
  385. reset($final_result);
  386. for ($n = 1; $n <= $n_end; $n++) {
  387. list($spider_id,$s_weight) = each($final_result);
  388. if (!$maxweight) {
  389. $maxweight = $s_weight;
  390. }
  391. if ($n >= $n_start) {
  392. $timer->start('Display queries');
  393. $query = "SELECT sites.site_url, sites.port, spider.path,spider.file,spider.first_words,sites.site_id,spider.spider_id,spider.last_modified,spider.md5 "
  394. ."FROM ".PHPDIG_DB_PREFIX."spider AS spider, ".PHPDIG_DB_PREFIX."sites AS sites "
  395. ."WHERE spider.spider_id=$spider_id AND sites.site_id = spider.site_id";
  396. $result = mysql_query($query,$id_connect);
  397. $content = mysql_fetch_array($result,MYSQL_ASSOC);
  398. mysql_free_result($result);
  399. if ($content['port']) {
  400. $content['site_url'] = ereg_replace('/$',':'.$content['port'].'/',$content['site_url']);
  401. }
  402. $weight = sprintf ("%01.2f", (100*$s_weight)/$maxweight);
  403. $url = eregi_replace("([".$phpdig_words_chars[PHPDIG_ENCODING]."])[/]{2,}","\\1/",urldecode($content['site_url'].$content['path'].$content['file']));
  404. $js_url = urlencode(eregi_replace("^[a-z]{3,5}://","",$url));
  405. $url = str_replace("\"","%22",str_replace("'","%27",str_replace(" ","%20",trim($url))));
  406. $l_site = "<a class='phpdig' href='".SEARCH_PAGE."?refine=1&amp;query_string=".urlencode($my_query_string_link)."&amp;site=".$content['site_id']."&amp;limite=$limite&amp;option=$option'>".htmlspecialchars(urldecode($content['site_url']),ENT_QUOTES)."</a>";
  407. if ($content['path']) {
  408. $content['path'] = urlencode(urldecode($content['path']));
  409. $content2['path'] = htmlspecialchars(urldecode($content['path']),ENT_QUOTES);
  410. $l_path = ", ".phpdigMsg('this_path')." : <a class='phpdig' href='".SEARCH_PAGE."?refine=1&amp;query_string=".urlencode($my_query_string_link)."&amp;site=".$content['site_id']."&amp;path=".$content['path']."&amp;limite=$limite&amp;option=$option' >".$content2['path']."</a>";
  411. }
  412. else {
  413. $content2['path'] = "";
  414. $l_path="";
  415. }
  416. $first_words = ereg_replace('txt-sep +txt-end', 'txt-end', ereg_replace('txt-sep +txt-sep', 'txt-sep', $content['first_words'])); // RH was just: $content['first_words'];
  417. $first_words = str_replace('-kw ', ' ', str_replace('txt-end', "\n", str_replace('txt-sep', '<br>', $first_words))); // RH this line added
  418. $timer->stop('Display queries');
  419. $timer->start('Extracts');
  420. $extract = "";
  421. //Try to retrieve matching lines if the content-text is set to 1
  422. if (CONTENT_TEXT == 1 && DISPLAY_SNIPPETS) {
  423. $content_file = $relative_script_path.'/'.TEXT_CONTENT_PATH.$content['spider_id'].'.txt';
  424. if (is_file($content_file)) {
  425. $num_extracts = 0;
  426. $my_extract_size = 200;
  427. $my_filesize_for_while = filesize($content_file);
  428. while (($num_extracts == 0) && ($my_extract_size <= $my_filesize_for_while)) { // ***
  429. $f_handler = fopen($content_file,'r');
  430. while($num_extracts < DISPLAY_SNIPPETS_NUM && $extract_content = preg_replace("/([ ]{2,}|\n|\r|\r\n)/"," ",fread($f_handler,$my_extract_size))) {
  431. if(eregi($reg_strings,$extract_content)) {
  432. $match_this_spot = eregi_replace($reg_strings,"\\1<\\2>\\3",$extract_content);
  433. $first_bold_spot = strpos($match_this_spot,"<");
  434. $first_bold_spot = max($first_bold_spot - round((SNIPPET_DISPLAY_LENGTH / 2),0), 0);
  435. $extract_content = substr($extract_content,$first_bold_spot,max(SNIPPET_DISPLAY_LENGTH, 2 * strlen($query_string)));
  436. $extract .= ' ...'.phpdigHighlight($reg_strings,$extract_content).'... ';
  437. $num_extracts++;
  438. }
  439. }
  440. fclose($f_handler);
  441. if ($my_extract_size < $my_filesize_for_while) {
  442. $my_extract_size *= 100;
  443. if ($my_extract_size > $my_filesize_for_while) {
  444. $my_extract_size = $my_filesize_for_while;
  445. }
  446. }
  447. else {
  448. $my_extract_size++;
  449. }
  450. } // ends ***
  451. }
  452. }
  453. list($title,$text) = explode("\n",$first_words);
  454. $title = htmlspecialchars(phpdigHighlight($reg_strings,urldecode($title)),ENT_QUOTES);
  455. $title = phpdigSpanReplace($title);
  456. $timer->stop('Extracts');
  457. $table_results[$n] = array (
  458. 'weight' => $weight,
  459. 'img_tag' => '<img border="0" src="'.WEIGHT_IMGSRC.'" width="'.ceil(WEIGHT_WIDTH*$weight/100).'" height="'.WEIGHT_HEIGHT.'" alt="" />',
  460. 'page_link' => "<a class=\"phpdig\" href=\"".$url."\" onmousedown=\"return clickit(".$n.",'".$js_url."')\" target=\"".LINK_TARGET."\" >".$title."</a>",
  461. 'limit_links' => phpdigMsg('limit_to')." ".$l_site.$l_path,
  462. 'filesize' => sprintf('%.1f',(ereg_replace('.*_([0-9]+)$','\1',$content['md5']))/1024),
  463. 'update_date' => ereg_replace('^([0-9]{4})([0-9]{2})([0-9]{2}).*',PHPDIG_DATE_FORMAT,$content['last_modified']),
  464. 'complete_path' => $url,
  465. 'link_title' => $title
  466. );
  467. $table_results[$n]['text'] = '';
  468. if (DISPLAY_SUMMARY) {
  469. $table_results[$n]['text'] = htmlspecialchars(phpdigHighlight($reg_strings,preg_replace("/([ ]{2,}|\n|\r|\r\n)/"," ",ereg_replace('(@@@.*)','',wordwrap($text, SUMMARY_DISPLAY_LENGTH, '@@@')))),ENT_QUOTES);
  470. $table_results[$n]['text'] = phpdigSpanReplace($table_results[$n]['text']);
  471. }
  472. if (DISPLAY_SUMMARY && DISPLAY_SNIPPETS) {
  473. $table_results[$n]['text'] .= "\n<br/><br/>\n";
  474. }
  475. if (DISPLAY_SNIPPETS) {
  476. if ($extract) {
  477. $extract = htmlspecialchars($extract,ENT_QUOTES);
  478. $extract = phpdigSpanReplace($extract);
  479. $table_results[$n]['text'] .= $extract;
  480. }
  481. else if (!$table_results[$n]['text']){
  482. $table_results[$n]['text'] = htmlspecialchars(phpdigHighlight($reg_strings,preg_replace("/([ ]{2,}|\n|\r|\r\n)/"," ",ereg_replace('(@@@.*)','',wordwrap($text, SUMMARY_DISPLAY_LENGTH, '@@@')))),ENT_QUOTES);
  483. $table_results[$n]['text'] = phpdigSpanReplace($table_results[$n]['text']);
  484. }
  485. }
  486. }
  487. }
  488. $timer->stop('Result table');
  489. $timer->start('Final strings');
  490. $url_bar = SEARCH_PAGE."?template_demo=$template_demo&amp;browse=1&amp;query_string=".urlencode($my_query_string_link)."$refine_url&amp;limite=$limite&amp;option=$option&amp;lim_start=";
  491. if ($lim_start > 0) {
  492. $previous_link = $url_bar.($lim_start-$limite);
  493. $nav_bar .= "<a class=\"phpdig\" href=\"$previous_link\" >&lt;&lt;".phpdigMsg('previous')."</a>&nbsp;&nbsp;&nbsp; \n";
  494. }
  495. $tot_pages = ceil($num_tot/$limite);
  496. $actual_page = $lim_start/$limite + 1;
  497. $page_inf = max(1,$actual_page - 5);
  498. $page_sup = min($tot_pages,max($actual_page+5,10));
  499. for ($page = $page_inf; $page <= $page_sup; $page++) {
  500. if ($page == $actual_page) {
  501. $nav_bar .= " <span class=\"phpdigHighlight\">$page</span> \n";
  502. $pages_bar .= " <span class=\"phpdigHighlight\">$page</span> \n";
  503. $link_actual = $url_bar.(($page-1)*$limite);
  504. }
  505. else {
  506. $nav_bar .= " <a class=\"phpdig\" href=\"".$url_bar.(($page-1)*$limite)."\" >$page</a> \n";
  507. $pages_bar .= " <a class=\"phpdig\" href=\"".$url_bar.(($page-1)*$limite)."\" >$page</a> \n";
  508. }
  509. }
  510. if ($more_results == 1) {
  511. $next_link = $url_bar.($lim_start+$limite);
  512. $nav_bar .= " &nbsp;&nbsp;&nbsp;<a class=\"phpdig\" href=\"$next_link\" >".phpdigMsg('next')."&gt;&gt;</a>\n";
  513. }
  514. $mtime = explode(' ',microtime());
  515. $search_time = sprintf('%01.2f',$mtime[0]+$mtime[1]-$start_time);
  516. $result_message = stripslashes(ucfirst(phpdigMsg('results'))." $n_start-$n_end, $num_tot ".phpdigMsg('total').", ".phpdigMsg('on')." \"".htmlspecialchars($query_string,ENT_QUOTES)."\" ($search_time ".phpdigMsg('seconds').")");
  517. $timer->stop('Final strings');
  518. }
  519. else {
  520. if (is_array($strings)) {
  521. $strings = array_values($strings);
  522. $num_in_strings_arr = count($strings);
  523. }
  524. else { $num_in_strings_arr = 0; }
  525. $leven_final = "";
  526. $leven_sum = 0;
  527. if (($num_in_strings_arr > 0) && (strlen($path) == 0)) {
  528. for ($i=0; $i<$num_in_strings_arr; $i++) {
  529. $soundex_query = "SELECT keyword FROM ".PHPDIG_DB_PREFIX."keywords WHERE SOUNDEX(CONCAT('Q',keyword)) = SOUNDEX(CONCAT('Q','".$strings[$i]."')) LIMIT 500";
  530. $soundex_results = mysql_query($soundex_query,$id_connect);
  531. if (mysql_num_rows($soundex_results) > 0) {
  532. $leven_ind = 0;
  533. $leven_amt1 = 256;
  534. $leven_keyword = array();
  535. while (list($soundex_keyword) = mysql_fetch_array($soundex_results)) {
  536. $leven_amt2 = min(levenshtein(stripslashes($strings[$i]),$soundex_keyword),$leven_amt1);
  537. if (($leven_amt2 < $leven_amt1) && ($leven_amt2 >= 0) && ($leven_amt2 <= 5)) {
  538. $leven_keyword[$leven_ind] = stripslashes($soundex_keyword);
  539. $leven_ind++;
  540. }
  541. $leven_amt1 = $leven_amt2;
  542. }
  543. $leven_count = count($leven_keyword);
  544. $leven_sum = $leven_sum + $leven_amt1;
  545. if ($leven_count > 0) {
  546. $leven_final .= $leven_keyword[$leven_count-1] . " ";
  547. }
  548. if (isset($leven_keyword)) { unset($leven_keyword); }
  549. }
  550. }
  551. }
  552. $num_tot = 0;
  553. $result_message = phpdigMsg('noresults');
  554. if ((strlen(trim($leven_final)) > 0) && ($leven_sum > 0)) {
  555. $leven_query = trim($leven_final);
  556. $result_message .= ". " . phpdigMsg('alt_try') ." <a class=\"phpdigMessage\" href=\"".SEARCH_PAGE."?template_demo=$template_demo&amp;query_string=".urlencode($leven_query)."\"><i>".htmlspecialchars($leven_query,ENT_QUOTES)."</i></a>?";
  557. }
  558. }
  559. if (isset($tempresult)) {
  560. mysql_free_result($tempresult);
  561. }
  562. $title_message = phpdigMsg('s_results');
  563. }
  564. else {
  565. $title_message = 'PhpDig '.PHPDIG_VERSION;
  566. $result_message = phpdigMsg('no_query').'.';
  567. }
  568. $timer->start('Logs');
  569. if (PHPDIG_LOGS == true && !$browse && !$refine && $adlog_flag == 0) {
  570. if (is_array($final_result)) {
  571. phpdigAddLog ($id_connect,$option,$strings,$exclude,count($final_result),$search_time);
  572. }
  573. else {
  574. phpdigAddLog ($id_connect,$option,$strings,$exclude,0,$search_time);
  575. }
  576. }
  577. $timer->stop('Logs');
  578. $timer->start('Template parsing');
  579. $powered_by_link = "<font size=\"1\" face=\"verdana,arial,sans-serif\">";
  580. if (ALLOW_RSS_FEED) {
  581. $powered_by_link .= "<a href=\"".$rssdf."\">".phpdigMsg('viewRSS')."</a><br>";
  582. }
  583. $powered_by_link .= "<a href=\"http://www.phpdig.net/\">".phpdigMsg('powered_by')."</a><br></font>";
  584. if (is_array($strings)) {
  585. $js_string = implode(" ",$strings);
  586. } else {
  587. $js_string = "";
  588. }
  589. $js_for_clicks = "
  590. <script type=\"text/javascript\">
  591. /* <![CDATA[ */
  592. function clickit(cn,clink) {
  593. if(document.images) {
  594. (new Image()).src=\"clickstats.php?num=\"+cn+\"&url=\"+clink+\"&val=".urlencode($js_string)."\";
  595. }
  596. return true;
  597. }
  598. /* ]]> */
  599. </script>
  600. ";
  601. if ($template == 'array' || is_file($template)) {
  602. $phpdig_version = PHPDIG_VERSION;
  603. $t_mstrings = compact('js_for_clicks','powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link');
  604. $t_fstrings = phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$template_demo,$num_tot,$refine);
  605. if ($template == 'array') {
  606. return array_merge($t_mstrings,$t_fstrings,array('results'=>$table_results));
  607. }
  608. else {
  609. $t_strings = array_merge($t_mstrings,$t_fstrings);
  610. phpdigParseTemplate($template,$t_strings,$table_results);
  611. }
  612. }
  613. else {
  614. ?>
  615. <?php include $relative_script_path.'/libs/htmlheader.php' ?>
  616. <head>
  617. <title><?php print $title_message ?></title>
  618. <?php include $relative_script_path.'/libs/htmlmetas.php' ?>
  619. <style type="text/css">
  620. /*<![CDATA[*/
  621. .phpdigHighlight {color:<?php print HIGHLIGHT_COLOR ?>;
  622. background-color:<?php print HIGHLIGHT_BACKGROUND ?>;
  623. font-weight:bold;
  624. }
  625. .phpdigMessage {padding:1px;background-color:#002288;color:white;}
  626. /*]]>*/
  627. </style>
  628. <script type="text/javascript">
  629. /* <![CDATA[ */
  630. function clickit(cn,clink) {
  631. if(document.images) {
  632. (new Image()).src="clickstats.php?num="+cn+"&url="+clink+"&val=<?php echo urlencode($js_string); ?>";
  633. }
  634. return true;
  635. }
  636. /* ]]> */
  637. </script>
  638. </head>
  639. <body bgcolor="white">
  640. <div align="center">
  641. <img src="phpdig_logo_2.png" width="200" height="114" alt="phpdig <?php print PHPDIG_VERSION ?>" border="0" />
  642. <br />
  643. <?php
  644. phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'classic',$template_demo,$num_tot,$refine);
  645. ?>
  646. <h3><span class="phpdigMsg"><?php print $result_message ?></span>
  647. <br /><span class="phpdigAlert"><?php print $ignore_message ?></span>
  648. <br /><span class="phpdigAlert"><?php print $ignore_commess ?></span>
  649. </h3>
  650. </div>
  651. <?php
  652. if (is_array($table_results)) {
  653. while (list($n,$t_result) = each($table_results)) {
  654. print "<p style='background-color:#CCDDFF;'>\n";
  655. print "<b>$n. <font style='font-size:10;'>[".$t_result['weight']." %]</font>&nbsp;&nbsp;".$t_result['page_link']."</b>\n<br />\n";
  656. print "<font style='font-size:10;background-color:#BBCCEE;'>".$t_result['limit_links']."</font>\n<br />\n";
  657. print "</p>\n";
  658. print "<blockquote style='background-color:#EEEEEE;font-size:10;'>\n";
  659. print $t_result['text'];
  660. print "</blockquote>\n";
  661. }
  662. }
  663. print "<p style='text-align:center;background-color:#CCDDFF;font-weight:bold'>\n";
  664. print $nav_bar;
  665. print "</p>\n";
  666. ?>
  667. <hr />
  668. <div align="center">
  669. <?php
  670. if ($query_string) {
  671. phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'classic',$template_demo,$num_tot,$refine);
  672. }
  673. ?>
  674. </div>
  675. <div align='center'>
  676. <a href='http://www.phpdig.net/' target='_blank'><img src='phpdig_powered_2.png' width='88' height='28' border='0' alt='Powered by PhpDig' /></a> &nbsp;
  677. </div>
  678. </body>
  679. </html>
  680. <?php
  681. }
  682. $timer->stop('Template parsing');
  683. $timer->stop('All display');
  684. $timer->stop('All');
  685. //$timer->display();
  686. }
  687. function phpdigSpanReplace($text) {
  688. $text = str_replace("&lt;br&gt;","<br>",$text); // RH
  689. $text = str_replace("&lt;/span&gt;","</span>",$text);
  690. $text = str_replace("&lt;span class=&quot;phpdigHighlight&quot;&gt;","<span class=\"phpdigHighlight\">",$text);
  691. return $text;
  692. }
  693. function phpdigByLength($a, $b) {
  694. $len_a = strlen($a);
  695. $len_b = strlen($b);
  696. if ($len_a == $len_b) { return 0; }
  697. return ($len_a < $len_b) ? 1 : -1;
  698. }
  699. ?>