Comments now displayed, and captcha functioning
[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 private $url;
300
301 public function __construct($comments_enabled = false) {
302 parent::__construct();
303 $this->check_exists();
304 $this->comments_enabled = $comments_enabled;
305 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
306 if (isset($_GET['verify'])) {
307 $url = substr($url, 0, (strlen($url)-7));
308 }
309 $this->url = $url;
310 }
311
312 private function check_exists() {
313 $sql = "SELECT COUNT(*) FROM notes
314 WHERE url = ?";
315 $results = $this->query($sql, "s", $_GET['note']);
316 if ($results[0]["COUNT(*)"] != 1) {
317 throw new notFound();
318 }
319 }
320
321 public function display() {
322 $this->display_head();
323 $this->display_note();
324 if (isset($_GET['verify'])) {
325 $this->verify();
326 }
327 if ($this->comments_enabled) {
328 $this->display_comments(); // but where are they?
329 $this->display_comment_form();
330 }
331 $this->write_navigation();
332 $this->display_close();
333 }
334
335 private function verify() {
336 require_once('includes/recaptchalib.php');
337 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
338 $_SERVER["REMOTE_ADDR"],
339 $_POST["recaptcha_challenge_field"],
340 $_POST["recaptcha_response_field"]);
341 if (!$resp->is_valid) {
342 echo "The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")";
343 }
344 }
345
346 private function display_note() {
347 $sql = "SELECT title, date_posted, text, id
348 FROM notes WHERE url = ?";
349 $result = $this->query($sql, "s",
350 $_GET['note']);
351 $entry = $result[0];
352 $this->id = $entry["id"]; // This is needed for display_comments()
353 $title = $entry["title"];
354 $date_posted = explode("-", $entry["date_posted"]);
355 $year_posted = $date_posted[0];
356 $month_posted = $date_posted[1];
357 $datetime_posted = explode(' ', $date_posted[2]);
358 $day_posted = $datetime_posted[0];
359 echo "<div id=\"note\">";
360 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span>$title</h2>";
361 if (!$this->comments_enabled) {
362 $this->display_comment_link();
363 }
364 echo $entry['text'];
365 }
366
367 private function write_navigation() {
368 echo "<br>";
369 echo "<div id=\"navigation\">";
370 echo "<h2>";
371 echo "<a href=\"/notes/\">notes</a>/";
372 echo "</h2>";
373 echo "</div>";
374 }
375
376 private function display_comment_link() {
377 $url = $this->url . 'comments/';
378 echo "<a id=\"comment_link\" href=\"$url\">comments</a>";
379 }
380
381 private function display_comments() {
382 echo "<div id=\"comments\">";
383 $sql= "SELECT date_posted, author, email, text
384 FROM comments WHERE note = ?";
385 $result = $this->query($sql, "d", $this->id);
386 foreach ($result as $row => $entry) {
387 $date_posted = $entry['date_posted'];
388 $author = $entry['author'];
389 $email = $entry['email'];
390 $text = htmlspecialchars($entry['text']);
391 echo <<<END_OF_COMMENT
392 <h3><a href="mailto:$email">$author</a></h3>
393 $text
394 <br>
395 <br>
396 END_OF_COMMENT;
397 }
398 echo "</div>";
399 }
400
401 private function display_comment_form() {
402 // Trailing slash is necessary for reloads to work
403 $url = $this->url . "verify/";
404 echo "<form id=\"comment\" method=\"post\" action=\"$url\">";
405 require_once('includes/recaptchalib.php');
406 echo recaptcha_get_html($this->recaptcha_publickey);
407 echo "<input type=\"submit\">";
408 echo "</form>";
409 }
410 }
411
412
413 class archive extends cms {
414
415 public function __construct() {
416 parent::__construct();
417 }
418
419 private function check_exists() {
420 $sql = "SELECT COUNT(*) FROM notes
421 WHERE url = ?";
422 $results = $this->query($sql, "s", $_GET['note']);
423 if ($results[0]["COUNT(*)"] != 1) {
424 $this->not_found();
425 }
426 }
427
428 public function display() {
429 // this really needs its own pagination...
430 // there should be a class for that.
431 $this->display_head();
432 switch (true) {
433 case (isset($_GET['year']) && !isset($_GET['month'])
434 && !isset($_GET['day'])):
435 $sql = "SELECT title, url, date_posted, text
436 FROM notes WHERE YEAR(date_posted) = ?
437 ORDER BY date_posted DESC";
438 $result = $this->query($sql, "d",
439 $_GET['year']);
440 break;
441 case (isset($_GET['year']) && isset($_GET['month'])
442 && !isset($_GET['day'])):
443 $sql = "SELECT title, url, date_posted, text
444 FROM notes WHERE YEAR(date_posted) = ?
445 AND MONTH(date_posted) = ?
446 ORDER BY date_posted DESC";
447 $result = $this->query($sql, "dd",
448 $_GET['year'], $_GET['month']);
449 break;
450 case (isset($_GET['year']) && isset($_GET['month'])
451 && isset($_GET['day'])):
452 $sql = "SELECT title, url, date_posted, text
453 FROM notes WHERE YEAR(date_posted) = ?
454 AND MONTH(date_posted) = ?
455 AND DAY(date_posted) = ?
456 ORDER BY date_posted DESC";
457 $result = $this->query($sql, "ddd",
458 $_GET['year'], $_GET['month'],
459 $_GET['day']);
460 break;
461 }
462 if (count($result) >= 1) {
463 echo "<div id=\"notes\">";
464 foreach ($result as $row => $entry) {
465 $title = $entry['title'];
466 $url = '/note/' . $entry['url'];
467 $date_posted = explode("-", $entry['date_posted']);
468 $year_posted = $date_posted[0];
469 $month_posted = $date_posted[1];
470 $datetime_posted = explode(' ', $date_posted[2]);
471 $day_posted = $datetime_posted[0];
472 echo "<div class=\"note\">";
473 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
474 echo $entry['text'];
475 echo "</div>";
476 }
477 echo "</div>";
478 $this->write_navigation();
479 } else {
480 echo "<br>";
481 echo "<h2 style=\"font-family:sans-serif;\">sorry, nothing here</h2>";
482 echo "<pre>Empty set (0.00 sec)</pre>";
483 }
484 $this->display_close();
485 }
486
487 private function write_navigation() {
488 echo "<br>";
489 echo "<div id=\"navigation\">";
490 echo "<h2>";
491 // fill me in!
492 echo "</h2>";
493 echo "</div>";
494 }
495 }
496
497
498 class notFound extends Exception {
499 public function __construct() {
500 header("HTTP/1.0 404 Not Found");
501 ob_end_clean();
502 include("404.php");
503 exit();
504 }
505 }
506
507 ## now actually do something:
508 switch (cms::determine_type()) {
509 case "index":
510 $index = new index();
511 $index->display();
512 break;
513 case "project":
514 $project = new project();
515 $project->display();
516 break;
517 case "note":
518 if (isset($_GET['comments'])) {
519 $note = new note($comments_enabled = true);
520 } else {
521 $note = new note;
522 }
523 $note->display();
524 break;
525 case "page":
526 $page = new page;
527 $page->display();
528 break;
529 case "archive":
530 $archive = new archive;
531 $archive->display();
532 break;
533 }
534
535 ?>