Initial commit

This commit is contained in:
Jacob 2023-08-10 02:14:12 +02:00
commit 15b1d4edcd
16 changed files with 4663 additions and 0 deletions

22
LICENSE Executable file
View file

@ -0,0 +1,22 @@
MIT License
Copyright (c) 2013 Steven Frank
Copyright (c) 2023 speedie <speedie@speedie.site>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

50
README.md Executable file
View file

@ -0,0 +1,50 @@
# swiki
Simple, suckless read-only wiki
## Features
- Simple
- swiki only requires PHP. It has no JavaScript, no SQL and doesn't
**require** a web server or any other dependencies.
- Minimal
- swiki doesn't have a thousand different features. It simply displays
text and images to the user. Nothing else.
- Markdown articles
- Markdown is a simple format that people can understand even with no
HTML/CSS or PHP knowledge.
- Read-only
- swiki is read only, meaning the user cannot make any changes using
a web interface. This is better, why implement a new editor when the
user can just clone a repository in their existing editor and push.
Contributions should be made using a separate tool, such as Git.
## Setup
Install PHP and clone the repository and point your web server to `index.php`.
To test locally, you can run `php` with the `-S` argument.
## License
MIT License
Copyright (c) 2013 Steven Frank
Copyright (c) 2023 speedie <speedie@speedie.site>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

1
articles/404.md Normal file
View file

@ -0,0 +1 @@
404

1
articles/footer.md Normal file
View file

@ -0,0 +1 @@
[swiki](https://git.speedie.site/speedie/swiki)

3
articles/index.md Normal file
View file

@ -0,0 +1,3 @@
# swiki
simple wiki

10
icons/fav.svg Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="body_1" width="384" height="384">
<g transform="matrix(1 0 0 1 0 0)">
<g transform="matrix(4.94 0 0 4.94 179.79 184.81)">
<path transform="matrix(1 0 0 1 -271.17 -151.62)" d="M235.33333 168.66667L267.3385 168.66667L267.3385 134.57936L307.0001 134.57936L307.0001 153.00302L280.97516 153.00302" stroke="#222222" stroke-width="10" fill="none" />
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 603 B

8
icons/home.svg Executable file
View file

@ -0,0 +1,8 @@
<svg version="1.1" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<defs>
<style id="current-color-scheme" type="text/css">.ColorScheme-Text {
color:#eff0f1;
}</style>
</defs>
<path class="ColorScheme-Text" d="m8 2-0.707 0.6992 0.003906 0.003906-5.297 5.295v0.001953 1h1.008v4h-0.007812v1h10v-1-4h1v-1-0.001953l-1.143-1.143-0.7012 0.7012 0.4434 0.4434h-0.5996v5h-3v-3h-2v3h-3v-4-1h-0.5996l4.598-4.596 0.001953 0.001953 0.001953-0.001953 1.441 1.439 0.7012-0.7012-1.441-1.439 0.003906-0.003906-0.707-0.6992zm3 1v3h1.008v-3h-1.008z" fill="#f0f0f0"/>
</svg>

After

Width:  |  Height:  |  Size: 582 B

8
icons/list.svg Executable file
View file

@ -0,0 +1,8 @@
<svg version="1.1" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<defs>
<style id="current-color-scheme" type="text/css">.ColorScheme-Text {
color:#eff0f1;
}</style>
</defs>
<path class="ColorScheme-Text" d="m2 2v12h12v-12zm1 1h10v10h-10zm1 1v1h8v-1zm0 3v1h8v-1zm0 3v1h8v-1z" fill="#f0f0f0"/>
</svg>

After

Width:  |  Height:  |  Size: 329 B

377
index.css Executable file
View file

@ -0,0 +1,377 @@
/* swiki
* See LICENSE file for copyright and license details.
*/
* {
font-family: Monospace;
font-size: 15px;
margin: 0;
padding: 0;
}
blockquote {
margin: 4px 4px 12px 20px;
padding: 4px 10px 4px 10px;
}
form {
display: inline;
}
li {
line-height: 24px;
}
h1, h2, h3, h4, h5, h6 {
margin-bottom: 10px;
margin-top: 10px;
}
h1 {
font-size: 25px;
}
h2 {
font-size: 20px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 15px;
}
hr {
border-bottom: 0;
border-left: 0;
border-right: 0;
margin-top: 12px;
margin-bottom: 12px;
}
ul, ol {
margin-bottom: 16px;
margin-top: 16px;
padding-left: 16px;
list-style-position: outside;
}
ul li, ol li {
padding-left: 8px;
}
li > ul, li > ol {
margin-top: 0px;
margin-bottom: 0px;
}
p {
margin-bottom: 16px;
margin-top: 16px;
}
pre {
font-family: Monospace;
margin-left: 0;
margin-bottom: 16px;
margin-top: 16px;
/* stroke each browser so that they wrap lines in the pre tag */
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
border: 1px dotted gray;
padding: 3px 3px 3px 6px;
}
code {
font-family: Monospace;
padding: .16px .16px .16px .16px;
}
table {
border-collapse: collapse;
padding: 3px;
max-width: 99%;
}
th {
text-align: left;
}
td, th {
padding: .16px 16px .16px .16px;
word-break: break-all;
}
thead {
font-weight: bold;
}
thead td a {
text-decoration:none;
}
thead td span.sortBy:after {
content: "\25BC";
}
.footer {
padding: 5px 5px 5px 16px;
font-family: Monospace;
width: 75%;
}
textarea {
font-family: Monospace;
width: 75%;
}
@media (max-width: 600px), (orientation: portrait) {
textarea {
width: 99%;
}
}
input[type="text"],
textarea {
padding: 2px;
}
input {
font-family: Monospace;
}
.main {
padding: 5px 5px 5px 16px;
}
.main a {
text-decoration: none;
transition: 0.1s;
}
.footer a {
text-decoration: none;
transition: 0.1s;
}
.main a:hover {
text-decoration: underline;
}
.footer a:hover {
text-decoration: underline;
}
.titlebar {
padding: 2px;
}
.titlebar span.title {
font-weight: bold;
margin: 5px;
}
.titledate {
margin-left: 16px;
margin-right: 16px;
}
.toolbar {
position: sticky;
top: 0;
}
.toolbar > a, .titlebar > a {
text-decoration: none;
font-weight: normal;
}
.toolbar > a:hover, .titlebar > a:hover {
text-decoration: underline;
}
.sidebar {
margin-right: 10px;
margin-top: 5px;
margin-left: 10px;
padding: 4px;
float: right;
}
.sidebar h1, h2 {
margin-top: 0px;
margin-bottom: 0px;
}
.sidebar ul {
margin-top: 0;
margin-left: 4px;
}
input.search {
width:8px;
padding: .16px;
}
input.search:focus {
width: 8px;
}
img {
max-width: 50%;
height: auto;
}
img.icon {
width: 24px;
vertical-align: middle;
}
a > img.icon:hover, a > span.icongroup:hover {
outline: 1px outset white;
}
a > img.icon:active, a > span.icongroup:active {
outline: 1px inset white;
}
input.search {
border-width: 0px;
width: 165px
}
.sidebar a {
text-decoration: none;
transition: 0.1s;
}
body {
background-color: #363636;
}
blockquote {
background-color: #0f070f;
}
hr {
border-top: 1px solid #363636;
}
pre {
background: #212121;
}
code {
background: #212121;
color: #bbbbbb;
}
table {
background-color: #212121;
color: #dddddd;
}
td, th {
background: #212121;
}
thead {
border-bottom: 1px #000000;
}
tr:nth-child(even) {
background: #e4e4e4;
}
tr:nth-child(odd) {
background: #eeeeee;
}
.footer {
color: #f0eee4;
}
textarea {
background-color: #212121;
color: #f0eee4;
}
.main {
background-color: #212121;
color: #f0eee4;
}
.main a {
color: #89bfff;
}
.footer a {
color: #89bfff;
}
.main a:hover {
color: #ccccff;
}
.footer a:hover {
color: #ccccff;
}
.main a.literalMatch {
color: #6666ff;
}
.main a.noexist {
color: #f34000 !important;
}
.titlebar {
background-color: #262626;
color: #89bfff;
}
.titledate {
color: #777777;
}
.toolbar {
background-color: #363636;
}
.toolbar > a, .titlebar > a {
color: #eeeeee;
}
.sidebar {
border: solid 1px #666644;
}
input.search {
border-color: #363636;
background-color: #363636;
color: #f0eee4;
}
.sidebar {
color: #aaaaaa;
border-color: #aaaaaa;
background-color: #262626;
}
.sidebar a {
color: #19bfff;
}
.sidebar a:hover {
color: #bbbbff;
}

311
index.php Executable file
View file

@ -0,0 +1,311 @@
<?php
/* swiki - simple wiki
* See LICENSE file for copyright and license details.
*/
spl_autoload_register(function($class){ require str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php'; });
use md\MarkdownExtra;
define('PAGES', dirname(__FILE__). '/articles');
define('BASE', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
define('SELF', $_SERVER['SCRIPT_NAME']);
define('DEFAULTP', 'index');
define('SIDEBAR', 'sidebar');
define('FOOTERP', 'footer');
define('NXTP', '404');
define('PREFIX', 'swiki: ');
define('ALLTITLE', 'all');
define('FAVICON', true);
define('TITLE', true);
define('CSS', true);
define('HEAD', true);
define('FOOTER', true);
define('TITLEBAR', true);
define('TOOLBAR', true);
define('ALL', true);
define('MAXCHARS', 512);
define('META_DESC', true);
define('META_ENC', true);
define('META_VIEWPORT', true);
define('TITLE_DATE', 'j M Y H:i');
function __( $label, $alt_word = null ) {
return is_null($alt_word) ? $label : $alt_word;
}
function truncate($text, $chars) {
if (strlen($text) <= $chars) {
return $text;
}
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
function phead($title, $action, $html, $datetime, $bodyclass="") {
/* TODO: improve this */
if (META_DESC) {
$mdesc = $html;
$mdesc = preg_replace( '@<p\b[^>]*>(?=.*?<a\b[^>]*>).*?<\@p>@si', '', $mdesc);
$mdesc = preg_replace( '@<(li)[^>]*?>.*?</\\1>@si', '', $mdesc);
$mdesc = preg_replace( '@<(ul)[^>]*?>.*?</\\1>@si', '', $mdesc);
$mdesc = strip_tags($mdesc);
$mdesc = preg_replace('/\s*$^\s*/m', "\n", $mdesc);
$mdesc = truncate($mdesc, MAXCHARS);
}
print "<!DOCTYPE html>\n";
print "<html lang=\"en\">\n";
if (HEAD) {
print " <head>\n";
if (META_ENC) {
print " <meta charset=\"UTF-8\">\n";
}
if (META_DESC) {
print " <meta name=\"description\" content=\"$mdesc\">\n";
}
if (META_VIEWPORT) {
print " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n";
}
if (FAVICON) {
print " <link rel=\"icon\" href=\"/icons/fav.svg\"/>\n";
}
if (CSS) {
print " <link type=\"text/css\" rel=\"stylesheet\" href=\"/index.css\"/>\n";
}
if (TITLE) {
print " <title>".PREFIX."$title</title>\n";
}
print " </head>\n";
}
print " <body".($bodyclass != "" ? " class=\"$bodyclass\"":"").">\n";
if (TITLEBAR) {
print " <div class=\"titlebar\"><span class=\"title\">$title</span>$datetime";
print " </div>\n";
}
if (TOOLBAR) {
print " <div class=\"toolbar\">\n";
print " <a href=\"" . SELF . "\"><img src=\"/icons/home.svg\" alt=\"". __(DEFAULTP) . "\" title=\"". __(DEFAULTP) . "\" class=\"icon\"></a>\n";
print " <a href=\"" . SELF . "?action=all\"><img src=\"/icons/list.svg\" alt=\"". __(ALLTITLE) . "\" title=\"". __(ALLTITLE) . "\" class=\"icon\"></a>\n";
print " </div>\n";
}
}
function pfooter() {
if (FOOTERP != '') {
print " </body>\n";
$p = getf(FOOTERP);
if (file_exists($p)) {
$fp = file_get_contents($p);
print " <div class=\"footer\">\n\n";
print tohtml($fp);
print " </div>\n";
}
}
}
function greppages($path = "") {
$filenames = array();
$dir = opendir(PAGES . "/$path" );
while ( $filename = readdir($dir) ) {
if ( $filename[0] == "." ) {
continue;
}
if ( is_dir( PAGES . "/$path/$filename" ) ) {
array_push($filenames, ...greppages( "$path/$filename" ) );
continue;
}
if ( preg_match("/.md$/", $filename) != 1) {
continue;
}
$filename = substr($filename, 0, -(strlen("md")+1) );
if ($filename == NXTP || $filename == FOOTERP)
continue;
$filenames[] = substr("$path/$filename", 1);
}
closedir($dir);
return $filenames;
}
function getf($page) {
return PAGES . "/$page.md";
}
/* TODO: escape even more symbols */
function repsym($fn) {
return str_replace(array('~', '..', '\\', ':', '|', '&'), '-', $fn);
}
function purl($page) {
return SELF . "/".str_replace("%2F", "/", str_replace("%23", "#", urlencode(repsym($page))));
}
function pageLink($page, $title, $attributes="") {
return "<a href=\"" . purl($page) ."\"$attributes>$title</a>";
}
function tohtml($fn) {
$parser = new MarkdownExtra;
$parser->no_markup = true;
$out = $parser->transform($fn);
preg_match_all("/\[\[(.*?)\]\]/", $out, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$fulllinktext = $matches[1][$i];
$linktitlesplit = explode('|', $fulllinktext);
$linkedp = $linktitlesplit[0];
$linkt = (count($linktitlesplit) > 1) ? $linktitlesplit[1] : $linkedp;
$pagept = explode('#', $linkedp)[0];
$linkedp = getf(repsym($pagept));
$exists = file_exists($linkedp);
$out = str_replace("[[$fulllinktext]]",
pageLink($linkedp, $linkt, ($exists? "" : " class=\"noexist\"")), $out);
}
$out = preg_replace("/\{\{(.*?)\}\}/", "<img src=\"" . BASE . "/images/\\1\" alt=\"\\1\" />", $out);
preg_match_all("/<h([1-4])>(.*?)<\/h\\1>/", $out, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$prefix = "<h".$matches[1][$i].">";
$caption = $matches[2][$i];
$suffix = substr_replace($prefix, "/", 1, 0);
}
return $out;
}
function main() {
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
$text = "";
$html = "";
if ($action === 'view') {
$page = preg_match('@^/@', @$_SERVER["PATH_INFO"]) ?
urldecode(substr($_SERVER["PATH_INFO"], 1)) : urldecode(@$_REQUEST['page']);
$page = repsym($page);
if ( $page == "" ) {
$page = DEFAULTP;
}
$filename = getf($page);
if ( file_exists($filename) ) {
$text = file_get_contents($filename);
} else {
if (file_exists(getf(NXTP))) {
$p = file_get_contents(getf(NXTP));
phead('404', 'view', tohtml($p), '');
print " <div class=\"main\">\n\n";
print tohtml($p);
print " </div>\n";
}
pfooter();
die();
}
} else if ( $action === 'all' && ALL ) {
$names = greppages();
$filelist = array();
$sortby = isset($_REQUEST['sortby']) ? $_REQUEST['sortby'] : 'name';
if (!in_array($sortby, array('name', 'recent'))) {
$sortby = 'name';
}
if ($sortby === 'name') {
natcasesort($names);
foreach($names as $page) {
$filelist[$page] = filemtime(getf($page));
}
} else {
foreach($names as $page) {
$filelist[$page] = filemtime(getf($page));
}
arsort($filelist, SORT_NUMERIC);
}
$html .= "<p>".__('Total').": ".count($names)." ".__("articles")."</p>";
$html .= "<table><thead>";
$html .= "<tr>".
"<td>".(($sortby!='name')?("<a href=\"".SELF."?action=all&sortby=name\">Name</a>"):"<span class=\"sortby\">".__('Name')."</span>")."</td>".
"<td>".(($sortby!='recent')?("<a href=\"".SELF."?action=all&sortby=recent\">".__('Modified')."</a>"):"<span class=\"sortby\">".__('Modified')."</span>")."</td>".
"</tr></thead><tbody>";
$dateformat = __('dateformat', TITLE_DATE);
foreach ($filelist as $pname => $pdate) {
$html .= "<tr>".
"<td>".pageLink($pname, $pname)."</td>".
"<td valign=\"top\"><nobr>".date( $dateformat, $pdate)."</nobr></td>".
"</tr>\n";
}
$html .= "</tbody></table>\n";
}
$html .= empty($text) ? '' : tohtml($text);
$datetime = '';
if (($action === 'all') && ALL) {
$title = __(ALLTITLE);
} else if ($filename != '') {
$title = $page;
$dateformat = __('dateformat', TITLE_DATE);
if ( $dateformat ) {
$datetime = "<span class=\"titledate\">" . date($dateformat, @filemtime($filename)) . "</span>";
}
} else {
$title = __($action);
}
phead($title, $action, $html, $datetime);
if (file_exists(getf(SIDEBAR))) {
$sb = file_get_contents(getf(SIDEBAR));
if (SIDEBAR != '') {
print " <div class=\"sidebar\">\n\n";
$text = $sb;
print tohtml($text);
print " </div>\n";
}
}
print " <div class=\"main\">\n\n";
print "$html\n";
print " </div>\n";
pfooter();
}
main();

10
md/Markdown.inc.php Executable file
View file

@ -0,0 +1,10 @@
<?php
// Use this file if you cannot use class autoloading. It will include all the
// files needed for the Markdown parser.
//
// Take a look at the PSR-0-compatible class autoloading implementation
// in the Readme.php file if you want a simple autoloader setup.
require_once dirname(__FILE__) . '/MarkdownInterface.php';
require_once dirname(__FILE__) . '/Markdown.php';

1909
md/Markdown.php Executable file

File diff suppressed because it is too large Load diff

11
md/MarkdownExtra.inc.php Executable file
View file

@ -0,0 +1,11 @@
<?php
// Use this file if you cannot use class autoloading. It will include all the
// files needed for the MarkdownExtra parser.
//
// Take a look at the PSR-0-compatible class autoloading implementation
// in the Readme.php file if you want a simple autoloader setup.
require_once dirname(__FILE__) . '/MarkdownInterface.php';
require_once dirname(__FILE__) . '/Markdown.php';
require_once dirname(__FILE__) . '/MarkdownExtra.php';

1895
md/MarkdownExtra.php Executable file

File diff suppressed because it is too large Load diff

9
md/MarkdownInterface.inc.php Executable file
View file

@ -0,0 +1,9 @@
<?php
// Use this file if you cannot use class autoloading. It will include all the
// files needed for the MarkdownInterface interface.
//
// Take a look at the PSR-0-compatible class autoloading implementation
// in the Readme.php file if you want a simple autoloader setup.
require_once dirname(__FILE__) . '/MarkdownInterface.php';

38
md/MarkdownInterface.php Executable file
View file

@ -0,0 +1,38 @@
<?php
/**
* Markdown - A text-to-HTML conversion tool for web writers
*
* @package php-markdown
* @author Michel Fortin <michel.fortin@michelf.com>
* @copyright 2004-2019 Michel Fortin <https://michelf.com/projects/php-markdown/>
* @copyright (Original Markdown) 2004-2006 John Gruber <https://daringfireball.net/projects/markdown/>
*/
namespace md;
/**
* Markdown Parser Interface
*/
interface MarkdownInterface {
/**
* Initialize the parser and return the result of its transform method.
* This will work fine for derived classes too.
*
* @api
*
* @param string $text
* @return string
*/
public static function defaultTransform($text);
/**
* Main function. Performs some preprocessing on the input text
* and pass it through the document gamut.
*
* @api
*
* @param string $text
* @return string
*/
public function transform($text);
}