mindwi.se: WikiMind-source

Code source entier de WikiMind.
Sous licence selon les termes et conditions de la licence MIT.

Version 2020.02 : 10336 octets, 210 lignes.

Pour l'utiliser

  1. Copier dans un fichier index.php (ou autre nom au choix)
  2. Utiliser le site bcrypt-generator pour générer un mot de passe et insérer le résultat à la ligne 10 (variable $_PASSWORD) entre les deux apostrophes.
  3. Transférer sur le serveur désiré.
  4. Rendez-vous sur le fichier en question avec votre navigateur.
  5. Testez!

Besoin d'aide?

Écrivez-moi au info at mindwi.se.

Changelog


    <!DOCTYPE html>
    <html lang="fr">
    <head>
        <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
        <title>mindwi.se</title>
        <style>.page{margin:0 auto;max-width: 500px;} div.c a{text-decoration: underline;} h1{font-size: 1.6em; color:#333; line-height: 1.5em; font-weight:normal;} a{text-decoration:none} a:link {color: #333; text-decoration: none;} a:visited {color: #333; text-decoration: none;} a:hover {color: #999; text-decoration: none;} blockquote{font-family:monospace} html{padding:0 10px 10px 10px} body{line-height:1.4;font-family: Helvetica-neue, Helvetica, sans-serif;font-size: 1em; line-height: 1.5em; margin:20px;} input, textarea {font-size:1em;}</style>
    </head>
    <body>
    <div class="page">
    <?php //WikiMind, version 2020.02
    $_PASSWORD = ''; //Hashed password for editing pages : https://bcrypt-generator.com/
    class HtmlUp { // HtmlUp, 2014 Jitendra Adhikari
        private $Lines;
        private $Pointer = -1;
        public function __construct($markdown) {
            $this->Lines = explode("\n",trim(str_replace(array("\r\n", "\r", ), "\n", str_replace("\t", '    ', $markdown)), "\n") . "\n");
            unset($markdown);
        }
        public function __toString() {
            return $this->parse();
        }
        public function parse() {
            if (empty($this->Lines)) return '';
            $markup = '';
            $nestLevel = $quoteLevel = 0;
            $indent = $nextIndent = 0;
            $stackList = $stackBlock = array();
            $lastPointer = count($this->Lines) - 1;
            while (isset($this->Lines[++$this->Pointer])) {
                $line = $this->Lines[$this->Pointer];
                $trimmedLine = trim($line);
                if (empty($trimmedLine)) {
                    while($stackList) {
                        $markup .= array_pop($stackList);
                    }
                    while($stackBlock) {
                        $markup .= array_pop($stackBlock);
                    }
                    $inList = $inQuote = $inPara = null;
                    $nestLevel = $quoteLevel = 0; 
                    continue;
                }
                if (preg_match('/^<\/?\w.*?\/?>/', $trimmedLine)) { // raw html
                    $markup .= "\n$line";
                    continue;
                }
                $nextLine = $this->Pointer < $lastPointer ? $this->Lines[$this->Pointer+1] : null;
                $trimmedNextLine = $nextLine ? trim($nextLine) : null;
                $indent = strlen($line) - strlen(ltrim($line));;            
                $nextIndent = $nextLine ? strlen($nextLine) - strlen(ltrim($nextLine)) : 0;
                $nextMark1 = isset($trimmedNextLine[0]) ? $trimmedNextLine[0] : null;
                $nextMark12 = $trimmedNextLine ? substr($trimmedNextLine, 0, 2) : null;
                if (preg_match('~^\s*(>+)\s+~', $line, $quoteMatch)){ // blockquote
                    $line = substr($line, strlen($quoteMatch[0]));
                    $trimmedLine = trim($line);
                    if (empty($inQuote) OR $quoteLevel < strlen($quoteMatch[1])) {
                        $markup .= "\n<blockquote>";
                        $stackBlock[] = "\n</blockquote>";
                        $quoteLevel++;                    
                    }
                    $inQuote = true;
                }
                $mark1 = $trimmedLine[0];
                $mark12 = substr($trimmedLine, 0, 2);
                if ($mark1 === '#') { // atx
                    $level = strlen($trimmedLine) - strlen(ltrim($trimmedLine, '#'));
                    $markup .= "\n<h{$level}>" . ltrim($trimmedLine, '# ') . "</h{$level}>";
                    continue;    
                }
                if (preg_match('~^\s(={3,}|-{3,})\s$~', $nextLine)) { // setext
                    $level = trim($nextLine, '- ') === '' ? '2' : '1';
                    $markup .= "\n<h{$level}>{$trimmedLine}</h{$level}>";
                    $this->Pointer++;
                    continue;
                }
                if ($codeBlock = preg_match('/^`\s*([\w-]+)?/', $line, $codeMatch) OR (empty($inList) AND empty($inQuote) AND $indent >= 4)) { // fence code
                    $lang = ($codeBlock AND isset($codeMatch[1])) ? " class=\"language-{$codeMatch[1]}\" " : '';
                    $markup .= "\n<pre><code{$lang}>";
                    if (!$codeBlock) $markup .= htmlspecialchars($line);
                    while(isset($this->Lines[$this->Pointer+1]) AND (($line = htmlspecialchars($this->Lines[$this->Pointer+1])) OR true) AND (($codeBlock AND substr(ltrim($line), 0, 3) !== '`') OR substr($line, 0, 4) === '    ')) {
                        $markup .= ("\n").$line;
                        $this->Pointer++;
                    }
                    $this->Pointer++;
                    $markup .= "</code></pre>";
                    continue;
                }
                if (isset($this->Lines[$this->Pointer-1]) AND trim($this->Lines[$this->Pointer-1]) === '' AND preg_match('~^(-{3,})$~', $trimmedLine)) { // rule
                    $markup .= "\n<hr />";
                    continue;
                }
                if ($ul = in_array($mark12, array('- ', '* ')) OR preg_match('/^[0-9]+\./', $mark12)) {
                    $wrapper = $ul ? 'ul' : 'ol'; // list
                    if (empty($inList)) {
                        $stackList[] = "</$wrapper>";
                        $markup .= "\n<$wrapper>\n";
                        $inList = true;
                        $nestLevel++;
                    }
                    $markup .= "\t<li>".substr($trimmedLine, 2);
                    if ($ul = in_array($nextMark12, array('- ', '* ')) OR preg_match('/^[0-9]+\./', $nextMark12)) {
                        $wrapper = $ul ? 'ul' : 'ol';
                        if ($nextIndent > $indent) {
                            $stackList[] = "</li>\n";
                            $stackList[] = "</$wrapper>";
                            $markup .= "\n<$wrapper>\n";
                            $nestLevel++;                        
                        } else $markup .= "</li>\n";
                        if ($nextIndent < $indent) {
                            $shift = intval(($indent - $nextIndent)/4);
                            while ($shift--) {
                                $markup .= array_pop($stackList);
                                if ($nestLevel > 2) {
                                    $markup .= array_pop($stackList);                            
                                }
                            }
                        }
                    } else $markup .= "</li>\n";
                    continue;
                }
                if (isset($inList)) {
                    $markup .= $trimmedLine;
                    continue;
                }
                if (empty($inPara)) $markup .= "\n<p>"; // paragraph
                else $markup .= "\n<br />";
                $markup .= "{$trimmedLine}";
                if (empty($trimmedNextLine)) {
                    $markup .= "</p>\n";
                    $inPara = null;
                } else $inPara = true;
            }
            $markup = preg_replace('~<(https?:[\/]{2}[^\s]+?)>~','<a href="$1">$1</a>',preg_replace('~<(\S+?@\S+?)>~','<a href="mailto:$1">$1</a>',$markup)); // url, emails
            $markup = preg_replace_callback('~!\[(.+?)\]\s\((.+?)\s(".+?")?\)~', function($img) { // images
                $title = isset($img[3]) ? " title={$img[3]} " : '';
                $alt = $img[1] ? " alt=\"{$img[1]}\" " : '';
                return "<img src=\"{$img[2]}\"{$title}{$alt}/>";
            }, $markup);
            $markup = preg_replace_callback('~\[(.+?)\]\s\((.+?)\s(".+?")?\)~', function($a) { // anchors
                $title = isset($a[3]) ? " title={$a[3]} " : '';
                return "<a href=\"{$a[2]}\"{$title}>{$a[1]}</a>";
            }, $markup);
            $markup = preg_replace_callback('!(\*{1,2}|`|~~)(.+?)\\1!', function($em) { // em/code/strong/del
                switch (true) {
                    case substr($em[1], 0, 2) === '**':
                        $tag = 'strong';
                        break;                
                    case substr($em[1], 0, 2) === '~~':
                        $tag = 'del';
                        break;
                    case $em[1] === '*':
                        $tag = 'em';
                        break;
                    default: 
                        $tag = 'code';
                        $em[2] = htmlspecialchars($em[2]);
                }
                return "<$tag>{$em[2]}</$tag>";
            }, $markup);
            return $markup;
        }
    }
    function f($f) { //WikWiki, by Martin Srank
        @mkdir('wik'); //Get a page
        return @file_get_contents("wik/".urldecode($f).".w");
    }
    function b($b) { //Backlinks
        echo "<h1><a href=\"?$b\">Liens vers $b</a></h1><div class=\"c\">";
        foreach (glob('wik/*.w') as $f) {
            $f = substr($f, 4, -2);
            if (strpos(f($f), $b) !== false)
                echo "<a href=\"?$f\">$f</a><br>";
        }
        echo "</div>";
    }
    function x($f) { //Is the page already existing?
        return file_exists("wik/".urldecode($f).".w");
    }
    function e($p) { //Edit page
        $p = $p ? $p : $_GET['e'];
        echo "<h1><a href=\"./\">mindwi.se</a>: <strong>edit <a href=\"?$p\">".urldecode($p)."</a></strong></h1><form action=\"?$p\" method=\"post\"><textarea name=\"c\" cols=\"60\" rows=\"25\">" . htmlspecialchars(f($p)) . "</textarea>\n<br>Mot de passe: <input type=\"password\" name=\"pw\" size=\"8\">\n<br><input type=\"submit\" value=\"Enregistrer\" accesskey=\"e\">\n</form>\n";
    }
    $p = preg_replace('~(e|b)=(.*)~', '', $_SERVER[QUERY_STRING]);
    $c = $_POST[c];
    $pw = $_POST[pw];
    $e = $_GET[e];
    $b = $_GET[b];
    if (!$p && !$e) //Go to main page
        $p = 'accueil';
    if ($c && password_verify($pw, $_PASSWORD)) { //Edit password
        @file_put_contents("wik/".urldecode($p).".w", htmlspecialchars_decode($c));
        header("Location: ?$p");
    } elseif ($c == "" && $pw == $_PASSWORD) { //Delete page if content removed in full
        unlink("wik/".urldecode($p).".w");
        $p = 'Accueil';
        header("Location: ?$p");
    }
    if (isset($_GET['o'])) {
     o();
     exit();
    }
    if (!$e) { //View page mode
        if (!$b) {
            echo x($p) ? "<h1><a href=\"./\">mindwi.se</a>: <strong><a href=\"?b=$p\">".urldecode($p)."</a></strong></h1>\n\n<div class='c' style='text-align:justify'>" . new HtmlUp(f($p)) . "</div>\n<hr />\n<div style='text-align:center'><strong><a href=\"?archives\">archives</a> :: <a href=\"?about\">about</a></strong>\n<br /><small><a href=\"?e=$p\" accesskey=\"e\">" . "edited</a> on " . date('Y-m-d', @filemtime("wik/".urldecode($p).".w")) . "</small></div>\n" : e($p);
        } else b($b);
    } else e($e);
    ?>
    </div>
    </body>
    </html>