3 class note
extends model
{
6 public $comments_enabled = false;
7 public $failed_captcha;
14 public $number_of_comments;
16 public function __construct() {
17 parent
::__construct();
18 if (isset($_GET['comments'])) {
19 $this->comments_enabled
= true;
21 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
22 if (isset($_GET['verify'])) {
23 $url = substr($url, 0, (strlen($url)-6));
26 $sql = "SELECT title, date_posted, text, id
27 FROM notes WHERE url = ?";
28 $result = $this->query($sql, "s",
32 $this->id
= $entry["id"];
33 $this->title
= $entry["title"];
34 $date_posted = explode("-", $entry["date_posted"]);
35 $this->year_posted
= $date_posted[0];
36 $this->month_posted
= $date_posted[1];
37 $datetime_posted = explode(' ', $date_posted[2]);
38 $this->day_posted
= $datetime_posted[0];
39 $this->text
= $entry["text"];
43 $sql = "SELECT COUNT(*) FROM comments
44 WHERE note = $this->id";
45 $result = $this->db
->query($sql);
46 $result = $result->fetch_array();
47 $this->number_of_comments
= $result[0];
48 if (isset($_GET['verify'])) {
53 public function display() {
54 require_once("view/note.php");
57 public function verify() {
58 if (!isset($_POST['captcha'])) {
59 require_once('includes/recaptchalib.php');
61 $resp = recaptcha_check_answer ($this->recaptcha_privatekey
,
62 $_SERVER["REMOTE_ADDR"],
63 $_POST["recaptcha_challenge_field"],
64 $_POST["recaptcha_response_field"]);
65 if (!$resp->is_valid
) {
66 $this->failed_captcha
= true;
69 if (isset($_POST['captcha']) ||
$resp->is_valid
) {
70 $sql = ("INSERT INTO comments (date_posted, author,
72 VALUES(NOW(), ?, ?, ?)");
73 $stmt = $this->db
->prepare($sql);
74 // Checks are needed here (no blank text,
75 // and a default author needs to be set
76 // for no-javascript users.
77 $stmt->bind_param('sss',
85 public function display_comment_link() {
86 if ($this->number_of_comments
> 0) {
87 $anchor_text = "comments($this->number_of_comments)/";
89 $anchor_text = "comment?";
91 if (substr($this->url
, (strlen($this->url
)-1), strlen($this->url
)) == '/') {
92 $url = $this->url
. 'comments/';
94 $url = $this->url
. '/comments/';
96 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
99 public function display_comments() {
100 echo "<div id='comments'>";
101 $sql= "SELECT date_posted, author, text
102 FROM comments WHERE note = ?
103 ORDER BY date_posted DESC";
104 $result = $this->query($sql, 'd', $this->id
);
105 foreach ($result as $row => $entry) {
106 $date_posted = $entry['date_posted'];
107 $author = $entry['author'];
108 $text = htmlspecialchars($entry['text']);
109 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
110 echo "<div class='comment'>";
118 public function display_comment_form() {
119 $publickey = $this->recaptcha_publickey
;
120 require_once("view/comment-form.php");