-
Notifications
You must be signed in to change notification settings - Fork 63
/
Template.php
66 lines (56 loc) · 1.71 KB
/
Template.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
<?
use function Safe\ob_end_clean;
use function Safe\ob_start;
class Template{
/**
* @param array<mixed> $arguments
*/
protected static function Get(string $templateName, array $arguments = []): string{
// Expand the passed variables to make them available to the included template.
// We use these funny names so that we can use 'name' and 'value' as template variables if we want to.
foreach($arguments as $innerName => $innerValue){
$$innerName = $innerValue;
}
ob_start();
include(TEMPLATES_PATH . '/' . $templateName . '.php');
$contents = ob_get_contents() ?: '';
ob_end_clean();
return $contents;
}
/**
* @param array<mixed> $arguments
*/
public static function __callStatic(string $function, array $arguments): string{
if(isset($arguments[0]) && is_array($arguments[0])){
return self::Get($function, $arguments[0]);
}
else{
return self::Get($function, $arguments);
}
}
public static function Emit403(): void{
http_response_code(403);
include(WEB_ROOT . '/403.php');
exit();
}
public static function Emit404(): void{
http_response_code(404);
include(WEB_ROOT . '/404.php');
exit();
}
public static function RedirectToLogin(bool $redirectToDestination = true, string $destinationUrl = null): void{
if($redirectToDestination){
if($destinationUrl === null){
$destinationUrl = $_SERVER['SCRIPT_URL'];
}
header('Location: /sessions/new?redirect=' . urlencode($destinationUrl));
}
else{
header('Location: /sessions/new');
}
exit();
}
public static function IsEreaderBrowser(): bool{
return isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], "Kobo") !== false || strpos($_SERVER['HTTP_USER_AGENT'], "Kindle") !== false);
}
}