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