PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/system/modules/search/mod.search.php

https://github.com/danboy/Croissierd
PHP | 1910 lines | 1174 code | 427 blank | 309 comment | 278 complexity | 723c698d5620b3c0c5cafb961b3ca817 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2003 - 2010 EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/docs/license.html
  13. =====================================================
  14. File: mod.search.php
  15. -----------------------------------------------------
  16. Purpose: Search class
  17. =====================================================
  18. */
  19. if ( ! defined('EXT'))
  20. {
  21. exit('Invalid file request');
  22. }
  23. class Search {
  24. var $min_length = 3; // Minimum length of search keywords
  25. var $cache_expire = 2; // How many hours should we keep search caches?
  26. var $keywords = "";
  27. var $text_format = 'xhtml'; // Excerpt text formatting
  28. var $html_format = 'all'; // Excerpt html formatting
  29. var $auto_links = 'y'; // Excerpt auto-linking: y/n
  30. var $allow_img_url = 'n'; // Excerpt - allow images: y/n
  31. var $blog_array = array();
  32. var $cat_array = array();
  33. var $fields = array();
  34. var $num_rows = 0;
  35. /** ----------------------------------------
  36. /** Perform Search
  37. /** ----------------------------------------*/
  38. function do_search()
  39. {
  40. global $IN, $LANG, $DB, $SESS, $OUT, $FNS, $REGX, $PREFS;
  41. /** ----------------------------------------
  42. /** Fetch the search language file
  43. /** ----------------------------------------*/
  44. $LANG->fetch_language_file('search');
  45. /** ----------------------------------------
  46. /** Profile Exception
  47. /** ----------------------------------------*/
  48. // This is an exception to the normal search routine.
  49. // It permits us to search for all posts by a particular user's screen name
  50. // We look for the "mbr" $_GET variable. If it exsists it will
  51. // trigger our exception
  52. if ($IN->GBL('mbr'))
  53. {
  54. $_POST['RP'] = ($IN->GBL('result_path') != '') ? $IN->GBL('result_path') : 'search/results';
  55. $_POST['keywords'] = '';
  56. $_POST['exact_match'] = 'y';
  57. $_POST['exact_keyword'] = 'n';
  58. // $_POST['member_name'] = urldecode($IN->GBL('fetch_posts_by'));
  59. }
  60. /** ----------------------------------------
  61. /** Pulldown Addition - Any, All, Exact
  62. /** ----------------------------------------*/
  63. if (isset($_POST['where']) && $_POST['where'] == 'exact')
  64. {
  65. $_POST['exact_keyword'] = 'y';
  66. }
  67. /** ----------------------------------------
  68. /** Do we have a search results page?
  69. /** ----------------------------------------*/
  70. // The search results template is specified as a parameter in the search form tag.
  71. // If the parameter is missing we'll issue an error since we don't know where to
  72. // show the results
  73. if ( ! isset($_POST['RP']) OR $_POST['RP'] == '')
  74. {
  75. return $OUT->show_user_error('general', array($LANG->line('search_path_error')));
  76. }
  77. /** ----------------------------------------
  78. /** Is the current user allowed to search?
  79. /** ----------------------------------------*/
  80. if ($SESS->userdata['can_search'] == 'n' AND $SESS->userdata['group_id'] != 1)
  81. {
  82. return $OUT->show_user_error('general', array($LANG->line('search_not_allowed')));
  83. }
  84. /** ----------------------------------------
  85. /** Flood control
  86. /** ----------------------------------------*/
  87. if ($SESS->userdata['search_flood_control'] > 0 AND $SESS->userdata['group_id'] != 1)
  88. {
  89. $cutoff = time() - $SESS->userdata['search_flood_control'];
  90. $sql = "SELECT search_id FROM exp_search WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND search_date > '{$cutoff}' AND ";
  91. if ($SESS->userdata['member_id'] != 0)
  92. {
  93. $sql .= "(member_id='".$DB->escape_str($SESS->userdata('member_id'))."' OR ip_address='".$DB->escape_str($IN->IP)."')";
  94. }
  95. else
  96. {
  97. $sql .= "ip_address='".$DB->escape_str($IN->IP)."'";
  98. }
  99. $query = $DB->query($sql);
  100. $text = str_replace("%x", $SESS->userdata['search_flood_control'], $LANG->line('search_time_not_expired'));
  101. if ($query->num_rows > 0)
  102. {
  103. return $OUT->show_user_error('general', array($text));
  104. }
  105. }
  106. /** ----------------------------------------
  107. /** Did the user submit any keywords?
  108. /** ----------------------------------------*/
  109. // We only require a keyword if the member name field is blank
  110. if ( ! isset($_GET['mbr']) OR ! is_numeric($_GET['mbr']))
  111. {
  112. if ( ! isset($_POST['member_name']) OR $_POST['member_name'] == '')
  113. {
  114. if ( ! isset($_POST['keywords']) OR $_POST['keywords'] == "")
  115. {
  116. return $OUT->show_user_error('general', array($LANG->line('search_no_keywords')));
  117. }
  118. }
  119. }
  120. /** ----------------------------------------
  121. /** Strip extraneous junk from keywords
  122. /** ----------------------------------------*/
  123. if ($_POST['keywords'] != "")
  124. {
  125. $this->keywords = $REGX->keyword_clean($_POST['keywords']);
  126. /** ----------------------------------------
  127. /** Is the search term long enough?
  128. /** ----------------------------------------*/
  129. if (strlen($this->keywords) < $this->min_length)
  130. {
  131. $text = $LANG->line('search_min_length');
  132. $text = str_replace("%x", $this->min_length, $text);
  133. return $OUT->show_user_error('general', array($text));
  134. }
  135. $this->keywords = ($PREFS->ini('auto_convert_high_ascii') == 'y') ? $REGX->ascii_to_entities($this->keywords) : $this->keywords;
  136. /** ----------------------------------------
  137. /** Remove "ignored" words
  138. /** ----------------------------------------*/
  139. if (( ! isset($_POST['exact_keyword']) OR $_POST['exact_keyword'] != 'y') && @include_once(PATH_LIB.'stopwords'.EXT))
  140. {
  141. $parts = explode('"', $this->keywords);
  142. $this->keywords = '';
  143. foreach($parts as $num => $part)
  144. {
  145. // The odd breaks contain quoted strings.
  146. if ($num % 2 == 0)
  147. {
  148. foreach ($ignore as $badword)
  149. {
  150. $part = preg_replace("/\b".preg_quote($badword, '/')."\b/i","", $part);
  151. }
  152. }
  153. $this->keywords .= ($num != 0) ? '"'.$part : $part;
  154. }
  155. if (trim($this->keywords) == '')
  156. {
  157. return $OUT->show_user_error('general', array($LANG->line('search_no_stopwords')));
  158. }
  159. }
  160. /** ----------------------------------------
  161. /** Log Search Terms
  162. /** ----------------------------------------*/
  163. $FNS->log_search_terms($this->keywords);
  164. }
  165. if (isset($_POST['member_name']) AND $_POST['member_name'] != "")
  166. {
  167. $_POST['member_name'] = $REGX->xss_clean($_POST['member_name']);
  168. }
  169. /** ----------------------------------------
  170. /** Build and run query
  171. /** ----------------------------------------*/
  172. $original_keywords = $this->keywords;
  173. $mbr = ( ! isset($_GET['mbr'])) ? '' : $_GET['mbr'];
  174. $sql = $this->build_standard_query();
  175. /** ----------------------------------------
  176. /** No query results?
  177. /** ----------------------------------------*/
  178. if ($sql == FALSE)
  179. {
  180. if (isset($_POST['NRP']) AND $_POST['NRP'] != '')
  181. {
  182. $hash = $FNS->random('md5');
  183. $data = array(
  184. 'search_id' => $hash,
  185. 'search_date' => time(),
  186. 'member_id' => $SESS->userdata('member_id'),
  187. 'keywords' => ($original_keywords != '') ? $original_keywords : $mbr,
  188. 'ip_address' => $IN->IP,
  189. 'total_results' => 0,
  190. 'per_page' => 0,
  191. 'query' => '',
  192. 'custom_fields' => '',
  193. 'result_page' => '',
  194. 'site_id' => $PREFS->ini('site_id')
  195. );
  196. $DB->query($DB->insert_string('exp_search', $data));
  197. return $FNS->redirect($FNS->create_url($FNS->extract_path("='".$_POST['NRP']."'")).$hash.'/');
  198. }
  199. else
  200. {
  201. return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
  202. }
  203. }
  204. /** ----------------------------------------
  205. /** If we have a result, cache it
  206. /** ----------------------------------------*/
  207. $hash = $FNS->random('md5');
  208. $sql = str_replace("\\", "\\\\", $sql);
  209. // This fixes a bug that occurs when a different table prefix is used
  210. $sql = str_replace('exp_', 'MDBMPREFIX', $sql);
  211. $data = array(
  212. 'search_id' => $hash,
  213. 'search_date' => time(),
  214. 'member_id' => $SESS->userdata('member_id'),
  215. 'keywords' => ($original_keywords != '') ? $original_keywords : $mbr,
  216. 'ip_address' => $IN->IP,
  217. 'total_results' => $this->num_rows,
  218. 'per_page' => (isset($_POST['RES']) AND is_numeric($_POST['RES']) AND $_POST['RES'] < 999 ) ? $_POST['RES'] : 50,
  219. 'query' => addslashes(serialize($sql)),
  220. 'custom_fields' => addslashes(serialize($this->fields)),
  221. 'result_page' => $_POST['RP'],
  222. 'site_id' => $PREFS->ini('site_id')
  223. );
  224. $DB->query($DB->insert_string('exp_search', $data));
  225. /** ----------------------------------------
  226. /** Redirect to search results page
  227. /** ----------------------------------------*/
  228. $path = $FNS->remove_double_slashes($FNS->create_url($REGX->trim_slashes($_POST['RP'])).$hash.'/');
  229. return $FNS->redirect($path);
  230. }
  231. /* END */
  232. /** ---------------------------------------
  233. /** Create the search query
  234. /** ---------------------------------------*/
  235. function build_standard_query()
  236. {
  237. global $DB, $LOC, $FNS, $IN, $PREFS;
  238. $blog_array = array();
  239. /** ---------------------------------------
  240. /** Fetch the weblog_id numbers
  241. /** ---------------------------------------*/
  242. // If $_POST['weblog_id'] exists we know the request is coming from the
  243. // advanced search form. We set those values to the $blog_id_array
  244. if (isset($_POST['weblog_id']) AND is_array($_POST['weblog_id']))
  245. {
  246. $blog_id_array = $_POST['weblog_id'];
  247. }
  248. // Since both the simple and advanced search form have
  249. // $_POST['weblog'], then we can safely find all of the
  250. // weblogs available for searching
  251. // By doing this for the advanced search form, we can discover
  252. // Which weblogs we are or are not supposed to search for, when
  253. // "Any Weblog" is chosen
  254. $sql = "SELECT weblog_id FROM exp_weblogs WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND ";
  255. if (USER_BLOG !== FALSE)
  256. {
  257. // If it's a "user blog" we limit to only their assigned blog
  258. $sql .= "weblog_id = '".UB_BLOG_ID."' ";
  259. }
  260. else
  261. {
  262. $sql .= "is_user_blog = 'n' ";
  263. if (isset($_POST['weblog']) AND $_POST['weblog'] != '')
  264. {
  265. $sql .= $FNS->sql_andor_string($_POST['weblog'], 'blog_name');
  266. }
  267. }
  268. $query = $DB->query($sql);
  269. foreach ($query->result as $row)
  270. {
  271. $blog_array[] = $row['weblog_id'];
  272. }
  273. /** ------------------------------------------------------
  274. /** Find the Common Weblog IDs for Advanced Search Form
  275. /** ------------------------------------------------------*/
  276. if (isset($blog_id_array) && $blog_id_array['0'] != 'null')
  277. {
  278. $blog_array = array_intersect($blog_id_array, $blog_array);
  279. }
  280. /** ----------------------------------------------
  281. /** Fetch the weblog_id numbers (from Advanced search)
  282. /** ----------------------------------------------*/
  283. // We do this up-front since we use this same sub-query in two places
  284. $id_query = '';
  285. if (count($blog_array) > 0)
  286. {
  287. foreach ($blog_array as $val)
  288. {
  289. if ($val != 'null' AND $val != '')
  290. {
  291. $id_query .= " exp_weblog_titles.weblog_id = '".$DB->escape_str($val)."' OR";
  292. }
  293. }
  294. if ($id_query != '')
  295. {
  296. $id_query = substr($id_query, 0, -2);
  297. $id_query = ' AND ('.$id_query.') ';
  298. }
  299. }
  300. /** ----------------------------------------------
  301. /** Limit to a specific member? We do this now
  302. /** as there's a potential for this to bring the
  303. /** search to an end if it's not a valid member
  304. /** ----------------------------------------------*/
  305. $member_array = array();
  306. $member_ids = '';
  307. if (isset($_GET['mbr']) AND is_numeric($_GET['mbr']))
  308. {
  309. $query = $DB->query("SELECT member_id FROM exp_members WHERE member_id = '".$DB->escape_str($_GET['mbr'])."'");
  310. if ($query->num_rows != 1)
  311. {
  312. return FALSE;
  313. }
  314. else
  315. {
  316. $member_array[] = $query->row['member_id'];
  317. }
  318. }
  319. else
  320. {
  321. if (isset($_POST['member_name']) AND $_POST['member_name'] != '')
  322. {
  323. $sql = "SELECT member_id FROM exp_members WHERE screen_name ";
  324. if (isset($_POST['exact_match']) AND $_POST['exact_match'] == 'y')
  325. {
  326. $sql .= " = '".$DB->escape_str($_POST['member_name'])."' ";
  327. }
  328. else
  329. {
  330. $sql .= " LIKE '%".$DB->escape_like_str($_POST['member_name'])."%' ";
  331. }
  332. $query = $DB->query($sql);
  333. if ($query->num_rows == 0)
  334. {
  335. return FALSE;
  336. }
  337. else
  338. {
  339. foreach ($query->result as $row)
  340. {
  341. $member_array[] = $row['member_id'];
  342. }
  343. }
  344. }
  345. }
  346. // and turn it into a string now so we only implode once
  347. if (count($member_array) > 0)
  348. {
  349. $member_ids = ' IN ('.implode(',', $member_array).') ';
  350. }
  351. unset($member_array);
  352. /** ---------------------------------------
  353. /** Fetch the searchable field names
  354. /** ---------------------------------------*/
  355. $fields = array();
  356. // no need to do this unless there are keywords to search
  357. if (trim($this->keywords) != '')
  358. {
  359. $xql = "SELECT DISTINCT(field_group) FROM exp_weblogs WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND ";
  360. if (USER_BLOG !== FALSE)
  361. {
  362. $xql .= "weblog_id = '".UB_BLOG_ID."' ";
  363. }
  364. else
  365. {
  366. $xql .= "is_user_blog = 'n' ";
  367. }
  368. if ($id_query != '')
  369. {
  370. $xql .= $id_query.' ';
  371. $xql = str_replace('exp_weblog_titles.', '', $xql);
  372. }
  373. $query = $DB->query($xql);
  374. if ($query->num_rows > 0)
  375. {
  376. $fql = "SELECT field_id, field_name, field_search FROM exp_weblog_fields WHERE (";
  377. foreach ($query->result as $row)
  378. {
  379. $fql .= " group_id = '".$row['field_group']."' OR";
  380. }
  381. $fql = substr($fql, 0, -2).')';
  382. $query = $DB->query($fql);
  383. if ($query->num_rows > 0)
  384. {
  385. foreach ($query->result as $row)
  386. {
  387. if ($row['field_search'] == 'y')
  388. {
  389. $fields[] = $row['field_id'];
  390. }
  391. $this->fields[$row['field_name']] = array($row['field_id'], $row['field_search']);
  392. }
  393. }
  394. }
  395. }
  396. /** ---------------------------------------
  397. /** Build the main query
  398. /** ---------------------------------------*/
  399. $sql = "SELECT
  400. DISTINCT(exp_weblog_titles.entry_id)
  401. FROM exp_weblog_titles
  402. LEFT JOIN exp_weblogs ON exp_weblog_titles.weblog_id = exp_weblogs.weblog_id
  403. LEFT JOIN exp_weblog_data ON exp_weblog_titles.entry_id = exp_weblog_data.entry_id
  404. LEFT JOIN exp_comments ON exp_weblog_titles.entry_id = exp_comments.entry_id
  405. LEFT JOIN exp_category_posts ON exp_weblog_titles.entry_id = exp_category_posts.entry_id
  406. LEFT JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id
  407. WHERE exp_weblogs.site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'
  408. AND ";
  409. /** ----------------------------------------------
  410. /** Is this a user blog?
  411. /** ----------------------------------------------*/
  412. if (USER_BLOG !== FALSE)
  413. {
  414. $sql .= "exp_weblogs.weblog_id = '".UB_BLOG_ID."' ";
  415. }
  416. else
  417. {
  418. $sql .= "exp_weblogs.is_user_blog = 'n' ";
  419. }
  420. /** ----------------------------------------------
  421. /** We only select entries that have not expired
  422. /** ----------------------------------------------*/
  423. if ( ! isset($_POST['show_future_entries']) OR $_POST['show_future_entries'] != 'yes')
  424. {
  425. $sql .= "\nAND exp_weblog_titles.entry_date < ".$LOC->now." ";
  426. }
  427. if ( ! isset($_POST['show_expired']) OR $_POST['show_expired'] != 'yes')
  428. {
  429. $sql .= "\nAND (exp_weblog_titles.expiration_date = 0 OR exp_weblog_titles.expiration_date > ".$LOC->now.") ";
  430. }
  431. /** ----------------------------------------------
  432. /** Add status declaration to the query
  433. /** ----------------------------------------------*/
  434. if (($status = $IN->GBL('status')) !== FALSE)
  435. {
  436. $status = str_replace('Open', 'open', $status);
  437. $status = str_replace('Closed', 'closed', $status);
  438. $sql .= $FNS->sql_andor_string($status, 'exp_weblog_titles.status');
  439. // add exclusion for closed unless it was explicitly used
  440. if (strncasecmp($status, 'not ', 4) == 0)
  441. {
  442. $status = trim(substr($status, 3));
  443. }
  444. $stati = explode('|', $status);
  445. if (! in_array('closed', $stati))
  446. {
  447. $sql .= "\nAND exp_weblog_titles.status != 'closed' ";
  448. }
  449. }
  450. else
  451. {
  452. $sql .= "AND exp_weblog_titles.status = 'open' ";
  453. }
  454. /** ----------------------------------------------
  455. /** Set Date filtering
  456. /** ----------------------------------------------*/
  457. if (isset($_POST['date']) AND $_POST['date'] != 0)
  458. {
  459. $cutoff = $LOC->now - (60*60*24*$_POST['date']);
  460. if (isset($_POST['date_order']) AND $_POST['date_order'] == 'older')
  461. {
  462. $sql .= "AND exp_weblog_titles.entry_date < ".$cutoff." ";
  463. }
  464. else
  465. {
  466. $sql .= "AND exp_weblog_titles.entry_date > ".$cutoff." ";
  467. }
  468. }
  469. /** ----------------------------------------------
  470. /** Add keyword to the query
  471. /** ----------------------------------------------*/
  472. if (trim($this->keywords) != '')
  473. {
  474. // So it begins
  475. $sql .= "\nAND (";
  476. /** -----------------------------------------
  477. /** Process our Keywords into Search Terms
  478. /** -----------------------------------------*/
  479. $this->keywords = stripslashes($this->keywords);
  480. $terms = array();
  481. $criteria = (isset($_POST['where']) && $_POST['where'] == 'all') ? 'AND' : 'OR';
  482. if (preg_match_all("/\-*\"(.*?)\"/", $this->keywords, $matches))
  483. {
  484. for($m=0; $m < sizeof($matches['1']); $m++)
  485. {
  486. $terms[] = trim(str_replace('"','',$matches['0'][$m]));
  487. $this->keywords = str_replace($matches['0'][$m],'', $this->keywords);
  488. }
  489. }
  490. if (trim($this->keywords) != '')
  491. {
  492. $terms = array_merge($terms, preg_split("/\s+/", trim($this->keywords)));
  493. }
  494. $not_and = (sizeof($terms) > 2) ? ') AND (' : 'AND';
  495. rsort($terms);
  496. $terms_like = $DB->escape_like_str($terms);
  497. $terms = $DB->escape_str($terms);
  498. /** ----------------------------------
  499. /** Search in Title Field
  500. /** ----------------------------------*/
  501. if (sizeof($terms) == 1 && isset($_POST['where']) && $_POST['where'] == 'word') // Exact word match
  502. {
  503. $sql .= "((exp_weblog_titles.title = '".$terms['0']."' OR exp_weblog_titles.title LIKE '".$terms_like['0']." %' OR exp_weblog_titles.title LIKE '% ".$terms_like['0']." %') ";
  504. // and close up the member clause
  505. if ($member_ids != '')
  506. {
  507. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) \n";
  508. }
  509. else
  510. {
  511. $sql .= ") \n";
  512. }
  513. }
  514. elseif ( ! isset($_POST['exact_keyword'])) // Any terms, all terms
  515. {
  516. $mysql_function = (substr($terms['0'], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  517. $search_term = (substr($terms['0'], 0,1) == '-') ? substr($terms_like['0'], 1) : $terms_like['0'];
  518. // We have three parentheses in the beginning in case
  519. // there are any NOT LIKE's being used and to allow for a member clause
  520. $sql .= "\n(((exp_weblog_titles.title $mysql_function '%".$search_term."%' ";
  521. for ($i=1; $i < sizeof($terms); $i++)
  522. {
  523. $mysql_criteria = ($mysql_function == 'NOT LIKE' OR substr($terms[$i], 0,1) == '-') ? $not_and : $criteria;
  524. $mysql_function = (substr($terms[$i], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  525. $search_term = (substr($terms[$i], 0,1) == '-') ? substr($terms_like[$i], 1) : $terms_like[$i];
  526. $sql .= "$mysql_criteria exp_weblog_titles.title $mysql_function '%".$search_term."%' ";
  527. }
  528. $sql .= ")) ";
  529. // and close up the member clause
  530. if ($member_ids != '')
  531. {
  532. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) \n";
  533. }
  534. else
  535. {
  536. $sql .= ") \n";
  537. }
  538. }
  539. else // exact phrase match
  540. {
  541. $search_term = (sizeof($terms) == 1) ? $terms_like[0] : $DB->escape_like_str($this->keywords);
  542. $sql .= "(exp_weblog_titles.title LIKE '%".$search_term."%' ";
  543. // and close up the member clause
  544. if ($member_ids != '')
  545. {
  546. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) \n";
  547. }
  548. else
  549. {
  550. $sql .= ") \n";
  551. }
  552. }
  553. /** ----------------------------------
  554. /** Search in Searchable Fields
  555. /** ----------------------------------*/
  556. if (isset($_POST['search_in']) AND ($_POST['search_in'] == 'entries' OR $_POST['search_in'] == 'everywhere'))
  557. {
  558. if (sizeof($terms) > 1 && isset($_POST['where']) && $_POST['where'] == 'all' && ! isset($_POST['exact_keyword']) && sizeof($fields) > 0)
  559. {
  560. // force case insensitivity, but only on 4.0.2 or higher
  561. if (version_compare(mysql_get_server_info(), '4.0.2', '>=') !== FALSE)
  562. {
  563. $concat_fields = "CAST(CONCAT_WS(' ', exp_weblog_data.field_id_".implode(', exp_weblog_data.field_id_', $fields).') AS CHAR)';
  564. }
  565. else
  566. {
  567. $concat_fields = "CONCAT_WS(' ', exp_weblog_data.field_id_".implode(', exp_weblog_data.field_id_', $fields).')';
  568. }
  569. $mysql_function = (substr($terms['0'], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  570. $search_term = (substr($terms['0'], 0,1) == '-') ? substr($terms_like['0'], 1) : $terms_like['0'];
  571. // Since Title is always required in a search we use OR
  572. // And then three parentheses just like above in case
  573. // there are any NOT LIKE's being used and to allow for a member clause
  574. $sql .= "\nOR ((($concat_fields $mysql_function '%".$search_term."%' ";
  575. for ($i=1; $i < sizeof($terms); $i++)
  576. {
  577. $mysql_criteria = ($mysql_function == 'NOT LIKE' OR substr($terms[$i], 0,1) == '-') ? $not_and : $criteria;
  578. $mysql_function = (substr($terms[$i], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  579. $search_term = (substr($terms[$i], 0,1) == '-') ? substr($terms_like[$i], 1) : $terms_like[$i];
  580. $sql .= "$mysql_criteria $concat_fields $mysql_function '%".$search_term."%' ";
  581. }
  582. $sql .= ")) ";
  583. // and close up the member clause
  584. if ($member_ids != '')
  585. {
  586. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) \n";
  587. }
  588. else
  589. {
  590. $sql .= ") \n";
  591. }
  592. }
  593. else
  594. {
  595. foreach ($fields as $val)
  596. {
  597. if (sizeof($terms) == 1 && isset($_POST['where']) && $_POST['where'] == 'word')
  598. {
  599. $sql .= "\nOR ((exp_weblog_data.field_id_".$val." LIKE '".$terms_like['0']." %' OR exp_weblog_data.field_id_".$val." LIKE '% ".$terms_like['0']." %' OR exp_weblog_data.field_id_".$val." LIKE '% ".$terms_like['0']."' OR exp_weblog_data.field_id_".$val." = '".$terms['0']."') ";
  600. // and close up the member clause
  601. if ($member_ids != '')
  602. {
  603. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) ";
  604. }
  605. else
  606. {
  607. $sql .= ") ";
  608. }
  609. }
  610. elseif ( ! isset($_POST['exact_keyword']))
  611. {
  612. $mysql_function = (substr($terms['0'], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  613. $search_term = (substr($terms['0'], 0,1) == '-') ? substr($terms_like['0'], 1) : $terms_like['0'];
  614. // Since Title is always required in a search we use OR
  615. // And then three parentheses just like above in case
  616. // there are any NOT LIKE's being used and to allow for a member clause
  617. $sql .= "\nOR (((exp_weblog_data.field_id_".$val." $mysql_function '%".$search_term."%' ";
  618. for ($i=1; $i < sizeof($terms); $i++)
  619. {
  620. $mysql_criteria = ($mysql_function == 'NOT LIKE' OR substr($terms[$i], 0,1) == '-') ? $not_and : $criteria;
  621. $mysql_function = (substr($terms[$i], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  622. $search_term = (substr($terms[$i], 0,1) == '-') ? substr($terms_like[$i], 1) : $terms_like[$i];
  623. $sql .= "$mysql_criteria exp_weblog_data.field_id_".$val." $mysql_function '%".$search_term."%' ";
  624. }
  625. $sql .= ")) ";
  626. // and close up the member clause
  627. if ($member_ids != '')
  628. {
  629. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) \n";
  630. }
  631. else
  632. {
  633. // close up the extra parenthesis
  634. $sql .= ") \n";
  635. }
  636. }
  637. else
  638. {
  639. $search_term = (sizeof($terms) == 1) ? $terms_like[0] : $DB->escape_like_str($this->keywords);
  640. $sql .= "\nOR (exp_weblog_data.field_id_".$val." LIKE '%".$search_term."%' ";
  641. // and close up the member clause
  642. if ($member_ids != '')
  643. {
  644. $sql .= " AND (exp_weblog_titles.author_id {$member_ids})) \n";
  645. }
  646. else
  647. {
  648. // close up the extra parenthesis
  649. $sql .= ") \n";
  650. }
  651. }
  652. }
  653. }
  654. }
  655. /** ----------------------------------
  656. /** Search in Comments
  657. /** ----------------------------------*/
  658. if (isset($_POST['search_in']) AND $_POST['search_in'] == 'everywhere')
  659. {
  660. if (sizeof($terms) == 1 && isset($_POST['where']) && $_POST['where'] == 'word')
  661. {
  662. $sql .= " OR (exp_comments.comment LIKE '% ".$terms_like['0']." %' ";
  663. // and close up the member clause
  664. if ($member_ids != '')
  665. {
  666. $sql .= " AND (exp_comments.author_id {$member_ids})) \n";
  667. }
  668. else
  669. {
  670. // close up the extra parenthesis
  671. $sql .= ") \n";
  672. }
  673. }
  674. elseif ( ! isset($_POST['exact_keyword']))
  675. {
  676. $mysql_function = (substr($terms['0'], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  677. $search_term = (substr($terms['0'], 0,1) == '-') ? substr($terms_like['0'], 1) : $terms_like['0'];
  678. // We have three parentheses in the beginning in case
  679. // there are any NOT LIKE's being used and to allow a member clause
  680. $sql .= "\nOR (((exp_comments.comment $mysql_function '%".$search_term."%' ";
  681. for ($i=1; $i < sizeof($terms); $i++)
  682. {
  683. $mysql_criteria = ($mysql_function == 'NOT LIKE' OR substr($terms[$i], 0,1) == '-') ? $not_and : $criteria;
  684. $mysql_function = (substr($terms[$i], 0,1) == '-') ? 'NOT LIKE' : 'LIKE';
  685. $search_term = (substr($terms[$i], 0,1) == '-') ? substr($terms_like[$i], 1) : $terms_like[$i];
  686. $sql .= "$mysql_criteria exp_comments.comment $mysql_function '%".$search_term."%' ";
  687. }
  688. $sql .= ")) ";
  689. // and close up the member clause
  690. if ($member_ids != '')
  691. {
  692. $sql .= " AND (exp_comments.author_id {$member_ids})) \n";
  693. }
  694. else
  695. {
  696. // close up the extra parenthesis
  697. $sql .= ") \n";
  698. }
  699. }
  700. else
  701. {
  702. $search_term = (sizeof($terms) == 1) ? $terms_like[0] : $DB->escape_like_str($this->keywords);
  703. $sql .= " OR ((exp_comments.comment LIKE '%".$search_term."%') ";
  704. // and close up the member clause
  705. if ($member_ids != '')
  706. {
  707. $sql .= " AND (exp_comments.author_id {$member_ids})) \n";
  708. }
  709. else
  710. {
  711. // close up the extra parenthesis
  712. $sql .= ") \n";
  713. }
  714. }
  715. }
  716. // So it ends
  717. $sql .= ") \n";
  718. }
  719. else
  720. {
  721. // there are no keywords at all. Do we still need a member search?
  722. if ($member_ids != '')
  723. {
  724. $sql .= "AND (exp_weblog_titles.author_id {$member_ids} ";
  725. // searching comments too?
  726. if (isset($_POST['search_in']) AND $_POST['search_in'] == 'everywhere')
  727. {
  728. $sql .= " OR exp_comments.author_id {$member_ids}";
  729. }
  730. $sql .= ")";
  731. }
  732. }
  733. //exit($sql);
  734. /** ----------------------------------------------
  735. /** Limit query to a specific weblog
  736. /** ----------------------------------------------*/
  737. if (count($blog_array) > 0)
  738. {
  739. $sql .= $id_query;
  740. }
  741. /** ----------------------------------------------
  742. /** Limit query to a specific category
  743. /** ----------------------------------------------*/
  744. if (isset($_POST['cat_id']) AND is_array($_POST['cat_id']))
  745. {
  746. $temp = '';
  747. foreach ($_POST['cat_id'] as $val)
  748. {
  749. if ($val != 'all' AND $val != '')
  750. {
  751. $temp .= " exp_categories.cat_id = '".$DB->escape_str($val)."' OR";
  752. }
  753. }
  754. if ($temp != '')
  755. {
  756. $temp = substr($temp, 0, -2);
  757. $sql .= ' AND ('.$temp.') ';
  758. }
  759. }
  760. /** ----------------------------------------------
  761. /** Are there results?
  762. /** ----------------------------------------------*/
  763. $query = $DB->query($sql);
  764. if ($query->num_rows == 0)
  765. {
  766. return FALSE;
  767. }
  768. $this->num_rows = $query->num_rows;
  769. /** ----------------------------------------------
  770. /** Set sort order
  771. /** ----------------------------------------------*/
  772. $order_by = ( ! isset($_POST['order_by'])) ? 'date' : $_POST['order_by'];
  773. $orderby = ( ! isset($_POST['orderby'])) ? $order_by : $_POST['orderby'];
  774. $end = '';
  775. switch ($orderby)
  776. {
  777. case 'most_comments' : $end .= " ORDER BY comment_total ";
  778. break;
  779. case 'recent_comment' : $end .= " ORDER BY recent_comment_date ";
  780. break;
  781. case 'title' : $end .= " ORDER BY title ";
  782. break;
  783. default : $end .= " ORDER BY entry_date ";
  784. break;
  785. }
  786. $order = ( ! isset($_POST['sort_order'])) ? 'desc' : $_POST['sort_order'];
  787. if ($order != 'asc' AND $order != 'desc')
  788. $order = 'desc';
  789. $end .= " ".$order;
  790. $sql = "SELECT DISTINCT(t.entry_id), t.entry_id, t.weblog_id, t.forum_topic_id, t.author_id, t.ip_address, t.title, t.url_title, t.status, t.dst_enabled, t.view_count_one, t.view_count_two, t.view_count_three, t.view_count_four, t.allow_comments, t.comment_expiration_date, t.allow_trackbacks, t.sticky, t.entry_date, t.year, t.month, t.day, t.entry_date, t.edit_date, t.expiration_date, t.recent_comment_date, t.comment_total, t.trackback_total, t.sent_trackbacks, t.recent_trackback_date, t.site_id as entry_site_id,
  791. w.blog_title, w.blog_name, w.search_results_url, w.search_excerpt, w.blog_url, w.comment_url, w.tb_return_url, w.comment_moderate, w.weblog_html_formatting, w.weblog_allow_img_urls, w.weblog_auto_link_urls, w.enable_trackbacks, w.trackback_use_url_title, w.trackback_field, w.trackback_use_captcha, w.trackback_system_enabled,
  792. m.username, m.email, m.url, m.screen_name, m.location, m.occupation, m.interests, m.aol_im, m.yahoo_im, m.msn_im, m.icq, m.signature, m.sig_img_filename, m.sig_img_width, m.sig_img_height, m.avatar_filename, m.avatar_width, m.avatar_height, m.photo_filename, m.photo_width, m.photo_height, m.group_id, m.member_id, m.bday_d, m.bday_m, m.bday_y, m.bio,
  793. md.*,
  794. wd.*
  795. FROM exp_weblog_titles AS t
  796. LEFT JOIN exp_weblogs AS w ON t.weblog_id = w.weblog_id
  797. LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id
  798. LEFT JOIN exp_members AS m ON m.member_id = t.author_id
  799. LEFT JOIN exp_member_data AS md ON md.member_id = m.member_id
  800. WHERE t.entry_id IN (";
  801. foreach ($query->result as $row)
  802. {
  803. $sql .= $row['entry_id'].',';
  804. }
  805. $sql = substr($sql, 0, -1).') '.$end;
  806. return $sql;
  807. }
  808. /* END */
  809. /** ----------------------------------------
  810. /** Total search results
  811. /** ----------------------------------------*/
  812. function total_results()
  813. {
  814. global $IN, $DB;
  815. /** ----------------------------------------
  816. /** Check search ID number
  817. /** ----------------------------------------*/
  818. // If the QSTR variable is less than 32 characters long we
  819. // don't have a valid search ID number
  820. if (strlen($IN->QSTR) < 32)
  821. {
  822. return '';
  823. }
  824. /** ----------------------------------------
  825. /** Fetch ID number and page number
  826. /** ----------------------------------------*/
  827. $search_id = substr($IN->QSTR, 0, 32);
  828. /** ----------------------------------------
  829. /** Fetch the cached search query
  830. /** ----------------------------------------*/
  831. $query = $DB->query("SELECT total_results FROM exp_search WHERE search_id = '".$DB->escape_str($search_id)."'");
  832. if ($query->num_rows == 1)
  833. {
  834. return $query->row['total_results'];
  835. }
  836. else
  837. {
  838. return 0;
  839. }
  840. }
  841. /* END */
  842. /** ----------------------------------------
  843. /** Search keywords
  844. /** ----------------------------------------*/
  845. function keywords()
  846. {
  847. global $IN, $DB, $REGX;
  848. /** ----------------------------------------
  849. /** Check search ID number
  850. /** ----------------------------------------*/
  851. // If the QSTR variable is less than 32 characters long we
  852. // don't have a valid search ID number
  853. if (strlen($IN->QSTR) < 32)
  854. {
  855. return '';
  856. }
  857. /** ----------------------------------------
  858. /** Fetch ID number and page number
  859. /** ----------------------------------------*/
  860. $search_id = substr($IN->QSTR, 0, 32);
  861. /** ----------------------------------------
  862. /** Fetch the cached search query
  863. /** ----------------------------------------*/
  864. $query = $DB->query("SELECT keywords FROM exp_search WHERE search_id = '".$DB->escape_str($search_id)."'");
  865. if ($query->num_rows == 1)
  866. {
  867. return $REGX->encode_ee_tags($REGX->xml_convert($query->row['keywords']));
  868. }
  869. else
  870. {
  871. return '';
  872. }
  873. }
  874. /* END */
  875. /** ----------------------------------------
  876. /** Show search results
  877. /** ----------------------------------------*/
  878. function search_results()
  879. {
  880. global $IN, $DB, $TMPL, $LANG, $FNS, $OUT, $LOC, $PREFS, $REGX;
  881. /** ----------------------------------------
  882. /** Fetch the search language file
  883. /** ----------------------------------------*/
  884. $LANG->fetch_language_file('search');
  885. /** ----------------------------------------
  886. /** Check search ID number
  887. /** ----------------------------------------*/
  888. // If the QSTR variable is less than 32 characters long we
  889. // don't have a valid search ID number
  890. if (strlen($IN->QSTR) < 32)
  891. {
  892. return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
  893. }
  894. /** ----------------------------------------
  895. /** Clear old search results
  896. /** ----------------------------------------*/
  897. $expire = time() - ($this->cache_expire * 3600);
  898. $DB->query("DELETE FROM exp_search WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND search_date < '$expire'");
  899. /** ----------------------------------------
  900. /** Fetch ID number and page number
  901. /** ----------------------------------------*/
  902. // We cleverly disguise the page number in the ID hash string
  903. $cur_page = 0;
  904. if (strlen($IN->QSTR) == 32)
  905. {
  906. $search_id = $IN->QSTR;
  907. }
  908. else
  909. {
  910. $search_id = substr($IN->QSTR, 0, 32);
  911. $cur_page = substr($IN->QSTR, 32);
  912. }
  913. /** ----------------------------------------
  914. /** Fetch the cached search query
  915. /** ----------------------------------------*/
  916. $query = $DB->query("SELECT * FROM exp_search WHERE search_id = '".$DB->escape_str($search_id)."'");
  917. if ($query->num_rows == 0 OR $query->row['total_results'] == 0)
  918. {
  919. return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
  920. }
  921. $fields = ($query->row['custom_fields'] == '') ? array() : unserialize(stripslashes($query->row['custom_fields']));
  922. $sql = unserialize(stripslashes($query->row['query']));
  923. $sql = str_replace('MDBMPREFIX', 'exp_', $sql);
  924. $per_page = $query->row['per_page'];
  925. $res_page = $query->row['result_page'];
  926. /** ----------------------------------------
  927. /** Run the search query
  928. /** ----------------------------------------*/
  929. $query = $DB->query(preg_replace("/SELECT(.*?)\s+FROM\s+/is", 'SELECT COUNT(*) AS count FROM ', $sql));
  930. if ($query->row['count'] == 0)
  931. {
  932. return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
  933. }
  934. /** ----------------------------------------
  935. /** Calculate total number of pages
  936. /** ----------------------------------------*/
  937. $current_page = ($cur_page / $per_page) + 1;
  938. $total_pages = intval($query->row['count'] / $per_page);
  939. if ($query->row['count'] % $per_page)
  940. {
  941. $total_pages++;
  942. }
  943. $page_count = $LANG->line('page').' '.$current_page.' '.$LANG->line('of').' '.$total_pages;
  944. /** -----------------------------
  945. /** Do we need pagination?
  946. /** -----------------------------*/
  947. // If so, we'll add the LIMIT clause to the SQL statement and run the query again
  948. $pager = '';
  949. if ($query->row['count'] > $per_page)
  950. {
  951. if ( ! class_exists('Paginate'))
  952. {
  953. require PATH_CORE.'core.paginate'.EXT;
  954. }
  955. $PGR = new Paginate();
  956. $PGR->path = $FNS->create_url($res_page.'/'.$search_id, 0, 0);
  957. $PGR->total_count = $query->row['count'];
  958. $PGR->per_page = $per_page;
  959. $PGR->cur_page = $cur_page;
  960. $pager = $PGR->show_links();
  961. $sql .= " LIMIT ".$cur_page.", ".$per_page;
  962. }
  963. $query = $DB->query($sql);
  964. $output = '';
  965. if ( ! class_exists('Weblog'))
  966. {
  967. require PATH_MOD.'/weblog/mod.weblog'.EXT;
  968. }
  969. unset($TMPL->var_single['auto_path']);
  970. unset($TMPL->var_single['excerpt']);
  971. unset($TMPL->var_single['id_auto_path']);
  972. unset($TMPL->var_single['full_text']);
  973. unset($TMPL->var_single['switch']);
  974. foreach($TMPL->var_single as $key => $value)
  975. {
  976. if (substr($key, 0, strlen('member_path')) == 'member_path')
  977. {
  978. unset($TMPL->var_single[$key]);
  979. }
  980. }
  981. $weblog = new Weblog;
  982. // This allows the weblog {absolute_count} variable to work
  983. $weblog->p_page = ($per_page * $current_page) - $per_page;
  984. $weblog->fetch_custom_weblog_fields();
  985. $weblog->fetch_custom_member_fields();
  986. $weblog->query = $DB->query($sql);
  987. if ($weblog->query->num_rows == 0)
  988. {
  989. return $TMPL->no_results();
  990. }
  991. if ( ! class_exists('Typography'))
  992. {
  993. require PATH_CORE.'core.typography'.EXT;
  994. }
  995. $weblog->TYPE = new Typography;
  996. $weblog->TYPE->convert_curly = FALSE;
  997. $weblog->TYPE->encode_email = FALSE;
  998. $weblog->fetch_categories();
  999. $weblog->parse_weblog_entries();
  1000. $tagdata = $TMPL->tagdata;
  1001. // Does the tag contain "related entries" that we need to parse out?
  1002. if (count($TMPL->related_data) > 0 AND count($weblog->related_entries) > 0)
  1003. {
  1004. $weblog->parse_related_entries();
  1005. }
  1006. if (count($TMPL->reverse_related_data) > 0 AND count($weblog->reverse_related_entries) > 0)
  1007. {
  1008. $weblog->parse_reverse_related_entries();
  1009. }
  1010. $output = $weblog->return_data;
  1011. $TMPL->tagdata = $tagdata;
  1012. /** -----------------------------
  1013. /** Fetch member path variable
  1014. /** -----------------------------*/
  1015. // We do it here in case it's used in multiple places.
  1016. $m_paths = array();
  1017. if (preg_match_all("/".LD."member_path(\s*=.*?)".RD."/s", $TMPL->tagdata, $matches))
  1018. {
  1019. for ($j = 0; $j < count($matches['0']); $j++)
  1020. {
  1021. $m_paths[] = array($matches['0'][$j], $FNS->extract_path($matches['1'][$j]));
  1022. }
  1023. }
  1024. /** -----------------------------
  1025. /** Fetch switch param
  1026. /** -----------------------------*/
  1027. $switch1 = '';
  1028. $switch2 = '';
  1029. if ($switch = $TMPL->fetch_param('switch'))
  1030. {
  1031. if (strpos($switch, '|') !== FALSE)
  1032. {
  1033. $x = explode("|", $switch);
  1034. $switch1 = $x['0'];
  1035. $switch2 = $x['1'];
  1036. }
  1037. else
  1038. {
  1039. $switch1 = $switch;
  1040. }
  1041. }
  1042. /** -----------------------------
  1043. /** Result Loop - Legacy!
  1044. /** -----------------------------*/
  1045. $i = 0;
  1046. foreach ($query->result as $row)
  1047. {
  1048. if (isset($row['field_id_'.$row['search_excerpt']]) AND $row['field_id_'.$row['search_excerpt']])
  1049. {
  1050. $format = ( ! isset($row['field_ft_'.$row['search_excerpt']])) ? 'xhtml' : $row['field_ft_'.$row['search_excerpt']];
  1051. $full_text = $weblog->TYPE->parse_type(strip_tags($row['field_id_'.$row['search_excerpt']]),
  1052. array(
  1053. 'text_format' => $format,
  1054. 'html_format' => 'safe',
  1055. 'auto_links' => 'y',
  1056. 'allow_img_url' => 'n'
  1057. ));
  1058. $excerpt = strip_tags($full_text);
  1059. $excerpt = trim(preg_replace("/(\015\012)|(\015)|(\012)/", " ", $excerpt));
  1060. $excerpt = $FNS->word_limiter($excerpt, 50);
  1061. }
  1062. else
  1063. {
  1064. $excerpt = '';
  1065. $full_text = '';
  1066. }
  1067. // Parse permalink path
  1068. $url = ($row['search_results_url'] != '') ? $row['search_results_url'] : $row['blog_url'];
  1069. $path = $FNS->remove_double_slashes($REGX->prep_query_string($url).'/'.$row['url_title'].'/');
  1070. $idpath = $FNS->remove_double_slashes($REGX->prep_query_string($url).'/'.$row['entry_id'].'/');
  1071. $switch = ($i++ % 2) ? $switch1 : $switch2;
  1072. $output = preg_replace("/".LD.'switch'.RD."/", $switch, $output, sizeof(explode(LD.'switch'.RD, $TMPL->tagdata)) - 1);
  1073. $output = preg_replace("/".LD.'auto_path'.RD."/", $path, $output, sizeof(explode(LD.'auto_path'.RD, $TMPL->tagdata)) - 1);
  1074. $output = preg_replace("/".LD.'id_auto_path'.RD."/", $idpath, $output, sizeof(explode(LD.'id_auto_path'.RD, $TMPL->tagdata)) - 1);
  1075. $output = preg_replace("/".LD.'excerpt'.RD."/", preg_quote($excerpt), $output, sizeof(explode(LD.'excerpt'.RD, $TMPL->tagdata)) - 1);
  1076. $output = preg_replace("/".LD.'full_text'.RD."/", preg_quote($full_text), $output, sizeof(explode(LD.'full_text'.RD, $TMPL->tagdata)) - 1);
  1077. // Parse member_path
  1078. if (count($m_paths) > 0)
  1079. {
  1080. foreach ($m_paths as $val)
  1081. {
  1082. $output = preg_replace("/".$val['0']."/", $FNS->create_url($val['1'].'/'.$row['member_id']), $output, 1);
  1083. }
  1084. }
  1085. }
  1086. $TMPL->tagdata = $output;
  1087. /** ----------------------------------------
  1088. /** Parse variables
  1089. /** ----------------------------------------*/
  1090. $swap = array(
  1091. 'lang:total_search_results' => $LANG->line('search_total_results'),
  1092. 'lang:search_engine' => $LANG->line('search_engine'),
  1093. 'lang:search_results' => $LANG->line('search_results'),
  1094. 'lang:search' => $LANG->line('search'),
  1095. 'lang:title' => $LANG->line('search_title'),
  1096. 'lang:weblog' => $LANG->line('search_weblog'),
  1097. 'lang:excerpt' => $LANG->line('search_excerpt'),
  1098. 'lang:author' => $LANG->line('search_author'),
  1099. 'lang:date' => $LANG->line('search_date'),
  1100. 'lang:total_comments' => $LANG->line('search_total_comments'),
  1101. 'lang:recent_comments' => $LANG->line('search_recent_comment_date'),
  1102. 'lang:keywords' => $LANG->line('search_keywords')
  1103. );
  1104. $TMPL->template = $FNS->var_swap($TMPL->template, $swap);
  1105. /** ----------------------------------------
  1106. /** Add Pagination
  1107. /** ----------------------------------------*/
  1108. if ($pager == '')
  1109. {
  1110. $TMPL->template = preg_replace("/".LD."if paginate".RD.".*?".LD."&#47;if".RD."/s", '', $TMPL->template);
  1111. }
  1112. else
  1113. {
  1114. $TMPL->template = preg_replace("/".LD."if paginate".RD."(.*?)".LD."&#47;if".RD."/s", "\\1", $TMPL->template);
  1115. }
  1116. $TMPL->template = str_replace(LD.'paginate'.RD, $pager, $TMPL->template);
  1117. $TMPL->template = str_replace(LD.'page_count'.RD, $page_count, $TMPL->template);
  1118. return stripslashes($TMPL->tagdata);
  1119. }
  1120. /* END */
  1121. /** ----------------------------------------
  1122. /** Simple Search Form
  1123. /** ----------------------------------------*/
  1124. function simple_form()
  1125. {
  1126. global $IN, $FNS, $PREFS, $TMPL, $DB, $LANG;
  1127. /** ----------------------------------------
  1128. /** Create form
  1129. /** ----------------------------------------*/
  1130. $result_page = ( ! $TMPL->fetch_param('result_page')) ? 'search/results' : $TMPL->fetch_param('result_page');
  1131. $data['hidden_fields'] = array(
  1132. 'ACT' => $FNS->fetch_action_id('Search', 'do_search'),
  1133. 'XID' => '',
  1134. 'RP' => $result_page,
  1135. 'NRP' => ($TMPL->fetch_param('no_result_page')) ? $TMPL->fetch_param('no_result_page') : '',
  1136. 'RES' => $TMPL->fetch_param('results'),
  1137. 'status' => $TMPL->fetch_param('status'),
  1138. 'weblog' => $TMPL->fetch_param('weblog'),
  1139. 'search_in' => $TMPL->fetch_param('search_in'),
  1140. 'where' => ( ! $TMPL->fetch_param('where')) ? 'all' : $TMPL->fetch_param('where')
  1141. );
  1142. if ($TMPL->fetch_param('show_expired') !== FALSE)
  1143. {
  1144. $data['hidden_fields']['show_expired'] = $TMPL->fetch_param('show_expired');
  1145. }
  1146. if ($TMPL->fetch_param('show_future_entries') !== FALSE)
  1147. {
  1148. $data['hidden_fields']['show_future_entries'] = $TMPL->fetch_param('show_future_entries');
  1149. }
  1150. if ($TMPL->fetch_param('name') !== FALSE &&
  1151. preg_match("#^[a-zA-Z0-9_\-]+$#i", $TMPL->fetch_param('name')))
  1152. {
  1153. $data['name'] = $TMPL->fetch_param('name');
  1154. }
  1155. if ($TMPL->fetch_param('id') !== FALSE &&
  1156. preg_match("#^[a-zA-Z0-9_\-]+$#i", $TMPL->fetch_param('id')))
  1157. {
  1158. $data['id'] = $TMPL->fetch_param('id');
  1159. }
  1160. $res = $FNS->form_declaration($data);
  1161. $res .= stripslashes($TMPL->tagdata);
  1162. $res .= "</form>";
  1163. return $res;
  1164. }
  1165. /* END */
  1166. /** ----------------------------------------
  1167. /** Advanced Search Form
  1168. /** ----------------------------------------*/
  1169. function advanced_form()
  1170. {
  1171. global $IN, $FNS, $PREFS, $TMPL, $DB, $LANG, $REGX;
  1172. $LANG->fetch_language_file('search');
  1173. /** ----------------------------------------
  1174. /** Fetch weblogs and categories
  1175. /** ----------------------------------------*/
  1176. // First we need to grab the name/ID number of all weblogs and categories
  1177. $sql = "SELECT blog_title, weblog_id, cat_group FROM exp_weblogs WHERE ";
  1178. if (USER_BLOG !== FALSE)
  1179. {
  1180. // If it's a "user blog" we limit to only their assigned blog
  1181. $sql .= "exp_weblogs.weblog_id = '".UB_BLOG_ID."' ";
  1182. }
  1183. else
  1184. {
  1185. $sql .= "exp_weblogs.is_user_blog = 'n' AND site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' ";
  1186. if ($weblog = $TMPL->fetch_param('weblog'))
  1187. {
  1188. $xql = "SELECT weblog_id FROM exp_weblogs WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' ";
  1189. $xql .= $FNS->sql_andor_string($weblog, 'blog_name');
  1190. $query = $DB->query($xql);
  1191. if ($query->num_rows > 0)
  1192. {
  1193. if ($query->num_rows == 1)
  1194. {
  1195. $sql .= "AND weblog_id = '".$query->row['weblog_id']."' ";
  1196. }
  1197. else
  1198. {
  1199. $sql .= "AND (";
  1200. foreach ($query->result as $row)
  1201. {
  1202. $sql .= "weblog_id = '".$row['weblog_id']."' OR ";
  1203. }
  1204. $sql = substr($sql, 0, - 3);
  1205. $sql .= ") ";
  1206. }
  1207. }
  1208. }
  1209. }
  1210. $sql .= " ORDER BY blog_title";
  1211. $query = $DB->query($sql);
  1212. foreach ($query->result as $row)
  1213. {
  1214. $this->blog_array[$ro

Large files files are truncated, but you can click here to view the full file