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