exec("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, username TEXT, password TEXT, usertype INT, primaryadmin INT, numberofcomments INT, lastused TEXT, created TEXT, ip TEXT, useragent TEXT, key TEXT)"); /* comments table * id (INTEGER PRIMARY KEY) * date (TEXT) * data (TEXT) * username (TEXT) * usertype (INT) * page (INT) */ $Database->exec("CREATE TABLE IF NOT EXISTS comments(id INTEGER PRIMARY KEY, date TEXT, data TEXT, username TEXT, usertype INT, page INT)"); /* pages table * id (INTEGER PRIMARY KEY) * username (TEXT) * date (TEXT) * endpoint (TEXT) * file (TEXT) */ $Database->exec("CREATE TABLE IF NOT EXISTS pages(id INTEGER PRIMARY KEY, username TEXT, date TEXT, endpoint TEXT, file TEXT)"); /* requests table * id (INTEGER PRIMARY KEY) * pageid (INT) * username (TEXT) * date (TEXT) * message (TEXT) * endpoint (TEXT) * file (TEXT) */ $Database->exec("CREATE TABLE IF NOT EXISTS requests(id INTEGER PRIMARY KEY, pageid INT, username TEXT, date TEXT, message TEXT, endpoint TEXT, file TEXT)"); /* history table * id (INTEGER PRIMARY KEY) * pageid (INT) * username (TEXT) * date (TEXT) * endpoint (TEXT) * file (TEXT) */ $Database->exec("CREATE TABLE IF NOT EXISTS history(id INTEGER PRIMARY KEY, pageid INT, username TEXT, date TEXT, endpoint TEXT, file TEXT)"); return $Database; } function removePrefix($prefix, $html) { return preg_replace("/$prefix.*/", "", $html); } function printFeed($ret, $subdir) { include "config.php"; $title = $ret->title; $desc = $ret->description; $pages = $ret->pages; $rss = ""; $rss .= "\n"; $rss .= "\n"; $rss .= "\t$title\n"; $rss .= "\t$desc\n"; $rss .= "\t\n"; $rDatabase = createTables($sqlDB); $rDatabaseQuery = $rDatabase->query('SELECT * FROM pages'); while ($rline = $rDatabaseQuery->fetchArray()) { foreach ($pages as $i => $it) { if ($rline['endpoint'] == $it) { // is our page $page = convertMarkdownToHTML(file_get_contents($rline['file'])); $ptitle = $page->title; $pdesc = $page->description; $pdata = $page->data; $pdate = $page->date; if ($pdate != "") { $pdate = date('r', strtotime($pdate)); } else { $pdate = "0"; } $rss .= "\t\n"; $rss .= "\t\t$ptitle\n"; $rss .= "\t\t$it\n"; $rss .= "\t\t$it\n"; $rss .= "\t\t$pdate\n"; $rss .= "\t\t\n"; $rss .= "\t\t\t\n"; $rss .= "\t\t\n"; $rss .= "\t\n"; } } } $rss .= "\n"; $rss .= ""; header('Content-type: application/xml'); print "$rss"; die(); } function printCommentField($html, $id, $pageID) { include "config.php"; $html .= "\t\t\t
\n"; $html .= "\t\t\t\t

Comment

\n"; if (isset($_SESSION['username'])) { $html .= "\t\t\t\t\t

Have anything to say? Feel free to comment it below:

\n"; $html .= "\t\t\t\t\t
\n"; $html .= "\t\t\t\t\t\t
\n"; $html .= "\t\t\t\t\t\t

\n"; $html .= "\t\t\t\t\t\t

\n"; $html .= "\t\t\t\t\t
\n"; } else { $html .= "\t\t\t\t\t

To post a comment, you must be logged in.

\n"; } // print the actual list $Database = createTables($sqlDB); $DatabaseQuery = $Database->query('SELECT * FROM comments'); while ($line = $DatabaseQuery->fetchArray()) { if ($line['page'] == $id) { $username = $line['username']; $date = $line['date']; $data = $line['data']; $cid = $line['id']; $html .= "\t\t\t\t\t
\n"; if ($line['usertype'] == 2) { $html .= "\t\t\t\t\t\t

$username on $date:\n"; if (isset($_SESSION['username']) && isset($_SESSION['type'])) { if ($line['username'] == htmlspecialchars($_SESSION['username']) || htmlspecialchars($_SESSION['type']) == 2) { $html .= "Remove

\n"; } } $html .= "\t\t\t\t\t\t

\n"; } else { $html .= "\t\t\t\t\t\t

$username on $date:\n"; if (isset($_SESSION['username']) && isset($_SESSION['type'])) { if ($line['username'] == htmlspecialchars($_SESSION['username']) || htmlspecialchars($_SESSION['type']) == 2) { $html .= "Remove

\n"; } } $html .= "\t\t\t\t\t\t

\n"; } $html .= "\t\t\t\t\t\t

$data

\n"; $html .= "\t\t\t\t\t
\n"; } } $html .= "\t\t\t
\n"; return $html; } function convertMarkdownToHTML($contents) { include "config.php"; $ret = new parsedMarkdown(); $parser = new MarkdownExtra; $parser->no_markup = false; $specialSyntax = array( '/.*@csgen\.title.*=.*"(.*)(");/', '/.*@csgen\.description.*=.*"(.*)(");/', '/.*@csgen\.favicon.*=.*"(.*)(");/', '/.*@csgen\.license.*=.*"(.*)(");/', '/.*@csgen\.date.*=.*"(.*)(");/', '/.*@csgen\.author.*=.*"(.*)(");/', '/.*@csgen\.tags.*=.*"(.*)(");/', '/.*@csgen\.addAuthor.*=.*"(.*)(");/', '/.*@csgen\.addSummary.*=.*"(.*)(");/', '/.*@csgen\.allowComments.*=.*"(.*)(");/', '/.*@csgen\.displayTitle.*=.*"(.*)(");/', '/.*@csgen\.displayDate.*=.*"(.*)(");/', '/.*@csgen\.displaySource.*=.*"(.*)(");/', '/.*@csgen\.displayAuthors.*=.*"(.*)(");/', '/.*@csgen\.displayLicense.*=.*"(.*)(");/', '/.*@csgen\.markAsFeed.*=.*"(.*)(");/', '/.*@csgen\.includePage.*=.*"(.*)(");/', '/.*@csgen\.redirectTo.*=.*"(.*)(");/', '/.*@csgen\.span.*<STYLE.*,.*TEXT>\(.*"(.*)".*, "(.*)"\);/', '/.*@csgen\.span.*<STYLE.*,.*HTML>\(.*"(.*)".*, "(.*)"\);/', '/.*@csgen\.inline.*<HTML>\(.*"(.*)"\);/', '/.*@csgen\.inline.*<CSS>\(.*"(.*)"\);/', '/.*@csgen\.inline.*<JAVASCRIPT>\(.*"(.*)"\);/', '/.*@csgen\.image.*<SIZE.*,.*PATH>\(.*"(.*)".*, "(.*)"\);/', '/.*@csgen\.div.*<START.*,.*NAME>\(.*"(.*)"\);/', '/.*@csgen\.div.*<END.*,.*NAME>\(.*"(.*)"\);/', '/.*@csgen\.div.*<STYLE.*,.*NAME>\(.*"(.*)".*, "(.*)"\);/', '/.*@csgen\.include.*<HTML>\(.*"(.*)"\);/', '/.*@csgen\.include.*<CSS>\(.*"(.*)"\);/', '/.*@csgen\.include.*<JAVASCRIPT>\(.*"(.*)"\);/', ); $out = $parser->transform($contents); $maxit = 1000; while ((preg_match('/.*@csgen.*;/', $out)) && $maxit > 0) { $maxit--; foreach ($specialSyntax as $pattern) { $matches = array(); if (preg_match($pattern, $out, $matches)) { switch ($pattern) { case '/.*@csgen\.title.*=.*"(.*)(");/': $ret->title = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.description.*=.*"(.*)(");/': $ret->description = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.favicon.*=.*"(.*)(");/': $ret->favicon = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.license.*=.*"(.*)(");/': $ret->license = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.date.*=.*"(.*)(");/': $ret->date = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.allowComments.*=.*"(.*)(");/': $ret->allowComments = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.displayTitle.*=.*"(.*)(");/': $ret->displayTitle = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.displayDate.*=.*"(.*)(");/': $ret->displayDate = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.displaySource.*=.*"(.*)(");/': $ret->displaySource = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.displayAuthors.*=.*"(.*)(");/': $ret->displayAuthors = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.displayLicense.*=.*"(.*)(");/': $ret->displayLicense = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.markAsFeed.*=.*"(.*)(");/': $ret->isFeed = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.addAuthor.*=.*"(.*)(");/': $ret->authors[] = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.addSummary.*=.*"(.*)(");/': $page = $matches[1]; $Database = createTables($sqlDB); $DatabaseQuery = $Database->query('SELECT * FROM pages'); while ($line = $DatabaseQuery->fetchArray()) { if ($line['endpoint'] != $page) { continue; } $converted = convertMarkdownToHTML(file_get_contents($line['file'])); $title = $converted->title; $description = $converted->description; $author_a = $converted->authors; $authors = ""; foreach ($author_a as $i => $it) { $authors .= $it; if (count($author_a) != $i + 1) { $authors .= ", "; } } $tags_a = $converted->tags; $tags = ""; foreach ($tags_a as $i => $it) { $tags .= $it; if (count($tags_a) != $i + 1) { $tags .= ", "; } } $date = $converted->date; $license = $converted->license; $text = "
\n"; if ($title != "") { $text .= "\t

$title

\n"; } else { $text .= "\t

" . $line['endpoint'] . "

\n"; } for ($i = 0; $i < 4; $i++) { $sep = " • "; if ($i == 0 && $authors != "") { $text .= "\tby $authors$sep\n"; continue; } else if ($i == 1 && $date != "") { $text .= "\t$date"; $sep = ""; } else if ($i == 2 && $tags != "") { $text .= "$sep\n"; $text .= "\t$tags"; $sep = ""; } else if ($i == 3 && $license != "") { $text .= "$sep\n"; $text .= "\t$license"; $text .= "\n"; } else if ($i == 3) { $text .= "\n"; } } if ($description != "") { $text .= "\t

$description

\n"; } $text .= "
\n"; $out = str_replace($matches[0], $text, $out); break; } break; case '/.*@csgen\.author.*=.*"(.*)(");/': $ret->authors[] = explode(',', $matches[1]); if (count($ret->authors) == 1) { $ret->authors = $ret->authors[0]; } $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.tags.*=.*"(.*)(");/': $ret->tags[] = explode(',', $matches[1]); if (count($ret->tags) == 1) { $ret->tags = $ret->tags[0]; } $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.includePage.*=.*"(.*)(");/': $ret->pages[] = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.redirectTo.*=.*"(.*)(");/': $ret->redirectTo = $matches[1]; $out = str_replace($matches[0], '', $out); break; case '/.*@csgen\.span.*<STYLE.*,.*TEXT>\(.*"(.*)".*, "(.*)"\);/': $cssCode = htmlspecialchars_decode($matches[1]); $out = str_replace($matches[0], "$matches[2]", $out); break; case '/.*@csgen\.span.*<STYLE.*,.*HTML>\(.*"(.*)".*, "(.*)"\);/': $cssCode = htmlspecialchars_decode($matches[1]); $htmlCode = htmlspecialchars_decode($matches[2]); $out = str_replace($matches[0], "$htmlCode", $out); break; case '/.*@csgen\.div.*<START.*,.*NAME>\(.*"(.*)"\);/': $out = str_replace($matches[0], "
", $out); break; case '/.*@csgen\.div.*<END.*,.*NAME>\(.*"(.*)"\);/': $out = str_replace($matches[0], "
", $out); break; case '/.*@csgen\.div.*<STYLE.*,.*NAME>\(.*"(.*)".*, "(.*)"\);/': $cssCode = htmlspecialchars_decode($matches[1]); $out = str_replace($matches[0], "\n
", $out); break; case '/.*@csgen\.inline.*<HTML>\(.*"(.*)"\);/': $htmlCode = htmlspecialchars_decode($matches[1]); $out = str_replace($matches[0], "$htmlCode", $out); break; case '/.*@csgen\.inline.*<CSS>\(.*"(.*)"\);/': $cssCode = htmlspecialchars_decode($matches[1]); $out = str_replace($matches[0], "", $out); break; case '/.*@csgen\.inline.*<JAVASCRIPT>\(.*"(.*)"\);/': $javascriptCode = htmlspecialchars_decode($matches[1]); $out = str_replace($matches[0], "", $out); break; case '/.*@csgen\.image.*<SIZE.*,.*PATH>\(.*"(.*)".*, "(.*)"\);/': $imgres = array(); if (preg_match('/([0-9]*)x([0-9]*)/', $matches[1], $imgres)) { $out = str_replace($matches[0], "", $out); } break; case '/.*@csgen\.include.*<HTML>\(.*"(.*)"\);/': if (file_exists($matches[1])) { $out = str_replace($matches[0], file_get_contents($matches[1]), $out); } break; case '/.*@csgen\.include.*<CSS>\(.*"(.*)"\);/': if (file_exists($matches[1])) { $out = str_replace($matches[0], "", $out); } break; case '/.*@csgen\.include.*<JAVASCRIPT>\(.*"(.*)"\);/': if (file_exists($matches[1])) { $out = str_replace($matches[0], "", $out); } break; } } } } $ret->data = htmlspecialchars_decode($out); return $ret; } function printHeader($html, $printpage) { include "config.php"; $pid = -1; $id = -1; if (isset($_REQUEST['id'])) { $id = htmlspecialchars($_REQUEST['id']); } $Database = createTables($sqlDB); $DatabaseQuery = $Database->query('SELECT * FROM pages'); $wasFound = 0; $title = $instanceName; $description = $instanceDescription; $subdir = ""; if (isset($_GET['endpoint'])) { $subdir = $_GET['endpoint']; } else if (isset($_SERVER['REQUEST_URI'])) { $subdir = '/' . trim(strtok($_SERVER['REQUEST_URI'], '?'), '/'); } else { $subdir = '/'; } while ($line = $DatabaseQuery->fetchArray()) { $endpoint = $line['endpoint']; if ((($endpoint == $subdir || "$endpoint/" == "$subdir") && $id == -1) || ($id != -1 && $printpage == 1)) { $pid = $line['id']; if ($pid != $id && $id != -1) { $pid = -1; continue; } $wasFound = 1; $ret = convertMarkdownToHTML(file_get_contents($line['file'])); $title = $ret->title; $description = $ret->description; $favicon = $ret->favicon; if ($title === "") { $title = $instanceName; } if ($description === "") { $description = $instanceDescription; } $html .= "\n"; $html .= "\n"; $html .= "\t\n"; $html .= "\t\t\n"; $html .= "\t\t\n"; if ($favicon != "") { $html .= "\t\t\n"; } else if (file_exists($Icon)) { $html .= "\t\t\n"; } if (file_exists($Stylesheet)) $html .= "\t\t\n"; if (file_exists($javaScript)) $html .= "\t\t\n"; $html .= "\t\t$title\n"; $html .= "\t\t
\n"; $endpointFound = 0; $HeaderDatabaseQuery = $Database->query('SELECT * FROM pages'); while ($head = $HeaderDatabaseQuery->fetchArray()) { if ($head['endpoint'] == "/_head") { $Header = convertMarkdownToHTML(file_get_contents($head['file'])); $endpointFound = 1; $html .= "\t\t$Header->data\n"; break; } } if ($endpointFound == 0) { if (file_exists($Logo)) $html .= "\t\t\t\n"; $html .= "\t\t\t$instanceName\n"; } $html .= "\t\t
\n"; $html .= "\t\t
\n"; $html .= "\t\t\t\n"; $html .= "\t\t\t\n"; $html .= "\t\t\t
\n"; $ListDatabaseQuery = $Database->query('SELECT * FROM pages'); while ($list = $ListDatabaseQuery->fetchArray()) { if ($list['endpoint'] == "/_list") { $List = convertMarkdownToHTML(file_get_contents($list['file'])); $html .= "\t\t\t\t$List->data\n"; } } if (isset($_SESSION['type']) && $_SESSION['type'] == 2) { $html .= "\t\t\t\tEdit\n"; } if (!isset($_SESSION['type'])) { if ($publicAccountCreation) { $html .= "\t\t\t\tRegister\n"; } $html .= "\t\t\t\tLog in\n"; } else { $Username = htmlspecialchars($_SESSION['username']); $html .= "\t\t\t\t$Username\n"; $html .= "\t\t\t\tLog out\n"; } if (isset($_SESSION['type']) && $_SESSION['type'] == 2) { $html .= "\t\t\t\tAdministration\n"; } $html .= "\t\t\t
\n"; $html .= "\t\t
\n"; $html .= "\t\n"; $html .= "\t\n"; $html .= "\t\t
\n

\n"; if ($printpage == 1) { if ($ret->redirectTo != '') { $path = $ret->redirectTo; header("Location: $path"); die(); } $License = $ret->license; $sourceFile = $line['file']; if ($ret->isFeed == "true") { printFeed($ret, $subdir); } if ($ret->displayTitle == "true" && $ret->title != "") { $html .= "\t\t\t

$ret->title

\n"; } if ($ret->displayDate == "true" && $ret->date != "") { $html .= "\t\t\t\t

$ret->date\n"; } $html .= "\t\t\t\t$ret->data\n"; if ($ret->displaySource == "true") { $html .= "\t\t\t\t\n"; } if (isset($_SESSION['type'])) { $html .= "\t\t\t\t\n"; } if ($ret->displayLicense == "true" && $License != '') { $html .= "\t\t\t\tThis page is licensed under the $License license."; } if ($ret->displayAuthors == "true" && $ret->authors) { $html .= "\t\t\t\t

Authors

\n"; $html .= "\t\t\t\t

"; foreach ($ret->authors as $i => $it) { $html .= "$it"; if (count($ret->authors) != $i + 1) { $html .= ", "; } } $html .= "\t\t\t\t

\n"; } if ($ret->allowComments == "true") { $html = printCommentField($html, $line['id'], $pid); } } break; } } if ($wasFound != 1) { $title = $instanceName; $description = $instanceDescription; $html .= "\n"; $html .= "\n"; $html .= "\t\n"; $html .= "\t\t\n"; $html .= "\t\t\n"; if (file_exists($Icon)) $html .= "\t\t\n"; if (file_exists($Stylesheet)) $html .= "\t\t\n"; if (file_exists($javaScript)) $html .= "\t\t\n"; $html .= "\t\t$title\n"; $html .= "\t\t
\n"; $endpointFound = 0; $HeaderDatabaseQuery = $Database->query('SELECT * FROM pages'); while ($head = $HeaderDatabaseQuery->fetchArray()) { if ($head['endpoint'] == "/_head") { $Header = convertMarkdownToHTML(file_get_contents($head['file'])); $endpointFound = 1; $html .= "\t\t$Header->data\n"; break; } } if ($endpointFound == 0) { if (file_exists($Logo)) $html .= "\t\t\t\n"; $html .= "\t\t\t$instanceName\n"; } $html .= "\t\t
\n"; $html .= "\t\t
\n"; $html .= "\t\t\t\n"; $html .= "\t\t\t\n"; $html .= "\t\t\t
\n"; $ListDatabaseQuery = $Database->query('SELECT * FROM pages'); while ($list = $ListDatabaseQuery->fetchArray()) { if ($list['endpoint'] == "/_list") { $List = convertMarkdownToHTML(file_get_contents($list['file'])); $html .= "\t\t\t\t$List->data\n"; } } if (isset($_SESSION['type']) && $_SESSION['type'] == 2) { $html .= "\t\t\t\tEdit\n"; } if (!isset($_SESSION['type'])) { if ($publicAccountCreation) { $html .= "\t\t\t\tRegister\n"; } $html .= "\t\t\t\tLog in\n"; } else { $Username = htmlspecialchars($_SESSION['username']); $html .= "\t\t\t\t$Username\n"; $html .= "\t\t\t\tLog out\n"; } if (isset($_SESSION['type']) && $_SESSION['type'] == 2) { $html .= "\t\t\t\tAdministration\n"; } $html .= "\t\t\t
\n"; $html .= "\t\t
\n"; $html .= "\t\n"; $html .= "\t\n"; $html .= "\t\t
\n"; if ($printpage == 1) { $ErrDatabaseQuery = $Database->query('SELECT * FROM pages'); $foundErrorPage = 0; while ($err = $ErrDatabaseQuery->fetchArray()) { if ($err['endpoint'] == "/_404") { $foundErrorPage = 1; $Err = convertMarkdownToHTML(file_get_contents($err['file'])); $html .= "\t\t\t$Err->data\n"; break; } } if ($foundErrorPage == 0) { $html .= "\t\t\t

404

\n\t\t\t\t

404: The page you requested could not be found.

\n"; } } } return $html; } function printFooter($html) { include "config.php"; $html .= "\t\t
\n"; $html .= "\t\n"; $html .= "\t
\n"; $html .= "\t\t\n"; $html .= "\t
\n"; $html .= "\n"; return "$html"; } function checkIfAdminExists() { include "config.php"; $adminExists = 0; $Database = createTables($sqlDB); $DatabaseQuery = $Database->query('SELECT * FROM users'); if (!is_dir($documentLocation)) mkdir($documentLocation, 0777, true); if (!is_dir($attachmentLocation)) mkdir($attachmentLocation, 0777, true); if (!is_dir($historyLocation)) mkdir($historyLocation, 0777, true); if (!is_dir($requestLocation)) mkdir($requestLocation, 0777, true); $adminExists = 0; while ($line = $DatabaseQuery->fetchArray()) { if ($line['usertype'] == 2) { $adminExists = 1; break; } } return $adminExists; } function getIPAddress() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { return $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { return $_SERVER['HTTP_X_FORWARDED_FOR']; } else { return $_SERVER['REMOTE_ADDR']; } } function getUserAgent() { return $_SERVER['HTTP_USER_AGENT']; } function truncateText($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 generatePassword($pwd) { return password_hash($pwd, PASSWORD_DEFAULT); } ?>