b29d6295d37b9e582fff279e77742f409b2911ae
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['challenge'])) {
48 public function query() {
49 $args = func_get_args();
50 $statement = $this->db
->prepare($args[0]);
51 $args = array_slice($args, 1);
52 call_user_func_array(array($statement, 'bind_param'), &$args);
53 $statement->execute();
55 $statement->store_result();
57 $data = $statement->result_metadata();
59 $fields[0] = &$statement;
60 while($field = $data->fetch_field()) {
61 $fields[] = &$row[$field->name
];
63 call_user_func_array("mysqli_stmt_bind_result", $fields);
65 while ($statement->fetch()) {
66 foreach ($row as $key=>$value) $return[$i][$key] = $value;
69 $statement->free_result();
73 public function display_head($title = "dylansserver",
75 $scripts = $this->scripts
;
76 $stylesheets = "<link href='/includes/style.css' rel='stylesheet' type='text/css'>";
77 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylansserver.com&ucn_task=conformance#";
79 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
80 "http://www.w3.org/TR/html4/loose.dtd">
84 <meta name="generator" content=
85 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
86 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
88 <title>$this->title</title>
89 <link rel="icon" href="/favicon.ico" type="image/png">
97 <a href="$this->home_link">
98 <img src="/images/dylansserver.png" alt="dylansserver"
106 public function display_contact() {
107 echo <<<END_OF_CONTACT
108 <div id="contact_me"><h1><a href=
109 "mailto:dylan@psu.edu">dylan</a></h1><a href=
110 "mailto:dylan@psu.edu">@psu.edu</a>
115 public function display_close($show_contact = true) {
117 $this->display_contact();
133 class index extends cms {
135 public function display() {
136 $this->scripts = "<script type='text/javascript' src='/includes/index.js'></script>";
137 $this->display_head();
138 $this->display_exhibits();
139 echo "<ul id='portfolio' style='text-align:right'>";
140 $this->list_projects();
141 echo <<<OTHER_PROJECTS
143 <h3>things i've done for others:</h3>
147 "http://activehamptons.com">activehamptons.com</a></li>
150 "http://transfishing.com">transfishing.com</a></li>
153 <h3>something i've worked on:</h3>
157 "http://tempositions.com">tempositions.com</a></li>
160 <h3>my repositories:</h3>
164 "git">git://dylansserver.com</a></li>
171 "/notes/">here</a></li>
176 // Because of the CSS necessary for the animations,
177 // the contact link needs to be in #portfolio to clear
180 $this->display_contact();
183 $this->display_close($show_contact = false);
186 protected function display_exhibits() {
187 echo "<div id='exhibit'>";
188 $sql = "SELECT text FROM projects";
189 $result = $this->db->query($sql);
190 while ($entry = $result->fetch_object()) {
196 private function list_projects() {
199 <h3>my projects:</h3>
202 $sql = "SELECT title FROM projects";
203 $result = $this->db->query($sql);
204 while ($entry = $result->fetch_object()) {
205 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
212 class project extends index {
214 protected function display_exhibits() {
215 echo "<div id='exhibit'>";
216 $sql = "SELECT text FROM projects
218 $result = $this->query($sql, "s", $_GET['project']);
219 if ($result = $result[0]['text']) {
220 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
224 throw new notFound();
231 class page extends cms {
235 private $notes_per_page = 4;
236 private $number_of_pages = 1;
238 public function __construct() {
239 parent::__construct();
240 $this->page_offset();
243 private function page_offset() {
244 $sql = "SELECT COUNT(*) FROM notes";
245 $result = $this->db->query($sql);
246 $result = $result->fetch_array();
247 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
248 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
249 $this->page = (int) $_GET['page'];
251 throw new notFound();
253 if ($this->page > $this->number_of_pages) {
254 throw new notFound();
256 if ($this->page < 1) {
257 throw new notFound();
259 $this->offset = ($this->page - 1) * $this->notes_per_page;
262 public function display() {
263 $this->display_head();
264 echo "<div id='notes'>";
265 $sql = "SELECT date_posted, title, url, text
266 FROM notes ORDER BY date_posted DESC
268 $result = $this->query($sql, "ii",
270 $this->notes_per_page);
271 foreach ($result as $row => $entry) {
272 $title = $entry['title'];
273 $url = '/note/' . $entry['url'];
274 $date_posted = explode("-", $entry['date_posted']);
275 $year_posted = $date_posted[0];
276 $month_posted = $date_posted[1];
277 $datetime_posted = explode(' ', $date_posted[2]);
278 $day_posted = $datetime_posted[0];
279 echo "<div class='note'>";
280 echo "<h1><span class='date'>$year_posted/$month_posted/$day_posted/</span><a href='$url'>$title</a></h1>";
285 $this->write_navigation();
286 $this->display_close();
289 private function write_navigation() {
290 echo "<div id='navigation'>";
293 $previous_page = $this->page - 1;
294 echo "<a href='/notes/page/$previous_page'>prev</a>";
296 if($this->page < $this->number_of_pages) {
297 $forward_page = $this->page + 1;
298 echo " <a href='/notes/page/$forward_page'>next</a>";
307 class note extends cms {
310 private $comments_enabled = false;
311 private $failed_captcha;
315 public $month_posted;
318 public $number_of_comments;
320 public function __construct() {
321 if (isset($_GET['comments'])) {
323 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
324 <script type='text/javascript' src='/includes/comment.js'></script>";
326 parent::__construct();
327 if (isset($_GET['comments'])) {
328 $this->comments_enabled = true;
330 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
331 if (isset($_GET['verify'])) {
332 $url = substr($url, 0, (strlen($url)-6));
335 $sql = "SELECT title, date_posted, text, id
336 FROM notes WHERE url = ?";
337 $result = $this->query($sql, "s",
341 $this->id = $entry["id"];
342 $this->title = $entry["title"];
343 $date_posted = explode("-", $entry["date_posted"]);
344 $this->year_posted = $date_posted[0];
345 $this->month_posted = $date_posted[1];
346 $datetime_posted = explode(' ', $date_posted[2]);
347 $this->day_posted = $datetime_posted[0];
348 $this->text = $entry["text"];
350 throw new notFound();
352 $sql = "SELECT COUNT(*) FROM comments
353 WHERE note = $this->id";
354 $result = $this->db->query($sql);
355 $result = $result->fetch_array();
356 $this->number_of_comments = $result[0];
357 if (isset($_GET['verify'])) {
362 public function display() {
363 $this->display_head();
364 $this->display_note();
365 if ($this->comments_enabled) {
366 $this->display_comments();
367 $this->display_comment_form();
369 $this->write_navigation();
370 $this->display_close();
373 private function verify() {
374 if (!isset($_POST['captcha'])) {
375 require_once('includes/recaptchalib.php');
377 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
378 $_SERVER["REMOTE_ADDR"],
379 $_POST["recaptcha_challenge_field"],
380 $_POST["recaptcha_response_field"]);
381 if (!$resp->is_valid) {
382 $this->failed_captcha = true;
385 if (isset($_POST['captcha']) || $resp->is_valid) {
386 $sql = ("INSERT INTO comments (date_posted, author,
388 VALUES(NOW(), ?, ?, ?)");
389 $stmt = $this->db->prepare($sql);
390 // Checks are needed here (no blank text,
391 // and a default author needs to be set
392 // for no-javascript users.
393 $stmt->bind_param('sss',
394 htmlspecialchars($_POST['name']),
395 htmlspecialchars($_POST['text']),
401 private function display_note() {
404 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
410 private function write_navigation() {
411 echo <<<END_OF_NAVIGATION
413 <div id='navigation'>
416 if (!$this->comments_enabled) {
417 $this->display_comment_link();
419 echo <<<END_OF_NAVIGATION
420 <a href="/notes/">back to notes</a>/
426 private function display_comment_link() {
427 if ($this->number_of_comments > 0) {
428 $anchor_text = "comments($this->number_of_comments)/";
430 $anchor_text = "comment?";
432 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
433 $url = $this->url . 'comments/';
435 $url = $this->url . '/comments/';
437 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
440 private function display_comments() {
441 echo "<div id='comments'>";
442 $sql= "SELECT date_posted, author, text
443 FROM comments WHERE note = ?
444 ORDER BY date_posted DESC";
445 $result = $this->query($sql, 'd', $this->id);
446 foreach ($result as $row => $entry) {
447 $date_posted = $entry['date_posted'];
448 $author = $entry['author'];
449 $text = htmlspecialchars($entry['text']);
450 $head = "<h3>$author</h3>";
451 echo <<<END_OF_COMMENT
452 <div class='comment'>
461 private function display_comment_form() {
462 $publickey = $this->recaptcha_publickey;
463 echo <<<END_CAPTCHA_STYLE
464 <script type="text/javascript">
465 Recaptcha.create("$publickey",
469 custom_theme_widget: 'recaptcha_widget',
470 callback: Recaptcha.focus_response_field
474 require_once('includes/recaptchalib.php');
475 $url = $this->url . "verify";
476 echo "<form id='comment_form' method='post' action='$url'>";
480 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
482 <input type=text name="name" id="comment_name">
485 <div id="recaptcha_widget">
486 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
487 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
488 <span style="font-size:80%;">
489 ( <a href="javascript:Recaptcha.reload()">another</a> /
490 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
491 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
494 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
496 <div style="float:right;position:relative;width:100px;">
497 <div id="recaptcha_image"></div>
502 echo recaptcha_get_html($this->recaptcha_publickey);
503 if ($this->failed_captcha) {
504 echo <<<END_OF_ERRORS
506 reCAPTCHA said you're not human, <br>
509 <input id="submit" class="submit" type="submit" value="post comment">
514 echo <<<END_OF_ERRORS
516 reCAPTCHA said you're not human, <br>
519 <div id="blank_comment">
520 but you didn't write anything! <br>
525 <input id="submit" class="submit" type="submit" value="post comment">
533 class archive extends cms {
535 public function __construct() {
536 parent::__construct();
539 private function check_exists() {
540 $sql = "SELECT COUNT(*) FROM notes
542 $results = $this->query($sql, "s", $_GET['note']);
543 if ($results[0]["COUNT(*)"] != 1) {
548 public function display() {
549 $this->display_head();
551 case (isset($_GET['year']) && !isset($_GET['month'])
552 && !isset($_GET['day'])):
553 $sql = "SELECT title, url, date_posted, text
554 FROM notes WHERE YEAR(date_posted) = ?
555 ORDER BY date_posted DESC";
556 $result = $this->query($sql, "d",
559 case (isset($_GET['year']) && isset($_GET['month'])
560 && !isset($_GET['day'])):
561 $sql = "SELECT title, url, date_posted, text
562 FROM notes WHERE YEAR(date_posted) = ?
563 AND MONTH(date_posted) = ?
564 ORDER BY date_posted DESC";
565 $result = $this->query($sql, "dd",
566 $_GET['year'], $_GET['month']);
568 case (isset($_GET['year']) && isset($_GET['month'])
569 && isset($_GET['day'])):
570 $sql = "SELECT title, url, date_posted, text
571 FROM notes WHERE YEAR(date_posted) = ?
572 AND MONTH(date_posted) = ?
573 AND DAY(date_posted) = ?
574 ORDER BY date_posted DESC";
575 $result = $this->query($sql, "ddd",
576 $_GET['year'], $_GET['month'],
580 if (count($result) >= 1) {
581 echo "<div id='notes'>";
582 foreach ($result as $row => $entry) {
583 $title = $entry['title'];
584 $url = '/note/' . $entry['url'];
585 $date_posted = explode("-", $entry['date_posted']);
586 $year_posted = $date_posted[0];
587 $month_posted = $date_posted[1];
588 $datetime_posted = explode(' ', $date_posted[2]);
589 $day_posted = $datetime_posted[0];
590 echo "<div class='note'>";
591 echo "<h1><span class='date'>";
592 echo "$year_posted/$month_posted/$day_posted/";
593 echo "</span><a href='$url'>$title</a></h1>";
598 $this->write_navigation();
601 echo "<h1>sorry, nothing here</h2>";
602 echo "<pre>Empty set (0.00 sec)</pre>";
604 $this->display_close();
607 private function write_navigation() {
609 echo "<div id='navigation'>";
617 class notFound extends Exception {
619 public function __construct() {
620 header('HTTP/1.0 404 Not Found');
629 class captcha extends cms {
631 public function display() {
632 $challenge = $_GET['challenge'];
633 $response = $_GET['response'];
634 $remoteip = $_SERVER['REMOTE_ADDR'];
635 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
636 curl_setopt ($curl, CURLOPT_POST, 4);
637 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
638 $result = curl_exec ($curl);
645 ## now actually do something:
646 switch (cms::determine_type()) {
648 $index = new index();
652 $project = new project();
664 $archive = new archive;
668 $captcha = new captcha;