8fe04325d6fcb5ec4aca99db08337b2fe99a9060
4 private $config_file = '/etc/dylanstestserver.ini';
7 public function __construct() {
8 $config = parse_ini_file($this->config_file
);
9 $this->db
= new mysqli(
14 if (mysqli_connect_errno()) {
15 echo "Problem connecting to database: ";
16 echo mysqli_connect_error();
22 public static function determine_type() {
23 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
25 } else if (isset($_GET['year'])) {
27 } else if (isset($_GET['note'])) {
29 } else if ($_SERVER['REQUEST_URI'] == '/') {
31 } else if (isset($_GET['project'])) {
33 } else if (isset($_GET['rss'])) {
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();
45 $statement->store_result();
47 $data = $statement->result_metadata();
49 $fields[0] = &$statement;
50 while($field = $data->fetch_field()) {
51 $fields[] = &$row[$field->name
];
53 call_user_func_array("mysqli_stmt_bind_result", $fields);
55 while ($statement->fetch()) {
56 foreach ($row as $key1=>$value1) $return[$i][$key1] = $value1;
59 $statement->free_result();
63 public function display_head($title = "dylanstestserver",
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&ucn_task=conformance#";
72 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
73 "http://www.w3.org/TR/html4/loose.dtd">
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">
82 <link rel="icon" href="favicon.ico" type="image/png">
91 <img src="/images/dylanstestserver.png" alt="dylanstestserver"
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>
108 public function display_close($show_contact = true) {
110 $this->display_contact();
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
133 <h3>things i've done for others:</h3>
137 "http://activehamptons.com">activehamptons.com</a></li>
140 "http://transfishing.com">transfishing.com</a></li>
143 <h3>something i've worked on:</h3>
147 "http://tempositions.com">tempositions.com</a></li>
150 <h3>my repositories:</h3>
153 <li><a href= "git">git://dylanstestserver.com</a></li>
160 "/notes/">here</a></li>
165 // Because of the CSS necessary for the animations,
166 // the contact link needs to be in #portfolio to clear
169 $this->display_contact();
172 $this->display_close($show_contact = false);
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()) {
185 private function list_projects() {
188 <h3>my projects:</h3>
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>";
199 class project extends index {
200 protected function display_exhibits() {
201 echo "<div id=\"exhibit\">";
202 $sql = "SELECT text FROM projects
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);
210 throw new notFound();
215 class page extends cms {
218 private $notes_per_page = 4;
219 private $number_of_pages = 1;
221 public function __construct() {
222 parent::__construct();
223 $this->page_offset();
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'];
234 throw new notFound();
236 if ($this->page > $this->number_of_pages) {
237 throw new notFound();
239 if ($this->page < 1) {
240 throw new notFound();
242 $this->offset = ($this->page - 1) * $this->notes_per_page;
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
251 $result = $this->query($sql, "ii",
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>";
268 $this->write_navigation();
269 $this->display_close();
272 private function write_navigation() {
273 echo "<div id=\"navigation\">";
276 $previous_page = $this->page - 1;
277 echo "<a href=\"/notes/page/$previous_page\">prev</a>";
279 if($this->page < $this->number_of_pages) {
280 $forward_page = $this->page + 1;
281 echo " <a href=\"/notes/page/$forward_page\">next</a>";
289 class note extends cms {
291 public function __construct() {
292 parent::__construct();
293 $this->check_exists();
296 private function check_exists() {
297 $sql = "SELECT COUNT(*) FROM notes
299 $results = $this->query($sql, "s", $_GET['note']);
300 if ($results[0]["COUNT(*)"] != 1) {
301 throw new notFound();
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",
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>";
321 $this->write_navigation();
322 $this->display_close();
325 private function write_navigation() {
327 echo "<div id=\"navigation\">";
329 echo "<a href=\"/notes/\">notes</a>/";
336 class archive extends cms {
338 public function __construct() {
339 parent::__construct();
342 private function check_exists() {
343 $sql = "SELECT COUNT(*) FROM notes
345 $results = $this->query($sql, "s", $_GET['note']);
346 if ($results[0]["COUNT(*)"] != 1) {
351 public function display() {
352 // this really needs its own pagination...
353 // there should be a class for that.
354 $this->display_head();
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",
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']);
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'],
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>";
401 $this->write_navigation();
404 echo "<h2 style=\"font-family:sans-serif;\">sorry, nothing here</h2>";
405 echo "<pre>Empty set (0.00 sec)</pre>";
407 $this->display_close();
410 private function write_navigation() {
412 echo "<div id=\"navigation\">";
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
427 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
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" />
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));
446 $description = substr($text, '0', ($end_of_first_sentence + 1));
451 <title>$title</title>
454 <description>$description</description>
465 class notFound extends Exception {
466 public function __construct() {
467 header("HTTP/1.0 404 Not Found");
474 ## now actually do something:
475 switch (cms::determine_type()) {
477 $index = new index();
481 $project = new project();
493 $archive = new archive;