88f82d2410f52e0f59fdfbe72e9142e53265b75d
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'>
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();
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'];
271 throw new notFound();
273 if ($this->page > $this->number_of_pages) {
274 throw new notFound();
276 if ($this->page < 1) {
277 throw new notFound();
279 $this->offset = ($this->page - 1) * $this->notes_per_page;
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
288 $result = $this->query($sql, "ii",
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'];
303 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
310 $this->write_navigation();
311 $this->display_close();
314 private function write_navigation() {
315 echo "<div id='navigation'>";
318 $previous_page = $this->page - 1;
319 echo "<a href='/notes/page/$previous_page'>prev</a>";
321 if($this->page < $this->number_of_pages) {
322 $forward_page = $this->page + 1;
323 echo " <a href='/notes/page/$forward_page'>next</a>";
332 class note extends cms {
335 private $comments_enabled = false;
336 private $failed_captcha;
340 public $month_posted;
343 public $number_of_comments;
345 public function __construct() {
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();
365 if (isset($_GET['comments'])) {
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>";
370 parent::__construct();
371 if (isset($_GET['comments'])) {
372 $this->comments_enabled = true;
374 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
375 if (isset($_GET['verify'])) {
376 $url = substr($url, 0, (strlen($url)-6));
379 $sql = "SELECT title, date_posted, text, id
380 FROM notes WHERE url = ?";
381 $result = $this->query($sql, "s",
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"];
394 throw new notFound();
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'])) {
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();
413 $this->write_navigation();
414 $this->display_close();
417 private function verify() {
418 if (!isset($_POST['captcha'])) {
419 require_once('includes/recaptchalib.php');
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;
429 if (isset($_POST['captcha']) || $resp->is_valid) {
430 $sql = ("INSERT INTO comments (date_posted, author,
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']),
445 private function display_note() {
448 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
454 private function write_navigation() {
455 echo <<<END_OF_NAVIGATION
457 <div id='navigation'>
460 if (!$this->comments_enabled) {
461 $this->display_comment_link();
463 echo <<<END_OF_NAVIGATION
464 <a href="/notes/">back to notes/</a>
470 private function display_comment_link() {
471 if ($this->number_of_comments > 0) {
472 $anchor_text = "comments($this->number_of_comments)/";
474 $anchor_text = "comment?";
476 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
477 $url = $this->url . 'comments/';
479 $url = $this->url . '/comments/';
481 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
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'>
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",
513 custom_theme_widget: 'recaptcha_widget',
514 callback: Recaptcha.focus_response_field
518 require_once('includes/recaptchalib.php');
519 $url = $this->url . "verify";
520 echo "<form id='comment_form' method='post' action='$url'>";
524 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
526 <input type=text name="name" id="comment_name">
529 <div id="recaptcha_widget">
531 <h3><b>what's this say</b>?</h3>
533 <div id="recaptcha_image"></div>
535 <span class="recaptcha_only_if_image"><br><br><br></span>
536 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
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>
546 echo recaptcha_get_html($this->recaptcha_publickey);
547 if ($this->failed_captcha) {
548 echo <<<END_OF_ERRORS
550 reCAPTCHA said you're not human, <br>
553 <input id="submit" class="submit" type="submit" value="post comment">
558 echo <<<END_OF_ERRORS
560 reCAPTCHA said you're not human, <br>
563 <div id="blank_comment">
564 but you didn't write anything! <br>
569 <input id="submit" class="submit" type="submit" value="post comment">
577 class archive extends cms {
579 public function __construct() {
580 parent::__construct();
583 private function check_exists() {
584 $sql = "SELECT COUNT(*) FROM notes
586 $results = $this->query($sql, "s", $_GET['note']);
587 if ($results[0]["COUNT(*)"] != 1) {
592 public function display() {
593 $this->display_head();
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",
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']);
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'],
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>";
642 $this->write_navigation();
645 echo "<h1>sorry, nothing here</h2>";
646 echo "<pre>Empty set (0.00 sec)</pre>";
648 $this->display_close();
651 private function write_navigation() {
653 echo "<div id='navigation'>";
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
667 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
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" />
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));
686 $description = substr($text, '0', ($end_of_first_sentence + 1));
691 <title>$title</title>
694 <description>$description</description>
705 class notFound extends Exception {
707 public function __construct() {
708 header('HTTP/1.0 404 Not Found');
717 class captcha extends cms {
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);
733 ## now actually do something:
734 switch (cms::determine_type()) {
736 $index = new index();
740 $project = new project();
755 $archive = new archive;
759 $captcha = new captcha;