+
\ No newline at end of file
+
+?>
diff --git a/functions.php b/functions.php
index fd01171..7397158 100755
--- a/functions.php
+++ b/functions.php
@@ -27,46 +27,69 @@
/***** FILE HANDLING ********************************************************************/
function sort_by_mtime($file1,$file2) {
+
$time1 = filemtime($file1);
$time2 = filemtime($file2);
- if ($time1 == $time2) {
+
+ if ($time1 == $time2)
return 0;
- }
+
return ($time1 < $time2) ? 1 : -1;
+
}
+// if php version is low enough, simulate a file_put_contents function
if(!function_exists("file_put_contents")) {
+
define('FILE_APPEND', 1);
+
function file_put_contents($n, $d, $flag = false) {
+
$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
$f = @fopen($n, $mode);
+
if ($f === false) {
+
return 0;
+
} else {
- if (is_array($d)) {
+
+ if (is_array($d))
$d = implode($d);
- }
+
$bytes_written = fwrite($f, $d);
fclose($f);
return $bytes_written;
+
}
+
}
+
}
+// if php version is low enough, simlate a file_get_contents function
if(!function_exists("file_get_contents")) {
+
function file_get_contents($filename) {
+
return implode("\n", file($filename));
+
}
+
}
-function write_config($newcfg) {
-global $config, $root;
// writes all values in array $newcfg to config.php with keys as variable names, overwriting current values
// ($newcfg does not need all config options in it; this function will preserve those that are not changed)
// note that if passed string "true" or "false" it will convert to a boolean value, otherwise datatype will be preserved
// returns true on success; false otherwise
+function write_config($newcfg) {
+
+ global $config, $root;
+
$write = " $value) {
+
if(isset($newcfg[$option])) // overwrite current values with passed values, if they were passed
$newvalue = $newcfg[$option];
else // otherwise, use the current values
@@ -75,78 +98,108 @@ global $config, $root;
$newvalue = config_value($newvalue);
$write .= "\$config['" . config_key($option) . "'] = $newvalue;\n";
+
}
+
$write .= "\n?>";
return (bool)(file_put_contents($root . "config.php", $write)); // true on success; false otherwise
+
}
-function config_key($var) {
// returns a format of $var friendly to writing into a config file (as a variable name or key)
+function config_key($var) {
+
$disallowed = array(
"`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "+", "\t", "[",
"{", "]", "}", "\\", "|", ";", ":", "'", "\"", "\n", ",", "<", ".", ">", "/", "?"
);
+
$var = str_replace($disallowed, "", $var);
return $var;
+
}
-function config_value($var) {
// returns a format of $var friendly to writing into a config file (as a variable value), e.g.
// if $var is a boolean false, "false" (a string) will be returned;
// if $var is a string "banana", "\"banana\"" will be returned;
// if $var is an int 71, 71 will be returned (no change), etc.
// note that passing this function "false" (string) is the same as passing it false (boolean),
// likewise passing it "14" (string) is the same as passing it 14 (int)
- if(is_bool($var) || $var === "true" || $var === "false") {
- // if it's a boolean, make it a string so it will write as a boolean
- if($var === "true" || $var === "false") return $var;
- else return ($var ? "true" : "false");
+function config_value($var) {
+
+ // if it's a boolean, make it a string so it will write as a boolean
+ if(is_bool($var) || $var === "true" || $var === "false") {
+
+ if($var === "true" || $var === "false")
+ return $var;
+ else
+ return ($var ? "true" : "false");
+
}
- if(ctype_digit($var))
+
// if it only contains numeric digits, return its int value
+ if(ctype_digit($var))
return (int)$var;
- if(is_numeric($var))
+
// if it's a number but contains more than just numeric digits, return its double value
+ if(is_numeric($var))
return (double)$var;
- if(is_string($var))
+
// if it's a string, surround it with quotes
+ if(is_string($var))
return "\"$var\"";
+
exit("Invalid datatype: write_value($var)");
+
}
-function create_entry($title, $content, $type) {
-global $root;
// creates entry with title $title, content $content and of type $type
// returns true on success. exit()s otherwise.
+function create_entry($title, $content, $type) {
+
+ global $root;
+
$title = parse_title($title);
$content = parse($content);
- if(substr($type, -1) != "s") $type .= "s"; // change "doc" -> "docs" and "post" -> "posts"
-
+
+ // change "doc" -> "docs" and "post" -> "posts"
+ if(substr($type, -1) != "s")
+ $type .= "s";
+
if(file_exists($root . $type . "/" . $title . ".txt"))
error("A post with that name already exists.");
elseif(!file_put_contents($root . $type . "/" . $title . ".txt", $content))
error("Could not create file " . $type . "/" . $title . ".txt. Check permissions and try again.\n
It is also possible that you used an invalid character in your title (titles are used as filenames).");
+
return true;
+
}
-function delete_entry($title, $type) {
-global $root;
// attempts to delete entry $title of type $type ($type can be "post", "posts", "doc" or "docs")
// returns true on success; false otherwise
+function delete_entry($title, $type) {
+
+ global $root;
+
$title = str_replace("../", "", $title);
$title = str_replace("/", "", $title);
$title = str_replace("\\", "", $title);
$title = str_replace(" ", "_", $title);
- if(substr($type, -1) != "s") $type .= "s"; // change "doc" -> "docs" and "post" -> "posts"
+ // change "doc" -> "docs" and "post" -> "posts"
+ if(substr($type, -1) != "s")
+ $type .= "s";
+
return unlink($root . $type . "/" . $title . ".txt");
+
}
/***** TEMPLATING ***********************************************************************/
-function runtemplate($page, $vars = null) {
-global $config, $root;
// outputs code from $page in the current template using the variables contained in array $vars
+function runtemplate($page, $vars = null) {
+
+ global $config, $root;
$code = file_get_contents($root . "templates/" . $config['template'] . "/" . $page . ".html");
@@ -166,20 +219,30 @@ global $config, $root;
'/{IF:DOC}(.*?)\{\/IF:DOC}/is',
);
- if($config['docdates']) $vars_replace[] = '$1';
- else $vars_replace[] = '';
+ if($config['docdates'])
+ $vars_replace[] = '$1';
+ else
+ $vars_replace[] = '';
- if($config['docspagedates']) $vars_replace[] = '$1';
- else $vars_replace[] = '';
+ if($config['docspagedates'])
+ $vars_replace[] = '$1';
+ else
+ $vars_replace[] = '';
- if($config['showadmin']) $vars_replace[] = '$1';
- else $vars_replace[] = '';
+ if($config['showadmin'])
+ $vars_replace[] = '$1';
+ else
+ $vars_replace[] = '';
- if($vars['ENTRYTYPE'] == "post") $vars_replace[] = '$1';
- else $vars_replace[] = '';
+ if($vars['ENTRYTYPE'] == "post")
+ $vars_replace[] = '$1';
+ else
+ $vars_replace[] = '';
- if($vars['ENTRYTYPE'] == "doc") $vars_replace[] = '$1';
- else $vars_replace[] = '';
+ if($vars['ENTRYTYPE'] == "doc")
+ $vars_replace[] = '$1';
+ else
+ $vars_replace[] = '';
$code = preg_replace($vars_find, $vars_replace, $code);
// end conditionals
@@ -199,18 +262,25 @@ global $config, $root;
"admplugins" => array("listing", "page")
);
- foreach($hook_pages as $page => $locs)
+ foreach($hook_pages as $page => $locs) {
+
foreach($locs as $loc) {
+
if(isset($vars['id']))
$plug = plug($page, $loc, $vars['id']); // if it's a dynamic hook, pass plug() the id for it
else
$plug = plug($page, $loc); // if not, then don't
+
$code = str_ireplace("{HOOK:" . $page . "-" . $loc . "}", $plug, $code);
+
}
+ }
+
// end hook processing
print($code);
+
}
/***** PLUGINS **************************************************************************/
@@ -218,76 +288,116 @@ global $config, $root;
/***** FIND PLUGINS *****/
$plugging = false;
$rac[] = true; // mockup array so all our foreach()s don't fail if we don't find plugins
+
if(file_exists($root . "plugins/")) { // plugins dir is optional
+
$findmods = glob($root . "plugins/*.php");
+
+ // intialize our arrays so array_merge_recursive won't fail if they are not arrays by that time
if(count($findmods) != 0) {
- // intialize our arrays so array_merge_recursive won't fail if they are not arrays by that time
+
$rac = array();
$rack = array();
+
foreach($findmods as $weight => $pluginfile) {
+
include($pluginfile); // read the plugin ($rack will get set by the plugin during this time)
$rack = add_dimension($rack, $pluginfile); // turns $rack['posts']['post-body_after'] into $rack['posts']['post-body_after']['pluginfilename.php']
$rac = array_merge_recursive($rac, $rack); // merge with all other plugins
unset($rack); // remove all entries from $rack so that they don't overflow into the next plugin's array
+
}
+
}
+
}
-function plug($page, $hook, $id = false) {
-global $plugging, $rac;
// processes and prints plugins in page $page at position $hook
+function plug($page, $hook, $id = false) {
+
+ global $plugging, $rac;
+
$plugging = true;
- if(!$id) $output = "";
+
+ if(!$id)
+ $output = "";
+
if(is_array($rac[$page][$hook])) {
+
foreach($rac[$page][$hook] as $file => $html) {
+
include($file);
$output .= ($rack[$page][$hook]); // print refreshed html
+
}
+
}
- if($id) $output .= plug($page, $hook . "#" . $id); // dynamic plug
+
+ if($id)
+ $output .= plug($page, $hook . "#" . $id); // dynamic plug
+
$plugging = false;
return $output;
+
}
-function add_dimension($array, $dimension) {
// adds dimension $dimension to each element in array $array, e.g. $x['a']['b'] = y; becomes $x['a']['b']['c'] = y;
- if(!is_array($array)) return array($dimension => $array); // base case
+function add_dimension($array, $dimension) {
+
+ if(!is_array($array))
+ return array($dimension => $array); // base case
+
foreach($array as $key => $val)
$array[$key] = add_dimension($val, $dimension);
+
return $array;
+
}
-function set_config($plugin, $config) {
-global $root;
// writes all values in array $config to /plugins/config/$plugin.php with keys as variable names
// this function will OVERWRITE the whole file and start clean each time it is called,
// writing only the variables that it is given. this means it must be passed ALL variables every time.
// note that if passed string "true" or "false" it will convert to a boolean value, otherwise datatype will be preserved
// returns true on success; false otherwise
+function set_config($plugin, $config) {
+
+ global $root;
+
$plugin = config_key($plugin);
- if(!file_exists($root . "plugins/config")) mkdir($root . "plugins/config");
+
+ if(!file_exists($root . "plugins/config"))
+ mkdir($root . "plugins/config");
+
$write = " $value)
$write .= "\$" . $plugin . "['" . $option . "'] = " . config_value($value) . ";\n";
+
$write .= "\n?>";
if(!file_put_contents($root . "plugins/config/$plugin.php", $write))
return false;
+
return true;
+
}
-function get_config($plugin) {
// returns an array containing all config variables for plugin $plugin,
// with variable names as the key and variable values as the value
+function get_config($plugin) {
+
$plugin = config_key($plugin);
include($root . "plugins/config/" . $plugin . ".php");
+
return $$plugin; // variable variables: http://php.net/manual/en/language.variables.variable.php
+
}
/***** PARSING **************************************************************************/
-function parse($string) {
// returns a parsed version of $string for use in entry content; removes any php code and applies bbcode
+function parse($string) {
+
$string = str_replace("", "<?", $string);
$string = str_replace("?>", "?>", $string);
$string = str_replace("\\", "", $string);
@@ -317,11 +427,14 @@ function parse($string) {
// end bbcode
return $string;
+
}
-function parse_title($string) {
// returns a parsed version of $string for use as an entry filename
+function parse_title($string) {
+
$string = parse($string);
+
if(strpos($string, "_") ||
strpos($string, "/") ||
strpos($string, "\\")||
@@ -333,31 +446,44 @@ function parse_title($string) {
strpos($string, "<") ||
strpos($string, ">") ||
strpos($string, "../")
- ) exit("Invalid characters in title.");
+ )
+ exit("Invalid characters in title.");
+
$string = str_replace(" ", "_", $string);
$string = str_replace("../", "", $string);
+
return $string;
+
}
-function deparse_title($string) {
// inverse function of parse_title()
+function deparse_title($string) {
+
return str_replace("_", " ", $string);
+
}
-function sanitize($string) {
// cleans $string of any attempts to access things it shouldn't
+function sanitize($string) {
+
$string = str_replace("../", "", $string);
$string = htmlspecialchars($string);
return $string;
+
}
-function error($string, $exit = true) {
// outputs $string in error format and stops page execution if $exit is true
+function error($string, $exit = true) {
+
print("ERROR: " . $string . " ");
+
if($exit) {
+
runtemplate("footer", array()); // print footer
exit();
+
}
+
}
?>
diff --git a/index.php b/index.php
index 434321e..4402a57 100755
--- a/index.php
+++ b/index.php
@@ -42,43 +42,59 @@ $root = "./"; // so functions.php knows where we are
/***** PREPERATIONS *********************************************************************/
/***** CHECK FOR INSTALLATION *****/
-if(!file_exists("config.php")) $need_install = true;
-else require_once("config.php");
+if(!file_exists("config.php"))
+ $need_install = true;
+else
+ require_once("config.php");
/***** CHECK FOR REQUIRED FILES/DIRECTORIES *****/
if(!$need_install) {
-// only check for these paths if we're already installed
+
$required_paths = array("admin/", "admin/index.php", "templates/", "posts/", "docs/", "functions.php");
$success = true;
+
foreach($required_paths as $path) {
+
if(!file_exists($path)) {
+
error("File or directory $path is missing.", false);
$success = false;
+
}
+
}
+
if(!$success)
exit(" Either remove config.php to perform a fresh install, or replace any missing files/directories above.");
+
}
/***** HEADER *****/
if($need_install)
exit("It looks like you haven't installed kure yet! Proceed to installation if you need to install. If you don't, be sure to make sure your Kure-related directories exist.\n");
-else runtemplate("header");
+else
+ runtemplate("header");
/***** DISPLAY **************************************************************************/
/***** DOCS *****/
if(isset($_GET['docs'])) { // if we're at the docs page
+
runtemplate("doclist_header");
$alldocs = glob("docs/*.txt");
- if(!$config['abcdocs']) { // if we're NOT sorting alphabetically
+
+ if(!$config['abcdocs']) // if we're NOT sorting alphabetically
usort($alldocs, "sort_by_mtime");
- }
+
if(count($alldocs) == 0) {
+
print("<no docs to display>");
+
} else {
+
foreach($alldocs as $file) {
+
$title = $file;
$title = str_replace("docs/", "", $title);
$title = str_replace(".txt", "", $title);
@@ -86,25 +102,39 @@ if(isset($_GET['docs'])) { // if we're at the docs page
$id = md5($title); // dynamic hook identifier
$title = deparse_title($title);
runtemplate("doclist", array("id" => $id, "DOCTITLE" => $title, "DOCADDRESS" => $uftitle, "DOCDATE" => date("F jS, Y", filemtime($file))));
+
}
+
}
+
runtemplate("doclist_footer");
+
}
/***** VIEWPOST/VIEWDOC *****/
elseif(isset($_GET['post']) || isset($_GET['doc'])) { // if a post/doc has been requested
+
if(isset($_GET['post'])) {
+
$type = "post";
$filename = $_GET['post'];
+
} else {
+
$type = "doc";
$filename = $_GET['doc'];
+
}
+
$filename = sanitize($filename);
print plug($type, "top");
+
if(!file_exists($type . "s/" . $filename . ".txt")) {
+
print("The requested file " . $type . "s/" . $filename . ".txt does not exist.\n");
+
} else {
+
$file = $type . "s/" . $filename . ".txt";
$title = $file;
$title = str_replace($type . "s/", "", $title);
@@ -114,36 +144,52 @@ elseif(isset($_GET['post']) || isset($_GET['doc'])) { // if a post/doc has been
$content = str_replace("\n", " \n", file_get_contents($file));
runtemplate("entry", array("ENTRYTYPE" => $type, "ENTRYTITLE" => $title, "ENTRYADDRESS" => $uftitle, "ENTRYDATE" => date("F jS, Y", filemtime($file)), "ENTRYCONTENT" => $content));
- }
+
+ }
+
}
/***** POSTS *****/
else { // if we weren't told to do anything else
+
runtemplate("postlist_header");
+
if(!isset($_GET['page']))
$_GET['page'] = 1; // default to page 1
+
$allposts = glob("posts/*.txt");
$total_posts = count($allposts);
- if($total_posts % $config['num_posts'] != 0) {
+
// if the total number of posts isn't divisible by the number we want to display,
// then we want to make $total_posts / $config['num_posts'] round up one. (think it out.) this is for pagination.
+ if($total_posts % $config['num_posts'] != 0)
$total_posts += $config['num_posts'];
- }
+
if(!$config['abcposts']) // if we're NOT sorting alphabetically
usort($allposts, "sort_by_mtime");
+
$page_firstpost = ($_GET['page'] * $config['num_posts']) - $config['num_posts'];
$curpost = 0;
$i = 0; // monitor how many posts we display
+
if(count($allposts) == 0) {
+
print("<no posts to display>");
+
} else {
- foreach($allposts as $file) {
+
+ foreach($allposts as $file) {
+
if($i == $config['num_posts'] && $config['num_posts'] != 0)
break;
+
if(isset($_GET['page']) && ($curpost < $page_firstpost) || ($curpost > ($page_firstpost + $config['num_posts']))) {
+
$curpost++;
continue;
+
}
+
$title = $file;
$title = str_replace("posts/", "", $title);
$title = str_replace(".txt", "", $title);
@@ -155,27 +201,36 @@ else { // if we weren't told to do anything else
runtemplate("postlist", array("id" => $id, "POSTTITLE" => $title, "POSTADDRESS" => $uftitle, "POSTDATE" => date("F jS, Y", filemtime($file)), "POSTCONTENT" => $content));
$i++;
+
}
+
}
if($config['num_posts'] != 0 && $total_posts > $config['num_posts']) {
- if($_GET['page'] + 1 <= $total_posts / $config['num_posts']) {
+
+ if($_GET['page'] + 1 <= $total_posts / $config['num_posts'])
print("<<previous posts\n");
- }
+
if($_GET['page'] != 1) {
- if($_GET['page'] == 2) {
+
// we check if we're on page 2, because the next page will be 1, aka homepage.
+ if($_GET['page'] == 2)
$next = "./";
- } else {
+ else
$next = "?page=" . ($_GET['page'] - 1);
- }
+
print("| more recent posts>>\n");
+
}
+
}
+
runtemplate("postlist_footer");
+
}
/***** DISASSEMBLY *****/
if(!isset($_GET['install']) && !$need_install) // if we're not installing
runtemplate("footer");
+
?>