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 require_once("view/page.php");
326 public function display_notes() {
327 echo "<div id='notes'>";
328 $sql = "SELECT date_posted, title, url, text
329 FROM notes ORDER BY date_posted DESC
331 $result = $this->query($sql, "ii",
333 $this->notes_per_page);
334 foreach ($result as $row => $entry) {
335 $title = $entry['title'];
336 $url = '/note/' . $entry['url'];
337 $date_posted = explode("-", $entry['date_posted']);
338 $year_posted = $date_posted[0];
339 $month_posted = $date_posted[1];
340 $datetime_posted = explode(' ', $date_posted[2]);
341 $day_posted = $datetime_posted[0];
342 $text = $entry['text'];
346 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
357 class note extends cms {
360 private $comments_enabled = false;
361 private $failed_captcha;
365 public $month_posted;
368 public $number_of_comments;
370 public function __construct() {
372 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
373 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
374 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
375 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
376 <script type='text/javascript'>
377 function highlight() {
378 SyntaxHighlighter.autoloader(
379 'js /includes/syntax/scripts/shBrushJScript.js',
380 'bash /includes/syntax/scripts/shBrushBash.js',
381 'sql /includes/syntax/scripts/shBrushSql.js',
382 'cpp /includes/syntax/scripts/shBrushCpp.js');
383 SyntaxHighlighter.defaults['gutter'] = false;
384 SyntaxHighlighter.defaults['toolbar'] = false;
385 SyntaxHighlighter.all();
390 if (isset($_GET['comments'])) {
392 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
393 <script type='text/javascript' src='/includes/comment.js'></script>";
395 parent::__construct();
396 if (isset($_GET['comments'])) {
397 $this->comments_enabled = true;
399 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
400 if (isset($_GET['verify'])) {
401 $url = substr($url, 0, (strlen($url)-6));
404 $sql = "SELECT title, date_posted, text, id
405 FROM notes WHERE url = ?";
406 $result = $this->query($sql, "s",
410 $this->id = $entry["id"];
411 $this->title = $entry["title"];
412 $date_posted = explode("-", $entry["date_posted"]);
413 $this->year_posted = $date_posted[0];
414 $this->month_posted = $date_posted[1];
415 $datetime_posted = explode(' ', $date_posted[2]);
416 $this->day_posted = $datetime_posted[0];
417 $this->text = $entry["text"];
419 throw new notFound();
421 $sql = "SELECT COUNT(*) FROM comments
422 WHERE note = $this->id";
423 $result = $this->db->query($sql);
424 $result = $result->fetch_array();
425 $this->number_of_comments = $result[0];
426 if (isset($_GET['verify'])) {
431 public function display() {
432 require_once("view/note.php");
435 private function verify() {
436 if (!isset($_POST['captcha'])) {
437 require_once('includes/recaptchalib.php');
439 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
440 $_SERVER["REMOTE_ADDR"],
441 $_POST["recaptcha_challenge_field"],
442 $_POST["recaptcha_response_field"]);
443 if (!$resp->is_valid) {
444 $this->failed_captcha = true;
447 if (isset($_POST['captcha']) || $resp->is_valid) {
448 $sql = ("INSERT INTO comments (date_posted, author,
450 VALUES(NOW(), ?, ?, ?)");
451 $stmt = $this->db->prepare($sql);
452 // Checks are needed here (no blank text,
453 // and a default author needs to be set
454 // for no-javascript users.
455 $stmt->bind_param('sss',
463 private function display_comment_link() {
464 if ($this->number_of_comments > 0) {
465 $anchor_text = "comments($this->number_of_comments)/";
467 $anchor_text = "comment?";
469 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
470 $url = $this->url . 'comments/';
472 $url = $this->url . '/comments/';
474 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
477 private function display_comments() {
478 echo "<div id='comments'>";
479 $sql= "SELECT date_posted, author, text
480 FROM comments WHERE note = ?
481 ORDER BY date_posted DESC";
482 $result = $this->query($sql, 'd', $this->id);
483 foreach ($result as $row => $entry) {
484 $date_posted = $entry['date_posted'];
485 $author = $entry['author'];
486 $text = htmlspecialchars($entry['text']);
487 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
488 echo <<<END_OF_COMMENT
489 <div class='comment'>
498 private function display_comment_form() {
499 $publickey = $this->recaptcha_publickey;
500 echo <<<END_CAPTCHA_STYLE
501 <script type="text/javascript">
502 Recaptcha.create("$publickey",
506 custom_theme_widget: 'recaptcha_widget',
507 callback: Recaptcha.focus_response_field
511 require_once('includes/recaptchalib.php');
512 $url = $this->url . "verify";
513 echo "<form id='comment_form' method='post' action='$url'>";
517 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
519 <input type=text name="name" id="comment_name">
522 <div id="recaptcha_widget">
524 <h3><b>what's this say</b>?</h3>
526 <div id="recaptcha_image"></div>
528 <span class="recaptcha_only_if_image"><br><br><br></span>
529 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
531 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
532 <span class="recaptcha_help">
533 <a href="javascript:Recaptcha.reload()">another?</a> /
534 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
535 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
539 echo recaptcha_get_html($this->recaptcha_publickey);
540 if ($this->failed_captcha) {
541 echo <<<END_OF_ERRORS
543 reCAPTCHA said you're not human, <br>
546 <input id="submit" class="submit" type="submit" value="post comment">
551 echo <<<END_OF_ERRORS
553 reCAPTCHA said you're not human, <br>
556 <div id="blank_comment">
557 but you didn't write anything! <br>
562 <input id="submit" class="submit" type="submit" value="post comment">
570 class archive extends cms {
572 public function __construct() {
573 parent::__construct();
576 private function check_exists() {
577 $sql = "SELECT COUNT(*) FROM notes
579 $results = $this->query($sql, "s", $_GET['note']);
580 if ($results[0]["COUNT(*)"] != 1) {
585 public function display() {
586 require_once("view/archive.php");
589 public function display_notes() {
591 case (isset($_GET['year']) && !isset($_GET['month'])
592 && !isset($_GET['day'])):
593 $sql = "SELECT title, url, date_posted, text
594 FROM notes WHERE YEAR(date_posted) = ?
595 ORDER BY date_posted DESC";
596 $result = $this->query($sql, "d",
599 case (isset($_GET['year']) && isset($_GET['month'])
600 && !isset($_GET['day'])):
601 $sql = "SELECT title, url, date_posted, text
602 FROM notes WHERE YEAR(date_posted) = ?
603 AND MONTH(date_posted) = ?
604 ORDER BY date_posted DESC";
605 $result = $this->query($sql, "dd",
606 $_GET['year'], $_GET['month']);
608 case (isset($_GET['year']) && isset($_GET['month'])
609 && isset($_GET['day'])):
610 $sql = "SELECT title, url, date_posted, text
611 FROM notes WHERE YEAR(date_posted) = ?
612 AND MONTH(date_posted) = ?
613 AND DAY(date_posted) = ?
614 ORDER BY date_posted DESC";
615 $result = $this->query($sql, "ddd",
616 $_GET['year'], $_GET['month'],
620 if (count($result) >= 1) {
621 echo "<div id='notes'>";
622 foreach ($result as $row => $entry) {
623 $title = $entry['title'];
624 $url = '/note/' . $entry['url'];
625 $date_posted = explode("-", $entry['date_posted']);
626 $year_posted = $date_posted[0];
627 $month_posted = $date_posted[1];
628 $datetime_posted = explode(' ', $date_posted[2]);
629 $day_posted = $datetime_posted[0];
630 echo "<div class='note'>";
631 echo "<h1><span class='date'>";
632 echo "$year_posted/$month_posted/$day_posted/";
633 echo "</span><a href='$url'>$title</a></h1>";
640 echo "<h1>sorry, nothing here</h2>";
641 echo "<pre>Empty set (0.00 sec)</pre>";
648 class rss extends cms {
649 public function display() {
650 $result = $this->db->query("SELECT date_posted, title, text, url
651 FROM notes ORDER BY date_posted DESC
654 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
656 <title>dylansserver.com/notes/rss</title>
657 <link>http://dylansserver.com/notes</link>
658 <description>dylansserver.com/notes/rss</description>
659 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
661 while ($entry = $result->fetch_object()) {
662 $title = $entry->title;
663 $date_posted = $entry->date_posted;
664 $url = "http://dylansserver.com/note/" . $entry->url;
665 $text = $entry->text;
666 $text = strip_tags($text);
667 $end_of_first_sentence = strpos($text, '.');
668 if ($end_of_first_sentence) {
669 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
670 if ($end_of_second_sentence) {
671 $description = substr($text, '0', ($end_of_second_sentence + 1));
673 $description = substr($text, '0', ($end_of_first_sentence + 1));
678 <title>$title</title>
681 <description>$description</description>
692 class notFound extends Exception {
694 public function __construct() {
695 header('HTTP/1.0 404 Not Found');
704 class captcha extends cms {
706 public function display() {
707 $challenge = $_GET['challenge'];
708 $response = $_GET['response'];
709 $remoteip = $_SERVER['REMOTE_ADDR'];
710 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
711 curl_setopt ($curl, CURLOPT_POST, 4);
712 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
713 $result = curl_exec ($curl);