mv includes/cms.php 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_close(){
97 echo <<<END_OF_CLOSE
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 </div>
103 <br>
104 <br>
105 </div>
106 </body>
107 </html>
108 END_OF_CLOSE;
109 ob_flush();
110 }
111
112 }
113
114 class index extends cms {
115 public function display() {
116 $this->display_head();
117 $this->display_exhibits();
118 echo "<ul id=\"portfolio\" style=\"text-align:right\">";
119 $this->list_projects();
120 echo <<<OTHER_PROJECTS
121 <li>
122 <h3>things i've done for others:</h3>
123 </li>
124
125 <li><a href=
126 "http://activehamptons.com">activehamptons.com</a></li>
127
128 <li><a href=
129 "http://transfishing.com">transfishing.com</a></li>
130
131 <li>
132 <h3>something i've worked on:</h3>
133 </li>
134
135 <li><a href=
136 "http://tempositions.com">tempositions.com</a></li>
137
138 <li>
139 <h3>my repositories:</h3>
140 </li>
141
142 <li><a href=
143 "git">git://dylanstestserver.com</a></li>
144
145 <li>
146 <h3>some notes:</h3>
147 </li>
148
149 <li><a href=
150 "/notes/">here</a></li>
151
152 <li>
153 </li>
154 </ul>
155 OTHER_PROJECTS;
156 $this->display_close();
157 }
158
159 protected function display_exhibits() {
160 echo "<div id=\"exhibit\">";
161 $sql = "SELECT text FROM projects";
162 $result = $this->db->query($sql);
163 while ($entry = $result->fetch_object()) {
164 echo $entry->text;
165 }
166 echo "</div>";
167 }
168
169 private function list_projects() {
170 echo "<div id=\"exhibit\">";
171 echo <<<HEREDOC
172 <li>
173 <h3>my projects:</h3>
174 </li>
175 HEREDOC;
176 $sql = "SELECT title FROM projects";
177 $result = $this->db->query($sql);
178 while ($entry = $result->fetch_object()) {
179 echo "<li><a class=\"tab\" href=\"$entry->title\">$entry->title</a></li>";
180 }
181 }
182 }
183
184 class project extends index {
185 protected function display_exhibits() {
186 echo "<div id=\"exhibit\">";
187 $sql = "SELECT text FROM projects
188 WHERE title = ?";
189 $result = $this->query($sql, "s", $_GET['project']);
190 if ($result = $result[0]['text']) {
191 $text = str_replace("class=\"exhibit\"", "class=\"exhibit\" style=\"display:block;\"", $result);
192 echo $text;
193 echo "</div>";
194 } else {
195 throw new notFound();
196 }
197 }
198 }
199
200 class page extends cms {
201 private $page = 1;
202 private $offset = 0;
203 private $notes_per_page = 4;
204 private $number_of_pages = 1;
205
206 public function __construct() {
207 parent::__construct();
208 $this->page_offset();
209 }
210
211 private function page_offset() {
212 $sql = "SELECT COUNT(*) FROM notes";
213 $result = $this->db->query($sql);
214 $result = $result->fetch_array();
215 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
216 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
217 $this->page = (int) $_GET['page'];
218 } else {
219 throw new notFound();
220 }
221 if ($this->page > $this->number_of_pages) {
222 throw new notFound();
223 }
224 if ($this->page < 1) {
225 throw new notFound();
226 }
227 $this->offset = ($this->page - 1) * $this->notes_per_page;
228 }
229
230 public function display() {
231 $this->display_head();
232 echo "<div id=\"notes\">";
233 $sql = "SELECT date_posted, title, url, text
234 FROM notes ORDER BY date_posted DESC
235 LIMIT ?, ?";
236 $result = $this->query($sql, "ii",
237 $this->offset,
238 $this->notes_per_page);
239 foreach ($result as $row => $entry) {
240 $title = $entry['title'];
241 $url = '/note/' . $entry['url'];
242 $date_posted = explode("-", $entry['date_posted']);
243 $year_posted = $date_posted[0];
244 $month_posted = $date_posted[1];
245 $datetime_posted = explode(' ', $date_posted[2]);
246 $day_posted = $datetime_posted[0];
247 echo "<div class=\"note\">";
248 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span><a href=\"$url\">$title</a></h2>";
249 echo $entry['text'];
250 echo "</div>";
251 }
252 echo "</div>";
253 $this->write_navigation();
254 $this->display_close();
255 }
256
257 private function write_navigation() {
258 echo "<div id=\"navigation\">";
259 echo "<h2>";
260 if($this->page > 1){
261 $previous_page = $this->page - 1;
262 echo "<a href=\"/notes/page/$previous_page\">prev</a>";
263 }
264 if($this->page < $this->number_of_pages) {
265 $forward_page = $this->page + 1;
266 echo " <a href=\"/notes/page/$forward_page\">next</a>";
267 }
268 echo "</h2>";
269 echo "</div>";
270 }
271
272 }
273
274 class note extends cms {
275
276 public function __construct() {
277 parent::__construct();
278 $this->check_exists();
279 }
280
281 private function check_exists() {
282 $sql = "SELECT COUNT(*) FROM notes
283 WHERE url = ?";
284 $results = $this->query($sql, "s", $_GET['note']);
285 if ($results[0]["COUNT(*)"] != 1) {
286 throw new notFound();
287 }
288 }
289
290 public function display() {
291 $this->display_head();
292 $sql = "SELECT title, date_posted, text
293 FROM notes WHERE url = ?";
294 $result = $this->query($sql, "s",
295 $_GET['note']);
296 $entry = $result[0];
297 $title = $entry["title"];
298 $date_posted = explode("-", $entry["date_posted"]);
299 $year_posted = $date_posted[0];
300 $month_posted = $date_posted[1];
301 $datetime_posted = explode(' ', $date_posted[2]);
302 $day_posted = $datetime_posted[0];
303 echo "<div id=\"note\">";
304 echo "<h2><span style=\"color:grey;\">$year_posted/$month_posted/$day_posted/</span>$title</h2>";
305 echo $entry['text'];
306 $this->write_navigation();
307 $this->display_close();
308 }
309
310 private function write_navigation() {
311 echo "<br>";
312 echo "<div id=\"navigation\">";
313 echo "<h2>";
314 echo "<a href=\"/notes/\">notes</a>/";
315 echo "</h2>";
316 echo "</div>";
317 }
318 }
319
320 class notFound extends Exception {
321 public function __construct() {
322 header("HTTP/1.0 404 Not Found");
323 ob_end_clean();
324 include("404.php");
325 exit();
326 }
327 }
328
329 ## now actually do something:
330 switch (cms::determine_type()) {
331 case "index":
332 $index = new index();
333 $index->display();
334 break;
335 case "project":
336 $project = new project();
337 $project->display();
338 break;
339 case "note":
340 $note = new note;
341 $note->display();
342 break;
343 case "page":
344 $page = new page;
345 $page->display();
346 break;
347 }
348
349 ?>