Document Archive
CLICK HERE FOR DEMO
The demo shows a way to create and organize documents using a simple flat-file data storage system. Document text is entered in markdown format, then converted to html. Both versions are saved in a text file 'records'.
Previous versions are saved after each edit and can be accessed if needed. Documents can be assigned to categories as well.
Files and Folders for this demo
index.php ▾
<?php
//Copyright (c) 2021, Susan V. Rodgers, Lila Avenue, LLC, lilaavenue@gmail.com
include ("inc/db-definitions.php");
include ("inc/db-functions.php");
$pageid = "home";
if (isset($_GET['page'])) {
$pageid = $_GET['page'];
}
if ($pageid !== "") {
if (!file_exists ("pages/". $pageid . ".php") ){
echo "This page does not exist";
}
else {
if ($pageid === 'print-document' || $pageid === 'blank-calendar') {
include ("pages/" . $pageid . ".php");
}
else {
include ("inc/header.php");
include ("pages/" . $pageid . ".php");
}
}
}
if ($pageid !== 'print-document') {
include ("inc/footer.php");
}
?>
print-document.php ▾
<?php
include ("inc/db-definitions.php");
include ("inc/db-functions.php");
$documentid = "";
if (isset ($_GET["document"])){
$documentid = filter_input(INPUT_GET, "document", FILTER_SANITIZE_STRING) ;
}
$documentrecord = readDatabaseRecord( "documents", $documentid);
?>
<!DOCTYPE html>
<html>
<head>
<base href = "http://localhost/JOURNAL/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href = "https://fonts.googleapis.com/css?family=Lato:300,300i,400,700" rel="stylesheet">
<link rel= 'stylesheet' type='text/css' href= 'inc/style.css'>
</head>
<body >
<div class = 'outerwrap'>
<div classs = 'main'>
<div class = 'content-column'>
<?php
echo nl2br ($documentrecord['html-content']);
?>
</div>
</div>
</div>
</body>
</html>
data
categories.txt ▾
recipes&&&letters&&&poetry
date-document-map.txt ▾
2020-12-31%#%letter-to-mayor%#%
&&&2021-01-17%#%letter-to-travelling-tabby%#%
&&&2021-01-17%#%marbled-rat-ribs%#%
&&&2021-09-02%#%the-litter-box%#%
&&&2021-09-04%#%braised-hummingbird-tongues%#%
&&&2021-09-04%#%chewy-chipmonk-breakfast-bars%#%
&&&2021-09-04%#%chocolate-mouse%#%
&&&2021-09-04%#%letter-to-wealthy-cats-magazine%#%
&&&2021-09-04%#%marbled-rat-ribs%#%
&&&2021-09-04%#%pickled-mouse-brain-salad%#%
inc
db-definitions.php ▾
<?php
$dl1 = "&&&";
$dl2 = "%#%";
$dbtables = array (
"documents" => array ("title", "html-content", "md-content", "category", "date"),
"document-versions" => array ("title", "html-content", "md-content", "category", "date")
);
?>
db-functions.php ▾
<?php
function checkForDelimiters ($array) {
$error = false;
global $dl1, $dl2;
foreach ($array as $item) {
if (! is_array($item)) {
if (strpos ($item, $dl1) !== false || strpos($item, $dl2) !== false) {
echo "<div class = 'error'>Invalid Characters - cannot include " . $dl1 . " or " . $dl2 . "</div>";
$error = true;
}
}
}
return $error;
}
function createRecordKey ($table, $newname) {
$newkey = "";
if ($newname === '') {
echo "<div class = 'error'>Missing Name</div>";
}
else {
if (strlen ($newname) > 50) {
echo "<div class = 'error'>Title must be less than 50 characters</div>";
}
else {
$newkey = str_replace(" ", "-", $newname);
$newkey = strtolower ($newkey);
$newkey = html_entity_decode($newkey, ENT_QUOTES);
$newkey = preg_replace('/[^A-Za-z0-9-]/', '', $newkey);
$newkey = preg_replace('/-+/', '-', $newkey);
if ($newkey === "") {
echo "<div class = 'error'>Invalid Record Name</div>";
}
else {
// CHECK THAT THIS RECORD DOESN'T ALREADY EXIST
$filename = "data/" . $table . "/" . $newkey . ".txt";
if (file_exists ($filename)) {
echo "<div class = 'error'>'" . $table . "' record with this name already exists</div>";
$newkey = "";
}
}
}
}
return $newkey;
}
function createKey ($name) {
$newname = "";
if ($name !== "") {
$newname = str_replace (" " , "-", $name);
$newname = html_entity_decode($newname, ENT_QUOTES);
$newname = strtolower ($newname);
$newname = preg_replace('/[^A-Za-z0-9-]/', '', $newname);
$newname = preg_replace('/-+/', '-', $newname);
if ($newname === "") {
echo "<div class = 'error'>Invalid name</div>";
}
}
return $newname;
}
function initializeRecord($table) {
//read a record in a multi-record table
global $dbtables;
global $dl1, $dl2;
//INITIALIZE TABLE TO EMPTY
$record = array();
foreach ($dbtables[$table] as $label) {
$record[$label] = "";
}
return $record;
}
function readDatabaseRecord($table, $recordid) {
//read a record in a multi-record table
global $dbtables;
global $dl1, $dl2;
//INITIALIZE TABLE TO EMPTY
$record = array();
foreach ($dbtables[$table] as $label) {
$record[$label] = "";
}
$filename = "data/" . $table . "/" . $recordid . ".txt";
if (file_exists($filename)) {
$string = file_get_contents ($filename);
$array1= explode ($dl1, $string);
foreach ($dbtables[$table] as $id => $label) {
$record[$label] = "";
if (array_key_exists ($id, $array1)) {
$record [$label] = $array1[$id];
}
}
}
return $record;
}
function writeDatabaseRecord ($table, $record, $recordid) {
global $dl1, $dl2;
$filename = "";
$filename = "data/" . $table . "/" . $recordid . ".txt";
$newstring = implode ($dl1, $record);
file_put_contents ($filename, $newstring);
}
function addMapEntry ($file, $key1, $key2) {
global $dl1, $dl2;
$string = file_get_contents ($file);
$array1 = explode ($dl1, $string);
$newentry = $key1 . $dl2 . $key2 . $dl2 . "\n";
array_push ($array1, $newentry);
$array1 = array_unique ($array1);
sort ($array1);
$string = implode ($dl1, $array1);
file_put_contents ($file, $string);
return $array1;
}
function removeFromMap ($map, $key0, $key1) {
global $dl1, $dl2;
$array1 = array();
$mapstring = file_get_contents ($map);
if ($key0 === "" && $key1 === "") {
file_put_contents ($map, "");
}
else {
$array1 = explode ($dl1, $mapstring);
foreach ($array1 as $id => $item1) {
$array2 = explode ($dl2, $item1);
if (array_key_exists (0, $array2) && array_key_exists (1, $array2)) {
if ($key0 !== "" && $key1 !== "") {
if ( $array2[0] === $key0 && $array2[1] === $key1 ) {
unset ($array1 [$id]);
}
}
else if ($key0 === "" && $key1 !== "") {
if ($array2[1] === $key1) {
unset ($array1 [$id]);
}
}
else if ($key0 !== "" && $key1 === "") {
if ($array2[0] === $key0) {
unset ($array1 [$id]);
}
}
}
}
$mapstring = implode ($dl1, $array1);
file_put_contents ($map, $mapstring);
}
return $array1;
}
function selectMapEntries ($maparray, $key0, $key1) {
//returns an array with selected key
global $dl1, $dl2;
$selectedarray = array();
foreach ($maparray as $item1) {
$array2 = explode ($dl2, $item1);
if (array_key_exists (0, $array2) && array_key_exists (1, $array2)) {
if ($key0 !== "" && $key1 === "") {
if ($array2[0] === $key0 ){
array_push ($selectedarray, $array2[1]);
}
}
else if ($key0 === "" && $key1 !== "") {
if ($array2[1] === $key1) {
array_push ($selectedarray, $array2[0]);
}
}
}
}
return $selectedarray;
}
function moveToTrash ($table, $recordid) {
$oldfilename = "data/" . $table . "/" . $recordid . ".txt";
$newfilename = "data/trash/" . $table . "----" . $recordid. ".txt";
if (file_exists ($oldfilename)) {
rename ($oldfilename, $newfilename);
}
}
function readArray ($filename){
global $dl1;
$array = array();
if (file_exists($filename)) {
$string = file_get_contents ($filename);
if ($string !== "") {
$array = explode ($dl1, $string);
}
}
return $array;
}
function writeArray ($filename, $array){
global $dl1;
$string = implode ($dl1, $array);
$string = trim ($string, $dl1);
file_put_contents ($filename, $string);
}
function removeNameFromArray ($filename, $name) {
global $dl1;
if (file_exists($filename)) {
$string = file_get_contents ($filename);
$array = explode ($dl1, $string);
foreach ($array as $id => $item) {
if ($item === $name) {
unset ($array [$id]);
}
}
array_values ($array);
$string = implode ($dl1, $array);
file_put_contents ($filename, $string);
}
}
function extractFromMap($map, $key) {
//returns a new array of either the the first or second elements
global $dl1, $dl2;
$mapstring = file_get_contents ($map);
$array1 = explode ($dl1, $mapstring);
$selectedarray = array();
foreach ($array1 as $item1) {
$array2 = explode ($dl2, $item1);
if ($key == 0) {
if (array_key_exists (0, $array2)) {
array_push ($selectedarray, $array2[0]);
}
}
else if ($key == 1) {
if (array_key_exists (1, $array2)) {
array_push ($selectedarray, $array2[1]);
}
}
}
return $selectedarray;
}
?>
footer.php ▾
<br><br><br>Copyright © <?php echo date('Y'); ?> Susan Rodgers, <a href = 'https://lilaavenue.com'>Lila Avenue</a><br><br><br><br>
</div>
</body>
</html>
header.php ▾
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel= 'stylesheet' type='text/css' href= 'inc/style.css'>
<title> Document Archive</title>
</head>
<body>
<div class = 'outerwrap <?php echo $pageid; ?> '>
<a class = 'return' href = '../../document-archive'>← Return</a><br>
<div class = 'header-column'>
<img src = 'data/images/ideas.jpg' alt = 'light bulb' />
<a class = 'site-title' href = 'index.php'>Miss Kitty's Document Archive</a>
</div>
style.css ▾
@font-face {
font-family: Elsie;
src: url('../fonts/Elsie-Regular.ttf');
}
@font-face {
font-family: Mulish;
src: url('../fonts/Mulish-Regular.ttf');
}
body {
font-size: 1em;
margin: 0 auto;
font-family: "Mulish", sans-serif;
box-sizing: border-box;
color: #3d2893;
}
h1, h2, h3 ,h4 {
text-align: center;
color: #4a4682;
}
h1, h2 {
color: purple;
}
h1 {
margin: 5px auto;
font-family: 'Elsie', cursive;
}
h4 {
text-align: left;
font-size: 20px;
}
a {
color:#652c77;
text-decoration: none;
}
a:hover {
color: darkmagenta;
}
a.site-title {
display: inline-block;
font-family: 'Elsie', cursive;
font-size: 30px;
vertical-align: top;
padding-top: 25px;
}
label {
color: purple;
}
b {
color: black;
font-size: .8em;
text-transform: uppercase;
font-family: "Roboto", sans-serif;
}
textarea, input {
max-width: 100%;
vertical-align: bottom;
font-size: 1em;
font-family: "Lato", sans-serif;
box-sizing: border-box;
border: 1px solid #4a4682;
padding: 5px;
background-color: #eee;
}
/** OUTERWRAP */
.outerwrap {
display: block;
margin: 0 auto;
max-width: 100%;
width: 1100px;
box-sizing: border-box;
padding: 20px;
text-align: center;
}
/** Header */
.header-column {
margin-bottom: 30px;
padding: 0 20px;
max-width: 100%;
}
.box {
border: 2px solid #f8d243;
padding: 20px;
margin: 0 10px 10px 20px;
background-color: #eee;
}
/**COLUMNS */
.full-column {
width: 100%;
text-align: left;
margin: auto;
}
.content-column, .sidebar-column, .half-column, .third-column, .full-column {
display: inline-block;
text-align: left;
box-sizing: border-box;
vertical-align: top;
max-width: 100%;
}
.full-column {
width: 100%;
}
.content-column {
width: 80%;
}
.sidebar-column {
width: 20%;
text-align: center;
padding-top: 20px;
}
.half-column {
width: 48%;
}
.third-column {
width: 30%;
}
.third-column a {
text-decoration: underline;
}
a.adminbutton, input.submitbutton {
color: white;
padding: 7px 12px;
margin: 10px auto;
font-weight: bold;
background-color: #4a4682;
text-align: center;
border-radius: 2px;
max-width: 100%;
border: 1px solid #bbb;
width: 160px;
display:inline-block;
}
input.submitbutton {
background-color: darkmagenta;
}
/** */
/** BREAKPOINTS */
/** */
@media only screen and (max-width: 800px) {
body {
font-size: .9em;
}
.header-column1 {
width: 100%;
text-align: center;
padding: 0;
}
.header-column2{
position: relative;
left: -40px;
}
.dropdownwrap {
left: 60%;
}
.content-column, .sidebar-column, .third-column, .two-thirds-column, .half-column {
width: 100%;
padding: 10px;
}
}
markdown
convert-markdown-blanklines.php ▾
<?php
$array1 = explode ("BLANKLINES:", $newstring);
if (count ($array1 ) > 1) {
$newstring1 = "";
foreach ($array1 as $id => $item1) {
if ($id === 0) {
$newstring1 = $item1;
}
else {
$count = 0;
if ( is_numeric (substr ($item1, 0,2))) {
$count = intval (substr ($item1,0,2));
//create string of blank lines
$lines = "";
for ($l = 0; $l < $count; $l++) {
$lines .= "BLANKLINE";
}
//Attach the blank lines to the beginning of the item
$newstring1 .= $lines . substr ($array1[$id], 2);
}
}
}
$newstring = $newstring1;
}
?>
convert-markdown-code.php ▾
<?php
$newstring = "";
function convertMD ($text) {
$text = trim ($text);
$textarray = explode ("STARTCODE", $text);
foreach ($textarray as $textitem) {
$pos1 = strpos ($textitem, 'ENDCODE') ;
if ($pos1) {
$codestring = substr ($textitem, 0, $pos1);
$codestring = htmlentities ($codestring);
$newstring .= "<div class = 'code-example'><pre><code>" . $codestring . "</code></pre>";
$noncode = substr ($textitem, $pos1 + 7);
$newstring .= convertMD ($noncodetring);
}
else {
$newstring .= convertMD ($textitem);
}
}
//echo htmlentities ($text);
$newstring = str_replace ("\n", "<br>", $text);
$newstring = str_replace ("\\*", "ESCAPEASTERIX", $newstring);
$newstring = str_replace ("\\-", "ESCAPEDASH", $newstring);
include ("admin/markdown/convert-markdown-headers.php");
include ("admin/markdown/convert-markdown-divs.php");
include ("admin/markdown/convert-markdown-ol.php");
include ("admin/markdown/convert-markdown-ul.php");
include ("admin/markdown/convert-markdown-inline.php");
if (strpos ($newstring, "![") !== false) {
include ("admin/markdown/convert-markdown-images.php");
}
if (strpos ($newstring, "[") !== false) {
include ("admin/markdown/convert-markdown-links.php");
}
include ("admin/markdown/convert-markdown-paragraphs.php");
$newstring = str_replace ("<br>", "", $newstring);
$newstring = str_replace ("ESCAPEASTERIX", "*", $newstring);
$newstring = str_replace ("ESCAPEDASH", "-", $newstring);
include ("admin/markdown/convert-markdown-blanklines.php");
$newstring = str_replace ("BLANKLINE", "<br>", $newstring);
include ("admin/markdown/convert-markdown-colors.php");
include ("admin/markdown/validate-HTML.php");
//return html string
return $newstring;
}
?>
convert-markdown-colors.php ▾
<?php
if (strpos ($newstring, 'CLR') !== false) {
global $color1, $color2, $color3;
$newstring = str_replace ("CLR1", "<span style = 'color:" . $color1 . ";'>", $newstring);
$newstring = str_replace ("CLR2", "<span style = 'color:" . $color2 . ";'>", $newstring);
$newstring = str_replace ("CLR3", "<span style = 'color:" . $color3 . ";'>",
$newstring);
$newstring = str_replace ("ENDCLR" , "</span>" , $newstring);
}
?>
convert-markdown-divs.php ▾
<?php
$newstring = str_replace ("ENDQUOTE", "</blockquote><br>", $newstring);
$newstring = str_replace ("QUOTE", "\n<blockquote><br>", $newstring);
$newstring = str_replace ("ENDBOX", "</div><br>", $newstring);
$newstring = str_replace ("BOX", "\n<div class = 'box'><br>", $newstring);
//Start new line after image
$newstring = str_replace ("BREAK", "\n<div style = 'clear: both; float: none;'></div><br>", $newstring);
//COLUMNS
$newstring = str_replace ("23COL1", "\n<div class = 'two-thirds-column-1'><br>", $newstring);
$newstring = str_replace ("23COL2", "</div><div class = 'two-thirds-column-2'><br>", $newstring);
$newstring = str_replace ("3COL3", "</div><div class = 'third-column-3'><br>", $newstring);
$newstring = str_replace ("3COL2", "</div><div class = 'third-column-2'><br>", $newstring);
$newstring = str_replace ("3COL1", "\n<div class = 'third-column-1'><br>", $newstring);
$newstring = str_replace ("2COL2", "</div><div class = 'half-column'><br>", $newstring);
$newstring = str_replace ("2COL1", "\n<div class = 'half-column'><br>", $newstring);
$newstring = str_replace ("ENDCOL", "</div><br>", $newstring);
?>
convert-markdown-headers.php ▾
<?php
$newarray = array();
$array = explode ("<br>", $newstring);
foreach ($array as $id => $item) {
$item = trim ($item);
if (substr ($item, 0, 5) === "#####"){
$array[$id] = "<h5>" . substr($item, 5) . "</h5>";
}
else if (substr ($item, 0, 4) === "####"){
$array[$id] = "<h4>" . substr($item, 4) . "</h4>";
}
else if (substr ($item, 0, 3) === "&&&") {
$array[$id] = "<h3>" . substr($item, 3) . "</h3>";
}
else if (substr ($item, 0, 2) === "##") {
$array[$id] = "<h2>" . substr($item, 2) . "</h2>";
}
else if (substr ($item, 0, 1) === "#") {
$array[$id] = "<h1>" . substr($item, 1) . "</h1>";
}
}
$newstring = implode ("<br>", $array);
?>
convert-markdown-images.php ▾
<?php
$newstring1 = "";
$array1 = explode ("![", $newstring);
if (count ($array1) > 1) {
foreach ($array1 as $id => $item) {
$item = trim ($item);
$newitem = $item;
if ($id === 0) {
//beginning of string
$newitem = $item;
}
else {
$newimagefield = "";
$lastpos = 0;
$pos1 = 0;
$pos2 = strpos ($item, "]");
$pos3 = strpos ($item, "(");
$pos4 = strpos ($item, ")");
//echo "HERE" . $pos1 . " " . $pos2 . " " . $pos3 . " " . $pos4;
$altlength = $pos2;
$srclength = $pos4 - $pos3;
$alt = substr ($item, $pos1 , $altlength);
$src = substr ($item, $pos3+1, $srclength-1);
//Determine width of image
$size = 'image-full';
if (preg_match("/SMALL/", $item) === 1) {
$size = 'image-small';
}
if (preg_match("/XSMALL/", $item) === 1) {
$size = 'image-xsmall';
}
else if (preg_match("/MEDIUM/", $item) === 1) {
$size = 'image-medium';
}
else if (preg_match("/LARGE/", $item) === 1) {
$size = 'image-large';
}
else if (preg_match("/XLARGE/", $item) === 1) {
$size = 'image-xlarge';
}
else if (preg_match("/FULL/", $item) === 1) {
$size = 'image-full';
}
//Determine image justification
$justify = "";
if (preg_match("/RIGHT/", $item) === 1) {
$justify = 'image-right';
}
else if (preg_match("/LEFT/", $item) === 1) {
$justify = 'image-left';
}
else if (preg_match("/CENTER/", $item) === 1) {
$justify = 'image-center';
}
$newimagefield .= "<img src = '" . $src . " ' alt = '" . $alt . "' class = '" . $justify . " " . $size . "' />";
$newitem = $newimagefield . substr ($item, $pos4 + 1 );
$newitem = str_replace ("LEFT", "", $newitem);
$newitem = str_replace ("RIGHT", "", $newitem) ;
$newitem = str_replace ("CENTER", "", $newitem) ;
$newitem = str_replace ("XSMALL", "", $newitem) ;
$newitem = str_replace ("SMALL", "", $newitem) ;
$newitem = str_replace ("MEDIUM", "", $newitem) ;
$newitem = str_replace ("XLARGE", "", $newitem) ;
$newitem = str_replace ("LARGE", "", $newitem) ;
$newitem = str_replace ("FULL", "", $newitem) ;
}
$newstring1 = $newstring1 . $newitem;
}
}
//Justify Images
$newstring = str_replace ("LEFT", "", $newstring);
$newstring = str_replace ("RIGHT", "", $newstring);
$newstring = $newstring1;
convert-markdown-inline.php ▾
<?php
//Convert bold and italic at at start of line
if (strpos ($newstring, "***", 0) !== false) {
$tag = 0;
$newstring1 = "";
$array1 = explode ("***", $newstring);
foreach ($array1 as $item) {
if ($tag === 0) {
$newstring1 .= $item;
$tag = 1;
//echo "<br><br>" . htmlentities ($newstring1);
}
else {
$newstring1 .= "<b><i>" . $item . "</i></b>" ;
$tag = 0;
}
}
$newstring = $newstring1;
//echo "<br>" . htmlentities ($newstring);
}
if (strpos ($newstring, "**", 0) !== false) {
$tag = 0;
$newstring1 = "";
$array2 = explode ("**", $newstring);
foreach ($array2 as $item) {
if ($tag === 0) {
$newstring1 .= $item;
$tag = 1;
//echo "<br><br>" . htmlentities ($newstring1);
}
else {
$newstring1 .= "<b>" . $item . "</b>" ;
$tag = 0;
}
}
$newstring = $newstring1;
// echo "<br>" . htmlentities ($newstring);
}
if (strpos ($newstring, "*", 0) !== false) {
$newstring1 = "";
$tag = 0;
$array1 = explode ("*", $newstring);
foreach ($array1 as $item) {
if ($tag === 0) {
$newstring1 .= $item;
$tag = 1;
//echo "<br><br>" . htmlentities ($newstring1);
}
else {
$newstring1 .= "<i>" . $item . "</i>" ;
$tag = 0;
}
}
$newstring = $newstring1;
// echo "<br>" . htmlentities ($newstring);
}
?>
convert-markdown-links.php ▾
<?php
$newstring1 = "";
$array1 = explode("[", $newstring);
if (count ($array1) > 1) {
foreach ($array1 as $id => $item) {
if ($id === 0) {
$newitem = $item;
}
else {
$linkfield = "";
$pos1 = 0;
$pos2 = strpos ($item, "]");
$pos3 = strpos ($item, "(");
$pos4 = strpos ($item, ")");
$namelength = $pos2;
$hreflength = $pos4 - $pos3;
$name = substr ($item, 0 , $namelength);
$href = substr ($item, $pos3+1, $hreflength -1);
$linkfield = "<a href = '" . $href . "'>" . $name . "</a>";
$newitem = $linkfield . substr ($item, $pos4+1);
}
$newstring1 = $newstring1 . $newitem;
}
}
$newstring = $newstring1;
?>
convert-markdown-ol.php ▾
<?php
$newstring1 = "";
$open = false;
$array1 = explode ("<br>", $newstring);
foreach ($array1 as $id => $item1) {
$item1 = trim($item1);
$pos1 = strpos ($item1, ".");
$first = substr ($item1, 0, $pos1 );
if (is_numeric ($first)) {
//this line part of numbered list
if ($open === false) {
//first list item - add the ol tag
$newstring1 .= "<br><ol>";
$open = true;
}
$newstring1 .= "<li>" . substr ($item1, $pos1 + 1) . "</li>";
}
else if ($open === true) {
$newstring1 .= "</ol><br>". $item1 ;
$open = false;
}
else {
$newstring1 .= $item1 . "<br>";
}
}
if ($open === true) {
$newstring1 .= "</ol><br>";
$open = false;
}
$newstring = $newstring1;
?>
convert-markdown-paragraphs.php ▾
<?php
//remove extra br tags at beginning and end
$array1 = explode ("<br>", $newstring);
$newarray = array();;
$breakcount = 0;
$ptag = 'closed';
foreach ($array1 as $id => $item) {
//Ignore all non-paragraph items
if (substr ($item, 0, 1) === '<' &&
substr ($item, 0, 3) !== "<i>" &&
substr ($item, 0, 3) !== "<b>" &&
substr ($item, 0, 5) !== "<span" &&
substr ($item, 0, 3) !== "<a>") {
if ($ptag === 'open') {
array_push ($newarray, "</p>\n" . $item);
$ptag = 'closed';
}
else {
array_push ($newarray, $item );
}
}
//Item contains paragraph text
else if ($item !== "") {
if ($ptag === 'closed') {
//Open paragraph
array_push ($newarray, "\n<p>" . $item);
$ptag = 'open';
}
else {
//paragraph already open, insert a blank line
array_push ($newarray, "BLANKLINE" . $item);
}
}
else {
//This is a blank line
$breakcount ++;
if ($breakcount > 0) {
//2 blank lines indicate a paragraph has ended
if ($ptag === 'open') {
array_push ($newarray , "</p>\n");
$ptag = 'closed';
$breakcount = 0;
}
}
}
}
if ($ptag === 'open') {
array_push ($newarray, "</p>\n");
}
//print_r ($newarray);
$newstring = implode ("<br>", $newarray);
?>
convert-markdown-ul.php ▾
<?php
$newstring1 = "";
$open = false;
$array1 = explode ("<br>", $newstring);
foreach ($array1 as $id => $item1) {
$item1 = trim ($item1);
if (substr ($item1, 0, 2) === "- " || substr ($item1, 0, 2) === "* " ) {
//List item
if ($open === false) {
//first list item1
$newstring1 .= "<br><ul>";
$open = true;
}
//Markdown character starts the line
$newstring1 .= "<li>" . substr ($item1, 2) . "</li>";
}
//not list item1
else if ($open === true) {
$newstring1 .= "</ul><br>" . $item1;
$open = false;
}
else {
$newstring1 .= $item1 . "<br>";
}
}
if ($open === true) {
$newstring1 .= "</ul><br>";
$open = false;
}
$newstring = $newstring1 ;
?>
convert-markdown.php ▾
<?php
function convertMD ($text, $color1, $color2, $color3) {
$text = trim ($text);
//echo htmlentities ($text);
$newstring = str_replace ("\n", "<br>", $text);
$newstring = str_replace ("\\*", "ESCAPEASTERIX", $newstring);
$newstring = str_replace ("\\-", "ESCAPEDASH", $newstring);
include ("markdown/convert-markdown-headers.php");
include ("markdown/convert-markdown-divs.php");
//include ("markdown/convert-markdown-ol.php");
include ("markdown/convert-markdown-ul.php");
include ("markdown/convert-markdown-inline.php");
if (strpos ($newstring, "![") !== false) {
include ("markdown/convert-markdown-images.php");
}
include ("markdown/convert-markdown-paragraphs.php");
if (strpos ($newstring, "[") !== false) {
include ("markdown/convert-markdown-links.php");
}
$newstring = str_replace ("<br>", "", $newstring);
$newstring = str_replace ("ESCAPEASTERIX", "*", $newstring);
$newstring = str_replace ("ESCAPEDASH", "-", $newstring);
include ("markdown/convert-markdown-blanklines.php");
$newstring = str_replace ("BLANKLINE", "<br>", $newstring);
include ("markdown/convert-markdown-colors.php");
include ("markdown/validate-HTML.php");
//return html string
return $newstring;
}
?>
markdown-instructions.php ▾
<div class = 'dropdownwrap'>
<div class = 'dropdownbutton' id = 'editing' onclick= 'accordionToggle(this.id)'>Formatting Instructions ▾</div>
<div id ='editing-content' class = 'dropdowncontent1' style = 'display: none; ' >
<div class = 'markdown-instructions'>
<b>#</b> Heading1 <br>
<b>##</b> Heading2 <br>
<b>&&&</b> Heading3 <br>
<b>####</b> Heading4 <br>
<b>#####</b> Heading5 <br>
<br>*<i>Italics</i>*
<br>**<b>Bold</b>**
<br> ***<i><b>Bold Italics</b></i>***
<br>
<br><b>Note: To show an asterisk in the text, use '\*' </b><br>
<br><b>QUOTE</b> Quoted text <b>ENDQUOTE</b>
<br><b>BOX</b> Text within box <b>ENDBOX</b>
<br><br><b>BREAK</b> Ends left or right justification after image
<br><b>BLANKLINE</b> Inserts a blank line
<br><b>BLANKLINES:04</b> Inserts 4 blank lines - use any number from 01 - 99
<br><br>
<b>Images:</b>
<br> ! [ image description ] ( image file )
<br> ! [image description ] ( image file )<b> RIGHT </b>
<br> ! [ image description ] ( image file )<b> LEFT</b>
<br> ! [ image description ] ( image file ) <b>LEFT SMALL</b>
<br><br>
<b>XSMALL</b> 100px <br>
<b>SMALL</b> 1080px <br>
<b>MEDIUM</b> 300px <br>
<b>LARGE</b> 450px <br>
<b>XLARGE</b> 700px <br>
<b>FULL</b> 100%
<br><br>
<b>Link</b>
<br> [ link name ] ( link url )
<br><br><b>Image Link</b>
<br> [ ! [image description] ( image file) ] (link url )
<br><b>Bullet List </b>* item (single asterisk followed by a space)
<br><b>Bullet List </b>- item (single dash followed by a space)
<br>
<br>
<b>CLR1</b> text to be colored <b>ENDCLR</b><br>
<b>CLR2</b> text to be colored <b>ENDCLR</b><br>
<b>CLR3 </b>text to be colored <b>ENDCLR</b><br>
<br>
<b>Columns: </b><br>
<b>2COL1</b>Text <b>2COL2</b> Text <b>ENDCOL</b><br>
<b>3COL1</b>Text <b>3COL2</b>Text<b> 3COL3</b>Text<b> ENDCOL </b><br>
<b>23COL1</b>Text<b>3COL3</b> Text <b>ENDCOL</b>
<b>3COL1</b>Text<b>23COL2</b> Text <b>ENDCOL</b>
</div>
</div>
</div>
validate-HTML.php ▾
<?php
$error = false;
$openbracketcount = substr_count($newstring,"<", 0);
$closingbracketcount = substr_count ($newstring, ">", 0);
//Checi for scripts
$tagarray = array();
$voidtags = array ("img", "br", "input", "hr", "col");
$tag = "";
$array1 = explode ("<", $newstring);
foreach ($array1 as $item) {
$item = trim($item);
//echo "<br>" . $item;
if ($item !== "") {
//Retrieve tag fromo string
$pos1 = strpos ($item, ">");
$tag = substr ($item, 0, $pos1);
$pos2 = strpos ($item, " ");
if ($pos2 !== false) {
$tag = substr ($tag, 0, $pos2);
}
if (! in_array ($tag, $voidtags)) {
array_push ($tagarray, $tag);
}
}
}
$opentagarray = array();
$previd = 0;
// print_r ($tagarray);
foreach ($tagarray as $item2) {
$item2 = trim ($item2);
if (strpos ($item2, "/") === false) {
array_push ($opentagarray, $item2);
// echo "<br>OPEN:" . $item2;
}
else {
$tag = str_replace ("/", "", $item2);
if ($tag === end($opentagarray)) {
//echo "<br>END: " . $item2;
array_pop ($opentagarray);
}
else {
// echo "TAG: " .$tag ;
$error = true;
}
}
}
if (count ($opentagarray) > 0) {
$error = true;
}
if ($error === true ) {
echo "<div style = 'background-color: white; color: red; padding: 20px; font-size: 18px;'>Invalid Syntax </div>";
}
?>
pages
add-remove-document-category.php ▾
<?php
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
if (isset ($_POST['newcategory'])) {
$newcategory = trim (FILTER_INPUT (INPUT_POST, "newcategory", FILTER_SANITIZE_STRING ));
$categoryarray = readArray("data/categories.txt");
$error = false;
checkForDelimiters($_POST);
if ($error === false) {
$newkey = createKey ($newcategory);
if ($newkey !== "") {
if (in_array($newkey, $categoryarray)) {
echo "<div class = 'error'>This category already exists</div>";
}
else {
array_push ($categoryarray, $newkey);
writeArray ("data/categories.txt", $categoryarray);
}
}
}
}
else if (isset ($_POST['removearray'])) {
$removearray = $_POST['removearray'];
foreach ($removearray as $item) {
removeNameFromArray ("data/categories.txt", $item);
}
}
}
?>
<div class = 'content-column'>
<div class = 'half-column'>
<h4>Existing Categories</h4>
<?php
$array1 = readArray("data/categories.txt");
foreach ($array1 as $item) {
echo $item . "<br>";
}
?>
<form method = 'post' action = 'index.php?page=add-remove-document-category'>
<h4>New Category Name</h4><br><input type = 'text' name = 'newcategory' />
<br> <input class = 'submitbutton' type = 'submit' name = 'submit' value='Update'>
</form>
</div><div class = 'half-column'>
<form method = 'post' action = 'index.php?page=add-remove-document-category'>
<h4>Remove Category</h4>
<?php
$array1 = readArray("data/categories.txt");
foreach ($array1 as $item) {
echo "<input type = 'checkbox' name = 'removearray[]' value = '" . $item . "' />" . $item . "<br>";
}
?>
<br> <input class = 'submitbutton' type = 'submit' name = 'remove' value='Remove'>
</form>
</div>
</div><div class = 'sidebar-column'>
<a class = 'adminbutton' href = 'index.php'>← Return</a>
</div>
add-update-document.php ▾
<?php
include ("markdown/convert-markdown.php");
$documentid = "";
if (isset ($_GET["document"])){
$documentid = filter_input(INPUT_GET, "document", FILTER_SANITIZE_STRING) ;
}
$documentrecord = readDatabaseRecord( "documents", $documentid);
$versionid = "";
if (isset ($_GET["version"])){
$versionid = filter_input(INPUT_GET, "version", FILTER_SANITIZE_STRING) ;
$documentid = substr ($versionid, 0, -19);
$documentrecord = readDatabaseRecord( "document-versions", $versionid);
}
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
//ASSIGN INPUT FIELDS TO RECORD FIELDS
foreach ($dbtables['documents'] as $label) {
if (isset ($_POST[$label])) {
$documentrecord [$label] = trim($_POST[$label]);
}
}
$documentrecord ['html-content'] = convertMD ($documentrecord['md-content'], "", "", "");
//Check if new document
if ($documentid === "") {
//New document - write record if either publish or draft
$documentid = createRecordKey ("documents", $documentrecord['title']);
}
if ($documentid !== "") {
writeDatabaseRecord("documents", $documentrecord, $documentid);
writeDatabaseRecord("document-versions", $documentrecord, $documentid ."-" . date ("Y-m-d-h-ia"));
addMapEntry ('data/date-document-map.txt', date("Y-m-d") , $documentid);
}
}
?>
<div class = 'content-column'>
<?php
if ($documentid === "") {
echo "<h2>Add Document</h2>";
}
else {
echo "<h2>Edit Document</h2>";
}
if ($versionid !== "") {
echo "<b>Id: </b>" . $versionid . "<br><br>";
}
else if ($documentid !== "") {
echo "<b>Id: </b>" . $documentid . "<br><br>";
}
?>
<form method = 'post' action = 'index.php?page=add-update-document&document=<?php echo $documentid; ?>' >
<label for= 'title-textarea'>Title: </label><br>
<textarea id = 'title-textarea' rows = '1' cols = '60' name = 'title'><?php echo $documentrecord ['title']; ?></textarea><br><br>
<?php
if ($documentrecord['date'] === "") {
$documentrecord ['date'] = date ("Y-m-d");
}
?>
<label for = 'date' >Date</label><br>
<input type = 'text' id = 'date' name = 'date' value = '<?php echo $documentrecord['date'];?>' /><br><br>
<label for= 'md-content-textarea'>Content: </label>
<textarea id = 'md-content-textarea' name = 'md-content' rows = '10' cols = '80'><?php echo $documentrecord['md-content']; ?></textarea> <br><br>
<h4>Category:</h4>
<?php
$array1 = readArray("data/categories.txt");
foreach ($array1 as $item) {
if ($documentrecord['category'] === $item) {
echo "<input id = '" . $item . "-id' type = 'radio' name = 'category' value = '" . $item . "' checked />";
}
else {
echo "<input id = '" . $item . "-id' type = 'radio' name = 'category' value = '" . $item . "' />";
}
echo "<label for = '" . $item . "-id'>" . $item . " </label>";
}
?>
<br><br>
<input type = 'submit' name = 'submit' value = 'Submit' />
</form>
</div><div class = 'sidebar-column'>
<a class = 'adminbutton' href = 'index.php?page=home'>Return </a><br>
<?php
if ($documentid !== "") {
echo "<a class = 'adminbutton ' href = 'index.php?page=display-document&document=" . $documentid . "'>View Document</a>";
echo "<a class = 'adminbutton' target = '_blank' href = 'print-document.php?document=" .$documentid . "'>Printable</a>";
echo "<a class = 'adminbutton' href = 'index.php?page=remove-document&document=" . $documentid . "'>Remove </a>";
echo "<a class = 'adminbutton' href = 'index.php?page=select-previous-document&document=" . $documentid . "'>Previous Versions</a>";
}
?>
</div>
display-document.php ▾
<div class = 'content-column'>
<br><a href = 'index.php'>← Return</a><br>
<?php
$documentid = "";
if (isset ($_GET["document"])){
$documentid = filter_input(INPUT_GET, "document", FILTER_SANITIZE_STRING) ;
}
$documentrecord = readDatabaseRecord( "documents", $documentid);
echo $documentrecord['html-content'];
?>
</div><div class = 'sidebar-column'>
<a class = 'adminbutton' href = 'index.php?page=home'>Return</a><br>
<?php
echo "<a class = 'adminbutton' href = 'index.php?page=add-update-document&document=" .$documentid . "'>Edit</a>";
echo "<a class = 'adminbutton' target = '_blank' href = 'print-document.php?document=" .$documentid . "'>Printable</a>";
?>
</div>
empty-trash.php ▾
<?php
$removeall = false;
$array1 = scandir ("data/trash");
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
if (isset ($_POST['trashfiles'])) {
$trashfiles = $_POST['trashfiles'];
foreach ($trashfiles as $item) {
if (! unlink("data/trash/" . $item)) {
echo "Error deleting " . $item . "<br>";
}
else {
echo $item . "Deleted<br>";
}
}
}
else if (isset ($_POST['removeflag'])) {
if ($_POST['removeflag'] === "REMOVE") {
$array1 = scandir ("data/trash");
foreach ($array1 as $item) {
if ($item !== "." && $item !== "..") {
unlink("data/trash/" . $item);
}
}
echo "<h2>Trash has been deleted</h2>";
}
}
}
?>
<h2>Empty Trash</h2>
<div class = 'content-column'>
<form method = 'post' action = 'index.php?page=empty-trash'>
<?php
if (count ($array1) > 2) {
echo "<h3>Remove All</h3>";
echo "NO: <input type = 'radio' name = 'removeflag' value = '' checked /> ";
echo "YES <input type = 'radio' name = 'removeflag' value = 'REMOVE' /> <br><br> ";
$array1 = scandir ("data/trash");
foreach ($array1 as $item) {
$item = trim ($item);
if ($item !== '.' && $item !== "..") {
echo "<input type = 'checkbox' name = 'trashfiles[]' value = '" . $item . "' />" . $item . "<br>";
}
}
}
else {
echo "Trash has been emptied";
}
?>
<br><br><input class = 'submitbutton' type = 'submit' name = 'submit' value='Delete'/>
</form>
</div><div class = 'sidebar-column'>
<br><a class = 'adminbutton' href = 'index.php'>← Return</a>
</div>
home.php ▾
<div class = 'content-column'>
<div class = 'half-column '>
<div class = 'box'>
<h3>All Documents</h3><br>
<?php
$array1 = scandir ("data/documents");
foreach ($array1 as $item1) {
if ($item1 !== '.' && $item1 !== "..") {
$documentid = str_replace (".txt", "", $item1);
$documentrecord = readDatabaseRecord ("documents", $documentid);
echo "<a href = 'index.php?page=display-document&document=" . $documentid . "'>" . $documentrecord['title'] . "</a><br>";
}
}?>
</div>
<div class = 'box'>
<h3>Documents by Date</h3><br>
<?php
$array1 = extractFromMap ("data/date-document-map.txt", 1);
$array1 = array_reverse ($array1);
foreach ($array1 as $documentid) {
$documentrecord = readDatabaseRecord ("documents", $documentid);
echo "<a href = 'index.php?page=display-document&document=" . $documentid . "'>" . $documentrecord['date'] . " " . $documentrecord['title'] . "</a><br>";
}
?>
</div>
</div><div class = 'half-column '>
<div class = 'box'>
<h3>Categories</h3>
<?php
$string = file_get_contents ("data/categories.txt") ;
$array1 = explode ($dl1, $string);
foreach ($array1 as $item1) {
echo "<h4>" . ucwords($item1) . "</h4>";
$array2 = scandir ("data/documents");
foreach ($array2 as $item2) {
if ($item2 !== '.' && $item2 !== "..") {
$documentid = str_replace (".txt", "", $item2);
$documentrecord = readDatabaseRecord ("documents", $documentid);
if ($documentrecord['category'] === $item1) {
echo "<a href = 'index.php?page=display-document&document=" . $documentid . "'>" . ucwords (str_replace ("-", " ", $documentid)) . "</a><br>";
}
}
}
}
?>
</div>
</div>
</div><div class = 'sidebar-column'>
<a class = 'adminbutton' href = 'index.php?page=add-update-document'>Add document</a>
<a class = 'adminbutton' href = 'index.php?page=add-remove-document-category'>Manage Categories</a>
<a class = 'adminbutton' href = 'index.php?page=remove-previous-versions'>Remove Preious Versions</a>
<a class = 'adminbutton' href = 'index.php?page=empty-trash'>Empty Trash</a>
</div>
remove-document.php ▾
<?php
echo "<div class = 'content-column'>";
$documentid = "";
if (isset ($_GET["document"])){
$documentid = filter_input(INPUT_GET, "document", FILTER_SANITIZE_STRING) ;
}
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
if (isset($_POST ['removeflag'])) {
$removeflag = trim($_POST['removeflag']);
if ($removeflag === "REMOVE") {
if (file_exists ("data/documents/" . $documentid . ".txt")) {
moveToTrash ("documents", $documentid);
removeFromMap ('data/date-document-map.txt', "", $documentid);
}
}
}
}
if (file_exists ("data/documents/" . $documentid . ".txt") || file_exists ("data/documents/" . $documentid . ".php")) {
echo "<h2>Remove document: " . $documentid . "</h2>";
?>
<form method = 'post' action = 'index.php?page=remove-document&document=<?php echo $documentid ;?>'>
<h3>Are you sure you want to move <?php echo $documentid ; ?> to the Trash Bin?</h3><br>
NO: <input type = 'radio' name = 'removeflag' value = '' checked />
YES <input type = 'radio' name = 'removeflag' value = 'REMOVE' />
<br><br><input class = 'submitbutton' type = 'submit' name = 'submit' value='Remove'/>
</form>
<?php
}
else {
echo "<h2>This document has been removed</h2>";
}
echo "</div><div class = 'sidebar-column'>";
echo "<a class = 'adminbutton' href = 'index.php?page=home'>Return </a><br>";
echo "</div>";
?>
remove-previous-versions.php ▾
<?php
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
if (isset ($_POST ['document-version-array'] )) {
$documentversionarray = $_POST['document-version-array'];
if (in_array ("all", $documentversionarray)) {
//REmove all versions
$array1 = scandir ("data/document-versions");
foreach ($array1 as $item1) {
if ($item1 !== "." && $item1 !== "..") {
$item1 = str_replace (".txt","", $item1);
moveToTrash ("document-versions", $item1);
}
}
}
else {
//Remove select versions
foreach ($documentversionarray as $item1) {
moveToTrash ("document-versions", $item1);
}
}
}
}
?>
<div class = 'content-column'>
<h3>Previous versions - Select items to be moved to trash</h3>
<form method = 'post' action = 'index.php?page=remove-previous-versions'>
<?php
echo "Remove All <input type = 'checkbox' name = 'document-version-array[]' value = 'all' /><br>";
$array1 = scandir ("data/document-versions");
foreach ($array1 as $id => $item1) {
if ($item1 !== "." && $item1 !== "..") {
$versionid = str_replace (".txt","", $item1);
echo "<br><a href = 'index.php?page=add-update-document&version=" . $versionid . "'><b>" . $versionid . "</b></a>";
echo "<label for = '" . $versionid . "'> Remove</label>";
echo "<input id = '" . $versionid . "' type = 'checkbox' name = 'document-version-array[]' value = '" . $versionid . "' />";
}
}
?>
<br><br><input class = 'submitbutton' type = 'submit' name = 'submit-delete-previous-versions' value='Submit'/>
</form>
</div><div class = 'sidebar-column'>
<a class = 'adminbutton' href = 'index.php?page=home'>Return</a>
</div>
select-previous-document.php ▾
<?php
$documentid = "";
if (isset ($_GET["document"])){
$documentid = filter_input(INPUT_GET, "document", FILTER_SANITIZE_STRING) ;
}
$array1 = scandir ("data/document-versions");
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
if (isset ($_POST['previous-versions'])) {
$previousversions = $_POST['previous-versions'];
foreach ($previousversions as $item1) {
moveToTrash ("document-versions", $item1);
}
$array1 = scandir ("data/document-versions");
}
}
?>
<div class = 'content-column'>
<h3>Select Previous Version </h3>
<form method = 'post' action = 'index.php?page=select-previous-document&document=<?php echo $documentid; ?>'>
<?php
foreach ($array1 as $item1) {
if ($item1 !== "." && $item1 !== "..") {
$version = str_replace (".txt", "", $item1);
$origdocumentid = substr ($version, 0, -19);
if ($origdocumentid === $documentid) {
echo "<a href = 'index.php?page=add-update-document&version=" . $version . "'>" . $version . "</a>";
echo " Remove: ";
echo "<input type = 'checkbox' name = 'previous-versions[]' value = '" . $version . "' /><br><br>";
}
}
}
?>
<input class = 'submitbutton' type = 'submit' name = 'submit' value = 'Submit' />
</form>
</div><div class = 'sidebar-column'>
<a class = 'adminbutton' href = 'index.php'>Return</a>
</div>