@@ -7,18 +7,18 @@ discard block |
||
| 7 | 7 | { |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * An associative array where the key is a namespace prefix and the value |
|
| 11 | - * is an array of base directories for classes in that namespace. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 10 | + * An associative array where the key is a namespace prefix and the value |
|
| 11 | + * is an array of base directories for classes in that namespace. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | 15 | protected $prefixes = []; |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Register loader with SPL autoloader stack. |
|
| 19 | - * |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 18 | + * Register loader with SPL autoloader stack. |
|
| 19 | + * |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | 22 | |
| 23 | 23 | public function register() |
| 24 | 24 | { |
@@ -26,28 +26,28 @@ discard block |
||
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | - * Adds a base directory for a namespace prefix. |
|
| 30 | - * |
|
| 31 | - * @param string $prefix The namespace prefix. |
|
| 32 | - * @param string $base_dir A base directory for class files in the |
|
| 33 | - * namespace. |
|
| 34 | - * @param bool $prepend If true, prepend the base directory to the stack |
|
| 35 | - * instead of appending it; this causes it to be searched first rather |
|
| 36 | - * than last. |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 29 | + * Adds a base directory for a namespace prefix. |
|
| 30 | + * |
|
| 31 | + * @param string $prefix The namespace prefix. |
|
| 32 | + * @param string $base_dir A base directory for class files in the |
|
| 33 | + * namespace. |
|
| 34 | + * @param bool $prepend If true, prepend the base directory to the stack |
|
| 35 | + * instead of appending it; this causes it to be searched first rather |
|
| 36 | + * than last. |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | 39 | public function addNamespaceTree($base_dir, $prepend = false) |
| 40 | 40 | { |
| 41 | - $dir_content = FileSystem::preg_scandir($base_dir, '/^[A-Z]{1}[A-Za-z]+(?!\.class.\.php)$/'); |
|
| 41 | + $dir_content = FileSystem::preg_scandir($base_dir, '/^[A-Z]{1}[A-Za-z]+(?!\.class.\.php)$/'); |
|
| 42 | 42 | |
| 43 | - foreach($dir_content as $res) |
|
| 44 | - { |
|
| 45 | - if(is_dir($fullpath = $base_dir.'/'.$res)) |
|
| 46 | - { |
|
| 47 | - $this->addNamespace('HexMakina\kadro\\'.$res, $fullpath); |
|
| 43 | + foreach($dir_content as $res) |
|
| 44 | + { |
|
| 45 | + if(is_dir($fullpath = $base_dir.'/'.$res)) |
|
| 46 | + { |
|
| 47 | + $this->addNamespace('HexMakina\kadro\\'.$res, $fullpath); |
|
| 48 | 48 | $this->addNamespaceTree($fullpath); |
| 49 | - } |
|
| 50 | - } |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | public function addNamespace($prefix, $base_dir, $prepend = false) |
@@ -73,12 +73,12 @@ discard block |
||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | - * Loads the class file for a given class name. |
|
| 77 | - * |
|
| 78 | - * @param string $class The fully-qualified class name. |
|
| 79 | - * @return mixed The mapped file name on success, or boolean false on |
|
| 80 | - * failure. |
|
| 81 | - */ |
|
| 76 | + * Loads the class file for a given class name. |
|
| 77 | + * |
|
| 78 | + * @param string $class The fully-qualified class name. |
|
| 79 | + * @return mixed The mapped file name on success, or boolean false on |
|
| 80 | + * failure. |
|
| 81 | + */ |
|
| 82 | 82 | public function loadClass($class) |
| 83 | 83 | { |
| 84 | 84 | // var_dump(__FUNCTION__."($class)"); |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | - * Load the mapped file for a namespace prefix and relative class. |
|
| 114 | - * |
|
| 115 | - * @param string $prefix The namespace prefix. |
|
| 116 | - * @param string $relative_class The relative class name. |
|
| 117 | - * @return mixed Boolean false if no mapped file can be loaded, or the |
|
| 118 | - * name of the mapped file that was loaded. |
|
| 119 | - */ |
|
| 113 | + * Load the mapped file for a namespace prefix and relative class. |
|
| 114 | + * |
|
| 115 | + * @param string $prefix The namespace prefix. |
|
| 116 | + * @param string $relative_class The relative class name. |
|
| 117 | + * @return mixed Boolean false if no mapped file can be loaded, or the |
|
| 118 | + * name of the mapped file that was loaded. |
|
| 119 | + */ |
|
| 120 | 120 | protected function loadMappedFile($prefix, $relative_class) |
| 121 | 121 | { |
| 122 | 122 | // are there any base directories for this namespace prefix? |
@@ -147,11 +147,11 @@ discard block |
||
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /** |
| 150 | - * If a file exists, require it from the file system. |
|
| 151 | - * |
|
| 152 | - * @param string $file The file to require. |
|
| 153 | - * @return bool True if the file exists, false if not. |
|
| 154 | - */ |
|
| 150 | + * If a file exists, require it from the file system. |
|
| 151 | + * |
|
| 152 | + * @param string $file The file to require. |
|
| 153 | + * @return bool True if the file exists, false if not. |
|
| 154 | + */ |
|
| 155 | 155 | protected function requireFile($file) |
| 156 | 156 | { |
| 157 | 157 | // var_dump(__FUNCTION__."($file)"); |
@@ -192,8 +192,8 @@ |
||
| 192 | 192 | |
| 193 | 193 | public function hop_url($url) |
| 194 | 194 | { |
| 195 | - header('Cache-Control: no-cache, must-revalidate'); |
|
| 196 | - header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
|
| 195 | + header('Cache-Control: no-cache, must-revalidate'); |
|
| 196 | + header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
|
| 197 | 197 | header('Location: '.$url); |
| 198 | 198 | exit(); |
| 199 | 199 | } |
@@ -2,266 +2,266 @@ |
||
| 2 | 2 | |
| 3 | 3 | class AltoRouter { |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * @var array Array of all routes (incl. named routes). |
|
| 7 | - */ |
|
| 8 | - protected $routes = []; |
|
| 9 | - |
|
| 10 | - /** |
|
| 11 | - * @var array Array of all named routes. |
|
| 12 | - */ |
|
| 13 | - protected $namedRoutes = []; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host) |
|
| 17 | - */ |
|
| 18 | - protected $basePath = ''; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var array Array of default match types (regex helpers) |
|
| 22 | - */ |
|
| 23 | - protected $matchTypes = array( |
|
| 24 | - 'i' => '[0-9]++', |
|
| 25 | - 'a' => '[0-9A-Za-z]++', |
|
| 26 | - 'h' => '[0-9A-Fa-f]++', |
|
| 27 | - '*' => '.+?', |
|
| 28 | - '**' => '.++', |
|
| 29 | - '' => '[^/\.]++' |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Create router in one call from config. |
|
| 34 | - * |
|
| 35 | - * @param array $routes |
|
| 36 | - * @param string $basePath |
|
| 37 | - * @param array $matchTypes |
|
| 38 | - */ |
|
| 39 | - public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) { |
|
| 40 | - $this->addRoutes($routes); |
|
| 41 | - $this->setBasePath($basePath); |
|
| 42 | - $this->addMatchTypes($matchTypes); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Retrieves all routes. |
|
| 47 | - * Useful if you want to process or display routes. |
|
| 48 | - * @return array All routes. |
|
| 49 | - */ |
|
| 50 | - public function getRoutes() { |
|
| 51 | - return $this->routes; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Add multiple routes at once from array in the following format: |
|
| 56 | - * |
|
| 57 | - * $routes = array( |
|
| 58 | - * array($method, $route, $target, $name) |
|
| 59 | - * ); |
|
| 60 | - * |
|
| 61 | - * @param array $routes |
|
| 62 | - * @return void |
|
| 63 | - * @author Koen Punt |
|
| 64 | - * @throws Exception |
|
| 65 | - */ |
|
| 66 | - public function addRoutes($routes){ |
|
| 67 | - if(!is_array($routes) && !$routes instanceof Traversable) { |
|
| 68 | - throw new \Exception('Routes should be an array or an instance of Traversable'); |
|
| 69 | - } |
|
| 70 | - foreach($routes as $route) { |
|
| 71 | - call_user_func_array(array($this, 'map'), $route); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Set the base path. |
|
| 77 | - * Useful if you are running your application from a subdirectory. |
|
| 78 | - */ |
|
| 79 | - public function setBasePath($basePath) { |
|
| 80 | - $this->basePath = $basePath; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Add named match types. It uses array_merge so keys can be overwritten. |
|
| 85 | - * |
|
| 86 | - * @param array $matchTypes The key is the name and the value is the regex. |
|
| 87 | - */ |
|
| 88 | - public function addMatchTypes($matchTypes) { |
|
| 89 | - $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Map a route to a target |
|
| 94 | - * |
|
| 95 | - * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
| 96 | - * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
| 97 | - * @param mixed $target The target where this route should point to. Can be anything. |
|
| 98 | - * @param string $name Optional name of this route. Supply if you want to reverse route this url in your application. |
|
| 99 | - * @throws Exception |
|
| 100 | - */ |
|
| 101 | - public function map($method, $route, $target, $name = null) { |
|
| 102 | - |
|
| 103 | - $this->routes[] = array($method, $route, $target, $name); |
|
| 104 | - |
|
| 105 | - if($name) { |
|
| 106 | - if(isset($this->namedRoutes[$name])) { |
|
| 107 | - throw new \Exception("Can not redeclare route '{$name}'"); |
|
| 108 | - } else { |
|
| 109 | - $this->namedRoutes[$name] = $route; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Reversed routing |
|
| 119 | - * |
|
| 120 | - * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 121 | - * |
|
| 122 | - * @param string $routeName The name of the route. |
|
| 123 | - * @param array @params Associative array of parameters to replace placeholders with. |
|
| 124 | - * @return string The URL of the route with named parameters in place. |
|
| 125 | - * @throws Exception |
|
| 126 | - */ |
|
| 127 | - public function generate($routeName, array $params = array()) { |
|
| 128 | - |
|
| 129 | - // Check if named route exists |
|
| 130 | - if(!isset($this->namedRoutes[$routeName])) { |
|
| 131 | - throw new \Exception("Route '{$routeName}' does not exist."); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Replace named parameters |
|
| 135 | - $route = $this->namedRoutes[$routeName]; |
|
| 136 | - |
|
| 137 | - // prepend base path to route url again |
|
| 138 | - $url = $this->basePath . $route; |
|
| 139 | - |
|
| 140 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 141 | - |
|
| 142 | - foreach($matches as $match) { |
|
| 143 | - list($block, $pre, $type, $param, $optional) = $match; |
|
| 144 | - |
|
| 145 | - if ($pre) { |
|
| 146 | - $block = substr($block, 1); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if(isset($params[$param])) { |
|
| 150 | - $url = str_replace($block, $params[$param], $url); |
|
| 151 | - } elseif ($optional) { |
|
| 152 | - $url = str_replace($pre . $block, '', $url); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $url; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Match a given Request Url against stored routes |
|
| 164 | - * @param string $requestUrl |
|
| 165 | - * @param string $requestMethod |
|
| 166 | - * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 167 | - */ |
|
| 168 | - public function match($requestUrl = null, $requestMethod = null) { |
|
| 169 | - |
|
| 170 | - $params = []; |
|
| 171 | - $match = false; |
|
| 172 | - |
|
| 173 | - // set Request Url if it isn't passed as parameter |
|
| 174 | - if($requestUrl === null) { |
|
| 175 | - $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // strip base path from request url |
|
| 179 | - $requestUrl = substr($requestUrl, strlen($this->basePath)); |
|
| 5 | + /** |
|
| 6 | + * @var array Array of all routes (incl. named routes). |
|
| 7 | + */ |
|
| 8 | + protected $routes = []; |
|
| 9 | + |
|
| 10 | + /** |
|
| 11 | + * @var array Array of all named routes. |
|
| 12 | + */ |
|
| 13 | + protected $namedRoutes = []; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host) |
|
| 17 | + */ |
|
| 18 | + protected $basePath = ''; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var array Array of default match types (regex helpers) |
|
| 22 | + */ |
|
| 23 | + protected $matchTypes = array( |
|
| 24 | + 'i' => '[0-9]++', |
|
| 25 | + 'a' => '[0-9A-Za-z]++', |
|
| 26 | + 'h' => '[0-9A-Fa-f]++', |
|
| 27 | + '*' => '.+?', |
|
| 28 | + '**' => '.++', |
|
| 29 | + '' => '[^/\.]++' |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Create router in one call from config. |
|
| 34 | + * |
|
| 35 | + * @param array $routes |
|
| 36 | + * @param string $basePath |
|
| 37 | + * @param array $matchTypes |
|
| 38 | + */ |
|
| 39 | + public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) { |
|
| 40 | + $this->addRoutes($routes); |
|
| 41 | + $this->setBasePath($basePath); |
|
| 42 | + $this->addMatchTypes($matchTypes); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Retrieves all routes. |
|
| 47 | + * Useful if you want to process or display routes. |
|
| 48 | + * @return array All routes. |
|
| 49 | + */ |
|
| 50 | + public function getRoutes() { |
|
| 51 | + return $this->routes; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Add multiple routes at once from array in the following format: |
|
| 56 | + * |
|
| 57 | + * $routes = array( |
|
| 58 | + * array($method, $route, $target, $name) |
|
| 59 | + * ); |
|
| 60 | + * |
|
| 61 | + * @param array $routes |
|
| 62 | + * @return void |
|
| 63 | + * @author Koen Punt |
|
| 64 | + * @throws Exception |
|
| 65 | + */ |
|
| 66 | + public function addRoutes($routes){ |
|
| 67 | + if(!is_array($routes) && !$routes instanceof Traversable) { |
|
| 68 | + throw new \Exception('Routes should be an array or an instance of Traversable'); |
|
| 69 | + } |
|
| 70 | + foreach($routes as $route) { |
|
| 71 | + call_user_func_array(array($this, 'map'), $route); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Set the base path. |
|
| 77 | + * Useful if you are running your application from a subdirectory. |
|
| 78 | + */ |
|
| 79 | + public function setBasePath($basePath) { |
|
| 80 | + $this->basePath = $basePath; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Add named match types. It uses array_merge so keys can be overwritten. |
|
| 85 | + * |
|
| 86 | + * @param array $matchTypes The key is the name and the value is the regex. |
|
| 87 | + */ |
|
| 88 | + public function addMatchTypes($matchTypes) { |
|
| 89 | + $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Map a route to a target |
|
| 94 | + * |
|
| 95 | + * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
| 96 | + * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
| 97 | + * @param mixed $target The target where this route should point to. Can be anything. |
|
| 98 | + * @param string $name Optional name of this route. Supply if you want to reverse route this url in your application. |
|
| 99 | + * @throws Exception |
|
| 100 | + */ |
|
| 101 | + public function map($method, $route, $target, $name = null) { |
|
| 102 | + |
|
| 103 | + $this->routes[] = array($method, $route, $target, $name); |
|
| 104 | + |
|
| 105 | + if($name) { |
|
| 106 | + if(isset($this->namedRoutes[$name])) { |
|
| 107 | + throw new \Exception("Can not redeclare route '{$name}'"); |
|
| 108 | + } else { |
|
| 109 | + $this->namedRoutes[$name] = $route; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Reversed routing |
|
| 119 | + * |
|
| 120 | + * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 121 | + * |
|
| 122 | + * @param string $routeName The name of the route. |
|
| 123 | + * @param array @params Associative array of parameters to replace placeholders with. |
|
| 124 | + * @return string The URL of the route with named parameters in place. |
|
| 125 | + * @throws Exception |
|
| 126 | + */ |
|
| 127 | + public function generate($routeName, array $params = array()) { |
|
| 128 | + |
|
| 129 | + // Check if named route exists |
|
| 130 | + if(!isset($this->namedRoutes[$routeName])) { |
|
| 131 | + throw new \Exception("Route '{$routeName}' does not exist."); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Replace named parameters |
|
| 135 | + $route = $this->namedRoutes[$routeName]; |
|
| 136 | + |
|
| 137 | + // prepend base path to route url again |
|
| 138 | + $url = $this->basePath . $route; |
|
| 139 | + |
|
| 140 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 141 | + |
|
| 142 | + foreach($matches as $match) { |
|
| 143 | + list($block, $pre, $type, $param, $optional) = $match; |
|
| 144 | + |
|
| 145 | + if ($pre) { |
|
| 146 | + $block = substr($block, 1); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if(isset($params[$param])) { |
|
| 150 | + $url = str_replace($block, $params[$param], $url); |
|
| 151 | + } elseif ($optional) { |
|
| 152 | + $url = str_replace($pre . $block, '', $url); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $url; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Match a given Request Url against stored routes |
|
| 164 | + * @param string $requestUrl |
|
| 165 | + * @param string $requestMethod |
|
| 166 | + * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 167 | + */ |
|
| 168 | + public function match($requestUrl = null, $requestMethod = null) { |
|
| 169 | + |
|
| 170 | + $params = []; |
|
| 171 | + $match = false; |
|
| 172 | + |
|
| 173 | + // set Request Url if it isn't passed as parameter |
|
| 174 | + if($requestUrl === null) { |
|
| 175 | + $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // strip base path from request url |
|
| 179 | + $requestUrl = substr($requestUrl, strlen($this->basePath)); |
|
| 180 | 180 | |
| 181 | - // Strip query string (?a=b) from Request Url |
|
| 182 | - if (($strpos = strpos($requestUrl, '?')) !== false) { |
|
| 183 | - $requestUrl = substr($requestUrl, 0, $strpos); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - // set Request Method if it isn't passed as a parameter |
|
| 187 | - if($requestMethod === null) { |
|
| 188 | - $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - foreach($this->routes as $handler) { |
|
| 192 | - list($methods, $route, $target, $name) = $handler; |
|
| 193 | - $method_match = (stripos($methods, $requestMethod) !== false); |
|
| 194 | - |
|
| 195 | - // Method did not match, continue to next route. |
|
| 196 | - if (!$method_match) continue; |
|
| 197 | - |
|
| 198 | - if ($route === '*') { |
|
| 199 | - // * wildcard (matches all) |
|
| 200 | - $match = true; |
|
| 201 | - } elseif (isset($route[0]) && $route[0] === '@') { |
|
| 202 | - // @ regex delimiter |
|
| 203 | - $pattern = '`' . substr($route, 1) . '`u'; |
|
| 204 | - $match = preg_match($pattern, $requestUrl, $params) === 1; |
|
| 205 | - } elseif (($position = strpos($route, '[')) === false) { |
|
| 206 | - // No params in url, do string comparison |
|
| 207 | - $match = strcmp($requestUrl, $route) === 0; |
|
| 208 | - } else { |
|
| 209 | - // Compare longest non-param string with url |
|
| 210 | - if (strncmp($requestUrl, $route, $position) !== 0) { |
|
| 211 | - continue; |
|
| 212 | - } |
|
| 213 | - $regex = $this->compileRoute($route); |
|
| 214 | - $match = preg_match($regex, $requestUrl, $params) === 1; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ($match) { |
|
| 218 | - |
|
| 219 | - if ($params) { |
|
| 220 | - foreach($params as $key => $value) { |
|
| 221 | - if(is_numeric($key)) unset($params[$key]); |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - return array( |
|
| 226 | - 'target' => $target, |
|
| 227 | - 'params' => $params, |
|
| 228 | - 'name' => $name |
|
| 229 | - ); |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - return false; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Compile the regex for a given route (EXPENSIVE) |
|
| 237 | - */ |
|
| 238 | - private function compileRoute($route) { |
|
| 239 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 240 | - |
|
| 241 | - $matchTypes = $this->matchTypes; |
|
| 242 | - foreach($matches as $match) { |
|
| 243 | - list($block, $pre, $type, $param, $optional) = $match; |
|
| 244 | - |
|
| 245 | - if (isset($matchTypes[$type])) { |
|
| 246 | - $type = $matchTypes[$type]; |
|
| 247 | - } |
|
| 248 | - if ($pre === '.') { |
|
| 249 | - $pre = '\.'; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 253 | - $pattern = '(?:' |
|
| 254 | - . ($pre !== '' ? $pre : null) |
|
| 255 | - . '(' |
|
| 256 | - . ($param !== '' ? "?P<$param>" : null) |
|
| 257 | - . $type |
|
| 258 | - . '))' |
|
| 259 | - . ($optional !== '' ? '?' : null); |
|
| 260 | - |
|
| 261 | - $route = str_replace($block, $pattern, $route); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - } |
|
| 265 | - return "`^$route$`u"; |
|
| 266 | - } |
|
| 181 | + // Strip query string (?a=b) from Request Url |
|
| 182 | + if (($strpos = strpos($requestUrl, '?')) !== false) { |
|
| 183 | + $requestUrl = substr($requestUrl, 0, $strpos); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + // set Request Method if it isn't passed as a parameter |
|
| 187 | + if($requestMethod === null) { |
|
| 188 | + $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + foreach($this->routes as $handler) { |
|
| 192 | + list($methods, $route, $target, $name) = $handler; |
|
| 193 | + $method_match = (stripos($methods, $requestMethod) !== false); |
|
| 194 | + |
|
| 195 | + // Method did not match, continue to next route. |
|
| 196 | + if (!$method_match) continue; |
|
| 197 | + |
|
| 198 | + if ($route === '*') { |
|
| 199 | + // * wildcard (matches all) |
|
| 200 | + $match = true; |
|
| 201 | + } elseif (isset($route[0]) && $route[0] === '@') { |
|
| 202 | + // @ regex delimiter |
|
| 203 | + $pattern = '`' . substr($route, 1) . '`u'; |
|
| 204 | + $match = preg_match($pattern, $requestUrl, $params) === 1; |
|
| 205 | + } elseif (($position = strpos($route, '[')) === false) { |
|
| 206 | + // No params in url, do string comparison |
|
| 207 | + $match = strcmp($requestUrl, $route) === 0; |
|
| 208 | + } else { |
|
| 209 | + // Compare longest non-param string with url |
|
| 210 | + if (strncmp($requestUrl, $route, $position) !== 0) { |
|
| 211 | + continue; |
|
| 212 | + } |
|
| 213 | + $regex = $this->compileRoute($route); |
|
| 214 | + $match = preg_match($regex, $requestUrl, $params) === 1; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ($match) { |
|
| 218 | + |
|
| 219 | + if ($params) { |
|
| 220 | + foreach($params as $key => $value) { |
|
| 221 | + if(is_numeric($key)) unset($params[$key]); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + return array( |
|
| 226 | + 'target' => $target, |
|
| 227 | + 'params' => $params, |
|
| 228 | + 'name' => $name |
|
| 229 | + ); |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + return false; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Compile the regex for a given route (EXPENSIVE) |
|
| 237 | + */ |
|
| 238 | + private function compileRoute($route) { |
|
| 239 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 240 | + |
|
| 241 | + $matchTypes = $this->matchTypes; |
|
| 242 | + foreach($matches as $match) { |
|
| 243 | + list($block, $pre, $type, $param, $optional) = $match; |
|
| 244 | + |
|
| 245 | + if (isset($matchTypes[$type])) { |
|
| 246 | + $type = $matchTypes[$type]; |
|
| 247 | + } |
|
| 248 | + if ($pre === '.') { |
|
| 249 | + $pre = '\.'; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 253 | + $pattern = '(?:' |
|
| 254 | + . ($pre !== '' ? $pre : null) |
|
| 255 | + . '(' |
|
| 256 | + . ($param !== '' ? "?P<$param>" : null) |
|
| 257 | + . $type |
|
| 258 | + . '))' |
|
| 259 | + . ($optional !== '' ? '?' : null); |
|
| 260 | + |
|
| 261 | + $route = str_replace($block, $pattern, $route); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + } |
|
| 265 | + return "`^$route$`u"; |
|
| 266 | + } |
|
| 267 | 267 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | interface DisplayController extends BaseController |
| 6 | 6 | { |
| 7 | 7 | |
| 8 | - /* |
|
| 8 | + /* |
|
| 9 | 9 | The viewport is an associative array of values to be exported as variables in the view |
| 10 | 10 | the assoc keys will be the variable names |
| 11 | 11 | |
@@ -42,9 +42,9 @@ discard block |
||
| 42 | 42 | returns null |
| 43 | 43 | */ |
| 44 | 44 | |
| 45 | - public function viewport($key=null, $value=null, $coercion=false); |
|
| 45 | + public function viewport($key=null, $value=null, $coercion=false); |
|
| 46 | 46 | |
| 47 | - public function display($custom_template = null, $standalone=false); |
|
| 47 | + public function display($custom_template = null, $standalone=false); |
|
| 48 | 48 | |
| 49 | 49 | } |
| 50 | 50 | |
@@ -7,8 +7,8 @@ discard block |
||
| 7 | 7 | { |
| 8 | 8 | protected static function prepare_pagination($records_total) |
| 9 | 9 | { |
| 10 | - global $settings; |
|
| 11 | - global $smarty; |
|
| 10 | + global $settings; |
|
| 11 | + global $smarty; |
|
| 12 | 12 | |
| 13 | 13 | $pages_max_on_each_side = 3; |
| 14 | 14 | $pages_max_displayed = $pages_max_on_each_side*2+1; |
@@ -35,12 +35,12 @@ discard block |
||
| 35 | 35 | if($pages_last > $pages_total) // $pages_max_displayed greater than the total of pages |
| 36 | 36 | $pages_last = $pages_total; |
| 37 | 37 | |
| 38 | - $this->viewport("pages_total", $pages_total); |
|
| 39 | - $this->viewport("pages_first", $pages_first); |
|
| 38 | + $this->viewport("pages_total", $pages_total); |
|
| 39 | + $this->viewport("pages_first", $pages_first); |
|
| 40 | 40 | $this->viewport("pages_previous", $pages_current <= 1 ? $pages_total : $pages_current - 1); |
| 41 | - $this->viewport("pages_current", $pages_current); |
|
| 42 | - $this->viewport("pages_next", $pages_current >= $pages_total ? 1 : $pages_current + 1); |
|
| 43 | - $this->viewport("pages_last", $pages_last); |
|
| 41 | + $this->viewport("pages_current", $pages_current); |
|
| 42 | + $this->viewport("pages_next", $pages_current >= $pages_total ? 1 : $pages_current + 1); |
|
| 43 | + $this->viewport("pages_last", $pages_last); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | } |
@@ -107,18 +107,18 @@ discard block |
||
| 107 | 107 | $custom_template = null; |
| 108 | 108 | |
| 109 | 109 | if(method_exists($this, 'prepare')) |
| 110 | - $this->prepare(); |
|
| 110 | + $this->prepare(); |
|
| 111 | 111 | |
| 112 | - if(method_exists($this, $method = $this->router()->target_method())) |
|
| 112 | + if(method_exists($this, $method = $this->router()->target_method())) |
|
| 113 | 113 | { |
| 114 | 114 | $custom_template = $this->$method(); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | if(method_exists($this, 'conclude')) |
| 118 | - $this->conclude(); |
|
| 118 | + $this->conclude(); |
|
| 119 | 119 | |
| 120 | 120 | if(method_exists($this, 'display')) |
| 121 | - $template = $this->display($custom_template); |
|
| 121 | + $template = $this->display($custom_template); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | public function conclude(){} |
@@ -143,16 +143,16 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | $template = $this->find_template($smarty, $custom_template); // throws Exception if nothing found |
| 145 | 145 | |
| 146 | - $this->viewport('controller', $this); |
|
| 146 | + $this->viewport('controller', $this); |
|
| 147 | 147 | |
| 148 | 148 | $this->viewport('user_messages', $this->logger()->get_user_report()); |
| 149 | 149 | |
| 150 | 150 | |
| 151 | - $this->viewport('file_root', $this->router()->file_root()); |
|
| 152 | - $this->viewport('view_path', $this->router()->file_root() . $this->box('settings.smarty.template_path').'app/'); |
|
| 153 | - $this->viewport('web_root', $this->router()->web_root()); |
|
| 154 | - $this->viewport('view_url', $this->router()->web_root() . $this->box('settings.smarty.template_path')); |
|
| 155 | - $this->viewport('images_url', $this->router()->web_root() . $this->box('settings.smarty.template_path') . 'images/'); |
|
| 151 | + $this->viewport('file_root', $this->router()->file_root()); |
|
| 152 | + $this->viewport('view_path', $this->router()->file_root() . $this->box('settings.smarty.template_path').'app/'); |
|
| 153 | + $this->viewport('web_root', $this->router()->web_root()); |
|
| 154 | + $this->viewport('view_url', $this->router()->web_root() . $this->box('settings.smarty.template_path')); |
|
| 155 | + $this->viewport('images_url', $this->router()->web_root() . $this->box('settings.smarty.template_path') . 'images/'); |
|
| 156 | 156 | |
| 157 | 157 | foreach($this->viewport() as $template_var_name => $value) |
| 158 | 158 | $smarty->assign($template_var_name, $value); |
@@ -227,12 +227,12 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | */ |
| 229 | 229 | public function route_back($route_name=null, $route_params=[]) : string |
| 230 | - { |
|
| 230 | + { |
|
| 231 | 231 | if(is_null($route_name)) |
| 232 | - return $this->route_back ?? $this->router()->prehop(RouterInterface::ROUTE_HOME_NAME); |
|
| 232 | + return $this->route_back ?? $this->router()->prehop(RouterInterface::ROUTE_HOME_NAME); |
|
| 233 | 233 | |
| 234 | 234 | return $this->route_back = $this->route_factory($route_name, $route_params); |
| 235 | - } |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | 237 | public function route_factory($route_name=null, $route_params=[]) : string |
| 238 | 238 | { |
@@ -16,8 +16,8 @@ discard block |
||
| 16 | 16 | $picture_directory = $this->build_path_to_directory(); |
| 17 | 17 | $thumbnail_directory = $picture_directory; |
| 18 | 18 | |
| 19 | - if(!file_exists($picture_directory) && mkdir($picture_directory) === false) |
|
| 20 | - return []; |
|
| 19 | + if(!file_exists($picture_directory) && mkdir($picture_directory) === false) |
|
| 20 | + return []; |
|
| 21 | 21 | |
| 22 | 22 | $filenames = self::preg_scandir($picture_directory, '/^[0-9]+_[0-9]+\.[a-zA-Z]+/');// ID_SEQUENCENUMBER.ext |
| 23 | 23 | |
@@ -75,11 +75,11 @@ discard block |
||
| 75 | 75 | $filepath = $this->build_filename() . '.' . self::file_ext($_FILES[$this->get_type()]['name']); |
| 76 | 76 | $filepath = $this->locate_file($filepath); |
| 77 | 77 | |
| 78 | - if(file_exists($filepath)) |
|
| 78 | + if(file_exists($filepath)) |
|
| 79 | 79 | throw new \Exception($this->get_type()." new path '$filepath' already exists"); |
| 80 | 80 | |
| 81 | - if(copy($_FILES[$this->get_type()]['tmp_name'], $filepath) === false) |
|
| 82 | - throw new \Exception(" cant copy ".$_FILES[$this->get_type()]['name']." to ($filepath)"); |
|
| 81 | + if(copy($_FILES[$this->get_type()]['tmp_name'], $filepath) === false) |
|
| 82 | + throw new \Exception(" cant copy ".$_FILES[$this->get_type()]['name']." to ($filepath)"); |
|
| 83 | 83 | |
| 84 | 84 | $this->make_thumbnail($filepath); |
| 85 | 85 | } |
@@ -95,64 +95,64 @@ discard block |
||
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | public function make_thumbnail($filepath) |
| 98 | - { |
|
| 98 | + { |
|
| 99 | 99 | global $settings; |
| 100 | 100 | |
| 101 | - $cover_iri = null; |
|
| 102 | - |
|
| 103 | - $mime_type = mime_content_type($filepath); |
|
| 104 | - switch($mime_type) |
|
| 105 | - { |
|
| 106 | - case 'image/jpeg': |
|
| 107 | - case 'image/pjpeg': |
|
| 108 | - $cover_iri = imagecreatefromjpeg($filepath); |
|
| 109 | - break; |
|
| 101 | + $cover_iri = null; |
|
| 110 | 102 | |
| 111 | - case 'image/png': |
|
| 112 | - $cover_iri = imagecreatefrompng($filepath); |
|
| 113 | - break; |
|
| 114 | - |
|
| 115 | - case 'image/gif': |
|
| 116 | - $cover_iri = imagecreatefromgif($filepath); |
|
| 117 | - break; |
|
| 118 | - } |
|
| 103 | + $mime_type = mime_content_type($filepath); |
|
| 104 | + switch($mime_type) |
|
| 105 | + { |
|
| 106 | + case 'image/jpeg': |
|
| 107 | + case 'image/pjpeg': |
|
| 108 | + $cover_iri = imagecreatefromjpeg($filepath); |
|
| 109 | + break; |
|
| 110 | + |
|
| 111 | + case 'image/png': |
|
| 112 | + $cover_iri = imagecreatefrompng($filepath); |
|
| 113 | + break; |
|
| 114 | + |
|
| 115 | + case 'image/gif': |
|
| 116 | + $cover_iri = imagecreatefromgif($filepath); |
|
| 117 | + break; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - if(!is_null($cover_iri)) |
|
| 121 | - { |
|
| 122 | - $width = imagesx( $cover_iri ); |
|
| 123 | - $height = imagesy( $cover_iri ); |
|
| 120 | + if(!is_null($cover_iri)) |
|
| 121 | + { |
|
| 122 | + $width = imagesx( $cover_iri ); |
|
| 123 | + $height = imagesy( $cover_iri ); |
|
| 124 | 124 | |
| 125 | - // calculate thumbnail size |
|
| 125 | + // calculate thumbnail size |
|
| 126 | 126 | |
| 127 | - $new_width = $settings[get_class($this->pmi)::model_type()][$this->get_type()]['thumbnail']['width']; |
|
| 128 | - $new_height = floor( $height * ( $new_width / $width ) ); |
|
| 127 | + $new_width = $settings[get_class($this->pmi)::model_type()][$this->get_type()]['thumbnail']['width']; |
|
| 128 | + $new_height = floor( $height * ( $new_width / $width ) ); |
|
| 129 | 129 | |
| 130 | - // create a new temporary image |
|
| 131 | - $thumb_iri = imagecreatetruecolor($new_width, $new_height); |
|
| 130 | + // create a new temporary image |
|
| 131 | + $thumb_iri = imagecreatetruecolor($new_width, $new_height); |
|
| 132 | 132 | |
| 133 | - // copy and resize old image into new image |
|
| 134 | - imagecopyresized( $thumb_iri, $cover_iri, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); |
|
| 133 | + // copy and resize old image into new image |
|
| 134 | + imagecopyresized( $thumb_iri, $cover_iri, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); |
|
| 135 | 135 | |
| 136 | - // save thumbnail into a file |
|
| 136 | + // save thumbnail into a file |
|
| 137 | 137 | imagejpeg($thumb_iri, $this->locate_thumbnail(pathinfo($filepath, PATHINFO_BASENAME))); |
| 138 | - } |
|
| 139 | - } |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | public function remove_all() |
| 142 | 142 | { |
| 143 | 143 | $filenames = $this->filenames(); |
| 144 | 144 | |
| 145 | 145 | foreach($filenames as $filename) |
| 146 | - $this->remove($filename); |
|
| 146 | + $this->remove($filename); |
|
| 147 | 147 | |
| 148 | 148 | $directory = $this->build_path_to_directory(); |
| 149 | - if(file_exists($directory) === true) |
|
| 149 | + if(file_exists($directory) === true) |
|
| 150 | 150 | { |
| 151 | 151 | if(is_dir($directory) === false) |
| 152 | 152 | throw new \Exception($this->get_type()."' directory '$directory' is not a directory"); |
| 153 | 153 | |
| 154 | - if(rmdir($directory) === false) |
|
| 155 | - throw new \Exception("rmdir($directory) failed like a bitch"); |
|
| 154 | + if(rmdir($directory) === false) |
|
| 155 | + throw new \Exception("rmdir($directory) failed like a bitch"); |
|
| 156 | 156 | } |
| 157 | 157 | else trigger_error($this->get_type()." $directory doesn't exist", E_USER_WARNING); |
| 158 | 158 | } |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | foreach($pathes as $what => $path) |
| 170 | 170 | { |
| 171 | 171 | $error = null; |
| 172 | - if(!file_exists($path)) |
|
| 172 | + if(!file_exists($path)) |
|
| 173 | 173 | $error = 'file does not exist'; |
| 174 | 174 | elseif(unlink($path)===false) |
| 175 | 175 | $error = 'unlink() failed'; |
@@ -223,16 +223,16 @@ discard block |
||
| 223 | 223 | global $settings; |
| 224 | 224 | $pi_manager = new PictureManager($item, $picture_type); |
| 225 | 225 | |
| 226 | - $pictures = $pi_manager->filenames(); |
|
| 226 | + $pictures = $pi_manager->filenames(); |
|
| 227 | 227 | |
| 228 | 228 | $item_model_type = get_class($item)::model_type(); |
| 229 | 229 | if(count($pictures)===0) |
| 230 | 230 | return $settings[$item_model_type][$picture_type]['generic_picture']; |
| 231 | 231 | |
| 232 | - if($settings[$item_model_type][$picture_type]['cycle_on_load']) |
|
| 233 | - $filename = $pictures[array_rand($pictures, 1)]; |
|
| 234 | - else |
|
| 235 | - $filename = array_shift($pictures); |
|
| 232 | + if($settings[$item_model_type][$picture_type]['cycle_on_load']) |
|
| 233 | + $filename = $pictures[array_rand($pictures, 1)]; |
|
| 234 | + else |
|
| 235 | + $filename = array_shift($pictures); |
|
| 236 | 236 | |
| 237 | 237 | return $thumbnail===true ? $pi_manager->locate_thumbnail($filename) : $pi_manager->locate_file($filename); |
| 238 | 238 | } |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | return $this->iso_name().' ('.$this->Part3.')'; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - public function traceable() : bool |
|
| 35 | - { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 34 | + public function traceable() : bool |
|
| 35 | + { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | public function iso_name() |
| 40 | 40 | { |
@@ -102,9 +102,9 @@ discard block |
||
| 102 | 102 | public static function search_language($term, $authority=null) |
| 103 | 103 | { |
| 104 | 104 | $rows = self::query_retrieve(['term' => $term, 'requires_authority' => $authority])->ret_ass(); |
| 105 | - $ret = []; |
|
| 106 | - foreach($rows as $row) |
|
| 107 | - $ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
| 105 | + $ret = []; |
|
| 106 | + foreach($rows as $row) |
|
| 107 | + $ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
| 108 | 108 | |
| 109 | 109 | return $ret; |
| 110 | 110 | } |
@@ -10,10 +10,10 @@ |
||
| 10 | 10 | const TABLE_NAME = 'kadro_traduki'; |
| 11 | 11 | const TABLE_ALIAS = 'traduko'; |
| 12 | 12 | |
| 13 | - public function traceable() : bool |
|
| 14 | - { |
|
| 15 | - return false; |
|
| 16 | - } |
|
| 13 | + public function traceable() : bool |
|
| 14 | + { |
|
| 15 | + return false; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | 18 | public function immortal() : bool |
| 19 | 19 | { |