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
|
<?php
/// CRIMSON --- A simple PHP framework.
/// Copyright © 2024 Freya Murphy <contact@freyacat.org>
///
/// This file is part of CRIMSON.
///
/// CRIMSON is free software; you can redistribute it and/or modify it
/// under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 3 of the License, or (at
/// your option) any later version.
///
/// CRIMSON is distributed in the hope that it will be useful, but
/// WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with CRIMSON. If not, see <http://www.gnu.org/licenses/>.
/**
* Gives access to imporant
* needed utility functions for
* accessing everything else!
*/
abstract class Base {
// ======================================================== LOADABLE OBJECTS ==
// keep track of what has been loaded
private static array $loaded = array();
/**
* Loads a $type of object from a $dir with a given $name
* @param string $name - the name of the object to load
* @param string $dir - the directory theese objects are stored in
* @param string $type - the type of the object
*/
private function load_type($name, $dir, $type): object|NULL
{
$path = $dir . '/' . $name . '.php';
// dont reload an ohject
if (array_key_exists($path, self::$loaded))
return self::$loaded[$path];
// only load a object if it exists
if (!file_exists($path))
return NULL;
$parts = explode('/', $name);
$part = end($parts);
$class = ucfirst($part) . '_' . $type;
require($path);
$ref = NULL;
try {
$ref = new ReflectionClass($class);
} catch (Exception $_e) {}
if ($ref === NULL)
return NULL;
$obj = $ref->newInstance();
self::$loaded[$path] = $obj;
return $obj;
}
/**
* Loads a model
* @param string $name - the name of the model to load
*/
public function load_model($name): Model|NULL
{
$dir = WEB_ROOT . '/_model';
return $this->load_type($name, $dir, 'model');
}
/**
* Loads a controller
* @param string $name - the name of the controller to load
*/
public function load_controller($name): Controller|NULL
{
$dir = WEB_ROOT . '/_controller';
return $this->load_type($name, $dir, 'controller');
}
// ==================================================================== LANG ==
// current loaded language strings
private static array $loaded_lang = array();
private static array $loaded_files = array();
/**
* Loads a php lang file into the lang array
*/
private function load_lang_file(string $file): void
{
if (isset(self::$loaded[$file]))
return;
self::$loaded[$file] = TRUE;
$lang = self::$loaded_lang;
require($file);
self::$loaded_lang = $lang;
}
/**
* Loads each php file lang strings in a directory
*/
private function load_lang_dir(string $dir): void
{
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry === '.' || $entry === '..')
continue;
$this->load_lang_file($entry);
}
}
}
/**
* Loads the given common lang
*/
public function load_lang(string ...$langs): array
{
$root = WEB_ROOT . '/lang';
foreach ($langs as $lang) {
$file = "{$root}/{$lang}.php";
$dir = "{$root}/{$lang}";
if (file_exists($file))
$this->load_lang_file($file);
else if (is_dir($dir))
$this->load_lang_dir($dir);
}
return self::$loaded_lang;
}
/**
* Returns the currently loaded lang
*/
public function get_lang(): array
{
return self::$loaded_lang;
}
// ================================================================ DATABASE ==
// current database connection
private static ?DatabaseHelper $db = NULL;
/**
* Loads the database
*/
public function db(): DatabaseHelper
{
if (!self::$db)
self::$db = new DatabaseHelper();
return self::$db;
}
// ================================================================ METADATA ==
/**
* Gets the stamp for a asset path
* @param string $path
*/
public function asset_stamp(string $path): int
{
if (ENVIRONMENT == 'devlopment')
return time();
if (isset(FILE_TIMES[$path]))
return FILE_TIMES[$path];
return 0;
}
/**
* Gets a full path url from a relative path
* @param string $path
* @param bool $timestamp
*/
public function get_url(string $path, bool $timestamp = FALSE): string
{
$scheme = 'http';
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']))
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'];
$host = $_SERVER['HTTP_HOST'];
if (ENVIRONMENT == 'production') {
$default = CONFIG['domain'];
$allowed = CONFIG['allowed_hosts'];
if (!is_array($allowed))
$allowed = [$allowed];
if (!in_array($host, $allowed))
$host = $default;
}
$base = CONFIG['base_path'];
$url = "{$scheme}://{$host}{$base}{$path}";
if ($timestamp) {
$time = $this->asset_stamp($path);
$url .= "?timestamp={$time}";
}
return $url;
}
/**
* Loads a js html link
* @param string $path - the path to the js file
*/
public function link_js(string $path): string
{
$stamp = $this->asset_stamp("public/$path");
$href = $this->get_url("public/{$path}?timestamp={$stamp}");
return '<script src="'. $href .'"></script>';
}
/**
* Loads a css html link
* @param string $path - the path to the css file
*/
public function link_css(string $path): string
{
$stamp = $this->asset_stamp("public/$path");
$href = $this->get_url("public/{$path}?timestamp={$stamp}");
return '<link rel="stylesheet" href="'. $href .'">';
}
/**
* Loads a css html link
* @param string $path - the path to the css file
*/
public function embed_css(string $path): string
{
$file = PUBLIC_ROOT . '/' . $path;
if (file_exists($file)) {
$text = file_get_contents($file);
return "<style>{$text}</style>";
} else {
return "";
}
}
// =============================================================== HTTP POST ==
/**
* Gets http POST data from $_POST if x-url-encoded, or parsed from
* php://input if AJAX. Returns FALSE if input is not a post request,
* or NULL if unable to parse request body.
*/
private function get_post_data()
{
static $data = NULL;
if (isset($data))
return $data;
// not a POST request
if ($_SERVER['REQUEST_METHOD'] != 'POST')
return NULL;
// ajax
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$data = json_decode(file_get_contents("php://input"),
true);
// on failure, make sure $data is FALSE and not NULL
if (!$data)
$data = FALSE;
return $data;
}
// x-url-encoded or form-data
$data = $_POST;
return $data;
}
/**
* Returns HTTP POST information if POST request.
* @returns $_POST if $key is not set and request is POST.
* @returns value at $key if $key is set and request is POST.
* @returns FALSE if value at $key is not set and request is POST.
* @returns FALSE if request is POST but has invalid body.
* @returns NULL if request is not POST.
*/
public function post_data(?string $key = NULL)
{
$data = $this->get_post_data();
if (!$data)
return $data;
if (!$key)
return $data;
if (!isset($data[$key]))
return FALSE;
return $data[$key];
}
}
|