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