5 private $config_file = '/etc/dylansserver.ini';
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
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();
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'];
31 public static function determine_type() {
32 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
34 } else if (isset($_GET['year'])) {
36 } else if (isset($_GET['note'])) {
38 } else if ($_SERVER['REQUEST_URI'] == '/') {
40 } else if (isset($_GET['project'])) {
42 } else if (isset($_GET['rss'])) {
44 } else if (isset($_GET['challenge'])) {
49 public function query() {
50 $args = func_get_args();
51 $statement = $this->db
->prepare($args[0]);
52 $args = array_slice($args, 1);
53 call_user_func_array(array($statement, 'bind_param'), &$args);
54 $statement->execute();
56 $statement->store_result();
58 $data = $statement->result_metadata();
60 $fields[0] = &$statement;
61 while($field = $data->fetch_field()) {
62 $fields[] = &$row[$field->name
];
64 call_user_func_array("mysqli_stmt_bind_result", $fields);
66 while ($statement->fetch()) {
67 foreach ($row as $key=>$value) $return[$i][$key] = $value;
70 $statement->free_result();
74 public function init() {
75 switch (cms
::determine_type()) {
81 $project = new project();
97 $archive = new archive
;
101 $captcha = new captcha
;
110 class index
extends cms
{
112 public function display() {
113 require_once("view/index.php");
116 protected function display_exhibits() {
117 echo "<div id='exhibit'>";
118 $sql = "SELECT text FROM projects ORDER BY rank";
119 $result = $this->db
->query($sql);
120 while ($entry = $result->fetch_object()) {
126 private function list_projects() {
127 $sql = "SELECT title FROM projects ORDER BY rank";
128 $result = $this->db
->query($sql);
129 while ($entry = $result->fetch_object()) {
130 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
137 class project
extends index
{
139 protected function display_exhibits() {
140 echo "<div id='exhibit'>";
141 $sql = "SELECT text FROM projects
143 $result = $this->query($sql, "s", $_GET['project']);
144 if ($result = $result[0]['text']) {
145 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
149 throw new notFound();
156 class page
extends cms
{
160 private $notes_per_page = 4;
161 private $number_of_pages = 1;
163 public function __construct() {
164 parent
::__construct();
165 $this->page_offset();
168 private function page_offset() {
169 $sql = "SELECT COUNT(*) FROM notes";
170 $result = $this->db
->query($sql);
171 $result = $result->fetch_array();
172 $this->number_of_pages
= ceil($result[0] / $this->notes_per_page
);
173 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
174 $this->page
= (int) $_GET['page'];
176 throw new notFound();
178 if ($this->page
> $this->number_of_pages
) {
179 throw new notFound();
181 if ($this->page
< 1) {
182 throw new notFound();
184 $this->offset
= ($this->page
- 1) * $this->notes_per_page
;
187 public function display() {
188 require_once("view/page.php");
191 public function display_notes() {
192 echo "<div id='notes'>";
193 $sql = "SELECT date_posted, title, url, text
194 FROM notes ORDER BY date_posted DESC
196 $result = $this->query($sql, "ii",
198 $this->notes_per_page
);
199 foreach ($result as $row => $entry) {
200 $title = $entry['title'];
201 $url = '/note/' . $entry['url'];
202 $date_posted = explode("-", $entry['date_posted']);
203 $year_posted = $date_posted[0];
204 $month_posted = $date_posted[1];
205 $datetime_posted = explode(' ', $date_posted[2]);
206 $day_posted = $datetime_posted[0];
207 $text = $entry['text'];
211 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
222 class note extends cms {
225 private $comments_enabled = false;
226 private $failed_captcha;
230 public $month_posted;
233 public $number_of_comments;
235 public function __construct() {
236 parent::__construct();
237 if (isset($_GET['comments'])) {
238 $this->comments_enabled = true;
240 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
241 if (isset($_GET['verify'])) {
242 $url = substr($url, 0, (strlen($url)-6));
245 $sql = "SELECT title, date_posted, text, id
246 FROM notes WHERE url = ?";
247 $result = $this->query($sql, "s",
251 $this->id = $entry["id"];
252 $this->title = $entry["title"];
253 $date_posted = explode("-", $entry["date_posted"]);
254 $this->year_posted = $date_posted[0];
255 $this->month_posted = $date_posted[1];
256 $datetime_posted = explode(' ', $date_posted[2]);
257 $this->day_posted = $datetime_posted[0];
258 $this->text = $entry["text"];
260 throw new notFound();
262 $sql = "SELECT COUNT(*) FROM comments
263 WHERE note = $this->id";
264 $result = $this->db->query($sql);
265 $result = $result->fetch_array();
266 $this->number_of_comments = $result[0];
267 if (isset($_GET['verify'])) {
272 public function display() {
273 require_once("view/note.php");
276 private function verify() {
277 if (!isset($_POST['captcha'])) {
278 require_once('includes/recaptchalib.php');
280 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
281 $_SERVER["REMOTE_ADDR"],
282 $_POST["recaptcha_challenge_field"],
283 $_POST["recaptcha_response_field"]);
284 if (!$resp->is_valid) {
285 $this->failed_captcha = true;
288 if (isset($_POST['captcha']) || $resp->is_valid) {
289 $sql = ("INSERT INTO comments (date_posted, author,
291 VALUES(NOW(), ?, ?, ?)");
292 $stmt = $this->db->prepare($sql);
293 // Checks are needed here (no blank text,
294 // and a default author needs to be set
295 // for no-javascript users.
296 $stmt->bind_param('sss',
304 private function display_comment_link() {
305 if ($this->number_of_comments > 0) {
306 $anchor_text = "comments($this->number_of_comments)/";
308 $anchor_text = "comment?";
310 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
311 $url = $this->url . 'comments/';
313 $url = $this->url . '/comments/';
315 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
318 private function display_comments() {
319 echo "<div id='comments'>";
320 $sql= "SELECT date_posted, author, text
321 FROM comments WHERE note = ?
322 ORDER BY date_posted DESC";
323 $result = $this->query($sql, 'd', $this->id);
324 foreach ($result as $row => $entry) {
325 $date_posted = $entry['date_posted'];
326 $author = $entry['author'];
327 $text = htmlspecialchars($entry['text']);
328 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
329 echo <<<END_OF_COMMENT
330 <div class='comment'>
339 private function display_comment_form() {
340 $publickey = $this->recaptcha_publickey;
341 echo <<<END_CAPTCHA_STYLE
342 <script type="text/javascript">
343 Recaptcha.create("$publickey",
347 custom_theme_widget: 'recaptcha_widget',
348 callback: Recaptcha.focus_response_field
352 require_once('includes/recaptchalib.php');
353 $url = $this->url . "verify";
354 echo "<form id='comment_form' method='post' action='$url'>";
358 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
360 <input type=text name="name" id="comment_name">
363 <div id="recaptcha_widget">
365 <h3><b>what's this say</b>?</h3>
367 <div id="recaptcha_image"></div>
369 <span class="recaptcha_only_if_image"><br><br><br></span>
370 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
372 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
373 <span class="recaptcha_help">
374 <a href="javascript:Recaptcha.reload()">another?</a> /
375 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
376 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
380 echo recaptcha_get_html($this->recaptcha_publickey);
381 if ($this->failed_captcha) {
382 echo <<<END_OF_ERRORS
384 reCAPTCHA said you're not human, <br>
387 <input id="submit" class="submit" type="submit" value="post comment">
392 echo <<<END_OF_ERRORS
394 reCAPTCHA said you're not human, <br>
397 <div id="blank_comment">
398 but you didn't write anything! <br>
403 <input id="submit" class="submit" type="submit" value="post comment">
411 class archive extends cms {
413 public function __construct() {
414 parent::__construct();
417 private function check_exists() {
418 $sql = "SELECT COUNT(*) FROM notes
420 $results = $this->query($sql, "s", $_GET['note']);
421 if ($results[0]["COUNT(*)"] != 1) {
426 public function display() {
427 require_once("view/archive.php");
430 public function display_notes() {
432 case (isset($_GET['year']) && !isset($_GET['month'])
433 && !isset($_GET['day'])):
434 $sql = "SELECT title, url, date_posted, text
435 FROM notes WHERE YEAR(date_posted) = ?
436 ORDER BY date_posted DESC";
437 $result = $this->query($sql, "d",
440 case (isset($_GET['year']) && isset($_GET['month'])
441 && !isset($_GET['day'])):
442 $sql = "SELECT title, url, date_posted, text
443 FROM notes WHERE YEAR(date_posted) = ?
444 AND MONTH(date_posted) = ?
445 ORDER BY date_posted DESC";
446 $result = $this->query($sql, "dd",
447 $_GET['year'], $_GET['month']);
449 case (isset($_GET['year']) && isset($_GET['month'])
450 && isset($_GET['day'])):
451 $sql = "SELECT title, url, date_posted, text
452 FROM notes WHERE YEAR(date_posted) = ?
453 AND MONTH(date_posted) = ?
454 AND DAY(date_posted) = ?
455 ORDER BY date_posted DESC";
456 $result = $this->query($sql, "ddd",
457 $_GET['year'], $_GET['month'],
461 if (count($result) >= 1) {
462 echo "<div id='notes'>";
463 foreach ($result as $row => $entry) {
464 $title = $entry['title'];
465 $url = '/note/' . $entry['url'];
466 $date_posted = explode("-", $entry['date_posted']);
467 $year_posted = $date_posted[0];
468 $month_posted = $date_posted[1];
469 $datetime_posted = explode(' ', $date_posted[2]);
470 $day_posted = $datetime_posted[0];
471 echo "<div class='note'>";
472 echo "<h1><span class='date'>";
473 echo "$year_posted/$month_posted/$day_posted/";
474 echo "</span><a href='$url'>$title</a></h1>";
481 echo "<h1>sorry, nothing here</h2>";
482 echo "<pre>Empty set (0.00 sec)</pre>";
489 class rss extends cms {
490 public function display() {
491 $result = $this->db->query("SELECT date_posted, title, text, url
492 FROM notes ORDER BY date_posted DESC
495 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
497 <title>dylansserver.com/notes/rss</title>
498 <link>http://dylansserver.com/notes</link>
499 <description>dylansserver.com/notes/rss</description>
500 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
502 while ($entry = $result->fetch_object()) {
503 $title = $entry->title;
504 $date_posted = $entry->date_posted;
505 $url = "http://dylansserver.com/note/" . $entry->url;
506 $text = $entry->text;
507 $text = strip_tags($text);
508 $end_of_first_sentence = strpos($text, '.');
509 if ($end_of_first_sentence) {
510 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
511 if ($end_of_second_sentence) {
512 $description = substr($text, '0', ($end_of_second_sentence + 1));
514 $description = substr($text, '0', ($end_of_first_sentence + 1));
519 <title>$title</title>
522 <description>$description</description>
533 class notFound extends Exception {
535 public function __construct() {
536 header('HTTP/1.0 404 Not Found');
545 class captcha extends cms {
547 public function display() {
548 $challenge = $_GET['challenge'];
549 $response = $_GET['response'];
550 $remoteip = $_SERVER['REMOTE_ADDR'];
551 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
552 curl_setopt ($curl, CURLOPT_POST, 4);
553 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
554 $result = curl_exec ($curl);