<?php
-if (!is_dir($entry['comments_loc'])) {
- if (!mkdir($entry['comments_loc'], 0775, true)) {
- echo 'Error making comments directory';
+class Comments
+{
+ var $comments = array();
+ var $comments_loc = '';
+ var $fileInfo = null;
+
+
+ function __construct( &$fileInfo ) {
+ $this->fileInfo = $fileInfo;
+ $this->comments = $this->get_comments( $fileInfo );
+ $this->comments_loc = get_comments_location($fileInfo);
}
-}
-global $captcha_publickey, $captcha_privatekey;
-# the response from reCAPTCHA
-$resp = null;
-# the error code from reCAPTCHA, if any
-$error = null;
+ /**
+ * get comments
+ * @param fileInfo the fileinfo object
+ * @return array() comments
+ */
+ function get_comments(&$fileInfo)
+ {
+ $comments_location = get_comments_location($fileInfo);
+ $comments = array();
+ if (is_dir($comments_location)) {
+ $dir_iterator = new DirectoryIterator($comments_location);
+ foreach ($dir_iterator as $file => $info) {
+ if (!$info->isDir() && !$info->isDot()) {
+ $contents = file_get_contents($info->getPath() . DIRECTORY_SEPARATOR . $info->getFilename());
+ $comments[] = json_decode($contents, true);
+ }
+ }
+ }
+ return $comments;
+ }
-# was there a reCAPTCHA response?
-if (isset($_POST["recaptcha_response_field"])) {
- $resp = recaptcha_check_answer ($captcha_privatekey,
- $_SERVER["REMOTE_ADDR"],
- $_POST["recaptcha_challenge_field"],
- $_POST["recaptcha_response_field"]);
- if ($resp->is_valid) {
- $name = !empty($_POST['name']) ? $_POST['name'] : 'anon';
- $www = !empty($_POST['www']) ? $_POST['www'] : null;
- $comment = !empty($_POST['comment']) ? $_POST['comment'] : null;
+ function create_comments_dir( &$dir )
+ {
+ try {
+ if (!mkdir($dir, 0777, true)) {
+ throw new Exception('Error creating comments directory');
+ }
+ } catch (Exception $e) {
+ echo 'Caught exception', $e->getMessage(), "\n";
+ }
+ }
- if (!$comment) {
- echo '<div class="error">You must enter a comment.</div>';
+ function process_post_request()
+ {
+ $this->_recaptcha_post_request();
+ }
- } else {
- $comment = json_encode(array(
- 'name' => $name,
- 'www' => $www,
- 'timestamp' => date('U'),
- 'comment' => $comment));
- file_put_contents($entry['comments_loc'] . date('U') . '.json', $comment);
+ # the response from reCAPTCHA
+ var $_recaptcha_resp = null;
+ # the error code from reCAPTCHA, if any
+ var $_recaptcha_error = null;
+ #error making comments
+ var $_comment_error = null;
- }
- } else {
- # set the error code so that we can display it
- $error = $resp->error;
+ function recaptcha_html()
+ {
+ global $captcha_publickey, $captcha_privatekey;
+ if ($this->_comment_error)
+ echo '<div class="error">', $this->_comment_error, '</div>';
+ echo recaptcha_get_html($captcha_publickey, $this->_recaptcha_error);
}
-}
-echo recaptcha_get_html($captcha_publickey, $error);
+
+ function _recaptcha_post_request() {
+
+ global $captcha_publickey, $captcha_privatekey;
+
+ # was there a reCAPTCHA response?
+ if (isset($_POST["recaptcha_response_field"])) {
+
+ $this->_recaptcha_resp = recaptcha_check_answer ($captcha_privatekey,
+ $_SERVER["REMOTE_ADDR"],
+ $_POST["recaptcha_challenge_field"],
+ $_POST["recaptcha_response_field"]);
+ if ($this->_recaptcha_resp->is_valid) {
+
+ $new_comment = array();
+ $new_comment['name'] = !empty($_POST['name']) ? $_POST['name'] : 'anon';
+ $new_comment['www'] = !empty($_POST['www']) ? $_POST['www'] : null;
+ $new_comment['comment'] = !empty($_POST['comment']) ? $_POST['comment'] : null;
+ $new_comment['timestamp'] = date('U');
+
+ if (!$new_comment['comment']) {
+
+ $this->_comment_error = 'You must enter a comment.';
+
+ } else {
+
+ if (!is_dir($this->comments_loc)) {
+ $this->create_comments_dir($this->comments_loc);
+ }
+
+ # put new comment
+ if (!file_put_contents($this->comments_loc . date('U') . '.json', json_encode($new_comment))) {
+ $_comment_error = 'error creating comment';
+ }
+
+ # all comments once new one is created
+ $this->comments = $this->get_comments( $this->fileInfo );
+ }
+
+ } else {
+ # set the error code so that we can display it
+ $this->_recaptcha_error = $this->_recaptcha_resp->error;
+ }
+ }
+ }
+}
*/
function get_pages()
{
- $path = LOCAL_ROOT . PAGE_DIR;
- $dir_iterator = new DirectoryIterator($path);
+ $dir_iterator = new DirectoryIterator(LOCAL_ROOT . PAGE_DIR);
$pages = array();
foreach($dir_iterator as $page) {
if ($page->isDir()) continue;
}
+
+
/**
* @param splFileInfo SPLFileInfo Object
* @param page default is false
$content .= $line;
}
- $f = array();
-
- $cat = clean_slashes(str_replace(LOCAL_ROOT . CONTENT_DIR, "", $fileInfo->getPath()));
- $clean_path = str_replace(LOCAL_ROOT . CONTENT_DIR, "", clean_slashes($fileInfo->getPath()));
+ $clean_path = get_clean_path($fileInfo);
+ $f = array();
$f['config'] = parse_ini_string($config);
$f['title'] = isset($f['config']['title']) ? $f['config']['title'] : $fileInfo->getFilename() ;
$f['config']['date'] = isset($f['config']['date']) ? $f['config']['date'] : null;
$f['timestamp'] = $f['config']['date'] ? date('U', strtotime( $f['config']['date'])) : $fileInfo->getCTime();
$f['tags'] = isset($f['config']['tags']) ? explode(" ", $f['config']['tags']) : null;
- $f['content'] = Markdown($content);
- // get comments
$f['comments_enabled'] = isset($f['config']['comments']) && $f['config']['comments'];
- $f['comments_loc'] = LOCAL_ROOT . COMMENTS_DIR . $clean_path . DIRECTORY_SEPARATOR . $fileInfo->getFilename() . DIRECTORY_SEPARATOR;
- $comments = array();
- if (is_dir($f['comments_loc'])) {
- $dir_iterator = new DirectoryIterator($f['comments_loc']);
- foreach ($dir_iterator as $file => $info) {
- if (!$info->isDir() && $info->getFilename() != '.' && $info->getFilename() != '..') {
- $contents = file_get_contents($info->getPath() . DIRECTORY_SEPARATOR . $info->getFilename());
- $comments[] = json_decode($contents, true);
- }
- }
- }
- $f['comments'] = $comments;
+ $f['comments'] = new Comments($fileInfo);
+ $f['content'] = Markdown($content);
+
if ($passed_more)
$f['content_short'] = Markdown($content_short);
}
+function get_comments_location ( &$fileInfo ) {
+ return LOCAL_ROOT . COMMENTS_DIR . get_clean_path($fileInfo) . DIRECTORY_SEPARATOR . $fileInfo->getFilename() . DIRECTORY_SEPARATOR;
+}
+
+function get_clean_path ( &$fileInfo, $page = false ) {
+ if (!$page) {
+ return str_replace(LOCAL_ROOT . CONTENT_DIR, "", clean_slashes($fileInfo->getPath()));
+ } else {
+ return str_replace(LOCAL_ROOT . PAGE_DIR, "", clean_slashes($fileInfo->getPath()));
+ }
+}
+
function get_entry ( $relative_path )
{
return parse_entry(new SplFileInfo(join(array(LOCAL_ROOT, CONTENT_DIR, $relative_path), DIRECTORY_SEPARATOR)));