1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| <?php
require_once './Markdown1.0.2/markdown.php';
define('PAGEDIR', dirname(__FILE__) . '/pages');
$page = isset($_GET['page']) ? $_GET['page'] : 'Home';
if(isset($_GET['edit'])){ pageHeader($page); edit($page); pageFooter($page,false); }else if(isset($_POST['edit'])){ file_put_contents(pageToFile($_POST['page']), $_POST['contents']);
header('Location:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?page=' . urlencode($_POST['page'])); exit; }else{ pageHeader($page);
if( is_readable(pageToFile($page)) ){
$text = file_get_contents(pageToFile($page));
$text = MarkDown($text);
$text = wikiLinks($text);
echo $text;
pageFooter($page, true); }else{
edit($page, true); pageFooter($page, false); } }
function pageHeader($page){ ?> <html> <head> <title>Wiki:<?php echo htmlentities($page) ?></title> </head> <body> <h1><?php echo htmlentities($page) ?></h1> <hr> <?php }
function pageFooter($page, $displayEditLink){ $timestamp = @filemtime(pageToFile($page)); if($timestamp){ $lastModified = strftime('%c', $timestamp); }else{ $lastModifed = 'Never'; } if($displayEditLink){ $editLink = ' - <a href="?page=' . urlencode($page) . '&edit=true">Edit</a>'; }else{ $editLink = ''; } ?> <hr/> <em>Last Modified:<?php echo $lastModified; ?> </em> <?php echo $editLink; ?> - <a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>">Home</a> </body> </html> <?php }
function edit($page, $isNew = false){ if ($isNew) { $contents = ''; ?> <p> <b> This page doesn't exit yet. </b> To create it, enter its contents below and click the <b> Save </b> button. </p> <?php } else { $contents = file_get_contents(pageToFile($page)); } ?> <form method="post" action="<?php echo htmlentities($_SERVER['SCRIPT_NAME']);?>"> <input type="hidden" name="edit" value="true" /> <input type="hidden" name="page" value="<?php echo htmlentities($page); ?>" /> <textarea name="contents" rows="20" cols="60"> <?php echo htmlentities($contents);?> </textarea> <br /> <input type="submit" value="Save" /> </form> <?php } // 将提交的页眉转换为一个文件名,使用md5()函数避免$page中的淘气字符可能导致的安全问题 function pageTofile($page){ return PAGEDIR . '/' . md5($page); } //把页面中诸如 [something] 之类的文本转化成 Wikeyem页面中"something"这样的HTML链接 function wikiLinks($page){ if ( @preg_match_all('/\[([^\]]+?]/', $page, $matches, PREG_SET_ORDER) ){ foreach($matches as $match){ $page = str_replace( $match[0], '<a href="' . $_SERVER['SCRIPT_NAME'] . '?page' . urlencode($match[1]) . '">' . htmlentities($match[1]) . '</a>', $page ); } } return $page; }
|
PHP Markdown扩展库
[file]https://littoral.michelf.ca/code/php-markdown/php-markdown-1.0.2.zip[/file]