Merge branch 'dev' of dl:dylansserver into dev
[dylansserver.git] / model / note.php
1 <?php
2
3 class note extends model {
4
5 public $id;
6 public $comments_enabled = false;
7 public $failed_captcha;
8 public $url;
9 public $title;
10 public $year_posted;
11 public $month_posted;
12 public $day_posted;
13 public $text;
14 public $number_of_comments;
15 public $comments;
16
17 public function __construct() {
18 parent::__construct();
19 if (isset($_GET['comments'])) {
20 $this->comments_enabled = true;
21 }
22 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
23 if (isset($_GET['verify'])) {
24 $url = substr($url, 0, (strlen($url)-6));
25 }
26 $this->url = $url;
27 $this->fetch_note();
28 $this->fetch_comments();
29 }
30
31 public function fetch_note() {
32 $sql = "SELECT title, date_posted, text, id
33 FROM notes WHERE url = ?";
34 $result = $this->query($sql, "s",
35 $_GET['note']);
36 if ($result) {
37 $entry = $result[0];
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"];
46 } else {
47 throw new notFound();
48 }
49 }
50
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'])) {
58 $this->verify();
59 }
60 }
61
62 public function display() {
63 require_once("view/note.php");
64 }
65
66 public function display_comment_link() {
67 if ($this->number_of_comments > 0) {
68 $anchor_text = "comments($this->number_of_comments)/";
69 } else {
70 $anchor_text = "comment?";
71 }
72 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
73 $url = $this->url . 'comments/';
74 } else {
75 $url = $this->url . '/comments/';
76 }
77 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
78 }
79
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);
85 $i = 0;
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>";
91 $i++;
92 }
93 }
94
95 public function display_comment_form() {
96 $publickey = $this->recaptcha_publickey;
97 require_once("view/comment-form.php");
98 }
99
100 public function verify() {
101 if (!isset($_POST['captcha'])) {
102 require_once('includes/recaptchalib.php');
103 echo "<br>";
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;
110 }
111 }
112 if (isset($_POST['captcha']) || $resp->is_valid) {
113 $sql = ("INSERT INTO comments (date_posted, author,
114 text, note)
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',
121 $_POST['name'],
122 $_POST['text'],
123 $this->id);
124 $stmt->execute();
125 }
126 }
127
128 }
129
130 ?>