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