From 657c27ed9e0c94f9561f7f93ac0d25f235862766 Mon Sep 17 00:00:00 2001 From: Gabriel Dunne Date: Fri, 11 Nov 2011 15:37:06 +0530 Subject: [PATCH] commenting with recaptcha --- content/.DS_Store | Bin 6148 -> 0 bytes content/categori1/.DS_Store | Bin 6148 -> 0 bytes css/comments.css | 5 +- index.php | 5 +- lib/comments.php | 137 +++++++++++++++++++++++++++--------- lib/data.php | 40 +++++------ lib/init.php | 1 + lib/model.php | 14 +++- lib/output.php | 2 +- templates/comments.html.tpl | 12 ++-- templates/entry.html.tpl | 8 +-- 11 files changed, 154 insertions(+), 70 deletions(-) delete mode 100644 content/.DS_Store delete mode 100644 content/categori1/.DS_Store diff --git a/content/.DS_Store b/content/.DS_Store deleted file mode 100644 index 16b6615e61e3baf4a802e09d4ca7e26ade6f824f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5T3PF6I94W=yCB}q_tvu@DM@;k3tJQSka^@HV~T9r1Vg$!DsM2d>fy~ zncWRo40scuY ziTjREzI=Z=3*sa|?|U$dqcm$YKEzI?y1Tb;*|v2!97NMFS$fH08m70s zNwVxk;W+T;cS_IvY>|5YTzPK%Y=R!8_md<>eQ2TwQJ78co!V-3(Q4M^S-Z2Y%T>GG zsLS(KXT7#9%XZ$Z%RZAetyX{ivkH(a%8Z8S5!(03e!;>;Mzr65zfdAFayj0 zGq8OOn6tn-*uK)aVrGCD_<;f1A0#$H*I=$uZ5`Or^_l!tLK4*JEkP&^x(0KN=s^)W z6;Y=O^TZH39qrP@xdwBMIvs>s8Rs!87mpXAR!6&3;UHX%+%p5rz$OC~J#^^&zrtUp z^pU@r!XsvY8Te-mh-$aj^{^;&wtg#*&RU7>0UHU$<)omXzHte_0qr9-<xyM WbB#0$*;P6sUj!5(+%W^cz`!T4YDxD1 diff --git a/content/categori1/.DS_Store b/content/categori1/.DS_Store deleted file mode 100644 index bcb3407b4e683c96f02e9102345b6202a1cc9022..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~J&pn~427Q;kXE9h+%gRZU{4SsH~|OXw?v}Y-9AU>*>S@_jYg|y$$7C8&(Bv( z#sF;ncUb`o0H1VMEPWW5F`nU!BYv^R_vPn&J>70sPvax^dO+tj9@leOA_5{H0wN#+ zA}}HXaftK$pO5I7^e7@A0^=ay--kkXt*NbRd^$Kp3qYMR9maX|64c@eYE5ljnW0&B z50<4CZHVWioLX{UO>JF!IV_tG%R8HQF*NJtu)=_5HAI65h`@lrq{jiOXb=6^m?8@WY*`6PL1sx9)1EC_)+{t5950A3ALuSuFTN%BM=xg Jh`?J3+yO<~69E7K diff --git a/css/comments.css b/css/comments.css index d517577..6fac0a8 100644 --- a/css/comments.css +++ b/css/comments.css @@ -11,11 +11,12 @@ color:#900; } .comments .comment { + color:#aaa; margin:20px 0; - font-style:italic; } .comments .comment .name { - color:#aaa; + font-style:italic; + color:#ccc; } .comments .comment_form { margin-top:40px; diff --git a/index.php b/index.php index 62b7c6d..c467ad7 100644 --- a/index.php +++ b/index.php @@ -4,9 +4,8 @@ require 'lib/init.php'; $request = get_request(); -$v = new View ($request); -$m = new Model ($request); - +$v = new View ( $request ); +$m = new Model ( $request ); if ($m->is_single()) $v->assign('entry', $m->entry); diff --git a/lib/comments.php b/lib/comments.php index 1b17db3..c413a0c 100644 --- a/lib/comments.php +++ b/lib/comments.php @@ -1,49 +1,118 @@ 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 '
You must enter a comment.
'; + 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 '
', $this->_comment_error, '
'; + 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; + } + } + } +} diff --git a/lib/data.php b/lib/data.php index c10e4da..129b419 100644 --- a/lib/data.php +++ b/lib/data.php @@ -102,8 +102,7 @@ function get_dirs( $path = "", $args = array()) */ 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; @@ -115,6 +114,8 @@ function get_pages() } + + /** * @param splFileInfo SPLFileInfo Object * @param page default is false @@ -143,33 +144,20 @@ function parse_entry($fileInfo, $page = 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); @@ -185,6 +173,18 @@ function parse_entry($fileInfo, $page = false) } +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))); diff --git a/lib/init.php b/lib/init.php index f26a619..9e84f7c 100644 --- a/lib/init.php +++ b/lib/init.php @@ -50,4 +50,5 @@ require_once 'output.php'; require_once 'markdown.php'; require_once 'model.php'; require_once 'view.php'; +require_once 'comments.php'; diff --git a/lib/model.php b/lib/model.php index d4f4991..c8910bf 100644 --- a/lib/model.php +++ b/lib/model.php @@ -47,7 +47,12 @@ class Model { $this->entry = get_entry( $this->content_request ); - // heavy handed prev/next + // post request + + if ($_SERVER['REQUEST_METHOD'] == 'POST') + $this->process_post_request($this->entry); + + // prev/next $entries = get_entries(); for($i = count($entries)-1; $i>=0; $i--) { if ($this->entry['url'] == $entries[$i]['url']) { @@ -75,6 +80,13 @@ class Model } } + function process_post_request( &$entry ) { + + if ($entry['comments']) + $entry['comments']->process_post_request(); + + } + function has_config() { return is_file(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request, CONFIG_FILE ), DIRECTORY_SEPARATOR )) ? 1 : 0; diff --git a/lib/output.php b/lib/output.php index 6afa477..12fa2fc 100644 --- a/lib/output.php +++ b/lib/output.php @@ -154,7 +154,7 @@ function get_request() $path = $_GET['p']; } else { // TODO - // this is fudged + // this is fudged because REQUEST_URI may vary for different server configs due to soft links if ($_SERVER['REQUEST_URI'] == WEB_ROOT) { $path = '/'; } diff --git a/templates/comments.html.tpl b/templates/comments.html.tpl index c16fe7c..a165598 100644 --- a/templates/comments.html.tpl +++ b/templates/comments.html.tpl @@ -1,11 +1,14 @@ +
+

comments

+
- + comments as $comment) : ?>
-
posted by at
+
posted by at
@@ -18,9 +21,8 @@

- markdown enabled - - + + recaptcha_html(); ?>
diff --git a/templates/entry.html.tpl b/templates/entry.html.tpl index bb7c88a..d5c4c20 100644 --- a/templates/entry.html.tpl +++ b/templates/entry.html.tpl @@ -11,14 +11,14 @@