00a8d7c151adc54074a4a4674d65ae39428a4349
[dylansserver.git] / cms.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['rss'])) {
44 return 'rss';
45 } else if (isset($_GET['challenge'])) {
46 return 'captcha';
47 }
48 }
49
50 public function query() {
51 $args = func_get_args();
52 $statement = $this->db->prepare($args[0]);
53 $args = array_slice($args, 1);
54 call_user_func_array(array($statement, 'bind_param'), &$args);
55 $statement->execute();
56 $return = array();
57 $statement->store_result();
58 $row = array();
59 $data = $statement->result_metadata();
60 $fields = array();
61 $fields[0] = &$statement;
62 while($field = $data->fetch_field()) {
63 $fields[] = &$row[$field->name];
64 }
65 call_user_func_array("mysqli_stmt_bind_result", $fields);
66 $i = 0;
67 while ($statement->fetch()) {
68 foreach ($row as $key=>$value) $return[$i][$key] = $value;
69 $i++;
70 }
71 $statement->free_result();
72 return $return;
73 }
74
75 public function display_head($title = "dylansserver",
76 $home_link = "/") {
77 $scripts = $this->scripts;
78 $stylesheets = "<link href='/includes/style.css' rel='stylesheet' type='text/css'>";
79 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylansserver.com&amp;ucn_task=conformance#";
80 echo <<<END_OF_HEAD
81 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
82 "http://www.w3.org/TR/html4/loose.dtd">
83
84 <html>
85 <head>
86 <meta name="generator" content=
87 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
88 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
89
90 <title>$this->title</title>
91 <link rel="icon" href="/favicon.ico" type="image/png">
92 $stylesheets
93 $scripts
94 </head>
95
96 <body onload="return typeof highlight == 'function' ? highlight() : true;">
97 <div id="structure">
98 <div id="banner">
99 <a href="$this->home_link">
100 <img src="/images/dylansserver.png" alt="dylansserver"
101 border="0"></a>
102 </div>
103
104 <div id="content">
105 END_OF_HEAD;
106 }
107
108 public function display_contact() {
109 echo <<<END_OF_CONTACT
110 <div id="contact_me"><h1><a href=
111 "mailto:dylan@psu.edu">dylan</a></h1><a href=
112 "mailto:dylan@psu.edu">@psu.edu</a>
113 </div>
114 END_OF_CONTACT;
115 }
116
117 public function display_close($show_contact = true) {
118 if ($show_contact) {
119 $this->display_contact();
120 }
121 echo <<<END_OF_CLOSE
122 </div>
123 <br>
124 <br>
125 </div>
126 </body>
127 </html>
128 END_OF_CLOSE;
129 ob_flush();
130 }
131
132 public function init() {
133 switch (cms::determine_type()) {
134 case 'index':
135 $index = new index();
136 $index->display();
137 break;
138 case 'project':
139 $project = new project();
140 $project->display();
141 break;
142 case 'note':
143 $note = new note;
144 $note->display();
145 break;
146 case 'page':
147 $page = new page;
148 $page->display();
149 break;
150 case "rss":
151 $rss = new rss();
152 $rss->display();
153 break;
154 case 'archive':
155 $archive = new archive;
156 $archive->display();
157 break;
158 case "captcha":
159 $captcha = new captcha;
160 $captcha->display();
161 break;
162 }
163 }
164
165 }
166
167
168 class index extends cms {
169
170 public function display() {
171 $this->scripts = "<script type='text/javascript' src='/includes/index.js'></script>";
172 $this->display_head();
173 $this->display_exhibits();
174 echo "<ul id='portfolio'>";
175 $this->list_projects();
176 echo <<<OTHER_PROJECTS
177 <li>
178 <h3>things i've done for others:</h3>
179 </li>
180
181 <li><a href=
182 "http://activehamptons.com">activehamptons.com</a></li>
183
184 <li><a href=
185 "http://transfishing.com">transfishing.com</a></li>
186
187 <li>
188 <h3>something i've worked on:</h3>
189 </li>
190
191 <li><a href=
192 "http://tempositions.com">tempositions.com</a></li>
193
194 <li>
195 <h3>my repositories:</h3>
196 </li>
197
198 <li><a href=
199 "/git/">git://dylansserver.com</a></li>
200
201 <li>
202 <h3>some notes:</h3>
203 </li>
204
205 <li><a href=
206 "/notes/">here</a> [<a href="/notes/rss">rss</a>]</li>
207
208 <li>
209 <h3>my resume:</h3>
210 </li>
211
212 <li>[<a href=
213 "/resume">pdf</a>]</li>
214
215 <li>
216 </li>
217 OTHER_PROJECTS;
218 // Because of the CSS necessary for the animations,
219 // the contact link needs to be in #portfolio to clear
220 // the floats.
221 echo "<li>";
222 $this->display_contact();
223 echo "</li>";
224 echo "</ul>";
225 $this->display_close($show_contact = false);
226 }
227
228 protected function display_exhibits() {
229 echo "<div id='exhibit'>";
230 $sql = "SELECT text FROM projects ORDER BY rank";
231 $result = $this->db->query($sql);
232 while ($entry = $result->fetch_object()) {
233 echo $entry->text;
234 }
235 echo "</div>";
236 }
237
238 private function list_projects() {
239 echo <<<HEREDOC
240 <li>
241 <h3>my projects:</h3>
242 </li>
243 HEREDOC;
244 $sql = "SELECT title FROM projects ORDER BY rank";
245 $result = $this->db->query($sql);
246 while ($entry = $result->fetch_object()) {
247 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
248 }
249 }
250
251 }
252
253
254 class project extends index {
255
256 protected function display_exhibits() {
257 echo "<div id='exhibit'>";
258 $sql = "SELECT text FROM projects
259 WHERE title = ?";
260 $result = $this->query($sql, "s", $_GET['project']);
261 if ($result = $result[0]['text']) {
262 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
263 echo $text;
264 echo "</div>";
265 } else {
266 throw new notFound();
267 }
268 }
269
270 }
271
272
273 class page extends cms {
274
275 private $page = 1;
276 private $offset = 0;
277 private $notes_per_page = 4;
278 private $number_of_pages = 1;
279
280 public function __construct() {
281 parent::__construct();
282 $this->page_offset();
283 $this->scripts = "
284 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
285 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
286 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
287 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
288 <script type='text/javascript'>
289 function highlight() {
290 SyntaxHighlighter.autoloader(
291 'js /includes/syntax/scripts/shBrushJScript.js',
292 'bash /includes/syntax/scripts/shBrushBash.js',
293 'sql /includes/syntax/scripts/shBrushSql.js',
294 'cpp /includes/syntax/scripts/shBrushCpp.js');
295 SyntaxHighlighter.defaults['gutter'] = false;
296 SyntaxHighlighter.defaults['toolbar'] = false;
297 SyntaxHighlighter.all();
298 }
299 </script>
300 ";
301 }
302
303 private function page_offset() {
304 $sql = "SELECT COUNT(*) FROM notes";
305 $result = $this->db->query($sql);
306 $result = $result->fetch_array();
307 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
308 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
309 $this->page = (int) $_GET['page'];
310 } else {
311 throw new notFound();
312 }
313 if ($this->page > $this->number_of_pages) {
314 throw new notFound();
315 }
316 if ($this->page < 1) {
317 throw new notFound();
318 }
319 $this->offset = ($this->page - 1) * $this->notes_per_page;
320 }
321
322 public function display() {
323 require_once("view/page.php");
324 }
325
326 public function display_notes() {
327 echo "<div id='notes'>";
328 $sql = "SELECT date_posted, title, url, text
329 FROM notes ORDER BY date_posted DESC
330 LIMIT ?, ?";
331 $result = $this->query($sql, "ii",
332 $this->offset,
333 $this->notes_per_page);
334 foreach ($result as $row => $entry) {
335 $title = $entry['title'];
336 $url = '/note/' . $entry['url'];
337 $date_posted = explode("-", $entry['date_posted']);
338 $year_posted = $date_posted[0];
339 $month_posted = $date_posted[1];
340 $datetime_posted = explode(' ', $date_posted[2]);
341 $day_posted = $datetime_posted[0];
342 $text = $entry['text'];
343 echo <<<END_NOTE
344 <div class='note'>
345 <h1>
346 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
347 </h1>
348 $text
349 </div>
350 END_NOTE;
351 }
352 echo "</div>";
353 }
354 }
355
356
357 class note extends cms {
358
359 private $id;
360 private $comments_enabled = false;
361 private $failed_captcha;
362 public $url;
363 public $title;
364 public $year_posted;
365 public $month_posted;
366 public $day_posted;
367 public $text;
368 public $number_of_comments;
369
370 public function __construct() {
371 $this->scripts = "
372 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
373 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
374 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
375 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
376 <script type='text/javascript'>
377 function highlight() {
378 SyntaxHighlighter.autoloader(
379 'js /includes/syntax/scripts/shBrushJScript.js',
380 'bash /includes/syntax/scripts/shBrushBash.js',
381 'sql /includes/syntax/scripts/shBrushSql.js',
382 'cpp /includes/syntax/scripts/shBrushCpp.js');
383 SyntaxHighlighter.defaults['gutter'] = false;
384 SyntaxHighlighter.defaults['toolbar'] = false;
385 SyntaxHighlighter.all();
386 }
387 </script>
388 ";
389
390 if (isset($_GET['comments'])) {
391 $this->scripts .= "
392 <script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
393 <script type='text/javascript' src='/includes/comment.js'></script>";
394 }
395 parent::__construct();
396 if (isset($_GET['comments'])) {
397 $this->comments_enabled = true;
398 }
399 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
400 if (isset($_GET['verify'])) {
401 $url = substr($url, 0, (strlen($url)-6));
402 }
403 $this->url = $url;
404 $sql = "SELECT title, date_posted, text, id
405 FROM notes WHERE url = ?";
406 $result = $this->query($sql, "s",
407 $_GET['note']);
408 if ($result) {
409 $entry = $result[0];
410 $this->id = $entry["id"];
411 $this->title = $entry["title"];
412 $date_posted = explode("-", $entry["date_posted"]);
413 $this->year_posted = $date_posted[0];
414 $this->month_posted = $date_posted[1];
415 $datetime_posted = explode(' ', $date_posted[2]);
416 $this->day_posted = $datetime_posted[0];
417 $this->text = $entry["text"];
418 } else {
419 throw new notFound();
420 }
421 $sql = "SELECT COUNT(*) FROM comments
422 WHERE note = $this->id";
423 $result = $this->db->query($sql);
424 $result = $result->fetch_array();
425 $this->number_of_comments = $result[0];
426 if (isset($_GET['verify'])) {
427 $this->verify();
428 }
429 }
430
431 public function display() {
432 require_once("view/note.php");
433 }
434
435 private function verify() {
436 if (!isset($_POST['captcha'])) {
437 require_once('includes/recaptchalib.php');
438 echo "<br>";
439 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
440 $_SERVER["REMOTE_ADDR"],
441 $_POST["recaptcha_challenge_field"],
442 $_POST["recaptcha_response_field"]);
443 if (!$resp->is_valid) {
444 $this->failed_captcha = true;
445 }
446 }
447 if (isset($_POST['captcha']) || $resp->is_valid) {
448 $sql = ("INSERT INTO comments (date_posted, author,
449 text, note)
450 VALUES(NOW(), ?, ?, ?)");
451 $stmt = $this->db->prepare($sql);
452 // Checks are needed here (no blank text,
453 // and a default author needs to be set
454 // for no-javascript users.
455 $stmt->bind_param('sss',
456 $_POST['name'],
457 $_POST['text'],
458 $this->id);
459 $stmt->execute();
460 }
461 }
462
463 private function display_comment_link() {
464 if ($this->number_of_comments > 0) {
465 $anchor_text = "comments($this->number_of_comments)/";
466 } else {
467 $anchor_text = "comment?";
468 }
469 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
470 $url = $this->url . 'comments/';
471 } else {
472 $url = $this->url . '/comments/';
473 }
474 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
475 }
476
477 private function display_comments() {
478 echo "<div id='comments'>";
479 $sql= "SELECT date_posted, author, text
480 FROM comments WHERE note = ?
481 ORDER BY date_posted DESC";
482 $result = $this->query($sql, 'd', $this->id);
483 foreach ($result as $row => $entry) {
484 $date_posted = $entry['date_posted'];
485 $author = $entry['author'];
486 $text = htmlspecialchars($entry['text']);
487 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
488 echo <<<END_OF_COMMENT
489 <div class='comment'>
490 $head
491 $text
492 </div>
493 END_OF_COMMENT;
494 }
495 echo "</div>";
496 }
497
498 private function display_comment_form() {
499 $publickey = $this->recaptcha_publickey;
500 echo <<<END_CAPTCHA_STYLE
501 <script type="text/javascript">
502 Recaptcha.create("$publickey",
503 "recaptcha_div",
504 {
505 theme : 'custom',
506 custom_theme_widget: 'recaptcha_widget',
507 callback: Recaptcha.focus_response_field
508 });
509 </script>
510 END_CAPTCHA_STYLE;
511 require_once('includes/recaptchalib.php');
512 $url = $this->url . "verify";
513 echo "<form id='comment_form' method='post' action='$url'>";
514 echo <<<END_OF_FORM
515 <div id="comment">
516 <h3>comment:</h3>
517 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
518 <h3>name:</h3>
519 <input type=text name="name" id="comment_name">
520
521 <nowiki>
522 <div id="recaptcha_widget">
523 <br>
524 <h3><b>what's this say</b>?</h3>
525 <br>
526 <div id="recaptcha_image"></div>
527 <br><br><br>
528 <span class="recaptcha_only_if_image"><br><br><br></span>
529 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
530 <br><br>
531 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
532 <span class="recaptcha_help">
533 <a href="javascript:Recaptcha.reload()">another?</a> /
534 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
535 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
536 </span>
537 </div>
538 END_OF_FORM;
539 echo recaptcha_get_html($this->recaptcha_publickey);
540 if ($this->failed_captcha) {
541 echo <<<END_OF_ERRORS
542 <div id="not_human">
543 reCAPTCHA said you're not human, <br>
544 try again?
545 </div>
546 <input id="submit" class="submit" type="submit" value="post comment">
547 </form>
548 </div>
549 END_OF_ERRORS;
550 } else {
551 echo <<<END_OF_ERRORS
552 <div id="not_human">
553 reCAPTCHA said you're not human, <br>
554 try again?
555 </div>
556 <div id="blank_comment">
557 but you didn't write anything! <br>
558 </div>
559 END_OF_ERRORS;
560 }
561 echo <<<END_OF_FORM
562 <input id="submit" class="submit" type="submit" value="post comment">
563 </form>
564 </div>
565 END_OF_FORM;
566 }
567 }
568
569
570 class archive extends cms {
571
572 public function __construct() {
573 parent::__construct();
574 }
575
576 private function check_exists() {
577 $sql = "SELECT COUNT(*) FROM notes
578 WHERE url = ?";
579 $results = $this->query($sql, "s", $_GET['note']);
580 if ($results[0]["COUNT(*)"] != 1) {
581 $this->not_found();
582 }
583 }
584
585 public function display() {
586 $this->display_head();
587 switch (true) {
588 case (isset($_GET['year']) && !isset($_GET['month'])
589 && !isset($_GET['day'])):
590 $sql = "SELECT title, url, date_posted, text
591 FROM notes WHERE YEAR(date_posted) = ?
592 ORDER BY date_posted DESC";
593 $result = $this->query($sql, "d",
594 $_GET['year']);
595 break;
596 case (isset($_GET['year']) && isset($_GET['month'])
597 && !isset($_GET['day'])):
598 $sql = "SELECT title, url, date_posted, text
599 FROM notes WHERE YEAR(date_posted) = ?
600 AND MONTH(date_posted) = ?
601 ORDER BY date_posted DESC";
602 $result = $this->query($sql, "dd",
603 $_GET['year'], $_GET['month']);
604 break;
605 case (isset($_GET['year']) && isset($_GET['month'])
606 && isset($_GET['day'])):
607 $sql = "SELECT title, url, date_posted, text
608 FROM notes WHERE YEAR(date_posted) = ?
609 AND MONTH(date_posted) = ?
610 AND DAY(date_posted) = ?
611 ORDER BY date_posted DESC";
612 $result = $this->query($sql, "ddd",
613 $_GET['year'], $_GET['month'],
614 $_GET['day']);
615 break;
616 }
617 if (count($result) >= 1) {
618 echo "<div id='notes'>";
619 foreach ($result as $row => $entry) {
620 $title = $entry['title'];
621 $url = '/note/' . $entry['url'];
622 $date_posted = explode("-", $entry['date_posted']);
623 $year_posted = $date_posted[0];
624 $month_posted = $date_posted[1];
625 $datetime_posted = explode(' ', $date_posted[2]);
626 $day_posted = $datetime_posted[0];
627 echo "<div class='note'>";
628 echo "<h1><span class='date'>";
629 echo "$year_posted/$month_posted/$day_posted/";
630 echo "</span><a href='$url'>$title</a></h1>";
631 echo $entry['text'];
632 echo "</div>";
633 }
634 echo "</div>";
635 $this->write_navigation();
636 } else {
637 echo "<br>";
638 echo "<h1>sorry, nothing here</h2>";
639 echo "<pre>Empty set (0.00 sec)</pre>";
640 }
641 $this->display_close();
642 }
643
644 private function write_navigation() {
645 echo "<br>";
646 echo "<div id='navigation'>";
647 // fill me in!
648 echo "</div>";
649 }
650
651 }
652
653
654 class rss extends cms {
655 public function display() {
656 $result = $this->db->query("SELECT date_posted, title, text, url
657 FROM notes ORDER BY date_posted DESC
658 LIMIT 5");
659 echo <<<END_OF_ENTRY
660 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
661 <channel>
662 <title>dylansserver.com/notes/rss</title>
663 <link>http://dylansserver.com/notes</link>
664 <description>dylansserver.com/notes/rss</description>
665 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
666 END_OF_ENTRY;
667 while ($entry = $result->fetch_object()) {
668 $title = $entry->title;
669 $date_posted = $entry->date_posted;
670 $url = "http://dylansserver.com/note/" . $entry->url;
671 $text = $entry->text;
672 $text = strip_tags($text);
673 $end_of_first_sentence = strpos($text, '.');
674 if ($end_of_first_sentence) {
675 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
676 if ($end_of_second_sentence) {
677 $description = substr($text, '0', ($end_of_second_sentence + 1));
678 } else {
679 $description = substr($text, '0', ($end_of_first_sentence + 1));
680 }
681 }
682 echo <<<END_OF_ENTRY
683 <item>
684 <title>$title</title>
685 <link>$url</link>
686 <guid>$url</guid>
687 <description>$description</description>
688 </item>
689 END_OF_ENTRY;
690 }
691 echo "</channel>";
692 echo "</rss>";
693
694 }
695 }
696
697
698 class notFound extends Exception {
699
700 public function __construct() {
701 header('HTTP/1.0 404 Not Found');
702 ob_end_clean();
703 include('404.php');
704 exit();
705 }
706
707 }
708
709
710 class captcha extends cms {
711
712 public function display() {
713 $challenge = $_GET['challenge'];
714 $response = $_GET['response'];
715 $remoteip = $_SERVER['REMOTE_ADDR'];
716 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
717 curl_setopt ($curl, CURLOPT_POST, 4);
718 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
719 $result = curl_exec ($curl);
720 curl_close ($curl);
721 }
722
723 }
724
725 ?>