d5ab563d6205d0f2d4e0ef59feda3595382cac70
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));
27 $sql = "SELECT title, date_posted, text, id
28 FROM notes WHERE url = ?";
29 $result = $this->query($sql, "s",
33 $this->id
= $entry["id"];
34 $this->title
= $entry["title"];
35 $date_posted = explode("-", $entry["date_posted"]);
36 $this->year_posted
= $date_posted[0];
37 $this->month_posted
= $date_posted[1];
38 $datetime_posted = explode(' ', $date_posted[2]);
39 $this->day_posted
= $datetime_posted[0];
40 $this->text
= $entry["text"];
44 $sql = "SELECT COUNT(*) FROM comments
45 WHERE note = $this->id";
46 $result = $this->db
->query($sql);
47 $result = $result->fetch_array();
48 $this->number_of_comments
= $result[0];
49 if (isset($_GET['verify'])) {
54 public function display() {
55 require_once("view/note.php");
58 public function verify() {
59 if (!isset($_POST['captcha'])) {
60 require_once('includes/recaptchalib.php');
62 $resp = recaptcha_check_answer ($this->recaptcha_privatekey
,
63 $_SERVER["REMOTE_ADDR"],
64 $_POST["recaptcha_challenge_field"],
65 $_POST["recaptcha_response_field"]);
66 if (!$resp->is_valid
) {
67 $this->failed_captcha
= true;
70 if (isset($_POST['captcha']) ||
$resp->is_valid
) {
71 $sql = ("INSERT INTO comments (date_posted, author,
73 VALUES(NOW(), ?, ?, ?)");
74 $stmt = $this->db
->prepare($sql);
75 // Checks are needed here (no blank text,
76 // and a default author needs to be set
77 // for no-javascript users.
78 $stmt->bind_param('sss',
86 public function display_comment_link() {
87 if ($this->number_of_comments
> 0) {
88 $anchor_text = "comments($this->number_of_comments)/";
90 $anchor_text = "comment?";
92 if (substr($this->url
, (strlen($this->url
)-1), strlen($this->url
)) == '/') {
93 $url = $this->url
. 'comments/';
95 $url = $this->url
. '/comments/';
97 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
100 public function display_comments() {
101 // should be called like $note->comment[0]['author']
102 $sql= "SELECT date_posted, author, text
103 FROM comments WHERE note = ?
104 ORDER BY date_posted DESC";
105 $result = $this->query($sql, 'd', $this->id
);
107 foreach ($result as $row => $entry) {
108 $this->comment
[$i]['date_posted'] = $entry['date_posted'];
109 $this->comment
[$i]['author'] = $entry['author'];
110 $this->comment
[$i]['text'] = htmlspecialchars($entry['text']);
111 $this->comment
[$i]['head'] = "<h3>" . htmlspecialchars($author) . "</h3>";
116 public function display_comment_form() {
117 $publickey = $this->recaptcha_publickey
;
118 require_once("view/comment-form.php");