added powermate css
[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
16 public function __construct() {
17 parent::__construct();
18 if (isset($_GET['comments'])) {
19 $this->comments_enabled = true;
20 }
21 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
22 if (isset($_GET['verify'])) {
23 $url = substr($url, 0, (strlen($url)-6));
24 }
25 $this->url = $url;
26 $sql = "SELECT title, date_posted, text, id
27 FROM notes WHERE url = ?";
28 $result = $this->query($sql, "s",
29 $_GET['note']);
30 if ($result) {
31 $entry = $result[0];
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"];
40 } else {
41 throw new notFound();
42 }
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'])) {
49 $this->verify();
50 }
51 }
52
53 public function display() {
54 require_once("view/note.php");
55 }
56
57 public function verify() {
58 if (!isset($_POST['captcha'])) {
59 require_once('includes/recaptchalib.php');
60 echo "<br>";
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;
67 }
68 }
69 if (isset($_POST['captcha']) || $resp->is_valid) {
70 $sql = ("INSERT INTO comments (date_posted, author,
71 text, note)
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',
78 $_POST['name'],
79 $_POST['text'],
80 $this->id);
81 $stmt->execute();
82 }
83 }
84
85 public function display_comment_link() {
86 if ($this->number_of_comments > 0) {
87 $anchor_text = "comments($this->number_of_comments)/";
88 } else {
89 $anchor_text = "comment?";
90 }
91 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
92 $url = $this->url . 'comments/';
93 } else {
94 $url = $this->url . '/comments/';
95 }
96 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
97 }
98
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'>";
111 echo $head;
112 echo $text;
113 echo "</div>";
114 }
115 echo "</div>";
116 }
117
118 public function display_comment_form() {
119 $publickey = $this->recaptcha_publickey;
120 require_once("view/comment-form.php");
121 }
122 }
123
124 ?>