protected static function _route_method($rel_uri) { // Convert the path to lowercase for namespaces $path = explode('/', $rel_uri); // Split controllers and parameters $url_parts = array('controller' => $rel_uri, 'params' => null); $c_path = APPPATH . 'controllers' . DIRECTORY_SEPARATOR; foreach ($path as $p) { if (is_dir($c_path . $p)) { $c_path .= strtolower($p) . DIRECTORY_SEPARATOR; } else { if (is_file($c_path . ucwords($p) . '.php')) { $c_path .= strtolower($p) . DIRECTORY_SEPARATOR; $url_parts['controller'] = rtrim(substr($c_path, strlen(APPPATH . 'controllers' . DIRECTORY_SEPARATOR)), '/'); break; } } } if ($t = str_replace($url_parts['controller'], '', $rel_uri)) { $url_parts['params'] = explode('/', trim($t, '/')); } else { $url_parts['params'] = null; } // Capitalize the last item (class name) and strip the extension $c_namespace = explode('/', $url_parts['controller']); $ridx = count($c_namespace) - 1; $c_namespace[$ridx] = str_replace('-', '_', trim(ucwords($c_namespace[$ridx]))); if (!$c_namespace[$ridx]) { die('Bad controller: ' . $c_namespace[$ridx]); } // Set PHP_SELF $_SERVER['PHP_SELF'] = implode('/', $c_namespace) . '.php'; // Prepend the necessary namespace part $class = 'app\\controllers\\' . implode('\\', $c_namespace); // Include the class if (!class_exists($class)) { Exception::error('Invalid controller'); } // Create instance /** * @var $MVC \sys\core\Controller */ $MVC = new $class(); // Assign a valid method depending upon the REQUEST_METHOD // If no handler is specified, then index is called $method = 'index'; $params = array(); if ($url_parts['params'] !== null) { $method = array_shift($url_parts['params']); $params = $url_parts['params']; } if (!is_callable(array($class, $method))) { Exception::error('No method defined to handle request'); } // And we have lift off... if ($MVC->dispatch_request()) { call_user_func_array(array($MVC, $method), $params); } }
/** * Set a HTTP response header * @param int $code * @param string $text */ public static function set_header($code = 200, $text = '') { $stati = array(200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 422 => 'Unprocessable Entity', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported'); if (empty($code) or !is_numeric($code)) { Exception::error('Status codes must be numeric', 500); } is_int($code) or $code = (int) $code; if (empty($text)) { if (isset($stati[$code])) { $text = $stati[$code]; } else { Exception::error('No status text available. Please check your status code number or supply your own message text.', 500); } } $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : false; if (strpos(php_sapi_name(), 'cgi') === 0) { header('Status: ' . $code . ' ' . $text, true); } else { header(($server_protocol ? $server_protocol : 'HTTP/1.1') . ' ' . $code . ' ' . $text, true, $code); } }
/** * Loads a config file and appends to the existing config * @param string $file File to load config from. * @return bool */ public function load($file = '') { $file = $file === '' ? 'config' : str_replace('.php', '', $file); $found = $loaded = false; $file_path = null; foreach ($this->_config_paths as $path) { foreach (array(ENVIRONMENT . '/' . $file, $file) as $location) { $file_path = $path . 'config/' . $location . '.php'; // Check if we have already loaded the file if (in_array($file_path, $this->_loaded_files, true)) { $loaded = true; continue 2; } if (file_exists($file_path)) { $found = true; break; } } if ($found === false) { continue; } require_once $file_path; // Whoa! We didn't find no $config var... Abort! if (!isset($config) or !is_array($config)) { Exception::error('Your ' . $file_path . ' file does not appear to contain a valid configuration array.'); } /** @var $config array */ $this->config = array_merge($this->config, $config); $this->_loaded_files[] = $file_path; // Clean up $config var for the next customer unset($config); $loaded = true; break; } if ($loaded === false) { Exception::error('The configuration file ' . $file . '.php does not exist.'); } return true; }
public function addUser($pid, $uid, $role = self::ROLE_ADMIN) { $projectRoleUri = $this->getRoleUri($pid, $role); $uri = "/gdc/projects/{$pid}/users"; $params = ['user' => ['content' => ['status' => 'ENABLED', 'userRoles' => [$projectRoleUri]], 'links' => ['self' => Users::getUriFromUid($uid)]]]; $result = $this->client->post($uri, $params); if (isset($result['projectUsersUpdateResult']['successful']) && count($result['projectUsersUpdateResult']['successful']) || isset($result['projectUsersUpdateResult']['failed']) && !count($result['projectUsersUpdateResult']['failed'])) { // SUCCESS // Sometimes API does not return } else { $errors = []; if (isset($result['projectUsersUpdateResult']['failed'])) { foreach ($result['projectUsersUpdateResult']['failed'] as $f) { $errors[] = $f['message']; } } throw Exception::error($uri, 'Error in addition to project: ' . implode('; ', $errors)); } }