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