search_function.php 31 KB

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