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