[ Index ]

PHP Cross Reference of Nuke-Evolution v2.0.5

title

Body

[close]

/includes/ -> functions_search.php (source)

   1  <?php
   2  /*=======================================================================
   3   Nuke-Evolution Basic: Enhanced PHP-Nuke Web Portal System
   4   =======================================================================*/
   5  
   6  /***************************************************************************
   7  *                              functions_search.php
   8  *                              -------------------
   9  *     begin                : Wed Sep 05 2001
  10  *     copyright            : (C) 2002 The phpBB Group
  11  *     email                : support@phpbb.com
  12  *
  13  *     Id: functions_search.php,v 1.8.2.19 2004/11/18 17:49:45 acydburn Exp
  14  *
  15  ****************************************************************************/
  16  
  17  /***************************************************************************
  18   *
  19   *   This program is free software; you can redistribute it and/or modify
  20   *   it under the terms of the GNU General Public License as published by
  21   *   the Free Software Foundation; either version 2 of the License, or
  22   *   (at your option) any later version.
  23   *
  24   ***************************************************************************/
  25  
  26  if (!defined('IN_PHPBB'))
  27  {
  28      die('Hacking attempt');
  29  }
  30  
  31  function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)
  32  {
  33          static $drop_char_match =   array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '-', '~', '+', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!');
  34          static $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', '',  ' ',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' , ' ', ' ', ' ', ' ',  ' ', ' ');
  35  
  36          $entry = ' ' . strip_tags(strtolower($entry)) . ' ';
  37  
  38          if ( $mode == 'post' )
  39          {
  40                  // Replace line endings by a space
  41                  $entry = preg_replace('/[\n\r]/is', ' ', $entry);
  42                  // HTML entities like &nbsp;
  43                  $entry = preg_replace('/\b&[a-z]+;\b/', ' ', $entry);
  44                  // Remove URL's
  45                  $entry = preg_replace('/\b[a-z0-9]+:\/\/[a-z0-9\.\-]+(\/[a-z0-9\?\.%_\-\+=&\/]+)?/', ' ', $entry);
  46                  // Quickly remove BBcode.
  47                  $entry = preg_replace('/\[img:[a-z0-9]{10,}\].*?\[\/img:[a-z0-9]{10,}\]/', ' ', $entry);
  48                  $entry = preg_replace('/\[\/?url(=.*?)?\]/', ' ', $entry);
  49                  $entry = preg_replace('/\[\/?[a-z\*=\+\-]+(\:?[0-9a-z]+)?:[a-z0-9]{10,}(\:[a-z0-9]+)?=?.*?\]/', ' ', $entry);
  50          }
  51          else if ( $mode == 'search' )
  52          {
  53                  $entry = str_replace(' +', ' and ', $entry);
  54                  $entry = str_replace(' -', ' not ', $entry);
  55          }
  56  
  57          //
  58          // Filter out strange characters like ^, $, &, change "it's" to "its"
  59          //
  60          for($i = 0; $i < count($drop_char_match); $i++)
  61          {
  62                  $entry =  str_replace($drop_char_match[$i], $drop_char_replace[$i], $entry);
  63          }
  64  
  65          if ( $mode == 'post' )
  66          {
  67                  $entry = str_replace('*', ' ', $entry);
  68  
  69                  // 'words' that consist of <3 or >20 characters are removed.
  70          $entry = preg_replace('/[ ]([\S]{1,2}|[\S]{21,})[ ]/',' ', $entry);
  71          }
  72  
  73          if ( !empty($stopword_list) )
  74          {
  75                  for ($j = 0; $j < count($stopword_list); $j++)
  76                  {
  77                          $stopword = trim($stopword_list[$j]);
  78  
  79                          if ( $mode == 'post' || ( $stopword != 'not' && $stopword != 'and' && $stopword != 'or' ) )
  80                          {
  81                                  $entry = str_replace(' ' . trim($stopword) . ' ', ' ', $entry);
  82                          }
  83                  }
  84          }
  85  
  86          if ( !empty($synonym_list) )
  87          {
  88                  for ($j = 0; $j < count($synonym_list); $j++)
  89                  {
  90                          list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_list[$j])));
  91                          if ( $mode == 'post' || ( $match_synonym != 'not' && $match_synonym != 'and' && $match_synonym != 'or' ) )
  92                          {
  93                                  $entry =  str_replace(' ' . trim($match_synonym) . ' ', ' ' . trim($replace_synonym) . ' ', $entry);
  94                          }
  95                  }
  96          }
  97  
  98          return $entry;
  99  }
 100  
 101  function split_words($entry, $mode = 'post')
 102  {
 103          // If you experience problems with the new method, uncomment this block.
 104  /*
 105          $rex = ( $mode == 'post' ) ? "/\b([\w±µ-ÿ][\w±µ-ÿ']*[\w±µ-ÿ]+|[\w±µ-ÿ]+?)\b/" : '/(\*?[a-z0-9±µ-ÿ]+\*?)|\b([a-z0-9±µ-ÿ]+)\b/';
 106          preg_match_all($rex, $entry, $split_entries);
 107  
 108          return $split_entries[1];
 109  */
 110      // Trim 1+ spaces to one space and split this trimmed string into words.
 111      return explode(' ', trim(preg_replace('#\s+#', ' ', $entry)));
 112  }
 113  
 114  function add_search_words($mode, $post_id, $post_text, $post_title = '')
 115  {
 116          global $db, $phpbb_root_path, $board_config, $lang;
 117  
 118          $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . "/search_stopwords.txt");
 119          $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . "/search_synonyms.txt");
 120  
 121          $search_raw_words = array();
 122          $search_raw_words['text'] = split_words(clean_words('post', $post_text, $stopword_array, $synonym_array));
 123          $search_raw_words['title'] = split_words(clean_words('post', $post_title, $stopword_array, $synonym_array));
 124          @set_time_limit(0);
 125          $word = array();
 126          $word_insert_sql = array();
 127          while ( list($word_in, $search_matches) = @each($search_raw_words) )
 128          {
 129                  $word_insert_sql[$word_in] = '';
 130                  if ( !empty($search_matches) )
 131                  {
 132                          for ($i = 0; $i < count($search_matches); $i++)
 133                          {
 134                                  $search_matches[$i] = trim($search_matches[$i]);
 135  
 136                                  if( $search_matches[$i] != '' )
 137                                  {
 138                                          $word[] = $search_matches[$i];
 139                                          if ( !strstr($word_insert_sql[$word_in], "'" . $search_matches[$i] . "'") )
 140                                          {
 141                                                  $word_insert_sql[$word_in] .= ( $word_insert_sql[$word_in] != "" ) ? ", '" . $search_matches[$i] . "'" : "'" . $search_matches[$i] . "'";
 142                                          }
 143                                  }
 144                          }
 145                  }
 146          }
 147  
 148          if ( count($word) )
 149          {
 150                  sort($word);
 151  
 152                  $prev_word = '';
 153                  $word_text_sql = '';
 154                  $temp_word = array();
 155                  for($i = 0; $i < count($word); $i++)
 156                  {
 157                          if ( $word[$i] != $prev_word )
 158                          {
 159                                  $temp_word[] = $word[$i];
 160                                  $word_text_sql .= ( ( $word_text_sql != '' ) ? ', ' : '' ) . "'" . $word[$i] . "'";
 161                          }
 162                          $prev_word = $word[$i];
 163                  }
 164                  $word = $temp_word;
 165  
 166                  $check_words = array();
 167                  switch( SQL_LAYER )
 168                  {
 169                          case 'postgresql':
 170                          case 'msaccess':
 171                          case 'mssql-odbc':
 172                          case 'oracle':
 173                          case 'db2':
 174                                  $sql = "SELECT word_id, word_text
 175                                          FROM " . SEARCH_WORD_TABLE . "
 176                                          WHERE word_text IN ($word_text_sql)";
 177                                  if ( !($result = $db->sql_query($sql)) )
 178                                  {
 179                                          message_die(GENERAL_ERROR, 'Could not select words', '', __LINE__, __FILE__, $sql);
 180                                  }
 181  
 182                                  while ( $row = $db->sql_fetchrow($result) )
 183                                  {
 184                                          $check_words[$row['word_text']] = $row['word_id'];
 185                                  }
 186                                  break;
 187                  }
 188  
 189                  $value_sql = '';
 190                  $match_word = array();
 191                  for ($i = 0; $i < count($word); $i++)
 192                  {
 193                          $new_match = true;
 194                          if ( isset($check_words[$word[$i]]) )
 195                          {
 196                                  $new_match = false;
 197                          }
 198  
 199                          if ( $new_match )
 200                          {
 201                                  switch( SQL_LAYER )
 202                                  {
 203                                          case 'mysql':
 204                                          case 'mysql4':
 205                                          case 'mysqli':
 206                                                  $value_sql .= ( ( $value_sql != '' ) ? ', ' : '' ) . '(\'' . $word[$i] . '\', 0)';
 207                                                  break;
 208                                          case 'mssql':
 209                                          case 'mssql-odbc':
 210                                                  $value_sql .= ( ( $value_sql != '' ) ? ' UNION ALL ' : '' ) . "SELECT '" . $word[$i] . "', 0";
 211                                                  break;
 212                                          default:
 213                                                  $sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
 214                                                          VALUES ('" . $word[$i] . "', '0')";
 215                                                  if( !$db->sql_query($sql) )
 216                                                  {
 217                                                          message_die(GENERAL_ERROR, 'Could not insert new word', '', __LINE__, __FILE__, $sql);
 218                                                  }
 219                                                  break;
 220                                  }
 221                          }
 222                  }
 223  
 224                  if ( $value_sql != '' )
 225                  {
 226                          switch ( SQL_LAYER )
 227                          {
 228                                  case 'mysql':
 229                                  case 'mysql4':
 230                                  case 'mysqli':
 231                                          $sql = "INSERT IGNORE INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
 232                                                  VALUES $value_sql";
 233                                          break;
 234                                  case 'mssql':
 235                                  case 'mssql-odbc':
 236                                          $sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
 237                                                  $value_sql";
 238                                          break;
 239                          }
 240  
 241                          if ( !$db->sql_query($sql) )
 242                          {
 243                                  message_die(GENERAL_ERROR, 'Could not insert new word', '', __LINE__, __FILE__, $sql);
 244                          }
 245                  }
 246          }
 247  
 248          while( list($word_in, $match_sql) = @each($word_insert_sql) )
 249          {
 250                  $title_match = ( $word_in == 'title' ) ? 1 : 0;
 251  
 252                  if ( $match_sql != '' )
 253                  {
 254                          $sql = "INSERT INTO " . SEARCH_MATCH_TABLE . " (post_id, word_id, title_match)
 255                                  SELECT $post_id, word_id, $title_match
 256                                          FROM " . SEARCH_WORD_TABLE . "
 257                                          WHERE word_text IN ($match_sql)";
 258                          if ( !$db->sql_query($sql) )
 259                          {
 260                                  message_die(GENERAL_ERROR, 'Could not insert new word matches', '', __LINE__, __FILE__, $sql);
 261                          }
 262                  }
 263          }
 264  
 265          if ($mode == 'single')
 266          {
 267                  remove_common('single', 4/10, $word);
 268          }
 269  
 270          return;
 271  }
 272  
 273  //
 274  // Check if specified words are too common now
 275  //
 276  function remove_common($mode, $fraction, $word_id_list = array())
 277  {
 278          global $db;
 279  
 280          $sql = "SELECT COUNT(post_id) AS total_posts
 281                  FROM " . POSTS_TABLE;
 282          if ( !($result = $db->sql_query($sql)) )
 283          {
 284                  message_die(GENERAL_ERROR, 'Could not obtain post count', '', __LINE__, __FILE__, $sql);
 285          }
 286  
 287          $row = $db->sql_fetchrow($result);
 288  
 289          if ( $row['total_posts'] >= 100 )
 290          {
 291                  $common_threshold = floor($row['total_posts'] * $fraction);
 292  
 293                  if ( $mode == 'single' && count($word_id_list) )
 294                  {
 295                          $word_id_sql = '';
 296                          for($i = 0; $i < count($word_id_list); $i++)
 297                          {
 298                                  $word_id_sql .= ( ( $word_id_sql != '' ) ? ', ' : '' ) . "'" . $word_id_list[$i] . "'";
 299                          }
 300  
 301                          $sql = "SELECT m.word_id
 302                                  FROM " . SEARCH_MATCH_TABLE . " m, " . SEARCH_WORD_TABLE . " w
 303                                  WHERE w.word_text IN ($word_id_sql)
 304                                          AND m.word_id = w.word_id
 305                                  GROUP BY m.word_id
 306                                  HAVING COUNT(m.word_id) > $common_threshold";
 307                  }
 308                  else
 309                  {
 310                          $sql = "SELECT word_id
 311                                  FROM " . SEARCH_MATCH_TABLE . "
 312                                  GROUP BY word_id
 313                                  HAVING COUNT(word_id) > $common_threshold";
 314                  }
 315  
 316                  if ( !($result = $db->sql_query($sql)) )
 317                  {
 318                          message_die(GENERAL_ERROR, 'Could not obtain common word list', '', __LINE__, __FILE__, $sql);
 319                  }
 320  
 321                  $common_word_id = '';
 322                  while ( $row = $db->sql_fetchrow($result) )
 323                  {
 324                          $common_word_id .= ( ( $common_word_id != '' ) ? ', ' : '' ) . $row['word_id'];
 325                  }
 326                  $db->sql_freeresult($result);
 327  
 328                  if ( $common_word_id != '' )
 329                  {
 330                          $sql = "UPDATE " . SEARCH_WORD_TABLE . "
 331                                  SET word_common = " . TRUE . "
 332                                  WHERE word_id IN ($common_word_id)";
 333                          if ( !$db->sql_query($sql) )
 334                          {
 335                                  message_die(GENERAL_ERROR, 'Could not delete word list entry', '', __LINE__, __FILE__, $sql);
 336                          }
 337  
 338                          $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . "
 339                                  WHERE word_id IN ($common_word_id)";
 340                          if ( !$db->sql_query($sql) )
 341                          {
 342                                  message_die(GENERAL_ERROR, 'Could not delete word match entry', '', __LINE__, __FILE__, $sql);
 343                          }
 344                  }
 345          }
 346  
 347          return;
 348  }
 349  
 350  function remove_search_post($post_id_sql)
 351  {
 352          global $db;
 353  
 354          $words_removed = false;
 355  
 356          switch ( SQL_LAYER )
 357          {
 358                  case 'mysql':
 359                  case 'mysql4':
 360                  case 'mysqli':
 361                          $sql = "SELECT word_id
 362                                  FROM " . SEARCH_MATCH_TABLE . "
 363                                  WHERE post_id IN ($post_id_sql)
 364                                  GROUP BY word_id";
 365                          if ( $result = $db->sql_query($sql) )
 366                          {
 367                                  $word_id_sql = '';
 368                                  while ( $row = $db->sql_fetchrow($result) )
 369                                  {
 370                                          $word_id_sql .= ( $word_id_sql != '' ) ? ', ' . $row['word_id'] : $row['word_id'];
 371                                  }
 372  
 373                                  $sql = "SELECT word_id
 374                                          FROM " . SEARCH_MATCH_TABLE . "
 375                                          WHERE word_id IN ($word_id_sql)
 376                                          GROUP BY word_id
 377                                          HAVING COUNT(word_id) = 1";
 378                                  if ( $result = $db->sql_query($sql) )
 379                                  {
 380                                          $word_id_sql = '';
 381                                          while ( $row = $db->sql_fetchrow($result) )
 382                                          {
 383                                                  $word_id_sql .= ( $word_id_sql != '' ) ? ', ' . $row['word_id'] : $row['word_id'];
 384                                          }
 385  
 386                                          if ( $word_id_sql != '' )
 387                                          {
 388                                                  $sql = "DELETE FROM " . SEARCH_WORD_TABLE . "
 389                                                          WHERE word_id IN ($word_id_sql)";
 390                                                  if ( !$db->sql_query($sql) )
 391                                                  {
 392                                                          message_die(GENERAL_ERROR, 'Could not delete word list entry', '', __LINE__, __FILE__, $sql);
 393                                                  }
 394  
 395                                                  $words_removed = $db->sql_affectedrows();
 396                                          }
 397                                  }
 398                          }
 399                          break;
 400  
 401                  default:
 402                          $sql = "DELETE FROM " . SEARCH_WORD_TABLE . "
 403                                  WHERE word_id IN (
 404                                          SELECT word_id
 405                                          FROM " . SEARCH_MATCH_TABLE . "
 406                                          WHERE word_id IN (
 407                                                  SELECT word_id
 408                                                  FROM " . SEARCH_MATCH_TABLE . "
 409                                                  WHERE post_id IN ($post_id_sql)
 410                                                  GROUP BY word_id
 411                                          )
 412                                          GROUP BY word_id
 413                                          HAVING COUNT(word_id) = 1
 414                                  )";
 415                          if ( !$db->sql_query($sql) )
 416                          {
 417                                  message_die(GENERAL_ERROR, 'Could not delete old words from word table', '', __LINE__, __FILE__, $sql);
 418                          }
 419  
 420                          $words_removed = $db->sql_affectedrows();
 421  
 422                          break;
 423          }
 424  
 425          $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . "
 426                  WHERE post_id IN ($post_id_sql)";
 427          if ( !$db->sql_query($sql) )
 428          {
 429                  message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
 430          }
 431  
 432          return $words_removed;
 433  }
 434  
 435  //
 436  // Username search
 437  //
 438  function username_search($search_match)
 439  {
 440          global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path, $starttime, $gen_simple_header;
 441  
 442          $gen_simple_header = TRUE;
 443  
 444          $username_list = '';
 445          if ( !empty($search_match) )
 446          {
 447          $username_search = preg_replace('/\*/', '%', phpbb_clean_username($search_match));
 448  
 449                  $sql = "SELECT username
 450                          FROM " . USERS_TABLE . "
 451                          WHERE username LIKE '" . str_replace("\'", "''", $username_search) . "' AND user_id <> " . ANONYMOUS . "
 452                          ORDER BY username";
 453                  if ( !($result = $db->sql_query($sql)) )
 454                  {
 455                          message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
 456                  }
 457  
 458                  if ( $row = $db->sql_fetchrow($result) )
 459                  {
 460                          do
 461                          {
 462                                  $username_list .= '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
 463                          }
 464                          while ( $row = $db->sql_fetchrow($result) );
 465                  }
 466                  else
 467                  {
 468                          $username_list .= '<option>' . $lang['No_match']. '</option>';
 469                  }
 470                  $db->sql_freeresult($result);
 471          }
 472  
 473          $page_title = $lang['Search'];
 474          include ("includes/page_header_review.php");
 475  
 476          $template->set_filenames(array(
 477                  'search_user_body' => 'search_username.tpl')
 478          );
 479  
 480          $template->assign_vars(array(
 481                  'USERNAME' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '',
 482  
 483                  'L_CLOSE_WINDOW' => $lang['Close_window'],
 484                  'L_SEARCH_USERNAME' => $lang['Find_username'],
 485                  'L_UPDATE_USERNAME' => $lang['Select_username'],
 486                  'L_SELECT' => $lang['Select'],
 487                  'L_SEARCH' => $lang['Search'],
 488                  'L_SEARCH_EXPLAIN' => $lang['Search_author_explain'],
 489                  'L_CLOSE_WINDOW' => $lang['Close_window'],
 490  
 491                  'S_USERNAME_OPTIONS' => $username_list,
 492                  'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser&popup=1"))
 493          );
 494  
 495          if ( $username_list != '' )
 496          {
 497                  $template->assign_block_vars('switch_select_name', array());
 498          }
 499  
 500          $template->pparse('search_user_body');
 501  
 502          include ("includes/page_tail_review.php");
 503  
 504          return;
 505  }
 506  
 507  ?>