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