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