Since docsify is a runtime library, I use php to process encrypted files for security reasons, otherwise encryption has no meaning.
Password: 123
https://retrocode.io/#/%E6%8A%80%E5%B7%A7/password-123.auth.md
Append .password.auth to the suffix of the md file.
filename.passwprd.auth.md
example:
testPassword.123.auth.md
At the same time, please modify the file path in _sidebar.md to
filename.auth.md
remove the encrypted password (the encrypted password only exists in the source file)
example:
* category
* [testPassword](category/testPassword.auth.md)
location ~* \.(auth.md)$ {
if (!-e $request_filename){
rewrite ^(.*)$ /auth.php?s=$1 last; break;
}
}
<?php
function startWith($str, $needle) {
return strpos($str, $needle) === 0;
}
function endWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
function echoForm($requestUri) {
echo <<<form
<form action="$requestUri" onsubmit="location.reload();" target="nm_iframe" method='get'>
Read password: <input type='text' name='auth'>
<input type='submit' id="id_submit" value='submit'>
</form>
<iframe id="id_iframe" name="nm_iframe" style="display:none;"></iframe>
form;
exit;
}
$requestUri = urldecode($_SERVER['REQUEST_URI']);
$requestUri = parse_url($requestUri)['path'];
if (isset($_COOKIE['auth']) || isset($_GET['auth'])) {
$cookieValue = $_COOKIE['auth'];
if (isset($_GET['auth'])) {
$cookieValue = md5($_GET['auth']);
setcookie('auth', $cookieValue, time() + 3600 * 12);
}
$pathinfo = pathinfo($requestUri);
$patharr = explode("/", $requestUri);
$basename = str_replace(".auth.md", "", end($patharr));
unset($patharr[count($patharr)-1]);
$mdpath = __DIR__ . implode("/", $patharr);
$files = scandir($mdpath);
foreach ($files as $filename) {
if (startWith($filename, $basename) && endWith($filename, '.auth.md')) {
$password = str_replace($basename . ".", "", $filename);
$password = str_replace(".auth.md", "", $password);
if (md5($password) === $cookieValue) {
echo $filecontent = file_get_contents($mdpath . "/" . $filename);
exit;
}
}
}
echoForm($requestUri);
} else {
echoForm($requestUri);
}
If you use script to generate _sidebar.md, you need to process the file name when generating the _sidebar.md file.
example:
$files = scandir($path);
foreach ($files as $filename) {
$suffix = pathinfo($filename, PATHINFO_EXTENSION);
if ($filename !== "." && $filename !== ".." && $suffix === "md") {
// 删除加密MD文件中的密码
if (endWith($filename, '.auth.md')) {
$tmpfns = explode(".", $filename);
unset($tmpfns[count($tmpfns)-3]);
$filename = implode(".",$tmpfns);
}
$fileRealPath = $categoryName . "/" . $filename;
file_put_contents($GLOBALS['sidebarPath'], " * [" . $filename . "](" . $fileRealPath . ")\r\n", FILE_APPEND);
}
}