rss layout now fully separated m/v/c
[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 $sql = "SELECT title, date_posted, text, id
28 FROM notes WHERE url = ?";
29 $result = $this->query($sql, "s",
30 $_GET['note']);
31 if ($result) {
32 $entry = $result[0];
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"];
41 } else {
42 throw new notFound();
43 }
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'])) {
50 $this->verify();
51 }
52 }
53
54 public function display() {
55 require_once("view/note.php");
56 }
57
58 public function verify() {
59 if (!isset($_POST['captcha'])) {
60 require_once('includes/recaptchalib.php');
61 echo "<br>";
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;
68 }
69 }
70 if (isset($_POST['captcha']) || $resp->is_valid) {
71 $sql = ("INSERT INTO comments (date_posted, author,
72 text, note)
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',
79 $_POST['name'],
80 $_POST['text'],
81 $this->id);
82 $stmt->execute();
83 }
84 }
85
86 public function display_comment_link() {
87 if ($this->number_of_comments > 0) {
88 $anchor_text = "comments($this->number_of_comments)/";
89 } else {
90 $anchor_text = "comment?";
91 }
92 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
93 $url = $this->url . 'comments/';
94 } else {
95 $url = $this->url . '/comments/';
96 }
97 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
98 }
99
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);
106 $i = 0;
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>";
112 $i++;
113 }
114 }
115
116 public function display_comment_form() {
117 $publickey = $this->recaptcha_publickey;
118 require_once("view/comment-form.php");
119 }
120 }
121
122 ?>