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
148 <h3>things i've done for others:</h3>
152 "http://activehamptons.com">activehamptons.com</a></li>
155 "http://transfishing.com">transfishing.com</a></li>
158 <h3>something i've worked on:</h3>
162 "http://tempositions.com">tempositions.com</a></li>
165 <h3>my repositories:</h3>
169 "git">git://dylanstestserver.com</a></li>
176 "/notes/">here</a></li>
181 // Because of the CSS necessary for the animations,
182 // the contact link needs to be in #portfolio to clear
185 $this->display_contact();
188 $this->display_close($show_contact = false);
191 protected function display_exhibits() {
192 echo "<div id='exhibit'>";
193 $sql = "SELECT text FROM projects";
194 $result = $this->db->query($sql);
195 while ($entry = $result->fetch_object()) {
201 private function list_projects() {
204 <h3>my projects:</h3>
207 $sql = "SELECT title FROM projects";
208 $result = $this->db->query($sql);
209 while ($entry = $result->fetch_object()) {
210 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
217 class project extends index {
219 protected function display_exhibits() {
220 echo "<div id='exhibit'>";
221 $sql = "SELECT text FROM projects
223 $result = $this->query($sql, "s", $_GET['project']);
224 if ($result = $result[0]['text']) {
225 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
229 throw new notFound();
236 class page extends cms {
240 private $notes_per_page = 4;
241 private $number_of_pages = 1;
243 public function __construct() {
244 parent::__construct();
245 $this->page_offset();
248 private function page_offset() {
249 $sql = "SELECT COUNT(*) FROM notes";
250 $result = $this->db->query($sql);
251 $result = $result->fetch_array();
252 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
253 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
254 $this->page = (int) $_GET['page'];
256 throw new notFound();
258 if ($this->page > $this->number_of_pages) {
259 throw new notFound();
261 if ($this->page < 1) {
262 throw new notFound();
264 $this->offset = ($this->page - 1) * $this->notes_per_page;
267 public function display() {
268 $this->display_head();
269 echo "<div id='notes'>";
270 $sql = "SELECT date_posted, title, url, text
271 FROM notes ORDER BY date_posted DESC
273 $result = $this->query($sql, "ii",
275 $this->notes_per_page);
276 foreach ($result as $row => $entry) {
277 $title = $entry['title'];
278 $url = '/note/' . $entry['url'];
279 $date_posted = explode("-", $entry['date_posted']);
280 $year_posted = $date_posted[0];
281 $month_posted = $date_posted[1];
282 $datetime_posted = explode(' ', $date_posted[2]);
283 $day_posted = $datetime_posted[0];
284 $text = $entry['text'];
289 $year_posted/$month_posted/$day_posted/
290 </span><a rel="canonical" href='$url'>$title</a>
297 $this->write_navigation();
298 $this->display_close();
301 private function write_navigation() {
302 echo "<div id='navigation'>";
305 $previous_page = $this->page - 1;
306 echo "<a href='/notes/page/$previous_page'>prev</a>";
308 if($this->page < $this->number_of_pages) {
309 $forward_page = $this->page + 1;
310 echo " <a href='/notes/page/$forward_page'>next</a>";
319 class note extends cms {
322 private $comments_enabled = false;
323 private $failed_captcha;
327 public $month_posted;
330 public $number_of_comments;
332 public function __construct() {
333 parent::__construct();
334 if (isset($_GET['comments'])) {
335 $this->comments_enabled = true;
337 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
338 if (isset($_GET['verify'])) {
339 $url = substr($url, 0, (strlen($url)-6));
342 $sql = "SELECT title, date_posted, text, id
343 FROM notes WHERE url = ?";
344 $result = $this->query($sql, "s",
348 $this->id = $entry["id"];
349 $this->title = $entry["title"];
350 $date_posted = explode("-", $entry["date_posted"]);
351 $this->year_posted = $date_posted[0];
352 $this->month_posted = $date_posted[1];
353 $datetime_posted = explode(' ', $date_posted[2]);
354 $this->day_posted = $datetime_posted[0];
355 $this->text = $entry["text"];
357 throw new notFound();
359 $sql = "SELECT COUNT(*) FROM comments
360 WHERE note = $this->id";
361 $result = $this->db->query($sql);
362 $result = $result->fetch_array();
363 $this->number_of_comments = $result[0];
364 if (isset($_GET['verify'])) {
369 public function display() {
370 $this->display_head();
371 $this->display_note();
372 if ($this->comments_enabled) {
373 $this->display_comments();
374 $this->display_comment_form();
376 $this->write_navigation();
377 $this->display_close();
380 private function verify() {
381 if (!isset($_POST['captcha'])) {
382 require_once('includes/recaptchalib.php');
384 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
385 $_SERVER["REMOTE_ADDR"],
386 $_POST["recaptcha_challenge_field"],
387 $_POST["recaptcha_response_field"]);
388 if (!$resp->is_valid) {
389 $this->failed_captcha = true;
392 if (isset($_POST['captcha']) || $resp->is_valid) {
393 $sql = ("INSERT INTO comments (date_posted, author,
395 VALUES(NOW(), ?, ?, ?)");
396 $stmt = $this->db->prepare($sql);
397 // Checks are needed here (no blank text,
398 // and a default author needs to be set
399 // for no-javascript users.
400 $stmt->bind_param('sss',
401 htmlspecialchars($_POST['name']),
402 htmlspecialchars($_POST['text']),
408 private function display_note() {
411 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
417 private function write_navigation() {
418 echo <<<END_OF_NAVIGATION
420 <div id='navigation'>
423 if (!$this->comments_enabled) {
424 $this->display_comment_link();
426 echo <<<END_OF_NAVIGATION
427 <a href="/notes/">back to notes</a>/
433 private function display_comment_link() {
434 if ($this->number_of_comments > 0) {
435 $anchor_text = "comments($this->number_of_comments)/";
437 $anchor_text = "comment?";
439 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
440 $url = $this->url . 'comments/';
442 $url = $this->url . '/comments/';
444 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
447 private function display_comments() {
448 echo "<div id='comments'>";
449 $sql= "SELECT date_posted, author, text
450 FROM comments WHERE note = ?
451 ORDER BY date_posted DESC";
452 $result = $this->query($sql, 'd', $this->id);
453 foreach ($result as $row => $entry) {
454 $date_posted = $entry['date_posted'];
455 $author = $entry['author'];
456 $text = htmlspecialchars($entry['text']);
457 $head = "<h3>$author</h3>";
458 echo <<<END_OF_COMMENT
459 <div class='comment'>
468 private function display_comment_form() {
469 $publickey = $this->recaptcha_publickey;
470 echo <<<END_CAPTCHA_STYLE
471 <script type="text/javascript">
472 Recaptcha.create("$publickey",
476 custom_theme_widget: 'recaptcha_widget',
477 callback: Recaptcha.focus_response_field
481 require_once('includes/recaptchalib.php');
482 $url = $this->url . "verify";
483 echo "<form id='comment_form' method='post' action='$url'>";
487 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
489 <input type=text name="name" id="comment_name">
492 <div id="recaptcha_widget">
493 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
494 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
495 <span style="font-size:80%;">
496 ( <a href="javascript:Recaptcha.reload()">another</a> /
497 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
498 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
501 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
503 <div style="float:right;position:relative;width:100px;">
504 <div id="recaptcha_image"></div>
509 echo recaptcha_get_html($this->recaptcha_publickey);
510 if ($this->failed_captcha) {
511 echo <<<END_OF_ERRORS
513 reCAPTCHA said you're not human, <br>
516 <input id="submit" class="submit" type="submit" value="post comment">
521 echo <<<END_OF_ERRORS
523 reCAPTCHA said you're not human, <br>
526 <div id="blank_comment">
527 but you didn't write anything! <br>
532 <input id="submit" class="submit" type="submit" value="post comment">
540 class archive extends cms {
542 public function __construct() {
543 parent::__construct();
546 private function check_exists() {
547 $sql = "SELECT COUNT(*) FROM notes
549 $results = $this->query($sql, "s", $_GET['note']);
550 if ($results[0]["COUNT(*)"] != 1) {
555 public function display() {
556 $this->display_head();
558 case (isset($_GET['year']) && !isset($_GET['month'])
559 && !isset($_GET['day'])):
560 $sql = "SELECT title, url, date_posted, text
561 FROM notes WHERE YEAR(date_posted) = ?
562 ORDER BY date_posted DESC";
563 $result = $this->query($sql, "d",
566 case (isset($_GET['year']) && isset($_GET['month'])
567 && !isset($_GET['day'])):
568 $sql = "SELECT title, url, date_posted, text
569 FROM notes WHERE YEAR(date_posted) = ?
570 AND MONTH(date_posted) = ?
571 ORDER BY date_posted DESC";
572 $result = $this->query($sql, "dd",
573 $_GET['year'], $_GET['month']);
575 case (isset($_GET['year']) && isset($_GET['month'])
576 && isset($_GET['day'])):
577 $sql = "SELECT title, url, date_posted, text
578 FROM notes WHERE YEAR(date_posted) = ?
579 AND MONTH(date_posted) = ?
580 AND DAY(date_posted) = ?
581 ORDER BY date_posted DESC";
582 $result = $this->query($sql, "ddd",
583 $_GET['year'], $_GET['month'],
587 if (count($result) >= 1) {
588 echo "<div id='notes'>";
589 foreach ($result as $row => $entry) {
590 $title = $entry['title'];
591 $url = '/note/' . $entry['url'];
592 $date_posted = explode("-", $entry['date_posted']);
593 $year_posted = $date_posted[0];
594 $month_posted = $date_posted[1];
595 $datetime_posted = explode(' ', $date_posted[2]);
596 $day_posted = $datetime_posted[0];
597 echo "<div class='note'>";
598 echo "<h1><span class='date'>";
599 echo "$year_posted/$month_posted/$day_posted/";
600 echo "</span><a href='$url'>$title</a></h1>";
605 $this->write_navigation();
608 echo "<h1>sorry, nothing here</h2>";
609 echo "<pre>Empty set (0.00 sec)</pre>";
611 $this->display_close();
614 private function write_navigation() {
616 echo "<div id='navigation'>";
624 class notFound extends Exception {
626 public function __construct() {
627 header('HTTP/1.0 404 Not Found');
636 class captcha extends cms {
638 public function display() {
639 $challenge = $_GET['challenge'];
640 $response = $_GET['response'];
641 $remoteip = $_SERVER['REMOTE_ADDR'];
642 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
643 curl_setopt ($curl, CURLOPT_POST, 4);
644 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
645 $result = curl_exec ($curl);
652 ## now actually do something:
653 switch (cms::determine_type()) {
655 $index = new index();
659 $project = new project();
671 $archive = new archive;
675 $captcha = new captcha;