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 require_once("view/index.php");
174 protected function display_exhibits() {
175 echo "<div id='exhibit'>";
176 $sql = "SELECT text FROM projects ORDER BY rank";
177 $result = $this->db->query($sql);
178 while ($entry = $result->fetch_object()) {
184 private function list_projects() {
185 $sql = "SELECT title FROM projects ORDER BY rank";
186 $result = $this->db->query($sql);
187 while ($entry = $result->fetch_object()) {
188 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
195 class project extends index {
197 protected function display_exhibits() {
198 echo "<div id='exhibit'>";
199 $sql = "SELECT text FROM projects
201 $result = $this->query($sql, "s", $_GET['project']);
202 if ($result = $result[0]['text']) {
203 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
207 throw new notFound();
214 class page extends cms {
218 private $notes_per_page = 4;
219 private $number_of_pages = 1;
221 public function __construct() {
222 parent::__construct();
223 $this->page_offset();
225 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
226 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
227 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
228 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
229 <script type='text/javascript'>
230 function highlight() {
231 SyntaxHighlighter.autoloader(
232 'js /includes/syntax/scripts/shBrushJScript.js',
233 'bash /includes/syntax/scripts/shBrushBash.js',
234 'sql /includes/syntax/scripts/shBrushSql.js',
235 'cpp /includes/syntax/scripts/shBrushCpp.js');
236 SyntaxHighlighter.defaults['gutter'] = false;
237 SyntaxHighlighter.defaults['toolbar'] = false;
238 SyntaxHighlighter.all();
244 private function page_offset() {
245 $sql = "SELECT COUNT(*) FROM notes";
246 $result = $this->db->query($sql);
247 $result = $result->fetch_array();
248 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
249 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
250 $this->page = (int) $_GET['page'];
252 throw new notFound();
254 if ($this->page > $this->number_of_pages) {
255 throw new notFound();
257 if ($this->page < 1) {
258 throw new notFound();
260 $this->offset = ($this->page - 1) * $this->notes_per_page;
263 public function display() {
264 require_once("view/page.php");
267 public function display_notes() {
268 echo "<div id='notes'>";
269 $sql = "SELECT date_posted, title, url, text
270 FROM notes ORDER BY date_posted DESC
272 $result = $this->query($sql, "ii",
274 $this->notes_per_page);
275 foreach ($result as $row => $entry) {
276 $title = $entry['title'];
277 $url = '/note/' . $entry['url'];
278 $date_posted = explode("-", $entry['date_posted']);
279 $year_posted = $date_posted[0];
280 $month_posted = $date_posted[1];
281 $datetime_posted = explode(' ', $date_posted[2]);
282 $day_posted = $datetime_posted[0];
283 $text = $entry['text'];
287 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
298 class note extends cms {
301 private $comments_enabled = false;
302 private $failed_captcha;
306 public $month_posted;
309 public $number_of_comments;
311 public function __construct() {
313 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
314 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
315 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
316 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
317 <script type='text/javascript'>
318 function highlight() {
319 SyntaxHighlighter.autoloader(
320 'js /includes/syntax/scripts/shBrushJScript.js',
321 'bash /includes/syntax/scripts/shBrushBash.js',
322 'sql /includes/syntax/scripts/shBrushSql.js',
323 'cpp /includes/syntax/scripts/shBrushCpp.js');
324 SyntaxHighlighter.defaults['gutter'] = false;
325 SyntaxHighlighter.defaults['toolbar'] = false;
326 SyntaxHighlighter.all();
331 if (isset($_GET['comments'])) {
333 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
334 <script type='text/javascript' src='/includes/comment.js'></script>";
336 parent::__construct();
337 if (isset($_GET['comments'])) {
338 $this->comments_enabled = true;
340 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
341 if (isset($_GET['verify'])) {
342 $url = substr($url, 0, (strlen($url)-6));
345 $sql = "SELECT title, date_posted, text, id
346 FROM notes WHERE url = ?";
347 $result = $this->query($sql, "s",
351 $this->id = $entry["id"];
352 $this->title = $entry["title"];
353 $date_posted = explode("-", $entry["date_posted"]);
354 $this->year_posted = $date_posted[0];
355 $this->month_posted = $date_posted[1];
356 $datetime_posted = explode(' ', $date_posted[2]);
357 $this->day_posted = $datetime_posted[0];
358 $this->text = $entry["text"];
360 throw new notFound();
362 $sql = "SELECT COUNT(*) FROM comments
363 WHERE note = $this->id";
364 $result = $this->db->query($sql);
365 $result = $result->fetch_array();
366 $this->number_of_comments = $result[0];
367 if (isset($_GET['verify'])) {
372 public function display() {
373 require_once("view/note.php");
376 private function verify() {
377 if (!isset($_POST['captcha'])) {
378 require_once('includes/recaptchalib.php');
380 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
381 $_SERVER["REMOTE_ADDR"],
382 $_POST["recaptcha_challenge_field"],
383 $_POST["recaptcha_response_field"]);
384 if (!$resp->is_valid) {
385 $this->failed_captcha = true;
388 if (isset($_POST['captcha']) || $resp->is_valid) {
389 $sql = ("INSERT INTO comments (date_posted, author,
391 VALUES(NOW(), ?, ?, ?)");
392 $stmt = $this->db->prepare($sql);
393 // Checks are needed here (no blank text,
394 // and a default author needs to be set
395 // for no-javascript users.
396 $stmt->bind_param('sss',
404 private function display_comment_link() {
405 if ($this->number_of_comments > 0) {
406 $anchor_text = "comments($this->number_of_comments)/";
408 $anchor_text = "comment?";
410 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
411 $url = $this->url . 'comments/';
413 $url = $this->url . '/comments/';
415 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
418 private function display_comments() {
419 echo "<div id='comments'>";
420 $sql= "SELECT date_posted, author, text
421 FROM comments WHERE note = ?
422 ORDER BY date_posted DESC";
423 $result = $this->query($sql, 'd', $this->id);
424 foreach ($result as $row => $entry) {
425 $date_posted = $entry['date_posted'];
426 $author = $entry['author'];
427 $text = htmlspecialchars($entry['text']);
428 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
429 echo <<<END_OF_COMMENT
430 <div class='comment'>
439 private function display_comment_form() {
440 $publickey = $this->recaptcha_publickey;
441 echo <<<END_CAPTCHA_STYLE
442 <script type="text/javascript">
443 Recaptcha.create("$publickey",
447 custom_theme_widget: 'recaptcha_widget',
448 callback: Recaptcha.focus_response_field
452 require_once('includes/recaptchalib.php');
453 $url = $this->url . "verify";
454 echo "<form id='comment_form' method='post' action='$url'>";
458 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
460 <input type=text name="name" id="comment_name">
463 <div id="recaptcha_widget">
465 <h3><b>what's this say</b>?</h3>
467 <div id="recaptcha_image"></div>
469 <span class="recaptcha_only_if_image"><br><br><br></span>
470 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
472 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
473 <span class="recaptcha_help">
474 <a href="javascript:Recaptcha.reload()">another?</a> /
475 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
476 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
480 echo recaptcha_get_html($this->recaptcha_publickey);
481 if ($this->failed_captcha) {
482 echo <<<END_OF_ERRORS
484 reCAPTCHA said you're not human, <br>
487 <input id="submit" class="submit" type="submit" value="post comment">
492 echo <<<END_OF_ERRORS
494 reCAPTCHA said you're not human, <br>
497 <div id="blank_comment">
498 but you didn't write anything! <br>
503 <input id="submit" class="submit" type="submit" value="post comment">
511 class archive extends cms {
513 public function __construct() {
514 parent::__construct();
517 private function check_exists() {
518 $sql = "SELECT COUNT(*) FROM notes
520 $results = $this->query($sql, "s", $_GET['note']);
521 if ($results[0]["COUNT(*)"] != 1) {
526 public function display() {
527 require_once("view/archive.php");
530 public function display_notes() {
532 case (isset($_GET['year']) && !isset($_GET['month'])
533 && !isset($_GET['day'])):
534 $sql = "SELECT title, url, date_posted, text
535 FROM notes WHERE YEAR(date_posted) = ?
536 ORDER BY date_posted DESC";
537 $result = $this->query($sql, "d",
540 case (isset($_GET['year']) && isset($_GET['month'])
541 && !isset($_GET['day'])):
542 $sql = "SELECT title, url, date_posted, text
543 FROM notes WHERE YEAR(date_posted) = ?
544 AND MONTH(date_posted) = ?
545 ORDER BY date_posted DESC";
546 $result = $this->query($sql, "dd",
547 $_GET['year'], $_GET['month']);
549 case (isset($_GET['year']) && isset($_GET['month'])
550 && isset($_GET['day'])):
551 $sql = "SELECT title, url, date_posted, text
552 FROM notes WHERE YEAR(date_posted) = ?
553 AND MONTH(date_posted) = ?
554 AND DAY(date_posted) = ?
555 ORDER BY date_posted DESC";
556 $result = $this->query($sql, "ddd",
557 $_GET['year'], $_GET['month'],
561 if (count($result) >= 1) {
562 echo "<div id='notes'>";
563 foreach ($result as $row => $entry) {
564 $title = $entry['title'];
565 $url = '/note/' . $entry['url'];
566 $date_posted = explode("-", $entry['date_posted']);
567 $year_posted = $date_posted[0];
568 $month_posted = $date_posted[1];
569 $datetime_posted = explode(' ', $date_posted[2]);
570 $day_posted = $datetime_posted[0];
571 echo "<div class='note'>";
572 echo "<h1><span class='date'>";
573 echo "$year_posted/$month_posted/$day_posted/";
574 echo "</span><a href='$url'>$title</a></h1>";
581 echo "<h1>sorry, nothing here</h2>";
582 echo "<pre>Empty set (0.00 sec)</pre>";
589 class rss extends cms {
590 public function display() {
591 $result = $this->db->query("SELECT date_posted, title, text, url
592 FROM notes ORDER BY date_posted DESC
595 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
597 <title>dylansserver.com/notes/rss</title>
598 <link>http://dylansserver.com/notes</link>
599 <description>dylansserver.com/notes/rss</description>
600 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
602 while ($entry = $result->fetch_object()) {
603 $title = $entry->title;
604 $date_posted = $entry->date_posted;
605 $url = "http://dylansserver.com/note/" . $entry->url;
606 $text = $entry->text;
607 $text = strip_tags($text);
608 $end_of_first_sentence = strpos($text, '.');
609 if ($end_of_first_sentence) {
610 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
611 if ($end_of_second_sentence) {
612 $description = substr($text, '0', ($end_of_second_sentence + 1));
614 $description = substr($text, '0', ($end_of_first_sentence + 1));
619 <title>$title</title>
622 <description>$description</description>
633 class notFound extends Exception {
635 public function __construct() {
636 header('HTTP/1.0 404 Not Found');
645 class captcha extends cms {
647 public function display() {
648 $challenge = $_GET['challenge'];
649 $response = $_GET['response'];
650 $remoteip = $_SERVER['REMOTE_ADDR'];
651 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
652 curl_setopt ($curl, CURLOPT_POST, 4);
653 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
654 $result = curl_exec ($curl);