public static function input($arguments) { if (!defined("STDIN")) { define("STDIN", fopen('php://stdin', 'r')); } $_argv = array(); $_argv['command'] = null; $_argv['args'] = array(); $_argv['options'] = array(); $i = 0; foreach ($arguments as $k => $v) { $v = strtolower($v); if (substr($v, 0, 2) == '--') { $_argv['options'][] = $v; } else { if ($i == 1) { $_argv['command'] = $v; } if ($i > 1) { $_argv['args'][] = $v; } } $i++; } $command = $_argv['command']; $method = 'help'; if (strpos($command, ':') !== false) { list($command, $method) = explode(':', $command); } static::$command = $command; static::$method = $method; static::$arguments = $_argv['args']; static::$options = $_argv['options']; return $_argv; }
/** * Actual routing + sanitizing data * * @param $class * @param array $params */ public static function connect($namespace, $class, $params = array()) { $defaults = array('indexPage' => 'index', 'loginPage' => false, 'loginRedirect' => false); static::$class = strtolower($class); $class = $namespace . '\\' . $class; $params += $defaults; extract($params); // Authenticated controllers if ($loginPage) { Auth::checkLogin($loginRedirect, $loginPage); } $method = $indexPage; $parameters = array(); if (isset($_SERVER[URI_INFO])) { $url = explode('/', substr($_SERVER[URI_INFO], 1)); array_shift($url); if ($url) { foreach ($url as $key => $element) { if (!$key && !is_numeric($element)) { $method = $element; } else { $parameters[] = $element; } } } } // Check availability try { $methodInfo = new \ReflectionMethod($class, $method); // Methods that start with _ are not accesible from browser $name = $methodInfo->getName(); if ($name[0] == '_') { $method = $indexPage; } $methodParams = $methodInfo->getParameters(); // Force cast parameters by arguments default value if ($methodParams) { foreach ($methodParams as $parameterKey => $parameterValue) { try { $defaultValue = $parameterValue->getDefaultValue(); $type = gettype($defaultValue); if ($defaultValue) { unset($methodParams[$parameterKey]); } // settype($parameters[$parameterKey], $type); } catch (\Exception $e) { continue; } } } // if(count($methodParams) != count($parameters)) { // $parameters = array(); // } } catch (\Exception $e) { $method = $indexPage; } static::$method = $method; call_user_func_array($class . '::' . $method, $parameters); return; }
public function before() { $this->template = $this->layout . '/template'; $this->controller = Request::active()->controller; $this->action = Request::active()->action; static::$method = $this->controller . '/' . $this->action; $this->render_template(); parent::before(); $this->set_path(); $this->check_maintenance(); $this->init(); }
public function before() { $this->controller = Request::active()->controller; $this->action = Request::active()->action; static::$method = $this->controller . '/' . $this->action; $this->data['success'] = false; Lang::load('app'); $this->render_template(); parent::before(); $this->set_title(); $this->set_path(); $this->init(); }
protected static function initialize() { if (static::$initialized) { return; } static::$initialized = true; static::$method = $_SERVER['REQUEST_METHOD']; static::$request = $_SERVER['REQUEST_URI']; static::$script = $_SERVER['SCRIPT_NAME']; static::$original = null; static::$redirect = null; static::applyRoutes(); static::route(); }
public static function dispatch() { static::$method = Request::method(); $uri = substr(str_replace('/ws/', '/', $_SERVER['REQUEST_URI']), 1); $tab = explode('/', $uri); if (count($tab) < 3) { Api::forbidden(); } $namespace = current($tab); $controller = $tab[1]; $action = $tab[2]; $tab = array_slice($tab, 3); $count = count($tab); if (0 < $count && $count % 2 == 0) { for ($i = 0; $i < $count; $i += 2) { $_REQUEST[$tab[$i]] = $tab[$i + 1]; } } $file = APPLICATION_PATH . DS . 'webservices' . DS . $namespace . DS . $controller . '.php'; if (!File::exists($file)) { Api::NotFound(); } require_once $file; $class = 'Thin\\' . ucfirst($controller) . 'Ws'; $i = new $class(); $methods = get_class_methods($i); $call = strtolower(static::$method) . ucfirst($action); if (!Arrays::in($call, $methods)) { Api::NotFound(); } if (Arrays::in('init', $methods)) { $i->init($call); } $i->{$call}(); if (Arrays::in('after', $methods)) { $i->after(); } }
public static function dispatch($module = null) { $module = is_null($module) ? 'front' : $module; $ever = context()->get('MVC404'); if (true !== $ever) { $file = APPLICATION_PATH . DS . 'config' . DS . SITE_NAME . '_routes.php'; if (File::exists($file)) { $routes = (include $file); static::$routes = isAke($routes, $module, []); } if (count(static::$routes)) { $baseUri = Config::get('application.base_uri', ''); if (strlen($baseUri)) { $uri = strReplaceFirst($baseUri, '', $_SERVER['REQUEST_URI']); } else { $uri = $_SERVER['REQUEST_URI']; } static::$uri = $uri; static::$method = Request::method(); return static::find(); } } return false; }
public static function init() { //Sanitize inputs //.Remove magic quotes if (magic_quotes()) { $magics = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST); foreach ($magics as &$magic) { $magic = array_strip_slashes($magic); } } //.Unset globals foreach (array($_GET, $_POST) as $global) { if (is_array($global)) { foreach ($global as $k => $v) { global ${$k}; ${$k} = NULL; } } } //.Clean post input array_map(function ($v) { return Request::clearValue($v); }, $_POST); //Remove /public/index.html from path_info.. foreach (array("PATH_INFO", "ORIG_PATH_INFO", "PATH_TRANSLATED", "PHP_SELF") as $k) { if (isset($_SERVER[$k])) { $_SERVER[$k] = str_replace("/public/index.html", "/", $_SERVER[$k]); } } static::$server = $_SERVER; static::$get = $_GET; static::$post = $_POST; $_GET = null; $_POST = null; $_SERVER = null; $_REQUEST = null; //Detect environment $list = (require J_PATH . "config" . DS . "environments" . EXT); $env = ""; $envWithWildcard = array_first($list); $hosts = array(array_get(static::$server, "HTTP_HOST", "localhost"), array_get(static::$server, "SERVER_NAME", "localhost"), array_get(static::$server, "SERVER_ADDR", "localhost"), gethostname()); foreach ($hosts as $host) { foreach ($list as $k => $v) { foreach ((array) $v as $hostname) { if ($hostname != "" && $hostname == $host) { $env = $k; break; } else { if ($hostname == "*") { $envWithWildcard = $k; } } } if (!empty($env)) { break; } } if (!empty($env)) { break; } } if (empty($env)) { $env = $envWithWildcard; } static::$env = $env; //Detect method $method = strtoupper(array_get(static::$server, "REQUEST_METHOD", "GET")); if ($method == "POST" && static::hasReq("_method")) { $methodReq = static::req("_method", "POST"); if (array_search($methodReq, static::$availableMethods) !== false) { $method = $methodReq; } } static::$method = $method; }
public static function dispatch() { header("Access-Control-Allow-Origin: *"); static::$method = Request::method(); $uri = substr(str_replace('/mobi/', '/', $_SERVER['REQUEST_URI']), 1); $tab = explode('/', $uri); if (!strlen($uri) || $uri == '/') { $namespace = 'static'; $controller = 'home'; $action = 'index'; } else { if (count($tab) < 3) { self::isForbidden(); } $namespace = current($tab); $controller = $tab[1]; $action = $tab[2]; $tab = array_slice($tab, 3); $count = count($tab); if (0 < $count && $count % 2 == 0) { for ($i = 0; $i < $count; $i += 2) { $_REQUEST[$tab[$i]] = $tab[$i + 1]; } } } $file = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'controllers' . DS . $controller . '.php'; // dd($file); if (!File::exists($file)) { self::is404(); } require_once $file; $class = 'Thin\\' . ucfirst($controller) . 'Mobi'; $i = new $class(); $methods = get_class_methods($i); $call = strtolower(static::$method) . ucfirst($action); if (!Arrays::in($call, $methods)) { self::is404(); } if (Arrays::in('init', $methods)) { $i->init($call); } $i->{$call}(); if ($i->view === true) { $tpl = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'views' . DS . $controller . DS . $action . '.phtml'; if (File::exists($tpl)) { $content = File::read($tpl); $content = str_replace('$this->', '$i->', $content); $fileTpl = CACHE_PATH . DS . sha1($content) . '.display'; File::put($fileTpl, $content); ob_start(); include $fileTpl; $html = ob_get_contents(); ob_end_clean(); File::delete($fileTpl); self::render($html); } else { self::render('OK'); } } if (Arrays::in('after', $methods)) { $i->after(); } }
private static function setRoute($route) { $value = explode('.', $route['value']); $method = explode('/', implode('.', array_slice($value, 1))); $vars = isset($method[1]) ? explode(',', $method[1]) : array(); static::$controller = str_replace('::', '\\', '\\'.$value[0]); static::$method = $method[0]; static::$params = $route['params']; static::$vars = $vars; static::$extension = (isset($route['params']['extension']) ? $route['params']['extension'] : null); }
/** * @return string */ public static function method() { if (!static::$method) { if (static::$post->has('_method')) { static::$method = strtoupper(static::$post->get('_method')); } else { static::$method = $_SERVER['REQUEST_METHOD']; } } return static::$method; }
public function __construct() { static::$instance = $this; static::$request = $_REQUEST; static::$get = $_GET; static::$post = $_POST; static::$server = $_SERVER; static::$headers = static::getAllHeaders(); static::$requestUri = static::prepareRequestUri(); static::$baseUrl = static::prepareBaseUrl(); static::$basePath = static::prepareBasePath(); static::$pathInfo = static::preparePathInfo(); static::$method = static::$server['REQUEST_METHOD']; }
public static function controller($dir, $cn) { static::$method = Request::method(); $uri = substr(str_replace('/api/', '/', $_SERVER['REQUEST_URI']), 1); $tab = explode('/', $uri); dd($tab); if (count($tab) < 1) { Api::forbidden(); } $action = current($tab); $tab = array_slice($tab, 1); $count = count($tab); if (0 < $count && $count % 2 == 0) { for ($i = 0; $i < $count; $i += 2) { $_REQUEST[$tab[$i]] = $tab[$i + 1]; } } $file = $dir . DS . 'controllers' . DS . 'api.php'; if (!File::exists($file)) { Api::NotFound(); } require_once $file; $class = 'Thin\\' . $cn; $i = new $class(); $methods = get_class_methods($i); $call = strtolower(static::$method) . ucfirst($action); if (!Arrays::in($call, $methods)) { Api::NotFound(); } if (Arrays::in('init', $methods)) { $i->init($call); } $i->{$call}(); if (Arrays::in('after', $methods)) { $i->after(); } }
public static function route() { static::$method = Request::method(); $pjax = isAke(Request::headers(), 'x-pjax', []); static::$pjax = !empty($pjax); $uri = substr($_SERVER['REQUEST_URI'], 1); if (static::$pjax) { static::$method = 'GET'; } if (fnmatch("*?*", $uri) && static::$pjax) { $uri = str_replace('?', '/', $uri); $uri = str_replace('=', '/', $uri); $uri = str_replace('&', '/', $uri); } if (!strlen($uri)) { $controller = 'index'; $action = 'home'; } else { $tab = explode('/', $uri); if (count($tab) == 1) { $seg = current($tab); if (strlen($seg) == 2) { $_REQUEST['lng'] = strtolower($seg); $controller = 'index'; $action = 'home'; } else { $controller = strtolower($seg); $action = 'index'; } } elseif (count($tab) == 2) { $first = current($tab); $second = end($tab); if (strlen($first) == 2) { $_REQUEST['lng'] = strtolower($first); $controller = $second; $action = 'index'; } else { $controller = strtolower($first); $action = strtolower($second); } } else { $first = current($tab); $second = $tab[1]; $third = end($tab); if (strlen($first) == 2) { $_REQUEST['lng'] = strtolower($first); $controller = $second; $action = 'index'; } else { $controller = strtolower($first); $action = strtolower($second); $tab = array_slice($tab, 2); $count = count($tab); if (0 < $count && $count % 2 == 0) { for ($i = 0; $i < $count; $i += 2) { $_REQUEST[$tab[$i]] = $tab[$i + 1]; } } } } } static::$route = ['controller' => $controller, 'action' => $action]; }
/** * Static Constructor * * Checks if the current request is an AJAX request and what HTTP method was used * when submitting the request. * * @return void */ public static function __initialize() { $req = static::server('http_x_requested_with', false); static::$ajax = $req and strtolower($req) == 'xmlhttprequest'; static::$method = strtolower(static::server('request_method', 'get')); }
/** * Sets up the request. */ public static function init() { static::$query = new ParameterBag($_GET); static::$post = new ParameterBag($_POST); static::$properties = new ParameterBag(); static::$server = new ParameterBag($_SERVER); static::$headers = static::buildHeaderBag(); static::$method = isset($_POST['_method']) ? $_POST['_method'] : $_SERVER['REQUEST_METHOD']; static::$requestUri = $_SERVER['REQUEST_URI']; static::$scriptName = $_SERVER['SCRIPT_NAME']; static::$basePath = rtrim(str_replace(basename(static::$scriptName), '', static::$scriptName), '/'); static::$pathInfo = str_replace(static::$scriptName, '', static::$requestUri); static::$pathInfo = static::preparePathInfo(); static::$segments = explode('/', trim(static::$pathInfo, '/')); }
public function __construct() { // Because some hosts are complete // idiotic pieces of shit, let's // strip slashes from input. if (get_magic_quotes_gpc()) { $php_is_the_worst_language_ever_because_of_this = function (&$value) { $value = stripslashes($value); }; array_walk_recursive($_GET, $php_is_the_worst_language_ever_because_of_this); array_walk_recursive($_POST, $php_is_the_worst_language_ever_because_of_this); array_walk_recursive($_COOKIE, $php_is_the_worst_language_ever_because_of_this); array_walk_recursive($_REQUEST, $php_is_the_worst_language_ever_because_of_this); } // Set query string static::$query = (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null); // Set request scheme static::$scheme = static::isSecure() ? 'https' : 'http'; // Set host static::$host = strtolower(preg_replace('/:\d+$/', '', trim($_SERVER['SERVER_NAME']))); // Set base url static::$base = static::baseUrl(); // Set the request path static::$request_uri = static::requestPath(); // Set relative uri without query string $uri = explode('?', str_replace(static::$base, '', static::$request_uri)); static::$uri = $uri[0]; // Request segments static::$segments = explode('/', trim(static::$uri, '/')); // Set the request method static::$method = strtolower($_SERVER['REQUEST_METHOD']); // Requested with static::$requested_with = @$_SERVER['HTTP_X_REQUESTED_WITH']; // _REQUEST static::$request = $_REQUEST; // _POST static::$post = $_POST; }
protected static function setRoute($route) { $destination = explode('::', $route->destination); $info = ['controller' => $destination[0], 'method' => $destination[1], 'params' => $route->params, 'defaults' => $route->defaults, 'extension' => isset($route->params['extension']) ? $route->params['extension'] : 'html']; // Remove the first dot from the extension if ($info['extension'][0] == '.') { $info['extension'] = substr($info['extension'], 1); } // Allow static use current route info. static::$controller = $info['controller']; static::$method = $info['method']; static::$params = $info['params']; static::$defaults = $info['defaults']; static::$extension = $info['extension']; return static::$currentRoute = $info; }
/** * Старт маршрутизации */ public static function start() { if (static::$start) { return false; } else { static::$start = true; } static::$record = true; static::$host = Request::host(); static::$path = Request::path(); static::$query = Request::query(); static::$method = Request::method(); if (preg_match('#^/index.php#i', static::$path)) { Error::e404(); } if (preg_match('#^/public/#i', static::$path)) { Error::e404(); } static::running(); if (!static::$found) { if (mb_substr(static::$path, -1, 1) != '/') { $extension = pathinfo(static::$path, PATHINFO_EXTENSION); if ($extension == "") { static::$path .= "/"; static::running(); if (static::$found) { Redirect::r302(static::$path . (static::$query != '' ? '?' . static::$query : '')); } } } } static::$record = false; if (static::$found) { static::render(); } else { Error::e404(); } }
/** * 获取控制器 * @param null $_action * @param null $_method */ public function setAction($_action = null, $_method = null) { if ($_action && $_method) { static::$action = $_action; static::$method = $_method; } }