Skip to content

Instantly share code, notes, and snippets.

@severak
Created May 24, 2013 04:41
Show Gist options
  • Save severak/5641304 to your computer and use it in GitHub Desktop.
Save severak/5641304 to your computer and use it in GitHub Desktop.
Simple multi-project wiki in one file.
<?php
$lang=array(
"new_project"=>"Nový projekt",
"new_adress_label"=>"Zvolte si prosím adresu vaší nové stránky:",
"adress_chars"=>"Adresa smí obsahovat pouze malá písmena bez háčů a čárek, číslice a pomlčky.",
"create"=>"Vytvořit",
"new_project_template"=>
"Nový projekt
------------
Do tohoto políčka vepište svůj text.
Pro více informací se podívejte do nápovědy.
Po založení stránky zvažte změnu nastavení.
",
"bad_password"=>"Špatné heslo!",
"password"=>"Heslo",
"login"=>"Přihlásit",
"nonpublic"=>"Neveřejné",
"404_create_page"=>"Stránka neexistuje. Můžete ji vytvořit kliknutím na tlačítko upravit.",
"project_exists"=>"Jméno je již obsazeno",
"save"=>"Uložit",
"logout_ok"=>"Byl/a jste úspěšně odhlášena",
"edit_menu"=>"Upravit menu",
"change_passwd"=>"Změnit heslo stránky",
"settings"=>"nastavení",
"use_passwd_for"=>"Použít heslo pro",
"passwd_none"=>"nepoužívat hesla",
"passwd_write"=>"pro zápis",
"passwd_read"=>"pro čtení",
"remove_page_question"=>"Přejete si opravdu smazat tuto stránku.<br>(Nelze vzít zpět)",
'yes'=>"ANO",
'no'=>"NE",
'just_deleted'=>"Stránka byla právě smazána.",
'edit'=>'upravit',
'show'=>'ukázat stránku',
'delete'=>'smazat',
'logout'=>'odhlásit',
'page_list'=>'Seznam stránek',
'backup'=>"Záloha webu",
'backup_note'=>"Zaloha webu {project}.\nVyexportovano dne {date}.\n\nZaloha je ve formatu Markdown.",
"zip_here"=>"Zálohu naleznete zde - ",
"export"=>"Exportovat",
"export_hint"=>"fddf k jh df fd fj dfh dfdf fjf hjdf dfdfhdfjhdfkj dfdfj dfhdf jhdfjdf dfjd fhdfjh dfkjdfhk jdfdf dfjkdf h dfjhdfkjh dfdfdfjhd fjdfhf kjh dfjk",
"top"=>"↑ nahoru ↑"
);
<?php
require "markdown.php";
require "cz.php"; // Translation
$drm="wiki-"; //data folders prefix for more security
$url_path="/"; //URL prefix
/** Jednodušší náhrada funkce glob()
* @param string vyhledávací maska může v názvu souboru obsahovat znak * a ?
* @return array pole obsahující všecny nalezené soubory/adresáře
* @copyright Jakub Vrána, http://php.vrana.cz/
*/
function simple_glob($mask) {
$dirname = preg_replace('~[^/]*$~', '', $mask);
$dir = opendir(strlen($dirname) ? $dirname : ".");
$return = array();
if ($dir) {
$pattern = '~^' . strtr(preg_quote($mask, '~'), array('\\*' => '.*', '\\?' => '.')) . '$~';
while (($filename = readdir($dir)) !== false) {
if ($filename != "." && $filename != ".." && preg_match($pattern, "$dirname$filename")) {
$return[] = "$dirname$filename";
}
}
closedir($dir);
sort($return);
}
return $return;
}
if (isset($_SERVER["PATH_INFO"])){
$path=explode("/",$_SERVER["PATH_INFO"]);
}
if (isset($path[1])){$project=$path[1];}else{$project="www";}
if (isset($path[2])){$page=$path[2];}else{$page="index";}
if (isset($path[3])){$action=$path[3];}else{$action="show";}
session_start();
function export_zip(){
global $url_path,$project,$page,$cont,$action,$drm,$lang;
@unlink("./export/".$project.".zip");
$zip = new ZipArchive();
$filename = "./export/".$project.".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
chdir($drm."text");
$pages=simple_glob($project.".*.txt");
foreach ($pages as $p){
$zip->addFile($p);
}
$exdate=date("j.n. Y");
$zip->setArchiveComment(str_replace(array("{project}","{date}"),array($project,$exdate),$lang["backup_note"]));
$zip->close();
$cont='<h1>'.$lang['backup'].'</h1>'.
$lang['zip_here'].'<a href="'.dirname($url_path)."/export/".$project.'.zip">'.$project.'.zip</a>';
}
function export_html(){
global $url_path,$project,$page,$cont,$action,$drm,$lang;
chdir($drm."text");
$pages=simple_glob($project.".*.txt");
echo '<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="fofr">
<title>'.$project.'</title>
</head>
<body>';
$index=file_get_contents($project.".index.txt");
$index=Markdown($index);
$index=str_replace('href="'.$url_path.$project."/",'href="#',$index);
$menu=@file_get_contents($project.".menu.txt");
if (empty($menu)){$menu="";}
$menu=Markdown($menu);
$menu=str_replace('href="'.$url_path.$project."/",'href="#',$menu);
echo '<a name="index"></a>';
echo '<div class="menu">'.$menu.'</div>';
echo $index;
foreach ($pages as $fname){
$parts=explode(".",$fname);
$text="";
$pagename=$parts[1];
if ($text=file_get_contents($fname)){
if ($pagename!="index"){
$text=Markdown($text);
$text=str_replace('href="'.$url_path.$project."/",'href="#',$text);
echo "<hr><div class=\"syslinks\"><a href=\"#index\">".$lang["top"]."</a> <strong>$pagename</strong></div>";
echo "<div class=\"page\">\n<a name=\"$pagename\"></a>\n";
echo $text."\n</div>\n";
}
}
}
echo "";
exit;
}
function login(){
global $url_path,$project,$page,$cont,$action,$drm,$lang;
if (file_exists($drm."proj/".$project.".pass.txt")){
if (isset($_POST["passwd"])){
$orig=file_get_contents($drm."proj/".$project.".pass.txt");
if (md5($_POST["passwd"])==$orig){
$_SESSION[$project."_p"]=1;
}
}
}else{
$_SESSION[$project."_p"]=1;
}
if (!isset($_SESSION[$project."_p"])){
$cont='<form action="'.$url_path.$project."/".$page.'/'.$action.'" method="POST">
<label for="passwd">'.$lang["password"].'</label><input name="passwd" type="password"><br>
<input type="submit" value="'.$lang["login"].'">
</form>';
if (isset($_POST["passwd"])){
$cont=$lang["bad_password"]."<br>".$cont;
}
return false;
}else{
return true;
}
}
$menu_src=$art=@file_get_contents($drm."text/".$project.".menu.txt");
if (empty($menu_src)){
$menu_src="";
}
if (file_exists($drm."proj/".$project.".secret.txt")){
if (!login()){
$menu="<h1>".$lang["nonpublic"]."</h1>";
}else{
$menu=Markdown($menu_src);
}
}else{
$menu=Markdown($menu_src);
}
$art=@file_get_contents($drm."text/".$project.".".$page.".txt");
if (empty($art)){
$art=($action=="show" ? $lang["404_create_page"] : "");
}
if (isset($_POST["text"])){
if (login()){
file_put_contents($drm."text/".$project.".".$page.".txt",$_POST["text"]);
$art=$_POST["text"];
}
}
switch($action){
case "edit":
if (login()){
$cont='
<div >
<div class="dp50">
<div id="wmd-button-bar"></div>
<form action="'.$url_path.$project."/".$page.'" method="POST">
<textarea name="text" cols="40" rows="20" id="wmd-input">
'.$art.'
</textarea><br>
<input type="submit" value="'.$lang["save"].'">
</form>
</div>
<div id="wmd-preview" class="dp50 scroll"></div>
<div class="clear"></div>
<script>
(function () {
var converter = Markdown.getSanitizingConverter();;
var editor = new Markdown.Editor(converter);
editor.run();
})();
</script>
<style>
.dp50 {float:left; display: inline; *margin-left:-0.04em; width: 50%;}
.clear{ clear:both;}
.scroll {overflow: hidden;}
</style>
';
}
break;
case "raw":
if (login()){
header('Content-type: text/plain; charset=utf-8');
echo $art;
exit;
}
break;
case "logout":
unset($_SESSION[$project."_p"]);
$cont=$lang["logout_ok"];
break;
case "settings":
if (login()){
$cont=
'<a href="'.$url_path.$project.'/menu/edit">'.$lang["edit_menu"].'</a><br>'.
'<a href="'.$url_path.$project.'/'.$page.'/setpass">'.$lang["change_passwd"].'</a><br>'.
'<a href="'.$url_path.$project.'/'.$page.'/list">'.$lang["page_list"].'</a><br>'.
'<a href="'.$url_path.$project.'/'.$page.'/zip">'.$lang["backup"].'</a><br>'.
'<a href="'.$url_path.$project.'/'.$page.'/export.htm">'.$lang["export"].'</a> '.$lang["export_hint"].'<br>';
$menu='<h1>'.$lang['settings'].'</h1>';
}
break;
case "setpass":
$cont='<form action="'.$url_path.$project."/".$page.'" method="POST">
<label>'.$lang['password'].'</label><input name="setpass" type="password"><br>
<label>'.$lang['use_passwd_for'].'</label><input type="radio" name="secret" value="0">'.$lang['passwd_none'].'<br>
<label>&nbsp</label><input type="radio" name="secret" value="1" checked="checked">'.$lang['passwd_write'].'<br>
<label>&nbsp</label><input type="radio" name="secret" value="2">'.$lang['passwd_read'].'<br>
<input type="submit" value="'.$lang['change_passwd'].'">
</form>';
$menu='<h1>'.$lang['settings'].'</h1>';
break;
case "delete":
if (login()){
$cont=$lang['remove_page_question'].
'<form action="'.$url_path.$project.'" method="POST"><input type="hidden" name="delete" value="'.$page.'"><input type="submit" value="'.$lang['yes'].'"></form>'.
'<a href="'.$url_path.$project.'/'.$page.'">'.$lang['no'].'</a>';
}
break;
case "newproj":
$menu='<h1>'.$lang["new_project"].'</h1>';
$cont='<form action="'.$url_path.$project.'" method="POST">'.
$lang['new_adress_label'].'<br>'.
$url_path.'<input name="create"><br>'.
'<small>'.$lang['adress_chars'].'</small><br>'.
'<input type="submit" value="'.$lang['create'].'"></form>';
break;
case "list":
$pages=simple_glob($drm."text/".$project.".*.txt");
$cont="<ul>";
foreach ($pages as $pag){
$parts=explode(".",$pag);
$cont.='<li><a href="'.$url_path.$project.'/'.$parts[1].'">'.$parts[1].'</a></li>';
}
$cont.='</ul>';
$menu='<h1>'.$lang['page_list'].'</h1>('.$project.')';
break;
case "zip":
if (login()){
export_zip();
}
break;
case "export.htm":
if (login()){
export_html();
}
break;
case "show":
default:
if (file_exists($drm."proj/".$project.".secret.txt")){
if (login()){
$cont=Markdown($art);
}
}else{
$cont=Markdown($art);
}
break;
}
if (isset($_POST["setpass"])){
if (file_exists($drm."proj/".$project.".pass.txt")){
$ok=(login() ? 1 : 0);
}else{
$ok=1;
}
if ($ok){
switch ($_POST["secret"]){
case 0:
@unlink($drm."proj/".$project.".pass.txt");
@unlink($drm."proj/".$project.".secret.txt");
break;
case 1:
file_put_contents($drm."proj/".$project.".pass.txt",md5($_POST["setpass"]));
@unlink($drm."proj/".$project.".secret.txt");
break;
case 2:
file_put_contents($drm."proj/".$project.".pass.txt",md5($_POST["setpass"]));
file_put_contents($drm."proj/".$project.".secret.txt",md5($_POST["setpass"]));
break;
}
unset($_SESSION[$project."_p"]);
}
}
if (isset($_POST["create"]) and login()){
$menu='<h1>'.$lang['new_project'].'<h1>';
if (!file_exists($drm."text/".$_POST["create"].".index.txt")){
file_put_contents($drm."text/".$_POST["create"].".index.txt",$lang['new_project_template']);
$proj=$_POST["create"];
$host=$_SERVER['HTTP_HOST'];
header("Location: http://$host$url_path$proj/index/edit");
exit;
}else{
$cont=$lang["project_exists"];
}
}
if (isset($_POST["delete"])){
if (login()){
@unlink($drm."text/".$project.".".$_POST["delete"].".txt");
if ($_POST["delete"]=="index" or $_POST["delete"]=""){
$cont=$lang['just_deleted'];
}
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="fofr">
<script type="text/javascript" src="<?php echo $url_path; ?>js/Markdown.Converter.js"></script>
<script type="text/javascript" src="<?php echo $url_path; ?>js/Markdown.Sanitizer.js"></script>
<script type="text/javascript" src="<?php echo $url_path; ?>js/Markdown.Editor.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $url_path; ?>js/wmd.css">
<?php
if (file_exists($drm."proj/".$project.".css")){
$css=file_get_contents($drm."proj/".$project.".css");
echo '<style>
'.$css.'
</style>
';
}else{
echo '<link rel="stylesheet" type="text/css" href="'.$url_path.'style.css">';
}
?>
<title><?php echo $project." - ".$page; ?></title>
</head>
<body>
<div>
<div id="menu">
<?php echo $menu; ?>
</div>
<div id="text">
<?php echo $cont; ?>
</div>
<div id="foot">
<?php if ($action=="show"): ?>
<a href="<?php echo $url_path.$project."/".$page."/edit"; ?>"><?php echo $lang['edit'];?></a>
<?php else: ?>
<a href="<?php echo $url_path.$project."/".$page; ?>"><?php echo $lang['show'];?></a>
<?php endif; ?>
<a href="<?php echo $url_path.$project."/".$page."/settings"; ?>"><?php echo $lang['settings'];?></a>
<a href="<?php echo $url_path.$project."/".$page."/delete"; ?>"><?php echo $lang['delete'];?></a>
<?php if (isset($_SESSION[$project."_p"])): ?>
<a href="<?php echo $url_path.$project."/".$page."/logout"; ?>"><?php echo $lang['logout'];?></a>
<?php endif; ?>
</div>
</div>
<div class="reklama"><endora></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment