vito / chyrp

The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.

chyrp / includes / controller / Main.php
100755 316 lines (258 sloc) 9.913 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Class: Main Controller
* The logic behind the Chyrp install.
*/
class MainController {
/**
* Function: index
* Grabs the posts for the main page.
*/
public function index() {
global $posts;
$posts = new Paginator(Post::find(array("placeholders" => true)), Config::current()->posts_per_page);
}
 
/**
* Function: archive
* Grabs the posts for the Archive page when viewing a year or a month.
*/
public function archive() {
global $posts;
if (!isset($_GET['month'])) return;
 
if (isset($_GET['day']))
$posts = new Paginator(Post::find(array("placeholders" => true,
"where" => "`__posts`.`created_at` like :date",
"params" => array(":date" => $_GET['year']."-".$_GET['month']."-".$_GET['day']."%"))),
Config::current()->posts_per_page);
else
$posts = new Paginator(Post::find(array("placeholders" => true,
"where" => "`__posts`.`created_at` like :date",
"params" => array(":date" => $_GET['year']."-".$_GET['month']."%"))),
Config::current()->posts_per_page);
}
 
/**
* Function: search
* Grabs the posts for a search query.
*/
public function search() {
global $posts;
fallback($_GET['query'], "");
$posts = new Paginator(Post::find(array("placeholders" => true,
"where" => "`xml` LIKE :query",
"params" => array(":query" => '%'.htmlspecialchars(urldecode($_GET['query'])).'%'))),
Config::current()->posts_per_page);
}
 
/**
* Function: drafts
* Grabs the posts for viewing the Drafts lists. Shows an error if the user lacks permissions.
*/
public function drafts() {
$visitor = Visitor::current();
if (!$visitor->group()->can("view_own_draft", "view_draft"))
show_403(__("Access Denied"), __("You do not have sufficient privileges to view drafts."));
 
global $posts;
$posts = new Paginator(Post::find(array("placeholders" => true,
"where" => array("`__posts`.`status` = 'draft'", "`__posts`.`user_id` = :current_user"),
"params" => array(":current_user" => $visitor->id))),
Config::current()->posts_per_page);
}
 
/**
* Function: feather
* Views posts of a specific feather.
*/
public function feather() {
global $posts;
$posts = new Paginator(Post::find(array("placeholders" => true,
"where" => "`feather` = :feather",
"params" => array(":feather" => depluralize($_GET['feather'])))),
Config::current()->posts_per_page);
}
 
/**
* Function: feed
* Grabs posts for the feed.
*/
public function feed() {
global $posts;
$posts = new Paginator(Post::find(array("placeholders" => true)), Config::current()->posts_per_page);
}
 
/**
* Function: rss
* Redirects to /feed (backwards compatibility).
*/
public function rss() {
header("HTTP/1.1 301 Moved Permanently");
redirect(fallback(Config::current()->feed_url, url("feed/"), true));
}
 
/**
* Function: view
* Views a post.
*/
public function view() {
global $post;
 
$config = Config::current();
$get = array_map("urldecode", $_GET);
 
if (!$config->clean_urls)
return $post = new Post(null, array("where" => "`__posts`.`url` = :url",
"params" => array(":url" => $get['url'])));
else
return $post = Post::from_url(Route::current()->post_url_attrs);
 
if ($post->no_results)
show_404();
}
 
/**
* Function: id
* Views a post by its static ID.
*/
public function id() {
global $post;
$post = new Post($_GET['id']);
}
 
/**
* Function: theme_preview
* Handles theme previewing.
*/
public function theme_preview() {
$visitor = Visitor::current();
$route = Route::current();
 
if (!$visitor->group()->can("change_settings"))
redirect("/");
 
if (empty($_GET['theme']))
error(__("Error"), __("Please specify a theme to preview."));
 
$this->index();
return $route->action = "index";
}
 
/**
* Function: toggle_admin
* Toggles the Admin control panel (if available).
*/
public function toggle_admin() {
if (!isset($_SESSION['chyrp_hide_admin']))
$_SESSION['chyrp_hide_admin'] = true;
else
unset($_SESSION['chyrp_hide_admin']);
 
redirect("/");
}
 
/**
* Function: register
* Process registration. If registration is disabled or if the user is already logged in, it will error.
*/
public function register() {
$config = Config::current();
if (!$config->can_register)
error(__("Registration Disabled"), __("I'm sorry, but this site is not allowing registration."));
 
if (logged_in())
error(__("Error"), __("You're already logged in."));
 
if (empty($_POST))
return;
 
$route = Route::current();
 
if (empty($_POST['login']))
return Flash::warning(__("Please enter a username for your account."));
 
if (count(User::find(array("where" => "`login` = :login",
"params" => array(":login" => $_POST['login'])))))
Flash::warning(__("That username is already in use."));
 
if (empty($_POST['password1']) and empty($_POST['password2']))
Flash::warning(__("Password cannot be blank."));
elseif ($_POST['password1'] != $_POST['password2'])
Flash::warning(__("Passwords do not match."));
 
if (empty($_POST['email']))
Flash::warning(__("E-mail address cannot be blank."));
elseif (!eregi("^[[:alnum:]][a-z0-9_.-\+]*@[a-z0-9.-]+\.[a-z]{2,6}$",$_POST['email']))
Flash::warning(__("Unsupported e-mail address."));
 
if (Flash::exists("warning"))
return;
 
User::add($_POST['login'], $_POST['password1'], $_POST['email']);
 
$_SESSION['chyrp_login'] = $_POST['login'];
$_SESSION['chyrp_password'] = md5($_POST['password1']);
 
Flash::notice(__("Registration successful."), "/");
}
 
/**
* Function: login
* Process logging in. If the username and password are incorrect or if the user is already logged in, it will error.
*/
public function login() {
if (logged_in())
error(__("Error"), __("You're already logged in."));
 
if (empty($_POST))
return;
 
fallback($_POST['login']);
fallback($_POST['password']);
 
if (!User::authenticate($_POST['login'], md5($_POST['password'])))
if (!count(User::find(array("where" => "__users.login = :login",
"params" => array(":login" => $_POST['login'])))))
Flash::warning(__("There is no user with that login name."));
else
Flash::warning(__("Password incorrect."));
 
if (Flash::exists("warning"))
return;
 
$_SESSION['chyrp_login'] = $_POST['login'];
$_SESSION['chyrp_password'] = md5($_POST['password']);
 
Flash::notice(__("Login successful."), "/");
}
 
/**
* Function: logout
* Logs the current user out. If they are not logged in, it will error.
*/
public function logout() {
if (!logged_in())
error(__("Error"), __("You aren't logged in."));
 
session_destroy();
 
session();
 
Flash::notice(__("Logged out."), "/");
}
 
/**
* Function: update_self
* Updates the current user when the form is submitted. Shows an error if they aren't logged in.
*/
public function update_self() {
if (empty($_POST))
return;
 
if (!logged_in())
error(__("Error"), __("You must be logged in to access this area."));
 
$visitor = Visitor::current();
 
$password = (!empty($_POST['new_password1']) and $_POST['new_password1'] == $_POST['new_password2']) ?
md5($_POST['new_password1']) :
$visitor->password ;
 
$visitor->update($visitor->login,
$password,
$_POST['full_name'],
$_POST['email'],
$_POST['website'],
$visitor->group()->id);
 
$_SESSION['chyrp_password'] = $password;
 
Flash::notice(__("Your profile has been updated."), "/");
}
 
/**
* Function: lost_password
* Handles e-mailing lost passwords to a user's email address.
*/
public function lost_password() {
if (empty($_POST))
return;
 
$user = new User(null, array("where" => "`login` = :login", "params" => array(":login" => $_POST['login'])));
if ($user->no_results)
return Flash::warning(__("Invalid user specified."));
 
$new_password = random(16);
 
$user->update($user->login,
md5($new_password),
$user->full_name,
$user->email,
$user->website,
$user->group_id);
 
$sent = @mail($user->email,
__("Lost Password Request"),
_f("%s,\n\nWe have received a request for a new password for your account at %s.\n\nPlease log in with the following password, and feel free to change it once you've successfully logged in:\n\t%s",
array($user->login, $config->name, $new_password)));
 
if ($sent)
return Flash::notice(_f("An e-mail has been sent to your e-mail address that contains a new password. Once you have logged in, you can change it at <a href=\"%s\">User Controls</a>.",
array(url("controls/"))));
 
# Set their password back to what it was originally.
$user->update($user->login,
$user->password,
$user->full_name,
$user->email,
$user->website,
$user->group_id);
 
Flash::warning(__("E-Mail could not be sent. Password change cancelled."));
}
}
$main = new MainController();