-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.php
110 lines (90 loc) · 4.74 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
<?php
// Turn on error reporting if the site is in development
if ( SITE_DEV ) {
error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
}
// Requests
define( 'REQUEST', trim( str_replace( dirname( $_SERVER['PHP_SELF'] ), '', preg_replace( '~/+~', '/' , parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ) ), '/' ) );
define( 'REQUEST_METHOD', $_SERVER['REQUEST_METHOD'] );
define( 'REQUEST_IS_AJAX', (bool)isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' );
if ( REQUEST == 'index.php' ) {
Header( 'HTTP/1.1 301 Moved Permanently' );
header( 'Location: /' );
exit;
}
//Check for a config file
if( !@include( 'system/config/config.php' ) ){
header("HTTP/1.1 500 Internal Server Error");
if ( REQUEST_IS_AJAX ) {
die( json_encode( array( 'message' => 'No config file found. Rename system/config/config_sample.php to config.php and edit it to reflect your needs.' ) ) );
}
echo "<p><strong>No config file found.</strong> Rename system/config/config_sample.php to config.php and edit it to reflect your needs.</p>";
exit;
}
// Only need these if this isn't an AJAX request
if ( !REQUEST_IS_AJAX ) {
require( DIR_LIB . '/FormStatusMessage/FormStatusMessage.php' );
require( DIR_HELPERS . '/view_helpers.php' );
$status_message = new FormStatusMessage;
}
// Simple registry
$registry = new StdClass;
// Require necessary files
require( DIR_CORE . '/Router.php' );
require( DIR_CONFIG . '/routes.php' );
require( DIR_CORE . '/Request.php' );
require( DIR_CORE . '/Response.php' );
if ( $registry->request = Router::route( REQUEST ) ) {
// Connect to the database
require( DIR_CORE . '/Db.php' );
if ( !$db = Db::connect( $config['db_user'], $config['db_password'], $config['db_name'], $config['db_host'], $config['db_type'] ) ) {
header("HTTP/1.1 500 Internal Server Error");
if ( REQUEST_IS_AJAX ) {
die( json_encode( array( 'message' => 'Unable to connect to the database. Please check your config and try again.' ) ) );
}
$status_message->setStatuses( array( 'error', 'block-message', 'remain' ) );
$status_message->setMessage( "<p><strong>Unable to connect to the database</strong>. Please check your config and try again.</p>" );
require( DIR_VIEWS . '/pages/error.php' );
exit;
}
// We will use a location table gateway on every page so we will create one here
require( DIR_MODELS . '/LocationTableGateway.php' );
require( DIR_MODELS . '/Location.php' );
$stg = new LocationTableGateway( $db, $config['db_table'], $config['column_map'] );
if ( !$stg->validateTable() ) {
header("HTTP/1.1 500 Internal Server Error");
if ( REQUEST_IS_AJAX ) {
die( json_encode( array( 'message' => 'Invalid table setup. Please check your config and try again.' ) ) );
}
$status_message->setStatuses( array('error', 'block-message', 'remain' ) );
$status_message->setMessage( "<p><strong>Invalid table setup</strong>. Please check your config and try again.</p>" );
require( DIR_VIEWS . '/pages/error.php' );
exit;
}
// Set variables
$registry->controller = Router::$controller;
$registry->column_info = $stg->getColumns();
$registry->columns = array_keys( $registry->column_info );
$registry->columns_list = array_values( array_diff( array_keys( $config['column_map'] ), array( $config['column_map']['id'], $config['column_map']['lat'], $config['column_map']['lng'] ) ) );
$registry->columns_list_titles = array_values( array_diff( array_values( $config['column_map'] ), array( $config['column_map']['id'], $config['column_map']['lat'], $config['column_map']['lng'] ) ) );
$registry->columns_edit = array_merge( array_values( array_diff( array_keys( $config['column_map'] ), array( $config['column_map']['id'], $config['column_map']['lat'], $config['column_map']['lng'] ) ) ), array( $config['column_map']['lat'], $config['column_map']['lng'] ) );
$registry->columns_edit_titles = array_merge( array_values( array_diff( array_values( $config['column_map'] ), array( $config['column_map']['id'], $config['column_map']['lat'], $config['column_map']['lng'] ) ) ), array( $config['column_map']['lat'], $config['column_map']['lng'] ) );
$registry->geocode_status = null;
if ( isset( $_GET['status'], $_GET['message'] ) ) {
$status_message->setStatus( $_GET['status'] );
$status_message->setMessage($_GET['message'] );
}
require( DIR_HELPERS . '/helpers.php' );
require( sprintf( "%s/%s.php", DIR_CONTROLLERS, $registry->controller ) );
exit;
}
// No route found, send 404
header("HTTP/1.1 404 Not Found");
if ( REQUEST_IS_AJAX ) {
die( json_encode( array( 'message' => 'File not found' ) ) );
}
$status_message->setStatuses( array( 'error', 'block-message', 'remain' ) );
$status_message->setMessage( "<p><strong>Page not found</strong></p>" );
require( DIR_VIEWS . '/pages/error.php' );
exit;