Beispiel #1
0
 /**
  *	Handle Request
  *	This method will attempt to load a real resource located on the server if one exists.
  *	@param \Touchbase\Control\HTTPRequest $request
  *	@param \Touchbase\Control\HTTPResponse &$response
  *	@return mixed
  */
 private static function handleRequest(HTTPRequest $request, HTTPResponse &$response)
 {
     //TopSite Preview
     if (isset($_SERVER["HTTP_X_PURPOSE"]) && $_SERVER["HTTP_X_PURPOSE"] == "preview") {
         $assetFile = File::create([BASE_PATH, 'public_html', 'preview.html']);
         //Favicon
     } else {
         if ($request->urlSegment() == "favicon") {
             $assetFile = File::create([BASE_PATH, 'public_html', 'favicon.ico']);
             if (!$assetFile->exists()) {
                 //Write an empty favicon if one doesn't exist
                 $assetFile->write(base64_decode("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAFklEQVR42mNkoBAwjhowasCoAcPFAAAmmAARm5JBWgAAAABJRU5ErkJggg=="));
             }
             //Asset Map
         } else {
             $assetFilePath = static::pathForAssetMap($request->urlSegment());
             if ($assetFilePath) {
                 $assetFile = File::create([BASE_PATH, $assetFilePath, implode("/", $request->urlSegments(1)) . '.' . $request->extension()]);
             }
         }
     }
     if (isset($assetFile)) {
         $supportedAssets = ["css" => "text/css; charset=utf-8", "js" => "text/javascript; charset=utf-8", "htc" => "text/x-component", "png" => "image/png", "jpg" => "image/jpg", "gif" => "image/gif", "svg" => "image/svg+xml", "ico" => "image/x-icon", "otf" => "application/font-otf", "eot" => "application/vnd.ms-fontobject", "ttf" => "application/font-ttf", "woff" => "application/font-woff", "woff2" => "application/font-woff2", "apk" => "application/vnd.android.package-archive", "ipa" => "application/octet-stream"];
         if ($assetFile->exists() && array_key_exists($assetFile->ext(), $supportedAssets)) {
             session_cache_limiter(false);
             $response->addHeader("Content-Type", $supportedAssets[$assetFile->ext()]);
             $response->addHeader("Content-Disposition", "attachment; filename=" . $assetFile->name);
             $response->addHeader('Content-Length', $assetFile->size());
             //TODO: Should be done in response setBody!
             if (php_sapi_name() != 'cli-server' && static::config("assets")->get("x_sendfile", false)) {
                 $response->addHeader("X-Sendfile", $assetFile->path);
             } else {
                 $response->setBody($assetFile->read());
             }
             return true;
         }
     }
     return false;
 }