forked from wishthis/wishthis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
154 lines (129 loc) · 3.07 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
<?php
/**
* wishthis - Make a wish
*
* @author Jay Trees <[email protected]>
*/
namespace wishthis;
define('VERSION', '1.2.0');
define('ROOT', __DIR__);
define('DEFAULT_LOCALE', 'en_GB');
/**
* Include
*/
require 'vendor/autoload.php';
require_once ROOT . '/src/functions/getCookieDomain.php';
require_once ROOT . '/src/functions/gettext.php';
require_once ROOT . '/src/functions/getWishlistNameSuggestion.php';
require_once ROOT . '/src/functions/redirect.php';
spl_autoload_register(
function (string $absoluteNamespace) {
if (__NAMESPACE__ !== substr($absoluteNamespace, 0, strlen(__NAMESPACE__))) {
return;
}
$absoluteNamespace = str_replace('\\', '/', $absoluteNamespace);
$filepath = ROOT . '/src/classes/' . $absoluteNamespace . '.php';
require $filepath;
}
);
/**
* Config
*/
$configPath = __DIR__ . '/' . 'src/config/config.php';
$config = new Config($configPath);
$config->load();
/**
* Session
*
* Has to be setup first, before anything else, so translations can be loaded.
* The configuration is the only exception, since `loadFromSession` needs the
* database.
*/
session_start(
[
'name' => 'wishthis',
]
);
$user = User::getCurrent();
/**
* Database
*/
$database = false;
$options = false;
if (
defined('DATABASE_HOST')
&& defined('DATABASE_NAME')
&& defined('DATABASE_USER')
&& defined('DATABASE_PASSWORD')
) {
$database = new Database(
DATABASE_HOST,
DATABASE_NAME,
DATABASE_USER,
DATABASE_PASSWORD
);
$database->connect();
/**
* Options
*/
$options = new Options($database);
/**
* User session
*/
$user->loadFromSession();
}
/**
* Language
*/
\Locale::setDefault(DEFAULT_LOCALE);
/** Determine Locale */
$locales = array_filter(
array_map(
function ($value) {
$extension = pathinfo($value, PATHINFO_EXTENSION);
$filename = pathinfo($value, PATHINFO_FILENAME);
if ('po' === $extension) {
return pathinfo($value, PATHINFO_FILENAME);
}
},
scandir(ROOT . '/translations')
)
);
$locale = isset($_REQUEST['locale']) ? $_REQUEST['locale'] : \Locale::lookup($locales, $user->getLocale(), false, 'en_GB');
/**
* Wish
*/
Wish::initialize();
/**
* Pretty URLs
*/
$url = new URL($_SERVER['REQUEST_URI']);
/**
* Database Update
*/
if ($options && $options->getOption('isInstalled')) {
if (-1 === version_compare($options->version, VERSION)) {
$options->setOption('updateAvailable', true);
}
}
/**
* Page
*/
if (!isset($page)) {
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
}
$pagePath = 'src/pages/' . $page . '.php';
$pagePathAlt = 'src/pages/' . $page . '/' . $page . '.php';
if (file_exists($pagePath)) {
require $pagePath;
} elseif (\file_exists($pagePathAlt)) {
require $pagePathAlt;
} else {
http_response_code(404);
?>
<h1>Not found</h1>
<p>The requested URL was not found on this server.</p>
<?php
echo $pagePath;
die();
}