5 private $config_file = '/etc/dylanstestserver.ini';
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
12 public function __construct() {
13 $config = parse_ini_file($this->config_file
, true);
14 $this->db
= new mysqli(
15 $config[database
]['domain'],
16 $config[database
]['user'],
17 $config[database
]['password'],
18 $config[database
]['database']);
19 if (mysqli_connect_errno()) {
20 echo "Problem connecting to database: ";
21 echo mysqli_connect_error();
24 $this->recaptcha_publickey
= $config['recaptcha']['publickey'];
25 $this->recaptcha_privatekey
= $config['recaptcha']['privatekey'];
26 $this->title
= $config['site']['default_title'];
27 $this->home_link
= $config['site']['home_link'];
31 public static function determine_type() {
32 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
34 } else if (isset($_GET['year'])) {
36 } else if (isset($_GET['note'])) {
38 } else if ($_SERVER['REQUEST_URI'] == '/') {
40 } else if (isset($_GET['project'])) {
42 } else if (isset($_GET['challenge'])) {
47 public function query() {
48 $args = func_get_args();
49 $statement = $this->db
->prepare($args[0]);
50 $args = array_slice($args, 1);
51 call_user_func_array(array($statement, 'bind_param'), &$args);
52 $statement->execute();
54 $statement->store_result();
56 $data = $statement->result_metadata();
58 $fields[0] = &$statement;
59 while($field = $data->fetch_field()) {
60 $fields[] = &$row[$field->name
];
62 call_user_func_array("mysqli_stmt_bind_result", $fields);
64 while ($statement->fetch()) {
65 foreach ($row as $key=>$value) $return[$i][$key] = $value;
68 $statement->free_result();
72 public function display_head($title = "dylanstestserver",
75 $stylesheets = "<link href='/includes/style.css' rel='stylesheet' type='text/css'>";
76 if (cms
::determine_type() == "index") {
77 $scripts = "<script type='text/javascript' src='/includes/all.js'></script>";
78 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylanstestserver.com&ucn_task=conformance#";
79 } else if ($this->determine_type() == 'note') {
80 $scripts = "<script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>";
81 $scripts .= "<script type='text/javascript' src='/includes/jquery-core.js'></script>";
82 $scripts .= "<script type='text/javascript' src='/includes/jquery-all-components.js'></script>";
83 $scripts .= "<script type='text/javascript' src='/includes/ajax.js'></script>";
86 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
87 "http://www.w3.org/TR/html4/loose.dtd">
91 <meta name="generator" content=
92 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
93 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
95 <title>$this->title</title>
96 <link rel="icon" href="favicon.ico" type="image/png">
104 <a href="$this->home_link">
105 <img src="/images/dylanstestserver.png" alt="dylanstestserver"
113 public function display_contact() {
114 echo <<<END_OF_CONTACT
115 <div id="contact_me"><h1><a href=
116 "mailto:dylan@psu.edu">dylan</a></h1><a href=
117 "mailto:dylan@psu.edu">@psu.edu</a>
122 public function display_close($show_contact = true) {
124 $this->display_contact();
140 class index extends cms {
142 public function display() {
143 $this->display_head();
144 $this->display_exhibits();
145 echo "<ul id='portfolio' style='text-align:right'>";
146 $this->list_projects();
147 echo <<<OTHER_PROJECTS
149 <h3>things i've done for others:</h3>
153 "http://activehamptons.com">activehamptons.com</a></li>
156 "http://transfishing.com">transfishing.com</a></li>
159 <h3>something i've worked on:</h3>
163 "http://tempositions.com">tempositions.com</a></li>
166 <h3>my repositories:</h3>
170 "git">git://dylanstestserver.com</a></li>
177 "/notes/">here</a></li>
182 // Because of the CSS necessary for the animations,
183 // the contact link needs to be in #portfolio to clear
186 $this->display_contact();
189 $this->display_close($show_contact = false);
192 protected function display_exhibits() {
193 echo "<div id='exhibit'>";
194 $sql = "SELECT text FROM projects";
195 $result = $this->db->query($sql);
196 while ($entry = $result->fetch_object()) {
202 private function list_projects() {
205 <h3>my projects:</h3>
208 $sql = "SELECT title FROM projects";
209 $result = $this->db->query($sql);
210 while ($entry = $result->fetch_object()) {
211 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
218 class project extends index {
220 protected function display_exhibits() {
221 echo "<div id='exhibit'>";
222 $sql = "SELECT text FROM projects
224 $result = $this->query($sql, "s", $_GET['project']);
225 if ($result = $result[0]['text']) {
226 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
230 throw new notFound();
237 class page extends cms {
241 private $notes_per_page = 4;
242 private $number_of_pages = 1;
244 public function __construct() {
245 parent::__construct();
246 $this->page_offset();
249 private function page_offset() {
250 $sql = "SELECT COUNT(*) FROM notes";
251 $result = $this->db->query($sql);
252 $result = $result->fetch_array();
253 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
254 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
255 $this->page = (int) $_GET['page'];
257 throw new notFound();
259 if ($this->page > $this->number_of_pages) {
260 throw new notFound();
262 if ($this->page < 1) {
263 throw new notFound();
265 $this->offset = ($this->page - 1) * $this->notes_per_page;
268 public function display() {
269 $this->display_head();
270 echo "<div id='notes'>";
271 $sql = "SELECT date_posted, title, url, text
272 FROM notes ORDER BY date_posted DESC
274 $result = $this->query($sql, "ii",
276 $this->notes_per_page);
277 foreach ($result as $row => $entry) {
278 $title = $entry['title'];
279 $url = '/note/' . $entry['url'];
280 $date_posted = explode("-", $entry['date_posted']);
281 $year_posted = $date_posted[0];
282 $month_posted = $date_posted[1];
283 $datetime_posted = explode(' ', $date_posted[2]);
284 $day_posted = $datetime_posted[0];
285 echo "<div class='note'>";
286 echo "<h2><span style='color:grey;'>$year_posted/$month_posted/$day_posted/</span><a href='$url'>$title</a></h2>";
291 $this->write_navigation();
292 $this->display_close();
295 private function write_navigation() {
296 echo "<div id='navigation'>";
299 $previous_page = $this->page - 1;
300 echo "<a href='/notes/page/$previous_page'>prev</a>";
302 if($this->page < $this->number_of_pages) {
303 $forward_page = $this->page + 1;
304 echo " <a href='/notes/page/$forward_page'>next</a>";
313 class note extends cms {
316 private $comments_enabled = false;
317 private $failed_captcha;
321 public $month_posted;
324 public $number_of_comments;
326 public function __construct() {
327 parent::__construct();
328 if (isset($_GET['comments'])) {
329 $this->comments_enabled = true;
331 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
332 if (isset($_GET['verify'])) {
333 $url = substr($url, 0, (strlen($url)-6));
336 $sql = "SELECT title, date_posted, text, id
337 FROM notes WHERE url = ?";
338 $result = $this->query($sql, "s",
342 $this->id = $entry["id"];
343 $this->title = $entry["title"];
344 $date_posted = explode("-", $entry["date_posted"]);
345 $this->year_posted = $date_posted[0];
346 $this->month_posted = $date_posted[1];
347 $datetime_posted = explode(' ', $date_posted[2]);
348 $this->day_posted = $datetime_posted[0];
349 $this->text = $entry["text"];
351 throw new notFound();
353 $sql = "SELECT COUNT(*) FROM comments
354 WHERE note = $this->id";
355 $result = $this->db->query($sql);
356 $result = $result->fetch_array();
357 $this->number_of_comments = $result[0];
358 if (isset($_GET['verify'])) {
363 public function display() {
364 $this->display_head();
365 $this->display_note();
366 if ($this->comments_enabled) {
367 $this->display_comments();
368 $this->display_comment_form();
370 $this->write_navigation();
371 $this->display_close();
374 private function verify() {
375 if (!isset($_POST['captcha'])) {
376 require_once('includes/recaptchalib.php');
378 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
379 $_SERVER["REMOTE_ADDR"],
380 $_POST["recaptcha_challenge_field"],
381 $_POST["recaptcha_response_field"]);
382 if (!$resp->is_valid) {
383 $this->failed_captcha = true;
386 if (isset($_POST['captcha']) || $resp->is_valid) {
387 $sql = ("INSERT INTO comments (date_posted, author,
389 VALUES(NOW(), ?, ?, ?)");
390 $stmt = $this->db->prepare($sql);
391 // Checks are needed here (no blank text,
392 // and a default author needs to be set
393 // for no-javascript users.
394 $stmt->bind_param('sss',
395 htmlspecialchars($_POST['name']),
396 htmlspecialchars($_POST['text']),
402 private function display_note() {
405 <h2><span style='color:grey;'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h2>
411 private function write_navigation() {
412 echo <<<END_OF_NAVIGATION
414 <div id='navigation'>
417 if (!$this->comments_enabled) {
418 $this->display_comment_link();
420 echo <<<END_OF_NAVIGATION
421 <a href="/notes/">back to notes</a>/
427 private function display_comment_link() {
428 if ($this->number_of_comments > 0) {
429 $anchor_text = "comments($this->number_of_comments)/";
431 $anchor_text = "comment?";
433 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
434 $url = $this->url . 'comments/';
436 $url = $this->url . '/comments/';
438 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
441 private function display_comments() {
442 echo "<div id='comments'>";
443 $sql= "SELECT date_posted, author, text
444 FROM comments WHERE note = ?
445 ORDER BY date_posted DESC";
446 $result = $this->query($sql, 'd', $this->id);
447 foreach ($result as $row => $entry) {
448 $date_posted = $entry['date_posted'];
449 $author = $entry['author'];
450 $text = htmlspecialchars($entry['text']);
451 $head = "<h3>$author</h3>";
452 echo <<<END_OF_COMMENT
453 <div class='comment'>
462 private function display_comment_form() {
463 $publickey = $this->recaptcha_publickey;
464 echo <<<END_CAPTCHA_STYLE
465 <script type="text/javascript">
466 Recaptcha.create("$publickey",
470 custom_theme_widget: 'recaptcha_widget',
471 callback: Recaptcha.focus_response_field
475 require_once('includes/recaptchalib.php');
476 $url = $this->url . "verify";
477 echo "<form id='comment_form' method='post' action='$url'>";
481 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
483 <input type=text name="name" id="comment_name">
486 <div id="recaptcha_widget">
487 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
488 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
489 <span style="font-size:80%;">
490 ( <a href="javascript:Recaptcha.reload()">another</a> /
491 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
492 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
495 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
497 <div style="float:right;position:relative;width:100px;">
498 <div id="recaptcha_image"></div>
503 echo recaptcha_get_html($this->recaptcha_publickey);
504 if ($this->failed_captcha) {
505 echo <<<END_OF_ERRORS
507 reCAPTCHA said you're not human, <br>
510 <input id="submit" class="submit" type="submit" value="post comment">
515 echo <<<END_OF_ERRORS
517 reCAPTCHA said you're not human, <br>
520 <div id="blank_comment">
521 but you didn't write anything! <br>
526 <input id="submit" class="submit" type="submit" value="post comment">
534 class archive extends cms {
536 public function __construct() {
537 parent::__construct();
540 private function check_exists() {
541 $sql = "SELECT COUNT(*) FROM notes
543 $results = $this->query($sql, "s", $_GET['note']);
544 if ($results[0]["COUNT(*)"] != 1) {
549 public function display() {
550 $this->display_head();
552 case (isset($_GET['year']) && !isset($_GET['month'])
553 && !isset($_GET['day'])):
554 $sql = "SELECT title, url, date_posted, text
555 FROM notes WHERE YEAR(date_posted) = ?
556 ORDER BY date_posted DESC";
557 $result = $this->query($sql, "d",
560 case (isset($_GET['year']) && isset($_GET['month'])
561 && !isset($_GET['day'])):
562 $sql = "SELECT title, url, date_posted, text
563 FROM notes WHERE YEAR(date_posted) = ?
564 AND MONTH(date_posted) = ?
565 ORDER BY date_posted DESC";
566 $result = $this->query($sql, "dd",
567 $_GET['year'], $_GET['month']);
569 case (isset($_GET['year']) && isset($_GET['month'])
570 && isset($_GET['day'])):
571 $sql = "SELECT title, url, date_posted, text
572 FROM notes WHERE YEAR(date_posted) = ?
573 AND MONTH(date_posted) = ?
574 AND DAY(date_posted) = ?
575 ORDER BY date_posted DESC";
576 $result = $this->query($sql, "ddd",
577 $_GET['year'], $_GET['month'],
581 if (count($result) >= 1) {
582 echo "<div id='notes'>";
583 foreach ($result as $row => $entry) {
584 $title = $entry['title'];
585 $url = '/note/' . $entry['url'];
586 $date_posted = explode("-", $entry['date_posted']);
587 $year_posted = $date_posted[0];
588 $month_posted = $date_posted[1];
589 $datetime_posted = explode(' ', $date_posted[2]);
590 $day_posted = $datetime_posted[0];
591 echo "<div class='note'>";
592 echo "<h2><span style='color:grey;'>";
593 echo "$year_posted/$month_posted/$day_posted/";
594 echo "</span><a href='$url'>$title</a></h2>";
599 $this->write_navigation();
602 echo "<h2 style='font-family:sans-serif;'>sorry, nothing here</h2>";
603 echo "<pre>Empty set (0.00 sec)</pre>";
605 $this->display_close();
608 private function write_navigation() {
610 echo "<div id='navigation'>";
620 class notFound extends Exception {
622 public function __construct() {
623 header('HTTP/1.0 404 Not Found');
632 class captcha extends cms {
634 public function display() {
635 $challenge = $_GET['challenge'];
636 $response = $_GET['response'];
637 $remoteip = $_SERVER['REMOTE_ADDR'];
638 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
639 curl_setopt ($curl, CURLOPT_POST, 4);
640 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
641 $result = curl_exec ($curl);
648 ## now actually do something:
649 switch (cms::determine_type()) {
651 $index = new index();
655 $project = new project();
667 $archive = new archive;
671 $captcha = new captcha;