/** * __construct * @param object $autoLoader - Autoloader instance * @param string $basePath */ public function __construct($autoLoader = null, $basePath = null) { $this->_autoLoader = $autoLoader; //Base Working Directory if (!defined('BASE_PATH')) { if (is_null($basePath)) { define('BASE_PATH', rtrim(realpath(dirname($_SERVER['DOCUMENT_ROOT'])), DIRECTORY_SEPARATOR)); } else { define('BASE_PATH', rtrim($basePath, DIRECTORY_SEPARATOR)); } } //Error Logging $error = new \Touchbase\Debug\Error(); //Configure Touchbase Project $this->setConfig($this->_configure(ConfigStore::create())); if (!defined('TOUCHBASE_ENV')) { define("TOUCHBASE_ENV", $this->config()->get("project")->get("environment", "production"), true); } //Work out touchbase path if ($this->_autoLoader instanceof \Composer\Autoload\ClassLoader) { $prefixes = $this->_autoLoader->getPrefixesPsr4(); foreach ($prefixes as $prefix => $path) { if (rtrim($prefix, "\\") == __NAMESPACE__) { $nsBasePath = realpath($path[0]) . DIRECTORY_SEPARATOR; break; } } } if (!defined('TOUCHBASE_PATH')) { define('TOUCHBASE_PATH', rtrim(realpath($nsBasePath), DIRECTORY_SEPARATOR)); } //Base Working Directory if (!defined('WORKING_DIR')) { // Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements $path = realpath($_SERVER['SCRIPT_FILENAME']); if (substr($path, 0, strlen(BASE_PATH)) == BASE_PATH) { $urlSegmentToRemove = substr($path, strlen(BASE_PATH)); if (substr($_SERVER['SCRIPT_NAME'], -strlen($urlSegmentToRemove)) == $urlSegmentToRemove) { $baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove)); } } define('WORKING_DIR', strtolower(rtrim(isset($baseURL) ? $baseURL : $this->config()->get("project")->get("working_dir", ""), "/")) . "/"); } if (!Router::isCLI()) { if (!defined('SITE_PROTOCOL')) { $protocol = ''; if (isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) == 'https') { $protocol = 'https://'; } else { if (isset($_SERVER['SSL']) || !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { $protocol = 'https://'; } else { $protocol = 'http://'; } } define('SITE_PROTOCOL', $protocol); } if (!defined('SITE_URL')) { define("SITE_URL", Router::buildPath(SITE_PROTOCOL . htmlentities(filter_var(strtolower(@$_SERVER['HTTP_X_FORWARDED_HOST'] ?: $_SERVER['HTTP_HOST']), FILTER_SANITIZE_URL), ENT_QUOTES, 'UTF-8'), WORKING_DIR), true); define("SITE_ROOT", SITE_URL, true); } } // \pre_r(BASE_PATH); // \pre_r(TOUCHBASE_PATH); // \pre_r(PROJECT_PATH); // \pre_r(SITE_URL); // \pre_r(TOUCHBASE_ENV); }
/** * 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; }
/** * Handle Request * Calls this controller and returns a HTTPResponse Object * @param \Touchbase\Control\HTTPRequest &$request * @param \Touchbase\Control\HTTPResponse &$response * @return string - The outgoing HTML */ public function handleRequest(HTTPRequest &$request, HTTPResponse &$response) { //Set Request/Response Into Var $this->request =& $request; $this->response =& $response; //If we had a redirection, cancel. if ($response->hasFinished()) { return $response; } //Pass through to RequestHandler $body = parent::handleRequest($request, $response); if ($body instanceof HTTPResponse) { $response = $body; } else { $body = $response->setBody($body)->body(); } return $body; }
/** * Render * Output layout as a string * @return string */ public function render() { if (!$this->controller->request()->isAjax()) { $this->body = $this->constructLayout(); } $this->validateHtml($this->body); return parent::render(); }