Fixed comment display broken from merge
[dylansserver.git] / index.php
1 <?php
2
3 abstract class cms {
4
5 private $config_file = '/etc/dylanstestserver.ini';
6 protected $db;
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
9 public $title;
10 public $home_link;
11
12 public function __construct() {
13 $config = parse_ini_file($this->config_file, true);
14 $this->db = new mysqli(
15 $config[database]['domain'],
16 $config[database]['user'],
17 $config[database]['password'],
18 $config[database]['database']);
19 if (mysqli_connect_errno()) {
20 echo "Problem connecting to database: ";
21 echo mysqli_connect_error();
22 exit();
23 }
24 $this->recaptcha_publickey = $config['recaptcha']['publickey'];
25 $this->recaptcha_privatekey = $config['recaptcha']['privatekey'];
26 $this->title = $config['site']['default_title'];
27 $this->home_link = $config['site']['home_link'];
28 ob_start();
29 }
30
31 public static function determine_type() {
32 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
33 return 'page';
34 } else if (isset($_GET['year'])) {
35 return 'archive';
36 } else if (isset($_GET['note'])) {
37 return 'note';
38 } else if ($_SERVER['REQUEST_URI'] == '/') {
39 return 'index';
40 } else if (isset($_GET['project'])) {
41 return 'project';
42 } else if (isset($_GET['challenge'])) {
43 return 'captcha';
44 }
45 }
46
47 public function query() {
48 $args = func_get_args();
49 $statement = $this->db->prepare($args[0]);
50 $args = array_slice($args, 1);
51 call_user_func_array(array($statement, 'bind_param'), &$args);
52 $statement->execute();
53 $return = array();
54 $statement->store_result();
55 $row = array();
56 $data = $statement->result_metadata();
57 $fields = array();
58 $fields[0] = &$statement;
59 while($field = $data->fetch_field()) {
60 $fields[] = &$row[$field->name];
61 }
62 call_user_func_array("mysqli_stmt_bind_result", $fields);
63 $i = 0;
64 while ($statement->fetch()) {
65 foreach ($row as $key=>$value) $return[$i][$key] = $value;
66 $i++;
67 }
68 $statement->free_result();
69 return $return;
70 }
71
72 public function display_head($title = "dylanstestserver",
73 $home_link = "/") {
74 $scripts = "";
75 $stylesheets = "<link href='/includes/style.css' rel='stylesheet' type='text/css'>";
76 if (cms::determine_type() == "index") {
77 $scripts = "<script type='text/javascript' src='/includes/all.js'></script>";
78 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylanstestserver.com&amp;ucn_task=conformance#";
79 } else if ($this->determine_type() == 'note') {
80 $scripts = "<script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>";
81 $scripts .= "<script type='text/javascript' src='/includes/jquery-core.js'></script>";
82 $scripts .= "<script type='text/javascript' src='/includes/jquery-all-components.js'></script>";
83 $scripts .= "<script type='text/javascript' src='/includes/ajax.js'></script>";
84 }
85 echo <<<END_OF_HEAD
86 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
87 "http://www.w3.org/TR/html4/loose.dtd">
88
89 <html>
90 <head>
91 <meta name="generator" content=
92 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
93 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
94
95 <title>$this->title</title>
96 <link rel="icon" href="favicon.ico" type="image/png">
97 $stylesheets
98 $scripts
99 </head>
100
101 <body>
102 <div id="structure">
103 <div id="banner">
104 <a href="$this->home_link">
105 <img src="/images/dylanstestserver.png" alt="dylanstestserver"
106 border="0"></a>
107 </div>
108
109 <div id="content">
110 END_OF_HEAD;
111 }
112
113 public function display_contact() {
114 echo <<<END_OF_CONTACT
115 <div id="contact_me"><h1><a href=
116 "mailto:dylan@psu.edu">dylan</a></h1><a href=
117 "mailto:dylan@psu.edu">@psu.edu</a>
118 </div>
119 END_OF_CONTACT;
120 }
121
122 public function display_close($show_contact = true) {
123 if ($show_contact) {
124 $this->display_contact();
125 }
126 echo <<<END_OF_CLOSE
127 </div>
128 <br>
129 <br>
130 </div>
131 </body>
132 </html>
133 END_OF_CLOSE;
134 ob_flush();
135 }
136
137 }
138
139
140 class index extends cms {
141
142 public function display() {
143 $this->display_head();
144 $this->display_exhibits();
145 echo "<ul id='portfolio' style='text-align:right'>";
146 $this->list_projects();
147 echo <<<OTHER_PROJECTS
148 <li>
149 <h3>things i've done for others:</h3>
150 </li>
151
152 <li><a href=
153 "http://activehamptons.com">activehamptons.com</a></li>
154
155 <li><a href=
156 "http://transfishing.com">transfishing.com</a></li>
157
158 <li>
159 <h3>something i've worked on:</h3>
160 </li>
161
162 <li><a href=
163 "http://tempositions.com">tempositions.com</a></li>
164
165 <li>
166 <h3>my repositories:</h3>
167 </li>
168
169 <li><a href=
170 "git">git://dylanstestserver.com</a></li>
171
172 <li>
173 <h3>some notes:</h3>
174 </li>
175
176 <li><a href=
177 "/notes/">here</a></li>
178
179 <li>
180 </li>
181 OTHER_PROJECTS;
182 // Because of the CSS necessary for the animations,
183 // the contact link needs to be in #portfolio to clear
184 // the floats.
185 echo "<li>";
186 $this->display_contact();
187 echo "</li>";
188 echo "</ul>";
189 $this->display_close($show_contact = false);
190 }
191
192 protected function display_exhibits() {
193 $sql = "SELECT text FROM projects";
194 $result = $this->db->query($sql);
195 while ($entry = $result->fetch_object()) {
196 echo "<div class='exhibit'>";
197 echo $entry->text;
198 echo "</div>";
199 }
200 }
201
202 private function list_projects() {
203 echo <<<HEREDOC
204 <li>
205 <h3>my projects:</h3>
206 </li>
207 HEREDOC;
208 $sql = "SELECT title FROM projects";
209 $result = $this->db->query($sql);
210 while ($entry = $result->fetch_object()) {
211 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
212 }
213 }
214
215 }
216
217
218 class project extends index {
219
220 protected function display_exhibits() {
221 echo "<div id='exhibit'>";
222 $sql = "SELECT text FROM projects
223 WHERE title = ?";
224 $result = $this->query($sql, "s", $_GET['project']);
225 if ($result = $result[0]['text']) {
226 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
227 echo $text;
228 echo "</div>";
229 } else {
230 throw new notFound();
231 }
232 }
233
234 }
235
236
237 class page extends cms {
238
239 private $page = 1;
240 private $offset = 0;
241 private $notes_per_page = 4;
242 private $number_of_pages = 1;
243
244 public function __construct() {
245 parent::__construct();
246 $this->page_offset();
247 }
248
249 private function page_offset() {
250 $sql = "SELECT COUNT(*) FROM notes";
251 $result = $this->db->query($sql);
252 $result = $result->fetch_array();
253 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
254 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
255 $this->page = (int) $_GET['page'];
256 } else {
257 throw new notFound();
258 }
259 if ($this->page > $this->number_of_pages) {
260 throw new notFound();
261 }
262 if ($this->page < 1) {
263 throw new notFound();
264 }
265 $this->offset = ($this->page - 1) * $this->notes_per_page;
266 }
267
268 public function display() {
269 $this->display_head();
270 echo "<div id='notes'>";
271 $sql = "SELECT date_posted, title, url, text
272 FROM notes ORDER BY date_posted DESC
273 LIMIT ?, ?";
274 $result = $this->query($sql, "ii",
275 $this->offset,
276 $this->notes_per_page);
277 foreach ($result as $row => $entry) {
278 $title = $entry['title'];
279 $url = '/note/' . $entry['url'];
280 $date_posted = explode("-", $entry['date_posted']);
281 $year_posted = $date_posted[0];
282 $month_posted = $date_posted[1];
283 $datetime_posted = explode(' ', $date_posted[2]);
284 $day_posted = $datetime_posted[0];
285 echo "<div class='note'>";
286 echo "<h2><span style='color:grey;'>$year_posted/$month_posted/$day_posted/</span><a href='$url'>$title</a></h2>";
287 echo $entry['text'];
288 echo "</div>";
289 }
290 echo "</div>";
291 $this->write_navigation();
292 $this->display_close();
293 }
294
295 private function write_navigation() {
296 echo "<div id='navigation'>";
297 echo "<h2>";
298 if($this->page > 1){
299 $previous_page = $this->page - 1;
300 echo "<a href='/notes/page/$previous_page'>prev</a>";
301 }
302 if($this->page < $this->number_of_pages) {
303 $forward_page = $this->page + 1;
304 echo " <a href='/notes/page/$forward_page'>next</a>";
305 }
306 echo "</h2>";
307 echo "</div>";
308 }
309
310 }
311
312
313 class note extends cms {
314
315 private $id;
316 private $comments_enabled = false;
317 private $failed_captcha;
318 public $url;
319 public $title;
320 public $year_posted;
321 public $month_posted;
322 public $day_posted;
323 public $text;
324 public $number_of_comments;
325
326 public function __construct() {
327 parent::__construct();
328 if (isset($_GET['comments'])) {
329 $this->comments_enabled = true;
330 }
331 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
332 if (isset($_GET['verify'])) {
333 $url = substr($url, 0, (strlen($url)-6));
334 }
335 $this->url = $url;
336 $sql = "SELECT title, date_posted, text, id
337 FROM notes WHERE url = ?";
338 $result = $this->query($sql, "s",
339 $_GET['note']);
340 if ($result) {
341 $entry = $result[0];
342 $this->id = $entry["id"];
343 $this->title = $entry["title"];
344 $date_posted = explode("-", $entry["date_posted"]);
345 $this->year_posted = $date_posted[0];
346 $this->month_posted = $date_posted[1];
347 $datetime_posted = explode(' ', $date_posted[2]);
348 $this->day_posted = $datetime_posted[0];
349 $this->text = $entry["text"];
350 } else {
351 throw new notFound();
352 }
353 $sql = "SELECT COUNT(*) FROM comments
354 WHERE note = $this->id";
355 $result = $this->db->query($sql);
356 $result = $result->fetch_array();
357 $this->number_of_comments = $result[0];
358 if (isset($_GET['verify'])) {
359 $this->verify();
360 }
361 }
362
363 public function display() {
364 $this->display_head();
365 $this->display_note();
366 if ($this->comments_enabled) {
367 $this->display_comments();
368 $this->display_comment_form();
369 }
370 $this->write_navigation();
371 $this->display_close();
372 }
373
374 private function verify() {
375 if (!isset($_POST['captcha'])) {
376 require_once('includes/recaptchalib.php');
377 echo "<br>";
378 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
379 $_SERVER["REMOTE_ADDR"],
380 $_POST["recaptcha_challenge_field"],
381 $_POST["recaptcha_response_field"]);
382 if (!$resp->is_valid) {
383 $this->failed_captcha = true;
384 }
385 }
386 if (isset($_POST['captcha']) || $resp->is_valid) {
387 $sql = ("INSERT INTO comments (date_posted, author,
388 email, text, note)
389 VALUES(NOW(), ?, ?, ?, ?)");
390 $stmt = $this->db->prepare($sql);
391 // Checks are needed here (no blank text,
392 // and a default author / email need to be set
393 // for no-javascript users.
394 $stmt->bind_param('ssss',
395 htmlspecialchars($_POST['name']),
396 htmlspecialchars($_POST['email']),
397 htmlspecialchars($_POST['text']),
398 $this->id);
399 $stmt->execute();
400 }
401 }
402
403 private function display_note() {
404 echo <<<END_OF_NOTE
405 <div id='note'>
406 <h2><span style='color:grey;'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h2>
407 $this->text
408 </div>
409 END_OF_NOTE;
410 }
411
412 private function write_navigation() {
413 echo <<<END_OF_NAVIGATION
414 <br>
415 <div id='navigation'>
416 <h2>
417 END_OF_NAVIGATION;
418 if (!$this->comments_enabled) {
419 $this->display_comment_link();
420 }
421 echo <<<END_OF_NAVIGATION
422 <a href="/notes/">back to notes</a>/
423 </h2>
424 </div>
425 END_OF_NAVIGATION;
426 }
427
428 private function display_comment_link() {
429 if ($this->number_of_comments > 0) {
430 $anchor_text = "comments($this->number_of_comments)/";
431 } else {
432 $anchor_text = "comment?";
433 }
434 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
435 $url = $this->url . 'comments/';
436 } else {
437 $url = $this->url . '/comments/';
438 }
439 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
440 }
441
442 private function display_comments() {
443 echo "<div id='comments'>";
444 $sql= "SELECT date_posted, author, email, text
445 FROM comments WHERE note = ?
446 ORDER BY date_posted DESC";
447 $result = $this->query($sql, 'd', $this->id);
448 foreach ($result as $row => $entry) {
449 $date_posted = $entry['date_posted'];
450 $author = $entry['author'];
451 $email = $entry['email'];
452 $text = htmlspecialchars($entry['text']);
453 if ($email == '') {
454 $head = "<h3>$author</h3>";
455 } else {
456 $head = "<h3><a href='mailto:$email'>$author</a></h3>";
457 }
458 echo <<<END_OF_COMMENT
459 <div class='comment'>
460 $head
461 $text
462 </div>
463 END_OF_COMMENT;
464 }
465 echo "</div>";
466 }
467
468 private function display_comment_form() {
469 $publickey = $this->recaptcha_publickey;
470 echo <<<END_CAPTCHA_STYLE
471 <script type="text/javascript">
472 Recaptcha.create("$publickey",
473 "recaptcha_div",
474 {
475 theme : 'custom',
476 custom_theme_widget: 'recaptcha_widget',
477 callback: Recaptcha.focus_response_field
478 });
479 </script>
480 END_CAPTCHA_STYLE;
481 require_once('includes/recaptchalib.php');
482 $url = $this->url . "verify";
483 echo "<form id='comment_form' method='post' action='$url'>";
484 echo <<<END_OF_FORM
485 <div id="comment">
486 <h3>comment:</h3>
487 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
488 <h3>name:</h3>
489 <input type=text name="name" id="comment_name">
490 <h3>email:</h3>
491 <input type=text name="email" id="comment_email"><br>
492
493 <nowiki>
494 <div id="recaptcha_widget">
495 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
496 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
497 <span style="font-size:80%;">
498 ( <a href="javascript:Recaptcha.reload()">another</a> /
499 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
500 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
501 </span>
502 <br><br>
503 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
504 <br><br>
505 <div style="float:right;position:relative;width:100px;">
506 <div id="recaptcha_image"></div>
507 </div>
508 <br><br><br><br>
509 </div>
510 END_OF_FORM;
511 echo recaptcha_get_html($this->recaptcha_publickey);
512 if ($this->failed_captcha) {
513 echo <<<END_OF_FORM
514 <span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>reCAPTCHA said you're not human,</span>
515 <input id="submit" class="submit" type="submit" value="try again?">
516 </form>
517 </div>
518 END_OF_FORM;
519 } else {
520 echo <<<END_OF_FORM
521 <input id="submit" class="submit" type="submit" value="post comment">
522 </form>
523 </div>
524 END_OF_FORM;
525 }
526 }
527 }
528
529
530 class archive extends cms {
531
532 public function __construct() {
533 parent::__construct();
534 }
535
536 private function check_exists() {
537 $sql = "SELECT COUNT(*) FROM notes
538 WHERE url = ?";
539 $results = $this->query($sql, "s", $_GET['note']);
540 if ($results[0]["COUNT(*)"] != 1) {
541 $this->not_found();
542 }
543 }
544
545 public function display() {
546 $this->display_head();
547 switch (true) {
548 case (isset($_GET['year']) && !isset($_GET['month'])
549 && !isset($_GET['day'])):
550 $sql = "SELECT title, url, date_posted, text
551 FROM notes WHERE YEAR(date_posted) = ?
552 ORDER BY date_posted DESC";
553 $result = $this->query($sql, "d",
554 $_GET['year']);
555 break;
556 case (isset($_GET['year']) && isset($_GET['month'])
557 && !isset($_GET['day'])):
558 $sql = "SELECT title, url, date_posted, text
559 FROM notes WHERE YEAR(date_posted) = ?
560 AND MONTH(date_posted) = ?
561 ORDER BY date_posted DESC";
562 $result = $this->query($sql, "dd",
563 $_GET['year'], $_GET['month']);
564 break;
565 case (isset($_GET['year']) && isset($_GET['month'])
566 && isset($_GET['day'])):
567 $sql = "SELECT title, url, date_posted, text
568 FROM notes WHERE YEAR(date_posted) = ?
569 AND MONTH(date_posted) = ?
570 AND DAY(date_posted) = ?
571 ORDER BY date_posted DESC";
572 $result = $this->query($sql, "ddd",
573 $_GET['year'], $_GET['month'],
574 $_GET['day']);
575 break;
576 }
577 if (count($result) >= 1) {
578 echo "<div id='notes'>";
579 foreach ($result as $row => $entry) {
580 $title = $entry['title'];
581 $url = '/note/' . $entry['url'];
582 $date_posted = explode("-", $entry['date_posted']);
583 $year_posted = $date_posted[0];
584 $month_posted = $date_posted[1];
585 $datetime_posted = explode(' ', $date_posted[2]);
586 $day_posted = $datetime_posted[0];
587 echo "<div class='note'>";
588 echo "<h2><span style='color:grey;'>";
589 echo "$year_posted/$month_posted/$day_posted/";
590 echo "</span><a href='$url'>$title</a></h2>";
591 echo $entry['text'];
592 echo "</div>";
593 }
594 echo "</div>";
595 $this->write_navigation();
596 } else {
597 echo "<br>";
598 echo "<h2 style='font-family:sans-serif;'>sorry, nothing here</h2>";
599 echo "<pre>Empty set (0.00 sec)</pre>";
600 }
601 $this->display_close();
602 }
603
604 private function write_navigation() {
605 echo "<br>";
606 echo "<div id='navigation'>";
607 echo "<h2>";
608 // fill me in!
609 echo "</h2>";
610 echo "</div>";
611 }
612
613 }
614
615
616 class notFound extends Exception {
617
618 public function __construct() {
619 header('HTTP/1.0 404 Not Found');
620 ob_end_clean();
621 include('404.php');
622 exit();
623 }
624
625 }
626
627
628 class captcha extends cms {
629
630 public function display() {
631 $challenge = $_GET['challenge'];
632 $response = $_GET['response'];
633 $remoteip = $_SERVER['REMOTE_ADDR'];
634 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
635 curl_setopt ($curl, CURLOPT_POST, 4);
636 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
637 $result = curl_exec ($curl);
638 curl_close ($curl);
639 }
640
641 }
642
643
644 ## now actually do something:
645 switch (cms::determine_type()) {
646 case 'index':
647 $index = new index();
648 $index->display();
649 break;
650 case 'project':
651 $project = new project();
652 $project->display();
653 break;
654 case 'note':
655 $note = new note;
656 $note->display();
657 break;
658 case 'page':
659 $page = new page;
660 $page->display();
661 break;
662 case 'archive':
663 $archive = new archive;
664 $archive->display();
665 break;
666 case "captcha":
667 $captcha = new captcha;
668 $captcha->display();
669 break;
670 }
671
672 ?>