]> git.quilime.com - plog.git/commitdiff
commenting with recaptcha comments2
authorGabriel Dunne <gdunne@quilime.com>
Fri, 11 Nov 2011 10:07:06 +0000 (15:37 +0530)
committerGabriel Dunne <gdunne@quilime.com>
Fri, 11 Nov 2011 10:07:06 +0000 (15:37 +0530)
content/.DS_Store [deleted file]
content/categori1/.DS_Store [deleted file]
css/comments.css
index.php
lib/comments.php
lib/data.php
lib/init.php
lib/model.php
lib/output.php
templates/comments.html.tpl
templates/entry.html.tpl

diff --git a/content/.DS_Store b/content/.DS_Store
deleted file mode 100644 (file)
index 16b6615..0000000
Binary files a/content/.DS_Store and /dev/null differ
diff --git a/content/categori1/.DS_Store b/content/categori1/.DS_Store
deleted file mode 100644 (file)
index bcb3407..0000000
Binary files a/content/categori1/.DS_Store and /dev/null differ
index d517577229b9653b3f34a97f0dcb676c11c354c1..6fac0a8b2d88569dde1069ff05435991f3670ead 100644 (file)
        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;
index 62b7c6d0dfe3cc36cb03ddd5fd10ecd8a3ccaa68..c467ad7b2ad28fd625ecdb0e93a5fc869b2aba41 100644 (file)
--- 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);
index 1b17db310dce61c4912d7192cc07fece30479bf9..c413a0c8d48db29d650857cf317711352df5c186 100644 (file)
 <?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;
+                       }
+               }
+       }
+}
index c10e4da0b71728e3bce0222a356840c77efad18d..129b419452648542f3aeb1050d1cd647b1e5698c 100644 (file)
@@ -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)));
index f26a6196b1100d2b3e5e896b6617b321bc205751..9e84f7cc47f6102d679e4cf09d18711f3c244795 100644 (file)
@@ -50,4 +50,5 @@ require_once 'output.php';
 require_once 'markdown.php';
 require_once 'model.php';
 require_once 'view.php';
+require_once 'comments.php';
 
index d4f49910d9571ba0f579596e02d1f619528eedf1..c8910bfd9df0ad4ba8da773d7b8d02d345f3ac55 100644 (file)
@@ -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;
index 6afa47705bdc65522ef2352483cf43bd349bb534..12fa2fcd25a5ed39c3312b45861ef43fcfcb0028 100644 (file)
@@ -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 = '/';
         }
index c16fe7cd46a837e336e159fe07fd0950a4268dfb..a1655988c118a00258aad08fd6d24b1026a2490e 100644 (file)
@@ -1,11 +1,14 @@
+
 <div class="comments">
+       <h2>comments</h2>
 
+       <a name="c"></a>
        <div class="comment_list">
                
-               <? foreach($entry['comments'] as $comment) : ?>
+               <? foreach($entry['comments']->comments as $comment) : ?>
                <div class="comment">
                        <?=$comment['comment'];?>
-                       <div class="name">posted by <?=$comment['name'];?> at <?=$comment['timestamp'];?></div>
+                       <div class="name">posted by <?=$comment['name'];?> at <?=date("g:i a,  M d Y", $comment['timestamp'])?></div>
                </div>
                <? endforeach; ?>
 
@@ -18,9 +21,8 @@
     <input type="text" name="www" /> <label for="www">www (optional)</label>  <br />
     <textarea name="comment"></textarea>
     <br />
-    markdown enabled
-
-       <?php include(LOCAL_ROOT . 'lib/' . 'comments.php'); ?>
+    
+       <?php echo $entry['comments']->recaptcha_html(); ?>
 
     <br/>
     <input type="submit" value="submit" />
index bb7c88a2b7b27686a5ecc9e74702706a10588728..d5c4c205d308b433e87f780039374d6361a3d7b8 100644 (file)
 
        <div class="metadata">
                <span title="<?=get_relative_time($entry['timestamp']);?>"><?=date(ENTRY_DATE_FORMAT, $entry['timestamp'])?>
-               <? if (count($entry['comments']) > 0 || $entry['comments_enabled']) : ?>
-               | <a href="<?=$entry['url'];?>#c"><? echo sizeof($entry['comments']) ?> comments</a>
-               <? endif; ?>
                </span> 
                <? if (!empty($entry['cat']['name'])): ?>
                in <a href="<?=$entry['cat']['url'];?>"><?=$entry['cat']['name'];?></a>
-               <br/>
                <? endif; ?>            
+               <? if (count($entry['comments']->comments) > 0 || $entry['comments_enabled']) : ?>
+               | <a href="<?=$entry['url'];?>#c"><? echo sizeof($entry['comments']->comments) ?> comments</a>
+               <? endif; ?>            
+               <br />
                <ul class="tags">
                <? if ($entry['tags']) foreach($entry['tags'] as $tag) : ?>
                <li>#<?=$tag?></li>