search.php: fix formatting

This commit is contained in:
Zankaria 2025-04-15 22:31:18 +02:00
parent 2e1cb7995f
commit 54dcf79a7f

View file

@ -1,23 +1,23 @@
<?php <?php
require 'inc/bootstrap.php'; require 'inc/bootstrap.php';
if (!$config['search']['enable']) { if (!$config['search']['enable']) {
die(_("Post search is disabled")); die(_("Post search is disabled"));
} }
$queries_per_minutes = $config['search']['queries_per_minutes']; $queries_per_minutes = $config['search']['queries_per_minutes'];
$queries_per_minutes_all = $config['search']['queries_per_minutes_all']; $queries_per_minutes_all = $config['search']['queries_per_minutes_all'];
$search_limit = $config['search']['search_limit']; $search_limit = $config['search']['search_limit'];
if (isset($config['search']['boards'])) { if (isset($config['search']['boards'])) {
$boards = $config['search']['boards']; $boards = $config['search']['boards'];
} else { } else {
$boards = listBoards(TRUE); $boards = listBoards(TRUE);
} }
$body = Element('search_form.html', Array('boards' => $boards, 'board' => isset($_GET['board']) ? $_GET['board'] : false, 'search' => isset($_GET['search']) ? str_replace('"', '&quot;', utf8tohtml($_GET['search'])) : false)); $body = Element('search_form.html', Array('boards' => $boards, 'board' => isset($_GET['board']) ? $_GET['board'] : false, 'search' => isset($_GET['search']) ? str_replace('"', '&quot;', utf8tohtml($_GET['search'])) : false));
if(isset($_GET['search']) && !empty($_GET['search']) && isset($_GET['board']) && in_array($_GET['board'], $boards)) { if (isset($_GET['search']) && !empty($_GET['search']) && isset($_GET['board']) && in_array($_GET['board'], $boards)) {
$phrase = $_GET['search']; $phrase = $_GET['search'];
$_body = ''; $_body = '';
@ -25,13 +25,13 @@
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->bindValue(':time', time() - ($queries_per_minutes[1] * 60)); $query->bindValue(':time', time() - ($queries_per_minutes[1] * 60));
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
if($query->fetchColumn() > $queries_per_minutes[0]) if ($query->fetchColumn() > $queries_per_minutes[0])
error(_('Wait a while before searching again, please.')); error(_('Wait a while before searching again, please.'));
$query = prepare("SELECT COUNT(*) FROM ``search_queries`` WHERE `time` > :time"); $query = prepare("SELECT COUNT(*) FROM ``search_queries`` WHERE `time` > :time");
$query->bindValue(':time', time() - ($queries_per_minutes_all[1] * 60)); $query->bindValue(':time', time() - ($queries_per_minutes_all[1] * 60));
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
if($query->fetchColumn() > $queries_per_minutes_all[0]) if ($query->fetchColumn() > $queries_per_minutes_all[0])
error(_('Wait a while before searching again, please.')); error(_('Wait a while before searching again, please.'));
@ -57,7 +57,7 @@
$name = $m[2]; $name = $m[2];
$value = isset($m[4]) ? $m[4] : $m[3]; $value = isset($m[4]) ? $m[4] : $m[3];
if(!in_array($name, array('id', 'thread', 'subject', 'name'))) { if (!in_array($name, array('id', 'thread', 'subject', 'name'))) {
// unknown filter // unknown filter
return $m[0]; return $m[0];
} }
@ -69,7 +69,7 @@
$phrase = trim(preg_replace_callback('/(^|\s)(\w+):("(.*)?"|[^\s]*)/', 'search_filters', $phrase)); $phrase = trim(preg_replace_callback('/(^|\s)(\w+):("(.*)?"|[^\s]*)/', 'search_filters', $phrase));
if(!preg_match('/[^*^\s]/', $phrase) && empty($filters)) { if (!preg_match('/[^*^\s]/', $phrase) && empty($filters)) {
_syslog(LOG_WARNING, 'Query too broad.'); _syslog(LOG_WARNING, 'Query too broad.');
$body .= '<p class="unimportant" style="text-align:center">(Query too broad.)</p>'; $body .= '<p class="unimportant" style="text-align:center">(Query too broad.)</p>';
echo Element('page.html', Array( echo Element('page.html', Array(
@ -96,7 +96,7 @@
$match = Array(); $match = Array();
// Find exact phrases // Find exact phrases
if(preg_match_all('/"(.+?)"/', $phrase, $m)) { if (preg_match_all('/"(.+?)"/', $phrase, $m)) {
foreach($m[1] as &$quote) { foreach($m[1] as &$quote) {
$phrase = str_replace("\"{$quote}\"", '', $phrase); $phrase = str_replace("\"{$quote}\"", '', $phrase);
$match[] = $pdo->quote($quote); $match[] = $pdo->quote($quote);
@ -105,22 +105,25 @@
$words = explode(' ', $phrase); $words = explode(' ', $phrase);
foreach($words as &$word) { foreach($words as &$word) {
if(empty($word)) if (empty($word)) {
continue; continue;
}
$match[] = $pdo->quote($word); $match[] = $pdo->quote($word);
} }
$like = ''; $like = '';
foreach($match as &$phrase) { foreach($match as &$phrase) {
if(!empty($like)) if (!empty($like)) {
$like .= ' AND '; $like .= ' AND ';
}
$phrase = preg_replace('/^\'(.+)\'$/', '\'%$1%\'', $phrase); $phrase = preg_replace('/^\'(.+)\'$/', '\'%$1%\'', $phrase);
$like .= '`body` LIKE ' . $phrase . ' ESCAPE \'!\''; $like .= '`body` LIKE ' . $phrase . ' ESCAPE \'!\'';
} }
foreach($filters as $name => $value) { foreach($filters as $name => $value) {
if(!empty($like)) if (!empty($like)) {
$like .= ' AND '; $like .= ' AND ';
}
$like .= '`' . $name . '` = '. $pdo->quote($value); $like .= '`' . $name . '` = '. $pdo->quote($value);
} }
@ -130,7 +133,7 @@
$query->bindValue(':limit', $search_limit, PDO::PARAM_INT); $query->bindValue(':limit', $search_limit, PDO::PARAM_INT);
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
if($query->rowCount() == $search_limit) { if ($query->rowCount() == $search_limit) {
_syslog(LOG_WARNING, 'Query too broad.'); _syslog(LOG_WARNING, 'Query too broad.');
$body .= '<p class="unimportant" style="text-align:center">('._('Query too broad.').')</p>'; $body .= '<p class="unimportant" style="text-align:center">('._('Query too broad.').')</p>';
echo Element('page.html', Array( echo Element('page.html', Array(
@ -142,8 +145,8 @@
} }
$temp = ''; $temp = '';
while($post = $query->fetch()) { while ($post = $query->fetch()) {
if(!$post['thread']) { if (!$post['thread']) {
$po = new Thread($post); $po = new Thread($post);
} else { } else {
$po = new Post($post); $po = new Post($post);
@ -151,7 +154,7 @@
$temp .= $po->build(true) . '<hr/>'; $temp .= $po->build(true) . '<hr/>';
} }
if(!empty($temp)) if (!empty($temp))
$_body .= '<fieldset><legend>' . $_body .= '<fieldset><legend>' .
sprintf(ngettext('%d result in', '%d results in', $query->rowCount()), sprintf(ngettext('%d result in', '%d results in', $query->rowCount()),
$query->rowCount()) . ' <a href="/' . $query->rowCount()) . ' <a href="/' .
@ -161,14 +164,15 @@
'</a></legend>' . $temp . '</fieldset>'; '</a></legend>' . $temp . '</fieldset>';
$body .= '<hr/>'; $body .= '<hr/>';
if(!empty($_body)) if (!empty($_body)) {
$body .= $_body; $body .= $_body;
else } else {
$body .= '<p style="text-align:center" class="unimportant">('._('No results.').')</p>'; $body .= '<p style="text-align:center" class="unimportant">('._('No results.').')</p>';
} }
}
echo Element('page.html', Array( echo Element('page.html', Array(
'config'=>$config, 'config'=>$config,
'title'=>_('Search'), 'title'=>_('Search'),
'body'=>'' . $body 'body'=>'' . $body
)); ));