3 class note
extends model
{
6 public $comments_enabled = false;
7 public $failed_captcha;
14 public $number_of_comments;
17 public function __construct() {
18 parent
::__construct();
19 if (isset($_GET['comments'])) {
20 $this->comments_enabled
= true;
22 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
23 if (isset($_GET['verify'])) {
24 $url = substr($url, 0, (strlen($url)-6));
28 $this->fetch_comments();
31 public function fetch_note() {
32 $sql = "SELECT title, date_posted, text, id
33 FROM notes WHERE url = ?";
34 $result = $this->query($sql, "s",
38 $this->id
= $entry["id"];
39 $this->title
= $entry["title"];
40 $date_posted = explode("-", $entry["date_posted"]);
41 $this->year_posted
= $date_posted[0];
42 $this->month_posted
= $date_posted[1];
43 $datetime_posted = explode(' ', $date_posted[2]);
44 $this->day_posted
= $datetime_posted[0];
45 $this->text
= $entry["text"];
51 public function fetch_comments() {
52 $sql = "SELECT COUNT(*) FROM comments
53 WHERE note = $this->id";
54 $result = $this->db
->query($sql);
55 $result = $result->fetch_array();
56 $this->number_of_comments
= $result[0];
57 if (isset($_GET['verify'])) {
62 public function display() {
63 require_once("view/note.php");
66 public function display_comment_link() {
67 if ($this->number_of_comments
> 0) {
68 $anchor_text = "comments($this->number_of_comments)/";
70 $anchor_text = "comment?";
72 if (substr($this->url
, (strlen($this->url
)-1), strlen($this->url
)) == '/') {
73 $url = $this->url
. 'comments/';
75 $url = $this->url
. '/comments/';
77 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
80 public function display_comments() {
81 $sql= "SELECT date_posted, author, text
82 FROM comments WHERE note = ?
83 ORDER BY date_posted DESC";
84 $result = $this->query($sql, 'd', $this->id
);
86 foreach ($result as $row => $entry) {
87 $this->comment
[$i]['date_posted'] = $entry['date_posted'];
88 $this->comment
[$i]['author'] = $entry['author'];
89 $this->comment
[$i]['text'] = htmlspecialchars($entry['text']);
90 $this->comment
[$i]['head'] = "<h3>" . htmlspecialchars($author) . "</h3>";
95 public function display_comment_form() {
96 $publickey = $this->recaptcha_publickey
;
97 require_once("view/comment-form.php");
100 public function verify() {
101 if (!isset($_POST['captcha'])) {
102 require_once('includes/recaptchalib.php');
104 $resp = recaptcha_check_answer ($this->recaptcha_privatekey
,
105 $_SERVER["REMOTE_ADDR"],
106 $_POST["recaptcha_challenge_field"],
107 $_POST["recaptcha_response_field"]);
108 if (!$resp->is_valid
) {
109 $this->failed_captcha
= true;
112 if (isset($_POST['captcha']) ||
$resp->is_valid
) {
113 $sql = ("INSERT INTO comments (date_posted, author,
115 VALUES(NOW(), ?, ?, ?)");
116 $stmt = $this->db
->prepare($sql);
117 // Checks are needed here (no blank text,
118 // and a default author needs to be set
119 // for no-javascript users.
120 $stmt->bind_param('sss',