Merge branch 'dev' into live
[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 onload="return typeof highlight == 'function' ? highlight() : true;">
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'>";
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> [<a href="/notes/rss">rss</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 $this->scripts = "
244 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
245 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
246 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
247 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
248 <script type='text/javascript'>
249 function highlight() {
250 SyntaxHighlighter.autoloader(
251 'js /includes/syntax/scripts/shBrushJScript.js',
252 'bash /includes/syntax/scripts/shBrushBash.js',
253 'sql /includes/syntax/scripts/shBrushSql.js',
254 'cpp /includes/syntax/scripts/shBrushCpp.js');
255 SyntaxHighlighter.defaults['gutter'] = false;
256 SyntaxHighlighter.defaults['toolbar'] = false;
257 SyntaxHighlighter.all();
258 }
259 </script>
260 ";
261 }
262
263 private function page_offset() {
264 $sql = "SELECT COUNT(*) FROM notes";
265 $result = $this->db->query($sql);
266 $result = $result->fetch_array();
267 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
268 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
269 $this->page = (int) $_GET['page'];
270 } else {
271 throw new notFound();
272 }
273 if ($this->page > $this->number_of_pages) {
274 throw new notFound();
275 }
276 if ($this->page < 1) {
277 throw new notFound();
278 }
279 $this->offset = ($this->page - 1) * $this->notes_per_page;
280 }
281
282 public function display() {
283 $this->display_head();
284 echo "<div id='notes'>";
285 $sql = "SELECT date_posted, title, url, text
286 FROM notes ORDER BY date_posted DESC
287 LIMIT ?, ?";
288 $result = $this->query($sql, "ii",
289 $this->offset,
290 $this->notes_per_page);
291 foreach ($result as $row => $entry) {
292 $title = $entry['title'];
293 $url = '/note/' . $entry['url'];
294 $date_posted = explode("-", $entry['date_posted']);
295 $year_posted = $date_posted[0];
296 $month_posted = $date_posted[1];
297 $datetime_posted = explode(' ', $date_posted[2]);
298 $day_posted = $datetime_posted[0];
299 $text = $entry['text'];
300 echo <<<END_NOTE
301 <div class='note'>
302 <h1>
303 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
304 </h1>
305 $text
306 </div>
307 END_NOTE;
308 }
309 echo "</div>";
310 $this->write_navigation();
311 $this->display_close();
312 }
313
314 private function write_navigation() {
315 echo "<div id='navigation'>";
316 echo "<h1>";
317 if($this->page > 1){
318 $previous_page = $this->page - 1;
319 echo "<a href='/notes/page/$previous_page'>prev</a>";
320 }
321 if($this->page < $this->number_of_pages) {
322 $forward_page = $this->page + 1;
323 echo " <a href='/notes/page/$forward_page'>next</a>";
324 }
325 echo "</h1>";
326 echo "</div>";
327 }
328
329 }
330
331
332 class note extends cms {
333
334 private $id;
335 private $comments_enabled = false;
336 private $failed_captcha;
337 public $url;
338 public $title;
339 public $year_posted;
340 public $month_posted;
341 public $day_posted;
342 public $text;
343 public $number_of_comments;
344
345 public function __construct() {
346 $this->scripts = "
347 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
348 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
349 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
350 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
351 <script type='text/javascript'>
352 function highlight() {
353 SyntaxHighlighter.autoloader(
354 'js /includes/syntax/scripts/shBrushJScript.js',
355 'bash /includes/syntax/scripts/shBrushBash.js',
356 'sql /includes/syntax/scripts/shBrushSql.js',
357 'cpp /includes/syntax/scripts/shBrushCpp.js');
358 SyntaxHighlighter.defaults['gutter'] = false;
359 SyntaxHighlighter.defaults['toolbar'] = false;
360 SyntaxHighlighter.all();
361 }
362 </script>
363 ";
364
365 if (isset($_GET['comments'])) {
366 $this->scripts .= "
367 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
368 <script type='text/javascript' src='/includes/comment.js'></script>";
369 }
370 parent::__construct();
371 if (isset($_GET['comments'])) {
372 $this->comments_enabled = true;
373 }
374 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
375 if (isset($_GET['verify'])) {
376 $url = substr($url, 0, (strlen($url)-6));
377 }
378 $this->url = $url;
379 $sql = "SELECT title, date_posted, text, id
380 FROM notes WHERE url = ?";
381 $result = $this->query($sql, "s",
382 $_GET['note']);
383 if ($result) {
384 $entry = $result[0];
385 $this->id = $entry["id"];
386 $this->title = $entry["title"];
387 $date_posted = explode("-", $entry["date_posted"]);
388 $this->year_posted = $date_posted[0];
389 $this->month_posted = $date_posted[1];
390 $datetime_posted = explode(' ', $date_posted[2]);
391 $this->day_posted = $datetime_posted[0];
392 $this->text = $entry["text"];
393 } else {
394 throw new notFound();
395 }
396 $sql = "SELECT COUNT(*) FROM comments
397 WHERE note = $this->id";
398 $result = $this->db->query($sql);
399 $result = $result->fetch_array();
400 $this->number_of_comments = $result[0];
401 if (isset($_GET['verify'])) {
402 $this->verify();
403 }
404 }
405
406 public function display() {
407 $this->display_head();
408 $this->display_note();
409 if ($this->comments_enabled) {
410 $this->display_comments();
411 $this->display_comment_form();
412 }
413 $this->write_navigation();
414 $this->display_close();
415 }
416
417 private function verify() {
418 if (!isset($_POST['captcha'])) {
419 require_once('includes/recaptchalib.php');
420 echo "<br>";
421 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
422 $_SERVER["REMOTE_ADDR"],
423 $_POST["recaptcha_challenge_field"],
424 $_POST["recaptcha_response_field"]);
425 if (!$resp->is_valid) {
426 $this->failed_captcha = true;
427 }
428 }
429 if (isset($_POST['captcha']) || $resp->is_valid) {
430 $sql = ("INSERT INTO comments (date_posted, author,
431 text, note)
432 VALUES(NOW(), ?, ?, ?)");
433 $stmt = $this->db->prepare($sql);
434 // Checks are needed here (no blank text,
435 // and a default author needs to be set
436 // for no-javascript users.
437 $stmt->bind_param('sss',
438 htmlspecialchars($_POST['name']),
439 htmlspecialchars($_POST['text']),
440 $this->id);
441 $stmt->execute();
442 }
443 }
444
445 private function display_note() {
446 echo <<<END_OF_NOTE
447 <div id='note'>
448 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
449 $this->text
450 </div>
451 END_OF_NOTE;
452 }
453
454 private function write_navigation() {
455 echo <<<END_OF_NAVIGATION
456 <br><br><br><br>
457 <div id='navigation'>
458 <h1>
459 END_OF_NAVIGATION;
460 if (!$this->comments_enabled) {
461 $this->display_comment_link();
462 }
463 echo <<<END_OF_NAVIGATION
464 <a href="/notes/">back to notes/</a>
465 </h1>
466 </div>
467 END_OF_NAVIGATION;
468 }
469
470 private function display_comment_link() {
471 if ($this->number_of_comments > 0) {
472 $anchor_text = "comments($this->number_of_comments)/";
473 } else {
474 $anchor_text = "comment?";
475 }
476 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
477 $url = $this->url . 'comments/';
478 } else {
479 $url = $this->url . '/comments/';
480 }
481 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
482 }
483
484 private function display_comments() {
485 echo "<div id='comments'>";
486 $sql= "SELECT date_posted, author, text
487 FROM comments WHERE note = ?
488 ORDER BY date_posted DESC";
489 $result = $this->query($sql, 'd', $this->id);
490 foreach ($result as $row => $entry) {
491 $date_posted = $entry['date_posted'];
492 $author = $entry['author'];
493 $text = htmlspecialchars($entry['text']);
494 $head = "<h3>$author</h3>";
495 echo <<<END_OF_COMMENT
496 <div class='comment'>
497 $head
498 $text
499 </div>
500 END_OF_COMMENT;
501 }
502 echo "</div>";
503 }
504
505 private function display_comment_form() {
506 $publickey = $this->recaptcha_publickey;
507 echo <<<END_CAPTCHA_STYLE
508 <script type="text/javascript">
509 Recaptcha.create("$publickey",
510 "recaptcha_div",
511 {
512 theme : 'custom',
513 custom_theme_widget: 'recaptcha_widget',
514 callback: Recaptcha.focus_response_field
515 });
516 </script>
517 END_CAPTCHA_STYLE;
518 require_once('includes/recaptchalib.php');
519 $url = $this->url . "verify";
520 echo "<form id='comment_form' method='post' action='$url'>";
521 echo <<<END_OF_FORM
522 <div id="comment">
523 <h3>comment:</h3>
524 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
525 <h3>name:</h3>
526 <input type=text name="name" id="comment_name">
527
528 <nowiki>
529 <div id="recaptcha_widget">
530 <br>
531 <h3><b>what's this say</b>?</h3>
532 <br>
533 <div id="recaptcha_image"></div>
534 <br><br><br>
535 <span class="recaptcha_only_if_image"><br><br><br></span>
536 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
537 <br><br>
538 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
539 <span class="recaptcha_help">
540 <a href="javascript:Recaptcha.reload()">another?</a> /
541 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
542 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
543 </span>
544 </div>
545 END_OF_FORM;
546 echo recaptcha_get_html($this->recaptcha_publickey);
547 if ($this->failed_captcha) {
548 echo <<<END_OF_ERRORS
549 <div id="not_human">
550 reCAPTCHA said you're not human, <br>
551 try again?
552 </div>
553 <input id="submit" class="submit" type="submit" value="post comment">
554 </form>
555 </div>
556 END_OF_ERRORS;
557 } else {
558 echo <<<END_OF_ERRORS
559 <div id="not_human">
560 reCAPTCHA said you're not human, <br>
561 try again?
562 </div>
563 <div id="blank_comment">
564 but you didn't write anything! <br>
565 </div>
566 END_OF_ERRORS;
567 }
568 echo <<<END_OF_FORM
569 <input id="submit" class="submit" type="submit" value="post comment">
570 </form>
571 </div>
572 END_OF_FORM;
573 }
574 }
575
576
577 class archive extends cms {
578
579 public function __construct() {
580 parent::__construct();
581 }
582
583 private function check_exists() {
584 $sql = "SELECT COUNT(*) FROM notes
585 WHERE url = ?";
586 $results = $this->query($sql, "s", $_GET['note']);
587 if ($results[0]["COUNT(*)"] != 1) {
588 $this->not_found();
589 }
590 }
591
592 public function display() {
593 $this->display_head();
594 switch (true) {
595 case (isset($_GET['year']) && !isset($_GET['month'])
596 && !isset($_GET['day'])):
597 $sql = "SELECT title, url, date_posted, text
598 FROM notes WHERE YEAR(date_posted) = ?
599 ORDER BY date_posted DESC";
600 $result = $this->query($sql, "d",
601 $_GET['year']);
602 break;
603 case (isset($_GET['year']) && isset($_GET['month'])
604 && !isset($_GET['day'])):
605 $sql = "SELECT title, url, date_posted, text
606 FROM notes WHERE YEAR(date_posted) = ?
607 AND MONTH(date_posted) = ?
608 ORDER BY date_posted DESC";
609 $result = $this->query($sql, "dd",
610 $_GET['year'], $_GET['month']);
611 break;
612 case (isset($_GET['year']) && isset($_GET['month'])
613 && isset($_GET['day'])):
614 $sql = "SELECT title, url, date_posted, text
615 FROM notes WHERE YEAR(date_posted) = ?
616 AND MONTH(date_posted) = ?
617 AND DAY(date_posted) = ?
618 ORDER BY date_posted DESC";
619 $result = $this->query($sql, "ddd",
620 $_GET['year'], $_GET['month'],
621 $_GET['day']);
622 break;
623 }
624 if (count($result) >= 1) {
625 echo "<div id='notes'>";
626 foreach ($result as $row => $entry) {
627 $title = $entry['title'];
628 $url = '/note/' . $entry['url'];
629 $date_posted = explode("-", $entry['date_posted']);
630 $year_posted = $date_posted[0];
631 $month_posted = $date_posted[1];
632 $datetime_posted = explode(' ', $date_posted[2]);
633 $day_posted = $datetime_posted[0];
634 echo "<div class='note'>";
635 echo "<h1><span class='date'>";
636 echo "$year_posted/$month_posted/$day_posted/";
637 echo "</span><a href='$url'>$title</a></h1>";
638 echo $entry['text'];
639 echo "</div>";
640 }
641 echo "</div>";
642 $this->write_navigation();
643 } else {
644 echo "<br>";
645 echo "<h1>sorry, nothing here</h2>";
646 echo "<pre>Empty set (0.00 sec)</pre>";
647 }
648 $this->display_close();
649 }
650
651 private function write_navigation() {
652 echo "<br>";
653 echo "<div id='navigation'>";
654 // fill me in!
655 echo "</div>";
656 }
657
658 }
659
660
661 class rss extends cms {
662 public function display() {
663 $result = $this->db->query("SELECT date_posted, title, text, url
664 FROM notes ORDER BY date_posted DESC
665 LIMIT 5");
666 echo <<<END_OF_ENTRY
667 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
668 <channel>
669 <title>dylanstestserver.com/notes/rss</title>
670 <link>http://dylanstestserver.com/notes</link>
671 <description>dylanstestserver.com/notes/rss</description>
672 <atom:link href="http://dylanstestserver.com/notes/rss" rel="self" type="application/rss+xml" />
673 END_OF_ENTRY;
674 while ($entry = $result->fetch_object()) {
675 $title = $entry->title;
676 $date_posted = $entry->date_posted;
677 $url = "http://dylanstestserver.com/note/" . $entry->url;
678 $text = $entry->text;
679 $text = strip_tags($text);
680 $end_of_first_sentence = strpos($text, '.');
681 if ($end_of_first_sentence) {
682 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
683 if ($end_of_second_sentence) {
684 $description = substr($text, '0', ($end_of_second_sentence + 1));
685 } else {
686 $description = substr($text, '0', ($end_of_first_sentence + 1));
687 }
688 }
689 echo <<<END_OF_ENTRY
690 <item>
691 <title>$title</title>
692 <link>$url</link>
693 <guid>$url</guid>
694 <description>$description</description>
695 </item>
696 END_OF_ENTRY;
697 }
698 echo "</channel>";
699 echo "</rss>";
700
701 }
702 }
703
704
705 class notFound extends Exception {
706
707 public function __construct() {
708 header('HTTP/1.0 404 Not Found');
709 ob_end_clean();
710 include('404.php');
711 exit();
712 }
713
714 }
715
716
717 class captcha extends cms {
718
719 public function display() {
720 $challenge = $_GET['challenge'];
721 $response = $_GET['response'];
722 $remoteip = $_SERVER['REMOTE_ADDR'];
723 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
724 curl_setopt ($curl, CURLOPT_POST, 4);
725 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
726 $result = curl_exec ($curl);
727 curl_close ($curl);
728 }
729
730 }
731
732
733 ## now actually do something:
734 switch (cms::determine_type()) {
735 case 'index':
736 $index = new index();
737 $index->display();
738 break;
739 case 'project':
740 $project = new project();
741 $project->display();
742 break;
743 case 'note':
744 $note = new note;
745 $note->display();
746 break;
747 case 'page':
748 $page = new page;
749 $page->display();
750 break;
751 case "rss":
752 $rss = new rss();
753 $rss->display();
754 case 'archive':
755 $archive = new archive;
756 $archive->display();
757 break;
758 case "captcha":
759 $captcha = new captcha;
760 $captcha->display();
761 break;
762 }
763
764 ?>