/** * Method that sets the cookie headers * @param $cookies */ public static function setCookieHeaders($cookies) { if (!empty($cookies) && is_array($cookies)) { foreach ($cookies as $cookie) { setcookie($cookie["name"], $cookie["value"], array_key_exists('expire', $cookie) ? $cookie["expire"] : NULL, array_key_exists('path', $cookie) ? $cookie["path"] : "/", array_key_exists('domain', $cookie) ? $cookie["domain"] : Request::getInstance()->getRootUrl(FALSE), array_key_exists('secure', $cookie) ? $cookie["secure"] : FALSE, array_key_exists('http', $cookie) ? $cookie["http"] : FALSE); } } }
/** * Stats initializer */ private function initiateStats() { Logger::log('Initialicing stats (mem + ts)'); if (null !== $_SERVER && array_key_exists('REQUEST_TIME_FLOAT', $_SERVER)) { $this->ts = (double) $_SERVER['REQUEST_TIME_FLOAT']; } else { $this->ts = $this->parser->getTs(); } $this->mem = memory_get_usage(); }
/** * Función que copia los recursos de las carpetas Public al DocumentRoot * @param $string * @param null $name * @param bool|TRUE $return * * @return string */ public static function asset($string, $name = null, $return = true) { $file_path = ""; if (!file_exists($file_path)) { $file_path = BASE_DIR . $string; } $filename_path = AssetsParser::findDomainPath($string, $file_path); $file_path = self::processAsset($string, $name, $return, $filename_path); $return_path = empty($name) ? Request::getInstance()->getRootUrl() . '/' . $file_path : $name; return $return ? $return_path : ''; }
/** * Basic test for Request functionality */ public function testRequest() { $request = Request::getInstance(); // Is Request instance? $this->assertTrue($request instanceof Request); // Check headers, uploads and cookies checkers $this->assertTrue(is_bool($request->hasHeader("session"))); $this->assertTrue(is_bool($request->hasUpload())); $this->assertTrue(is_bool($request->hasCookies())); $this->assertTrue(is_bool($request->isAjax())); // Checks if timestamp was generated $this->assertNotNull($request->getTs()); }
/** * Check service authentication * @return bool */ private function checkAuth() { $namespace = explode('\\', $this->getModelTableMap()); $module = strtolower($namespace[0]); $secret = Config::getInstance()->get($module . '.api.secret'); if (NULL === $secret) { $secret = Config::getInstance()->get("api.secret"); } if (NULL === $secret) { $auth = TRUE; } else { $token = Request::getInstance()->getHeader('X-API-SEC-TOKEN'); if (array_key_exists('API_TOKEN', $this->query)) { $token = $this->query['API_TOKEN']; } $auth = Security::checkToken($token ?: '', $secret, $module); } return $auth || $this->isAdmin(); }
/** * Check CROS requests */ public static function checkCORS() { Logger::log('Checking CORS'); $corsEnabled = Config::getInstance()->get('cors.enabled'); $request = Request::getInstance(); if (NULL !== $corsEnabled) { if ($corsEnabled === '*' || preg_match($corsEnabled, $request->getServer('HTTP_REFERER'))) { if (!headers_sent()) { // TODO include this headers in Template class output method header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); header("Access-Control-Allow-Headers: Access-Control-Allow-Methods, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Origin, X-Requested-With, Content-Type, Accept, Authorization, X-API-SEC-TOKEN, X-API-USER-TOKEN"); } if (Request::getInstance()->getMethod() == 'OPTIONS') { Logger::log('Returning OPTIONS header confirmation for CORS pre flight requests'); header("HTTP/1.1 200 OK"); exit; } } } }
/** * Método que extrae los datos del formulario * @return Form */ public function hydrate() { $data = Request::getInstance()->getData() ?: []; //Hidratamos los campos con lo que venga del formulario $form_name = $this->getName(); if (array_key_exists($form_name, $data)) { foreach ($this->fields as $key => &$field) { list($data, $field) = $this->hydrateField($data, $form_name, $key, $field); } //Limpiamos los datos unset($data[$form_name]); } //Cargamos los campos extras $this->extra = $data; return $this; }
/** * Método que devuelve una ruta del framework * * @param string $slug * @param boolean $absolute * @param array $params * * @return string|null * @throws RouterException */ public function getRoute($slug = '', $absolute = FALSE, $params = []) { if (strlen($slug) === 0) { return $absolute ? Request::getInstance()->getRootUrl() . '/' : '/'; } if (NULL === $slug || !array_key_exists($slug, $this->slugs)) { throw new RouterException(_("No existe la ruta especificada")); } $url = $absolute ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; if (!empty($params)) { foreach ($params as $key => $value) { $url = str_replace("{" . $key . "}", $value, $url); } } elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) { $url = $absolute ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"]; } return preg_replace('/(GET|POST|PUT|DELETE|ALL)\\#\\|\\#/', '', $url); }
/** * Method that check if the user is trying to save the config * @return bool */ public function checkTryToSaveConfig() { $uri = Request::getInstance()->getRequestUri(); $method = Request::getInstance()->getMethod(); return preg_match('/^\\/admin\\/(config|setup)$/', $uri) !== false && strtoupper($method) === 'POST'; }
/** * Hydrate data from request */ private function hydrateRequestData() { $request = Request::getInstance(); $this->query = array_merge($this->query, $request->getQueryParams()); $this->data = array_merge($this->data, $request->getData()); }
/** * Método que devuelve el objeto de petición * @return \PSFS\base\Request */ protected function getRequest() { return Request::getInstance(); }
/** * Método que calcula si se está logado o para acceder a administración * @return bool */ public function canAccessRestrictedAdmin() { return null !== $this->admin || false != preg_match('/^\\/admin\\/login/i', Request::requestUri()); }
/** * Método que cierra y limpia los buffers de salida */ public function closeRender() { Logger::log('Close template render'); Security::getInstance()->setSessionKey("lastRequest", array("url" => Request::getInstance()->getRootUrl() . Request::requestUri(), "ts" => microtime(true))); Security::getInstance()->updateSession(); Logger::log('End request: ' . Request::requestUri(), LOG_INFO); exit; }