Merge pull request #867 from splifingate/master

Ensure three-character minimum for $keyword search by @splifingate
This commit is contained in:
Dan 2025-01-29 18:09:26 +07:00 committed by GitHub
commit 410496d0b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 32 deletions

View file

@ -1319,35 +1319,38 @@ function get_frontpage()
// Return search page.
function get_keyword($keyword, $page, $perpage)
{
$posts = get_blog_posts();
if (strlen($keyword) >= 3) { // three-character minimum
$tmp = array();
$posts = get_blog_posts();
foreach ($posts as $index => $v) {
$tmp = array();
$filepath = $v['dirname'] . '/' . $v['basename'];
foreach ($posts as $index => $v) {
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // Add word boundaries (find whole-words only)
$filepath = $v['dirname'] . '/' . $v['basename'];
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // whole-words only
$lines = file($filepath);
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
$lines = file($filepath);
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
}
}
if (empty($tmp)) {
return false;
}
return $tmp = get_posts($tmp, $page, $perpage);
}
if (empty($tmp)) {
return false;
}
return $tmp = get_posts($tmp, $page, $perpage);
}
// Get related posts base on post category.
@ -1513,32 +1516,34 @@ function get_tagcount($var)
// Return search result count
function keyword_count($keyword)
{
$posts = get_blog_posts();
if (strlen($keyword) >= 3) { // three-character minimum
$tmp = array();
$posts = get_blog_posts();
foreach ($posts as $index => $v) {
$tmp = array();
$filepath = $v['dirname'] . '/' . $v['basename'];
foreach ($posts as $index => $v) {
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // Add word boundaries (find whole-words only)
$filepath = $v['dirname'] . '/' . $v['basename'];
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // whole-words only
$lines = file($filepath);
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
$lines = file($filepath);
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
}
}
$tmp = array_unique($tmp, SORT_REGULAR);
return count($tmp);
}
$tmp = array_unique($tmp, SORT_REGULAR);
return count($tmp);
}
// Return recent posts lists

View file

@ -31,7 +31,7 @@
<!-- main.html.php -->
<div class="row justify-content-center" style="padding-top: 3rem;">
<div class="col-md-12 text-center">
<h1 class="mt-0">Search: <span style='color: #628B48;'><?php echo $search->title;?></span> (<?php echo $search->count;?>)</h1>
<h1 class="mt-0">Search: <?php echo $search->title;?> (<?php echo $search->count;?>)</h1>
<form><input type="search" name="search" class="form-control is-search" placeholder="<?php echo i18n('Type_to_search');?>"></form>
</div>
</div>