Fixed link error with new post display
[dylansserver.git] / index.php
index 16a01ec..5598679 100644 (file)
--- a/index.php
+++ b/index.php
@@ -34,7 +34,10 @@ abstract class cms {
       return 'index';
        } else if (isset($_GET['project'])) {
       return 'project';
+       } else if (isset($_GET['challenge'])) {
+      return 'captcha';
        }
+
   }
 
   public function query() {
@@ -55,7 +58,7 @@ abstract class cms {
        call_user_func_array("mysqli_stmt_bind_result", $fields);
        $i = 0;
        while ($statement->fetch()) {
-         foreach ($row as $key1=>$value1) $return[$i][$key1] = $value1;
+         foreach ($row as $key=>$value) $return[$i][$key] = $value;
          $i++;
        }
        $statement->free_result();
@@ -66,8 +69,14 @@ abstract class cms {
                                                                $home_link = "/") {
     $scripts = "";
        $stylesheets = "<link href=\"/includes/style.css\" rel=\"stylesheet\" type=\"text/css\">";
-       if (cms::determine_type() == "index") { $scripts = "<script type=\"text/javascript\" src=\"/includes/all.js\">";
+       if ($this->determine_type() == "index") {
+         $scripts = "<script type=\"text/javascript\" src=\"/includes/all.js\">";
          $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylanstestserver.com&amp;ucn_task=conformance#";
+       } else if ($this->determine_type() == 'note') {
+         $scripts = "<script type=\"text/javascript\" src=\"http://www.google.com/recaptcha/api/js/recaptcha_ajax.js\"></script>";
+         $scripts .= "<script type=\"text/javascript\" src=\"/includes/jquery-core.js\"></script>";
+         $scripts .= "<script type=\"text/javascript\" src=\"/includes/jquery-all-components.js\"></script>";
+         $scripts .= "<script type=\"text/javascript\" src=\"/includes/ajax.js\"></script>";
        }
   echo <<<END_OF_HEAD
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
@@ -296,36 +305,55 @@ class note extends cms {
 
   private $id;
   private $comments_enabled = false;
-  private $url;
+  private $failed_captcha;
+  public $url;
+  public $title;
+  public $year_posted;
+  public $month_posted;
+  public $day_posted;
+  public $text;
+  public $number_of_comments;
 
   public function __construct($comments_enabled = false) {
     parent::__construct();
-       $this->check_exists();
        $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;
-  }
-
-  private function check_exists() {
-    $sql = "SELECT COUNT(*) FROM notes
-                         WHERE url = ?";
-       $results = $this->query($sql, "s", $_GET['note']);
-       if ($results[0]["COUNT(*)"] != 1) {
+    $sql = "SELECT title, date_posted, text, id
+                         FROM notes WHERE url = ?";
+       $result = $this->query($sql, "s",
+                                                         $_GET['note']);
+       if ($result) {
+         $entry = $result[0];
+         $this->id = $entry["id"];
+         $this->title = $entry["title"];
+         $date_posted =  explode("-", $entry["date_posted"]);
+         $this->year_posted = $date_posted[0];
+         $this->month_posted = $date_posted[1];
+         $datetime_posted = explode(' ', $date_posted[2]);
+         $this->day_posted = $datetime_posted[0];
+         $this->text = $entry["text"];
+       } else {
          throw new notFound();
        }
+       $sql = "SELECT COUNT(*) FROM comments
+                         WHERE note = $this->id";
+       $result = $this->db->query($sql);
+       $result = $result->fetch_array();
+       $this->number_of_comments = $result[0];
+       if (isset($_GET['verify'])) {
+         $this->verify();
+       }
   }
 
   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();
@@ -333,79 +361,154 @@ class note extends cms {
   }
 
   private function verify() {
-    require_once('includes/recaptchalib.php');
-    $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 . ")";
+    if (!isset($_POST['captcha'])) {
+      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) {
+           $this->failed_captcha = true;
+         }
     }
+       if (isset($_POST['captcha']) || $resp->is_valid) {
+         $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
+         // for no-javascript users.
+         $stmt->bind_param('ssss',
+                                               htmlspecialchars($_POST['name']),
+                                               htmlspecialchars($_POST['email']),
+                                               htmlspecialchars($_POST['text']),
+                                               $this->id);
+         $stmt->execute();
+       }
   }
 
   private function display_note() {
-    $sql = "SELECT title, date_posted, text, id
-                         FROM notes WHERE url = ?";
-       $result = $this->query($sql, "s",
-                                                         $_GET['note']);
-       $entry = $result[0];
-       $this->id = $entry["id"]; // This is needed for display_comments()
-       $title = $entry["title"];
-       $date_posted =  explode("-", $entry["date_posted"]);
-       $year_posted = $date_posted[0];
-       $month_posted = $date_posted[1];
-       $datetime_posted = explode(' ', $date_posted[2]);
-       $day_posted = $datetime_posted[0];
        echo "<div id=\"note\">";
-    echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span>$title</h2>";
-       if (!$this->comments_enabled) {
-         $this->display_comment_link();
-       }
-       echo $entry['text'];
+    echo "<h2><span style=\"color:grey;\">$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h2>";
+       echo $this->text;
   }
 
   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>
+END_OF_NAVIGATION;
+       if (!$this->comments_enabled) {
+         $this->display_comment_link();
+       }
+    echo <<<END_OF_NAVIGATION
+       <a href="/notes/">notes</a>/
+    </h2>
+    </div>
+END_OF_NAVIGATION;
   }
 
   private function display_comment_link() {
-    $url = $this->url . 'comments/';
-    echo "<a id=\"comment_link\" href=\"$url\">comments</a>";
+    if ($this->number_of_comments > 0) {
+         $anchor_text = "comments ($this->number_of_comments)";
+       } else {
+         $anchor_text = "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\">$anchor_text</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'];
          $author = $entry['author'];
          $email = $entry['email'];
          $text = htmlspecialchars($entry['text']);
+         if ($email == '') {
+           $head = "<h3>$author</h3>";
+         } else {
+           $head = "<h3><a href='mailto:$email'>$author</a></h3>";
+         }
          echo <<<END_OF_COMMENT
-         <h3><a href="mailto:$email">$author</a></h3>
+         <div class='comment'>
+         $head
          $text
-         <br>
-         <br>
+         </div>
 END_OF_COMMENT;
-       }
+         }
        echo "</div>";
   }
 
   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\">";
+    $publickey = $this->recaptcha_publickey;
+    echo <<<END_CAPTCHA_STYLE
+<script type="text/javascript">
+Recaptcha.create("$publickey",
+   "recaptcha_div", 
+   {
+     theme : 'custom',
+     custom_theme_widget: 'recaptcha_widget',
+     callback: Recaptcha.focus_response_field
+   });
+</script>
+END_CAPTCHA_STYLE;
     require_once('includes/recaptchalib.php');
-       echo recaptcha_get_html($this->recaptcha_publickey);
-       echo "<input type=\"submit\">";
-       echo "</form>";
+    $url = $this->url . "verify";
+       echo "<form id=\"comment_form\"  method=\"post\" action=\"$url\">";
+       echo <<<END_OF_FORM
+    <div id="comment">
+      <h3>comment:</h3>
+      <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
+      <h3>name:</h3>
+      <input type=text name="name" id="comment_name">
+      <h3>email:</h3>
+      <input type=text name="email" id="comment_email"><br>
+  
+      <nowiki>
+      <div id="recaptcha_widget"> 
+        <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
+        <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
+        <span style="font-size:80%;">
+             ( <a href="javascript:Recaptcha.reload()">another</a> /
+          <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
+          <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
+           </span>
+        <br><br>
+        <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
+        <br><br>
+        <div style="float:right;position:relative;width:100px;">
+             <div id="recaptcha_image"></div>
+           </div>
+        <br><br><br><br>
+      </div>
+END_OF_FORM;
+    echo recaptcha_get_html($this->recaptcha_publickey); 
+    if ($this->failed_captcha) {
+    echo <<<END_OF_FORM
+        <span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>reCAPTCHA said you're not human,</span>
+        <input id="submit" class="submit" type="submit" value="try again?">
+        </form>
+      </div>
+END_OF_FORM;
+    } else {
+         echo <<<END_OF_FORM
+      <input id="submit" class="submit" type="submit" value="post comment">
+      </form>
+      </div>
+END_OF_FORM;
+    }
   }
 }
 
@@ -504,6 +607,19 @@ class notFound extends Exception {
        }
 }
 
+class captcha extends cms {
+       public function display() {
+         $challenge = $_GET['challenge'];
+         $response = $_GET['response'];
+         $remoteip = $_SERVER['REMOTE_ADDR'];
+      $curl = curl_init('http://api-verify.recaptcha.net/verify?');
+      curl_setopt ($curl, CURLOPT_POST, 4);
+      curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
+         $result = curl_exec ($curl);
+         curl_close ($curl);
+       }
+}
+
 ## now actually do something:
 switch (cms::determine_type()) {
   case "index":
@@ -530,6 +646,10 @@ switch (cms::determine_type()) {
     $archive = new archive;
        $archive->display();
        break;
+  case "captcha":
+    $captcha = new captcha;
+       $captcha->display();
+       break;
 }
 
 ?>