8cb1d62e4db4d149033f1e520d63d5f8ffbf8cfa
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();
135 class index extends cms {
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
145 <h3>things i've done for others:</h3>
149 "http://activehamptons.com">activehamptons.com</a></li>
152 "http://transfishing.com">transfishing.com</a></li>
155 <h3>something i've worked on:</h3>
159 "http://tempositions.com">tempositions.com</a></li>
162 <h3>my repositories:</h3>
166 "/git/">git://dylansserver.com</a></li>
173 "/notes/">here</a> [<a href="/notes/rss">rss</a>]</li>
180 "/resume">pdf</a>]</li>
185 // Because of the CSS necessary for the animations,
186 // the contact link needs to be in #portfolio to clear
189 $this->display_contact();
192 $this->display_close($show_contact = false);
195 protected function display_exhibits() {
196 echo "<div id='exhibit'>";
197 $sql = "SELECT text FROM projects ORDER BY rank";
198 $result = $this->db->query($sql);
199 while ($entry = $result->fetch_object()) {
205 private function list_projects() {
208 <h3>my projects:</h3>
211 $sql = "SELECT title FROM projects ORDER BY rank";
212 $result = $this->db->query($sql);
213 while ($entry = $result->fetch_object()) {
214 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
221 class project extends index {
223 protected function display_exhibits() {
224 echo "<div id='exhibit'>";
225 $sql = "SELECT text FROM projects
227 $result = $this->query($sql, "s", $_GET['project']);
228 if ($result = $result[0]['text']) {
229 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
233 throw new notFound();
240 class page extends cms {
244 private $notes_per_page = 4;
245 private $number_of_pages = 1;
247 public function __construct() {
248 parent::__construct();
249 $this->page_offset();
251 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
252 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
253 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
254 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
255 <script type='text/javascript'>
256 function highlight() {
257 SyntaxHighlighter.autoloader(
258 'js /includes/syntax/scripts/shBrushJScript.js',
259 'bash /includes/syntax/scripts/shBrushBash.js',
260 'sql /includes/syntax/scripts/shBrushSql.js',
261 'cpp /includes/syntax/scripts/shBrushCpp.js');
262 SyntaxHighlighter.defaults['gutter'] = false;
263 SyntaxHighlighter.defaults['toolbar'] = false;
264 SyntaxHighlighter.all();
270 private function page_offset() {
271 $sql = "SELECT COUNT(*) FROM notes";
272 $result = $this->db->query($sql);
273 $result = $result->fetch_array();
274 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
275 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
276 $this->page = (int) $_GET['page'];
278 throw new notFound();
280 if ($this->page > $this->number_of_pages) {
281 throw new notFound();
283 if ($this->page < 1) {
284 throw new notFound();
286 $this->offset = ($this->page - 1) * $this->notes_per_page;
289 public function display() {
290 $this->display_head();
291 echo "<div id='notes'>";
292 $sql = "SELECT date_posted, title, url, text
293 FROM notes ORDER BY date_posted DESC
295 $result = $this->query($sql, "ii",
297 $this->notes_per_page);
298 foreach ($result as $row => $entry) {
299 $title = $entry['title'];
300 $url = '/note/' . $entry['url'];
301 $date_posted = explode("-", $entry['date_posted']);
302 $year_posted = $date_posted[0];
303 $month_posted = $date_posted[1];
304 $datetime_posted = explode(' ', $date_posted[2]);
305 $day_posted = $datetime_posted[0];
306 $text = $entry['text'];
310 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
317 $this->write_navigation();
318 $this->display_close();
321 private function write_navigation() {
322 echo "<div id='navigation'>";
325 $previous_page = $this->page - 1;
326 echo "<a href='/notes/page/$previous_page'>prev</a>";
328 if($this->page < $this->number_of_pages) {
329 $forward_page = $this->page + 1;
330 echo " <a href='/notes/page/$forward_page'>next</a>";
339 class note extends cms {
342 private $comments_enabled = false;
343 private $failed_captcha;
347 public $month_posted;
350 public $number_of_comments;
352 public function __construct() {
354 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
355 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
356 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
357 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
358 <script type='text/javascript'>
359 function highlight() {
360 SyntaxHighlighter.autoloader(
361 'js /includes/syntax/scripts/shBrushJScript.js',
362 'bash /includes/syntax/scripts/shBrushBash.js',
363 'sql /includes/syntax/scripts/shBrushSql.js',
364 'cpp /includes/syntax/scripts/shBrushCpp.js');
365 SyntaxHighlighter.defaults['gutter'] = false;
366 SyntaxHighlighter.defaults['toolbar'] = false;
367 SyntaxHighlighter.all();
372 if (isset($_GET['comments'])) {
374 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
375 <script type='text/javascript' src='/includes/comment.js'></script>";
377 parent::__construct();
378 if (isset($_GET['comments'])) {
379 $this->comments_enabled = true;
381 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
382 if (isset($_GET['verify'])) {
383 $url = substr($url, 0, (strlen($url)-6));
386 $sql = "SELECT title, date_posted, text, id
387 FROM notes WHERE url = ?";
388 $result = $this->query($sql, "s",
392 $this->id = $entry["id"];
393 $this->title = $entry["title"];
394 $date_posted = explode("-", $entry["date_posted"]);
395 $this->year_posted = $date_posted[0];
396 $this->month_posted = $date_posted[1];
397 $datetime_posted = explode(' ', $date_posted[2]);
398 $this->day_posted = $datetime_posted[0];
399 $this->text = $entry["text"];
401 throw new notFound();
403 $sql = "SELECT COUNT(*) FROM comments
404 WHERE note = $this->id";
405 $result = $this->db->query($sql);
406 $result = $result->fetch_array();
407 $this->number_of_comments = $result[0];
408 if (isset($_GET['verify'])) {
413 public function display() {
414 $this->display_head();
415 $this->display_note();
416 if ($this->comments_enabled) {
417 $this->display_comments();
418 $this->display_comment_form();
420 $this->write_navigation();
421 $this->display_close();
424 private function verify() {
425 if (!isset($_POST['captcha'])) {
426 require_once('includes/recaptchalib.php');
428 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
429 $_SERVER["REMOTE_ADDR"],
430 $_POST["recaptcha_challenge_field"],
431 $_POST["recaptcha_response_field"]);
432 if (!$resp->is_valid) {
433 $this->failed_captcha = true;
436 if (isset($_POST['captcha']) || $resp->is_valid) {
437 $sql = ("INSERT INTO comments (date_posted, author,
439 VALUES(NOW(), ?, ?, ?)");
440 $stmt = $this->db->prepare($sql);
441 // Checks are needed here (no blank text,
442 // and a default author needs to be set
443 // for no-javascript users.
444 $stmt->bind_param('sss',
452 private function display_note() {
455 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
461 private function write_navigation() {
462 echo <<<END_OF_NAVIGATION
464 <div id='navigation'>
467 if (!$this->comments_enabled) {
468 $this->display_comment_link();
470 echo <<<END_OF_NAVIGATION
471 <a href="/notes/">back to notes/</a>
477 private function display_comment_link() {
478 if ($this->number_of_comments > 0) {
479 $anchor_text = "comments($this->number_of_comments)/";
481 $anchor_text = "comment?";
483 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
484 $url = $this->url . 'comments/';
486 $url = $this->url . '/comments/';
488 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
491 private function display_comments() {
492 echo "<div id='comments'>";
493 $sql= "SELECT date_posted, author, text
494 FROM comments WHERE note = ?
495 ORDER BY date_posted DESC";
496 $result = $this->query($sql, 'd', $this->id);
497 foreach ($result as $row => $entry) {
498 $date_posted = $entry['date_posted'];
499 $author = $entry['author'];
500 $text = htmlspecialchars($entry['text']);
501 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
502 echo <<<END_OF_COMMENT
503 <div class='comment'>
512 private function display_comment_form() {
513 $publickey = $this->recaptcha_publickey;
514 echo <<<END_CAPTCHA_STYLE
515 <script type="text/javascript">
516 Recaptcha.create("$publickey",
520 custom_theme_widget: 'recaptcha_widget',
521 callback: Recaptcha.focus_response_field
525 require_once('includes/recaptchalib.php');
526 $url = $this->url . "verify";
527 echo "<form id='comment_form' method='post' action='$url'>";
531 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
533 <input type=text name="name" id="comment_name">
536 <div id="recaptcha_widget">
538 <h3><b>what's this say</b>?</h3>
540 <div id="recaptcha_image"></div>
542 <span class="recaptcha_only_if_image"><br><br><br></span>
543 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
545 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
546 <span class="recaptcha_help">
547 <a href="javascript:Recaptcha.reload()">another?</a> /
548 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
549 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
553 echo recaptcha_get_html($this->recaptcha_publickey);
554 if ($this->failed_captcha) {
555 echo <<<END_OF_ERRORS
557 reCAPTCHA said you're not human, <br>
560 <input id="submit" class="submit" type="submit" value="post comment">
565 echo <<<END_OF_ERRORS
567 reCAPTCHA said you're not human, <br>
570 <div id="blank_comment">
571 but you didn't write anything! <br>
576 <input id="submit" class="submit" type="submit" value="post comment">
584 class archive extends cms {
586 public function __construct() {
587 parent::__construct();
590 private function check_exists() {
591 $sql = "SELECT COUNT(*) FROM notes
593 $results = $this->query($sql, "s", $_GET['note']);
594 if ($results[0]["COUNT(*)"] != 1) {
599 public function display() {
600 $this->display_head();
602 case (isset($_GET['year']) && !isset($_GET['month'])
603 && !isset($_GET['day'])):
604 $sql = "SELECT title, url, date_posted, text
605 FROM notes WHERE YEAR(date_posted) = ?
606 ORDER BY date_posted DESC";
607 $result = $this->query($sql, "d",
610 case (isset($_GET['year']) && isset($_GET['month'])
611 && !isset($_GET['day'])):
612 $sql = "SELECT title, url, date_posted, text
613 FROM notes WHERE YEAR(date_posted) = ?
614 AND MONTH(date_posted) = ?
615 ORDER BY date_posted DESC";
616 $result = $this->query($sql, "dd",
617 $_GET['year'], $_GET['month']);
619 case (isset($_GET['year']) && isset($_GET['month'])
620 && isset($_GET['day'])):
621 $sql = "SELECT title, url, date_posted, text
622 FROM notes WHERE YEAR(date_posted) = ?
623 AND MONTH(date_posted) = ?
624 AND DAY(date_posted) = ?
625 ORDER BY date_posted DESC";
626 $result = $this->query($sql, "ddd",
627 $_GET['year'], $_GET['month'],
631 if (count($result) >= 1) {
632 echo "<div id='notes'>";
633 foreach ($result as $row => $entry) {
634 $title = $entry['title'];
635 $url = '/note/' . $entry['url'];
636 $date_posted = explode("-", $entry['date_posted']);
637 $year_posted = $date_posted[0];
638 $month_posted = $date_posted[1];
639 $datetime_posted = explode(' ', $date_posted[2]);
640 $day_posted = $datetime_posted[0];
641 echo "<div class='note'>";
642 echo "<h1><span class='date'>";
643 echo "$year_posted/$month_posted/$day_posted/";
644 echo "</span><a href='$url'>$title</a></h1>";
649 $this->write_navigation();
652 echo "<h1>sorry, nothing here</h2>";
653 echo "<pre>Empty set (0.00 sec)</pre>";
655 $this->display_close();
658 private function write_navigation() {
660 echo "<div id='navigation'>";
668 class rss extends cms {
669 public function display() {
670 $result = $this->db->query("SELECT date_posted, title, text, url
671 FROM notes ORDER BY date_posted DESC
674 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
676 <title>dylanstestserver.com/notes/rss</title>
677 <link>http://dylanstestserver.com/notes</link>
678 <description>dylanstestserver.com/notes/rss</description>
679 <atom:link href="http://dylanstestserver.com/notes/rss" rel="self" type="application/rss+xml" />
681 while ($entry = $result->fetch_object()) {
682 $title = $entry->title;
683 $date_posted = $entry->date_posted;
684 $url = "http://dylanstestserver.com/note/" . $entry->url;
685 $text = $entry->text;
686 $text = strip_tags($text);
687 $end_of_first_sentence = strpos($text, '.');
688 if ($end_of_first_sentence) {
689 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
690 if ($end_of_second_sentence) {
691 $description = substr($text, '0', ($end_of_second_sentence + 1));
693 $description = substr($text, '0', ($end_of_first_sentence + 1));
698 <title>$title</title>
701 <description>$description</description>
712 class notFound extends Exception {
714 public function __construct() {
715 header('HTTP/1.0 404 Not Found');
724 class captcha extends cms {
726 public function display() {
727 $challenge = $_GET['challenge'];
728 $response = $_GET['response'];
729 $remoteip = $_SERVER['REMOTE_ADDR'];
730 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
731 curl_setopt ($curl, CURLOPT_POST, 4);
732 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
733 $result = curl_exec ($curl);
740 ## now actually do something:
741 switch (cms::determine_type()) {
743 $index = new index();
747 $project = new project();
762 $archive = new archive;
766 $captcha = new captcha;