Merged branch 'dev' into rss
[dylansserver.git] / index.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 display_head($title = "dylansserver",
76 $home_link = "/") {
77 $scripts = $this->scripts;
78 $stylesheets = "<link href='/includes/style.css' rel='stylesheet' type='text/css'>";
79 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylansserver.com&amp;ucn_task=conformance#";
80 echo <<<END_OF_HEAD
81 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
82 "http://www.w3.org/TR/html4/loose.dtd">
83
84 <html>
85 <head>
86 <meta name="generator" content=
87 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
88 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
89
90 <title>$this->title</title>
91 <link rel="icon" href="/favicon.ico" type="image/png">
92 $stylesheets
93 $scripts
94 </head>
95
96 <body>
97 <div id="structure">
98 <div id="banner">
99 <a href="$this->home_link">
100 <img src="/images/dylansserver.png" alt="dylansserver"
101 border="0"></a>
102 </div>
103
104 <div id="content">
105 END_OF_HEAD;
106 }
107
108 public function display_contact() {
109 echo <<<END_OF_CONTACT
110 <div id="contact_me"><h1><a href=
111 "mailto:dylan@psu.edu">dylan</a></h1><a href=
112 "mailto:dylan@psu.edu">@psu.edu</a>
113 </div>
114 END_OF_CONTACT;
115 }
116
117 public function display_close($show_contact = true) {
118 if ($show_contact) {
119 $this->display_contact();
120 }
121 echo <<<END_OF_CLOSE
122 </div>
123 <br>
124 <br>
125 </div>
126 </body>
127 </html>
128 END_OF_CLOSE;
129 ob_flush();
130 }
131
132 }
133
134
135 class index extends cms {
136
137 public function display() {
138 $this->scripts = "<script type='text/javascript' src='/includes/index.js'></script>";
139 $this->display_head();
140 $this->display_exhibits();
141 echo "<ul id='portfolio' style='text-align:right'>";
142 $this->list_projects();
143 echo <<<OTHER_PROJECTS
144 <li>
145 <h3>things i've done for others:</h3>
146 </li>
147
148 <li><a href=
149 "http://activehamptons.com">activehamptons.com</a></li>
150
151 <li><a href=
152 "http://transfishing.com">transfishing.com</a></li>
153
154 <li>
155 <h3>something i've worked on:</h3>
156 </li>
157
158 <li><a href=
159 "http://tempositions.com">tempositions.com</a></li>
160
161 <li>
162 <h3>my repositories:</h3>
163 </li>
164
165 <li><a href=
166 "git">git://dylansserver.com</a></li>
167
168 <li>
169 <h3>some notes:</h3>
170 </li>
171
172 <li><a href=
173 "/notes/">here</a></li>
174
175 <li>
176 </li>
177 OTHER_PROJECTS;
178 // Because of the CSS necessary for the animations,
179 // the contact link needs to be in #portfolio to clear
180 // the floats.
181 echo "<li>";
182 $this->display_contact();
183 echo "</li>";
184 echo "</ul>";
185 $this->display_close($show_contact = false);
186 }
187
188 protected function display_exhibits() {
189 echo "<div id='exhibit'>";
190 $sql = "SELECT text FROM projects";
191 $result = $this->db->query($sql);
192 while ($entry = $result->fetch_object()) {
193 echo $entry->text;
194 }
195 echo "</div>";
196 }
197
198 private function list_projects() {
199 echo <<<HEREDOC
200 <li>
201 <h3>my projects:</h3>
202 </li>
203 HEREDOC;
204 $sql = "SELECT title FROM projects";
205 $result = $this->db->query($sql);
206 while ($entry = $result->fetch_object()) {
207 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
208 }
209 }
210
211 }
212
213
214 class project extends index {
215
216 protected function display_exhibits() {
217 echo "<div id='exhibit'>";
218 $sql = "SELECT text FROM projects
219 WHERE title = ?";
220 $result = $this->query($sql, "s", $_GET['project']);
221 if ($result = $result[0]['text']) {
222 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
223 echo $text;
224 echo "</div>";
225 } else {
226 throw new notFound();
227 }
228 }
229
230 }
231
232
233 class page extends cms {
234
235 private $page = 1;
236 private $offset = 0;
237 private $notes_per_page = 4;
238 private $number_of_pages = 1;
239
240 public function __construct() {
241 parent::__construct();
242 $this->page_offset();
243 }
244
245 private function page_offset() {
246 $sql = "SELECT COUNT(*) FROM notes";
247 $result = $this->db->query($sql);
248 $result = $result->fetch_array();
249 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
250 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
251 $this->page = (int) $_GET['page'];
252 } else {
253 throw new notFound();
254 }
255 if ($this->page > $this->number_of_pages) {
256 throw new notFound();
257 }
258 if ($this->page < 1) {
259 throw new notFound();
260 }
261 $this->offset = ($this->page - 1) * $this->notes_per_page;
262 }
263
264 public function display() {
265 $this->display_head();
266 echo "<div id='notes'>";
267 $sql = "SELECT date_posted, title, url, text
268 FROM notes ORDER BY date_posted DESC
269 LIMIT ?, ?";
270 $result = $this->query($sql, "ii",
271 $this->offset,
272 $this->notes_per_page);
273 foreach ($result as $row => $entry) {
274 $title = $entry['title'];
275 $url = '/note/' . $entry['url'];
276 $date_posted = explode("-", $entry['date_posted']);
277 $year_posted = $date_posted[0];
278 $month_posted = $date_posted[1];
279 $datetime_posted = explode(' ', $date_posted[2]);
280 $day_posted = $datetime_posted[0];
281 $text = $entry['text'];
282 echo <<<END_NOTE
283 <div class='note'>
284 <h1>
285 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
286 </h1>
287 $text
288 </div>
289 END_NOTE;
290 }
291 echo "</div>";
292 $this->write_navigation();
293 $this->display_close();
294 }
295
296 private function write_navigation() {
297 echo "<div id='navigation'>";
298 echo "<h1>";
299 if($this->page > 1){
300 $previous_page = $this->page - 1;
301 echo "<a href='/notes/page/$previous_page'>prev</a>";
302 }
303 if($this->page < $this->number_of_pages) {
304 $forward_page = $this->page + 1;
305 echo " <a href='/notes/page/$forward_page'>next</a>";
306 }
307 echo "</h1>";
308 echo "</div>";
309 }
310
311 }
312
313
314 class note extends cms {
315
316 private $id;
317 private $comments_enabled = false;
318 private $failed_captcha;
319 public $url;
320 public $title;
321 public $year_posted;
322 public $month_posted;
323 public $day_posted;
324 public $text;
325 public $number_of_comments;
326
327 public function __construct() {
328 if (isset($_GET['comments'])) {
329 $this->scripts = "
330 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
331 <script type='text/javascript' src='/includes/comment.js'></script>";
332 }
333 parent::__construct();
334 if (isset($_GET['comments'])) {
335 $this->comments_enabled = true;
336 }
337 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
338 if (isset($_GET['verify'])) {
339 $url = substr($url, 0, (strlen($url)-6));
340 }
341 $this->url = $url;
342 $sql = "SELECT title, date_posted, text, id
343 FROM notes WHERE url = ?";
344 $result = $this->query($sql, "s",
345 $_GET['note']);
346 if ($result) {
347 $entry = $result[0];
348 $this->id = $entry["id"];
349 $this->title = $entry["title"];
350 $date_posted = explode("-", $entry["date_posted"]);
351 $this->year_posted = $date_posted[0];
352 $this->month_posted = $date_posted[1];
353 $datetime_posted = explode(' ', $date_posted[2]);
354 $this->day_posted = $datetime_posted[0];
355 $this->text = $entry["text"];
356 } else {
357 throw new notFound();
358 }
359 $sql = "SELECT COUNT(*) FROM comments
360 WHERE note = $this->id";
361 $result = $this->db->query($sql);
362 $result = $result->fetch_array();
363 $this->number_of_comments = $result[0];
364 if (isset($_GET['verify'])) {
365 $this->verify();
366 }
367 }
368
369 public function display() {
370 $this->display_head();
371 $this->display_note();
372 if ($this->comments_enabled) {
373 $this->display_comments();
374 $this->display_comment_form();
375 }
376 $this->write_navigation();
377 $this->display_close();
378 }
379
380 private function verify() {
381 if (!isset($_POST['captcha'])) {
382 require_once('includes/recaptchalib.php');
383 echo "<br>";
384 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
385 $_SERVER["REMOTE_ADDR"],
386 $_POST["recaptcha_challenge_field"],
387 $_POST["recaptcha_response_field"]);
388 if (!$resp->is_valid) {
389 $this->failed_captcha = true;
390 }
391 }
392 if (isset($_POST['captcha']) || $resp->is_valid) {
393 $sql = ("INSERT INTO comments (date_posted, author,
394 text, note)
395 VALUES(NOW(), ?, ?, ?)");
396 $stmt = $this->db->prepare($sql);
397 // Checks are needed here (no blank text,
398 // and a default author needs to be set
399 // for no-javascript users.
400 $stmt->bind_param('sss',
401 htmlspecialchars($_POST['name']),
402 htmlspecialchars($_POST['text']),
403 $this->id);
404 $stmt->execute();
405 }
406 }
407
408 private function display_note() {
409 echo <<<END_OF_NOTE
410 <div id='note'>
411 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
412 $this->text
413 </div>
414 END_OF_NOTE;
415 }
416
417 private function write_navigation() {
418 echo <<<END_OF_NAVIGATION
419 <br>
420 <div id='navigation'>
421 <h1>
422 END_OF_NAVIGATION;
423 if (!$this->comments_enabled) {
424 $this->display_comment_link();
425 }
426 echo <<<END_OF_NAVIGATION
427 <a href="/notes/">back to notes</a>/
428 </h1>
429 </div>
430 END_OF_NAVIGATION;
431 }
432
433 private function display_comment_link() {
434 if ($this->number_of_comments > 0) {
435 $anchor_text = "comments($this->number_of_comments)/";
436 } else {
437 $anchor_text = "comment?";
438 }
439 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
440 $url = $this->url . 'comments/';
441 } else {
442 $url = $this->url . '/comments/';
443 }
444 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
445 }
446
447 private function display_comments() {
448 echo "<div id='comments'>";
449 $sql= "SELECT date_posted, author, text
450 FROM comments WHERE note = ?
451 ORDER BY date_posted DESC";
452 $result = $this->query($sql, 'd', $this->id);
453 foreach ($result as $row => $entry) {
454 $date_posted = $entry['date_posted'];
455 $author = $entry['author'];
456 $text = htmlspecialchars($entry['text']);
457 $head = "<h3>$author</h3>";
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
491 <nowiki>
492 <div id="recaptcha_widget">
493 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
494 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
495 <span style="font-size:80%;">
496 ( <a href="javascript:Recaptcha.reload()">another</a> /
497 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
498 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
499 </span>
500 <br><br>
501 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
502 <br><br>
503 <div style="float:right;position:relative;width:100px;">
504 <div id="recaptcha_image"></div>
505 </div>
506 <br><br><br><br>
507 </div>
508 END_OF_FORM;
509 echo recaptcha_get_html($this->recaptcha_publickey);
510 if ($this->failed_captcha) {
511 echo <<<END_OF_ERRORS
512 <div id="not_human">
513 reCAPTCHA said you're not human, <br>
514 try again?
515 </div>
516 <input id="submit" class="submit" type="submit" value="post comment">
517 </form>
518 </div>
519 END_OF_ERRORS;
520 } else {
521 echo <<<END_OF_ERRORS
522 <div id="not_human">
523 reCAPTCHA said you're not human, <br>
524 try again?
525 </div>
526 <div id="blank_comment">
527 but you didn't write anything! <br>
528 </div>
529 END_OF_ERRORS;
530 }
531 echo <<<END_OF_FORM
532 <input id="submit" class="submit" type="submit" value="post comment">
533 </form>
534 </div>
535 END_OF_FORM;
536 }
537 }
538
539
540 class archive extends cms {
541
542 public function __construct() {
543 parent::__construct();
544 }
545
546 private function check_exists() {
547 $sql = "SELECT COUNT(*) FROM notes
548 WHERE url = ?";
549 $results = $this->query($sql, "s", $_GET['note']);
550 if ($results[0]["COUNT(*)"] != 1) {
551 $this->not_found();
552 }
553 }
554
555 public function display() {
556 $this->display_head();
557 switch (true) {
558 case (isset($_GET['year']) && !isset($_GET['month'])
559 && !isset($_GET['day'])):
560 $sql = "SELECT title, url, date_posted, text
561 FROM notes WHERE YEAR(date_posted) = ?
562 ORDER BY date_posted DESC";
563 $result = $this->query($sql, "d",
564 $_GET['year']);
565 break;
566 case (isset($_GET['year']) && isset($_GET['month'])
567 && !isset($_GET['day'])):
568 $sql = "SELECT title, url, date_posted, text
569 FROM notes WHERE YEAR(date_posted) = ?
570 AND MONTH(date_posted) = ?
571 ORDER BY date_posted DESC";
572 $result = $this->query($sql, "dd",
573 $_GET['year'], $_GET['month']);
574 break;
575 case (isset($_GET['year']) && isset($_GET['month'])
576 && isset($_GET['day'])):
577 $sql = "SELECT title, url, date_posted, text
578 FROM notes WHERE YEAR(date_posted) = ?
579 AND MONTH(date_posted) = ?
580 AND DAY(date_posted) = ?
581 ORDER BY date_posted DESC";
582 $result = $this->query($sql, "ddd",
583 $_GET['year'], $_GET['month'],
584 $_GET['day']);
585 break;
586 }
587 if (count($result) >= 1) {
588 echo "<div id='notes'>";
589 foreach ($result as $row => $entry) {
590 $title = $entry['title'];
591 $url = '/note/' . $entry['url'];
592 $date_posted = explode("-", $entry['date_posted']);
593 $year_posted = $date_posted[0];
594 $month_posted = $date_posted[1];
595 $datetime_posted = explode(' ', $date_posted[2]);
596 $day_posted = $datetime_posted[0];
597 echo "<div class='note'>";
598 echo "<h1><span class='date'>";
599 echo "$year_posted/$month_posted/$day_posted/";
600 echo "</span><a href='$url'>$title</a></h1>";
601 echo $entry['text'];
602 echo "</div>";
603 }
604 echo "</div>";
605 $this->write_navigation();
606 } else {
607 echo "<br>";
608 echo "<h1>sorry, nothing here</h2>";
609 echo "<pre>Empty set (0.00 sec)</pre>";
610 }
611 $this->display_close();
612 }
613
614 private function write_navigation() {
615 echo "<br>";
616 echo "<div id='navigation'>";
617 // fill me in!
618 echo "</div>";
619 }
620
621 }
622
623
624 class rss extends cms {
625 public function display() {
626 $result = $this->db->query("SELECT date_posted, title, text, url
627 FROM notes ORDER BY date_posted DESC
628 LIMIT 5");
629 echo <<<END_OF_ENTRY
630 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
631 <channel>
632 <title>dylanstestserver.com/notes/rss</title>
633 <link>http://dylanstestserver.com/notes</link>
634 <description>dylanstestserver.com/notes/rss</description>
635 <atom:link href="http://dylanstestserver.com/notes/rss" rel="self" type="application/rss+xml" />
636 END_OF_ENTRY;
637 while ($entry = $result->fetch_object()) {
638 $title = $entry->title;
639 $date_posted = $entry->date_posted;
640 $url = "http://dylanstestserver.com/note/" . $entry->url;
641 $text = $entry->text;
642 $text = strip_tags($text);
643 $end_of_first_sentence = strpos($text, '.');
644 if ($end_of_first_sentence) {
645 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
646 if ($end_of_second_sentence) {
647 $description = substr($text, '0', ($end_of_second_sentence + 1));
648 } else {
649 $description = substr($text, '0', ($end_of_first_sentence + 1));
650 }
651 }
652 echo <<<END_OF_ENTRY
653 <item>
654 <title>$title</title>
655 <link>$url</link>
656 <guid>$url</guid>
657 <description>$description</description>
658 </item>
659 END_OF_ENTRY;
660 }
661 echo "</channel>";
662 echo "</rss>";
663
664 }
665 }
666
667
668 class notFound extends Exception {
669
670 public function __construct() {
671 header('HTTP/1.0 404 Not Found');
672 ob_end_clean();
673 include('404.php');
674 exit();
675 }
676
677 }
678
679
680 class captcha extends cms {
681
682 public function display() {
683 $challenge = $_GET['challenge'];
684 $response = $_GET['response'];
685 $remoteip = $_SERVER['REMOTE_ADDR'];
686 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
687 curl_setopt ($curl, CURLOPT_POST, 4);
688 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
689 $result = curl_exec ($curl);
690 curl_close ($curl);
691 }
692
693 }
694
695
696 ## now actually do something:
697 switch (cms::determine_type()) {
698 case 'index':
699 $index = new index();
700 $index->display();
701 break;
702 case 'project':
703 $project = new project();
704 $project->display();
705 break;
706 case 'note':
707 $note = new note;
708 $note->display();
709 break;
710 case 'page':
711 $page = new page;
712 $page->display();
713 break;
714 case "rss":
715 $rss = new rss();
716 $rss->display();
717 case 'archive':
718 $archive = new archive;
719 $archive->display();
720 break;
721 case "captcha":
722 $captcha = new captcha;
723 $captcha->display();
724 break;
725 }
726
727 ?>