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