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