[ Index ]
 

Code source de PHP NUKE 7.9

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/includes/ -> functions_search.php (source)

   1  <?php
   2  /***************************************************************************

   3  *                              functions_search.php

   4  *                              -------------------

   5  *     begin                : Wed Sep 05 2001

   6  *     copyright            : (C) 2002 The phpBB Group

   7  *     email                : support@phpbb.com

   8  *

   9  *     Id: functions_search.php,v 1.8.2.19 2004/11/18 17:49:45 acydburn Exp

  10  *

  11  ****************************************************************************/
  12  
  13  /***************************************************************************

  14   *

  15   *   This program is free software; you can redistribute it and/or modify

  16   *   it under the terms of the GNU General Public License as published by

  17   *   the Free Software Foundation; either version 2 of the License, or

  18   *   (at your option) any later version.

  19   *

  20   ***************************************************************************/
  21  
  22  if (!defined('IN_PHPBB')) {
  23      die();
  24  }
  25  function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)
  26  {
  27          static $drop_char_match =   array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '-', '~', '+', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!');
  28          static $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', '',  ' ',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' , ' ', ' ', ' ', ' ',  ' ', ' ');
  29  
  30          $entry = ' ' . strip_tags(strtolower($entry)) . ' ';
  31  
  32          if ( $mode == 'post' )
  33          {
  34                  // Replace line endings by a space

  35                  $entry = preg_replace('/[\n\r]/is', ' ', $entry);
  36                  // HTML entities like &nbsp;

  37                  $entry = preg_replace('/\b&[a-z]+;\b/', ' ', $entry);
  38                  // Remove URL's

  39                  $entry = preg_replace('/\b[a-z0-9]+:\/\/[a-z0-9\.\-]+(\/[a-z0-9\?\.%_\-\+=&\/]+)?/', ' ', $entry);
  40                  // Quickly remove BBcode.

  41                  $entry = preg_replace('/\[img:[a-z0-9]{10,}\].*?\[\/img:[a-z0-9]{10,}\]/', ' ', $entry);
  42                  $entry = preg_replace('/\[\/?url(=.*?)?\]/', ' ', $entry);
  43                  $entry = preg_replace('/\[\/?[a-z\*=\+\-]+(\:?[0-9a-z]+)?:[a-z0-9]{10,}(\:[a-z0-9]+)?=?.*?\]/', ' ', $entry);
  44          }
  45          else if ( $mode == 'search' )
  46          {
  47                  $entry = str_replace(' +', ' and ', $entry);
  48                  $entry = str_replace(' -', ' not ', $entry);
  49          }
  50  
  51          //

  52          // Filter out strange characters like ^, $, &, change "it's" to "its"

  53          //

  54          for($i = 0; $i < count($drop_char_match); $i++)
  55          {
  56                  $entry =  str_replace($drop_char_match[$i], $drop_char_replace[$i], $entry);
  57          }
  58  
  59          if ( $mode == 'post' )
  60          {
  61                  $entry = str_replace('*', ' ', $entry);
  62  
  63                  // 'words' that consist of <3 or >20 characters are removed.

  64          $entry = preg_replace('/[ ]([\S]{1,2}|[\S]{21,})[ ]/',' ', $entry);
  65          }
  66  
  67          if ( !empty($stopword_list) )
  68          {
  69                  for ($j = 0; $j < count($stopword_list); $j++)
  70                  {
  71                          $stopword = trim($stopword_list[$j]);
  72  
  73                          if ( $mode == 'post' || ( $stopword != 'not' && $stopword != 'and' && $stopword != 'or' ) )
  74                          {
  75                                  $entry = str_replace(' ' . trim($stopword) . ' ', ' ', $entry);
  76                          }
  77                  }
  78          }
  79  
  80          if ( !empty($synonym_list) )
  81          {
  82                  for ($j = 0; $j < count($synonym_list); $j++)
  83                  {
  84                          list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_list[$j])));
  85                          if ( $mode == 'post' || ( $match_synonym != 'not' && $match_synonym != 'and' && $match_synonym != 'or' ) )
  86                          {
  87                                  $entry =  str_replace(' ' . trim($match_synonym) . ' ', ' ' . trim($replace_synonym) . ' ', $entry);
  88                          }
  89                  }
  90          }
  91  
  92          return $entry;
  93  }
  94  
  95  function split_words(&$entry, $mode = 'post')
  96  {
  97          // If you experience problems with the new method, uncomment this block.

  98  /*

  99          $rex = ( $mode == 'post' ) ? "/\b([\w±µ-ÿ][\w±µ-ÿ']*[\w±µ-ÿ]+|[\w±µ-ÿ]+?)\b/" : '/(\*?[a-z0-9±µ-ÿ]+\*?)|\b([a-z0-9±µ-ÿ]+)\b/';

 100          preg_match_all($rex, $entry, $split_entries);

 101  

 102          return $split_entries[1];

 103  */
 104      // Trim 1+ spaces to one space and split this trimmed string into words.

 105      return explode(' ', trim(preg_replace('#\s+#', ' ', $entry)));
 106  }
 107  
 108  function add_search_words($mode, $post_id, $post_text, $post_title = '')
 109  {
 110          global $db, $phpbb_root_path, $board_config, $lang;
 111  
 112          $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . "/search_stopwords.txt");
 113          $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . "/search_synonyms.txt");
 114  
 115          $search_raw_words = array();
 116          $search_raw_words['text'] = split_words(clean_words('post', $post_text, $stopword_array, $synonym_array));
 117          $search_raw_words['title'] = split_words(clean_words('post', $post_title, $stopword_array, $synonym_array));
 118      @set_time_limit(0);
 119          $word = array();
 120          $word_insert_sql = array();
 121          while ( list($word_in, $search_matches) = @each($search_raw_words) )
 122          {
 123                  $word_insert_sql[$word_in] = '';
 124                  if ( !empty($search_matches) )
 125                  {
 126                          for ($i = 0; $i < count($search_matches); $i++)
 127                          {
 128                                  $search_matches[$i] = trim($search_matches[$i]);
 129  
 130                                  if( $search_matches[$i] != '' )
 131                                  {
 132                                          $word[] = $search_matches[$i];
 133                                          if ( !strstr($word_insert_sql[$word_in], "'" . $search_matches[$i] . "'") )
 134                                          {
 135                                                  $word_insert_sql[$word_in] .= ( $word_insert_sql[$word_in] != "" ) ? ", '" . $search_matches[$i] . "'" : "'" . $search_matches[$i] . "'";
 136                                          }
 137                                  }
 138                          }
 139                  }
 140          }
 141  
 142          if ( count($word) )
 143          {
 144                  sort($word);
 145  
 146                  $prev_word = '';
 147                  $word_text_sql = '';
 148                  $temp_word = array();
 149                  for($i = 0; $i < count($word); $i++)
 150                  {
 151                          if ( $word[$i] != $prev_word )
 152                          {
 153                                  $temp_word[] = $word[$i];
 154                                  $word_text_sql .= ( ( $word_text_sql != '' ) ? ', ' : '' ) . "'" . $word[$i] . "'";
 155                          }
 156                          $prev_word = $word[$i];
 157                  }
 158                  $word = $temp_word;
 159  
 160                  $check_words = array();
 161                  switch( SQL_LAYER )
 162                  {
 163                          case 'postgresql':
 164                          case 'msaccess':
 165                          case 'mssql-odbc':
 166                          case 'oracle':
 167                          case 'db2':
 168                                  $sql = "SELECT word_id, word_text
 169                                          FROM " . SEARCH_WORD_TABLE . "
 170                                          WHERE word_text IN ($word_text_sql)";
 171                                  if ( !($result = $db->sql_query($sql)) )
 172                                  {
 173                                          message_die(GENERAL_ERROR, 'Could not select words', '', __LINE__, __FILE__, $sql);
 174                                  }
 175  
 176                                  while ( $row = $db->sql_fetchrow($result) )
 177                                  {
 178                                          $check_words[$row['word_text']] = $row['word_id'];
 179                                  }
 180                                  break;
 181                  }
 182  
 183                  $value_sql = '';
 184                  $match_word = array();
 185                  for ($i = 0; $i < count($word); $i++)
 186                  {
 187                          $new_match = true;
 188                          if ( isset($check_words[$word[$i]]) )
 189                          {
 190                                  $new_match = false;
 191                          }
 192  
 193                          if ( $new_match )
 194                          {
 195                                  switch( SQL_LAYER )
 196                                  {
 197                                          case 'mysql':
 198                                          case 'mysql4':
 199                                                  $value_sql .= ( ( $value_sql != '' ) ? ', ' : '' ) . '(\'' . $word[$i] . '\', 0)';
 200                                                  break;
 201                                          case 'mssql':
 202                                          case 'mssql-odbc':
 203                                                  $value_sql .= ( ( $value_sql != '' ) ? ' UNION ALL ' : '' ) . "SELECT '" . $word[$i] . "', 0";
 204                                                  break;
 205                                          default:
 206                                                  $sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
 207                                                          VALUES ('" . $word[$i] . "', '0')";
 208                                                  if( !$db->sql_query($sql) )
 209                                                  {
 210                                                          message_die(GENERAL_ERROR, 'Could not insert new word', '', __LINE__, __FILE__, $sql);
 211                                                  }
 212                                                  break;
 213                                  }
 214                          }
 215                  }
 216  
 217                  if ( $value_sql != '' )
 218                  {
 219                          switch ( SQL_LAYER )
 220                          {
 221                                  case 'mysql':
 222                                  case 'mysql4':
 223                                          $sql = "INSERT IGNORE INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
 224                                                  VALUES $value_sql";
 225                                          break;
 226                                  case 'mssql':
 227                                  case 'mssql-odbc':
 228                                          $sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
 229                                                  $value_sql";
 230                                          break;
 231                          }
 232  
 233                          if ( !$db->sql_query($sql) )
 234                          {
 235                                  message_die(GENERAL_ERROR, 'Could not insert new word', '', __LINE__, __FILE__, $sql);
 236                          }
 237                  }
 238          }
 239  
 240          while( list($word_in, $match_sql) = @each($word_insert_sql) )
 241          {
 242                  $title_match = ( $word_in == 'title' ) ? 1 : 0;
 243  
 244                  if ( $match_sql != '' )
 245                  {
 246                          $sql = "INSERT INTO " . SEARCH_MATCH_TABLE . " (post_id, word_id, title_match)
 247                                  SELECT $post_id, word_id, $title_match
 248                                          FROM " . SEARCH_WORD_TABLE . "
 249                                          WHERE word_text IN ($match_sql)";
 250                          if ( !$db->sql_query($sql) )
 251                          {
 252                                  message_die(GENERAL_ERROR, 'Could not insert new word matches', '', __LINE__, __FILE__, $sql);
 253                          }
 254                  }
 255          }
 256  
 257          if ($mode == 'single')
 258          {
 259                  remove_common('single', 4/10, $word);
 260          }
 261  
 262          return;
 263  }
 264  
 265  //

 266  // Check if specified words are too common now

 267  //

 268  function remove_common($mode, $fraction, $word_id_list = array())
 269  {
 270          global $db;
 271  
 272          $sql = "SELECT COUNT(post_id) AS total_posts
 273                  FROM " . POSTS_TABLE;
 274          if ( !($result = $db->sql_query($sql)) )
 275          {
 276                  message_die(GENERAL_ERROR, 'Could not obtain post count', '', __LINE__, __FILE__, $sql);
 277          }
 278  
 279          $row = $db->sql_fetchrow($result);
 280  
 281          if ( $row['total_posts'] >= 100 )
 282          {
 283                  $common_threshold = floor($row['total_posts'] * $fraction);
 284  
 285                  if ( $mode == 'single' && count($word_id_list) )
 286                  {
 287                          $word_id_sql = '';
 288                          for($i = 0; $i < count($word_id_list); $i++)
 289                          {
 290                                  $word_id_sql .= ( ( $word_id_sql != '' ) ? ', ' : '' ) . "'" . $word_id_list[$i] . "'";
 291                          }
 292  
 293                          $sql = "SELECT m.word_id
 294                                  FROM " . SEARCH_MATCH_TABLE . " m, " . SEARCH_WORD_TABLE . " w
 295                                  WHERE w.word_text IN ($word_id_sql)
 296                                          AND m.word_id = w.word_id
 297                                  GROUP BY m.word_id
 298                                  HAVING COUNT(m.word_id) > $common_threshold";
 299                  }
 300                  else
 301                  {
 302                          $sql = "SELECT word_id
 303                                  FROM " . SEARCH_MATCH_TABLE . "
 304                                  GROUP BY word_id
 305                                  HAVING COUNT(word_id) > $common_threshold";
 306                  }
 307  
 308                  if ( !($result = $db->sql_query($sql)) )
 309                  {
 310                          message_die(GENERAL_ERROR, 'Could not obtain common word list', '', __LINE__, __FILE__, $sql);
 311                  }
 312  
 313                  $common_word_id = '';
 314                  while ( $row = $db->sql_fetchrow($result) )
 315                  {
 316                          $common_word_id .= ( ( $common_word_id != '' ) ? ', ' : '' ) . $row['word_id'];
 317                  }
 318                  $db->sql_freeresult($result);
 319  
 320                  if ( $common_word_id != '' )
 321                  {
 322                          $sql = "UPDATE " . SEARCH_WORD_TABLE . "
 323                                  SET word_common = " . TRUE . "
 324                                  WHERE word_id IN ($common_word_id)";
 325                          if ( !$db->sql_query($sql) )
 326                          {
 327                                  message_die(GENERAL_ERROR, 'Could not delete word list entry', '', __LINE__, __FILE__, $sql);
 328                          }
 329  
 330                          $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . "
 331                                  WHERE word_id IN ($common_word_id)";
 332                          if ( !$db->sql_query($sql) )
 333                          {
 334                                  message_die(GENERAL_ERROR, 'Could not delete word match entry', '', __LINE__, __FILE__, $sql);
 335                          }
 336                  }
 337          }
 338  
 339          return;
 340  }
 341  
 342  function remove_search_post($post_id_sql)
 343  {
 344          global $db;
 345  
 346          $words_removed = false;
 347  
 348          switch ( SQL_LAYER )
 349          {
 350                  case 'mysql':
 351                  case 'mysql4':
 352                          $sql = "SELECT word_id
 353                                  FROM " . SEARCH_MATCH_TABLE . "
 354                                  WHERE post_id IN ($post_id_sql)
 355                                  GROUP BY word_id";
 356                          if ( $result = $db->sql_query($sql) )
 357                          {
 358                                  $word_id_sql = '';
 359                                  while ( $row = $db->sql_fetchrow($result) )
 360                                  {
 361                                          $word_id_sql .= ( $word_id_sql != '' ) ? ', ' . $row['word_id'] : $row['word_id'];
 362                                  }
 363  
 364                                  $sql = "SELECT word_id
 365                                          FROM " . SEARCH_MATCH_TABLE . "
 366                                          WHERE word_id IN ($word_id_sql)
 367                                          GROUP BY word_id
 368                                          HAVING COUNT(word_id) = 1";
 369                                  if ( $result = $db->sql_query($sql) )
 370                                  {
 371                                          $word_id_sql = '';
 372                                          while ( $row = $db->sql_fetchrow($result) )
 373                                          {
 374                                                  $word_id_sql .= ( $word_id_sql != '' ) ? ', ' . $row['word_id'] : $row['word_id'];
 375                                          }
 376  
 377                                          if ( $word_id_sql != '' )
 378                                          {
 379                                                  $sql = "DELETE FROM " . SEARCH_WORD_TABLE . "
 380                                                          WHERE word_id IN ($word_id_sql)";
 381                                                  if ( !$db->sql_query($sql) )
 382                                                  {
 383                                                          message_die(GENERAL_ERROR, 'Could not delete word list entry', '', __LINE__, __FILE__, $sql);
 384                                                  }
 385  
 386                                                  $words_removed = $db->sql_affectedrows();
 387                                          }
 388                                  }
 389                          }
 390                          break;
 391  
 392                  default:
 393                          $sql = "DELETE FROM " . SEARCH_WORD_TABLE . "
 394                                  WHERE word_id IN (
 395                                          SELECT word_id
 396                                          FROM " . SEARCH_MATCH_TABLE . "
 397                                          WHERE word_id IN (
 398                                                  SELECT word_id
 399                                                  FROM " . SEARCH_MATCH_TABLE . "
 400                                                  WHERE post_id IN ($post_id_sql)
 401                                                  GROUP BY word_id
 402                                          )
 403                                          GROUP BY word_id
 404                                          HAVING COUNT(word_id) = 1
 405                                  )";
 406                          if ( !$db->sql_query($sql) )
 407                          {
 408                                  message_die(GENERAL_ERROR, 'Could not delete old words from word table', '', __LINE__, __FILE__, $sql);
 409                          }
 410  
 411                          $words_removed = $db->sql_affectedrows();
 412  
 413                          break;
 414          }
 415  
 416          $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . "
 417                  WHERE post_id IN ($post_id_sql)";
 418          if ( !$db->sql_query($sql) )
 419          {
 420                  message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
 421          }
 422  
 423          return $words_removed;
 424  }
 425  
 426  //

 427  // Username search

 428  //

 429  function username_search($search_match)
 430  {
 431          global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path, $starttime, $gen_simple_header;
 432  
 433          $gen_simple_header = TRUE;
 434  
 435          $username_list = '';
 436          if ( !empty($search_match) )
 437          {
 438          $username_search = preg_replace('/\*/', '%', phpbb_clean_username($search_match));
 439  
 440                  $sql = "SELECT username
 441                          FROM " . USERS_TABLE . "
 442                          WHERE username LIKE '" . str_replace("\'", "''", $username_search) . "' AND user_id <> " . ANONYMOUS . "
 443                          ORDER BY username";
 444                  if ( !($result = $db->sql_query($sql)) )
 445                  {
 446                          message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
 447                  }
 448  
 449                  if ( $row = $db->sql_fetchrow($result) )
 450                  {
 451                          do
 452                          {
 453                                  $username_list .= '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
 454                          }
 455                          while ( $row = $db->sql_fetchrow($result) );
 456                  }
 457                  else
 458                  {
 459                          $username_list .= '<option>' . $lang['No_match']. '</option>';
 460                  }
 461                  $db->sql_freeresult($result);
 462          }
 463  
 464          $page_title = $lang['Search'];
 465          include ("includes/page_header_review.php");
 466  
 467          $template->set_filenames(array(
 468                  'search_user_body' => 'search_username.tpl')
 469          );
 470  
 471          $template->assign_vars(array(
 472                  'USERNAME' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '',
 473  
 474                  'L_CLOSE_WINDOW' => $lang['Close_window'],
 475                  'L_SEARCH_USERNAME' => $lang['Find_username'],
 476                  'L_UPDATE_USERNAME' => $lang['Select_username'],
 477                  'L_SELECT' => $lang['Select'],
 478                  'L_SEARCH' => $lang['Search'],
 479                  'L_SEARCH_EXPLAIN' => $lang['Search_author_explain'],
 480                  'L_CLOSE_WINDOW' => $lang['Close_window'],
 481  
 482                  'S_USERNAME_OPTIONS' => $username_list,
 483                  'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser&popup=1"))
 484          );
 485  
 486          if ( $username_list != '' )
 487          {
 488                  $template->assign_block_vars('switch_select_name', array());
 489          }
 490  
 491          $template->pparse('search_user_body');
 492  
 493          include ("includes/page_tail_review.php");
 494  
 495          return;
 496  }
 497  
 498  ?>


Généré le : Sun Apr 1 11:11:59 2007 par Balluche grâce à PHPXref 0.7