Added form fields and started insert SQL
[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 } else {
344 $sql = ("INSERT INTO comments (date_posted, author,
345 email, text, note
346 VALUES(NOW(), ?, ?, ?, ?, ?");
347 echo htmlspecialchars($_POST['author']);
348 echo htmlspecialchars($_POST['email']);
349 echo htmlspecialchars($_POST['text']);
350 }
351 }
352
353 private function display_note() {
354 $sql = "SELECT title, date_posted, text, id
355 FROM notes WHERE url = ?";
356 $result = $this->query($sql, "s",
357 $_GET['note']);
358 $entry = $result[0];
359 $this->id = $entry["id"]; // This is needed for display_comments()
360 $title = $entry["title"];
361 $date_posted = explode("-", $entry["date_posted"]);
362 $year_posted = $date_posted[0];
363 $month_posted = $date_posted[1];
364 $datetime_posted = explode(' ', $date_posted[2]);
365 $day_posted = $datetime_posted[0];
366 echo "<div id=\"note\">";
367 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span>$title</h2>";
368 if (!$this->comments_enabled) {
369 $this->display_comment_link();
370 }
371 echo $entry['text'];
372 }
373
374 private function write_navigation() {
375 echo <<<END_OF_NAVIGATION
376 <br>
377 <div id=\"navigation\">
378 <h2>
379 <a href=\"/notes/\">notes</a>/
380 </h2>
381 </div>
382 END_OF_NAVIGATION;
383 }
384
385 private function display_comment_link() {
386 $url = $this->url . 'comments/';
387 echo "<a id=\"comment_link\" href=\"$url\">comments</a>";
388 }
389
390 private function display_comments() {
391 echo "<div id=\"comments\">";
392 $sql= "SELECT date_posted, author, email, text
393 FROM comments WHERE note = ?";
394 $result = $this->query($sql, "d", $this->id);
395 foreach ($result as $row => $entry) {
396 $date_posted = $entry['date_posted'];
397 $author = $entry['author'];
398 $email = $entry['email'];
399 $text = htmlspecialchars($entry['text']);
400 echo <<<END_OF_COMMENT
401 <h3><a href="mailto:$email">$author</a></h3>
402 $text
403 <br>
404 <br>
405 END_OF_COMMENT;
406 }
407 echo "</div>";
408 }
409
410 private function display_comment_form() {
411 echo <<<END_CAPTCHA_STYLE
412 <script type="text/javascript">
413 var RecaptchaOptions = {
414 theme : 'custom',
415 custom_theme_widget: 'recaptcha_widget'
416 };
417 </script>
418 END_CAPTCHA_STYLE;
419 require_once('includes/recaptchalib.php');
420 // Trailing slash is necessary for reloads to work
421 $url = $this->url . "verify/";
422 echo "<form method=\"post\" action=\"$url\">";
423 echo <<<FORM
424 <div id="comment">
425
426 <h3>comment:</h3><br>
427 <textarea rows="10" cols="30" name=text></textarea><br>
428 <h3>name:</h3><br>
429 <input type=text name=author><br>
430 <h3>email:</h3><br>
431 <input type=text name=email><br>
432 <nowiki>
433
434 <div id="recaptcha_widget">
435 <div id="recaptcha_image"></div>
436 <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
437 <span class="recaptcha_only_if_image"><b>enter the words above</b>:</span>
438 <span class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</span>
439 <br>
440 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
441 <div><a href="javascript:Recaptcha.reload()">get another CAPTCHA</a></div>
442 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">get an audio CAPTCHA</a></div>
443 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>
444 <div><a href="javascript:Recaptcha.showhelp()">help?</a></div>
445 <br><br>
446 </div>
447 FORM;
448 echo recaptcha_get_html($this->recaptcha_publickey);
449 echo <<<END_OF_FORM
450 <input class="submit" type="submit" value="comment">
451 </form>
452 </div>
453 END_OF_FORM;
454 }
455 }
456
457
458 class archive extends cms {
459
460 public function __construct() {
461 parent::__construct();
462 }
463
464 private function check_exists() {
465 $sql = "SELECT COUNT(*) FROM notes
466 WHERE url = ?";
467 $results = $this->query($sql, "s", $_GET['note']);
468 if ($results[0]["COUNT(*)"] != 1) {
469 $this->not_found();
470 }
471 }
472
473 public function display() {
474 // this really needs its own pagination...
475 // there should be a class for that.
476 $this->display_head();
477 switch (true) {
478 case (isset($_GET['year']) && !isset($_GET['month'])
479 && !isset($_GET['day'])):
480 $sql = "SELECT title, url, date_posted, text
481 FROM notes WHERE YEAR(date_posted) = ?
482 ORDER BY date_posted DESC";
483 $result = $this->query($sql, "d",
484 $_GET['year']);
485 break;
486 case (isset($_GET['year']) && isset($_GET['month'])
487 && !isset($_GET['day'])):
488 $sql = "SELECT title, url, date_posted, text
489 FROM notes WHERE YEAR(date_posted) = ?
490 AND MONTH(date_posted) = ?
491 ORDER BY date_posted DESC";
492 $result = $this->query($sql, "dd",
493 $_GET['year'], $_GET['month']);
494 break;
495 case (isset($_GET['year']) && isset($_GET['month'])
496 && isset($_GET['day'])):
497 $sql = "SELECT title, url, date_posted, text
498 FROM notes WHERE YEAR(date_posted) = ?
499 AND MONTH(date_posted) = ?
500 AND DAY(date_posted) = ?
501 ORDER BY date_posted DESC";
502 $result = $this->query($sql, "ddd",
503 $_GET['year'], $_GET['month'],
504 $_GET['day']);
505 break;
506 }
507 if (count($result) >= 1) {
508 echo "<div id=\"notes\">";
509 foreach ($result as $row => $entry) {
510 $title = $entry['title'];
511 $url = '/note/' . $entry['url'];
512 $date_posted = explode("-", $entry['date_posted']);
513 $year_posted = $date_posted[0];
514 $month_posted = $date_posted[1];
515 $datetime_posted = explode(' ', $date_posted[2]);
516 $day_posted = $datetime_posted[0];
517 echo "<div class=\"note\">";
518 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
519 echo $entry['text'];
520 echo "</div>";
521 }
522 echo "</div>";
523 $this->write_navigation();
524 } else {
525 echo "<br>";
526 echo "<h2 style=\"font-family:sans-serif;\">sorry, nothing here</h2>";
527 echo "<pre>Empty set (0.00 sec)</pre>";
528 }
529 $this->display_close();
530 }
531
532 private function write_navigation() {
533 echo "<br>";
534 echo "<div id=\"navigation\">";
535 echo "<h2>";
536 // fill me in!
537 echo "</h2>";
538 echo "</div>";
539 }
540 }
541
542
543 class notFound extends Exception {
544 public function __construct() {
545 header("HTTP/1.0 404 Not Found");
546 ob_end_clean();
547 include("404.php");
548 exit();
549 }
550 }
551
552 ## now actually do something:
553 switch (cms::determine_type()) {
554 case "index":
555 $index = new index();
556 $index->display();
557 break;
558 case "project":
559 $project = new project();
560 $project->display();
561 break;
562 case "note":
563 if (isset($_GET['comments'])) {
564 $note = new note($comments_enabled = true);
565 } else {
566 $note = new note;
567 }
568 $note->display();
569 break;
570 case "page":
571 $page = new page;
572 $page->display();
573 break;
574 case "archive":
575 $archive = new archive;
576 $archive->display();
577 break;
578 }
579
580 ?>