a93e0950f9cbbbb34afa6cdd22d8c0f983d2b65f
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>
178 // Because of the CSS necessary for the animations,
179 // the contact link needs to be in #portfolio to clear
182 $this->display_contact();
185 $this->display_close($show_contact = false);
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()) {
198 private function list_projects() {
201 <h3>my projects:</h3>
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>";
214 class project extends index {
216 protected function display_exhibits() {
217 echo "<div id='exhibit'>";
218 $sql = "SELECT text FROM projects
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);
226 throw new notFound();
233 class page extends cms {
237 private $notes_per_page = 4;
238 private $number_of_pages = 1;
240 public function __construct() {
241 parent::__construct();
242 $this->page_offset();
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'></script>
247 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'></link>
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 SyntaxHighlighter.defaults['gutter'] = false;
255 SyntaxHighlighter.all();
261 private function page_offset() {
262 $sql = "SELECT COUNT(*) FROM notes";
263 $result = $this->db->query($sql);
264 $result = $result->fetch_array();
265 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
266 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
267 $this->page = (int) $_GET['page'];
269 throw new notFound();
271 if ($this->page > $this->number_of_pages) {
272 throw new notFound();
274 if ($this->page < 1) {
275 throw new notFound();
277 $this->offset = ($this->page - 1) * $this->notes_per_page;
280 public function display() {
281 $this->display_head();
282 echo "<div id='notes'>";
283 $sql = "SELECT date_posted, title, url, text
284 FROM notes ORDER BY date_posted DESC
286 $result = $this->query($sql, "ii",
288 $this->notes_per_page);
289 foreach ($result as $row => $entry) {
290 $title = $entry['title'];
291 $url = '/note/' . $entry['url'];
292 $date_posted = explode("-", $entry['date_posted']);
293 $year_posted = $date_posted[0];
294 $month_posted = $date_posted[1];
295 $datetime_posted = explode(' ', $date_posted[2]);
296 $day_posted = $datetime_posted[0];
297 $text = $entry['text'];
301 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
308 $this->write_navigation();
309 $this->display_close();
312 private function write_navigation() {
313 echo "<div id='navigation'>";
316 $previous_page = $this->page - 1;
317 echo "<a href='/notes/page/$previous_page'>prev</a>";
319 if($this->page < $this->number_of_pages) {
320 $forward_page = $this->page + 1;
321 echo " <a href='/notes/page/$forward_page'>next</a>";
330 class note extends cms {
333 private $comments_enabled = false;
334 private $failed_captcha;
338 public $month_posted;
341 public $number_of_comments;
343 public function __construct() {
345 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
346 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
347 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'></script>
348 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'></link>
349 <script type='text/javascript'>
350 function highlight() {
351 SyntaxHighlighter.autoloader(
352 'js /includes/syntax/scripts/shBrushJScript.js',
353 'bash /includes/syntax/scripts/shBrushBash.js',
354 'sql /includes/syntax/scripts/shBrushSql.js');
355 SyntaxHighlighter.defaults['gutter'] = false;
356 SyntaxHighlighter.all();
361 if (isset($_GET['comments'])) {
363 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
364 <script type='text/javascript' src='/includes/comment.js'></script>";
366 parent::__construct();
367 if (isset($_GET['comments'])) {
368 $this->comments_enabled = true;
370 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
371 if (isset($_GET['verify'])) {
372 $url = substr($url, 0, (strlen($url)-6));
375 $sql = "SELECT title, date_posted, text, id
376 FROM notes WHERE url = ?";
377 $result = $this->query($sql, "s",
381 $this->id = $entry["id"];
382 $this->title = $entry["title"];
383 $date_posted = explode("-", $entry["date_posted"]);
384 $this->year_posted = $date_posted[0];
385 $this->month_posted = $date_posted[1];
386 $datetime_posted = explode(' ', $date_posted[2]);
387 $this->day_posted = $datetime_posted[0];
388 $this->text = $entry["text"];
390 throw new notFound();
392 $sql = "SELECT COUNT(*) FROM comments
393 WHERE note = $this->id";
394 $result = $this->db->query($sql);
395 $result = $result->fetch_array();
396 $this->number_of_comments = $result[0];
397 if (isset($_GET['verify'])) {
402 public function display() {
403 $this->display_head();
404 $this->display_note();
405 if ($this->comments_enabled) {
406 $this->display_comments();
407 $this->display_comment_form();
409 $this->write_navigation();
410 $this->display_close();
413 private function verify() {
414 if (!isset($_POST['captcha'])) {
415 require_once('includes/recaptchalib.php');
417 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
418 $_SERVER["REMOTE_ADDR"],
419 $_POST["recaptcha_challenge_field"],
420 $_POST["recaptcha_response_field"]);
421 if (!$resp->is_valid) {
422 $this->failed_captcha = true;
425 if (isset($_POST['captcha']) || $resp->is_valid) {
426 $sql = ("INSERT INTO comments (date_posted, author,
428 VALUES(NOW(), ?, ?, ?)");
429 $stmt = $this->db->prepare($sql);
430 // Checks are needed here (no blank text,
431 // and a default author needs to be set
432 // for no-javascript users.
433 $stmt->bind_param('sss',
434 htmlspecialchars($_POST['name']),
435 htmlspecialchars($_POST['text']),
441 private function display_note() {
444 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
450 private function write_navigation() {
451 echo <<<END_OF_NAVIGATION
453 <div id='navigation'>
456 if (!$this->comments_enabled) {
457 $this->display_comment_link();
459 echo <<<END_OF_NAVIGATION
460 <a href="/notes/">back to notes/</a>
466 private function display_comment_link() {
467 if ($this->number_of_comments > 0) {
468 $anchor_text = "comments($this->number_of_comments)/";
470 $anchor_text = "comment?";
472 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
473 $url = $this->url . 'comments/';
475 $url = $this->url . '/comments/';
477 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
480 private function display_comments() {
481 echo "<div id='comments'>";
482 $sql= "SELECT date_posted, author, text
483 FROM comments WHERE note = ?
484 ORDER BY date_posted DESC";
485 $result = $this->query($sql, 'd', $this->id);
486 foreach ($result as $row => $entry) {
487 $date_posted = $entry['date_posted'];
488 $author = $entry['author'];
489 $text = htmlspecialchars($entry['text']);
490 $head = "<h3>$author</h3>";
491 echo <<<END_OF_COMMENT
492 <div class='comment'>
501 private function display_comment_form() {
502 $publickey = $this->recaptcha_publickey;
503 echo <<<END_CAPTCHA_STYLE
504 <script type="text/javascript">
505 Recaptcha.create("$publickey",
509 custom_theme_widget: 'recaptcha_widget',
510 callback: Recaptcha.focus_response_field
514 require_once('includes/recaptchalib.php');
515 $url = $this->url . "verify";
516 echo "<form id='comment_form' method='post' action='$url'>";
520 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
522 <input type=text name="name" id="comment_name">
525 <div id="recaptcha_widget">
527 <h3><b>what's this say</b>?</h3>
529 <div id="recaptcha_image"></div>
531 <span class="recaptcha_only_if_image"><br><br><br></span>
532 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
534 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
535 <span class="recaptcha_help">
536 <a href="javascript:Recaptcha.reload()">another?</a> /
537 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
538 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
542 echo recaptcha_get_html($this->recaptcha_publickey);
543 if ($this->failed_captcha) {
544 echo <<<END_OF_ERRORS
546 reCAPTCHA said you're not human, <br>
549 <input id="submit" class="submit" type="submit" value="post comment">
554 echo <<<END_OF_ERRORS
556 reCAPTCHA said you're not human, <br>
559 <div id="blank_comment">
560 but you didn't write anything! <br>
565 <input id="submit" class="submit" type="submit" value="post comment">
573 class archive extends cms {
575 public function __construct() {
576 parent::__construct();
579 private function check_exists() {
580 $sql = "SELECT COUNT(*) FROM notes
582 $results = $this->query($sql, "s", $_GET['note']);
583 if ($results[0]["COUNT(*)"] != 1) {
588 public function display() {
589 $this->display_head();
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>";
638 $this->write_navigation();
641 echo "<h1>sorry, nothing here</h2>";
642 echo "<pre>Empty set (0.00 sec)</pre>";
644 $this->display_close();
647 private function write_navigation() {
649 echo "<div id='navigation'>";
657 class rss extends cms {
658 public function display() {
659 $result = $this->db->query("SELECT date_posted, title, text, url
660 FROM notes ORDER BY date_posted DESC
663 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
665 <title>dylanstestserver.com/notes/rss</title>
666 <link>http://dylanstestserver.com/notes</link>
667 <description>dylanstestserver.com/notes/rss</description>
668 <atom:link href="http://dylanstestserver.com/notes/rss" rel="self" type="application/rss+xml" />
670 while ($entry = $result->fetch_object()) {
671 $title = $entry->title;
672 $date_posted = $entry->date_posted;
673 $url = "http://dylanstestserver.com/note/" . $entry->url;
674 $text = $entry->text;
675 $text = strip_tags($text);
676 $end_of_first_sentence = strpos($text, '.');
677 if ($end_of_first_sentence) {
678 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
679 if ($end_of_second_sentence) {
680 $description = substr($text, '0', ($end_of_second_sentence + 1));
682 $description = substr($text, '0', ($end_of_first_sentence + 1));
687 <title>$title</title>
690 <description>$description</description>
701 class notFound extends Exception {
703 public function __construct() {
704 header('HTTP/1.0 404 Not Found');
713 class captcha extends cms {
715 public function display() {
716 $challenge = $_GET['challenge'];
717 $response = $_GET['response'];
718 $remoteip = $_SERVER['REMOTE_ADDR'];
719 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
720 curl_setopt ($curl, CURLOPT_POST, 4);
721 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
722 $result = curl_exec ($curl);
729 ## now actually do something:
730 switch (cms::determine_type()) {
732 $index = new index();
736 $project = new project();
751 $archive = new archive;
755 $captcha = new captcha;