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