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