8fe04325d6fcb5ec4aca99db08337b2fe99a9060
[dylansserver.git] / index.php
1 <?php
2
3 abstract class cms {
4 private $config_file = '/etc/dylanstestserver.ini';
5 protected $db;
6
7 public function __construct() {
8 $config = parse_ini_file($this->config_file);
9 $this->db = new mysqli(
10 $config['domain'],
11 $config['user'],
12 $config['password'],
13 $config['database']);
14 if (mysqli_connect_errno()) {
15 echo "Problem connecting to database: ";
16 echo mysqli_connect_error();
17 exit();
18 }
19 ob_start();
20 }
21
22 public static function determine_type() {
23 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
24 return 'page';
25 } else if (isset($_GET['year'])) {
26 return 'archive';
27 } else if (isset($_GET['note'])) {
28 return 'note';
29 } else if ($_SERVER['REQUEST_URI'] == '/') {
30 return 'index';
31 } else if (isset($_GET['project'])) {
32 return 'project';
33 } else if (isset($_GET['rss'])) {
34 return 'rss';
35 }
36 }
37
38 public function query() {
39 $args = func_get_args();
40 $statement = $this->db->prepare($args[0]);
41 $args = array_slice($args, 1);
42 call_user_func_array(array($statement, 'bind_param'), &$args);
43 $statement->execute();
44 $return = array();
45 $statement->store_result();
46 $row = array();
47 $data = $statement->result_metadata();
48 $fields = array();
49 $fields[0] = &$statement;
50 while($field = $data->fetch_field()) {
51 $fields[] = &$row[$field->name];
52 }
53 call_user_func_array("mysqli_stmt_bind_result", $fields);
54 $i = 0;
55 while ($statement->fetch()) {
56 foreach ($row as $key1=>$value1) $return[$i][$key1] = $value1;
57 $i++;
58 }
59 $statement->free_result();
60 return $return;
61 }
62
63 public function display_head($title = "dylanstestserver",
64 $home_link = "/") {
65 $scripts = "";
66 $stylesheets = "<link href=\"/includes/style.css\" rel=\"stylesheet\" type=\"text/css\">";
67 if (cms::determine_type() == "index") {
68 $scripts = "<script type=\"text/javascript\" src=\"/includes/all.js\"></script>";
69 $home_link = "http://validator.w3.org/unicorn/check?ucn_uri=dylanstestserver.com&amp;ucn_task=conformance#";
70 }
71 echo <<<END_OF_HEAD
72 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
73 "http://www.w3.org/TR/html4/loose.dtd">
74
75 <html>
76 <head>
77 <meta name="generator" content=
78 "HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
79 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
80
81 <title>$title</title>
82 <link rel="icon" href="favicon.ico" type="image/png">
83 $stylesheets
84 $scripts
85 </head>
86
87 <body>
88 <div id="structure">
89 <div id="banner">
90 <a href="$home_link">
91 <img src="/images/dylanstestserver.png" alt="dylanstestserver"
92 border="0"></a>
93 </div>
94
95 <div id="content">
96 END_OF_HEAD;
97 }
98
99 public function display_contact() {
100 echo <<<END_OF_CONTACT
101 <div id="contact_me"><h1><a href=
102 "mailto:dylan@psu.edu">dylan</a></h1><a href=
103 "mailto:dylan@psu.edu">@psu.edu</a>
104 </div>
105 END_OF_CONTACT;
106 }
107
108 public function display_close($show_contact = true) {
109 if ($show_contact) {
110 $this->display_contact();
111 }
112 echo <<<END_OF_CLOSE
113 </div>
114 <br>
115 <br>
116 </div>
117 </body>
118 </html>
119 END_OF_CLOSE;
120 ob_flush();
121 }
122
123 }
124
125 class index extends cms {
126 public function display() {
127 $this->display_head();
128 $this->display_exhibits();
129 echo "<ul id=\"portfolio\" style=\"text-align:right\">";
130 $this->list_projects();
131 echo <<<OTHER_PROJECTS
132 <li>
133 <h3>things i've done for others:</h3>
134 </li>
135
136 <li><a href=
137 "http://activehamptons.com">activehamptons.com</a></li>
138
139 <li><a href=
140 "http://transfishing.com">transfishing.com</a></li>
141
142 <li>
143 <h3>something i've worked on:</h3>
144 </li>
145
146 <li><a href=
147 "http://tempositions.com">tempositions.com</a></li>
148
149 <li>
150 <h3>my repositories:</h3>
151 </li>
152
153 <li><a href= "git">git://dylanstestserver.com</a></li>
154
155 <li>
156 <h3>some notes:</h3>
157 </li>
158
159 <li><a href=
160 "/notes/">here</a></li>
161
162 <li>
163 </li>
164 OTHER_PROJECTS;
165 // Because of the CSS necessary for the animations,
166 // the contact link needs to be in #portfolio to clear
167 // the floats.
168 echo "<li>";
169 $this->display_contact();
170 echo "</li>";
171 echo "</ul>";
172 $this->display_close($show_contact = false);
173 }
174
175 protected function display_exhibits() {
176 echo "<div id=\"exhibit\">";
177 $sql = "SELECT text FROM projects";
178 $result = $this->db->query($sql);
179 while ($entry = $result->fetch_object()) {
180 echo $entry->text;
181 }
182 echo "</div>";
183 }
184
185 private function list_projects() {
186 echo <<<HEREDOC
187 <li>
188 <h3>my projects:</h3>
189 </li>
190 HEREDOC;
191 $sql = "SELECT title FROM projects";
192 $result = $this->db->query($sql);
193 while ($entry = $result->fetch_object()) {
194 echo "<li><a class=\"tab\" href=\"$entry->title\">$entry->title</a></li>";
195 }
196 }
197 }
198
199 class project extends index {
200 protected function display_exhibits() {
201 echo "<div id=\"exhibit\">";
202 $sql = "SELECT text FROM projects
203 WHERE title = ?";
204 $result = $this->query($sql, "s", $_GET['project']);
205 if ($result = $result[0]['text']) {
206 $text = str_replace("class=\"exhibit\"", "class=\"exhibit\" style=\"display:block;\"", $result);
207 echo $text;
208 echo "</div>";
209 } else {
210 throw new notFound();
211 }
212 }
213 }
214
215 class page extends cms {
216 private $page = 1;
217 private $offset = 0;
218 private $notes_per_page = 4;
219 private $number_of_pages = 1;
220
221 public function __construct() {
222 parent::__construct();
223 $this->page_offset();
224 }
225
226 private function page_offset() {
227 $sql = "SELECT COUNT(*) FROM notes";
228 $result = $this->db->query($sql);
229 $result = $result->fetch_array();
230 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
231 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
232 $this->page = (int) $_GET['page'];
233 } else {
234 throw new notFound();
235 }
236 if ($this->page > $this->number_of_pages) {
237 throw new notFound();
238 }
239 if ($this->page < 1) {
240 throw new notFound();
241 }
242 $this->offset = ($this->page - 1) * $this->notes_per_page;
243 }
244
245 public function display() {
246 $this->display_head();
247 echo "<div id=\"notes\">";
248 $sql = "SELECT date_posted, title, url, text
249 FROM notes ORDER BY date_posted DESC
250 LIMIT ?, ?";
251 $result = $this->query($sql, "ii",
252 $this->offset,
253 $this->notes_per_page);
254 foreach ($result as $row => $entry) {
255 $title = $entry['title'];
256 $url = '/note/' . $entry['url'];
257 $date_posted = explode("-", $entry['date_posted']);
258 $year_posted = $date_posted[0];
259 $month_posted = $date_posted[1];
260 $datetime_posted = explode(' ', $date_posted[2]);
261 $day_posted = $datetime_posted[0];
262 echo "<div class=\"note\">";
263 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
264 echo $entry['text'];
265 echo "</div>";
266 }
267 echo "</div>";
268 $this->write_navigation();
269 $this->display_close();
270 }
271
272 private function write_navigation() {
273 echo "<div id=\"navigation\">";
274 echo "<h2>";
275 if($this->page > 1){
276 $previous_page = $this->page - 1;
277 echo "<a href=\"/notes/page/$previous_page\">prev</a>";
278 }
279 if($this->page < $this->number_of_pages) {
280 $forward_page = $this->page + 1;
281 echo " <a href=\"/notes/page/$forward_page\">next</a>";
282 }
283 echo "</h2>";
284 echo "</div>";
285 }
286
287 }
288
289 class note extends cms {
290
291 public function __construct() {
292 parent::__construct();
293 $this->check_exists();
294 }
295
296 private function check_exists() {
297 $sql = "SELECT COUNT(*) FROM notes
298 WHERE url = ?";
299 $results = $this->query($sql, "s", $_GET['note']);
300 if ($results[0]["COUNT(*)"] != 1) {
301 throw new notFound();
302 }
303 }
304
305 public function display() {
306 $this->display_head();
307 $sql = "SELECT title, date_posted, text
308 FROM notes WHERE url = ?";
309 $result = $this->query($sql, "s",
310 $_GET['note']);
311 $entry = $result[0];
312 $title = $entry["title"];
313 $date_posted = explode("-", $entry["date_posted"]);
314 $year_posted = $date_posted[0];
315 $month_posted = $date_posted[1];
316 $datetime_posted = explode(' ', $date_posted[2]);
317 $day_posted = $datetime_posted[0];
318 echo "<div id=\"note\">";
319 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span>$title</h2>";
320 echo $entry['text'];
321 $this->write_navigation();
322 $this->display_close();
323 }
324
325 private function write_navigation() {
326 echo "<br>";
327 echo "<div id=\"navigation\">";
328 echo "<h2>";
329 echo "<a href=\"/notes/\">notes</a>/";
330 echo "</h2>";
331 echo "</div>";
332 }
333 }
334
335
336 class archive extends cms {
337
338 public function __construct() {
339 parent::__construct();
340 }
341
342 private function check_exists() {
343 $sql = "SELECT COUNT(*) FROM notes
344 WHERE url = ?";
345 $results = $this->query($sql, "s", $_GET['note']);
346 if ($results[0]["COUNT(*)"] != 1) {
347 $this->not_found();
348 }
349 }
350
351 public function display() {
352 // this really needs its own pagination...
353 // there should be a class for that.
354 $this->display_head();
355 switch (true) {
356 case (isset($_GET['year']) && !isset($_GET['month'])
357 && !isset($_GET['day'])):
358 $sql = "SELECT title, url, date_posted, text
359 FROM notes WHERE YEAR(date_posted) = ?
360 ORDER BY date_posted DESC";
361 $result = $this->query($sql, "d",
362 $_GET['year']);
363 break;
364 case (isset($_GET['year']) && isset($_GET['month'])
365 && !isset($_GET['day'])):
366 $sql = "SELECT title, url, date_posted, text
367 FROM notes WHERE YEAR(date_posted) = ?
368 AND MONTH(date_posted) = ?
369 ORDER BY date_posted DESC";
370 $result = $this->query($sql, "dd",
371 $_GET['year'], $_GET['month']);
372 break;
373 case (isset($_GET['year']) && isset($_GET['month'])
374 && isset($_GET['day'])):
375 $sql = "SELECT title, url, date_posted, text
376 FROM notes WHERE YEAR(date_posted) = ?
377 AND MONTH(date_posted) = ?
378 AND DAY(date_posted) = ?
379 ORDER BY date_posted DESC";
380 $result = $this->query($sql, "ddd",
381 $_GET['year'], $_GET['month'],
382 $_GET['day']);
383 break;
384 }
385 if (count($result) >= 1) {
386 echo "<div id=\"notes\">";
387 foreach ($result as $row => $entry) {
388 $title = $entry['title'];
389 $url = '/note/' . $entry['url'];
390 $date_posted = explode("-", $entry['date_posted']);
391 $year_posted = $date_posted[0];
392 $month_posted = $date_posted[1];
393 $datetime_posted = explode(' ', $date_posted[2]);
394 $day_posted = $datetime_posted[0];
395 echo "<div class=\"note\">";
396 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
397 echo $entry['text'];
398 echo "</div>";
399 }
400 echo "</div>";
401 $this->write_navigation();
402 } else {
403 echo "<br>";
404 echo "<h2 style=\"font-family:sans-serif;\">sorry, nothing here</h2>";
405 echo "<pre>Empty set (0.00 sec)</pre>";
406 }
407 $this->display_close();
408 }
409
410 private function write_navigation() {
411 echo "<br>";
412 echo "<div id=\"navigation\">";
413 echo "<h2>";
414 // fill me in!
415 echo "</h2>";
416 echo "</div>";
417 }
418 }
419
420
421 class rss extends cms {
422 public function display() {
423 $result = $this->db->query("SELECT date_posted, title, text, url
424 FROM notes ORDER BY date_posted DESC
425 LIMIT 5");
426 echo <<<END_OF_ENTRY
427 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
428 <channel>
429 <title>dylanstestserver.com/notes/rss</title>
430 <link>http://dylanstestserver.com/notes</link>
431 <description>dylanstestserver.com/notes/rss</description>
432 <atom:link href="http://dylanstestserver.com/notes/rss" rel="self" type="application/rss+xml" />
433 END_OF_ENTRY;
434 while ($entry = $result->fetch_object()) {
435 $title = $entry->title;
436 $date_posted = $entry->date_posted;
437 $url = "http://dylanstestserver.com/note/" . $entry->url;
438 $text = $entry->text;
439 $text = strip_tags($text);
440 $end_of_first_sentence = strpos($text, '.');
441 if ($end_of_first_sentence) {
442 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
443 if ($end_of_second_sentence) {
444 $description = substr($text, '0', ($end_of_second_sentence + 1));
445 } else {
446 $description = substr($text, '0', ($end_of_first_sentence + 1));
447 }
448 }
449 echo <<<END_OF_ENTRY
450 <item>
451 <title>$title</title>
452 <link>$url</link>
453 <guid>$url</guid>
454 <description>$description</description>
455 </item>
456 END_OF_ENTRY;
457 }
458 echo "</channel>";
459 echo "</rss>";
460
461 }
462 }
463
464
465 class notFound extends Exception {
466 public function __construct() {
467 header("HTTP/1.0 404 Not Found");
468 ob_end_clean();
469 include("404.php");
470 exit();
471 }
472 }
473
474 ## now actually do something:
475 switch (cms::determine_type()) {
476 case "index":
477 $index = new index();
478 $index->display();
479 break;
480 case "project":
481 $project = new project();
482 $project->display();
483 break;
484 case "note":
485 $note = new note;
486 $note->display();
487 break;
488 case "page":
489 $page = new page;
490 $page->display();
491 break;
492 case "archive":
493 $archive = new archive;
494 $archive->display();
495 break;
496 case "rss":
497 $rss = new rss();
498 $rss->display();
499 }
500
501 ?>