Merge branch 'live' into dev
[dylansserver.git] / index.php
1 <?php
2
3 abstract class cms {
4
5 private $config_file = '/etc/dylansserver.ini';
6 protected $db;
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
9 protected $scripts;
10 public $title;
11 public $home_link;
12
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();
23 exit();
24 }
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'];
29 ob_start();
30 }
31
32 public static function determine_type() {
33 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
34 return 'page';
35 } else if (isset($_GET['year'])) {
36 return 'archive';
37 } else if (isset($_GET['note'])) {
38 return 'note';
39 } else if ($_SERVER['REQUEST_URI'] == '/') {
40 return 'index';
41 } else if (isset($_GET['project'])) {
42 return 'project';
43 } else if (isset($_GET['challenge'])) {
44 return 'captcha';
45 }
46 }
47
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();
54 $return = array();
55 $statement->store_result();
56 $row = array();
57 $data = $statement->result_metadata();
58 $fields = array();
59 $fields[0] = &$statement;
60 while($field = $data->fetch_field()) {
61 $fields[] = &$row[$field->name];
62 }
63 call_user_func_array("mysqli_stmt_bind_result", $fields);
64 $i = 0;
65 while ($statement->fetch()) {
66 foreach ($row as $key=>$value) $return[$i][$key] = $value;
67 $i++;
68 }
69 $statement->free_result();
70 return $return;
71 }
72
73 public function display_head($title = "dylansserver",
74 $home_link = "/") {
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&amp;ucn_task=conformance#";
78 echo <<<END_OF_HEAD
79 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
80 "http://www.w3.org/TR/html4/loose.dtd">
81
82 <html>
83 <head>
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">
87
88 <title>$this->title</title>
89 <link rel="icon" href="/favicon.ico" type="image/png">
90 $stylesheets
91 $scripts
92 </head>
93
94 <body>
95 <div id="structure">
96 <div id="banner">
97 <a href="$this->home_link">
98 <img src="/images/dylansserver.png" alt="dylansserver"
99 border="0"></a>
100 </div>
101
102 <div id="content">
103 END_OF_HEAD;
104 }
105
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>
111 </div>
112 END_OF_CONTACT;
113 }
114
115 public function display_close($show_contact = true) {
116 if ($show_contact) {
117 $this->display_contact();
118 }
119 echo <<<END_OF_CLOSE
120 </div>
121 <br>
122 <br>
123 </div>
124 </body>
125 </html>
126 END_OF_CLOSE;
127 ob_flush();
128 }
129
130 }
131
132
133 class index extends cms {
134
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
142 <li>
143 <h3>things i've done for others:</h3>
144 </li>
145
146 <li><a href=
147 "http://activehamptons.com">activehamptons.com</a></li>
148
149 <li><a href=
150 "http://transfishing.com">transfishing.com</a></li>
151
152 <li>
153 <h3>something i've worked on:</h3>
154 </li>
155
156 <li><a href=
157 "http://tempositions.com">tempositions.com</a></li>
158
159 <li>
160 <h3>my repositories:</h3>
161 </li>
162
163 <li><a href=
164 "git">git://dylansserver.com</a></li>
165
166 <li>
167 <h3>some notes:</h3>
168 </li>
169
170 <li><a href=
171 "/notes/">here</a></li>
172
173 <li>
174 </li>
175 OTHER_PROJECTS;
176 // Because of the CSS necessary for the animations,
177 // the contact link needs to be in #portfolio to clear
178 // the floats.
179 echo "<li>";
180 $this->display_contact();
181 echo "</li>";
182 echo "</ul>";
183 $this->display_close($show_contact = false);
184 }
185
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()) {
191 echo $entry->text;
192 }
193 echo "</div>";
194 }
195
196 private function list_projects() {
197 echo <<<HEREDOC
198 <li>
199 <h3>my projects:</h3>
200 </li>
201 HEREDOC;
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>";
206 }
207 }
208
209 }
210
211
212 class project extends index {
213
214 protected function display_exhibits() {
215 echo "<div id='exhibit'>";
216 $sql = "SELECT text FROM projects
217 WHERE title = ?";
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);
221 echo $text;
222 echo "</div>";
223 } else {
224 throw new notFound();
225 }
226 }
227
228 }
229
230
231 class page extends cms {
232
233 private $page = 1;
234 private $offset = 0;
235 private $notes_per_page = 4;
236 private $number_of_pages = 1;
237
238 public function __construct() {
239 parent::__construct();
240 $this->page_offset();
241 }
242
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'];
250 } else {
251 throw new notFound();
252 }
253 if ($this->page > $this->number_of_pages) {
254 throw new notFound();
255 }
256 if ($this->page < 1) {
257 throw new notFound();
258 }
259 $this->offset = ($this->page - 1) * $this->notes_per_page;
260 }
261
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
267 LIMIT ?, ?";
268 $result = $this->query($sql, "ii",
269 $this->offset,
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'];
280 echo <<<END_NOTE
281 <div class='note'>
282 <h1>
283 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
284 </h1>
285 $text
286 </div>
287 END_NOTE;
288 }
289 echo "</div>";
290 $this->write_navigation();
291 $this->display_close();
292 }
293
294 private function write_navigation() {
295 echo "<div id='navigation'>";
296 echo "<h1>";
297 if($this->page > 1){
298 $previous_page = $this->page - 1;
299 echo "<a href='/notes/page/$previous_page'>prev</a>";
300 }
301 if($this->page < $this->number_of_pages) {
302 $forward_page = $this->page + 1;
303 echo " <a href='/notes/page/$forward_page'>next</a>";
304 }
305 echo "</h1>";
306 echo "</div>";
307 }
308
309 }
310
311
312 class note extends cms {
313
314 private $id;
315 private $comments_enabled = false;
316 private $failed_captcha;
317 public $url;
318 public $title;
319 public $year_posted;
320 public $month_posted;
321 public $day_posted;
322 public $text;
323 public $number_of_comments;
324
325 public function __construct() {
326 if (isset($_GET['comments'])) {
327 $this->scripts = "
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>";
330 }
331 parent::__construct();
332 if (isset($_GET['comments'])) {
333 $this->comments_enabled = true;
334 }
335 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
336 if (isset($_GET['verify'])) {
337 $url = substr($url, 0, (strlen($url)-6));
338 }
339 $this->url = $url;
340 $sql = "SELECT title, date_posted, text, id
341 FROM notes WHERE url = ?";
342 $result = $this->query($sql, "s",
343 $_GET['note']);
344 if ($result) {
345 $entry = $result[0];
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"];
354 } else {
355 throw new notFound();
356 }
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'])) {
363 $this->verify();
364 }
365 }
366
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();
373 }
374 $this->write_navigation();
375 $this->display_close();
376 }
377
378 private function verify() {
379 if (!isset($_POST['captcha'])) {
380 require_once('includes/recaptchalib.php');
381 echo "<br>";
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;
388 }
389 }
390 if (isset($_POST['captcha']) || $resp->is_valid) {
391 $sql = ("INSERT INTO comments (date_posted, author,
392 text, note)
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']),
401 $this->id);
402 $stmt->execute();
403 }
404 }
405
406 private function display_note() {
407 echo <<<END_OF_NOTE
408 <div id='note'>
409 <h1><span class='date'>$this->year_posted/$this->month_posted/$this->day_posted/</span>$this->title</h1>
410 $this->text
411 </div>
412 END_OF_NOTE;
413 }
414
415 private function write_navigation() {
416 echo <<<END_OF_NAVIGATION
417 <br>
418 <div id='navigation'>
419 <h1>
420 END_OF_NAVIGATION;
421 if (!$this->comments_enabled) {
422 $this->display_comment_link();
423 }
424 echo <<<END_OF_NAVIGATION
425 <a href="/notes/">back to notes</a>/
426 </h1>
427 </div>
428 END_OF_NAVIGATION;
429 }
430
431 private function display_comment_link() {
432 if ($this->number_of_comments > 0) {
433 $anchor_text = "comments($this->number_of_comments)/";
434 } else {
435 $anchor_text = "comment?";
436 }
437 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
438 $url = $this->url . 'comments/';
439 } else {
440 $url = $this->url . '/comments/';
441 }
442 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
443 }
444
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'>
458 $head
459 $text
460 </div>
461 END_OF_COMMENT;
462 }
463 echo "</div>";
464 }
465
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",
471 "recaptcha_div",
472 {
473 theme : 'custom',
474 custom_theme_widget: 'recaptcha_widget',
475 callback: Recaptcha.focus_response_field
476 });
477 </script>
478 END_CAPTCHA_STYLE;
479 require_once('includes/recaptchalib.php');
480 $url = $this->url . "verify";
481 echo "<form id='comment_form' method='post' action='$url'>";
482 echo <<<END_OF_FORM
483 <div id="comment">
484 <h3>comment:</h3>
485 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
486 <h3>name:</h3>
487 <input type=text name="name" id="comment_name">
488
489 <nowiki>
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> )
497 </span>
498 <br><br>
499 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
500 <br><br>
501 <div style="float:right;position:relative;width:100px;">
502 <div id="recaptcha_image"></div>
503 </div>
504 <br><br><br><br>
505 </div>
506 END_OF_FORM;
507 echo recaptcha_get_html($this->recaptcha_publickey);
508 if ($this->failed_captcha) {
509 echo <<<END_OF_ERRORS
510 <div id="not_human">
511 reCAPTCHA said you're not human, <br>
512 try again?
513 </div>
514 <input id="submit" class="submit" type="submit" value="post comment">
515 </form>
516 </div>
517 END_OF_ERRORS;
518 } else {
519 echo <<<END_OF_ERRORS
520 <div id="not_human">
521 reCAPTCHA said you're not human, <br>
522 try again?
523 </div>
524 <div id="blank_comment">
525 but you didn't write anything! <br>
526 </div>
527 END_OF_ERRORS;
528 }
529 echo <<<END_OF_FORM
530 <input id="submit" class="submit" type="submit" value="post comment">
531 </form>
532 </div>
533 END_OF_FORM;
534 }
535 }
536
537
538 class archive extends cms {
539
540 public function __construct() {
541 parent::__construct();
542 }
543
544 private function check_exists() {
545 $sql = "SELECT COUNT(*) FROM notes
546 WHERE url = ?";
547 $results = $this->query($sql, "s", $_GET['note']);
548 if ($results[0]["COUNT(*)"] != 1) {
549 $this->not_found();
550 }
551 }
552
553 public function display() {
554 $this->display_head();
555 switch (true) {
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",
562 $_GET['year']);
563 break;
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']);
572 break;
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'],
582 $_GET['day']);
583 break;
584 }
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>";
599 echo $entry['text'];
600 echo "</div>";
601 }
602 echo "</div>";
603 $this->write_navigation();
604 } else {
605 echo "<br>";
606 echo "<h1>sorry, nothing here</h2>";
607 echo "<pre>Empty set (0.00 sec)</pre>";
608 }
609 $this->display_close();
610 }
611
612 private function write_navigation() {
613 echo "<br>";
614 echo "<div id='navigation'>";
615 // fill me in!
616 echo "</div>";
617 }
618
619 }
620
621
622 class notFound extends Exception {
623
624 public function __construct() {
625 header('HTTP/1.0 404 Not Found');
626 ob_end_clean();
627 include('404.php');
628 exit();
629 }
630
631 }
632
633
634 class captcha extends cms {
635
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);
644 curl_close ($curl);
645 }
646
647 }
648
649
650 ## now actually do something:
651 switch (cms::determine_type()) {
652 case 'index':
653 $index = new index();
654 $index->display();
655 break;
656 case 'project':
657 $project = new project();
658 $project->display();
659 break;
660 case 'note':
661 $note = new note;
662 $note->display();
663 break;
664 case 'page':
665 $page = new page;
666 $page->display();
667 break;
668 case 'archive':
669 $archive = new archive;
670 $archive->display();
671 break;
672 case "captcha":
673 $captcha = new captcha;
674 $captcha->display();
675 break;
676 }
677
678 ?>