Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cdn/dev/css/kb-search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.search-bar {
display: flex;
align-items: center;
width: 40vw;
height: 56px;
padding: 0 10px;
margin-bottom: 20px;
margin-left: 10px;
background-color: #eceef3;
border: 1px solid gray;
}

.search-icon {
font-size: 20px;
opacity: 0.6;
margin-right: 10px;
}

.search-bar form {
width: 100%;
display: flex;
flex-direction: row;
}

.search-bar input {
flex: 1;
background: transparent;
border: none;
outline: none;
font-size: 18px;
width: 100%;
padding: 0 10px;
}
25 changes: 19 additions & 6 deletions knowledge-base/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
// Required
head([
'title' =>'Keyman Support | ' . $title,
'css' => ['template.css','prism.css'],
'css' => ['template.css','prism.css', 'kb-search.css'],
'showMenu' => true,
'index' => false
]);

echo "<h1>Knowledge Base index</h1>";
if($id) {
echo "<p>";
$pid = intval($id,10) - 1;
Expand All @@ -55,8 +55,15 @@
$ParsedownAndAlerts = new \Keyman\Site\Common\GFMAlerts();
echo $ParsedownAndAlerts->text($kb);
} else {
echo "<h1>Knowledge Base index</h1>";
echo "<ul>";
$query = trim($_GET['q'] ?? '');
echo "<div class='search-bar'>";
echo "<form>";
echo "<span class='search-icon'>🔍</span>";
echo "<input type='text' id='searchInput' name='q' value='" .htmlspecialchars($query). "' placeholder='What would you like to find?'>";
echo "</form>";
echo "</div>";

echo "<ul id='KMKB'>";
$kbs = glob("kb*.md");
foreach($kbs as $kb) {
if(preg_match("/^kb(\d+)\.md$/", $kb, $matches)) {
Expand All @@ -65,9 +72,15 @@
$title = fgets($handle);
$title = substr($title, 2);
fclose($handle);

$searchTarget = strtolower("KMKB{$id} {$title}");
if ($query !== '' && stripos($searchTarget, $query) === false) {
continue; // skip this if there isn't a query and a query match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to fclose($handle); before we continue so that we don't leak $handle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. What will be the issue if it is leaked?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the worst case the server uses more and more resources and eventually can crash because it runs out of file handles. Whether or not that happens depends on the implementation of the server, how often processes get restarted etc, but leaks always smell.

I'm not that familiar with php and don't know if fclose is mainly cosmetic and php does some kind of garbage collection in the background, but if there is a fclose and we call it elsewhere, I'd call it here as well.

}

echo "<li><a href='".link_from_id($id)."'>KMKB{$matches[1]}: " . htmlspecialchars($title) . "</a></li>";
}
}

}
echo "</ul>";
}
?>