de2747353f677915f413a8b955f2dedba2039e93
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 $text = $entry['text'];
283 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
290 $this->write_navigation();
291 $this->display_close();
294 private function write_navigation() {
295 echo "<div id='navigation'>";
298 $previous_page = $this->page - 1;
299 echo "<a href='/notes/page/$previous_page'>prev</a>";
301 if($this->page < $this->number_of_pages) {
302 $forward_page = $this->page + 1;
303 echo " <a href='/notes/page/$forward_page'>next</a>";
312 class note extends cms {
315 private $comments_enabled = false;
316 private $failed_captcha;
320 public $month_posted;
323 public $number_of_comments;
325 public function __construct() {
326 if (isset($_GET['comments'])) {
328 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
329 <script type='text/javascript' src='/includes/comment.js'></script>";
331 parent::__construct();
332 if (isset($_GET['comments'])) {
333 $this->comments_enabled = true;
335 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
336 if (isset($_GET['verify'])) {
337 $url = substr($url, 0, (strlen($url)-6));
340 $sql = "SELECT title, date_posted, text, id
341 FROM notes WHERE url = ?";
342 $result = $this->query($sql, "s",
346 $this->id = $entry["id"];
347 $this->title = $entry["title"];
348 $date_posted = explode("-", $entry["date_posted"]);
349 $this->year_posted = $date_posted[0];
350 $this->month_posted = $date_posted[1];
351 $datetime_posted = explode(' ', $date_posted[2]);
352 $this->day_posted = $datetime_posted[0];
353 $this->text = $entry["text"];
355 throw new notFound();
357 $sql = "SELECT COUNT(*) FROM comments
358 WHERE note = $this->id";
359 $result = $this->db->query($sql);
360 $result = $result->fetch_array();
361 $this->number_of_comments = $result[0];
362 if (isset($_GET['verify'])) {
367 public function display() {
368 $this->display_head();
369 $this->display_note();
370 if ($this->comments_enabled) {
371 $this->display_comments();
372 $this->display_comment_form();
374 $this->write_navigation();
375 $this->display_close();
378 private function verify() {
379 if (!isset($_POST['captcha'])) {
380 require_once('includes/recaptchalib.php');
382 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
383 $_SERVER["REMOTE_ADDR"],
384 $_POST["recaptcha_challenge_field"],
385 $_POST["recaptcha_response_field"]);
386 if (!$resp->is_valid) {
387 $this->failed_captcha = true;
390 if (isset($_POST['captcha']) || $resp->is_valid) {
391 $sql = ("INSERT INTO comments (date_posted, author,
393 VALUES(NOW(), ?, ?, ?)");
394 $stmt = $this->db->prepare($sql);
395 // Checks are needed here (no blank text,
396 // and a default author needs to be set
397 // for no-javascript users.
398 $stmt->bind_param('sss',
399 htmlspecialchars($_POST['name']),
400 htmlspecialchars($_POST['text']),
406 private function display_note() {
409 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
415 private function write_navigation() {
416 echo <<<END_OF_NAVIGATION
418 <div id='navigation'>
421 if (!$this->comments_enabled) {
422 $this->display_comment_link();
424 echo <<<END_OF_NAVIGATION
425 <a href="/notes/">back to notes</a>/
431 private function display_comment_link() {
432 if ($this->number_of_comments > 0) {
433 $anchor_text = "comments($this->number_of_comments)/";
435 $anchor_text = "comment?";
437 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
438 $url = $this->url . 'comments/';
440 $url = $this->url . '/comments/';
442 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
445 private function display_comments() {
446 echo "<div id='comments'>";
447 $sql= "SELECT date_posted, author, text
448 FROM comments WHERE note = ?
449 ORDER BY date_posted DESC";
450 $result = $this->query($sql, 'd', $this->id);
451 foreach ($result as $row => $entry) {
452 $date_posted = $entry['date_posted'];
453 $author = $entry['author'];
454 $text = htmlspecialchars($entry['text']);
455 $head = "<h3>$author</h3>";
456 echo <<<END_OF_COMMENT
457 <div class='comment'>
466 private function display_comment_form() {
467 $publickey = $this->recaptcha_publickey;
468 echo <<<END_CAPTCHA_STYLE
469 <script type="text/javascript">
470 Recaptcha.create("$publickey",
474 custom_theme_widget: 'recaptcha_widget',
475 callback: Recaptcha.focus_response_field
479 require_once('includes/recaptchalib.php');
480 $url = $this->url . "verify";
481 echo "<form id='comment_form' method='post' action='$url'>";
485 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
487 <input type=text name="name" id="comment_name">
490 <div id="recaptcha_widget">
491 <h3 class="recaptcha_only_if_image"><b>what's this say</b>?</h3>
492 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
493 <span style="font-size:80%;">
494 ( <a href="javascript:Recaptcha.reload()">another</a> /
495 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio</a></span> /
496 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image</a></span><a href="javascript:Recaptcha.showhelp()">help</a> )
499 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
501 <div style="float:right;position:relative;width:100px;">
502 <div id="recaptcha_image"></div>
507 echo recaptcha_get_html($this->recaptcha_publickey);
508 if ($this->failed_captcha) {
509 echo <<<END_OF_ERRORS
511 reCAPTCHA said you're not human, <br>
514 <input id="submit" class="submit" type="submit" value="post comment">
519 echo <<<END_OF_ERRORS
521 reCAPTCHA said you're not human, <br>
524 <div id="blank_comment">
525 but you didn't write anything! <br>
530 <input id="submit" class="submit" type="submit" value="post comment">
538 class archive extends cms {
540 public function __construct() {
541 parent::__construct();
544 private function check_exists() {
545 $sql = "SELECT COUNT(*) FROM notes
547 $results = $this->query($sql, "s", $_GET['note']);
548 if ($results[0]["COUNT(*)"] != 1) {
553 public function display() {
554 $this->display_head();
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 ORDER BY date_posted DESC";
561 $result = $this->query($sql, "d",
564 case (isset($_GET['year']) && isset($_GET['month'])
565 && !isset($_GET['day'])):
566 $sql = "SELECT title, url, date_posted, text
567 FROM notes WHERE YEAR(date_posted) = ?
568 AND MONTH(date_posted) = ?
569 ORDER BY date_posted DESC";
570 $result = $this->query($sql, "dd",
571 $_GET['year'], $_GET['month']);
573 case (isset($_GET['year']) && isset($_GET['month'])
574 && isset($_GET['day'])):
575 $sql = "SELECT title, url, date_posted, text
576 FROM notes WHERE YEAR(date_posted) = ?
577 AND MONTH(date_posted) = ?
578 AND DAY(date_posted) = ?
579 ORDER BY date_posted DESC";
580 $result = $this->query($sql, "ddd",
581 $_GET['year'], $_GET['month'],
585 if (count($result) >= 1) {
586 echo "<div id='notes'>";
587 foreach ($result as $row => $entry) {
588 $title = $entry['title'];
589 $url = '/note/' . $entry['url'];
590 $date_posted = explode("-", $entry['date_posted']);
591 $year_posted = $date_posted[0];
592 $month_posted = $date_posted[1];
593 $datetime_posted = explode(' ', $date_posted[2]);
594 $day_posted = $datetime_posted[0];
595 echo "<div class='note'>";
596 echo "<h1><span class='date'>";
597 echo "$year_posted/$month_posted/$day_posted/";
598 echo "</span><a href='$url'>$title</a></h1>";
603 $this->write_navigation();
606 echo "<h1>sorry, nothing here</h2>";
607 echo "<pre>Empty set (0.00 sec)</pre>";
609 $this->display_close();
612 private function write_navigation() {
614 echo "<div id='navigation'>";
622 class notFound extends Exception {
624 public function __construct() {
625 header('HTTP/1.0 404 Not Found');
634 class captcha extends cms {
636 public function display() {
637 $challenge = $_GET['challenge'];
638 $response = $_GET['response'];
639 $remoteip = $_SERVER['REMOTE_ADDR'];
640 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
641 curl_setopt ($curl, CURLOPT_POST, 4);
642 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
643 $result = curl_exec ($curl);
650 ## now actually do something:
651 switch (cms::determine_type()) {
653 $index = new index();
657 $project = new project();
669 $archive = new archive;
673 $captcha = new captcha;