Files
hustoj/web/docs.php

71 lines
2.1 KiB
PHP

<?php
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once('./include/const.inc.php');
function normalize_doc_title($text) {
$text = trim((string)$text);
$text = preg_replace('/[\s\p{P}\p{S}]+/u', '', $text);
if (function_exists('mb_strtolower')) {
return mb_strtolower($text, 'UTF-8');
}
return strtolower($text);
}
$docs_dir = __DIR__ . '/doc';
$doc_entries = array();
if (is_dir($docs_dir)) {
$doc_files = glob($docs_dir . '/*.md');
if ($doc_files !== false) {
sort($doc_files, SORT_NATURAL | SORT_FLAG_CASE);
foreach ($doc_files as $doc_file) {
$doc_entries[] = array(
'file' => basename($doc_file),
'title' => preg_replace('/\.md$/i', '', basename($doc_file))
);
}
}
}
$selected_file = '';
if (isset($_GET['file'])) {
$selected_file = basename($_GET['file']);
}
if (empty($selected_file) && !empty($doc_entries)) {
$selected_file = $doc_entries[0]['file'];
}
$selected_title = '文档';
$selected_markdown = '';
$show_selected_title = true;
if (!empty($selected_file)) {
$selected_path = realpath($docs_dir . DIRECTORY_SEPARATOR . $selected_file);
$docs_root = realpath($docs_dir);
if (
$selected_path !== false &&
$docs_root !== false &&
strpos($selected_path, $docs_root . DIRECTORY_SEPARATOR) === 0 &&
is_file($selected_path) &&
preg_match('/\.md$/i', $selected_path)
) {
$selected_markdown = file_get_contents($selected_path);
if ($selected_markdown === false) {
$selected_markdown = '';
} else {
$selected_markdown = preg_replace('/^\xEF\xBB\xBF/', '', $selected_markdown);
}
$selected_title = preg_replace('/\.md$/i', '', basename($selected_path));
if (preg_match('/^\s*#\s+(.+)$/mu', $selected_markdown, $heading_match)) {
$show_selected_title = normalize_doc_title($heading_match[1]) !== normalize_doc_title($selected_title);
}
}
}
$show_title = $selected_title . ' - ' . $OJ_NAME;
require('template/' . $OJ_TEMPLATE . '/docs.php');
?>