Fixed /note/'s comment link logic
[dylansserver.git] / index.php
index 16a01ec..5bf04d6 100644 (file)
--- a/index.php
+++ b/index.php
@@ -304,7 +304,7 @@ class note extends cms {
        $this->comments_enabled = $comments_enabled;
     $url = htmlspecialchars($_SERVER['REQUEST_URI']);
        if (isset($_GET['verify'])) {
-      $url = substr($url, 0, (strlen($url)-7));
+      $url = substr($url, 0, (strlen($url)-6));
        }
        $this->url = $url;
   }
@@ -321,11 +321,8 @@ class note extends cms {
   public function display() {
        $this->display_head();
        $this->display_note();
-       if (isset($_GET['verify'])) {
-         $this->verify();
-       }
        if ($this->comments_enabled) {
-         $this->display_comments();  // but where are they?
+         $this->display_comments();
          $this->display_comment_form();
        }
        $this->write_navigation();
@@ -334,13 +331,27 @@ class note extends cms {
 
   private function verify() {
     require_once('includes/recaptchalib.php');
+       echo "<br>";
     $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
                                                                    $_SERVER["REMOTE_ADDR"],
                                                                    $_POST["recaptcha_challenge_field"],
                                                                    $_POST["recaptcha_response_field"]);
     if (!$resp->is_valid) {
-      echo "The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")";
-    }
+      echo "<span style=\"color:red;border:1px solid black;padding:15px;\">sorry, reCAPTCHA said you're not human.</span><br><br><br>";
+    } else {
+         $sql = ("INSERT INTO comments (date_posted, author,
+                               email, text, note)
+                               VALUES(NOW(), ?, ?, ?, ?)");
+         $stmt = $this->db->prepare($sql);
+         // Checks are needed here (no blank text,
+         // and a default author / email need to be set
+         $stmt->bind_param('ssss',
+                                               htmlspecialchars($_POST['author']),
+                                               htmlspecialchars($_POST['email']),
+                                               htmlspecialchars($_POST['text']),
+                                               $this->id);
+         $stmt->execute();
+       }
   }
 
   private function display_note() {
@@ -365,23 +376,32 @@ class note extends cms {
   }
 
   private function write_navigation() {
-       echo "<br>";
-    echo "<div id=\"navigation\">";
-    echo "<h2>";
-    echo "<a href=\"/notes/\">notes</a>/";
-    echo "</h2>";
-    echo "</div>";
+    echo <<<END_OF_NAVIGATION
+       <br>
+    <div id=\"navigation\">
+    <h2>
+       <a href="/notes/">notes</a>/
+    </h2>
+    </div>
+END_OF_NAVIGATION;
   }
 
   private function display_comment_link() {
-    $url = $this->url . 'comments/';
+       // somehow I should be checking if there are any first,
+       // change to 'comment?'
+       if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
+      $url = $this->url . 'comments/';
+       } else {
+      $url = $this->url . '/comments/';
+       }
     echo "<a id=\"comment_link\" href=\"$url\">comments</a>";
   }
 
   private function display_comments() {
     echo "<div id=\"comments\">";
        $sql= "SELECT date_posted, author, email, text
-                FROM comments WHERE note = ?";
+                FROM comments WHERE note = ?
+                        ORDER BY date_posted DESC";
     $result = $this->query($sql, "d", $this->id);
        foreach ($result as $row => $entry) {
          $date_posted = $entry['date_posted'];
@@ -399,13 +419,52 @@ END_OF_COMMENT;
   }
 
   private function display_comment_form() {
-       // Trailing slash is necessary for reloads to work
-    $url = $this->url . "verify/";
-       echo "<form id=\"comment\" method=\"post\" action=\"$url\">";
+    echo <<<END_CAPTCHA_STYLE
+<script type="text/javascript">
+var RecaptchaOptions = {
+   theme : 'custom',
+   custom_theme_widget: 'recaptcha_widget'
+   };
+</script>
+END_CAPTCHA_STYLE;
     require_once('includes/recaptchalib.php');
+       // Trailing slash is necessary for reloads to work
+    $url = $this->url . "verify";
+       echo "<form method=\"post\" action=\"$url\">";
+       echo  <<<FORM
+       <div id="comment">
+
+<h3>comment:</h3><br>
+<textarea rows="10" cols="70" name=text></textarea><br>
+<h3>name:</h3><br>
+<input type=text name=author><br>
+<h3>email:</h3><br>
+<input type=text name=email><br>
+<nowiki>
+
+<div id="recaptcha_widget"> 
+  <div id="recaptcha_image"></div>
+  <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
+  <span class="recaptcha_only_if_image"><b>enter the words above</b>:</span>
+  <span class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</span>
+  <br>
+  <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
+  <div><a href="javascript:Recaptcha.reload()"><small>another CAPTCHA?</small></a></div>
+  <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')"><small>audio CAPTCHA?</small></a></div>
+  <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')"><small>Get an image CAPTCHA</small></a></div>
+  <div><a href="javascript:Recaptcha.showhelp()"><small>help?</small></a></div>
+  <br>
+</div>
+FORM;
        echo recaptcha_get_html($this->recaptcha_publickey);
-       echo "<input type=\"submit\">";
-       echo "</form>";
+       if (isset($_GET['verify'])) {
+         $this->verify();
+       }
+       echo  <<<END_OF_FORM
+       <input class="submit" type="submit" value="comment">
+       </form>
+       </div>
+END_OF_FORM;
   }
 }