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