Newer
Older
<?php declare(strict_types=1);
namespace uib\ub\loadspeakr\modules;
use uib\ub\loadspeakr\HTTPStatus;
use uib\ub\loadspeakr\Logging;
use uib\ub\loadspeakr\Utils;
class StaticModule implements ModuleInterface
{
public function match($uri)
{
global $conf;
global $localUri;
global $uri;
global $acceptContentType;
global $endpoints;
global $lodspk;
$q = preg_replace('|^' . $conf['basedir'] . '|', '', $localUri);
if (strlen($q) > 0 && file_exists($conf['home'] . $conf['static']['directory'] . $q)) {
return $q;
}
return false;
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
public function execute($file)
{
global $conf;
global $localUri;
global $uri;
global $acceptContentType;
global $endpoints;
global $lodspk;
$this->validateDirectory($conf, $file);
$filenamearray = explode(".", $file);
$extension = end($filenamearray);
$ct = $this->getContentType($extension);
header("Content-type: " . $ct);
$uri = $localUri;
if ($conf['debug']) {
Logging::log("In " . $conf['static']['directory'] . " static file $file");
}
$htmlExtension = 'html';
if ($conf['static']['haanga'] && substr_compare(
$file,
$htmlExtension,
-strlen($htmlExtension),
strlen($htmlExtension)
) === 0) {
$lodspk['home'] = $conf['basedir'];
$lodspk['baseUrl'] = $conf['basedir'];
$lodspk['module'] = 'static';
$lodspk['root'] = $conf['root'];
$lodspk['contentType'] = $acceptContentType;
$lodspk['ns'] = $conf['ns'];
$lodspk['this']['value'] = $localUri;
$lodspk['this']['curie'] = Utils::uri2curie($localUri);
$lodspk['local']['value'] = $localUri;
$lodspk['local']['curie'] = Utils::uri2curie($localUri);
$lodspk['contentType'] = $acceptContentType;
$lodspk['endpoint'] = $conf['endpoint'];
$lodspk['baseUrl'] = $conf['basedir'];
Utils::processDocument($conf['static']['directory'] . $file, $lodspk, null);
} else {
echo file_get_contents($conf['static']['directory'] . $file);
}
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
private function getContentType($e)
{
$contentTypes = [
'html' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'jsonp' => 'application/javascript',
'nt' => 'text/plain',
'ttl' => 'text/turtle',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'pdf' => 'application/pdf',
'zip' => 'application/zip',
'gz' => 'application/gzip',
'svg' => 'image/svg+xml',
];
//Add new/override existing mime types defined by user.
if (isset($conf['static']['mimetypes'])) {
foreach ($conf['static']['mimetypes'] as $k => $v) {
$contentTypes[$k] = $v;
}
}
if (isset($contentTypes[$e])) {
return $contentTypes[$e];
}
return "";
/**
* Validate that resource directory is valid and safe to use.
*
* @param array $conf
* Global configuration.
* @param $file
*
* @return void
*/
private function validateDirectory(array $conf, $file): void
{
$staticDirectory = realpath($conf['static']['directory']);
$imgDirectory = realpath($conf['static']['directory'] . "img");
$resourcePath = realpath($conf['static']['directory'] . $file);
if (strpos($resourcePath, $staticDirectory) !== 0 && strpos($resourcePath, $imgDirectory) !== 0) {
HTTPStatus::send404($file);
}