/** * Respond function to echo json with status and data * * @param boolean $success Success status of the call * @param integer|boolean $status The StatusCode of the call * @param array|boolean $data Extra data * * @return array */ protected static function respond($success, $status = false, $data = false) { Request::contentType('application/json'); $reply = ['success' => $success]; $reply['multiple'] = false; $reply['status'] = []; if ($status !== false) { if (is_array($status)) { $reply['multiple'] = true; $reply['status']['codes'] = []; $reply['status']['hex'] = []; $reply['status']['names'] = []; $reply['status']['messages'] = []; foreach ($status as $stat) { $object = StatusCodeHandler::find($stat); $reply['status']['codes'][] = $object->code(); $reply['status']['hex'][] = $object->hex(); $reply['status']['names'][] = $object->name(); $reply['status']['messages'][] = $object->readable(); } } else { $object = StatusCodeHandler::find($status); $reply['status']['code'] = $object->code(); $reply['status']['hex'] = $object->hex(); $reply['status']['name'] = $object->name(); $reply['status']['message'] = $object->readable(); } } if ($data !== false) { $reply['data'] = $data; } return $reply; }
/** * Checks whether the request uri is a match * * @param string $request * * @return boolean */ public function match($request) { if (is_array($this->_route) && isset($this->_route['environment']) && $this->_route['environment'] != App::environment()) { return false; } if (is_string($this->_handle)) { //split handle for multi handle $handles = explode('|', $this->_handle); foreach ($handles as $handle) { $handle_matched = true; //clear previous data Request::clear(); //ignore starting and trailing slashes $q_index = strpos($request, '?'); if ($q_index === false) { $ex_request = explode('/', trim($request, '/')); } else { $ex_request = explode('/', trim(substr($request, 0, $q_index), '/')); $ex_request[count($ex_request) - 1] .= substr($request, $q_index); } $ex_handle = explode('/', $handle); //if the amount of parts dont comply just, end if (count($ex_request) != count($ex_handle)) { continue; } //check all parts of the handle and see whether they match up to the request $ex_count = count($ex_handle); $rx_matches; $i = 0; while ($i < $ex_count) { //check $handle_part = $ex_handle[$i]; $request_part = $ex_request[$i]; //check {get} parameter first if ($i == $ex_count - 1) { //checking the last part of the handle //+{get} if (preg_match("/^(.*)(\\+{get})\$/i", $handle_part)) { //$handle_part ends with +{get} //thus get parameters are allowed //get rid of +{get} in the handle $handle_part = substr($handle_part, 0, strlen($handle_part) - 6); //get rid of anything after first question mark in the request part $qm_pos = strpos($request_part, '?'); if ($qm_pos !== false) { //remove GET part from URL $request_part = substr($request_part, 0, $qm_pos); } } } //check up //most complicated structure -> regexed URL variable if (preg_match("/^(rx)(\\{)([a-zA-Z0-9_]+)(\\})(\\{)(.*)(\\})\$/", $handle_part, $rx_matches) && preg_match("/^" . $rx_matches[6] . "\$/", $request_part)) { //regex for URL variable matches and handle is a regexed variable //setData on the UrlHandler to set URL parameter with name and value Request::set($rx_matches[3], $request_part); } elseif (preg_match("/^(\\{)([a-zA-Z0-9_]+)(\\})\$/", $handle_part, $rx_matches)) { //the handle is a non regex URL variable //just set whatever is in the URL to the variable Request::set($rx_matches[2], $request_part); } elseif (!(preg_match("/^(rx)(\\{)(.*)(\\})\$/", $handle_part, $rx_matches) && preg_match("/" . $rx_matches[3] . "/", $request_part) || preg_match("/^(\\*+)\$/", $handle_part) || $request_part == $handle_part)) { //if all of te above fails, return false $handle_matched = false; } ++$i; } if (!$handle_matched) { continue; } //set the route on the UrlHandler Request::route($this); return true; } } return false; }
/** * * @param type $asset */ public function imageURL($asset, $namespace = null) { $base = Core\Request::baseMediaUrl(Module\MODULE_ID) . '/'; $base .= $namespace ? $namespace : $this->getLayout(); return $base . "/images/{$asset}"; }
/** * * @param type $asset */ public function imageURL($asset) { return Core\Request::baseMediaUrl(Module\MODULE_ID) . "/{$asset}"; }
/** * Executes the router * * @return boolean */ public static function start() { $request = Request::path(); //allow method spoofing $post = Request::$post; if (isset($post['_method'])) { Request::method($post['_method']); } //check home route if (isset(self::$_home) && self::$_home->match($request)) { self::$_home->execute(); return true; } //check any routes foreach (self::$_any as $handle => $route_obj) { if ($route_obj->match($request)) { $route_obj->execute(); return true; } } //check for method routes | POST or GET $relevant_requests; if (Request::method() == "POST") { $relevant_requests = self::$_post; } elseif (Request::method() == "GET") { $relevant_requests = self::$_get; } elseif (Request::method() == "PUT") { $relevant_requests = self::$_put; } elseif (Request::method() == "DELETE") { $relevant_requests = self::$_delete; } elseif (Request::method() == "PATCH") { $relevant_requests = self::$_patch; } elseif (Request::method() == "OPTIONS") { $relevant_requests = self::$_options; } //check the releavant requests foreach ($relevant_requests as $handle => $route_obj) { if ($route_obj->match($request)) { $route_obj->execute(); return true; } } //no routes have been executed here //check for error page if (isset(self::$_default)) { self::$_default->execute(); return true; } App::throw(0x194); return false; }
/** * Runs the app */ public static function run() { // set display errors // upon development environment if (self::$_environment == 'development' || self::$_environment == 'dev') { ini_set('display_errors', 1); } // Set directories self::$_directoryBackups = new Directory(self::$_directorySystem . '/' . self::$_directoryBackups); self::$_directoryBlueprints = new Directory(self::$_directorySystem . '/' . self::$_directoryBlueprints); self::$_directoryConfig = new Directory(self::$_directorySystem . '/' . self::$_directoryConfig); self::$_directoryControllers = new Directory(self::$_directorySystem . '/' . self::$_directoryControllers); self::$_directoryLayouts = new Directory(self::$_directorySystem . '/' . self::$_directoryLayouts); self::$_directoryLibs = new Directory(self::$_directorySystem . '/' . self::$_directoryLibs); self::$_directoryLogs = new Directory(self::$_directorySystem . '/' . self::$_directoryLogs); self::$_directoryMeta = new Directory(self::$_directorySystem . '/' . self::$_directoryMeta); self::$_directoryModels = new Directory(self::$_directorySystem . '/' . self::$_directoryModels); self::$_directoryModules = new Directory(self::$_directorySystem . '/' . self::$_directoryModules); self::$_directoryViewOutput = new Directory(self::$_directorySystem . '/' . self::$_directoryViewOutput); self::$_directoryViews = new Directory(self::$_directorySystem . '/' . self::$_directoryViews); self::$_directoryObjects = new Directory(self::$_directorySystem . '/' . self::$_directoryObjects); self::$_directorySystem = new Directory(self::$_directorySystem); self::$_directoryPublic = new Directory(self::$_directoryPublic); // run self::integrity(); PackagistHandler::start(); self::libraries(); if (self::$_modifier < App::BAREBONES) { BackupManager::create(); SessionHandler::start(); Request::start(); self::configure(); } else { self::bareconfig(); } if (self::$_modifier < App::NO_ROUTING) { Router::start(); } }