vito / chyrp

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

This URL has Read+Write access

chyrp / includes / common.php
100755 378 lines (296 sloc) 10.501 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* File: Common
*
* Chyrp - A Lightweight Blogging Engine
*
* Version:
* v2.0 RC1
*
* License:
* GPL-3
*
* Chyrp Copyright:
* Copyright (c) 2008 Alex Suraci, <http://toogeneric.com/>
*/
 
if (version_compare(PHP_VERSION, "5.1.3", "<"))
exit("Chyrp requires PHP 5.1.3 or greater. Installation cannot continue.");
 
# Constant: CHYRP_VERSION
# Chyrp's version number.
define('CHYRP_VERSION', "2.0 RC1");
 
# Constant: DEBUG
# Should Chyrp use debugging processes?
define('DEBUG', false);
 
# Fallback all these definitions.
if (!defined('JAVASCRIPT')) define('JAVASCRIPT', false);
if (!defined('ADMIN')) define('ADMIN', false);
if (!defined('AJAX')) define('AJAX', false);
if (!defined('XML_RPC')) define('XML_RPC', false);
if (!defined('TRACKBACK')) define('TRACKBACK', false);
 
define('UPGRADING', false);
define('INSTALLING', false);
 
# Constant: INDEX
# Is the requested file index.php?
define('INDEX', (pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_BASENAME) == "index.php") and !ADMIN);
 
# Use GZip compression if available.
if (!AJAX and extension_loaded("zlib") and
!ini_get("zlib.output_compression") and
isset($_SERVER['HTTP_ACCEPT_ENCODING']) and
substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip")) {
ob_start("ob_gzhandler");
header("Content-Encoding: gzip");
} else
ob_start();
 
if (JAVASCRIPT) {
error_reporting(0);
header("Content-Type: application/x-javascript");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 03 Jun 1991 05:30:00 GMT");
} else
error_reporting(E_ALL | E_STRICT); # Make sure E_STRICT is on so Chyrp remains errorless.
 
# Constant: MAIN_DIR
# Absolute path to the Chyrp root
define('MAIN_DIR', dirname(dirname(__FILE__)));
 
# Constant: INCLUDES_DIR
# Absolute path to /includes
define('INCLUDES_DIR', MAIN_DIR."/includes");
 
# Constant: MODULES_DIR
# Absolute path to /modules
define('MODULES_DIR', MAIN_DIR."/modules");
 
# Constant: FEATHERS_DIR
# Absolute path to /feathers
define('FEATHERS_DIR', MAIN_DIR."/feathers");
 
# Constant: THEMES_DIR
# Absolute path to /themes
define('THEMES_DIR', MAIN_DIR."/themes");
 
# File: Helpers
# Various functions used throughout Chyrp's code.
require_once INCLUDES_DIR."/helpers.php";
 
# Not installed?
if (!file_exists(INCLUDES_DIR."/config.yaml.php"))
redirect("install.php");
 
require_once INCLUDES_DIR."/class/Query.php";
require_once INCLUDES_DIR."/class/QueryBuilder.php";
require_once INCLUDES_DIR."/lib/YAML.php";
 
require_once INCLUDES_DIR."/class/Config.php";
require_once INCLUDES_DIR."/class/SQL.php";
 
# Translation stuff
require_once INCLUDES_DIR."/lib/gettext/gettext.php";
require_once INCLUDES_DIR."/lib/gettext/streams.php";
 
if (function_exists("date_default_timezone_set"))
date_default_timezone_set($config->timezone);
else
ini_set("date.timezone", $config->timezone);
 
header("X-Pingback: ".$config->chyrp_url."/includes/xmlrpc.php");
 
$sql->connect();
 
sanitize_input($_GET);
sanitize_input($_POST);
sanitize_input($_COOKIE);
sanitize_input($_REQUEST);
 
set_error_handler("error_panicker");
 
# File: Model
# See Also:
# <Model>
require_once INCLUDES_DIR."/class/Model.php";
 
# File: User
# See Also:
# <User>
require_once INCLUDES_DIR."/model/User.php";
 
# File: Visitor
# See Also:
# <Visitor>
require_once INCLUDES_DIR."/model/Visitor.php";
 
# File: Post
# See Also:
# <Post>
require_once INCLUDES_DIR."/model/Post.php";
 
# File: Page
# See Also:
# <Page>
require_once INCLUDES_DIR."/model/Page.php";
 
# File: Group
# See Also:
# <Group>
require_once INCLUDES_DIR."/model/Group.php";
 
# File: Session
# See Also:
# <Session>
require_once INCLUDES_DIR."/class/Session.php";
 
if (!JAVASCRIPT)
session();
 
# File: Flash
# See Also:
# <Flash>
require_once INCLUDES_DIR."/class/Flash.php";
 
# File: Theme
# See Also:
# <Theme>
require_once INCLUDES_DIR."/class/Theme.php";
 
# File: Trigger
# See Also:
# <Trigger>
require_once INCLUDES_DIR."/class/Trigger.php";
 
# File: Module
# See Also:
# <Module>
require_once INCLUDES_DIR."/class/Modules.php";
 
# File: Feathers
# See Also:
# <Feathers>
require_once INCLUDES_DIR."/class/Feathers.php";
 
# File: Paginator
# See Also:
# <Paginator>
require_once INCLUDES_DIR."/class/Paginator.php";
 
# File: Twig
# Chyrp's templating engine.
require_once INCLUDES_DIR."/class/Twig.php";
 
# File: Route
# See Also:
# <Route>
require_once INCLUDES_DIR."/class/Route.php";
 
# File: Main
# See Also:
# <Main Controller>
require_once INCLUDES_DIR."/controller/Main.php";
 
# File: Admin
# See Also:
# <Admin Controller>
require_once INCLUDES_DIR."/controller/Admin.php";
 
# File: Feather
# See Also:
# <Feather>
require_once INCLUDES_DIR."/interface/Feather.php";
 
timer_start();
 
$flash = Flash::current();
 
set_locale($config->locale);
 
# Require feathers/modules and load their translations.
foreach ($config->enabled_feathers as $index => $feather) {
if (!file_exists(FEATHERS_DIR."/".$feather."/".$feather.".php")) {
unset($config->enabled_feathers[$index]);
continue;
}
 
if (file_exists(FEATHERS_DIR."/".$feather."/locale/".$config->locale.".mo"))
load_translator($feather, FEATHERS_DIR."/".$feather."/locale/".$config->locale.".mo");
 
require FEATHERS_DIR."/".$feather."/".$feather.".php";
 
$info = YAML::load(FEATHERS_DIR."/".$feather."/info.yaml");
$pluralizations[$feather] = $pluralizations["feathers"][$feather] = fallback($info["plural"], pluralize($feather), true);
}
 
foreach ($config->enabled_modules as $index => $module) {
if (!file_exists(MODULES_DIR."/".$module."/".$module.".php")) {
unset($config->enabled_modules[$index]);
continue;
}
 
if (file_exists(MODULES_DIR."/".$module."/locale/".$config->locale.".mo"))
load_translator($module, MODULES_DIR."/".$module."/locale/".$config->locale.".mo");
 
require MODULES_DIR."/".$module."/".$module.".php";
}
 
# Load the /clean/urls into their correct $_GET values.
if (INDEX or ADMIN)
$route->determine_action();
 
# Variable: $visitor
# Holds the current user and their group.
$visitor = Visitor::current();
 
define('PREVIEWING', ($visitor->group()->can("change_settings") and
!empty($_GET['action']) and
$_GET['action'] == "theme_preview" and
!empty($_GET['theme'])));
$theme = PREVIEWING ? $_GET['theme'] : $config->theme;
 
# Constant: THEME_DIR
# Absolute path to /themes/(current theme)
define('THEME_DIR', MAIN_DIR."/themes/".$theme);
 
# Constant: THEME_URL
# URL to /themes/(current theme)
define('THEME_URL', $config->chyrp_url."/themes/".$theme);
 
$theme = Theme::current();
 
foreach (YAML::load(THEME_DIR."/info.yaml") as $key => $val)
$theme->$key = $val;
 
if (INDEX)
header("Content-type: ".fallback($theme->type, "text/html")."; charset=UTF-8");
elseif (AJAX)
header("Content-type: text/html; charset=UTF-8");
 
# These are down here so that the modules are
# initialized after the $_GET values are filled.
/**
* Array: $feathers
* Contains all of the enabled Feather's Classes.
*/
$feathers = array();
foreach ($config->enabled_feathers as $feather) {
$camelized = camelize($feather);
if (!class_exists($camelized)) continue;
 
$feathers[$feather] = new $camelized;
$feathers[$feather]->safename = $feather;
 
if (!ADMIN and $route->action != "feed") continue;
 
foreach (YAML::load(FEATHERS_DIR."/".$feather."/info.yaml") as $key => $val)
$feathers[$feather]->$key = (is_string($val)) ? __($val, $feather) : $val ;
}
 
/**
* Array: $modules
* Contains all of the enabled Module's Classes.
*/
$modules = array();
foreach ($config->enabled_modules as $module) {
$camelized = camelize($module);
if (!class_exists($camelized)) continue;
 
$modules[$module] = new $camelized;
$modules[$module]->safename = $module;
 
if (!ADMIN) continue;
 
foreach (YAML::load(MODULES_DIR."/".$module."/info.yaml") as $key => $val)
$modules[$module]->$key = (is_string($val)) ? __($val, $module) : $val ;
}
 
# Now that they're all instantiated, call __init().
foreach ($feathers as $feather)
if (method_exists($feather, "__init"))
$feather->__init();
foreach ($modules as $module)
if (method_exists($module, "__init"))
$module->__init();
 
if (INDEX) {
$route->check_custom_routes();
 
# If the post viewing URL is the same as the page viewing URL, check for viewing a page first.
if (preg_match("/^\((clean|url)\)\/?$/", $config->post_url)) {
$route->check_viewing_page();
$route->check_viewing_post(true);
} else {
$route->check_viewing_post();
$route->check_viewing_page(true);
}
}
 
if (INDEX or ADMIN)
$trigger->call("runtime");
 
# Array: $statuses
# An array of post statuses that <Visitor> can view.
$statuses = array("public");
if (logged_in())
$statuses[] = "registered_only";
if ($visitor->group()->can("view_private"))
$statuses[] = "private";
 
Post::$private = "status IN ('".implode("', '", $statuses)."')";
Post::$enabled_feathers = "feather IN ('".implode("', '", $config->enabled_feathers)."')";
 
# Load the translation engine
load_translator("chyrp", INCLUDES_DIR."/locale/".$config->locale.".mo");
 
# Load the theme translator
if (file_exists(THEME_DIR."/locale/".$config->locale.".mo"))
load_translator("theme", THEME_DIR."/locale/".$config->locale.".mo");
 
if (INDEX or ADMIN or AJAX) {
if (!$visitor->group()->can("view_site") and !in_array($route->action, array("login", "logout", "register", "lost_password")))
if ($trigger->exists("can_not_view_site"))
$trigger->call("can_not_view_site");
else
show_403(__("Access Denied"), __("You are not allowed to view this site."));
 
if (isset($_GET['feed']))
$config->posts_per_page = $config->feed_items;
 
if (!ADMIN and method_exists($main, $route->action))
call_user_func(array($main, $route->action));
 
# Call any plugin route functions
if (INDEX)
$trigger->call("route_".$route->action);
 
if (isset($_GET['feed']))
if ($trigger->exists($route->action."_feed")) # What about custom feeds?
$trigger->call($route->action."_feed");
elseif (isset($posts)) # Are there already posts to show?
$route->action = "feed";
else
redirect(fallback($config->feed_url, url("feed/"), true)); # Really? Nothing? Too bad. MAIN FEED 4 U.
}