f3902b336c22cb47e922adf794bf86b80cabcba5
5 private $config_file = '/etc/dylansserver.ini';
7 protected $recaptcha_publickey;
8 protected $recaptcha_privatekey;
13 public function __construct() {
14 $config = parse_ini_file($this->config_file
, true);
15 $this->db
= new mysqli(
16 $config['database']['domain'],
17 $config['database']['user'],
18 $config['database']['password'],
19 $config['database']['database']);
20 if (mysqli_connect_errno()) {
21 echo "Problem connecting to database: ";
22 echo mysqli_connect_error();
25 $this->recaptcha_publickey
= $config['recaptcha']['publickey'];
26 $this->recaptcha_privatekey
= $config['recaptcha']['privatekey'];
27 $this->title
= $config['site']['default_title'];
28 $this->home_link
= $config['site']['home_link'];
32 public static function determine_type() {
33 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
35 } else if (isset($_GET['year'])) {
37 } else if (isset($_GET['note'])) {
39 } else if ($_SERVER['REQUEST_URI'] == '/') {
41 } else if (isset($_GET['project'])) {
43 } else if (isset($_GET['rss'])) {
45 } else if (isset($_GET['challenge'])) {
50 public function query() {
51 $args = func_get_args();
52 $statement = $this->db
->prepare($args[0]);
53 $args = array_slice($args, 1);
54 call_user_func_array(array($statement, 'bind_param'), &$args);
55 $statement->execute();
57 $statement->store_result();
59 $data = $statement->result_metadata();
61 $fields[0] = &$statement;
62 while($field = $data->fetch_field()) {
63 $fields[] = &$row[$field->name
];
65 call_user_func_array("mysqli_stmt_bind_result", $fields);
67 while ($statement->fetch()) {
68 foreach ($row as $key=>$value) $return[$i][$key] = $value;
71 $statement->free_result();
75 public function init() {
76 switch (cms
::determine_type()) {
82 $project = new project();
98 $archive = new archive
;
102 $captcha = new captcha
;
111 class index
extends cms
{
113 public function display() {
114 require_once("view/index.php");
117 protected function display_exhibits() {
118 echo "<div id='exhibit'>";
119 $sql = "SELECT text FROM projects ORDER BY rank";
120 $result = $this->db
->query($sql);
121 while ($entry = $result->fetch_object()) {
127 private function list_projects() {
128 $sql = "SELECT title FROM projects ORDER BY rank";
129 $result = $this->db
->query($sql);
130 while ($entry = $result->fetch_object()) {
131 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
138 class project
extends index
{
140 protected function display_exhibits() {
141 echo "<div id='exhibit'>";
142 $sql = "SELECT text FROM projects
144 $result = $this->query($sql, "s", $_GET['project']);
145 if ($result = $result[0]['text']) {
146 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
150 throw new notFound();
157 class page
extends cms
{
161 private $notes_per_page = 4;
162 private $number_of_pages = 1;
164 public function __construct() {
165 parent
::__construct();
166 $this->page_offset();
168 <script type='text/javascript' src='/includes/syntax/scripts/shCore.js'></script>
169 <script type='text/javascript' src='/includes/syntax/scripts/shAutoloader.js'></script>
170 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shCore.css'>
171 <link type='text/css' rel='stylesheet' href='/includes/syntax/styles/shThemeDefault.css'>
172 <script type='text/javascript'>
173 function highlight() {
174 SyntaxHighlighter.autoloader(
175 'js /includes/syntax/scripts/shBrushJScript.js',
176 'bash /includes/syntax/scripts/shBrushBash.js',
177 'sql /includes/syntax/scripts/shBrushSql.js',
178 'cpp /includes/syntax/scripts/shBrushCpp.js');
179 SyntaxHighlighter.defaults['gutter'] = false;
180 SyntaxHighlighter.defaults['toolbar'] = false;
181 SyntaxHighlighter.all();
187 private function page_offset() {
188 $sql = "SELECT COUNT(*) FROM notes";
189 $result = $this->db
->query($sql);
190 $result = $result->fetch_array();
191 $this->number_of_pages
= ceil($result[0] / $this->notes_per_page
);
192 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
193 $this->page
= (int) $_GET['page'];
195 throw new notFound();
197 if ($this->page
> $this->number_of_pages
) {
198 throw new notFound();
200 if ($this->page
< 1) {
201 throw new notFound();
203 $this->offset
= ($this->page
- 1) * $this->notes_per_page
;
206 public function display() {
207 require_once("view/page.php");
210 public function display_notes() {
211 echo "<div id='notes'>";
212 $sql = "SELECT date_posted, title, url, text
213 FROM notes ORDER BY date_posted DESC
215 $result = $this->query($sql, "ii",
217 $this->notes_per_page
);
218 foreach ($result as $row => $entry) {
219 $title = $entry['title'];
220 $url = '/note/' . $entry['url'];
221 $date_posted = explode("-", $entry['date_posted']);
222 $year_posted = $date_posted[0];
223 $month_posted = $date_posted[1];
224 $datetime_posted = explode(' ', $date_posted[2]);
225 $day_posted = $datetime_posted[0];
226 $text = $entry['text'];
230 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
241 class note extends cms {
244 private $comments_enabled = false;
245 private $failed_captcha;
249 public $month_posted;
252 public $number_of_comments;
254 public function __construct() {
255 parent::__construct();
256 if (isset($_GET['comments'])) {
257 $this->comments_enabled = true;
259 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
260 if (isset($_GET['verify'])) {
261 $url = substr($url, 0, (strlen($url)-6));
264 $sql = "SELECT title, date_posted, text, id
265 FROM notes WHERE url = ?";
266 $result = $this->query($sql, "s",
270 $this->id = $entry["id"];
271 $this->title = $entry["title"];
272 $date_posted = explode("-", $entry["date_posted"]);
273 $this->year_posted = $date_posted[0];
274 $this->month_posted = $date_posted[1];
275 $datetime_posted = explode(' ', $date_posted[2]);
276 $this->day_posted = $datetime_posted[0];
277 $this->text = $entry["text"];
279 throw new notFound();
281 $sql = "SELECT COUNT(*) FROM comments
282 WHERE note = $this->id";
283 $result = $this->db->query($sql);
284 $result = $result->fetch_array();
285 $this->number_of_comments = $result[0];
286 if (isset($_GET['verify'])) {
291 public function display() {
292 require_once("view/note.php");
295 private function verify() {
296 if (!isset($_POST['captcha'])) {
297 require_once('includes/recaptchalib.php');
299 $resp = recaptcha_check_answer ($this->recaptcha_privatekey,
300 $_SERVER["REMOTE_ADDR"],
301 $_POST["recaptcha_challenge_field"],
302 $_POST["recaptcha_response_field"]);
303 if (!$resp->is_valid) {
304 $this->failed_captcha = true;
307 if (isset($_POST['captcha']) || $resp->is_valid) {
308 $sql = ("INSERT INTO comments (date_posted, author,
310 VALUES(NOW(), ?, ?, ?)");
311 $stmt = $this->db->prepare($sql);
312 // Checks are needed here (no blank text,
313 // and a default author needs to be set
314 // for no-javascript users.
315 $stmt->bind_param('sss',
323 private function display_comment_link() {
324 if ($this->number_of_comments > 0) {
325 $anchor_text = "comments($this->number_of_comments)/";
327 $anchor_text = "comment?";
329 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
330 $url = $this->url . 'comments/';
332 $url = $this->url . '/comments/';
334 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
337 private function display_comments() {
338 echo "<div id='comments'>";
339 $sql= "SELECT date_posted, author, text
340 FROM comments WHERE note = ?
341 ORDER BY date_posted DESC";
342 $result = $this->query($sql, 'd', $this->id);
343 foreach ($result as $row => $entry) {
344 $date_posted = $entry['date_posted'];
345 $author = $entry['author'];
346 $text = htmlspecialchars($entry['text']);
347 $head = "<h3>" . htmlspecialchars($author) . "</h3>";
348 echo <<<END_OF_COMMENT
349 <div class='comment'>
358 private function display_comment_form() {
359 $publickey = $this->recaptcha_publickey;
360 echo <<<END_CAPTCHA_STYLE
361 <script type="text/javascript">
362 Recaptcha.create("$publickey",
366 custom_theme_widget: 'recaptcha_widget',
367 callback: Recaptcha.focus_response_field
371 require_once('includes/recaptchalib.php');
372 $url = $this->url . "verify";
373 echo "<form id='comment_form' method='post' action='$url'>";
377 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
379 <input type=text name="name" id="comment_name">
382 <div id="recaptcha_widget">
384 <h3><b>what's this say</b>?</h3>
386 <div id="recaptcha_image"></div>
388 <span class="recaptcha_only_if_image"><br><br><br></span>
389 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
391 <h3 class="recaptcha_only_if_audio"><b>enter the numbers you hear</b>:</h3>
392 <span class="recaptcha_help">
393 <a href="javascript:Recaptcha.reload()">another?</a> /
394 <span class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">audio?</a> /</span>
395 <span class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">image?</a></span><a href="javascript:Recaptcha.showhelp()">help?</a>
399 echo recaptcha_get_html($this->recaptcha_publickey);
400 if ($this->failed_captcha) {
401 echo <<<END_OF_ERRORS
403 reCAPTCHA said you're not human, <br>
406 <input id="submit" class="submit" type="submit" value="post comment">
411 echo <<<END_OF_ERRORS
413 reCAPTCHA said you're not human, <br>
416 <div id="blank_comment">
417 but you didn't write anything! <br>
422 <input id="submit" class="submit" type="submit" value="post comment">
430 class archive extends cms {
432 public function __construct() {
433 parent::__construct();
436 private function check_exists() {
437 $sql = "SELECT COUNT(*) FROM notes
439 $results = $this->query($sql, "s", $_GET['note']);
440 if ($results[0]["COUNT(*)"] != 1) {
445 public function display() {
446 require_once("view/archive.php");
449 public function display_notes() {
451 case (isset($_GET['year']) && !isset($_GET['month'])
452 && !isset($_GET['day'])):
453 $sql = "SELECT title, url, date_posted, text
454 FROM notes WHERE YEAR(date_posted) = ?
455 ORDER BY date_posted DESC";
456 $result = $this->query($sql, "d",
459 case (isset($_GET['year']) && isset($_GET['month'])
460 && !isset($_GET['day'])):
461 $sql = "SELECT title, url, date_posted, text
462 FROM notes WHERE YEAR(date_posted) = ?
463 AND MONTH(date_posted) = ?
464 ORDER BY date_posted DESC";
465 $result = $this->query($sql, "dd",
466 $_GET['year'], $_GET['month']);
468 case (isset($_GET['year']) && isset($_GET['month'])
469 && isset($_GET['day'])):
470 $sql = "SELECT title, url, date_posted, text
471 FROM notes WHERE YEAR(date_posted) = ?
472 AND MONTH(date_posted) = ?
473 AND DAY(date_posted) = ?
474 ORDER BY date_posted DESC";
475 $result = $this->query($sql, "ddd",
476 $_GET['year'], $_GET['month'],
480 if (count($result) >= 1) {
481 echo "<div id='notes'>";
482 foreach ($result as $row => $entry) {
483 $title = $entry['title'];
484 $url = '/note/' . $entry['url'];
485 $date_posted = explode("-", $entry['date_posted']);
486 $year_posted = $date_posted[0];
487 $month_posted = $date_posted[1];
488 $datetime_posted = explode(' ', $date_posted[2]);
489 $day_posted = $datetime_posted[0];
490 echo "<div class='note'>";
491 echo "<h1><span class='date'>";
492 echo "$year_posted/$month_posted/$day_posted/";
493 echo "</span><a href='$url'>$title</a></h1>";
500 echo "<h1>sorry, nothing here</h2>";
501 echo "<pre>Empty set (0.00 sec)</pre>";
508 class rss extends cms {
509 public function display() {
510 $result = $this->db->query("SELECT date_posted, title, text, url
511 FROM notes ORDER BY date_posted DESC
514 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
516 <title>dylansserver.com/notes/rss</title>
517 <link>http://dylansserver.com/notes</link>
518 <description>dylansserver.com/notes/rss</description>
519 <atom:link href="http://dylansserver.com/notes/rss" rel="self" type="application/rss+xml" />
521 while ($entry = $result->fetch_object()) {
522 $title = $entry->title;
523 $date_posted = $entry->date_posted;
524 $url = "http://dylansserver.com/note/" . $entry->url;
525 $text = $entry->text;
526 $text = strip_tags($text);
527 $end_of_first_sentence = strpos($text, '.');
528 if ($end_of_first_sentence) {
529 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
530 if ($end_of_second_sentence) {
531 $description = substr($text, '0', ($end_of_second_sentence + 1));
533 $description = substr($text, '0', ($end_of_first_sentence + 1));
538 <title>$title</title>
541 <description>$description</description>
552 class notFound extends Exception {
554 public function __construct() {
555 header('HTTP/1.0 404 Not Found');
564 class captcha extends cms {
566 public function display() {
567 $challenge = $_GET['challenge'];
568 $response = $_GET['response'];
569 $remoteip = $_SERVER['REMOTE_ADDR'];
570 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
571 curl_setopt ($curl, CURLOPT_POST, 4);
572 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
573 $result = curl_exec ($curl);