encrypted-docsify-md-with-php
简体中文
Since docsify is a runtime library, I use php to process encrypted files for security reasons, otherwise encryption has no meaning.
DEMO
Password: 123
https://retrocode.io/#/%E6%8A%80%E5%B7%A7/password-123.auth.md
How to use
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)
Configure nginx pseudo-static to forward requests for encrypted files to auth.php
location ~* .(auth.md)$ { if (!-e $request_filename){ rewrite ^(.*)$ /auth.php?s=$1 last; break; } }
Create auth.php and place it in the same directory as docsify's index.html
<?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); }
Notice
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 . ")rn", FILE_APPEND); } }
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。