Skip to content
Snippets Groups Projects
StaticModule.php 3.86 KiB
Newer Older
<?php declare(strict_types=1);

namespace uib\ub\loadspeakr\modules;
alvaro's avatar
alvaro committed

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;
    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);
        }
    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);
        }
alangrafu's avatar
alangrafu committed
}