dev_endboard/opt/search.php

111 lines
3.8 KiB
PHP

<?php
/*
* This is the endboard software, version beta 0.80
* It is a textboard written for the use in the darknets.
*
* This file holds all the functions used to do a fulltext search on
* the content of the db.
*
* The writing of this code started some time ago with another software
* called smolBBS. Although there is almost no original code left now,
* I still regard endboard as a fork of smolBBS.
* The author of smolBBS has required that the following text be
* distributed with any redistribution, so here it goes.
* The license and other conditions apply to endboard as well.
*
* IRC: *dulm @ irc.rizon.net
*
* Copyright (C) 2020 sandlind
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// do a fulltext search on the content of the table "threads", which
// contains the posts
function fulltext_search($db, $settings, $text)
{
if ( ($settings['enable_search'] == FALSE) ) {
return;
}
$pieces = array_reverse(explode(' ', $text));
$new_text = array();
foreach($pieces as $word){
$new_word = filter($word, 'alnumstar', '250');
array_push($new_text, $new_word);
}
$text = implode(' ', $new_text);
$results = array();
$db->exec('DROP TABLE search');
$db->exec('CREATE VIRTUAL TABLE IF NOT EXISTS search
USING fts5 (sub, text, name, post_id, org_id, shadow)');
$db->exec('INSERT INTO search
SELECT sub, text, name, post_id, org_id, shadow
FROM threads');
try {
$db->enableExceptions(true);
$statement = $db->prepare("SELECT DISTINCT sub, text, name,
post_id, org_id
FROM search
WHERE text MATCH ?
AND shadow = 'no'
ORDER BY rank");
$statement->bindParam(1, $text);
$result = $statement->execute();
} catch (Exception $fault) {
echo "The search: \"$text\" produced an error: "
. $fault->getMessage()
. " <br>Please go back to try again.";
quit($db, '');
}
while ($row = $result->fetchArray(SQLITE3_NUM)) {
$search = array();
$search['0'] = "{$row[0]}";
$search['1'] = "{$row[1]}";
$search['2'] = "{$row[2]}";
$search['3'] = "{$row[3]}";
$search['4'] = "{$row[4]}";
array_push($results, $search);
}
return $results;
}
// EOF