page now using model
[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 require_once("model/page.php");
93 $page = new page();
94 require_once("view/page.php");
95 break;
96 case "rss":
97 $rss = new rss();
98 $rss->display();
99 break;
100 case 'archive':
101 $archive = new archive;
102 $archive->display();
103 break;
104 case "captcha":
105 $captcha = new captcha;
106 $captcha->display();
107 break;
108 }
109 }
110
111 }
112
113
114 class index extends cms {
115
116 public function display() {
117 require_once("view/index.php");
118 }
119
120 protected function display_exhibits() {
121 echo "<div id='exhibit'>";
122 $sql = "SELECT text FROM projects ORDER BY rank";
123 $result = $this->db->query($sql);
124 while ($entry = $result->fetch_object()) {
125 echo $entry->text;
126 }
127 echo "</div>";
128 }
129
130 private function list_projects() {
131 $sql = "SELECT title FROM projects ORDER BY rank";
132 $result = $this->db->query($sql);
133 while ($entry = $result->fetch_object()) {
134 echo "<li><a class='tab' href='$entry->title'>$entry->title</a></li>";
135 }
136 }
137
138 }
139
140
141 class project extends index {
142
143 protected function display_exhibits() {
144 echo "<div id='exhibit'>";
145 $sql = "SELECT text FROM projects
146 WHERE title = ?";
147 $result = $this->query($sql, "s", $_GET['project']);
148 if ($result = $result[0]['text']) {
149 $text = str_replace("class='exhibit'", "class='exhibit' style='display:block;'", $result);
150 echo $text;
151 echo "</div>";
152 } else {
153 throw new notFound();
154 }
155 }
156
157 }
158
159
160 class archive extends cms {
161
162 public function __construct() {
163 parent::__construct();
164 }
165
166 private function check_exists() {
167 $sql = "SELECT COUNT(*) FROM notes
168 WHERE url = ?";
169 $results = $this->query($sql, "s", $_GET['note']);
170 if ($results[0]["COUNT(*)"] != 1) {
171 $this->not_found();
172 }
173 }
174
175 public function display() {
176 require_once("view/archive.php");
177 }
178
179 public function display_notes() {
180 switch (true) {
181 case (isset($_GET['year']) && !isset($_GET['month'])
182 && !isset($_GET['day'])):
183 $sql = "SELECT title, url, date_posted, text
184 FROM notes WHERE YEAR(date_posted) = ?
185 ORDER BY date_posted DESC";
186 $result = $this->query($sql, "d",
187 $_GET['year']);
188 break;
189 case (isset($_GET['year']) && isset($_GET['month'])
190 && !isset($_GET['day'])):
191 $sql = "SELECT title, url, date_posted, text
192 FROM notes WHERE YEAR(date_posted) = ?
193 AND MONTH(date_posted) = ?
194 ORDER BY date_posted DESC";
195 $result = $this->query($sql, "dd",
196 $_GET['year'], $_GET['month']);
197 break;
198 case (isset($_GET['year']) && isset($_GET['month'])
199 && isset($_GET['day'])):
200 $sql = "SELECT title, url, date_posted, text
201 FROM notes WHERE YEAR(date_posted) = ?
202 AND MONTH(date_posted) = ?
203 AND DAY(date_posted) = ?
204 ORDER BY date_posted DESC";
205 $result = $this->query($sql, "ddd",
206 $_GET['year'], $_GET['month'],
207 $_GET['day']);
208 break;
209 }
210 if (count($result) >= 1) {
211 echo "<div id='notes'>";
212 foreach ($result as $row => $entry) {
213 $title = $entry['title'];
214 $url = '/note/' . $entry['url'];
215 $date_posted = explode("-", $entry['date_posted']);
216 $year_posted = $date_posted[0];
217 $month_posted = $date_posted[1];
218 $datetime_posted = explode(' ', $date_posted[2]);
219 $day_posted = $datetime_posted[0];
220 echo "<div class='note'>";
221 echo "<h1><span class='date'>";
222 echo "$year_posted/$month_posted/$day_posted/";
223 echo "</span><a href='$url'>$title</a></h1>";
224 echo $entry['text'];
225 echo "</div>";
226 }
227 echo "</div>";
228 } else {
229 echo "<br>";
230 echo "<h1>sorry, nothing here</h2>";
231 echo "<pre>Empty set (0.00 sec)</pre>";
232 }
233 }
234
235 }
236
237
238 class rss extends cms {
239
240 public function display() {
241 require_once("view/rss.php");
242 }
243
244 public function display_items() {
245 $result = $this->db->query("SELECT date_posted, title, text, url
246 FROM notes ORDER BY date_posted DESC
247 LIMIT 5");
248 while ($entry = $result->fetch_object()) {
249 $title = $entry->title;
250 $date_posted = $entry->date_posted;
251 $url = "http://dylansserver.com/note/" . $entry->url;
252 $text = $entry->text;
253 $text = strip_tags($text);
254 $end_of_first_sentence = strpos($text, '.');
255 if ($end_of_first_sentence) {
256 $end_of_second_sentence = strpos($text, '.', ($end_of_first_sentence + 1));
257 if ($end_of_second_sentence) {
258 $description = substr($text, '0', ($end_of_second_sentence + 1));
259 } else {
260 $description = substr($text, '0', ($end_of_first_sentence + 1));
261 }
262 }
263 echo "<item>";
264 echo " <title>$title</title>";
265 echo " <link>$url</link>";
266 echo " <guid>$url</guid>";
267 echo " <description>$description</description>";
268 echo "</item>";
269 }
270 }
271 }
272
273
274 class notFound extends Exception {
275
276 public function __construct() {
277 header('HTTP/1.0 404 Not Found');
278 ob_end_clean();
279 include('404.php');
280 exit();
281 }
282
283 }
284
285
286 class captcha extends cms {
287
288 public function display() {
289 $challenge = $_GET['challenge'];
290 $response = $_GET['response'];
291 $remoteip = $_SERVER['REMOTE_ADDR'];
292 $curl = curl_init('http://api-verify.recaptcha.net/verify?');
293 curl_setopt ($curl, CURLOPT_POST, 4);
294 curl_setopt ($curl, CURLOPT_POSTFIELDS, "privatekey=$this->recaptcha_privatekey&remoteip=$remoteip&challenge=$challenge&response=$response");
295 $result = curl_exec ($curl);
296 curl_close ($curl);
297 }
298
299 }
300
301 ?>