Commit 219b803651a31c53335a660e8c281d20ad5490d2
0 parents
DEEP
Showing
79 changed files
with
19751 additions
and
0 deletions
.htaccess
0 → 100755
DEEP.php
0 → 100755
1 | +++ a/DEEP.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Created by PhpStorm. | |
4 | + * User: Luigi Serra | |
5 | + * Date: 31/03/2015 | |
6 | + * Time: 17.25 | |
7 | + */ | |
8 | +ini_set('display_errors',1); | |
9 | +ini_set('display_startup_errors',1); | |
10 | +error_reporting(-1); | |
11 | + | |
12 | +require 'Slim/Slim.php'; | |
13 | + | |
14 | +class DEEP { | |
15 | + private static $instance = null; | |
16 | + private $app; | |
17 | + private $all_datalets; | |
18 | + private $all_controllets; | |
19 | + | |
20 | + public static function getInstance() | |
21 | + { | |
22 | + if(self::$instance == null) | |
23 | + { | |
24 | + $c = __CLASS__; | |
25 | + self::$instance = new $c; | |
26 | + } | |
27 | + | |
28 | + return self::$instance; | |
29 | + } | |
30 | + | |
31 | + private function __construct() | |
32 | + { | |
33 | + \Slim\Slim::registerAutoloader(); | |
34 | + $this->app = new \Slim\Slim(); | |
35 | + | |
36 | + $this->all_datalets = $this->loadServices("datalets.xml"); | |
37 | + $this->app->get('/datalets-list', function(){ | |
38 | + $this->app->response()->header("Content-Type", "application/json"); | |
39 | + $this->app->response()->header("Access-Control-Allow-Origin", "*"); | |
40 | + echo json_encode($this->all_datalets); | |
41 | + | |
42 | + }); | |
43 | + | |
44 | + $this->all_controllets = $this->loadServices("controllets.xml"); | |
45 | + $this->app->get('/controllets-list', function(){ | |
46 | + $this->app->response()->header("Content-Type", "application/json"); | |
47 | + $this->app->response()->header("Access-Control-Allow-Origin", "*"); | |
48 | + echo json_encode($this->all_controllets); | |
49 | + | |
50 | + }); | |
51 | + | |
52 | + //main service | |
53 | + $this->app->get('/', function(){ | |
54 | + echo "Hello from web compoments RESTful service, call /datalets-list to get datalets list"; | |
55 | + }); | |
56 | + } | |
57 | + | |
58 | + public function loadServices($source){ | |
59 | + $components_array = array(); | |
60 | + $handler_configuration = simplexml_load_file($source) or die("ERROR: cant read Components configuration \n"); | |
61 | + $deep_configuration = $handler_configuration->deep_handler_configuration; | |
62 | + | |
63 | + foreach($handler_configuration->components->children() as $component){ | |
64 | + //array_push($components_array, $component->name.""); | |
65 | + $component->url = $handler_configuration->deep_handler_configuration->components_repository_url_reference . $component->name . "/"; | |
66 | + array_push($components_array, $component); | |
67 | + $this->app->get('/'.$component->name, function() use($component, $deep_configuration ){ | |
68 | + $response = array( | |
69 | + "name" => $component->name."", | |
70 | + "bridge_link" => $deep_configuration->components_repository_url_reference."", | |
71 | + "component_link" => $component->name."/".$component->name.".html", | |
72 | + "idm" => $component->idm | |
73 | + ); | |
74 | + | |
75 | + if(isset($component->attributes)) { | |
76 | + $response['attributes'] = array(); | |
77 | + foreach ($component->attributes->children() as $attribute) { | |
78 | + array_push($response['attributes'], $attribute->name.""); | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + $this->app->response()->header("Content-Type", "application/json"); | |
83 | + $this->app->response()->header("Access-Control-Allow-Origin", "*"); | |
84 | + echo json_encode($response); | |
85 | + }); | |
86 | + } | |
87 | + return $components_array; | |
88 | + } | |
89 | + | |
90 | + public function run(){ | |
91 | + //run the Slim app | |
92 | + $this->app->run(); | |
93 | + } | |
94 | + | |
95 | + | |
96 | +} | |
97 | +?> | |
0 | 98 | \ No newline at end of file | ... | ... |
Slim/.gitignore
0 → 100755
Slim/.htaccess
0 → 100755
1 | +++ a/Slim/.htaccess | |
1 | +RewriteEngine On | |
2 | + | |
3 | +# Some hosts may require you to use the `RewriteBase` directive. | |
4 | +# If you need to use the `RewriteBase` directive, it should be the | |
5 | +# absolute physical path to the directory that contains this htaccess file. | |
6 | +# | |
7 | +# RewriteBase / | |
8 | + | |
9 | +RewriteCond %{REQUEST_FILENAME} !-d | |
10 | +RewriteCond %{REQUEST_FILENAME} !-f | |
11 | +RewriteRule ^ index.php [QSA,L] | ... | ... |
Slim/.travis.yml
0 → 100755
Slim/CONTRIBUTING.md
0 → 100755
1 | +++ a/Slim/CONTRIBUTING.md | |
1 | +# How to Contribute | |
2 | + | |
3 | +## Pull Requests | |
4 | + | |
5 | +1. Fork the Slim Framework repository | |
6 | +2. Create a new branch for each feature or improvement | |
7 | +3. Send a pull request from each feature branch to the **develop** branch | |
8 | + | |
9 | +It is very important to separate new features or improvements into separate feature branches, and to send a | |
10 | +pull request for each branch. This allows me to review and pull in new features or improvements individually. | |
11 | + | |
12 | +## Style Guide | |
13 | + | |
14 | +All pull requests must adhere to the [PSR-2 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). | |
15 | + | |
16 | +## Unit Testing | |
17 | + | |
18 | +All pull requests must be accompanied by passing unit tests and complete code coverage. The Slim Framework uses phpunit for testing. | |
19 | + | |
20 | +[Learn about PHPUnit](https://github.com/sebastianbergmann/phpunit/) | ... | ... |
Slim/Environment.php
0 → 100755
1 | +++ a/Slim/Environment.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim; | |
34 | + | |
35 | +/** | |
36 | + * Environment | |
37 | + * | |
38 | + * This class creates and returns a key/value array of common | |
39 | + * environment variables for the current HTTP request. | |
40 | + * | |
41 | + * This is a singleton class; derived environment variables will | |
42 | + * be common across multiple Slim applications. | |
43 | + * | |
44 | + * This class matches the Rack (Ruby) specification as closely | |
45 | + * as possible. More information available below. | |
46 | + * | |
47 | + * @package Slim | |
48 | + * @author Josh Lockhart | |
49 | + * @since 1.6.0 | |
50 | + */ | |
51 | +class Environment implements \ArrayAccess, \IteratorAggregate | |
52 | +{ | |
53 | + /** | |
54 | + * @var array | |
55 | + */ | |
56 | + protected $properties; | |
57 | + | |
58 | + /** | |
59 | + * @var \Slim\Environment | |
60 | + */ | |
61 | + protected static $environment; | |
62 | + | |
63 | + /** | |
64 | + * Get environment instance (singleton) | |
65 | + * | |
66 | + * This creates and/or returns an environment instance (singleton) | |
67 | + * derived from $_SERVER variables. You may override the global server | |
68 | + * variables by using `\Slim\Environment::mock()` instead. | |
69 | + * | |
70 | + * @param bool $refresh Refresh properties using global server variables? | |
71 | + * @return \Slim\Environment | |
72 | + */ | |
73 | + public static function getInstance($refresh = false) | |
74 | + { | |
75 | + if (is_null(self::$environment) || $refresh) { | |
76 | + self::$environment = new self(); | |
77 | + } | |
78 | + | |
79 | + return self::$environment; | |
80 | + } | |
81 | + | |
82 | + /** | |
83 | + * Get mock environment instance | |
84 | + * | |
85 | + * @param array $userSettings | |
86 | + * @return \Slim\Environment | |
87 | + */ | |
88 | + public static function mock($userSettings = array()) | |
89 | + { | |
90 | + $defaults = array( | |
91 | + 'REQUEST_METHOD' => 'GET', | |
92 | + 'SCRIPT_NAME' => '', | |
93 | + 'PATH_INFO' => '', | |
94 | + 'QUERY_STRING' => '', | |
95 | + 'SERVER_NAME' => 'localhost', | |
96 | + 'SERVER_PORT' => 80, | |
97 | + 'ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
98 | + 'ACCEPT_LANGUAGE' => 'en-US,en;q=0.8', | |
99 | + 'ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | |
100 | + 'USER_AGENT' => 'Slim Framework', | |
101 | + 'REMOTE_ADDR' => '127.0.0.1', | |
102 | + 'slim.url_scheme' => 'http', | |
103 | + 'slim.input' => '', | |
104 | + 'slim.errors' => @fopen('php://stderr', 'w') | |
105 | + ); | |
106 | + self::$environment = new self(array_merge($defaults, $userSettings)); | |
107 | + | |
108 | + return self::$environment; | |
109 | + } | |
110 | + | |
111 | + /** | |
112 | + * Constructor (private access) | |
113 | + * | |
114 | + * @param array|null $settings If present, these are used instead of global server variables | |
115 | + */ | |
116 | + private function __construct($settings = null) | |
117 | + { | |
118 | + if ($settings) { | |
119 | + $this->properties = $settings; | |
120 | + } else { | |
121 | + $env = array(); | |
122 | + | |
123 | + //The HTTP request method | |
124 | + $env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD']; | |
125 | + | |
126 | + //The IP | |
127 | + $env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; | |
128 | + | |
129 | + // Server params | |
130 | + $scriptName = $_SERVER['SCRIPT_NAME']; // <-- "/foo/index.php" | |
131 | + $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc" | |
132 | + $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; // <-- "test=abc" or "" | |
133 | + | |
134 | + // Physical path | |
135 | + if (strpos($requestUri, $scriptName) !== false) { | |
136 | + $physicalPath = $scriptName; // <-- Without rewriting | |
137 | + } else { | |
138 | + $physicalPath = str_replace('\\', '', dirname($scriptName)); // <-- With rewriting | |
139 | + } | |
140 | + $env['SCRIPT_NAME'] = rtrim($physicalPath, '/'); // <-- Remove trailing slashes | |
141 | + | |
142 | + // Virtual path | |
143 | + $env['PATH_INFO'] = $requestUri; | |
144 | + if (substr($requestUri, 0, strlen($physicalPath)) == $physicalPath) { | |
145 | + $env['PATH_INFO'] = substr($requestUri, strlen($physicalPath)); // <-- Remove physical path | |
146 | + } | |
147 | + $env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']); // <-- Remove query string | |
148 | + $env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); // <-- Ensure leading slash | |
149 | + | |
150 | + // Query string (without leading "?") | |
151 | + $env['QUERY_STRING'] = $queryString; | |
152 | + | |
153 | + //Name of server host that is running the script | |
154 | + $env['SERVER_NAME'] = $_SERVER['SERVER_NAME']; | |
155 | + | |
156 | + //Number of server port that is running the script | |
157 | + //Fixes: https://github.com/slimphp/Slim/issues/962 | |
158 | + $env['SERVER_PORT'] = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80; | |
159 | + | |
160 | + //HTTP request headers (retains HTTP_ prefix to match $_SERVER) | |
161 | + $headers = \Slim\Http\Headers::extract($_SERVER); | |
162 | + foreach ($headers as $key => $value) { | |
163 | + $env[$key] = $value; | |
164 | + } | |
165 | + | |
166 | + //Is the application running under HTTPS or HTTP protocol? | |
167 | + $env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; | |
168 | + | |
169 | + //Input stream (readable one time only; not available for multipart/form-data requests) | |
170 | + $rawInput = @file_get_contents('php://input'); | |
171 | + if (!$rawInput) { | |
172 | + $rawInput = ''; | |
173 | + } | |
174 | + $env['slim.input'] = $rawInput; | |
175 | + | |
176 | + //Error stream | |
177 | + $env['slim.errors'] = @fopen('php://stderr', 'w'); | |
178 | + | |
179 | + $this->properties = $env; | |
180 | + } | |
181 | + } | |
182 | + | |
183 | + /** | |
184 | + * Array Access: Offset Exists | |
185 | + */ | |
186 | + public function offsetExists($offset) | |
187 | + { | |
188 | + return isset($this->properties[$offset]); | |
189 | + } | |
190 | + | |
191 | + /** | |
192 | + * Array Access: Offset Get | |
193 | + */ | |
194 | + public function offsetGet($offset) | |
195 | + { | |
196 | + if (isset($this->properties[$offset])) { | |
197 | + return $this->properties[$offset]; | |
198 | + } | |
199 | + | |
200 | + return null; | |
201 | + } | |
202 | + | |
203 | + /** | |
204 | + * Array Access: Offset Set | |
205 | + */ | |
206 | + public function offsetSet($offset, $value) | |
207 | + { | |
208 | + $this->properties[$offset] = $value; | |
209 | + } | |
210 | + | |
211 | + /** | |
212 | + * Array Access: Offset Unset | |
213 | + */ | |
214 | + public function offsetUnset($offset) | |
215 | + { | |
216 | + unset($this->properties[$offset]); | |
217 | + } | |
218 | + | |
219 | + /** | |
220 | + * IteratorAggregate | |
221 | + * | |
222 | + * @return \ArrayIterator | |
223 | + */ | |
224 | + public function getIterator() | |
225 | + { | |
226 | + return new \ArrayIterator($this->properties); | |
227 | + } | |
228 | +} | ... | ... |
Slim/Exception/Pass.php
0 → 100755
1 | +++ a/Slim/Exception/Pass.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Exception; | |
34 | + | |
35 | +/** | |
36 | + * Pass Exception | |
37 | + * | |
38 | + * This Exception will cause the Router::dispatch method | |
39 | + * to skip the current matching route and continue to the next | |
40 | + * matching route. If no subsequent routes are found, a | |
41 | + * HTTP 404 Not Found response will be sent to the client. | |
42 | + * | |
43 | + * @package Slim | |
44 | + * @author Josh Lockhart | |
45 | + * @since 1.0.0 | |
46 | + */ | |
47 | +class Pass extends \Exception | |
48 | +{ | |
49 | +} | ... | ... |
Slim/Exception/Stop.php
0 → 100755
1 | +++ a/Slim/Exception/Stop.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Exception; | |
34 | + | |
35 | +/** | |
36 | + * Stop Exception | |
37 | + * | |
38 | + * This Exception is thrown when the Slim application needs to abort | |
39 | + * processing and return control flow to the outer PHP script. | |
40 | + * | |
41 | + * @package Slim | |
42 | + * @author Josh Lockhart | |
43 | + * @since 1.0.0 | |
44 | + */ | |
45 | +class Stop extends \Exception | |
46 | +{ | |
47 | +} | ... | ... |
Slim/Helper/Set.php
0 → 100755
1 | +++ a/Slim/Helper/Set.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Helper; | |
34 | + | |
35 | +class Set implements \ArrayAccess, \Countable, \IteratorAggregate | |
36 | +{ | |
37 | + /** | |
38 | + * Key-value array of arbitrary data | |
39 | + * @var array | |
40 | + */ | |
41 | + protected $data = array(); | |
42 | + | |
43 | + /** | |
44 | + * Constructor | |
45 | + * @param array $items Pre-populate set with this key-value array | |
46 | + */ | |
47 | + public function __construct($items = array()) | |
48 | + { | |
49 | + $this->replace($items); | |
50 | + } | |
51 | + | |
52 | + /** | |
53 | + * Normalize data key | |
54 | + * | |
55 | + * Used to transform data key into the necessary | |
56 | + * key format for this set. Used in subclasses | |
57 | + * like \Slim\Http\Headers. | |
58 | + * | |
59 | + * @param string $key The data key | |
60 | + * @return mixed The transformed/normalized data key | |
61 | + */ | |
62 | + protected function normalizeKey($key) | |
63 | + { | |
64 | + return $key; | |
65 | + } | |
66 | + | |
67 | + /** | |
68 | + * Set data key to value | |
69 | + * @param string $key The data key | |
70 | + * @param mixed $value The data value | |
71 | + */ | |
72 | + public function set($key, $value) | |
73 | + { | |
74 | + $this->data[$this->normalizeKey($key)] = $value; | |
75 | + } | |
76 | + | |
77 | + /** | |
78 | + * Get data value with key | |
79 | + * @param string $key The data key | |
80 | + * @param mixed $default The value to return if data key does not exist | |
81 | + * @return mixed The data value, or the default value | |
82 | + */ | |
83 | + public function get($key, $default = null) | |
84 | + { | |
85 | + if ($this->has($key)) { | |
86 | + $isInvokable = is_object($this->data[$this->normalizeKey($key)]) && method_exists($this->data[$this->normalizeKey($key)], '__invoke'); | |
87 | + | |
88 | + return $isInvokable ? $this->data[$this->normalizeKey($key)]($this) : $this->data[$this->normalizeKey($key)]; | |
89 | + } | |
90 | + | |
91 | + return $default; | |
92 | + } | |
93 | + | |
94 | + /** | |
95 | + * Add data to set | |
96 | + * @param array $items Key-value array of data to append to this set | |
97 | + */ | |
98 | + public function replace($items) | |
99 | + { | |
100 | + foreach ($items as $key => $value) { | |
101 | + $this->set($key, $value); // Ensure keys are normalized | |
102 | + } | |
103 | + } | |
104 | + | |
105 | + /** | |
106 | + * Fetch set data | |
107 | + * @return array This set's key-value data array | |
108 | + */ | |
109 | + public function all() | |
110 | + { | |
111 | + return $this->data; | |
112 | + } | |
113 | + | |
114 | + /** | |
115 | + * Fetch set data keys | |
116 | + * @return array This set's key-value data array keys | |
117 | + */ | |
118 | + public function keys() | |
119 | + { | |
120 | + return array_keys($this->data); | |
121 | + } | |
122 | + | |
123 | + /** | |
124 | + * Does this set contain a key? | |
125 | + * @param string $key The data key | |
126 | + * @return boolean | |
127 | + */ | |
128 | + public function has($key) | |
129 | + { | |
130 | + return array_key_exists($this->normalizeKey($key), $this->data); | |
131 | + } | |
132 | + | |
133 | + /** | |
134 | + * Remove value with key from this set | |
135 | + * @param string $key The data key | |
136 | + */ | |
137 | + public function remove($key) | |
138 | + { | |
139 | + unset($this->data[$this->normalizeKey($key)]); | |
140 | + } | |
141 | + | |
142 | + /** | |
143 | + * Property Overloading | |
144 | + */ | |
145 | + | |
146 | + public function __get($key) | |
147 | + { | |
148 | + return $this->get($key); | |
149 | + } | |
150 | + | |
151 | + public function __set($key, $value) | |
152 | + { | |
153 | + $this->set($key, $value); | |
154 | + } | |
155 | + | |
156 | + public function __isset($key) | |
157 | + { | |
158 | + return $this->has($key); | |
159 | + } | |
160 | + | |
161 | + public function __unset($key) | |
162 | + { | |
163 | + $this->remove($key); | |
164 | + } | |
165 | + | |
166 | + /** | |
167 | + * Clear all values | |
168 | + */ | |
169 | + public function clear() | |
170 | + { | |
171 | + $this->data = array(); | |
172 | + } | |
173 | + | |
174 | + /** | |
175 | + * Array Access | |
176 | + */ | |
177 | + | |
178 | + public function offsetExists($offset) | |
179 | + { | |
180 | + return $this->has($offset); | |
181 | + } | |
182 | + | |
183 | + public function offsetGet($offset) | |
184 | + { | |
185 | + return $this->get($offset); | |
186 | + } | |
187 | + | |
188 | + public function offsetSet($offset, $value) | |
189 | + { | |
190 | + $this->set($offset, $value); | |
191 | + } | |
192 | + | |
193 | + public function offsetUnset($offset) | |
194 | + { | |
195 | + $this->remove($offset); | |
196 | + } | |
197 | + | |
198 | + /** | |
199 | + * Countable | |
200 | + */ | |
201 | + | |
202 | + public function count() | |
203 | + { | |
204 | + return count($this->data); | |
205 | + } | |
206 | + | |
207 | + /** | |
208 | + * IteratorAggregate | |
209 | + */ | |
210 | + | |
211 | + public function getIterator() | |
212 | + { | |
213 | + return new \ArrayIterator($this->data); | |
214 | + } | |
215 | + | |
216 | + /** | |
217 | + * Ensure a value or object will remain globally unique | |
218 | + * @param string $key The value or object name | |
219 | + * @param \Closure $value The closure that defines the object | |
220 | + * @return mixed | |
221 | + */ | |
222 | + public function singleton($key, $value) | |
223 | + { | |
224 | + $this->set($key, function ($c) use ($value) { | |
225 | + static $object; | |
226 | + | |
227 | + if (null === $object) { | |
228 | + $object = $value($c); | |
229 | + } | |
230 | + | |
231 | + return $object; | |
232 | + }); | |
233 | + } | |
234 | + | |
235 | + /** | |
236 | + * Protect closure from being directly invoked | |
237 | + * @param \Closure $callable A closure to keep from being invoked and evaluated | |
238 | + * @return \Closure | |
239 | + */ | |
240 | + public function protect(\Closure $callable) | |
241 | + { | |
242 | + return function () use ($callable) { | |
243 | + return $callable; | |
244 | + }; | |
245 | + } | |
246 | +} | ... | ... |
Slim/Http/Cookies.php
0 → 100755
1 | +++ a/Slim/Http/Cookies.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Http; | |
34 | + | |
35 | +class Cookies extends \Slim\Helper\Set | |
36 | +{ | |
37 | + /** | |
38 | + * Default cookie settings | |
39 | + * @var array | |
40 | + */ | |
41 | + protected $defaults = array( | |
42 | + 'value' => '', | |
43 | + 'domain' => null, | |
44 | + 'path' => null, | |
45 | + 'expires' => null, | |
46 | + 'secure' => false, | |
47 | + 'httponly' => false | |
48 | + ); | |
49 | + | |
50 | + /** | |
51 | + * Set cookie | |
52 | + * | |
53 | + * The second argument may be a single scalar value, in which case | |
54 | + * it will be merged with the default settings and considered the `value` | |
55 | + * of the merged result. | |
56 | + * | |
57 | + * The second argument may also be an array containing any or all of | |
58 | + * the keys shown in the default settings above. This array will be | |
59 | + * merged with the defaults shown above. | |
60 | + * | |
61 | + * @param string $key Cookie name | |
62 | + * @param mixed $value Cookie settings | |
63 | + */ | |
64 | + public function set($key, $value) | |
65 | + { | |
66 | + if (is_array($value)) { | |
67 | + $cookieSettings = array_replace($this->defaults, $value); | |
68 | + } else { | |
69 | + $cookieSettings = array_replace($this->defaults, array('value' => $value)); | |
70 | + } | |
71 | + parent::set($key, $cookieSettings); | |
72 | + } | |
73 | + | |
74 | + /** | |
75 | + * Remove cookie | |
76 | + * | |
77 | + * Unlike \Slim\Helper\Set, this will actually *set* a cookie with | |
78 | + * an expiration date in the past. This expiration date will force | |
79 | + * the client-side cache to remove its cookie with the given name | |
80 | + * and settings. | |
81 | + * | |
82 | + * @param string $key Cookie name | |
83 | + * @param array $settings Optional cookie settings | |
84 | + */ | |
85 | + public function remove($key, $settings = array()) | |
86 | + { | |
87 | + $settings['value'] = ''; | |
88 | + $settings['expires'] = time() - 86400; | |
89 | + $this->set($key, array_replace($this->defaults, $settings)); | |
90 | + } | |
91 | +} | ... | ... |
Slim/Http/Headers.php
0 → 100755
1 | +++ a/Slim/Http/Headers.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Http; | |
34 | + | |
35 | + /** | |
36 | + * HTTP Headers | |
37 | + * | |
38 | + * @package Slim | |
39 | + * @author Josh Lockhart | |
40 | + * @since 1.6.0 | |
41 | + */ | |
42 | +class Headers extends \Slim\Helper\Set | |
43 | +{ | |
44 | + /******************************************************************************** | |
45 | + * Static interface | |
46 | + *******************************************************************************/ | |
47 | + | |
48 | + /** | |
49 | + * Special-case HTTP headers that are otherwise unidentifiable as HTTP headers. | |
50 | + * Typically, HTTP headers in the $_SERVER array will be prefixed with | |
51 | + * `HTTP_` or `X_`. These are not so we list them here for later reference. | |
52 | + * | |
53 | + * @var array | |
54 | + */ | |
55 | + protected static $special = array( | |
56 | + 'CONTENT_TYPE', | |
57 | + 'CONTENT_LENGTH', | |
58 | + 'PHP_AUTH_USER', | |
59 | + 'PHP_AUTH_PW', | |
60 | + 'PHP_AUTH_DIGEST', | |
61 | + 'AUTH_TYPE' | |
62 | + ); | |
63 | + | |
64 | + /** | |
65 | + * Extract HTTP headers from an array of data (e.g. $_SERVER) | |
66 | + * @param array $data | |
67 | + * @return array | |
68 | + */ | |
69 | + public static function extract($data) | |
70 | + { | |
71 | + $results = array(); | |
72 | + foreach ($data as $key => $value) { | |
73 | + $key = strtoupper($key); | |
74 | + if (strpos($key, 'X_') === 0 || strpos($key, 'HTTP_') === 0 || in_array($key, static::$special)) { | |
75 | + if ($key === 'HTTP_CONTENT_LENGTH') { | |
76 | + continue; | |
77 | + } | |
78 | + $results[$key] = $value; | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + return $results; | |
83 | + } | |
84 | + | |
85 | + /******************************************************************************** | |
86 | + * Instance interface | |
87 | + *******************************************************************************/ | |
88 | + | |
89 | + /** | |
90 | + * Transform header name into canonical form | |
91 | + * @param string $key | |
92 | + * @return string | |
93 | + */ | |
94 | + protected function normalizeKey($key) | |
95 | + { | |
96 | + $key = strtolower($key); | |
97 | + $key = str_replace(array('-', '_'), ' ', $key); | |
98 | + $key = preg_replace('#^http #', '', $key); | |
99 | + $key = ucwords($key); | |
100 | + $key = str_replace(' ', '-', $key); | |
101 | + | |
102 | + return $key; | |
103 | + } | |
104 | +} | ... | ... |
Slim/Http/Request.php
0 → 100755
1 | +++ a/Slim/Http/Request.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Http; | |
34 | + | |
35 | +/** | |
36 | + * Slim HTTP Request | |
37 | + * | |
38 | + * This class provides a human-friendly interface to the Slim environment variables; | |
39 | + * environment variables are passed by reference and will be modified directly. | |
40 | + * | |
41 | + * @package Slim | |
42 | + * @author Josh Lockhart | |
43 | + * @since 1.0.0 | |
44 | + */ | |
45 | +class Request | |
46 | +{ | |
47 | + const METHOD_HEAD = 'HEAD'; | |
48 | + const METHOD_GET = 'GET'; | |
49 | + const METHOD_POST = 'POST'; | |
50 | + const METHOD_PUT = 'PUT'; | |
51 | + const METHOD_PATCH = 'PATCH'; | |
52 | + const METHOD_DELETE = 'DELETE'; | |
53 | + const METHOD_OPTIONS = 'OPTIONS'; | |
54 | + const METHOD_OVERRIDE = '_METHOD'; | |
55 | + | |
56 | + /** | |
57 | + * @var array | |
58 | + */ | |
59 | + protected static $formDataMediaTypes = array('application/x-www-form-urlencoded'); | |
60 | + | |
61 | + /** | |
62 | + * Application Environment | |
63 | + * @var \Slim\Environment | |
64 | + */ | |
65 | + protected $env; | |
66 | + | |
67 | + /** | |
68 | + * HTTP Headers | |
69 | + * @var \Slim\Http\Headers | |
70 | + */ | |
71 | + public $headers; | |
72 | + | |
73 | + /** | |
74 | + * HTTP Cookies | |
75 | + * @var \Slim\Helper\Set | |
76 | + */ | |
77 | + public $cookies; | |
78 | + | |
79 | + /** | |
80 | + * Constructor | |
81 | + * @param \Slim\Environment $env | |
82 | + */ | |
83 | + public function __construct(\Slim\Environment $env) | |
84 | + { | |
85 | + $this->env = $env; | |
86 | + $this->headers = new \Slim\Http\Headers(\Slim\Http\Headers::extract($env)); | |
87 | + $this->cookies = new \Slim\Helper\Set(\Slim\Http\Util::parseCookieHeader($env['HTTP_COOKIE'])); | |
88 | + } | |
89 | + | |
90 | + /** | |
91 | + * Get HTTP method | |
92 | + * @return string | |
93 | + */ | |
94 | + public function getMethod() | |
95 | + { | |
96 | + return $this->env['REQUEST_METHOD']; | |
97 | + } | |
98 | + | |
99 | + /** | |
100 | + * Is this a GET request? | |
101 | + * @return bool | |
102 | + */ | |
103 | + public function isGet() | |
104 | + { | |
105 | + return $this->getMethod() === self::METHOD_GET; | |
106 | + } | |
107 | + | |
108 | + /** | |
109 | + * Is this a POST request? | |
110 | + * @return bool | |
111 | + */ | |
112 | + public function isPost() | |
113 | + { | |
114 | + return $this->getMethod() === self::METHOD_POST; | |
115 | + } | |
116 | + | |
117 | + /** | |
118 | + * Is this a PUT request? | |
119 | + * @return bool | |
120 | + */ | |
121 | + public function isPut() | |
122 | + { | |
123 | + return $this->getMethod() === self::METHOD_PUT; | |
124 | + } | |
125 | + | |
126 | + /** | |
127 | + * Is this a PATCH request? | |
128 | + * @return bool | |
129 | + */ | |
130 | + public function isPatch() | |
131 | + { | |
132 | + return $this->getMethod() === self::METHOD_PATCH; | |
133 | + } | |
134 | + | |
135 | + /** | |
136 | + * Is this a DELETE request? | |
137 | + * @return bool | |
138 | + */ | |
139 | + public function isDelete() | |
140 | + { | |
141 | + return $this->getMethod() === self::METHOD_DELETE; | |
142 | + } | |
143 | + | |
144 | + /** | |
145 | + * Is this a HEAD request? | |
146 | + * @return bool | |
147 | + */ | |
148 | + public function isHead() | |
149 | + { | |
150 | + return $this->getMethod() === self::METHOD_HEAD; | |
151 | + } | |
152 | + | |
153 | + /** | |
154 | + * Is this a OPTIONS request? | |
155 | + * @return bool | |
156 | + */ | |
157 | + public function isOptions() | |
158 | + { | |
159 | + return $this->getMethod() === self::METHOD_OPTIONS; | |
160 | + } | |
161 | + | |
162 | + /** | |
163 | + * Is this an AJAX request? | |
164 | + * @return bool | |
165 | + */ | |
166 | + public function isAjax() | |
167 | + { | |
168 | + if ($this->params('isajax')) { | |
169 | + return true; | |
170 | + } elseif (isset($this->headers['X_REQUESTED_WITH']) && $this->headers['X_REQUESTED_WITH'] === 'XMLHttpRequest') { | |
171 | + return true; | |
172 | + } | |
173 | + | |
174 | + return false; | |
175 | + } | |
176 | + | |
177 | + /** | |
178 | + * Is this an XHR request? (alias of Slim_Http_Request::isAjax) | |
179 | + * @return bool | |
180 | + */ | |
181 | + public function isXhr() | |
182 | + { | |
183 | + return $this->isAjax(); | |
184 | + } | |
185 | + | |
186 | + /** | |
187 | + * Fetch GET and POST data | |
188 | + * | |
189 | + * This method returns a union of GET and POST data as a key-value array, or the value | |
190 | + * of the array key if requested; if the array key does not exist, NULL is returned, | |
191 | + * unless there is a default value specified. | |
192 | + * | |
193 | + * @param string $key | |
194 | + * @param mixed $default | |
195 | + * @return array|mixed|null | |
196 | + */ | |
197 | + public function params($key = null, $default = null) | |
198 | + { | |
199 | + $union = array_merge($this->get(), $this->post()); | |
200 | + if ($key) { | |
201 | + return isset($union[$key]) ? $union[$key] : $default; | |
202 | + } | |
203 | + | |
204 | + return $union; | |
205 | + } | |
206 | + | |
207 | + /** | |
208 | + * Fetch GET data | |
209 | + * | |
210 | + * This method returns a key-value array of data sent in the HTTP request query string, or | |
211 | + * the value of the array key if requested; if the array key does not exist, NULL is returned. | |
212 | + * | |
213 | + * @param string $key | |
214 | + * @param mixed $default Default return value when key does not exist | |
215 | + * @return array|mixed|null | |
216 | + */ | |
217 | + public function get($key = null, $default = null) | |
218 | + { | |
219 | + if (!isset($this->env['slim.request.query_hash'])) { | |
220 | + $output = array(); | |
221 | + if (function_exists('mb_parse_str') && !isset($this->env['slim.tests.ignore_multibyte'])) { | |
222 | + mb_parse_str($this->env['QUERY_STRING'], $output); | |
223 | + } else { | |
224 | + parse_str($this->env['QUERY_STRING'], $output); | |
225 | + } | |
226 | + $this->env['slim.request.query_hash'] = Util::stripSlashesIfMagicQuotes($output); | |
227 | + } | |
228 | + if ($key) { | |
229 | + if (isset($this->env['slim.request.query_hash'][$key])) { | |
230 | + return $this->env['slim.request.query_hash'][$key]; | |
231 | + } else { | |
232 | + return $default; | |
233 | + } | |
234 | + } else { | |
235 | + return $this->env['slim.request.query_hash']; | |
236 | + } | |
237 | + } | |
238 | + | |
239 | + /** | |
240 | + * Fetch POST data | |
241 | + * | |
242 | + * This method returns a key-value array of data sent in the HTTP request body, or | |
243 | + * the value of a hash key if requested; if the array key does not exist, NULL is returned. | |
244 | + * | |
245 | + * @param string $key | |
246 | + * @param mixed $default Default return value when key does not exist | |
247 | + * @return array|mixed|null | |
248 | + * @throws \RuntimeException If environment input is not available | |
249 | + */ | |
250 | + public function post($key = null, $default = null) | |
251 | + { | |
252 | + if (!isset($this->env['slim.input'])) { | |
253 | + throw new \RuntimeException('Missing slim.input in environment variables'); | |
254 | + } | |
255 | + if (!isset($this->env['slim.request.form_hash'])) { | |
256 | + $this->env['slim.request.form_hash'] = array(); | |
257 | + if ($this->isFormData() && is_string($this->env['slim.input'])) { | |
258 | + $output = array(); | |
259 | + if (function_exists('mb_parse_str') && !isset($this->env['slim.tests.ignore_multibyte'])) { | |
260 | + mb_parse_str($this->env['slim.input'], $output); | |
261 | + } else { | |
262 | + parse_str($this->env['slim.input'], $output); | |
263 | + } | |
264 | + $this->env['slim.request.form_hash'] = Util::stripSlashesIfMagicQuotes($output); | |
265 | + } else { | |
266 | + $this->env['slim.request.form_hash'] = Util::stripSlashesIfMagicQuotes($_POST); | |
267 | + } | |
268 | + } | |
269 | + if ($key) { | |
270 | + if (isset($this->env['slim.request.form_hash'][$key])) { | |
271 | + return $this->env['slim.request.form_hash'][$key]; | |
272 | + } else { | |
273 | + return $default; | |
274 | + } | |
275 | + } else { | |
276 | + return $this->env['slim.request.form_hash']; | |
277 | + } | |
278 | + } | |
279 | + | |
280 | + /** | |
281 | + * Fetch PUT data (alias for \Slim\Http\Request::post) | |
282 | + * @param string $key | |
283 | + * @param mixed $default Default return value when key does not exist | |
284 | + * @return array|mixed|null | |
285 | + */ | |
286 | + public function put($key = null, $default = null) | |
287 | + { | |
288 | + return $this->post($key, $default); | |
289 | + } | |
290 | + | |
291 | + /** | |
292 | + * Fetch PATCH data (alias for \Slim\Http\Request::post) | |
293 | + * @param string $key | |
294 | + * @param mixed $default Default return value when key does not exist | |
295 | + * @return array|mixed|null | |
296 | + */ | |
297 | + public function patch($key = null, $default = null) | |
298 | + { | |
299 | + return $this->post($key, $default); | |
300 | + } | |
301 | + | |
302 | + /** | |
303 | + * Fetch DELETE data (alias for \Slim\Http\Request::post) | |
304 | + * @param string $key | |
305 | + * @param mixed $default Default return value when key does not exist | |
306 | + * @return array|mixed|null | |
307 | + */ | |
308 | + public function delete($key = null, $default = null) | |
309 | + { | |
310 | + return $this->post($key, $default); | |
311 | + } | |
312 | + | |
313 | + /** | |
314 | + * Fetch COOKIE data | |
315 | + * | |
316 | + * This method returns a key-value array of Cookie data sent in the HTTP request, or | |
317 | + * the value of a array key if requested; if the array key does not exist, NULL is returned. | |
318 | + * | |
319 | + * @param string $key | |
320 | + * @return array|string|null | |
321 | + */ | |
322 | + public function cookies($key = null) | |
323 | + { | |
324 | + if ($key) { | |
325 | + return $this->cookies->get($key); | |
326 | + } | |
327 | + | |
328 | + return $this->cookies; | |
329 | + // if (!isset($this->env['slim.request.cookie_hash'])) { | |
330 | + // $cookieHeader = isset($this->env['COOKIE']) ? $this->env['COOKIE'] : ''; | |
331 | + // $this->env['slim.request.cookie_hash'] = Util::parseCookieHeader($cookieHeader); | |
332 | + // } | |
333 | + // if ($key) { | |
334 | + // if (isset($this->env['slim.request.cookie_hash'][$key])) { | |
335 | + // return $this->env['slim.request.cookie_hash'][$key]; | |
336 | + // } else { | |
337 | + // return null; | |
338 | + // } | |
339 | + // } else { | |
340 | + // return $this->env['slim.request.cookie_hash']; | |
341 | + // } | |
342 | + } | |
343 | + | |
344 | + /** | |
345 | + * Does the Request body contain parsed form data? | |
346 | + * @return bool | |
347 | + */ | |
348 | + public function isFormData() | |
349 | + { | |
350 | + $method = isset($this->env['slim.method_override.original_method']) ? $this->env['slim.method_override.original_method'] : $this->getMethod(); | |
351 | + | |
352 | + return ($method === self::METHOD_POST && is_null($this->getContentType())) || in_array($this->getMediaType(), self::$formDataMediaTypes); | |
353 | + } | |
354 | + | |
355 | + /** | |
356 | + * Get Headers | |
357 | + * | |
358 | + * This method returns a key-value array of headers sent in the HTTP request, or | |
359 | + * the value of a hash key if requested; if the array key does not exist, NULL is returned. | |
360 | + * | |
361 | + * @param string $key | |
362 | + * @param mixed $default The default value returned if the requested header is not available | |
363 | + * @return mixed | |
364 | + */ | |
365 | + public function headers($key = null, $default = null) | |
366 | + { | |
367 | + if ($key) { | |
368 | + return $this->headers->get($key, $default); | |
369 | + } | |
370 | + | |
371 | + return $this->headers; | |
372 | + // if ($key) { | |
373 | + // $key = strtoupper($key); | |
374 | + // $key = str_replace('-', '_', $key); | |
375 | + // $key = preg_replace('@^HTTP_@', '', $key); | |
376 | + // if (isset($this->env[$key])) { | |
377 | + // return $this->env[$key]; | |
378 | + // } else { | |
379 | + // return $default; | |
380 | + // } | |
381 | + // } else { | |
382 | + // $headers = array(); | |
383 | + // foreach ($this->env as $key => $value) { | |
384 | + // if (strpos($key, 'slim.') !== 0) { | |
385 | + // $headers[$key] = $value; | |
386 | + // } | |
387 | + // } | |
388 | + // | |
389 | + // return $headers; | |
390 | + // } | |
391 | + } | |
392 | + | |
393 | + /** | |
394 | + * Get Body | |
395 | + * @return string | |
396 | + */ | |
397 | + public function getBody() | |
398 | + { | |
399 | + return $this->env['slim.input']; | |
400 | + } | |
401 | + | |
402 | + /** | |
403 | + * Get Content Type | |
404 | + * @return string|null | |
405 | + */ | |
406 | + public function getContentType() | |
407 | + { | |
408 | + return $this->headers->get('CONTENT_TYPE'); | |
409 | + } | |
410 | + | |
411 | + /** | |
412 | + * Get Media Type (type/subtype within Content Type header) | |
413 | + * @return string|null | |
414 | + */ | |
415 | + public function getMediaType() | |
416 | + { | |
417 | + $contentType = $this->getContentType(); | |
418 | + if ($contentType) { | |
419 | + $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType); | |
420 | + | |
421 | + return strtolower($contentTypeParts[0]); | |
422 | + } | |
423 | + | |
424 | + return null; | |
425 | + } | |
426 | + | |
427 | + /** | |
428 | + * Get Media Type Params | |
429 | + * @return array | |
430 | + */ | |
431 | + public function getMediaTypeParams() | |
432 | + { | |
433 | + $contentType = $this->getContentType(); | |
434 | + $contentTypeParams = array(); | |
435 | + if ($contentType) { | |
436 | + $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType); | |
437 | + $contentTypePartsLength = count($contentTypeParts); | |
438 | + for ($i = 1; $i < $contentTypePartsLength; $i++) { | |
439 | + $paramParts = explode('=', $contentTypeParts[$i]); | |
440 | + $contentTypeParams[strtolower($paramParts[0])] = $paramParts[1]; | |
441 | + } | |
442 | + } | |
443 | + | |
444 | + return $contentTypeParams; | |
445 | + } | |
446 | + | |
447 | + /** | |
448 | + * Get Content Charset | |
449 | + * @return string|null | |
450 | + */ | |
451 | + public function getContentCharset() | |
452 | + { | |
453 | + $mediaTypeParams = $this->getMediaTypeParams(); | |
454 | + if (isset($mediaTypeParams['charset'])) { | |
455 | + return $mediaTypeParams['charset']; | |
456 | + } | |
457 | + | |
458 | + return null; | |
459 | + } | |
460 | + | |
461 | + /** | |
462 | + * Get Content-Length | |
463 | + * @return int | |
464 | + */ | |
465 | + public function getContentLength() | |
466 | + { | |
467 | + return $this->headers->get('CONTENT_LENGTH', 0); | |
468 | + } | |
469 | + | |
470 | + /** | |
471 | + * Get Host | |
472 | + * @return string | |
473 | + */ | |
474 | + public function getHost() | |
475 | + { | |
476 | + if (isset($this->env['HTTP_HOST'])) { | |
477 | + if (strpos($this->env['HTTP_HOST'], ':') !== false) { | |
478 | + $hostParts = explode(':', $this->env['HTTP_HOST']); | |
479 | + | |
480 | + return $hostParts[0]; | |
481 | + } | |
482 | + | |
483 | + return $this->env['HTTP_HOST']; | |
484 | + } | |
485 | + | |
486 | + return $this->env['SERVER_NAME']; | |
487 | + } | |
488 | + | |
489 | + /** | |
490 | + * Get Host with Port | |
491 | + * @return string | |
492 | + */ | |
493 | + public function getHostWithPort() | |
494 | + { | |
495 | + return sprintf('%s:%s', $this->getHost(), $this->getPort()); | |
496 | + } | |
497 | + | |
498 | + /** | |
499 | + * Get Port | |
500 | + * @return int | |
501 | + */ | |
502 | + public function getPort() | |
503 | + { | |
504 | + return (int)$this->env['SERVER_PORT']; | |
505 | + } | |
506 | + | |
507 | + /** | |
508 | + * Get Scheme (https or http) | |
509 | + * @return string | |
510 | + */ | |
511 | + public function getScheme() | |
512 | + { | |
513 | + return $this->env['slim.url_scheme']; | |
514 | + } | |
515 | + | |
516 | + /** | |
517 | + * Get Script Name (physical path) | |
518 | + * @return string | |
519 | + */ | |
520 | + public function getScriptName() | |
521 | + { | |
522 | + return $this->env['SCRIPT_NAME']; | |
523 | + } | |
524 | + | |
525 | + /** | |
526 | + * LEGACY: Get Root URI (alias for Slim_Http_Request::getScriptName) | |
527 | + * @return string | |
528 | + */ | |
529 | + public function getRootUri() | |
530 | + { | |
531 | + return $this->getScriptName(); | |
532 | + } | |
533 | + | |
534 | + /** | |
535 | + * Get Path (physical path + virtual path) | |
536 | + * @return string | |
537 | + */ | |
538 | + public function getPath() | |
539 | + { | |
540 | + return $this->getScriptName() . $this->getPathInfo(); | |
541 | + } | |
542 | + | |
543 | + /** | |
544 | + * Get Path Info (virtual path) | |
545 | + * @return string | |
546 | + */ | |
547 | + public function getPathInfo() | |
548 | + { | |
549 | + return $this->env['PATH_INFO']; | |
550 | + } | |
551 | + | |
552 | + /** | |
553 | + * LEGACY: Get Resource URI (alias for Slim_Http_Request::getPathInfo) | |
554 | + * @return string | |
555 | + */ | |
556 | + public function getResourceUri() | |
557 | + { | |
558 | + return $this->getPathInfo(); | |
559 | + } | |
560 | + | |
561 | + /** | |
562 | + * Get URL (scheme + host [ + port if non-standard ]) | |
563 | + * @return string | |
564 | + */ | |
565 | + public function getUrl() | |
566 | + { | |
567 | + $url = $this->getScheme() . '://' . $this->getHost(); | |
568 | + if (($this->getScheme() === 'https' && $this->getPort() !== 443) || ($this->getScheme() === 'http' && $this->getPort() !== 80)) { | |
569 | + $url .= sprintf(':%s', $this->getPort()); | |
570 | + } | |
571 | + | |
572 | + return $url; | |
573 | + } | |
574 | + | |
575 | + /** | |
576 | + * Get IP | |
577 | + * @return string | |
578 | + */ | |
579 | + public function getIp() | |
580 | + { | |
581 | + $keys = array('X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP', 'REMOTE_ADDR'); | |
582 | + foreach ($keys as $key) { | |
583 | + if (isset($this->env[$key])) { | |
584 | + return $this->env[$key]; | |
585 | + } | |
586 | + } | |
587 | + | |
588 | + return $this->env['REMOTE_ADDR']; | |
589 | + } | |
590 | + | |
591 | + /** | |
592 | + * Get Referrer | |
593 | + * @return string|null | |
594 | + */ | |
595 | + public function getReferrer() | |
596 | + { | |
597 | + return $this->headers->get('HTTP_REFERER'); | |
598 | + } | |
599 | + | |
600 | + /** | |
601 | + * Get Referer (for those who can't spell) | |
602 | + * @return string|null | |
603 | + */ | |
604 | + public function getReferer() | |
605 | + { | |
606 | + return $this->getReferrer(); | |
607 | + } | |
608 | + | |
609 | + /** | |
610 | + * Get User Agent | |
611 | + * @return string|null | |
612 | + */ | |
613 | + public function getUserAgent() | |
614 | + { | |
615 | + return $this->headers->get('HTTP_USER_AGENT'); | |
616 | + } | |
617 | +} | ... | ... |
Slim/Http/Response.php
0 → 100755
1 | +++ a/Slim/Http/Response.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Slim - a micro PHP 5 framework | |
4 | + * | |
5 | + * @author Josh Lockhart <info@slimframework.com> | |
6 | + * @copyright 2011 Josh Lockhart | |
7 | + * @link http://www.slimframework.com | |
8 | + * @license http://www.slimframework.com/license | |
9 | + * @version 2.6.1 | |
10 | + * @package Slim | |
11 | + * | |
12 | + * MIT LICENSE | |
13 | + * | |
14 | + * Permission is hereby granted, free of charge, to any person obtaining | |
15 | + * a copy of this software and associated documentation files (the | |
16 | + * "Software"), to deal in the Software without restriction, including | |
17 | + * without limitation the rights to use, copy, modify, merge, publish, | |
18 | + * distribute, sublicense, and/or sell copies of the Software, and to | |
19 | + * permit persons to whom the Software is furnished to do so, subject to | |
20 | + * the following conditions: | |
21 | + * | |
22 | + * The above copyright notice and this permission notice shall be | |
23 | + * included in all copies or substantial portions of the Software. | |
24 | + * | |
25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 | + */ | |
33 | +namespace Slim\Http; | |
34 | + | |
35 | +/** | |
36 | + * Response | |
37 | + * | |
38 | + * This is a simple abstraction over top an HTTP response. This | |
39 | + * provides methods to set the HTTP status, the HTTP headers, | |
40 | + * and the HTTP body. | |
41 | + * | |
42 | + * @package Slim | |
43 | + * @author Josh Lockhart | |
44 | + * @since 1.0.0 | |
45 | + */ | |
46 | +class Response implements \ArrayAccess, \Countable, \IteratorAggregate | |
47 | +{ | |
48 | + /** | |
49 | + * @var int HTTP status code | |
50 | + */ | |
51 | + protected $status; | |
52 | + | |
53 | + /** | |
54 | + * @var \Slim\Http\Headers | |
55 | + */ | |
56 | + public $headers; | |
57 | + | |
58 | + /** | |
59 | + * @var \Slim\Http\Cookies | |
60 | + */ | |
61 | + public $cookies; | |
62 | + | |
63 | + /** | |
64 | + * @var string HTTP response body | |
65 | + */ | |
66 | + protected $body; | |
67 | + | |
68 | + /** | |
69 | + * @var int Length of HTTP response body | |
70 | + */ | |
71 | + protected $length; | |
72 | + | |
73 | + /** | |
74 | + * @var array HTTP response codes and messages | |
75 | + */ | |
76 | + protected static $messages = array( | |
77 | + //Informational 1xx | |
78 | + 100 => '100 Continue', | |
79 | + 101 => '101 Switching Protocols', | |
80 | + //Successful 2xx | |
81 | + 200 => '200 OK', | |
82 | + 201 => '201 Created', | |
83 | + 202 => '202 Accepted', | |
84 | + 203 => '203 Non-Authoritative Information', | |
85 | + 204 => '204 No Content', | |
86 | + 205 => '205 Reset Content', | |
87 | + 206 => '206 Partial Content', | |
88 | + 226 => '226 IM Used', | |
89 | + //Redirection 3xx | |
90 | + 300 => '300 Multiple Choices', | |
91 | + 301 => '301 Moved Permanently', | |
92 | + 302 => '302 Found', | |
93 | + 303 => '303 See Other', | |
94 | + 304 => '304 Not Modified', | |
95 | + 305 => '305 Use Proxy', | |
96 | + 306 => '306 (Unused)', | |
97 | + 307 => '307 Temporary Redirect', | |
98 | + //Client Error 4xx | |
99 | + 400 => '400 Bad Request', | |
100 | + 401 => '401 Unauthorized', | |
101 | + 402 => '402 Payment Required', | |
102 | + 403 => '403 Forbidden', | |
103 | + 404 => '404 Not Found', | |
104 | + 405 => '405 Method Not Allowed', | |
105 | + 406 => '406 Not Acceptable', | |
106 | + 407 => '407 Proxy Authentication Required', | |
107 | + 408 => '408 Request Timeout', | |
108 | + 409 => '409 Conflict', | |
109 | + 410 => '410 Gone', | |
110 | + 411 => '411 Length Required', | |
111 | + 412 => '412 Precondition Failed', | |
112 | + 413 => '413 Request Entity Too Large', | |
113 | + 414 => '414 Request-URI Too Long', | |
114 | + 415 => '415 Unsupported Media Type', | |
115 | + 416 => '416 Requested Range Not Satisfiable', | |
116 | + 417 => '417 Expectation Failed', | |
117 | + 418 => '418 I\'m a teapot', | |
118 | + 422 => '422 Unprocessable Entity', | |
119 | + 423 => '423 Locked', | |
120 | + 426 => '426 Upgrade Required', | |
121 | + 428 => '428 Precondition Required', | |
122 | + 429 => '429 Too Many Requests', | |
123 | + 431 => '431 Request Header Fields Too Large', | |
124 | + //Server Error 5xx | |
125 | + 500 => '500 Internal Server Error', | |
126 | + 501 => '501 Not Implemented', | |
127 | + 502 => '502 Bad Gateway', | |
128 | + 503 => '503 Service Unavailable', | |
129 | + 504 => '504 Gateway Timeout', | |
130 | + 505 => '505 HTTP Version Not Supported', | |
131 | + 506 => '506 Variant Also Negotiates', | |
132 | + 510 => '510 Not Extended', | |
133 | + 511 => '511 Network Authentication Required' | |
134 | + ); | |
135 | + | |
136 | + /** | |
137 | + * Constructor | |
138 | + * @param string $body The HTTP response body | |
139 | + * @param int $status The HTTP response status | |
140 | + * @param \Slim\Http\Headers|array $headers The HTTP response headers | |
141 | + */ | |
142 | + public function __construct($body = '', $status = 200, $headers = array()) | |
143 | + { | |
144 | + $this->setStatus($status); | |
145 | + $this->headers = new \Slim\Http\Headers(array('Content-Type' => 'text/html')); | |
146 | + $this->headers->replace($headers); | |
147 | + $this->cookies = new \Slim\Http\Cookies(); | |
148 | + $this->write($body); | |
149 | + } | |
150 | + | |
151 | + public function getStatus() | |
152 | + { | |
153 | + return $this->status; | |
154 | + } | |
155 | + | |
156 | + public function setStatus($status) | |
157 | + { | |
158 | + $this->status = (int)$status; | |
159 | + } | |
160 | + | |
161 | + /** | |
162 | + * DEPRECATION WARNING! Use `getStatus` or `setStatus` instead. | |
163 | + * | |
164 | + * Get and set status | |
165 | + * @param int|null $status | |
166 | + * @return int | |
167 | + */ | |
168 | + public function status($status = null) | |
169 | + { | |
170 | + if (!is_null($status)) { | |
171 | + $this->status = (int) $status; | |
172 | + } | |
173 | + | |
174 | + return $this->status; | |
175 | + } | |
176 | + | |
177 | + /** | |
178 | + * DEPRECATION WARNING! Access `headers` property directly. | |
179 | + * | |
180 | + * Get and set header | |
181 | + * @param string $name Header name | |
182 | + * @param string|null $value Header value | |
183 | + * @return string Header value | |
184 | + */ | |
185 | + public function header($name, $value = null) | |
186 | + { | |
187 | + if (!is_null($value)) { | |
188 | + $this->headers->set($name, $value); | |
189 | + } | |
190 | + | |
191 | + return $this->headers->get($name); | |
192 | + } | |
193 | + | |
194 | + /** | |
195 | + * DEPRECATION WARNING! Access `headers` property directly. | |
196 | + * | |
197 | + * Get headers | |
198 |