forked from statedecoded/statedecoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
305 lines (252 loc) · 6.25 KB
/
index.php
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
<?php
/**
* Index
*
* Routes all the main requests. The site's home page can be found at home.php.
*
* PHP version 5
*
* @license http://www.gnu.org/licenses/gpl.html GPL 3
* @version 0.8
* @link http://www.statedecoded.com/
* @since 0.8
*
*/
/*
* Make sure that mod_env is installed, as it must be.
*/
if (!isset($_SERVER['HTTP_MOD_ENV']))
{
die('The State Decoded cannot run without Apache’s mod_env installed.');
}
/*
* If we have not defined the include path yet, then try to do so automatically. Once we have
* automatically defined the include path, we store it in .htaccess, where it becomes available
* within the scope of $_SERVER.
*/
if ( !isset($_SERVER['INCLUDE_PATH']) )
{
/*
* Try a couple of likely locations.
*/
if (file_exists('includes'))
{
$include_path = dirname(__FILE__) . '/includes/';
}
elseif (file_exists('../includes'))
{
$include_path = dirname(dirname(__FILE__)) . '/includes/';
}
/*
* Since we have not found it, recurse through the directories to locate it.
*/
else
{
/*
* These are the directories that we want to peer into the child directories of -- the
* current directory and its parent directory.
*/
$parent_directories = array('.', '..');
foreach ($parent_directories as $parent_directory)
{
/*
* Iterate through all of the parent directory's contents.
*/
$files = scandir($parent_directory);
foreach ($files as $file)
{
/*
* If this file is a directory, peer into its contents.
*/
if ( ($file != '.') && ($file != '..') && is_dir($parent_directory . '/' . $file) )
{
$child_files = scandir($parent_directory . '/' . $file);
/*
* To pick a file more or less at random, we look for class.Law.inc.php.
*/
if (in_array('class.Law.inc.php', $child_files) === TRUE)
{
$include_path = realpath(dirname(__FILE__) . '/' . $parent_directory . '/' . $file . '/');
break(2);
}
}
}
}
}
/*
* If we've defined our include path, then modify this file to store it permanently and store it
* as a constant.
*/
if (isset($include_path))
{
/*
* If possible, modify the .htaccess file, to store permanently the include path.
*
* If we *can't* modify the .htaccess file, then we have to define the constant on the fly,
* with every page view. This is really quite undesirable, because it will slow down the
* site non-trivially, but it's better than not working at all.
*/
if (is_writable('.htaccess') == TRUE)
{
$htaccess = PHP_EOL . PHP_EOL . 'SetEnv INCLUDE_PATH ' . $include_path . PHP_EOL;
$result = file_put_contents('.htaccess' , $htaccess, FILE_APPEND);
}
define('INCLUDE_PATH', $include_path);
}
}
/*
* Save the include path as a constant.
*/
if (isset($_SERVER['INCLUDE_PATH']))
{
define('INCLUDE_PATH', $_SERVER['INCLUDE_PATH']);
}
/*
* If the edition ID was provided by the .htaccess file, save it as a constant.
*/
if (isset($_SERVER['EDITION_ID']))
{
define('EDITION_ID', $_SERVER['EDITION_ID']);
}
/*
* If APC is not running.
*/
if ( !extension_loaded('apc') || (ini_get('apc.enabled') != 1) )
{
/*
* Include the site's config file.
*/
if ( (include INCLUDE_PATH . '/config.inc.php') === FALSE )
{
die('Cannot run without a config.inc.php file. See the installation documentation.');
}
define('APC_RUNNING', FALSE);
}
/*
* Else if APC is running, get data from the cache.
*/
else
{
/*
* Attempt to load the config file constants out of APC.
*/
$result = apc_load_constants('config');
/*
* If loading from APC worked, just set the include path.
*/
if ($result == TRUE)
{
set_include_path(get_include_path() . PATH_SEPARATOR . INCLUDE_PATH);
}
/*
* If we couldn't load the constants from APC.
*/
else
{
/*
* Load constants from the config file.
*/
if ( (include INCLUDE_PATH . '/config.inc.php') === FALSE )
{
die('Cannot run without a config.inc.php file. See the installation documentation.');
}
define('APC_RUNNING', TRUE);
/*
* And then save them to APC.
*/
$constants = get_defined_constants(TRUE);
apc_define_constants('config', $constants['user']);
}
}
/*
* Include the functions that drive the site.
*/
require('functions.inc.php');
/*
* Connect to the database.
*/
try
{
$db = new Database( PDO_DSN, PDO_USERNAME, PDO_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT) );
}
/*
* If we cannot connect.
*/
catch (PDOException $e)
{
/*
* If we get error 1049, that means that no database of this name could be found. This means
* that The State Decoded has not yet been installed. Redirect to the admin section.
*/
if (strpos($e->getMessage(), '[1049]') !== FALSE)
{
if (strpos($_SERVER['REQUEST_URI'], '/admin') === FALSE)
{
header('Location: /admin/');
exit;
}
}
/*
* Else it's a generic database problem.
*/
else
{
/*
* A specific error page has been created for database connection failures, display that.
*/
if (defined('ERROR_PAGE_DB'))
{
require($_SERVER['DOCUMENT_ROOT'] . '/' . ERROR_PAGE_DB);
exit();
}
/*
* If no special error page exists, display a generic error.
*/
else
{
die(SITE_TITLE . ' is having some database trouble right now. Please check back in a few minutes.');
}
}
}
/*
* Prior to PHP v5.3.6, the PDO does not pass along to MySQL the DSN charset configuration option,
* and it must be done manually.
*/
if (version_compare(PHP_VERSION, '5.3.6', '<'))
{
$db->exec("SET NAMES utf8");
}
/*
* We're going to need access to the database connection throughout the site.
*/
global $db;
/*
* Include Solarium's autoloader, for queries to Solr.
*/
require('Solarium/Autoloader.php');
Solarium_Autoloader::register();
/*
* Turn the Solr URL constant into a configuration array that we can use anywhere we need to use
* Solarium.
*/
$solr_url = parse_url(SOLR_URL);
$GLOBALS['solr_config'] = array(
'adapteroptions' => array(
'host' => $solr_url['host'],
'port' => $solr_url['port'],
'path' => $solr_url['path'],
)
);
/*
* Include the custom functions file.
*/
require(CUSTOM_FUNCTIONS);
/*
* Establish routes
*/
require('routes.inc.php');
/*
* Initialize the master controller
*/
$mc = new MasterController();
$mc->execute();