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