4 private $config_file = '/etc/dylanstestserver.ini';
6 protected $recaptcha_publickey;
7 protected $recaptcha_privatekey;
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();
21 $this->recaptcha_publickey
= $config[recaptcha
]['publickey'];
22 $this->recaptcha_privatekey
= $config[recaptcha
]['privatekey'];
26 public static function determine_type() {
27 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
29 } else if (isset($_GET['year'])) {
31 } else if (isset($_GET['note'])) {
33 } else if ($_SERVER['REQUEST_URI'] == '/') {
35 } else if (isset($_GET['project'])) {
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();
47 $statement->store_result();
49 $data = $statement->result_metadata();
51 $fields[0] = &$statement;
52 while($field = $data->fetch_field()) {
53 $fields[] = &$row[$field->name
];
55 call_user_func_array("mysqli_stmt_bind_result", $fields);
57 while ($statement->fetch()) {
58 foreach ($row as $key1=>$value1) $return[$i][$key1] = $value1;
61 $statement->free_result();
65 public function display_head($title = "dylanstestserver",
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&ucn_task=conformance#";
73 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
74 "http://www.w3.org/TR/html4/loose.dtd">
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">
83 <link rel="icon" href="favicon.ico" type="image/png">
93 <img src="/images/dylanstestserver.png" alt="dylanstestserver"
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>
110 public function display_close($show_contact = true) {
112 $this->display_contact();
127 class blank_page extends cms {
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
139 <h3>things i've done for others:</h3>
143 "http://activehamptons.com">activehamptons.com</a></li>
146 "http://transfishing.com">transfishing.com</a></li>
149 <h3>something i've worked on:</h3>
153 "http://tempositions.com">tempositions.com</a></li>
156 <h3>my repositories:</h3>
160 "git">git://dylanstestserver.com</a></li>
167 "/notes/">here</a></li>
172 // Because of the CSS necessary for the animations,
173 // the contact link needs to be in #portfolio to clear
175 $this->display_contact();
177 $this->display_close($show_contact = false);
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()) {
190 private function list_projects() {
191 echo "<div id=\"exhibit\">";
194 <h3>my projects:</h3>
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>";
205 class project extends index {
206 protected function display_exhibits() {
207 echo "<div id=\"exhibit\">";
208 $sql = "SELECT text FROM projects
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);
216 throw new notFound();
221 class page extends cms {
224 private $notes_per_page = 4;
225 private $number_of_pages = 1;
227 public function __construct() {
228 parent::__construct();
229 $this->page_offset();
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'];
240 throw new notFound();
242 if ($this->page > $this->number_of_pages) {
243 throw new notFound();
245 if ($this->page < 1) {
246 throw new notFound();
248 $this->offset = ($this->page - 1) * $this->notes_per_page;
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
257 $result = $this->query($sql, "ii",
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>";
274 $this->write_navigation();
275 $this->display_close();
278 private function write_navigation() {
279 echo "<div id=\"navigation\">";
282 $previous_page = $this->page - 1;
283 echo "<a href=\"/notes/page/$previous_page\">prev</a>";
285 if($this->page < $this->number_of_pages) {
286 $forward_page = $this->page + 1;
287 echo " <a href=\"/notes/page/$forward_page\">next</a>";
295 class note extends cms {
298 private $comments_enabled = false;
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));
312 private function check_exists() {
313 $sql = "SELECT COUNT(*) FROM notes
315 $results = $this->query($sql, "s", $_GET['note']);
316 if ($results[0]["COUNT(*)"] != 1) {
317 throw new notFound();
321 public function display() {
322 $this->display_head();
323 $this->display_note();
324 if ($this->comments_enabled) {
325 $this->display_comments();
326 $this->display_comment_form();
328 $this->write_navigation();
329 $this->display_close();
332 private function verify() {
333 require_once('includes/recaptchalib.php');
335 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
336 $_SERVER["REMOTE_ADDR"],
337 $_POST["recaptcha_challenge_field"],
338 $_POST["recaptcha_response_field"]);
339 if (!$resp->is_valid) {
340 echo "<span style=\"color:red;border:1px solid black;padding:15px;\">sorry, reCAPTCHA said you're not human.</span><br><br><br>";
342 $sql = ("INSERT INTO comments (date_posted, author,
344 VALUES(NOW(), ?, ?, ?, ?)");
345 $stmt = $this->db->prepare($sql);
346 // Checks are needed here (no blank text,
347 // and a default author / email need to be set
348 $stmt->bind_param('ssss',
349 htmlspecialchars($_POST['author']),
350 htmlspecialchars($_POST['email']),
351 htmlspecialchars($_POST['text']),
357 private function display_note() {
358 $sql = "SELECT title, date_posted, text, id
359 FROM notes WHERE url = ?";
360 $result = $this->query($sql, "s",
363 $this->id = $entry["id"]; // This is needed for display_comments()
364 $title = $entry["title"];
365 $date_posted = explode("-", $entry["date_posted"]);
366 $year_posted = $date_posted[0];
367 $month_posted = $date_posted[1];
368 $datetime_posted = explode(' ', $date_posted[2]);
369 $day_posted = $datetime_posted[0];
370 echo "<div id=\"note\">";
371 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span>$title</h2>";
372 if (!$this->comments_enabled) {
373 $this->display_comment_link();
378 private function write_navigation() {
379 echo <<<END_OF_NAVIGATION
381 <div id=\"navigation\">
383 <a href="/notes/">notes</a>/
389 private function display_comment_link() {
390 // somehow I should be checking if there are any first,
391 // change to 'comment?'
392 $url = $this->url . 'comments/';
393 echo "<a id=\"comment_link\" href=\"$url\">comments</a>";
396 private function display_comments() {
397 echo "<div id=\"comments\">";
398 $sql= "SELECT date_posted, author, email, text
399 FROM comments WHERE note = ?
400 ORDER BY date_posted DESC";
401 $result = $this->query($sql, "d", $this->id);
402 foreach ($result as $row => $entry) {
403 $date_posted = $entry['date_posted'];
404 $author = $entry['author'];
405 $email = $entry['email'];
406 $text = htmlspecialchars($entry['text']);
407 echo <<<END_OF_COMMENT
408 <h3><a href="mailto:$email">$author</a></h3>
417 private function display_comment_form() {
418 echo <<<END_CAPTCHA_STYLE
419 <script type="text/javascript">
420 var RecaptchaOptions = {
422 custom_theme_widget: 'recaptcha_widget'
426 require_once('includes/recaptchalib.php');
427 // Trailing slash is necessary for reloads to work
428 $url = $this->url . "verify/";
429 echo "<form method=\"post\" action=\"$url\">";
433 <h3>comment:</h3><br>
434 <textarea rows="10" cols="70" name=text></textarea><br>
436 <input type=text name=author><br>
438 <input type=text name=email><br>
441 <div id="recaptcha_widget">
442 <div id="recaptcha_image"></div>
443 <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
444 <span class="recaptcha_only_if_image"><b>enter the words above</b>:</span>
445 <span class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</span>
447 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
448 <div><a href="javascript:Recaptcha.reload()"><small>another CAPTCHA?</small></a></div>
449 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')"><small>audio CAPTCHA?</small></a></div>
450 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')"><small>Get an image CAPTCHA</small></a></div>
451 <div><a href="javascript:Recaptcha.showhelp()"><small>help?</small></a></div>
455 echo recaptcha_get_html($this->recaptcha_publickey);
456 if (isset($_GET['verify'])) {
460 <input class="submit" type="submit" value="comment">
468 class archive extends cms {
470 public function __construct() {
471 parent::__construct();
474 private function check_exists() {
475 $sql = "SELECT COUNT(*) FROM notes
477 $results = $this->query($sql, "s", $_GET['note']);
478 if ($results[0]["COUNT(*)"] != 1) {
483 public function display() {
484 // this really needs its own pagination...
485 // there should be a class for that.
486 $this->display_head();
488 case (isset($_GET['year']) && !isset($_GET['month'])
489 && !isset($_GET['day'])):
490 $sql = "SELECT title, url, date_posted, text
491 FROM notes WHERE YEAR(date_posted) = ?
492 ORDER BY date_posted DESC";
493 $result = $this->query($sql, "d",
496 case (isset($_GET['year']) && isset($_GET['month'])
497 && !isset($_GET['day'])):
498 $sql = "SELECT title, url, date_posted, text
499 FROM notes WHERE YEAR(date_posted) = ?
500 AND MONTH(date_posted) = ?
501 ORDER BY date_posted DESC";
502 $result = $this->query($sql, "dd",
503 $_GET['year'], $_GET['month']);
505 case (isset($_GET['year']) && isset($_GET['month'])
506 && isset($_GET['day'])):
507 $sql = "SELECT title, url, date_posted, text
508 FROM notes WHERE YEAR(date_posted) = ?
509 AND MONTH(date_posted) = ?
510 AND DAY(date_posted) = ?
511 ORDER BY date_posted DESC";
512 $result = $this->query($sql, "ddd",
513 $_GET['year'], $_GET['month'],
517 if (count($result) >= 1) {
518 echo "<div id=\"notes\">";
519 foreach ($result as $row => $entry) {
520 $title = $entry['title'];
521 $url = '/note/' . $entry['url'];
522 $date_posted = explode("-", $entry['date_posted']);
523 $year_posted = $date_posted[0];
524 $month_posted = $date_posted[1];
525 $datetime_posted = explode(' ', $date_posted[2]);
526 $day_posted = $datetime_posted[0];
527 echo "<div class=\"note\">";
528 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
533 $this->write_navigation();
536 echo "<h2 style=\"font-family:sans-serif;\">sorry, nothing here</h2>";
537 echo "<pre>Empty set (0.00 sec)</pre>";
539 $this->display_close();
542 private function write_navigation() {
544 echo "<div id=\"navigation\">";
553 class notFound extends Exception {
554 public function __construct() {
555 header("HTTP/1.0 404 Not Found");
562 ## now actually do something:
563 switch (cms::determine_type()) {
565 $index = new index();
569 $project = new project();
573 if (isset($_GET['comments'])) {
574 $note = new note($comments_enabled = true);
585 $archive = new archive;