-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
190 lines (150 loc) · 5.28 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
<?php
/**
* The initialization script for the app
*
* @author Jason Lengstorf <[email protected]>
* @author Phil Leggetter <[email protected]>
*/
//-----------------------------------------------------------------------------
// Initializes environment variables
//-----------------------------------------------------------------------------
// Server path to this app (i.e. /var/www/vhosts/realtime/httpdocs/realtime)
define('APP_PATH', dirname(__FILE__));
// App folder, relative from web root (i.e. /realtime)
define('APP_FOLDER', dirname($_SERVER['SCRIPT_NAME']));
// URI path to the app (i.e. http://example.org/realtime)
define(
'APP_URI',
remove_double_slashes('http://' . $_SERVER['SERVER_NAME'] . APP_FOLDER)
);
// Server path to the system folder (for includes)
define('SYS_PATH', APP_PATH . '/system');
//-----------------------------------------------------------------------------
// Initializes the app
//-----------------------------------------------------------------------------
// Starts the session
if (!isset($_SESSION)) {
session_start();
}
// Loads the configuration variables
require_once SYS_PATH . '/config/config.inc.php';
// Loads Pusher
require_once SYS_PATH . '/lib/Pusher.php';
// Turns on error reporting if in debug mode
if (DEBUG===TRUE) {
ini_set('display_errors', 1);
error_reporting(E_ALL^E_STRICT);
} else {
ini_set('display_errors', 0);
error_reporting(0);
}
// Sets the timezone to avoid a notice
date_default_timezone_set(APP_TIMEZONE);
// Registers class_loader() as the autoload function
spl_autoload_register('class_autoloader');
//-----------------------------------------------------------------------------
// Loads and processes view data
//-----------------------------------------------------------------------------
// Parses the URI
$uri_array = parse_uri();
$class_name = get_controller_classname($uri_array);
$options = $uri_array;
// Sets a default view if nothing is passed in the URI (i.e. on the home page)
if (empty($class_name)) {
$class_name = 'Home';
}
// Tries to initialize the requested view, or else throws a 404 error
try {
$controller = new $class_name($options);
} catch (Exception $e) {
$options[1] = $e->getMessage();
$controller = new Error($options);
}
//-----------------------------------------------------------------------------
// Outputs the view
//-----------------------------------------------------------------------------
// Loads the <title> tag value for the header markup
$title = $controller->get_title();
// Sets the path to the app stylesheet for the header markup
$dirty_path = APP_URI . '/assets/styles/main.css';
$css_path = remove_double_slashes($dirty_path);
// Includes the header, requested view, and footer markup
require_once SYS_PATH . '/inc/header.inc.php';
$controller->output_view();
// Configures the Pusher channel if we're in a room
$channel = !empty($uri_array[0]) ? 'room_' . $uri_array[0] : 'default';
require_once SYS_PATH . '/inc/footer.inc.php';
//-----------------------------------------------------------------------------
// Function declarations
//-----------------------------------------------------------------------------
/**
* Breaks the URI into an array at the slashes
*
* @return array The broken up URI
*/
function parse_uri( )
{
// Removes any subfolders in which the app is installed
$real_uri = preg_replace(
'~^'.APP_FOLDER.'~',
'',
$_SERVER['REQUEST_URI'],
1
);
$uri_array = explode('/', $real_uri);
// If the first element is empty, get rid of it
if (empty($uri_array[0])) {
array_shift($uri_array);
}
// If the last element is empty, get rid of it
if (empty($uri_array[count($uri_array)-1])) {
array_pop($uri_array);
}
return $uri_array;
}
/**
* Determines the controller name using the first element of the URI array
*
* @param $uri_array array The broken up URI
* @return string The controller classname
*/
function get_controller_classname( &$uri_array )
{
$controller = array_shift($uri_array);
return ucfirst($controller);
}
/**
* Removes double slashes (except in the protocol)
*
* @param $dirty_path string The path to check for double slashes
* @return string The cleaned path
*/
function remove_double_slashes( $dirty_path )
{
return preg_replace('~(?<!:)//~', '/', $dirty_path);
}
/**
* Autoloads classes as they are instantiated
*
* @param $class_name string The name of the class to be loaded
* @return bool Returns TRUE on success (Exception on failure)
*/
function class_autoloader( $class_name )
{
$fname = strtolower($class_name);
// Defines all of the valid places a class file could be stored
$possible_locations = array(
SYS_PATH . '/models/class.' . $fname . '.inc.php',
SYS_PATH . '/controllers/class.' . $fname . '.inc.php',
SYS_PATH . '/core/class.' . $fname . '.inc.php',
);
// Loops through the location array and checks for a file to load
foreach ($possible_locations as $loc) {
if (file_exists($loc)) {
require_once $loc;
return TRUE;
}
}
// Fails because a valid class wasn't found
throw new Exception("Class $class_name wasn't found.");
}