output = Factory::getInstance()->output; } public function resourceExists(string $requestURL): bool { // First find the resource $file = $this->findResource($requestURL); // If not found, return false; if (is_null($file)) return false; // If found, simply return true return true; } /** * Serves a static file if found. * * @param string $requestURL * @return bool * @throws WebException * * @todo Bypass the Output system and use the readFile() method. * @todo Run as FuzeWorks pre-code, before creating the container */ public function serveResource(string $requestURL): bool { // First find the resource $file = $this->findResource($requestURL); // If not found return false if (is_null($file)) return false; // If a file is found, fire a serveResourceEvent /** @var ResourceServeEvent $event */ try { $event = Events::fireEvent('resourceServeEvent', $file['resourceName'], $file['requestURL'], $file['file']); } catch (Exception\EventException $e) { throw new WebException("Could not serve resource. resourceServeEvent threw exception: '" . $e->getMessage() . "'"); } // If cancelled, don't serve if ($event->isCancelled()) return false; // Log the resource serving Logger::log("Serving static resource '/" . $file['resourceName'] . '/' . $file['requestURL'] . "'"); // Serve file in accordance with event $fileExtension = pathinfo($event->resourceFilePath, PATHINFO_EXTENSION); $this->output->setContentType($fileExtension); $this->output->setOutput(file_get_contents($event->resourceFilePath)); #readfile($event->resourceFilePath); // And return true at the end return true; } protected function findResource(string $requestURL): ?array { // First segment should be the resourceName, check if it exists foreach ($this->resources as $resourceName => $resourceDir) { if (substr($requestURL, 0, strlen($resourceName)) === $resourceName) { $fileURL = ltrim(substr($requestURL, strlen($resourceName)), '/'); $fileURL = str_replace('/', DS, $fileURL); $file = $this->resources[$resourceName] . DS . $fileURL; // Test if file exists, if it does, return the string if (file_exists($file) && is_file($file)) return ['file' => $file, 'resourceName' => $resourceName, 'requestURL' => $fileURL]; } } return null; } /** * Register a resource which can be served statically. * * The resourceName will be the directory under which the files shall be served on the web server. * The filePath is where FuzeWorks should look for the files. * * @param string $resourceName * @param string $filePath * @throws WebException * @return bool */ public function registerResources(string $resourceName, string $filePath): bool { // First check if the resource already exists $resourceName = urldecode($resourceName); if (isset($this->resources[$resourceName])) throw new WebException("Could not register resources. Resources with same name already exists."); // Also check if the file path exists and is a directory if (!file_exists($filePath) && !is_dir($filePath)) throw new WebException("Could not register resources. Provided filePath does not exist."); // Add the resource $this->resources[$resourceName] = $filePath; // Log the registration Logger::log("Adding static resources on: '/" . $resourceName . "'"); return true; } }