Skip to content
Snippets Groups Projects
Commit 6e00b2c7 authored by Stein Magne Bjorklund's avatar Stein Magne Bjorklund
Browse files

Issue 6 Reformatting old module code

- Improve readability
parent d5fe2b0b
No related branches found
No related tags found
1 merge request!5add check for static dir and static/img
<?php <?php
require_once('abstractModule.php'); require_once('abstractModule.php');
class StaticModule extends abstractModule{ class StaticModule extends abstractModule{
//Static module
public function match($uri)
public function match($uri){ {
global $conf; global $conf;
global $localUri; global $localUri;
global $uri; global $uri;
global $acceptContentType; global $acceptContentType;
global $endpoints; global $endpoints;
global $lodspk; global $lodspk;
$q = preg_replace('|^'.$conf['basedir'].'|', '', $localUri); $q = preg_replace('|^' . $conf['basedir'] . '|', '', $localUri);
if(strlen($q)>0 && file_exists($conf['home'].$conf['static']['directory'].$q)){
return $q; if (strlen($q) > 0 && file_exists($conf['home'] . $conf['static']['directory'] . $q)) {
} return $q;
return FALSE; }
return FALSE;
} }
public function execute($file){ public function execute($file)
global $conf; {
global $localUri; global $conf;
global $uri; global $localUri;
global $acceptContentType; global $uri;
global $endpoints; global $acceptContentType;
global $lodspk; global $endpoints;
global $lodspk;
$this->validateDirectory($conf, $file); $this->validateDirectory($conf, $file);
$filenamearray =explode(".",$file); $filenamearray = explode(".", $file);
$extension = end($filenamearray); $extension = end($filenamearray);
$ct = $this->getContentType($extension); $ct = $this->getContentType($extension);
header("Content-type: ".$ct); header("Content-type: " . $ct);
$uri = $localUri; $uri = $localUri;
if($conf['debug']){
Logging::log("In ".$conf['static']['directory']." static file $file"); 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){ $htmlExtension = 'html';
$lodspk['home'] = $conf['basedir'];
$lodspk['baseUrl'] = $conf['basedir']; if ($conf['static']['haanga'] && substr_compare($file, $htmlExtension, -strlen($htmlExtension), strlen($htmlExtension)) === 0) {
$lodspk['module'] = 'static'; $lodspk['home'] = $conf['basedir'];
$lodspk['root'] = $conf['root']; $lodspk['baseUrl'] = $conf['basedir'];
$lodspk['contentType'] = $acceptContentType; $lodspk['module'] = 'static';
$lodspk['ns'] = $conf['ns']; $lodspk['root'] = $conf['root'];
$lodspk['this']['value'] = $localUri; $lodspk['contentType'] = $acceptContentType;
$lodspk['this']['curie'] = Utils::uri2curie($localUri); $lodspk['ns'] = $conf['ns'];
$lodspk['local']['value'] = $localUri; $lodspk['this']['value'] = $localUri;
$lodspk['local']['curie'] = Utils::uri2curie($localUri); $lodspk['this']['curie'] = Utils::uri2curie($localUri);
$lodspk['contentType'] = $acceptContentType; $lodspk['local']['value'] = $localUri;
$lodspk['endpoint'] = $conf['endpoint']; $lodspk['local']['curie'] = Utils::uri2curie($localUri);
$lodspk['baseUrl'] = $conf['basedir']; $lodspk['contentType'] = $acceptContentType;
$lodspk['endpoint'] = $conf['endpoint'];
Utils::processDocument($conf['static']['directory'].$file, $lodspk, null); $lodspk['baseUrl'] = $conf['basedir'];
}else{
echo file_get_contents($conf['static']['directory'].$file); Utils::processDocument($conf['static']['directory'] . $file, $lodspk, null);
} }
else {
echo file_get_contents($conf['static']['directory'] . $file);
}
} }
private function getContentType($e){ private function getContentType($e)
$contentTypes = array('html' => 'text/html', {
'css' => 'text/css', $contentTypes = [
'js' => 'application/javascript', 'html' => 'text/html',
'json' => 'application/json', 'css' => 'text/css',
'jsonp'=> 'application/javascript', 'js' => 'application/javascript',
'nt' => 'text/plain', 'json' => 'application/json',
'ttl' => 'text/turtle', 'jsonp' => 'application/javascript',
'png' => 'image/png', 'nt' => 'text/plain',
'jpg' => 'image/jpeg', 'ttl' => 'text/turtle',
'gif' => 'image/gif', 'png' => 'image/png',
'bmp' => 'image/bmp', 'jpg' => 'image/jpeg',
'pdf' => 'application/pdf', 'gif' => 'image/gif',
'zip' => 'application/zip', 'bmp' => 'image/bmp',
'gz' => 'application/gzip', 'pdf' => 'application/pdf',
'svg' => 'image/svg+xml' '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){ //Add new/override existing mime types defined by user.
$contentTypes[$k] = $v; if (isset($conf['static']['mimetypes'])) {
} foreach ($conf['static']['mimetypes'] as $k => $v) {
} $contentTypes[$k] = $v;
}
if(isset($contentTypes[$e])){ }
return $contentTypes[$e];
} if (isset($contentTypes[$e])) {
return ""; //empty string seems to work fine with browsers return $contentTypes[$e];
}
return "";
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment