4 private $config_file = '/etc/dylanstestserver.ini';
6 protected $recaptcha_publickey;
7 protected $recaptcha_privatekey;
9 public function __construct() {
10 $config = parse_ini_file($this->config_file
, true);
11 $this->db
= new mysqli(
12 $config[database
]['domain'],
13 $config[database
]['user'],
14 $config[database
]['password'],
15 $config[database
]['database']);
16 if (mysqli_connect_errno()) {
17 echo "Problem connecting to database: ";
18 echo mysqli_connect_error();
21 $this->recaptcha_publickey
= $config[recaptcha
]['publickey'];
22 $this->recaptcha_privatekey
= $config[recaptcha
]['privatekey'];
26 public static function determine_type() {
27 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
29 } else if (isset($_GET['year'])) {
31 } else if (isset($_GET['note'])) {
33 } else if ($_SERVER['REQUEST_URI'] == '/') {
35 } else if (isset($_GET['project'])) {
40 public function query() {
41 $args = func_get_args();
42 $statement = $this->db
->prepare($args[0]);
43 $args = array_slice($args, 1);
44 call_user_func_array(array($statement, 'bind_param'), &$args);
45 $statement->execute();
47 $statement->store_result();
49 $data = $statement->result_metadata();
51 $fields[0] = &$statement;
52 while($field = $data->fetch_field()) {
53 $fields[] = &$row[$field->name
];
55 call_user_func_array("mysqli_stmt_bind_result", $fields);
57 while ($statement->fetch()) {
58 foreach ($row as $key=>$value) $return[$i][$key] = $value;
61 $statement->free_result();
65 public function display_head($title = "dylanstestserver",
68 $stylesheets = "<link href=\"/includes/style.css\" rel=\"stylesheet\" type=\"text/css\">";
69 if ($this->determine_type() == "index") {
70 $scripts = "<script type=\"text/javascript\" src=\"/includes/all.js\">";
71 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylanstestserver.com&ucn_task=conformance#";
72 } else if ($this->determine_type() == 'note') {
73 $scripts = "<script type=\"text/javascript\" src=\"http://www.google.com/recaptcha/api/js/recaptcha_ajax.js\"></script>";
76 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
77 "http://www.w3.org/TR/html4/loose.dtd">
81 <meta name="generator" content=
82 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
83 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
86 <link rel="icon" href="favicon.ico" type="image/png">
96 <img src="/images/dylanstestserver.png" alt="dylanstestserver"
104 public function display_contact() {
105 echo <<<END_OF_CONTACT
106 <div id="contact_me"><h1><a href=
107 "mailto:dylan@psu.edu">dylan</a></h1><a href=
108 "mailto:dylan@psu.edu">@psu.edu</a>
113 public function display_close($show_contact = true) {
115 $this->display_contact();
130 class blank_page extends cms {
134 class index extends cms {
135 public function display() {
136 $this->display_head();
137 $this->display_exhibits();
138 echo "<ul id=\"portfolio\" style=\"text-align:right\">";
139 $this->list_projects();
140 echo <<<OTHER_PROJECTS
142 <h3>things i've done for others:</h3>
146 "http://activehamptons.com">activehamptons.com</a></li>
149 "http://transfishing.com">transfishing.com</a></li>
152 <h3>something i've worked on:</h3>
156 "http://tempositions.com">tempositions.com</a></li>
159 <h3>my repositories:</h3>
163 "git">git://dylanstestserver.com</a></li>
170 "/notes/">here</a></li>
175 // Because of the CSS necessary for the animations,
176 // the contact link needs to be in #portfolio to clear
178 $this->display_contact();
180 $this->display_close($show_contact = false);
183 protected function display_exhibits() {
184 echo "<div id=\"exhibit\">";
185 $sql = "SELECT text FROM projects";
186 $result = $this->db->query($sql);
187 while ($entry = $result->fetch_object()) {
193 private function list_projects() {
194 echo "<div id=\"exhibit\">";
197 <h3>my projects:</h3>
200 $sql = "SELECT title FROM projects";
201 $result = $this->db->query($sql);
202 while ($entry = $result->fetch_object()) {
203 echo "<li><a class=\"tab\" href=\"$entry->title\">$entry->title</a></li>";
208 class project extends index {
209 protected function display_exhibits() {
210 echo "<div id=\"exhibit\">";
211 $sql = "SELECT text FROM projects
213 $result = $this->query($sql, "s", $_GET['project']);
214 if ($result = $result[0]['text']) {
215 $text = str_replace("class=\"exhibit\"", "class=\"exhibit\" style=\"display:block;\"", $result);
219 throw new notFound();
224 class page extends cms {
227 private $notes_per_page = 4;
228 private $number_of_pages = 1;
230 public function __construct() {
231 parent::__construct();
232 $this->page_offset();
235 private function page_offset() {
236 $sql = "SELECT COUNT(*) FROM notes";
237 $result = $this->db->query($sql);
238 $result = $result->fetch_array();
239 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
240 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
241 $this->page = (int) $_GET['page'];
243 throw new notFound();
245 if ($this->page > $this->number_of_pages) {
246 throw new notFound();
248 if ($this->page < 1) {
249 throw new notFound();
251 $this->offset = ($this->page - 1) * $this->notes_per_page;
254 public function display() {
255 $this->display_head();
256 echo "<div id=\"notes\">";
257 $sql = "SELECT date_posted, title, url, text
258 FROM notes ORDER BY date_posted DESC
260 $result = $this->query($sql, "ii",
262 $this->notes_per_page);
263 foreach ($result as $row => $entry) {
264 $title = $entry['title'];
265 $url = '/note/' . $entry['url'];
266 $date_posted = explode("-", $entry['date_posted']);
267 $year_posted = $date_posted[0];
268 $month_posted = $date_posted[1];
269 $datetime_posted = explode(' ', $date_posted[2]);
270 $day_posted = $datetime_posted[0];
271 echo "<div class=\"note\">";
272 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
277 $this->write_navigation();
278 $this->display_close();
281 private function write_navigation() {
282 echo "<div id=\"navigation\">";
285 $previous_page = $this->page - 1;
286 echo "<a href=\"/notes/page/$previous_page\">prev</a>";
288 if($this->page < $this->number_of_pages) {
289 $forward_page = $this->page + 1;
290 echo " <a href=\"/notes/page/$forward_page\">next</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($comments_enabled = false) {
312 parent::__construct();
313 $this->comments_enabled = $comments_enabled;
314 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
315 if (isset($_GET['verify'])) {
316 $url = substr($url, 0, (strlen($url)-6));
319 $sql = "SELECT title, date_posted, text, id
320 FROM notes WHERE url = ?";
321 $result = $this->query($sql, "s",
325 $this->id = $entry["id"];
326 $this->title = $entry["title"];
327 $date_posted = explode("-", $entry["date_posted"]);
328 $this->year_posted = $date_posted[0];
329 $this->month_posted = $date_posted[1];
330 $datetime_posted = explode(' ', $date_posted[2]);
331 $this->day_posted = $datetime_posted[0];
332 $this->text = $entry["text"];
334 throw new notFound();
336 $sql = "SELECT COUNT(*) FROM comments
337 WHERE note = $this->id";
338 $result = $this->db->query($sql);
339 $result = $result->fetch_array();
340 $this->number_of_comments = $result[0];
341 if (isset($_GET['verify'])) {
346 public function display() {
347 $this->display_head();
348 $this->display_note();
349 if ($this->comments_enabled) {
350 $this->display_comments();
351 $this->display_comment_form();
353 $this->write_navigation();
354 $this->display_close();
357 private function verify() {
358 require_once('includes/recaptchalib.php');
360 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
361 $_SERVER["REMOTE_ADDR"],
362 $_POST["recaptcha_challenge_field"],
363 $_POST["recaptcha_response_field"]);
364 if (!$resp->is_valid) {
365 $this->failed_captcha = true;
367 $sql = ("INSERT INTO comments (date_posted, author,
369 VALUES(NOW(), ?, ?, ?, ?)");
370 $stmt = $this->db->prepare($sql);
371 // Checks are needed here (no blank text,
372 // and a default author / email need to be set
373 $stmt->bind_param('ssss',
374 htmlspecialchars($_POST['author']),
375 htmlspecialchars($_POST['email']),
376 htmlspecialchars($_POST['text']),
382 private function display_note() {
383 echo "<div id=\"note\">";
384 echo "<h2><span style=\"color:grey;\">$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h2>";
388 private function write_navigation() {
389 echo <<<END_OF_NAVIGATION
391 <div id=\"navigation\">
394 if ($this->failed_captcha) {
395 echo "<span style=\"color:red;border:1px solid black;padding:15px;\">sorry, reCAPTCHA said you're not human.</span><br><br><br>";
397 if (!$this->comments_enabled) {
398 $this->display_comment_link();
400 echo <<<END_OF_NAVIGATION
401 <a href="/notes/">notes</a>/
407 private function display_comment_link() {
408 if ($this->number_of_comments > 0) {
409 $anchor_text = "comments ($this->number_of_comments)";
411 $anchor_text = "comment?";
413 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
414 $url = $this->url . 'comments/';
416 $url = $this->url . '/comments/';
418 echo "<a id=\"comment_link\" href=\"$url\">$anchor_text</a>";
421 private function display_comments() {
422 echo "<div id=\"comments\">";
423 $sql= "SELECT date_posted, author, email, text
424 FROM comments WHERE note = ?
425 ORDER BY date_posted DESC";
426 $result = $this->query($sql, "d", $this->id);
427 foreach ($result as $row => $entry) {
428 $date_posted = $entry['date_posted'];
429 $author = $entry['author'];
430 $email = $entry['email'];
431 $text = htmlspecialchars($entry['text']);
432 echo <<<END_OF_COMMENT
433 <h3><a href="mailto:$email">$author</a></h3>
442 private function display_comment_form() {
443 $publickey = $this->recaptcha_publickey;
444 echo <<<END_CAPTCHA_STYLE
445 <script type="text/javascript">
446 function showRecaptcha(element) {
447 Recaptcha.create("$publickey",
451 custom_theme_widget: 'recaptcha_widget',
452 callback: Recaptcha.focus_response_field
457 require_once('includes/recaptchalib.php');
458 // Trailing slash is necessary for reloads to work
459 $url = $this->url . "verify";
460 echo "<form method=\"post\" action=\"$url\">";
463 <input type="button" value="comment" onclick="showRecaptcha('recaptcha_div');"></input>
465 <div id="recaptcha_div">
468 <textarea rows="10" cols="70" name=text></textarea>
470 <input type=text name=author>
472 <input type=text name=email><br>
475 <div id="recaptcha_widget">
476 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
477 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3><span style="font-size:80%;">(<a href="javascript:Recaptcha.reload()">another</a>/<span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span>/<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>)</span><br><br>
478 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
480 <div style="float:right;position:relative;width:100px;"><div id="recaptcha_image"></div></div>
485 <input class="submit" type="submit" value="comment">
493 class archive extends cms {
495 public function __construct() {
496 parent::__construct();
499 private function check_exists() {
500 $sql = "SELECT COUNT(*) FROM notes
502 $results = $this->query($sql, "s", $_GET['note']);
503 if ($results[0]["COUNT(*)"] != 1) {
508 public function display() {
509 // this really needs its own pagination...
510 // there should be a class for that.
511 $this->display_head();
513 case (isset($_GET['year']) && !isset($_GET['month'])
514 && !isset($_GET['day'])):
515 $sql = "SELECT title, url, date_posted, text
516 FROM notes WHERE YEAR(date_posted) = ?
517 ORDER BY date_posted DESC";
518 $result = $this->query($sql, "d",
521 case (isset($_GET['year']) && isset($_GET['month'])
522 && !isset($_GET['day'])):
523 $sql = "SELECT title, url, date_posted, text
524 FROM notes WHERE YEAR(date_posted) = ?
525 AND MONTH(date_posted) = ?
526 ORDER BY date_posted DESC";
527 $result = $this->query($sql, "dd",
528 $_GET['year'], $_GET['month']);
530 case (isset($_GET['year']) && isset($_GET['month'])
531 && isset($_GET['day'])):
532 $sql = "SELECT title, url, date_posted, text
533 FROM notes WHERE YEAR(date_posted) = ?
534 AND MONTH(date_posted) = ?
535 AND DAY(date_posted) = ?
536 ORDER BY date_posted DESC";
537 $result = $this->query($sql, "ddd",
538 $_GET['year'], $_GET['month'],
542 if (count($result) >= 1) {
543 echo "<div id=\"notes\">";
544 foreach ($result as $row => $entry) {
545 $title = $entry['title'];
546 $url = '/note/' . $entry['url'];
547 $date_posted = explode("-", $entry['date_posted']);
548 $year_posted = $date_posted[0];
549 $month_posted = $date_posted[1];
550 $datetime_posted = explode(' ', $date_posted[2]);
551 $day_posted = $datetime_posted[0];
552 echo "<div class=\"note\">";
553 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
558 $this->write_navigation();
561 echo "<h2 style=\"font-family:sans-serif;\">sorry, nothing here</h2>";
562 echo "<pre>Empty set (0.00 sec)</pre>";
564 $this->display_close();
567 private function write_navigation() {
569 echo "<div id=\"navigation\">";
578 class notFound extends Exception {
579 public function __construct() {
580 header("HTTP/1.0 404 Not Found");
587 ## now actually do something:
588 switch (cms::determine_type()) {
590 $index = new index();
594 $project = new project();
598 if (isset($_GET['comments'])) {
599 $note = new note($comments_enabled = true);
610 $archive = new archive;