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 $sql = "SELECT text FROM projects";
194 $result = $this->db->query($sql);
195 while ($entry = $result->fetch_object()) {
196 echo "<div class='exhibit'>";
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 / email need to be set
393 // for no-javascript users.
394 $stmt->bind_param('ssss',
395 htmlspecialchars($_POST['name']),
396 htmlspecialchars($_POST['email']),
397 htmlspecialchars($_POST['text']),
403 private function display_note() {
406 <h2><span style='color:grey;'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h2>
412 private function write_navigation() {
413 echo <<<END_OF_NAVIGATION
415 <div id='navigation'>
418 if (!$this->comments_enabled) {
419 $this->display_comment_link();
421 echo <<<END_OF_NAVIGATION
422 <a href="/notes/">back to notes</a>/
428 private function display_comment_link() {
429 if ($this->number_of_comments > 0) {
430 $anchor_text = "comments($this->number_of_comments)/";
432 $anchor_text = "comment?";
434 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
435 $url = $this->url . 'comments/';
437 $url = $this->url . '/comments/';
439 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
442 private function display_comments() {
443 echo "<div id='comments'>";
444 $sql= "SELECT date_posted, author, email, text
445 FROM comments WHERE note = ?
446 ORDER BY date_posted DESC";
447 $result = $this->query($sql, 'd', $this->id);
448 foreach ($result as $row => $entry) {
449 $date_posted = $entry['date_posted'];
450 $author = $entry['author'];
451 $email = $entry['email'];
452 $text = htmlspecialchars($entry['text']);
454 $head = "<h3>$author</h3>";
456 $head = "<h3><a href='mailto:$email'>$author</a></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">
491 <input type=text name="email" id="comment_email"><br>
494 <div id="recaptcha_widget">
495 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
496 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
497 <span style="font-size:80%;">
498 ( <a href="javascript:Recaptcha.reload()">another</a> /
499 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
500 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
503 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
505 <div style="float:right;position:relative;width:100px;">
506 <div id="recaptcha_image"></div>
511 echo recaptcha_get_html($this->recaptcha_publickey);
512 if ($this->failed_captcha) {
514 <span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>reCAPTCHA said you're not human,</span>
515 <input id="submit" class="submit" type="submit" value="try again?">
521 <input id="submit" class="submit" type="submit" value="post comment">
530 class archive extends cms {
532 public function __construct() {
533 parent::__construct();
536 private function check_exists() {
537 $sql = "SELECT COUNT(*) FROM notes
539 $results = $this->query($sql, "s", $_GET['note']);
540 if ($results[0]["COUNT(*)"] != 1) {
545 public function display() {
546 $this->display_head();
548 case (isset($_GET['year']) && !isset($_GET['month'])
549 && !isset($_GET['day'])):
550 $sql = "SELECT title, url, date_posted, text
551 FROM notes WHERE YEAR(date_posted) = ?
552 ORDER BY date_posted DESC";
553 $result = $this->query($sql, "d",
556 case (isset($_GET['year']) && isset($_GET['month'])
557 && !isset($_GET['day'])):
558 $sql = "SELECT title, url, date_posted, text
559 FROM notes WHERE YEAR(date_posted) = ?
560 AND MONTH(date_posted) = ?
561 ORDER BY date_posted DESC";
562 $result = $this->query($sql, "dd",
563 $_GET['year'], $_GET['month']);
565 case (isset($_GET['year']) && isset($_GET['month'])
566 && isset($_GET['day'])):
567 $sql = "SELECT title, url, date_posted, text
568 FROM notes WHERE YEAR(date_posted) = ?
569 AND MONTH(date_posted) = ?
570 AND DAY(date_posted) = ?
571 ORDER BY date_posted DESC";
572 $result = $this->query($sql, "ddd",
573 $_GET['year'], $_GET['month'],
577 if (count($result) >= 1) {
578 echo "<div id='notes'>";
579 foreach ($result as $row => $entry) {
580 $title = $entry['title'];
581 $url = '/note/' . $entry['url'];
582 $date_posted = explode("-", $entry['date_posted']);
583 $year_posted = $date_posted[0];
584 $month_posted = $date_posted[1];
585 $datetime_posted = explode(' ', $date_posted[2]);
586 $day_posted = $datetime_posted[0];
587 echo "<div class='note'>";
588 echo "<h2><span style='color:grey;'>";
589 echo "$year_posted/$month_posted/$day_posted/";
590 echo "</span><a href='$url'>$title</a></h2>";
595 $this->write_navigation();
598 echo "<h2 style='font-family:sans-serif;'>sorry, nothing here</h2>";
599 echo "<pre>Empty set (0.00 sec)</pre>";
601 $this->display_close();
604 private function write_navigation() {
606 echo "<div id='navigation'>";
616 class notFound extends Exception {
618 public function __construct() {
619 header('HTTP/1.0 404 Not Found');
628 class captcha extends cms {
630 public function display() {
631 $challenge = $_GET['challenge'];
632 $response = $_GET['response'];
633 $remoteip = $_SERVER['REMOTE_ADDR'];
634 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
635 curl_setopt ($curl, CURLOPT_POST, 4);
636 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
637 $result = curl_exec ($curl);
644 ## now actually do something:
645 switch (cms::determine_type()) {
647 $index = new index();
651 $project = new project();
663 $archive = new archive;
667 $captcha = new captcha;