5 private $config_file = '/etc/dylansserver.ini';
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
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();
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'];
32 public static function determine_type() {
33 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
35 } else if (isset($_GET['year'])) {
37 } else if (isset($_GET['note'])) {
39 } else if ($_SERVER['REQUEST_URI'] == '/') {
41 } else if (isset($_GET['project'])) {
43 } else if (isset($_GET['rss'])) {
45 } else if (isset($_GET['challenge'])) {
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();
57 $statement->store_result();
59 $data = $statement->result_metadata();
61 $fields[0] = &$statement;
62 while($field = $data->fetch_field()) {
63 $fields[] = &$row[$field->name
];
65 call_user_func_array("mysqli_stmt_bind_result", $fields);
67 while ($statement->fetch()) {
68 foreach ($row as $key=>$value) $return[$i][$key] = $value;
71 $statement->free_result();
75 public function display_head($title = "dylansserver",
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&ucn_task=conformance#";
81 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
82 "http://www.w3.org/TR/html4/loose.dtd">
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">
90 <title>$this->title</title>
91 <link rel="icon" href="/favicon.ico" type="image/png">
96 <body onload="return typeof highlight == 'function' ? highlight() : true;">
99 <a href="$this->home_link">
100 <img src="/images/dylansserver.png" alt="dylansserver"
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>
117 public function display_close($show_contact = true) {
119 $this->display_contact();
132 public function init() {
133 switch (cms::determine_type()) {
135 $index = new index();
139 $project = new project();
155 $archive = new archive;
159 $captcha = new captcha;
168 class index extends cms {
170 public function display() {
171 $this->scripts = "<script type='text/javascript' src='/includes/index.js'></script>";
172 $this->display_head();
173 $this->display_exhibits();
174 echo "<ul id='portfolio'>";
175 $this->list_projects();
176 echo <<<OTHER_PROJECTS
178 <h3>things i've done for others:</h3>
182 "http://activehamptons.com">activehamptons.com</a></li>
185 "http://transfishing.com">transfishing.com</a></li>
188 <h3>something i've worked on:</h3>
192 "http://tempositions.com">tempositions.com</a></li>
195 <h3>my repositories:</h3>
199 "/git/">git://dylansserver.com</a></li>
206 "/notes/">here</a> [<a href="/notes/rss">rss</a>]</li>
213 "/resume">pdf</a>]</li>
218 // Because of the CSS necessary for the animations,
219 // the contact link needs to be in #portfolio to clear
222 $this->display_contact();
225 $this->display_close($show_contact = false);
228 protected function display_exhibits() {
229 echo "<div id='exhibit'>";
230 $sql = "SELECT text FROM projects ORDER BY rank";
231 $result = $this->db->query($sql);
232 while ($entry = $result->fetch_object()) {
238 private function list_projects() {
241 <h3>my projects:</h3>
244 $sql = "SELECT title FROM projects ORDER BY rank";
245 $result = $this->db->query($sql);
246 while ($entry = $result->fetch_object()) {
247 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
254 class project extends index {
256 protected function display_exhibits() {
257 echo "<div id='exhibit'>";
258 $sql = "SELECT text FROM projects
260 $result = $this->query($sql, "s", $_GET['project']);
261 if ($result = $result[0]['text']) {
262 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
266 throw new notFound();
273 class page extends cms {
277 private $notes_per_page = 4;
278 private $number_of_pages = 1;
280 public function __construct() {
281 parent::__construct();
282 $this->page_offset();
284 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
285 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
286 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
287 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
288 <script type='text/javascript'>
289 function highlight() {
290 SyntaxHighlighter.autoloader(
291 'js /includes/syntax/scripts/shBrushJScript.js',
292 'bash /includes/syntax/scripts/shBrushBash.js',
293 'sql /includes/syntax/scripts/shBrushSql.js',
294 'cpp /includes/syntax/scripts/shBrushCpp.js');
295 SyntaxHighlighter.defaults['gutter'] = false;
296 SyntaxHighlighter.defaults['toolbar'] = false;
297 SyntaxHighlighter.all();
303 private function page_offset() {
304 $sql = "SELECT COUNT(*) FROM notes";
305 $result = $this->db->query($sql);
306 $result = $result->fetch_array();
307 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
308 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
309 $this->page = (int) $_GET['page'];
311 throw new notFound();
313 if ($this->page > $this->number_of_pages) {
314 throw new notFound();
316 if ($this->page < 1) {
317 throw new notFound();
319 $this->offset = ($this->page - 1) * $this->notes_per_page;
322 public function display() {
323 $this->display_head();
324 echo "<div id='notes'>";
325 $sql = "SELECT date_posted, title, url, text
326 FROM notes ORDER BY date_posted DESC
328 $result = $this->query($sql, "ii",
330 $this->notes_per_page);
331 foreach ($result as $row => $entry) {
332 $title = $entry['title'];
333 $url = '/note/' . $entry['url'];
334 $date_posted = explode("-", $entry['date_posted']);
335 $year_posted = $date_posted[0];
336 $month_posted = $date_posted[1];
337 $datetime_posted = explode(' ', $date_posted[2]);
338 $day_posted = $datetime_posted[0];
339 $text = $entry['text'];
343 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
350 $this->write_navigation();
351 $this->display_close();
354 private function write_navigation() {
355 echo "<div id='navigation'>";
358 $previous_page = $this->page - 1;
359 echo "<a href='/notes/page/$previous_page'>prev</a>";
361 if($this->page < $this->number_of_pages) {
362 $forward_page = $this->page + 1;
363 echo " <a href='/notes/page/$forward_page'>next</a>";
372 class note extends cms {
375 private $comments_enabled = false;
376 private $failed_captcha;
380 public $month_posted;
383 public $number_of_comments;
385 public function __construct() {
387 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
388 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
389 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
390 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
391 <script type='text/javascript'>
392 function highlight() {
393 SyntaxHighlighter.autoloader(
394 'js /includes/syntax/scripts/shBrushJScript.js',
395 'bash /includes/syntax/scripts/shBrushBash.js',
396 'sql /includes/syntax/scripts/shBrushSql.js',
397 'cpp /includes/syntax/scripts/shBrushCpp.js');
398 SyntaxHighlighter.defaults['gutter'] = false;
399 SyntaxHighlighter.defaults['toolbar'] = false;
400 SyntaxHighlighter.all();
405 if (isset($_GET['comments'])) {
407 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
408 <script type='text/javascript' src='/includes/comment.js'></script>";
410 parent::__construct();
411 if (isset($_GET['comments'])) {
412 $this->comments_enabled = true;
414 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
415 if (isset($_GET['verify'])) {
416 $url = substr($url, 0, (strlen($url)-6));
419 $sql = "SELECT title, date_posted, text, id
420 FROM notes WHERE url = ?";
421 $result = $this->query($sql, "s",
425 $this->id = $entry["id"];
426 $this->title = $entry["title"];
427 $date_posted = explode("-", $entry["date_posted"]);
428 $this->year_posted = $date_posted[0];
429 $this->month_posted = $date_posted[1];
430 $datetime_posted = explode(' ', $date_posted[2]);
431 $this->day_posted = $datetime_posted[0];
432 $this->text = $entry["text"];
434 throw new notFound();
436 $sql = "SELECT COUNT(*) FROM comments
437 WHERE note = $this->id";
438 $result = $this->db->query($sql);
439 $result = $result->fetch_array();
440 $this->number_of_comments = $result[0];
441 if (isset($_GET['verify'])) {
446 public function display() {
447 require_once("view/note.php");
450 private function verify() {
451 if (!isset($_POST['captcha'])) {
452 require_once('includes/recaptchalib.php');
454 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
455 $_SERVER["REMOTE_ADDR"],
456 $_POST["recaptcha_challenge_field"],
457 $_POST["recaptcha_response_field"]);
458 if (!$resp->is_valid) {
459 $this->failed_captcha = true;
462 if (isset($_POST['captcha']) || $resp->is_valid) {
463 $sql = ("INSERT INTO comments (date_posted, author,
465 VALUES(NOW(), ?, ?, ?)");
466 $stmt = $this->db->prepare($sql);
467 // Checks are needed here (no blank text,
468 // and a default author needs to be set
469 // for no-javascript users.
470 $stmt->bind_param('sss',
478 private function display_comment_link() {
479 if ($this->number_of_comments > 0) {
480 $anchor_text = "comments($this->number_of_comments)/";
482 $anchor_text = "comment?";
484 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
485 $url = $this->url . 'comments/';
487 $url = $this->url . '/comments/';
489 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
492 private function display_comments() {
493 echo "<div id='comments'>";
494 $sql= "SELECT date_posted, author, text
495 FROM comments WHERE note = ?
496 ORDER BY date_posted DESC";
497 $result = $this->query($sql, 'd', $this->id);
498 foreach ($result as $row => $entry) {
499 $date_posted = $entry['date_posted'];
500 $author = $entry['author'];
501 $text = htmlspecialchars($entry['text']);
502 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
503 echo <<<END_OF_COMMENT
504 <div class='comment'>
513 private function display_comment_form() {
514 $publickey = $this->recaptcha_publickey;
515 echo <<<END_CAPTCHA_STYLE
516 <script type="text/javascript">
517 Recaptcha.create("$publickey",
521 custom_theme_widget: 'recaptcha_widget',
522 callback: Recaptcha.focus_response_field
526 require_once('includes/recaptchalib.php');
527 $url = $this->url . "verify";
528 echo "<form id='comment_form' method='post' action='$url'>";
532 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
534 <input type=text name="name" id="comment_name">
537 <div id="recaptcha_widget">
539 <h3><b>what's this say</b>?</h3>
541 <div id="recaptcha_image"></div>
543 <span class="recaptcha_only_if_image"><br><br><br></span>
544 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
546 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
547 <span class="recaptcha_help">
548 <a href="javascript:Recaptcha.reload()">another?</a> /
549 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
550 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
554 echo recaptcha_get_html($this->recaptcha_publickey);
555 if ($this->failed_captcha) {
556 echo <<<END_OF_ERRORS
558 reCAPTCHA said you're not human, <br>
561 <input id="submit" class="submit" type="submit" value="post comment">
566 echo <<<END_OF_ERRORS
568 reCAPTCHA said you're not human, <br>
571 <div id="blank_comment">
572 but you didn't write anything! <br>
577 <input id="submit" class="submit" type="submit" value="post comment">
585 class archive extends cms {
587 public function __construct() {
588 parent::__construct();
591 private function check_exists() {
592 $sql = "SELECT COUNT(*) FROM notes
594 $results = $this->query($sql, "s", $_GET['note']);
595 if ($results[0]["COUNT(*)"] != 1) {
600 public function display() {
601 $this->display_head();
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 ORDER BY date_posted DESC";
608 $result = $this->query($sql, "d",
611 case (isset($_GET['year']) && isset($_GET['month'])
612 && !isset($_GET['day'])):
613 $sql = "SELECT title, url, date_posted, text
614 FROM notes WHERE YEAR(date_posted) = ?
615 AND MONTH(date_posted) = ?
616 ORDER BY date_posted DESC";
617 $result = $this->query($sql, "dd",
618 $_GET['year'], $_GET['month']);
620 case (isset($_GET['year']) && isset($_GET['month'])
621 && isset($_GET['day'])):
622 $sql = "SELECT title, url, date_posted, text
623 FROM notes WHERE YEAR(date_posted) = ?
624 AND MONTH(date_posted) = ?
625 AND DAY(date_posted) = ?
626 ORDER BY date_posted DESC";
627 $result = $this->query($sql, "ddd",
628 $_GET['year'], $_GET['month'],
632 if (count($result) >= 1) {
633 echo "<div id='notes'>";
634 foreach ($result as $row => $entry) {
635 $title = $entry['title'];
636 $url = '/note/' . $entry['url'];
637 $date_posted = explode("-", $entry['date_posted']);
638 $year_posted = $date_posted[0];
639 $month_posted = $date_posted[1];
640 $datetime_posted = explode(' ', $date_posted[2]);
641 $day_posted = $datetime_posted[0];
642 echo "<div class='note'>";
643 echo "<h1><span class='date'>";
644 echo "$year_posted/$month_posted/$day_posted/";
645 echo "</span><a href='$url'>$title</a></h1>";
650 $this->write_navigation();
653 echo "<h1>sorry, nothing here</h2>";
654 echo "<pre>Empty set (0.00 sec)</pre>";
656 $this->display_close();
659 private function write_navigation() {
661 echo "<div id='navigation'>";
669 class rss extends cms {
670 public function display() {
671 $result = $this->db->query("SELECT date_posted, title, text, url
672 FROM notes ORDER BY date_posted DESC
675 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
677 <title>dylansserver.com/notes/rss</title>
678 <link>http://dylansserver.com/notes</link>
679 <description>dylansserver.com/notes/rss</description>
680 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
682 while ($entry = $result->fetch_object()) {
683 $title = $entry->title;
684 $date_posted = $entry->date_posted;
685 $url = "http://dylansserver.com/note/" . $entry->url;
686 $text = $entry->text;
687 $text = strip_tags($text);
688 $end_of_first_sentence = strpos($text, '.');
689 if ($end_of_first_sentence) {
690 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
691 if ($end_of_second_sentence) {
692 $description = substr($text, '0', ($end_of_second_sentence + 1));
694 $description = substr($text, '0', ($end_of_first_sentence + 1));
699 <title>$title</title>
702 <description>$description</description>
713 class notFound extends Exception {
715 public function __construct() {
716 header('HTTP/1.0 404 Not Found');
725 class captcha extends cms {
727 public function display() {
728 $challenge = $_GET['challenge'];
729 $response = $_GET['response'];
730 $remoteip = $_SERVER['REMOTE_ADDR'];
731 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
732 curl_setopt ($curl, CURLOPT_POST, 4);
733 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
734 $result = curl_exec ($curl);