added missing comment link to view/note.php
[dylansserver.git] / cms.php
1 <?php
2
3 abstract class cms {
4
5 private $config_file = '/etc/dylansserver.ini';
6 protected $db;
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
9 protected $scripts;
10 public $title;
11 public $home_link;
12
13 public function __construct() {
14 $config = parse_ini_file($this->config_file, true);
15 $this->db = new mysqli(
16 $config['database']['domain'],
17 $config['database']['user'],
18 $config['database']['password'],
19 $config['database']['database']);
20 if (mysqli_connect_errno()) {
21 echo "Problem connecting to database: ";
22 echo mysqli_connect_error();
23 exit();
24 }
25 $this->recaptcha_publickey = $config['recaptcha']['publickey'];
26 $this->recaptcha_privatekey = $config['recaptcha']['privatekey'];
27 $this->title = $config['site']['default_title'];
28 $this->home_link = $config['site']['home_link'];
29 ob_start();
30 }
31
32 public static function determine_type() {
33 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
34 return 'page';
35 } else if (isset($_GET['year'])) {
36 return 'archive';
37 } else if (isset($_GET['note'])) {
38 return 'note';
39 } else if ($_SERVER['REQUEST_URI'] == '/') {
40 return 'index';
41 } else if (isset($_GET['project'])) {
42 return 'project';
43 } else if (isset($_GET['rss'])) {
44 return 'rss';
45 } else if (isset($_GET['challenge'])) {
46 return 'captcha';
47 }
48 }
49
50 public function query() {
51 $args = func_get_args();
52 $statement = $this->db->prepare($args[0]);
53 $args = array_slice($args, 1);
54 call_user_func_array(array($statement, 'bind_param'), &$args);
55 $statement->execute();
56 $return = array();
57 $statement->store_result();
58 $row = array();
59 $data = $statement->result_metadata();
60 $fields = array();
61 $fields[0] = &$statement;
62 while($field = $data->fetch_field()) {
63 $fields[] = &$row[$field->name];
64 }
65 call_user_func_array("mysqli_stmt_bind_result", $fields);
66 $i = 0;
67 while ($statement->fetch()) {
68 foreach ($row as $key=>$value) $return[$i][$key] = $value;
69 $i++;
70 }
71 $statement->free_result();
72 return $return;
73 }
74
75 public function init() {
76 switch (cms::determine_type()) {
77 case 'index':
78 $index = new index();
79 $index->display();
80 break;
81 case 'project':
82 $project = new project();
83 $project->display();
84 break;
85 case 'note':
86 $note = new note;
87 $note->display();
88 break;
89 case 'page':
90 $page = new page;
91 $page->display();
92 break;
93 case "rss":
94 $rss = new rss();
95 $rss->display();
96 break;
97 case 'archive':
98 $archive = new archive;
99 $archive->display();
100 break;
101 case "captcha":
102 $captcha = new captcha;
103 $captcha->display();
104 break;
105 }
106 }
107
108 }
109
110
111 class index extends cms {
112
113 public function display() {
114 require_once("view/index.php");
115 }
116
117 protected function display_exhibits() {
118 echo "<div id='exhibit'>";
119 $sql = "SELECT text FROM projects ORDER BY rank";
120 $result = $this->db->query($sql);
121 while ($entry = $result->fetch_object()) {
122 echo $entry->text;
123 }
124 echo "</div>";
125 }
126
127 private function list_projects() {
128 $sql = "SELECT title FROM projects ORDER BY rank";
129 $result = $this->db->query($sql);
130 while ($entry = $result->fetch_object()) {
131 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
132 }
133 }
134
135 }
136
137
138 class project extends index {
139
140 protected function display_exhibits() {
141 echo "<div id='exhibit'>";
142 $sql = "SELECT text FROM projects
143 WHERE title = ?";
144 $result = $this->query($sql, "s", $_GET['project']);
145 if ($result = $result[0]['text']) {
146 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
147 echo $text;
148 echo "</div>";
149 } else {
150 throw new notFound();
151 }
152 }
153
154 }
155
156
157 class page extends cms {
158
159 private $page = 1;
160 private $offset = 0;
161 private $notes_per_page = 4;
162 private $number_of_pages = 1;
163
164 public function __construct() {
165 parent::__construct();
166 $this->page_offset();
167 $this->scripts = "
168 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
169 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
170 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
171 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
172 <script type='text/javascript'>
173 function highlight() {
174 SyntaxHighlighter.autoloader(
175 'js /includes/syntax/scripts/shBrushJScript.js',
176 'bash /includes/syntax/scripts/shBrushBash.js',
177 'sql /includes/syntax/scripts/shBrushSql.js',
178 'cpp /includes/syntax/scripts/shBrushCpp.js');
179 SyntaxHighlighter.defaults['gutter'] = false;
180 SyntaxHighlighter.defaults['toolbar'] = false;
181 SyntaxHighlighter.all();
182 }
183 </script>
184 ";
185 }
186
187 private function page_offset() {
188 $sql = "SELECT COUNT(*) FROM notes";
189 $result = $this->db->query($sql);
190 $result = $result->fetch_array();
191 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
192 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
193 $this->page = (int) $_GET['page'];
194 } else {
195 throw new notFound();
196 }
197 if ($this->page > $this->number_of_pages) {
198 throw new notFound();
199 }
200 if ($this->page < 1) {
201 throw new notFound();
202 }
203 $this->offset = ($this->page - 1) * $this->notes_per_page;
204 }
205
206 public function display() {
207 require_once("view/page.php");
208 }
209
210 public function display_notes() {
211 echo "<div id='notes'>";
212 $sql = "SELECT date_posted, title, url, text
213 FROM notes ORDER BY date_posted DESC
214 LIMIT ?, ?";
215 $result = $this->query($sql, "ii",
216 $this->offset,
217 $this->notes_per_page);
218 foreach ($result as $row => $entry) {
219 $title = $entry['title'];
220 $url = '/note/' . $entry['url'];
221 $date_posted = explode("-", $entry['date_posted']);
222 $year_posted = $date_posted[0];
223 $month_posted = $date_posted[1];
224 $datetime_posted = explode(' ', $date_posted[2]);
225 $day_posted = $datetime_posted[0];
226 $text = $entry['text'];
227 echo <<<END_NOTE
228 <div class='note'>
229 <h1>
230 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
231 </h1>
232 $text
233 </div>
234 END_NOTE;
235 }
236 echo "</div>";
237 }
238 }
239
240
241 class note extends cms {
242
243 private $id;
244 private $comments_enabled = false;
245 private $failed_captcha;
246 public $url;
247 public $title;
248 public $year_posted;
249 public $month_posted;
250 public $day_posted;
251 public $text;
252 public $number_of_comments;
253
254 public function __construct() {
255 parent::__construct();
256 if (isset($_GET['comments'])) {
257 $this->comments_enabled = true;
258 }
259 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
260 if (isset($_GET['verify'])) {
261 $url = substr($url, 0, (strlen($url)-6));
262 }
263 $this->url = $url;
264 $sql = "SELECT title, date_posted, text, id
265 FROM notes WHERE url = ?";
266 $result = $this->query($sql, "s",
267 $_GET['note']);
268 if ($result) {
269 $entry = $result[0];
270 $this->id = $entry["id"];
271 $this->title = $entry["title"];
272 $date_posted = explode("-", $entry["date_posted"]);
273 $this->year_posted = $date_posted[0];
274 $this->month_posted = $date_posted[1];
275 $datetime_posted = explode(' ', $date_posted[2]);
276 $this->day_posted = $datetime_posted[0];
277 $this->text = $entry["text"];
278 } else {
279 throw new notFound();
280 }
281 $sql = "SELECT COUNT(*) FROM comments
282 WHERE note = $this->id";
283 $result = $this->db->query($sql);
284 $result = $result->fetch_array();
285 $this->number_of_comments = $result[0];
286 if (isset($_GET['verify'])) {
287 $this->verify();
288 }
289 }
290
291 public function display() {
292 require_once("view/note.php");
293 }
294
295 private function verify() {
296 if (!isset($_POST['captcha'])) {
297 require_once('includes/recaptchalib.php');
298 echo "<br>";
299 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
300 $_SERVER["REMOTE_ADDR"],
301 $_POST["recaptcha_challenge_field"],
302 $_POST["recaptcha_response_field"]);
303 if (!$resp->is_valid) {
304 $this->failed_captcha = true;
305 }
306 }
307 if (isset($_POST['captcha']) || $resp->is_valid) {
308 $sql = ("INSERT INTO comments (date_posted, author,
309 text, note)
310 VALUES(NOW(), ?, ?, ?)");
311 $stmt = $this->db->prepare($sql);
312 // Checks are needed here (no blank text,
313 // and a default author needs to be set
314 // for no-javascript users.
315 $stmt->bind_param('sss',
316 $_POST['name'],
317 $_POST['text'],
318 $this->id);
319 $stmt->execute();
320 }
321 }
322
323 private function display_comment_link() {
324 if ($this->number_of_comments > 0) {
325 $anchor_text = "comments($this->number_of_comments)/";
326 } else {
327 $anchor_text = "comment?";
328 }
329 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
330 $url = $this->url . 'comments/';
331 } else {
332 $url = $this->url . '/comments/';
333 }
334 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
335 }
336
337 private function display_comments() {
338 echo "<div id='comments'>";
339 $sql= "SELECT date_posted, author, text
340 FROM comments WHERE note = ?
341 ORDER BY date_posted DESC";
342 $result = $this->query($sql, 'd', $this->id);
343 foreach ($result as $row => $entry) {
344 $date_posted = $entry['date_posted'];
345 $author = $entry['author'];
346 $text = htmlspecialchars($entry['text']);
347 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
348 echo <<<END_OF_COMMENT
349 <div class='comment'>
350 $head
351 $text
352 </div>
353 END_OF_COMMENT;
354 }
355 echo "</div>";
356 }
357
358 private function display_comment_form() {
359 $publickey = $this->recaptcha_publickey;
360 echo <<<END_CAPTCHA_STYLE
361 <script type="text/javascript">
362 Recaptcha.create("$publickey",
363 "recaptcha_div",
364 {
365 theme : 'custom',
366 custom_theme_widget: 'recaptcha_widget',
367 callback: Recaptcha.focus_response_field
368 });
369 </script>
370 END_CAPTCHA_STYLE;
371 require_once('includes/recaptchalib.php');
372 $url = $this->url . "verify";
373 echo "<form id='comment_form' method='post' action='$url'>";
374 echo <<<END_OF_FORM
375 <div id="comment">
376 <h3>comment:</h3>
377 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
378 <h3>name:</h3>
379 <input type=text name="name" id="comment_name">
380
381 <nowiki>
382 <div id="recaptcha_widget">
383 <br>
384 <h3><b>what's this say</b>?</h3>
385 <br>
386 <div id="recaptcha_image"></div>
387 <br><br><br>
388 <span class="recaptcha_only_if_image"><br><br><br></span>
389 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
390 <br><br>
391 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
392 <span class="recaptcha_help">
393 <a href="javascript:Recaptcha.reload()">another?</a> /
394 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
395 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
396 </span>
397 </div>
398 END_OF_FORM;
399 echo recaptcha_get_html($this->recaptcha_publickey);
400 if ($this->failed_captcha) {
401 echo <<<END_OF_ERRORS
402 <div id="not_human">
403 reCAPTCHA said you're not human, <br>
404 try again?
405 </div>
406 <input id="submit" class="submit" type="submit" value="post comment">
407 </form>
408 </div>
409 END_OF_ERRORS;
410 } else {
411 echo <<<END_OF_ERRORS
412 <div id="not_human">
413 reCAPTCHA said you're not human, <br>
414 try again?
415 </div>
416 <div id="blank_comment">
417 but you didn't write anything! <br>
418 </div>
419 END_OF_ERRORS;
420 }
421 echo <<<END_OF_FORM
422 <input id="submit" class="submit" type="submit" value="post comment">
423 </form>
424 </div>
425 END_OF_FORM;
426 }
427 }
428
429
430 class archive extends cms {
431
432 public function __construct() {
433 parent::__construct();
434 }
435
436 private function check_exists() {
437 $sql = "SELECT COUNT(*) FROM notes
438 WHERE url = ?";
439 $results = $this->query($sql, "s", $_GET['note']);
440 if ($results[0]["COUNT(*)"] != 1) {
441 $this->not_found();
442 }
443 }
444
445 public function display() {
446 require_once("view/archive.php");
447 }
448
449 public function display_notes() {
450 switch (true) {
451 case (isset($_GET['year']) && !isset($_GET['month'])
452 && !isset($_GET['day'])):
453 $sql = "SELECT title, url, date_posted, text
454 FROM notes WHERE YEAR(date_posted) = ?
455 ORDER BY date_posted DESC";
456 $result = $this->query($sql, "d",
457 $_GET['year']);
458 break;
459 case (isset($_GET['year']) && isset($_GET['month'])
460 && !isset($_GET['day'])):
461 $sql = "SELECT title, url, date_posted, text
462 FROM notes WHERE YEAR(date_posted) = ?
463 AND MONTH(date_posted) = ?
464 ORDER BY date_posted DESC";
465 $result = $this->query($sql, "dd",
466 $_GET['year'], $_GET['month']);
467 break;
468 case (isset($_GET['year']) && isset($_GET['month'])
469 && isset($_GET['day'])):
470 $sql = "SELECT title, url, date_posted, text
471 FROM notes WHERE YEAR(date_posted) = ?
472 AND MONTH(date_posted) = ?
473 AND DAY(date_posted) = ?
474 ORDER BY date_posted DESC";
475 $result = $this->query($sql, "ddd",
476 $_GET['year'], $_GET['month'],
477 $_GET['day']);
478 break;
479 }
480 if (count($result) >= 1) {
481 echo "<div id='notes'>";
482 foreach ($result as $row => $entry) {
483 $title = $entry['title'];
484 $url = '/note/' . $entry['url'];
485 $date_posted = explode("-", $entry['date_posted']);
486 $year_posted = $date_posted[0];
487 $month_posted = $date_posted[1];
488 $datetime_posted = explode(' ', $date_posted[2]);
489 $day_posted = $datetime_posted[0];
490 echo "<div class='note'>";
491 echo "<h1><span class='date'>";
492 echo "$year_posted/$month_posted/$day_posted/";
493 echo "</span><a href='$url'>$title</a></h1>";
494 echo $entry['text'];
495 echo "</div>";
496 }
497 echo "</div>";
498 } else {
499 echo "<br>";
500 echo "<h1>sorry, nothing here</h2>";
501 echo "<pre>Empty set (0.00 sec)</pre>";
502 }
503 }
504
505 }
506
507
508 class rss extends cms {
509 public function display() {
510 $result = $this->db->query("SELECT date_posted, title, text, url
511 FROM notes ORDER BY date_posted DESC
512 LIMIT 5");
513 echo <<<END_OF_ENTRY
514 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
515 <channel>
516 <title>dylansserver.com/notes/rss</title>
517 <link>http://dylansserver.com/notes</link>
518 <description>dylansserver.com/notes/rss</description>
519 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
520 END_OF_ENTRY;
521 while ($entry = $result->fetch_object()) {
522 $title = $entry->title;
523 $date_posted = $entry->date_posted;
524 $url = "http://dylansserver.com/note/" . $entry->url;
525 $text = $entry->text;
526 $text = strip_tags($text);
527 $end_of_first_sentence = strpos($text, '.');
528 if ($end_of_first_sentence) {
529 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
530 if ($end_of_second_sentence) {
531 $description = substr($text, '0', ($end_of_second_sentence + 1));
532 } else {
533 $description = substr($text, '0', ($end_of_first_sentence + 1));
534 }
535 }
536 echo <<<END_OF_ENTRY
537 <item>
538 <title>$title</title>
539 <link>$url</link>
540 <guid>$url</guid>
541 <description>$description</description>
542 </item>
543 END_OF_ENTRY;
544 }
545 echo "</channel>";
546 echo "</rss>";
547
548 }
549 }
550
551
552 class notFound extends Exception {
553
554 public function __construct() {
555 header('HTTP/1.0 404 Not Found');
556 ob_end_clean();
557 include('404.php');
558 exit();
559 }
560
561 }
562
563
564 class captcha extends cms {
565
566 public function display() {
567 $challenge = $_GET['challenge'];
568 $response = $_GET['response'];
569 $remoteip = $_SERVER['REMOTE_ADDR'];
570 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
571 curl_setopt ($curl, CURLOPT_POST, 4);
572 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
573 $result = curl_exec ($curl);
574 curl_close ($curl);
575 }
576
577 }
578
579 ?>