punbb/include/search_idx.php
author Dan
Thu, 12 Jul 2007 01:04:01 -0400
changeset 2 a8a21e1c7afa
parent 0 f9ffdbd96607
permissions -rw-r--r--
Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     1
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     2
/***********************************************************************
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     3
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     4
  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     5
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     6
  This file is part of PunBB.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     7
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     8
  PunBB is free software; you can redistribute it and/or modify it
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     9
  under the terms of the GNU General Public License as published
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    10
  by the Free Software Foundation; either version 2 of the License,
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    11
  or (at your option) any later version.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    12
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    13
  PunBB is distributed in the hope that it will be useful, but
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    14
  WITHOUT ANY WARRANTY; without even the implied warranty of
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    16
  GNU General Public License for more details.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    17
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    18
  You should have received a copy of the GNU General Public License
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    19
  along with this program; if not, write to the Free Software
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    20
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    21
  MA  02111-1307  USA
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    22
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    23
************************************************************************/
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    24
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    25
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    26
// The contents of this file are very much inspired by the file functions_search.php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    27
// from the phpBB Group forum software phpBB2 (http://www.phpbb.com). 
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    28
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    29
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    30
// Make sure no one attempts to run this script "directly"
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    31
if (!defined('PUN'))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    32
	exit;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    33
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    34
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    35
//
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    36
// "Cleans up" a text string and returns an array of unique words
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    37
// This function depends on the current locale setting
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    38
//
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    39
function split_words($text)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    40
{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    41
	global $pun_user;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    42
	static $noise_match, $noise_replace, $stopwords;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    43
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    44
	if (empty($noise_match))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    45
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    46
		$noise_match = 		array('[quote', '[code', '[url', '[img', '[email', '[color', '[colour', 'quote]', 'code]', 'url]', 'img]', 'email]', 'color]', 'colour]', '^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '+', '[', ']', '{', '}', ':', '\\', '/', '=', '#', ';', '!', '*');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    47
		$noise_replace =	array('',       '',      '',     '',     '',       '',       '',        '',       '',      '',     '',     '',       '',       '',        ' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' ,  ' ', ' ', ' ', ' ', ' ', ' ');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    48
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    49
		$stopwords = (array)@file(PUN_ROOT.'lang/'.$pun_user['language'].'/stopwords.txt');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    50
		$stopwords = array_map('trim', $stopwords);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    51
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    52
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    53
	// Clean up
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    54
	$patterns[] = '#&[\#a-z0-9]+?;#i';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    55
	$patterns[] = '#\b[\w]+:\/\/[a-z0-9\.\-]+(\/[a-z0-9\?\.%_\-\+=&\/~]+)?#';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    56
	$patterns[] = '#\[\/?[a-z\*=\+\-]+(\:?[0-9a-z]+)?:[a-z0-9]{10,}(\:[a-z0-9]+)?=?.*?\]#';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    57
	$text = preg_replace($patterns, ' ', ' '.strtolower($text).' ');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    58
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    59
	// Filter out junk
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    60
	$text = str_replace($noise_match, $noise_replace, $text);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    61
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    62
	// Strip out extra whitespace between words
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    63
	$text = trim(preg_replace('#\s+#', ' ', $text));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    64
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    65
	// Fill an array with all the words
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    66
	$words = explode(' ', $text);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    67
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    68
	if (!empty($words))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    69
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    70
		while (list($i, $word) = @each($words))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    71
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    72
			$words[$i] = trim($word, '.');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    73
			$num_chars = pun_strlen($word);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    74
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    75
			if ($num_chars < 3 || $num_chars > 20 || in_array($word, $stopwords))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    76
				unset($words[$i]);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    77
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    78
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    79
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    80
	return array_unique($words);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    81
}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    82
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    83
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    84
//
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    85
// Updates the search index with the contents of $post_id (and $subject)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    86
//
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    87
function update_search_index($mode, $post_id, $message, $subject = null)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    88
{
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
    89
	global $db_type, $pun_db;
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    90
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    91
	// Split old and new post/subject to obtain array of 'words'
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    92
	$words_message = split_words($message);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    93
	$words_subject = ($subject) ? split_words($subject) : array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    94
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    95
	if ($mode == 'edit')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    96
	{
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
    97
		$result = $pun_db->query('SELECT w.id, w.word, m.subject_match FROM '.$pun_db->prefix.'search_words AS w INNER JOIN '.$pun_db->prefix.'search_matches AS m ON w.id=m.word_id WHERE m.post_id='.$post_id, true) or error('Unable to fetch search index words', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    98
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    99
		// Declare here to stop array_keys() and array_diff() from complaining if not set
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   100
		$cur_words['post'] = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   101
		$cur_words['subject'] = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   102
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   103
		while ($row = $pun_db->fetch_row($result))
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   104
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   105
			$match_in = ($row[2]) ? 'subject' : 'post';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   106
			$cur_words[$match_in][$row[1]] = $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   107
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   108
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   109
		$pun_db->free_result($result);
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   110
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   111
		$words['add']['post'] = array_diff($words_message, array_keys($cur_words['post']));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   112
		$words['add']['subject'] = array_diff($words_subject, array_keys($cur_words['subject']));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   113
		$words['del']['post'] = array_diff(array_keys($cur_words['post']), $words_message);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   114
		$words['del']['subject'] = array_diff(array_keys($cur_words['subject']), $words_subject);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   115
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   116
	else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   117
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   118
		$words['add']['post'] = $words_message;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   119
		$words['add']['subject'] = $words_subject;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   120
		$words['del']['post'] = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   121
		$words['del']['subject'] = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   122
	}
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   123
  
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   124
	unset($words_message);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   125
	unset($words_subject);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   126
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   127
	// Get unique words from the above arrays
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   128
	$unique_words = array_unique(array_merge($words['add']['post'], $words['add']['subject']));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   129
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   130
	if (!empty($unique_words))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   131
	{
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   132
		$result = $pun_db->query('SELECT id, word FROM '.$pun_db->prefix.'search_words WHERE word IN('.implode(',', preg_replace('#^(.*)$#', '\'\1\'', $unique_words)).')', true) or error('Unable to fetch search index words', __FILE__, __LINE__, $pun_db->error());
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   133
    
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   134
		$word_ids = array();
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   135
		while ($row = $pun_db->fetch_row($result))
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   136
			$word_ids[$row[1]] = $row[0];
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   137
    
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   138
		$pun_db->free_result($result);
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   139
    
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   140
		$new_words = array_diff($unique_words, array_keys($word_ids));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   141
		unset($unique_words);
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   142
    
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   143
		if (!empty($new_words))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   144
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   145
			switch ($db_type)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   146
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   147
				case 'mysql':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   148
				case 'mysqli':
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   149
					$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_words (word) VALUES'.implode(',', preg_replace('#^(.*)$#', '(\'\1\')', $new_words))) or error('Unable to insert search index words', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   150
					break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   151
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   152
				default:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   153
					while (list(, $word) = @each($new_words))
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   154
						$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_words (word) VALUES(\''.$word.'\')') or error('Unable to insert search index words', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   155
					break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   156
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   157
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   158
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   159
		unset($new_words);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   160
	}
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   161
  
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   162
	// Delete matches (only if editing a post)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   163
	while (list($match_in, $wordlist) = @each($words['del']))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   164
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   165
		$subject_match = ($match_in == 'subject') ? 1 : 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   166
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   167
		if (!empty($wordlist))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   168
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   169
			$sql = '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   170
			while (list(, $word) = @each($wordlist))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   171
				$sql .= (($sql != '') ? ',' : '').$cur_words[$match_in][$word];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   172
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   173
			$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_matches WHERE word_id IN('.$sql.') AND post_id='.$post_id.' AND subject_match='.$subject_match) or error('Unable to delete search index word matches', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   174
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   175
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   176
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   177
	// Add new matches
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   178
	while (list($match_in, $wordlist) = @each($words['add']))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   179
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   180
		$subject_match = ($match_in == 'subject') ? 1 : 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   181
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   182
		if (!empty($wordlist))
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   183
			$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_matches (post_id, word_id, subject_match) SELECT '.$post_id.', id, '.$subject_match.' FROM '.$pun_db->prefix.'search_words WHERE word IN('.implode(',', preg_replace('#^(.*)$#', '\'\1\'', $wordlist)).')') or error('Unable to insert search index word matches', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   184
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   185
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   186
	unset($words);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   187
}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   188
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   189
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   190
//
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   191
// Strip search index of indexed words in $post_ids
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   192
//
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   193
function strip_search_index($post_ids)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   194
{
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   195
	global $db_type, $pun_db;
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   196
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   197
	switch ($db_type)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   198
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   199
		case 'mysql':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   200
		case 'mysqli':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   201
		{
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   202
			$result = $pun_db->query('SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE post_id IN('.$post_ids.') GROUP BY word_id') or error('Unable to fetch search index word match', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   203
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   204
			if ($pun_db->num_rows($result))
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   205
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   206
				$word_ids = '';
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   207
				while ($row = $pun_db->fetch_row($result))
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   208
					$word_ids .= ($word_ids != '') ? ','.$row[0] : $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   209
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   210
				$result = $pun_db->query('SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE word_id IN('.$word_ids.') GROUP BY word_id HAVING COUNT(word_id)=1') or error('Unable to fetch search index word match', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   211
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   212
				if ($pun_db->num_rows($result))
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   213
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   214
					$word_ids = '';
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   215
					while ($row = $pun_db->fetch_row($result))
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   216
						$word_ids .= ($word_ids != '') ? ','.$row[0] : $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   217
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   218
					$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_words WHERE id IN('.$word_ids.')') or error('Unable to delete search index word', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   219
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   220
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   221
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   222
			break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   223
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   224
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   225
		default:
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   226
			$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_words WHERE id IN(SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE word_id IN(SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE post_id IN('.$post_ids.') GROUP BY word_id) GROUP BY word_id HAVING COUNT(word_id)=1)') or error('Unable to delete from search index', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   227
			break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   228
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   229
2
a8a21e1c7afa Let's just say that the API loads. While a decent part of PunBB works, we've still got a LONG way to go, mainly with form validation and security. At this point, Punano is NOT secure as far as privileges and user levels go.
Dan
parents: 0
diff changeset
   230
	$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_matches WHERE post_id IN('.$post_ids.')') or error('Unable to delete search index word match', __FILE__, __LINE__, $pun_db->error());
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   231
}