rss now from view/rss.php
[dylansserver.git] / cms.php
1 <?php
2
3 abstract class cms {
4
5 private $config_file = '/etc/dylansserver.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['rss'])) {
43 return 'rss';
44 } else if (isset($_GET['challenge'])) {
45 return 'captcha';
46 }
47 }
48
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();
55 $return = array();
56 $statement->store_result();
57 $row = array();
58 $data = $statement->result_metadata();
59 $fields = array();
60 $fields[0] = &$statement;
61 while($field = $data->fetch_field()) {
62 $fields[] = &$row[$field->name];
63 }
64 call_user_func_array("mysqli_stmt_bind_result", $fields);
65 $i = 0;
66 while ($statement->fetch()) {
67 foreach ($row as $key=>$value) $return[$i][$key] = $value;
68 $i++;
69 }
70 $statement->free_result();
71 return $return;
72 }
73
74 public function init() {
75 switch (cms::determine_type()) {
76 case 'index':
77 $index = new index();
78 $index->display();
79 break;
80 case 'project':
81 $project = new project();
82 $project->display();
83 break;
84 case 'note':
85 $note = new note;
86 $note->display();
87 break;
88 case 'page':
89 $page = new page;
90 $page->display();
91 break;
92 case "rss":
93 $rss = new rss();
94 $rss->display();
95 break;
96 case 'archive':
97 $archive = new archive;
98 $archive->display();
99 break;
100 case "captcha":
101 $captcha = new captcha;
102 $captcha->display();
103 break;
104 }
105 }
106
107 }
108
109
110 class index extends cms {
111
112 public function display() {
113 require_once("view/index.php");
114 }
115
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()) {
121 echo $entry->text;
122 }
123 echo "</div>";
124 }
125
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>";
131 }
132 }
133
134 }
135
136
137 class project extends index {
138
139 protected function display_exhibits() {
140 echo "<div id='exhibit'>";
141 $sql = "SELECT text FROM projects
142 WHERE title = ?";
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);
146 echo $text;
147 echo "</div>";
148 } else {
149 throw new notFound();
150 }
151 }
152
153 }
154
155
156 class page extends cms {
157
158 private $page = 1;
159 private $offset = 0;
160 private $notes_per_page = 4;
161 private $number_of_pages = 1;
162
163 public function __construct() {
164 parent::__construct();
165 $this->page_offset();
166 }
167
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'];
175 } else {
176 throw new notFound();
177 }
178 if ($this->page > $this->number_of_pages) {
179 throw new notFound();
180 }
181 if ($this->page < 1) {
182 throw new notFound();
183 }
184 $this->offset = ($this->page - 1) * $this->notes_per_page;
185 }
186
187 public function display() {
188 require_once("view/page.php");
189 }
190
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
195 LIMIT ?, ?";
196 $result = $this->query($sql, "ii",
197 $this->offset,
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'];
208 echo <<<END_NOTE
209 <div class='note'>
210 <h1>
211 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
212 </h1>
213 $text
214 </div>
215 END_NOTE;
216 }
217 echo "</div>";
218 }
219 }
220
221
222 class note extends cms {
223
224 private $id;
225 private $comments_enabled = false;
226 private $failed_captcha;
227 public $url;
228 public $title;
229 public $year_posted;
230 public $month_posted;
231 public $day_posted;
232 public $text;
233 public $number_of_comments;
234
235 public function __construct() {
236 parent::__construct();
237 if (isset($_GET['comments'])) {
238 $this->comments_enabled = true;
239 }
240 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
241 if (isset($_GET['verify'])) {
242 $url = substr($url, 0, (strlen($url)-6));
243 }
244 $this->url = $url;
245 $sql = "SELECT title, date_posted, text, id
246 FROM notes WHERE url = ?";
247 $result = $this->query($sql, "s",
248 $_GET['note']);
249 if ($result) {
250 $entry = $result[0];
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"];
259 } else {
260 throw new notFound();
261 }
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'])) {
268 $this->verify();
269 }
270 }
271
272 public function display() {
273 require_once("view/note.php");
274 }
275
276 private function verify() {
277 if (!isset($_POST['captcha'])) {
278 require_once('includes/recaptchalib.php');
279 echo "<br>";
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;
286 }
287 }
288 if (isset($_POST['captcha']) || $resp->is_valid) {
289 $sql = ("INSERT INTO comments (date_posted, author,
290 text, note)
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',
297 $_POST['name'],
298 $_POST['text'],
299 $this->id);
300 $stmt->execute();
301 }
302 }
303
304 private function display_comment_link() {
305 if ($this->number_of_comments > 0) {
306 $anchor_text = "comments($this->number_of_comments)/";
307 } else {
308 $anchor_text = "comment?";
309 }
310 if (substr($this->url, (strlen($this->url)-1), strlen($this->url)) == '/') {
311 $url = $this->url . 'comments/';
312 } else {
313 $url = $this->url . '/comments/';
314 }
315 echo "<a id='comment_link' href='$url'>$anchor_text</a>";
316 }
317
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'>
331 $head
332 $text
333 </div>
334 END_OF_COMMENT;
335 }
336 echo "</div>";
337 }
338
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",
344 "recaptcha_div",
345 {
346 theme : 'custom',
347 custom_theme_widget: 'recaptcha_widget',
348 callback: Recaptcha.focus_response_field
349 });
350 </script>
351 END_CAPTCHA_STYLE;
352 require_once('includes/recaptchalib.php');
353 $url = $this->url . "verify";
354 echo "<form id='comment_form' method='post' action='$url'>";
355 echo <<<END_OF_FORM
356 <div id="comment">
357 <h3>comment:</h3>
358 <textarea rows="10" cols="70" name="text" id="comment_text"></textarea>
359 <h3>name:</h3>
360 <input type=text name="name" id="comment_name">
361
362 <nowiki>
363 <div id="recaptcha_widget">
364 <br>
365 <h3><b>what's this say</b>?</h3>
366 <br>
367 <div id="recaptcha_image"></div>
368 <br><br><br>
369 <span class="recaptcha_only_if_image"><br><br><br></span>
370 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
371 <br><br>
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>
377 </span>
378 </div>
379 END_OF_FORM;
380 echo recaptcha_get_html($this->recaptcha_publickey);
381 if ($this->failed_captcha) {
382 echo <<<END_OF_ERRORS
383 <div id="not_human">
384 reCAPTCHA said you're not human, <br>
385 try again?
386 </div>
387 <input id="submit" class="submit" type="submit" value="post comment">
388 </form>
389 </div>
390 END_OF_ERRORS;
391 } else {
392 echo <<<END_OF_ERRORS
393 <div id="not_human">
394 reCAPTCHA said you're not human, <br>
395 try again?
396 </div>
397 <div id="blank_comment">
398 but you didn't write anything! <br>
399 </div>
400 END_OF_ERRORS;
401 }
402 echo <<<END_OF_FORM
403 <input id="submit" class="submit" type="submit" value="post comment">
404 </form>
405 </div>
406 END_OF_FORM;
407 }
408 }
409
410
411 class archive extends cms {
412
413 public function __construct() {
414 parent::__construct();
415 }
416
417 private function check_exists() {
418 $sql = "SELECT COUNT(*) FROM notes
419 WHERE url = ?";
420 $results = $this->query($sql, "s", $_GET['note']);
421 if ($results[0]["COUNT(*)"] != 1) {
422 $this->not_found();
423 }
424 }
425
426 public function display() {
427 require_once("view/archive.php");
428 }
429
430 public function display_notes() {
431 switch (true) {
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",
438 $_GET['year']);
439 break;
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']);
448 break;
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'],
458 $_GET['day']);
459 break;
460 }
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>";
475 echo $entry['text'];
476 echo "</div>";
477 }
478 echo "</div>";
479 } else {
480 echo "<br>";
481 echo "<h1>sorry, nothing here</h2>";
482 echo "<pre>Empty set (0.00 sec)</pre>";
483 }
484 }
485
486 }
487
488
489 class rss extends cms {
490
491 public function display() {
492 require_once("view/rss.php");
493 }
494
495 public function display_items() {
496 $result = $this->db->query("SELECT date_posted, title, text, url
497 FROM notes ORDER BY date_posted DESC
498 LIMIT 5");
499 while ($entry = $result->fetch_object()) {
500 $title = $entry->title;
501 $date_posted = $entry->date_posted;
502 $url = "http://dylansserver.com/note/" . $entry->url;
503 $text = $entry->text;
504 $text = strip_tags($text);
505 $end_of_first_sentence = strpos($text, '.');
506 if ($end_of_first_sentence) {
507 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
508 if ($end_of_second_sentence) {
509 $description = substr($text, '0', ($end_of_second_sentence + 1));
510 } else {
511 $description = substr($text, '0', ($end_of_first_sentence + 1));
512 }
513 }
514 echo "<item>";
515 echo " <title>$title</title>";
516 echo " <link>$url</link>";
517 echo " <guid>$url</guid>";
518 echo " <description>$description</description>";
519 echo "</item>";
520 }
521 }
522 }
523
524
525 class notFound extends Exception {
526
527 public function __construct() {
528 header('HTTP/1.0 404 Not Found');
529 ob_end_clean();
530 include('404.php');
531 exit();
532 }
533
534 }
535
536
537 class captcha extends cms {
538
539 public function display() {
540 $challenge = $_GET['challenge'];
541 $response = $_GET['response'];
542 $remoteip = $_SERVER['REMOTE_ADDR'];
543 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
544 curl_setopt ($curl, CURLOPT_POST, 4);
545 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
546 $result = curl_exec ($curl);
547 curl_close ($curl);
548 }
549
550 }
551
552 ?>