@@ -100,4 +100,4 @@ |
||
100 | 100 | |
101 | 101 | } |
102 | 102 | |
103 | - ?> |
|
103 | + ?> |
@@ -15,4 +15,4 @@ |
||
15 | 15 | } |
16 | 16 | } |
17 | 17 | |
18 | - ?> |
|
18 | + ?> |
@@ -29,8 +29,8 @@ discard block |
||
29 | 29 | { |
30 | 30 | $picture_directory = $this->build_path_to_directory(); |
31 | 31 | $thumbnail_directory = $picture_directory; |
32 | - if(!file_exists($picture_directory) && mkdir($picture_directory) === false) |
|
33 | - throw new PictureManagerException("directory '$picture_directory' does not exist"); |
|
32 | + if(!file_exists($picture_directory) && mkdir($picture_directory) === false) |
|
33 | + throw new PictureManagerException("directory '$picture_directory' does not exist"); |
|
34 | 34 | |
35 | 35 | $filenames = self::preg_scandir($picture_directory, self::FILENAME_REGEX); |
36 | 36 | |
@@ -87,11 +87,11 @@ discard block |
||
87 | 87 | $filepath = $this->build_filename() . '.' . self::file_ext($_FILES[$this->get_type()]['name']); |
88 | 88 | $filepath = $this->locate_file($filepath); |
89 | 89 | |
90 | - if(file_exists($filepath)) |
|
90 | + if(file_exists($filepath)) |
|
91 | 91 | throw new PictureManagerException($this->get_type()." new path '$filepath' already exists"); |
92 | 92 | |
93 | - if(copy($_FILES[$this->get_type()]['tmp_name'], $filepath) === false) |
|
94 | - throw new PictureManagerException(" cant copy ".$_FILES[$this->get_type()]['name']." to ($filepath)"); |
|
93 | + if(copy($_FILES[$this->get_type()]['tmp_name'], $filepath) === false) |
|
94 | + throw new PictureManagerException(" cant copy ".$_FILES[$this->get_type()]['name']." to ($filepath)"); |
|
95 | 95 | |
96 | 96 | $this->make_thumbnail($filepath); |
97 | 97 | } |
@@ -107,64 +107,64 @@ discard block |
||
107 | 107 | } |
108 | 108 | |
109 | 109 | public function make_thumbnail($filepath) |
110 | - { |
|
110 | + { |
|
111 | 111 | global $settings; |
112 | 112 | |
113 | - $cover_iri = null; |
|
114 | - |
|
115 | - $mime_type = mime_content_type($filepath); |
|
116 | - switch($mime_type) |
|
117 | - { |
|
118 | - case 'image/jpeg': |
|
119 | - case 'image/pjpeg': |
|
120 | - $cover_iri = imagecreatefromjpeg($filepath); |
|
121 | - break; |
|
113 | + $cover_iri = null; |
|
122 | 114 | |
123 | - case 'image/png': |
|
124 | - $cover_iri = imagecreatefrompng($filepath); |
|
125 | - break; |
|
126 | - |
|
127 | - case 'image/gif': |
|
128 | - $cover_iri = imagecreatefromgif($filepath); |
|
129 | - break; |
|
130 | - } |
|
115 | + $mime_type = mime_content_type($filepath); |
|
116 | + switch($mime_type) |
|
117 | + { |
|
118 | + case 'image/jpeg': |
|
119 | + case 'image/pjpeg': |
|
120 | + $cover_iri = imagecreatefromjpeg($filepath); |
|
121 | + break; |
|
122 | + |
|
123 | + case 'image/png': |
|
124 | + $cover_iri = imagecreatefrompng($filepath); |
|
125 | + break; |
|
126 | + |
|
127 | + case 'image/gif': |
|
128 | + $cover_iri = imagecreatefromgif($filepath); |
|
129 | + break; |
|
130 | + } |
|
131 | 131 | |
132 | - if(!is_null($cover_iri)) |
|
133 | - { |
|
134 | - $width = imagesx( $cover_iri ); |
|
135 | - $height = imagesy( $cover_iri ); |
|
132 | + if(!is_null($cover_iri)) |
|
133 | + { |
|
134 | + $width = imagesx( $cover_iri ); |
|
135 | + $height = imagesy( $cover_iri ); |
|
136 | 136 | |
137 | - // calculate thumbnail size |
|
137 | + // calculate thumbnail size |
|
138 | 138 | |
139 | - $new_width = $settings[get_class($this->pmi)::model_type()][$this->get_type()]['thumbnail']['width']; |
|
140 | - $new_height = floor( $height * ( $new_width / $width ) ); |
|
139 | + $new_width = $settings[get_class($this->pmi)::model_type()][$this->get_type()]['thumbnail']['width']; |
|
140 | + $new_height = floor( $height * ( $new_width / $width ) ); |
|
141 | 141 | |
142 | - // create a new temporary image |
|
143 | - $thumb_iri = imagecreatetruecolor($new_width, $new_height); |
|
142 | + // create a new temporary image |
|
143 | + $thumb_iri = imagecreatetruecolor($new_width, $new_height); |
|
144 | 144 | |
145 | - // copy and resize old image into new image |
|
146 | - imagecopyresized( $thumb_iri, $cover_iri, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); |
|
145 | + // copy and resize old image into new image |
|
146 | + imagecopyresized( $thumb_iri, $cover_iri, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); |
|
147 | 147 | |
148 | - // save thumbnail into a file |
|
148 | + // save thumbnail into a file |
|
149 | 149 | imagejpeg($thumb_iri, $this->locate_thumbnail(pathinfo($filepath, PATHINFO_BASENAME))); |
150 | - } |
|
151 | - } |
|
150 | + } |
|
151 | + } |
|
152 | 152 | |
153 | 153 | public function remove_all() |
154 | 154 | { |
155 | 155 | $filenames = $this->filenames(); |
156 | 156 | |
157 | 157 | foreach($filenames as $filename) |
158 | - $this->remove($filename); |
|
158 | + $this->remove($filename); |
|
159 | 159 | |
160 | 160 | $directory = $this->build_path_to_directory(); |
161 | - if(file_exists($directory) === true) |
|
161 | + if(file_exists($directory) === true) |
|
162 | 162 | { |
163 | 163 | if(is_dir($directory) === false) |
164 | 164 | throw new PictureManagerException($this->get_type()."' directory '$directory' is not a directory"); |
165 | 165 | |
166 | - if(rmdir($directory) === false) |
|
167 | - throw new PictureManagerException("rmdir($directory) failed like a bitch"); |
|
166 | + if(rmdir($directory) === false) |
|
167 | + throw new PictureManagerException("rmdir($directory) failed like a bitch"); |
|
168 | 168 | } |
169 | 169 | else trigger_error($this->get_type()." $directory doesn't exist", E_USER_WARNING); |
170 | 170 | } |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | foreach($pathes as $what => $path) |
182 | 182 | { |
183 | 183 | $error = null; |
184 | - if(!file_exists($path)) |
|
184 | + if(!file_exists($path)) |
|
185 | 185 | $error = 'file does not exist'; |
186 | 186 | elseif(unlink($path)===false) |
187 | 187 | $error = 'unlink() failed'; |
@@ -235,16 +235,16 @@ discard block |
||
235 | 235 | global $settings; |
236 | 236 | $pi_manager = new PictureManager($item, $picture_type); |
237 | 237 | |
238 | - $pictures = $pi_manager->filenames(); |
|
238 | + $pictures = $pi_manager->filenames(); |
|
239 | 239 | |
240 | 240 | $item_model_type = get_class($item)::model_type(); |
241 | 241 | if(count($pictures)===0) |
242 | 242 | return hopper::file_uri($settings[$item_model_type][$picture_type]['generic_picture']); |
243 | 243 | |
244 | - if($settings[$item_model_type][$picture_type]['cycle_on_load']) |
|
245 | - $filename = $pictures[array_rand($pictures, 1)]; |
|
246 | - else |
|
247 | - $filename = array_shift($pictures); |
|
244 | + if($settings[$item_model_type][$picture_type]['cycle_on_load']) |
|
245 | + $filename = $pictures[array_rand($pictures, 1)]; |
|
246 | + else |
|
247 | + $filename = array_shift($pictures); |
|
248 | 248 | |
249 | 249 | return hopper::file_uri( $thumbnail===true ? $pi_manager->locate_thumbnail($filename) : $pi_manager->locate_file($filename)); |
250 | 250 | } |
@@ -9,9 +9,9 @@ |
||
9 | 9 | const TABLE_ALIAS = 'acl'; |
10 | 10 | |
11 | 11 | public function traceable() : bool |
12 | - { |
|
13 | - return false; |
|
14 | - } |
|
12 | + { |
|
13 | + return false; |
|
14 | + } |
|
15 | 15 | |
16 | 16 | public static function match(OperatorInterface $op, $permission_name) |
17 | 17 | { |
@@ -45,8 +45,8 @@ discard block |
||
45 | 45 | |
46 | 46 | public static function enhance_query_retrieve($Query, $filters, $options) |
47 | 47 | { |
48 | - $joined_alias = $Query->auto_join([ACL::table(),'ACL'], null, 'LEFT OUTER'); |
|
49 | - $joined_alias = $Query->auto_join([Permission::table(), 'permission'], null, 'LEFT OUTER'); |
|
48 | + $joined_alias = $Query->auto_join([ACL::table(),'ACL'], null, 'LEFT OUTER'); |
|
49 | + $joined_alias = $Query->auto_join([Permission::table(), 'permission'], null, 'LEFT OUTER'); |
|
50 | 50 | |
51 | 51 | $permission_ids_and_names = []; |
52 | 52 | $permission_ids_and_names []= sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $joined_alias, 'id', $joined_alias.'_ids'); |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | return $Query; |
68 | 68 | } |
69 | 69 | |
70 | - public function is_active() : bool |
|
70 | + public function is_active() : bool |
|
71 | 71 | { |
72 | 72 | return is_null($this->operator()) ? false : $this->operator()->is_active(); |
73 | 73 | } |
@@ -77,12 +77,12 @@ discard block |
||
77 | 77 | return is_null($this->operator()) ? null : $this->operator()->operator_id(); |
78 | 78 | } |
79 | 79 | |
80 | - public function username() |
|
80 | + public function username() |
|
81 | 81 | { |
82 | 82 | return is_null($this->operator()) ? null : $this->operator()->username(); |
83 | 83 | } |
84 | 84 | |
85 | - public function password() |
|
85 | + public function password() |
|
86 | 86 | { |
87 | 87 | return is_null($this->operator()) ? null : $this->operator()->password(); |
88 | 88 | } |
@@ -97,22 +97,22 @@ discard block |
||
97 | 97 | return $this->operator()->password_verify($string); |
98 | 98 | } |
99 | 99 | |
100 | - public function name() |
|
100 | + public function name() |
|
101 | 101 | { |
102 | 102 | return is_null($this->operator()) ? null : $this->operator()->name(); |
103 | 103 | } |
104 | 104 | |
105 | - public function email() |
|
105 | + public function email() |
|
106 | 106 | { |
107 | 107 | return is_null($this->operator()) ? null : $this->operator()->email(); |
108 | 108 | } |
109 | 109 | |
110 | - public function phone() |
|
110 | + public function phone() |
|
111 | 111 | { |
112 | 112 | return is_null($this->operator()) ? null : $this->operator()->phone(); |
113 | 113 | } |
114 | 114 | |
115 | - public function language_code() |
|
115 | + public function language_code() |
|
116 | 116 | { |
117 | 117 | return is_null($this->operator()) ? null : $this->operator()->language_code(); |
118 | 118 | } |
@@ -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 | } |
@@ -15,9 +15,9 @@ |
||
15 | 15 | } |
16 | 16 | |
17 | 17 | public function route_back($route_name = NULL, $route_params = []) : string |
18 | - { |
|
19 | - return $this->router()->prehop('traduko'); |
|
20 | - } |
|
18 | + { |
|
19 | + return $this->router()->prehop('traduko'); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | public function update_file($lang='fra') |
23 | 23 | { |