search_function.php 31 KB

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