now using a model for note pages, but its not clean
[dylansserver.git] / cms.php
1 <?php
2
3 require_once("model/model.php");
4
5 abstract class cms {
6
7 private $config_file = '/etc/dylansserver.ini';
8 protected $model;
9 protected $recaptcha_publickey;
10 protected $recaptcha_privatekey;
11 public $title;
12 public $home_link;
13
14 public function __construct() {
15 $this->model = new model();
16 $config = parse_ini_file($this->config_file, true);
17 $this->db = new mysqli(
18 $config['database']['domain'],
19 $config['database']['user'],
20 $config['database']['password'],
21 $config['database']['database']);
22 if (mysqli_connect_errno()) {
23 echo "Problem connecting to database: ";
24 echo mysqli_connect_error();
25 exit();
26 }
27 $this->recaptcha_publickey = $config['recaptcha']['publickey'];
28 $this->recaptcha_privatekey = $config['recaptcha']['privatekey'];
29 $this->title = $config['site']['default_title'];
30 $this->home_link = $config['site']['home_link'];
31 }
32
33 public function query() {
34 $args = func_get_args();
35 $statement = $this->db->prepare($args[0]);
36 $args = array_slice($args, 1);
37 call_user_func_array(array($statement, 'bind_param'), &$args);
38 $statement->execute();
39 $return = array();
40 $statement->store_result();
41 $row = array();
42 $data = $statement->result_metadata();
43 $fields = array();
44 $fields[0] = &$statement;
45 while($field = $data->fetch_field()) {
46 $fields[] = &$row[$field->name];
47 }
48 call_user_func_array("mysqli_stmt_bind_result", $fields);
49 $i = 0;
50 while ($statement->fetch()) {
51 foreach ($row as $key=>$value) $return[$i][$key] = $value;
52 $i++;
53 }
54 $statement->free_result();
55 return $return;
56 }
57
58 public static function determine_type() {
59 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
60 return 'page';
61 } else if (isset($_GET['year'])) {
62 return 'archive';
63 } else if (isset($_GET['note'])) {
64 return 'note';
65 } else if ($_SERVER['REQUEST_URI'] == '/') {
66 return 'index';
67 } else if (isset($_GET['project'])) {
68 return 'project';
69 } else if (isset($_GET['rss'])) {
70 return 'rss';
71 } else if (isset($_GET['challenge'])) {
72 return 'captcha';
73 }
74 }
75
76 public function init() {
77 switch (cms::determine_type()) {
78 case 'index':
79 $index = new index();
80 $index->display();
81 break;
82 case 'project':
83 $project = new project();
84 $project->display();
85 break;
86 case 'note':
87 require_once("model/note.php");
88 $note = new note();
89 require_once("view/note.php");
90 break;
91 case 'page':
92 $page = new page;
93 $page->display();
94 break;
95 case "rss":
96 $rss = new rss();
97 $rss->display();
98 break;
99 case 'archive':
100 $archive = new archive;
101 $archive->display();
102 break;
103 case "captcha":
104 $captcha = new captcha;
105 $captcha->display();
106 break;
107 }
108 }
109
110 }
111
112
113 class index extends cms {
114
115 public function display() {
116 require_once("view/index.php");
117 }
118
119 protected function display_exhibits() {
120 echo "<div id='exhibit'>";
121 $sql = "SELECT text FROM projects ORDER BY rank";
122 $result = $this->db->query($sql);
123 while ($entry = $result->fetch_object()) {
124 echo $entry->text;
125 }
126 echo "</div>";
127 }
128
129 private function list_projects() {
130 $sql = "SELECT title FROM projects ORDER BY rank";
131 $result = $this->db->query($sql);
132 while ($entry = $result->fetch_object()) {
133 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
134 }
135 }
136
137 }
138
139
140 class project extends index {
141
142 protected function display_exhibits() {
143 echo "<div id='exhibit'>";
144 $sql = "SELECT text FROM projects
145 WHERE title = ?";
146 $result = $this->query($sql, "s", $_GET['project']);
147 if ($result = $result[0]['text']) {
148 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
149 echo $text;
150 echo "</div>";
151 } else {
152 throw new notFound();
153 }
154 }
155
156 }
157
158
159 class page extends cms {
160
161 private $page = 1;
162 private $offset = 0;
163 private $notes_per_page = 4;
164 private $number_of_pages = 1;
165
166 public function __construct() {
167 parent::__construct();
168 $this->page_offset();
169 }
170
171 private function page_offset() {
172 $sql = "SELECT COUNT(*) FROM notes";
173 $result = $this->db->query($sql);
174 $result = $result->fetch_array();
175 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
176 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
177 $this->page = (int) $_GET['page'];
178 } else {
179 throw new notFound();
180 }
181 if ($this->page > $this->number_of_pages) {
182 throw new notFound();
183 }
184 if ($this->page < 1) {
185 throw new notFound();
186 }
187 $this->offset = ($this->page - 1) * $this->notes_per_page;
188 }
189
190 public function display() {
191 require_once("view/page.php");
192 }
193
194 public function display_notes() {
195 echo "<div id='notes'>";
196 $sql = "SELECT date_posted, title, url, text
197 FROM notes ORDER BY date_posted DESC
198 LIMIT ?, ?";
199 $result = $this->query($sql, "ii",
200 $this->offset,
201 $this->notes_per_page);
202 foreach ($result as $row => $entry) {
203 $title = $entry['title'];
204 $url = '/note/' . $entry['url'];
205 $date_posted = explode("-", $entry['date_posted']);
206 $year_posted = $date_posted[0];
207 $month_posted = $date_posted[1];
208 $datetime_posted = explode(' ', $date_posted[2]);
209 $day_posted = $datetime_posted[0];
210 $text = $entry['text'];
211 echo <<<END_NOTE
212 <div class='note'>
213 <h1>
214 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
215 </h1>
216 $text
217 </div>
218 END_NOTE;
219 }
220 echo "</div>";
221 }
222 }
223
224
225 class archive extends cms {
226
227 public function __construct() {
228 parent::__construct();
229 }
230
231 private function check_exists() {
232 $sql = "SELECT COUNT(*) FROM notes
233 WHERE url = ?";
234 $results = $this->query($sql, "s", $_GET['note']);
235 if ($results[0]["COUNT(*)"] != 1) {
236 $this->not_found();
237 }
238 }
239
240 public function display() {
241 require_once("view/archive.php");
242 }
243
244 public function display_notes() {
245 switch (true) {
246 case (isset($_GET['year']) && !isset($_GET['month'])
247 && !isset($_GET['day'])):
248 $sql = "SELECT title, url, date_posted, text
249 FROM notes WHERE YEAR(date_posted) = ?
250 ORDER BY date_posted DESC";
251 $result = $this->query($sql, "d",
252 $_GET['year']);
253 break;
254 case (isset($_GET['year']) && isset($_GET['month'])
255 && !isset($_GET['day'])):
256 $sql = "SELECT title, url, date_posted, text
257 FROM notes WHERE YEAR(date_posted) = ?
258 AND MONTH(date_posted) = ?
259 ORDER BY date_posted DESC";
260 $result = $this->query($sql, "dd",
261 $_GET['year'], $_GET['month']);
262 break;
263 case (isset($_GET['year']) && isset($_GET['month'])
264 && isset($_GET['day'])):
265 $sql = "SELECT title, url, date_posted, text
266 FROM notes WHERE YEAR(date_posted) = ?
267 AND MONTH(date_posted) = ?
268 AND DAY(date_posted) = ?
269 ORDER BY date_posted DESC";
270 $result = $this->query($sql, "ddd",
271 $_GET['year'], $_GET['month'],
272 $_GET['day']);
273 break;
274 }
275 if (count($result) >= 1) {
276 echo "<div id='notes'>";
277 foreach ($result as $row => $entry) {
278 $title = $entry['title'];
279 $url = '/note/' . $entry['url'];
280 $date_posted = explode("-", $entry['date_posted']);
281 $year_posted = $date_posted[0];
282 $month_posted = $date_posted[1];
283 $datetime_posted = explode(' ', $date_posted[2]);
284 $day_posted = $datetime_posted[0];
285 echo "<div class='note'>";
286 echo "<h1><span class='date'>";
287 echo "$year_posted/$month_posted/$day_posted/";
288 echo "</span><a href='$url'>$title</a></h1>";
289 echo $entry['text'];
290 echo "</div>";
291 }
292 echo "</div>";
293 } else {
294 echo "<br>";
295 echo "<h1>sorry, nothing here</h2>";
296 echo "<pre>Empty set (0.00 sec)</pre>";
297 }
298 }
299
300 }
301
302
303 class rss extends cms {
304
305 public function display() {
306 require_once("view/rss.php");
307 }
308
309 public function display_items() {
310 $result = $this->db->query("SELECT date_posted, title, text, url
311 FROM notes ORDER BY date_posted DESC
312 LIMIT 5");
313 while ($entry = $result->fetch_object()) {
314 $title = $entry->title;
315 $date_posted = $entry->date_posted;
316 $url = "http://dylansserver.com/note/" . $entry->url;
317 $text = $entry->text;
318 $text = strip_tags($text);
319 $end_of_first_sentence = strpos($text, '.');
320 if ($end_of_first_sentence) {
321 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
322 if ($end_of_second_sentence) {
323 $description = substr($text, '0', ($end_of_second_sentence + 1));
324 } else {
325 $description = substr($text, '0', ($end_of_first_sentence + 1));
326 }
327 }
328 echo "<item>";
329 echo " <title>$title</title>";
330 echo " <link>$url</link>";
331 echo " <guid>$url</guid>";
332 echo " <description>$description</description>";
333 echo "</item>";
334 }
335 }
336 }
337
338
339 class notFound extends Exception {
340
341 public function __construct() {
342 header('HTTP/1.0 404 Not Found');
343 ob_end_clean();
344 include('404.php');
345 exit();
346 }
347
348 }
349
350
351 class captcha extends cms {
352
353 public function display() {
354 $challenge = $_GET['challenge'];
355 $response = $_GET['response'];
356 $remoteip = $_SERVER['REMOTE_ADDR'];
357 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
358 curl_setopt ($curl, CURLOPT_POST, 4);
359 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
360 $result = curl_exec ($curl);
361 curl_close ($curl);
362 }
363
364 }
365
366 ?>