├── .gitignore ├── 404.php ├── LICENSE ├── README.md ├── login.php └── rest ├── core ├── errors.php ├── headers.php ├── methods.php ├── mimetypes.php ├── request.php ├── response.php ├── rest.php └── statuscodes.php ├── languages └── languages.php └── login └── errors.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /404.php: -------------------------------------------------------------------------------- 1 | setError(NotFound::error()); 12 | $response->render(); 13 | 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Ninjas.cl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Processwire Rest Helper 2 | A simple Rest Helper for Processwire 2.x or 3.x 3 | 4 | comment https://github.com/NinjasCL/pw-rest/blob/master/rest/core/errors.php#L38 5 | if you want to use PW 2.x 6 | 7 | ## Usage 8 | Use this code inside your `templates` directory. 9 | 10 | An example `login.php` and `404.php` template files 11 | were made for demostration. 12 | 13 | 14 | For a more complete example see 15 | https://github.com/NinjasCL/voxgram 16 | 17 | ### Note 18 | For getting params for other methods than GET OR POST, you should send the request 19 | as `application/json` object. 20 | 21 | **Example** 22 | 23 | ```json 24 | { 25 | "param1": "param1 value", 26 | "param2": "param2 value" 27 | } 28 | ``` 29 | 30 | Made with by Ninjas. 31 | 32 | -------------------------------------------------------------------------------- /login.php: -------------------------------------------------------------------------------- 1 | allowMethod(Method::POST); 25 | 26 | $params = Request::params(); 27 | 28 | if (!Request::isPost()) { 29 | 30 | $response->setError(MethodNotAllowed::error()); 31 | 32 | } else { 33 | 34 | $username = $params['username']; 35 | $password = $params['password']; 36 | 37 | if (!$username || !$password) { 38 | 39 | // Basic Auth 40 | $username = Header::username(); 41 | $password = Header::password(); 42 | } 43 | 44 | if ((!isset($username) || $username == '') || 45 | (!isset($password) || $password == '')) { 46 | 47 | $response->setError(Login\Errors\InvalidCredentials::error()); 48 | 49 | } else { 50 | 51 | if ($username == 'hello' && $password == 'world') { 52 | 53 | $response->output['data']['name'] = 'Tony'; 54 | $response->output['data']['lastname'] = 'Stark'; 55 | $response->output['data']['job'] = 'Ironman'; 56 | 57 | } else { 58 | 59 | $response->setError(Login\Errors\InvalidCredentials::error()); 60 | } 61 | } 62 | } 63 | 64 | $response->addArray(Language::current()); 65 | 66 | $headerParam = Header::get('origin'); 67 | $response->addArray(['_request_headers' => Header::getAll()]); 68 | $response->addArray(['headerParam' => $headerParam]); 69 | 70 | /* 71 | Should render 72 | { 73 | "data": { 74 | "name": "Tony", 75 | "lastname": "Stark", 76 | "job": "Ironman" 77 | }, 78 | "_language": { 79 | "id": 1017, 80 | "code": "default", 81 | "name": "English" 82 | } 83 | } 84 | */ 85 | $response->render(); -------------------------------------------------------------------------------- /rest/core/errors.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest\Errors; 26 | 27 | /** 28 | * errors.php 29 | * Generic Client Errors Created for 30 | * displaying on json requests 31 | */ 32 | 33 | include_once __DIR__ . '/mimetypes.php'; 34 | include_once __DIR__ . '/statuscodes.php'; 35 | 36 | use \Rest\MimeType as MimeType; 37 | use \Rest\StatusCode as Status; 38 | 39 | use function \Processwire\_x as _x; 40 | use function \Processwire\__ as __; 41 | 42 | 43 | 44 | interface JSONErrorInterface { 45 | public static function error(); 46 | } 47 | 48 | class JSONException extends \Exception { 49 | public $error; 50 | public $response; 51 | } 52 | 53 | class JSONError { 54 | 55 | // The error code number is the same as the status code 56 | public $responseCode; 57 | 58 | // The name is the same as the status code 59 | public $name; 60 | 61 | // Describes the reason for this error to happen 62 | public $message; 63 | 64 | // Additional info for what to do or how to solve 65 | public $info; 66 | 67 | // A Unique string identifier for this error 68 | public $uuid; 69 | 70 | // A Unique integer identifier for this error 71 | public $number; 72 | 73 | // The mime type for this error to render 74 | public $mimeType; 75 | 76 | 77 | 78 | public function __construct($_code = 500, $_message = '', $_uuid = '', $_info = '', $_number = '', $_mime = '') { 79 | 80 | $this->responseCode = $_code; 81 | $this->name = Status::nameForCode($_code); 82 | 83 | $this->message = $_message; 84 | $this->uuid = $_uuid; 85 | $this->info = $_info; 86 | $this->number = $_number; 87 | 88 | if($_mime == '') { 89 | $_mime = MimeType::json(); 90 | } 91 | 92 | $this->mimeType = $_mime; 93 | } 94 | 95 | public function output() { 96 | 97 | $output['error']['code'] = $this->responseCode; 98 | $output['error']['name'] = $this->name; 99 | $output['error']['uuid'] = $this->uuid; 100 | $output['error']['number'] = $this->number; 101 | $output['error']['message'] = $this->message; 102 | $output['error']['info'] = $this->info; 103 | 104 | return $output; 105 | } 106 | 107 | public function exception($_response = null) { 108 | 109 | $exception = new JSONException($this->message, $this->number); 110 | 111 | $exception->error = $this; 112 | $exception->response = $_response; 113 | 114 | return $exception; 115 | } 116 | 117 | } 118 | 119 | class BadRequest implements JSONErrorInterface { 120 | 121 | public static function error() { 122 | 123 | $code = Status::badRequest(); 124 | $message = _x('Generic Bad Request Message', 'Something bad was bad at the request '); 125 | $info = _x('Generic Bad Request Error', 'Additional info for knowing what happened'); 126 | $number = $code; 127 | 128 | $error = new JSONError($code, $message, 'kBadRequest', $info, $number); 129 | 130 | return $error; 131 | } 132 | 133 | } 134 | 135 | class MethodNotAllowed implements JSONErrorInterface { 136 | 137 | public static function error() { 138 | 139 | $code = Status::methodNotAllowed(); 140 | $message = _x('Generic Method Not Allowed Error Message', 'The Method used was not allowed'); 141 | $info = _x('Generic Method Not Allowed Error', 'The Method used was not allowed for some reason'); 142 | $number = $code; 143 | 144 | $error = new JSONError($code, $message, 'kMethodNotAllowed', $info, $number); 145 | 146 | return $error; 147 | } 148 | } 149 | 150 | class NotFound implements JSONErrorInterface { 151 | 152 | public static function error() { 153 | 154 | $code = Status::notFound(); 155 | $message = _x('Generic Not Found Error Message', 'The resource was not found'); 156 | $info = _x('Generic Not Found Error', 'The resource was not found for some reason'); 157 | $number = $code; 158 | 159 | $error = new JSONError($code, $message, 'kNotFound', $info, $number); 160 | 161 | return $error; 162 | } 163 | } 164 | 165 | class Unauthorized implements JSONErrorInterface { 166 | 167 | public static function error() { 168 | 169 | $code = Status::unauthorized(); 170 | $message = _x('Generic Not Found Error Message', 'The resource was not found'); 171 | $info = _x('Generic Not Found Error', 'The resource was not found for some reason'); 172 | $number = $code; 173 | 174 | $error = new JSONError($code, $message, 'kUnauthorized', $info, $number); 175 | 176 | return $error; 177 | } 178 | } 179 | 180 | class Forbidden implements JSONErrorInterface { 181 | 182 | public static function error() { 183 | 184 | $code = Status::forbidden(); 185 | $message = _x('Generic Not Found Error Message', 'The resource was not found'); 186 | $info = _x('Generic Not Found Error', 'The resource was not found for some reason'); 187 | $number = $code; 188 | 189 | $error = new JSONError($code, $message, 'kForbidden', $info, $number); 190 | 191 | return $error; 192 | } 193 | } 194 | 195 | class Conflict implements JSONErrorInterface { 196 | 197 | public static function error() { 198 | 199 | $code = Status::conflict(); 200 | $message = _x('Generic Conflict Message', 'There was a conflict'); 201 | $info = _x('Generic Conflict Error', 'The was a conflict for a reason'); 202 | $number = $code; 203 | 204 | $error = new JSONError($code, $message, 'kConflict', $info, $number); 205 | 206 | return $error; 207 | } 208 | } 209 | 210 | class NotImplemented implements JSONErrorInterface { 211 | 212 | public static function error() { 213 | 214 | $code = Status::notImplemented(); 215 | $message = _x('Generic Not Implemented Message', 'The resource is not implemented'); 216 | $info = _x('Generic Not Implemented Error', 'The resource is not implemented'); 217 | $number = $code; 218 | 219 | $error = new JSONError($code, $message, 'kNotImplemented', $info, $number); 220 | 221 | return $error; 222 | } 223 | } 224 | 225 | 226 | class InternalServerError implements JSONErrorInterface { 227 | 228 | public static function error() { 229 | 230 | $code = Status::internalServerError(); 231 | $message = _x('Generic Internal Server Message', 'Something is wrong with the server'); 232 | $info = _x('Generic Internal Server Error', 'The server does not function correctly'); 233 | $number = $code; 234 | 235 | $error = new JSONError($code, $message, 'kInternalServerError', $info, $number); 236 | 237 | return $error; 238 | } 239 | } 240 | 241 | // Special Errors 242 | class RequestContentTypeMisMatchError extends BadRequest { 243 | public static function error() { 244 | 245 | $error = parent::error(); 246 | $error->message = __('The Content Type Given in the Request is Not Valid.'); 247 | $error->info = __('The content type must be changed.'); 248 | $error->uuid = 'kRequestContentTypeMisMatchError'; 249 | $error->number = 1000; 250 | return $error; 251 | } 252 | } 253 | 254 | class InvalidCredentials extends Unauthorized { 255 | public static function error() { 256 | 257 | $error = parent::error(); 258 | $error->message = __('The credentials you gave were not valid'); 259 | $error->info = __('User or password were incorrect or wrong format of params.'); 260 | $error->uuid = 'kLoginInvalidCredentials'; 261 | $error->number = 1001; 262 | return $error; 263 | } 264 | } -------------------------------------------------------------------------------- /rest/core/headers.php: -------------------------------------------------------------------------------- 1 | 0) { 34 | $header = "Accept-Language: " . implode(',', $langs); 35 | } 36 | 37 | return $header; 38 | } 39 | 40 | private static function gmtDate($timestamp) { 41 | 42 | // Dates in Headers should have this format 43 | // http://tools.ietf.org/html/rfc2616#section-3.3 44 | // Ex 45 | // Tue, 15 Nov 1994 12:45:26 GMT 46 | 47 | $format = 'D, d M Y H:i:s T'; 48 | 49 | $date = gmdate($format); 50 | 51 | if ($timestamp && is_integer($timestamp)) { 52 | $date = gmdate($format, $timestamp); 53 | } 54 | 55 | return $date; 56 | } 57 | 58 | // Date should be a unix timestamp 59 | public static function date($timestamp) { 60 | 61 | $date = Header::gmtDate($timestamp); 62 | 63 | return "Date: $date"; 64 | } 65 | 66 | // Date should be a unix timestamp 67 | public static function lastModified($timestamp) { 68 | 69 | $date = Header::gmtDate($timestamp); 70 | 71 | return "Last-Modified: $date"; 72 | } 73 | 74 | public static function allow($_methods = []) { 75 | 76 | $validMethods = Method::allMethods(); 77 | 78 | $outputMethods = []; 79 | 80 | $header = ''; 81 | 82 | if (is_array($_methods)) { 83 | 84 | foreach ($_methods as $method) { 85 | 86 | if (is_string($method)) { 87 | 88 | $method = strtoupper($method); 89 | 90 | if (in_array($method, $validMethods)) { 91 | $outputMethods[] = $method; 92 | } 93 | } 94 | } 95 | } 96 | 97 | if (count($outputMethods) > 0) { 98 | $header = "Allow: " . implode(',', $outputMethods); 99 | } 100 | 101 | return $header; 102 | 103 | } 104 | 105 | public static function set($header) { 106 | 107 | if (is_string($header)) { 108 | header($header); 109 | } 110 | } 111 | 112 | public static function setWithNameAndValue($name, $value) { 113 | 114 | if (is_string($name) && is_string($value)) { 115 | 116 | header("$name: $value"); 117 | } 118 | 119 | } 120 | 121 | 122 | public static function getAll() { 123 | 124 | $headers = array(); 125 | 126 | foreach ($_SERVER as $key => $value) { 127 | 128 | if (strpos($key, 'HTTP_') === 0) { 129 | 130 | $key = substr($key, strlen('HTTP_')); 131 | $key = str_replace('_', ' ', $key); 132 | $key = ucwords(strtolower($key)); 133 | $key = str_replace(' ', '', $key); 134 | 135 | // Keys now have all Camel Case 136 | // LikeThis 137 | 138 | $headers[$key] = $value; 139 | } 140 | 141 | } 142 | 143 | return $headers; 144 | } 145 | 146 | public static function get($key = '') { 147 | 148 | // You should find all HTTP headers in the $_SERVER global variable prefixed 149 | // with HTTP_ uppercased and with dashes (-) replaced by underscores (_). 150 | // For instance X-Requested-With can be found in: 151 | // $_SERVER['HTTP_X_REQUESTED_WITH'] 152 | // idea from http://stackoverflow.com/questions/541430/how-do-i-read-any-request-header-in-php 153 | 154 | $result = null; 155 | 156 | if (is_string($key)) { 157 | 158 | $key = str_replace(' ', '', $key); 159 | $key = str_replace('-', '_', $key); 160 | $key = strtoupper($key); 161 | 162 | $key = "HTTP_$key"; 163 | $result = (isset($_SERVER[$key]) ? $_SERVER[$key] : null); 164 | } 165 | 166 | return $result; 167 | } 168 | 169 | // Basic HTTP AUTH 170 | public static function getBasicAuth() { 171 | 172 | $username = (isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null); 173 | $password = (isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null); 174 | 175 | if (!$username || !$password) { 176 | 177 | $auth = (isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : null); 178 | 179 | if ($auth) { 180 | 181 | $itsBasicAuth = (strpos(strtolower($auth), 'basic') === 0); 182 | 183 | if ($itsBasicAuth) { 184 | list($username, $password) = explode(':', base64_decode(substr($auth, strlen('basic:')))); 185 | } 186 | } 187 | } 188 | 189 | return ['username' => $username, 190 | 'password' => $password]; 191 | 192 | } 193 | 194 | public static function username() { 195 | 196 | $auth = Header::getBasicAuth(); 197 | 198 | return $auth['username']; 199 | } 200 | 201 | public static function password() { 202 | 203 | $auth = Header::getBasicAuth(); 204 | 205 | return $auth['password']; 206 | } 207 | } -------------------------------------------------------------------------------- /rest/core/methods.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest; 26 | 27 | class Method { 28 | 29 | const GET = "GET"; 30 | const POST = "POST"; 31 | const PUT = "PUT"; 32 | const DELETE = "DELETE"; 33 | const PATCH = "PATCH"; 34 | const OPTIONS = "OPTIONS"; 35 | const HEAD = "HEAD"; 36 | 37 | public static function allMethods() { 38 | return [Method::GET, Method::POST, Method::PUT, Method::DELETE, 39 | Method::PATCH, Method::OPTIONS, Method::HEAD]; 40 | } 41 | } -------------------------------------------------------------------------------- /rest/core/mimetypes.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest; 26 | 27 | /** 28 | * mime.types.php 29 | * Contains several mimetypes for the headers 30 | */ 31 | 32 | class MimeType { 33 | 34 | /** 35 | * A collection of mime types 36 | */ 37 | public static $mimeTypes = array( 38 | 'asc' => 'text/plain', 39 | 'au' => 'audio/basic', 40 | 'avi' => 'video/x-msvideo', 41 | 'bin' => 'application/octet-stream', 42 | 'class' => 'application/octet-stream', 43 | 'css' => 'text/css', 44 | 'csv' => 'application/vnd.ms-excel', 45 | 'doc' => 'application/msword', 46 | 'dll' => 'application/octet-stream', 47 | 'dvi' => 'application/x-dvi', 48 | 'exe' => 'application/octet-stream', 49 | 'htm' => 'text/html', 50 | 'html' => 'text/html', 51 | 'json' => 'application/json', 52 | 'js' => 'application/x-javascript', 53 | 'txt' => 'text/plain', 54 | 'bmp' => 'image/bmp', 55 | 'rss' => 'application/rss+xml', 56 | 'atom' => 'application/atom+xml', 57 | 'gif' => 'image/gif', 58 | 'jpeg' => 'image/jpeg', 59 | 'jpg' => 'image/jpeg', 60 | 'jpe' => 'image/jpeg', 61 | 'png' => 'image/png', 62 | 'ico' => 'image/vnd.microsoft.icon', 63 | 'mpeg' => 'video/mpeg', 64 | 'mpg' => 'video/mpeg', 65 | 'mpe' => 'video/mpeg', 66 | 'qt' => 'video/quicktime', 67 | 'mov' => 'video/quicktime', 68 | 'wmv' => 'video/x-ms-wmv', 69 | 'mp2' => 'audio/mpeg', 70 | 'mp3' => 'audio/mpeg', 71 | 'rm' => 'audio/x-pn-realaudio', 72 | 'ram' => 'audio/x-pn-realaudio', 73 | 'rpm' => 'audio/x-pn-realaudio-plugin', 74 | 'ra' => 'audio/x-realaudio', 75 | 'wav' => 'audio/x-wav', 76 | 'zip' => 'application/zip', 77 | 'pdf' => 'application/pdf', 78 | 'xls' => 'application/vnd.ms-excel', 79 | 'ppt' => 'application/vnd.ms-powerpoint', 80 | 'wbxml' => 'application/vnd.wap.wbxml', 81 | 'wmlc' => 'application/vnd.wap.wmlc', 82 | 'wmlsc' => 'application/vnd.wap.wmlscriptc', 83 | 'spl' => 'application/x-futuresplash', 84 | 'gtar' => 'application/x-gtar', 85 | 'gzip' => 'application/x-gzip', 86 | 'swf' => 'application/x-shockwave-flash', 87 | 'tar' => 'application/x-tar', 88 | 'xhtml' => 'application/xhtml+xml', 89 | 'snd' => 'audio/basic', 90 | 'midi' => 'audio/midi', 91 | 'mid' => 'audio/midi', 92 | 'm3u' => 'audio/x-mpegurl', 93 | 'tiff' => 'image/tiff', 94 | 'tif' => 'image/tiff', 95 | 'rtf' => 'text/rtf', 96 | 'wml' => 'text/vnd.wap.wml', 97 | 'wmls' => 'text/vnd.wap.wmlscript', 98 | 'xsl' => 'text/xml', 99 | 'xml' => 'text/xml' 100 | ); 101 | 102 | /** 103 | * Returns a mime type by name 104 | */ 105 | public static function mimeType($_mime) { 106 | $mime = MimeType::$mimeTypes[$_mime]; 107 | return $mime; 108 | } 109 | 110 | /** 111 | * Returns the json mime type 112 | */ 113 | public static function json(){ 114 | return MimeType::mimeType('json'); 115 | } 116 | } -------------------------------------------------------------------------------- /rest/core/request.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest; 26 | 27 | /** 28 | * request.php 29 | * Contains the Helper methods for a Request 30 | * Some portions of code are based on the Lime Project 31 | * https://github.com/aheinze/Lime/ 32 | * Copyright (c) 2014 Artur Heinze 33 | */ 34 | 35 | class Request 36 | { 37 | 38 | public static $types = array ( 39 | 'get', 40 | 'post', 41 | 'put', 42 | 'patch', 43 | 'delete', 44 | 'head', 45 | 'options', 46 | 'ajax', 47 | 'mobile', 48 | 'ssl' 49 | ); 50 | 51 | /** 52 | * Request helper function 53 | * @param String $type 54 | * @return Boolean 55 | */ 56 | public static function is($type) 57 | { 58 | 59 | $method = (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : ''); 60 | $method = strtolower($method); 61 | 62 | switch(strtolower($type)) 63 | { 64 | 65 | case 'ajax': 66 | return ( 67 | (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 68 | ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) || 69 | 70 | (isset($_SERVER["CONTENT_TYPE"]) && 71 | stripos($_SERVER["CONTENT_TYPE"],'application/json')!==false) || 72 | 73 | (isset($_SERVER["HTTP_CONTENT_TYPE"]) && 74 | stripos($_SERVER["HTTP_CONTENT_TYPE"], 'application/json') !== false) 75 | ); 76 | break; 77 | 78 | case 'mobile': 79 | $mobileDevices = array( 80 | "midp","240x320","blackberry","netfront","nokia","panasonic", 81 | "portalmmm","sharp","sie-","sonyericsson", 82 | "symbian","windows ce","benq","mda","mot-","opera mini", 83 | "philips","pocket pc","sagem","samsung", 84 | "sda","sgh-","vodafone","xda","iphone", "ipod", "ipad", "android" 85 | ); 86 | 87 | return preg_match('/(' . implode('|', $mobileDevices). ')/i', 88 | strtolower($_SERVER['HTTP_USER_AGENT']) 89 | ); 90 | break; 91 | 92 | case 'post': 93 | return ($method == 'post'); 94 | break; 95 | 96 | case 'get': 97 | return ($method == 'get'); 98 | break; 99 | 100 | case 'put': 101 | return ($method == 'put'); 102 | break; 103 | 104 | case 'options': 105 | return ($method == 'options'); 106 | break; 107 | 108 | case 'patch': 109 | return ($method == 'patch'); 110 | break; 111 | 112 | case 'delete': 113 | return ($method == 'delete'); 114 | break; 115 | 116 | case 'head': 117 | return ($method == 'head'); 118 | break; 119 | 120 | case 'ssl': 121 | return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'); 122 | break; 123 | } 124 | 125 | return false; 126 | } 127 | 128 | public static function isAjax() 129 | { 130 | return self::is('ajax'); 131 | } 132 | 133 | public static function isMobile() 134 | { 135 | return self::is('mobile'); 136 | } 137 | 138 | public static function isPost() 139 | { 140 | return self::is('post'); 141 | } 142 | 143 | public static function isGet() 144 | { 145 | return self::is('get'); 146 | } 147 | 148 | public static function isPut() 149 | { 150 | return self::is('put'); 151 | } 152 | 153 | public static function isPatch() 154 | { 155 | return self::is('patch'); 156 | } 157 | 158 | public static function isDelete() 159 | { 160 | return self::is('delete'); 161 | } 162 | 163 | public static function isHead() 164 | { 165 | return self::is('head'); 166 | } 167 | 168 | public static function isOptions() 169 | { 170 | return self::is('options'); 171 | } 172 | 173 | public static function isSSL() 174 | { 175 | return self::is('ssl'); 176 | } 177 | 178 | /** 179 | * Get current request method 180 | * @return String 181 | */ 182 | public static function currentMethod() 183 | { 184 | 185 | $currentType = null; 186 | 187 | foreach(self::$types as $type) 188 | { 189 | 190 | if(self::is($type)) 191 | { 192 | $currentType = strtoupper($type); 193 | break; 194 | } 195 | 196 | } 197 | 198 | return $currentType; 199 | } 200 | 201 | public static function contentType() 202 | { 203 | 204 | $contentType = ''; 205 | 206 | if (array_key_exists('CONTENT_TYPE', $_SERVER) && 207 | isset($_SERVER["CONTENT_TYPE"])) 208 | { 209 | 210 | $contentType = $_SERVER["CONTENT_TYPE"]; 211 | 212 | } 213 | else 214 | { 215 | 216 | if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER) && 217 | isset($_SERVER["HTTP_CONTENT_TYPE"])) 218 | { 219 | $contentType = $_SERVER['HTTP_CONTENT_TYPE']; 220 | } 221 | } 222 | 223 | return $contentType; 224 | } 225 | 226 | /** 227 | * Get request variables 228 | * @param String $index 229 | * @param Misc $default 230 | * @param Array $source 231 | * @return Misc 232 | */ 233 | public static function params($index=null, $default = null, $source = null) 234 | { 235 | 236 | // check for php://input and merge with $_REQUEST 237 | 238 | $contentType = self::contentType(); 239 | 240 | if(stripos($contentType, 'application/json') !== false) 241 | { 242 | if ($json = json_decode(@file_get_contents('php://input'), true)) 243 | { 244 | $_REQUEST = array_merge($_REQUEST, $json); 245 | } 246 | } 247 | 248 | $src = $source ? $source : $_REQUEST; 249 | 250 | return self::fetch_from_array($src, $index, $default); 251 | } 252 | 253 | /** 254 | * Same as params. Just sintax sugar. 255 | */ 256 | public static function inputs($index=null, $default = null, $source = null) 257 | { 258 | return self::params($index=null, $default = null, $source = null); 259 | } 260 | 261 | /** 262 | * Enables getting easily the params. 263 | * Also if you got the params previously you can use that array. 264 | * If not it fetches the current params with Request::params(); 265 | * 266 | * Example 267 | * Request::getParam('username'); 268 | */ 269 | public static function getParam($_name, $_defaultValue = null, $_params = null) 270 | { 271 | 272 | $params = $_params; 273 | 274 | if (!is_array($_params) || $_params == null ) 275 | { 276 | $params = self::params(); 277 | } 278 | 279 | if (!is_string($_name)) 280 | { 281 | $_name = ""; 282 | } 283 | 284 | return (array_key_exists($_name, $params) ? $params[$_name] : $_defaultValue); 285 | } 286 | 287 | /** 288 | * Same as getParam. Just sintax sugar. 289 | */ 290 | public static function input($_name, $_defaultValue = null, $_params = null) 291 | { 292 | return self::getParam($_name, $_defaultValue = null, $_params = null); 293 | } 294 | 295 | private static function fetch_from_array(&$array, $index=null, $default = null) { 296 | 297 | if (is_null($index)) 298 | { 299 | 300 | return $array; 301 | 302 | } 303 | elseif (isset($array[$index])) 304 | { 305 | 306 | return $array[$index]; 307 | 308 | } 309 | elseif (strpos($index, '/')) 310 | { 311 | 312 | $keys = explode('/', $index); 313 | 314 | switch(count($keys)) 315 | { 316 | 317 | case 1: 318 | if (isset($array[$keys[0]])) 319 | { 320 | return $array[$keys[0]]; 321 | } 322 | break; 323 | case 2: 324 | if (isset($array[$keys[0]][$keys[1]])) 325 | { 326 | return $array[$keys[0]][$keys[1]]; 327 | } 328 | break; 329 | case 3: 330 | if (isset($array[$keys[0]][$keys[1]][$keys[2]])) 331 | { 332 | return $array[$keys[0]][$keys[1]][$keys[2]]; 333 | } 334 | break; 335 | case 4: 336 | if (isset($array[$keys[0]][$keys[1]][$keys[2]][$keys[3]])) 337 | { 338 | return $array[$keys[0]][$keys[1]][$keys[2]][$keys[3]]; 339 | } 340 | break; 341 | } 342 | } 343 | 344 | return $default; 345 | } 346 | } 347 | -------------------------------------------------------------------------------- /rest/core/response.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest; 26 | 27 | /** 28 | * Helps with the Response 29 | */ 30 | 31 | include_once __DIR__ . '/mimetypes.php'; 32 | include_once __DIR__ . '/headers.php'; 33 | include_once __DIR__ . '/methods.php'; 34 | include_once __DIR__ . '/request.php'; 35 | include_once __DIR__ . '/errors.php'; 36 | 37 | use \Rest\MimeType as MimeType; 38 | use \Rest\Header as Header; 39 | use \Rest\Method as Method; 40 | use \Rest\Request as Request; 41 | use \Rest\Errors\JSONError as JSONError; 42 | use \Rest\Errors\MethodNotAllowed as MethodNotAllowed; 43 | use \Rest\Errors\BadRequest as BadRequest; 44 | use \Rest\Errors\RequestContentTypeMisMatchError as RequestContentTypeMisMatchError; 45 | 46 | class Response { 47 | 48 | public $mimeType; 49 | 50 | public $code; 51 | 52 | public $headers; 53 | 54 | public $output; 55 | 56 | public $meta; 57 | 58 | public $data; 59 | 60 | public $error; 61 | 62 | private $gotError; 63 | 64 | private $allowMethodsHeader; 65 | 66 | private $clearHeaders; 67 | 68 | private $methods; 69 | 70 | public function __construct($_output = [], $_mimeType = '', $_responseCode = 200, $_headers = []) { 71 | 72 | $this->output = $_output; 73 | 74 | 75 | if($_mimeType == '') { 76 | $_mimeType = MimeType::json(); 77 | } 78 | 79 | $this->mimeType = $_mimeType; 80 | $this->code = $_responseCode; 81 | 82 | $this->headers = $_headers; 83 | 84 | $this->headers[] = Header::contentType($_mimeType); 85 | 86 | $this->gotError = false; 87 | $this->error = null; 88 | 89 | $this->clearHeaders = true; 90 | 91 | $this->meta = null; 92 | 93 | $this->data = null; 94 | 95 | $this->methods = []; 96 | } 97 | 98 | // Calls header_remove() on render if true 99 | public function clearHeaders($_clearHeaders = true) { 100 | $this->clearHeaders = $_clearHeaders; 101 | } 102 | 103 | public function addArray($_data) { 104 | 105 | $this->output = array_merge($this->output, $_data); 106 | } 107 | 108 | public function setError($_error) { 109 | 110 | $this->error = $_error; 111 | 112 | $this->gotError = true; 113 | } 114 | 115 | public function allowMethod($_method) { 116 | 117 | if (is_string($_method)) { 118 | self::allowMethods([$_method]); 119 | } 120 | } 121 | 122 | public function allowMethods($_methods = []) { 123 | 124 | if (is_array($_methods)) { 125 | $this->allowMethodsHeader = Header::allow($_methods); 126 | $this->methods = $_methods; 127 | } 128 | } 129 | 130 | // Render Methods 131 | 132 | public function render() { 133 | 134 | if ($this->clearHeaders) { 135 | Header::removeAllHeaders(); 136 | } 137 | 138 | if ($this->gotError) { 139 | 140 | $this->output = $this->error->output(); 141 | 142 | $this->mimeType = $this->error->mimeType; 143 | 144 | $this->code = $this->error->responseCode; 145 | 146 | $this->headers = []; 147 | 148 | $this->headers[] = Header::contentType($this->error->mimeType); 149 | } 150 | 151 | if ($this->allowMethodsHeader) { 152 | $this->headers[] = $this->allowMethodsHeader; 153 | } 154 | 155 | foreach($this->headers as $header) { 156 | Header::set($header); 157 | } 158 | 159 | if (isset($this->meta)) { 160 | $this->output['_meta'] = $this->meta; 161 | } 162 | 163 | if (isset($this->data)) { 164 | $this->output['data'] = $this->data; 165 | } 166 | 167 | http_response_code($this->code); 168 | 169 | echo json_encode($this->output); 170 | } 171 | 172 | /** 173 | * Renders the Error as JSON and Exit. 174 | * if $_throwException is true the error is not rendered. 175 | * instead triggers an exception (\Rest\Errors\JSONException) so you can 176 | * render the error yourself in a try catch block. 177 | */ 178 | public function renderErrorAndExit($_error, $_throwException = false) { 179 | 180 | 181 | if (get_class($_error) == 'Rest\Errors\JSONError') { 182 | 183 | $this->setError($_error); 184 | 185 | if ($_throwException === true) { 186 | 187 | throw $_error->exception($this); 188 | 189 | } else { 190 | 191 | $this->render(); 192 | 193 | exit(1); 194 | } 195 | } 196 | 197 | throw new \Exception("Not Supposed to reach here. You must pass a subclass of 'Rest\Errors\JSONError' class given " . get_class($_error), 1); 198 | } 199 | 200 | public function renderAndExit() { 201 | $this->render(); 202 | exit(0); 203 | } 204 | 205 | public function renderErrorAndExitUnlessTheseMethodsAreUsed($_methods = [], $_throwException = false) { 206 | 207 | if (!isset($_methods)) { 208 | $_methods = [Method::GET]; 209 | } 210 | 211 | if (!is_array($_methods)) { 212 | $_methods = [$_methods]; 213 | } 214 | 215 | $this->allowMethods($_methods); 216 | 217 | $currentRequestMethod = Request::currentMethod(); 218 | 219 | if(!in_array($currentRequestMethod, $this->methods)) { 220 | 221 | $this->meta['method_used'] = $currentRequestMethod; 222 | 223 | $this->meta['allowed_methods'] = $this->methods; 224 | 225 | $this->renderErrorAndExit(MethodNotAllowed::error(), $_throwException); 226 | } 227 | } 228 | 229 | public function renderErrorAndExitIfTheseParamsAreNotFound($_params = [], $_error = null, $_throwException = false) { 230 | 231 | if ($_error == null || !(get_class($_error) == 'Rest\Errors\JSONError')) { 232 | $_error = BadRequest::error(); 233 | } 234 | 235 | if (is_array($_params)) { 236 | 237 | foreach ($_params as $key => $value) { 238 | 239 | if (!isset($value) || $value == '') { 240 | 241 | $this->meta['required'] = array_keys($_params); 242 | $this->meta['params'] = $_params; 243 | $this->meta['missing'] = $key; 244 | 245 | $this->renderErrorAndExit($_error, $_throwException); 246 | break; 247 | } 248 | } 249 | } 250 | } 251 | 252 | public function renderErrorAndExitUnlessThisContentTypeIsUsed($_contentType, $_throwException = false) { 253 | 254 | if (Request::contentType() != $_contentType) { 255 | 256 | $this->meta['contentType'] = Request::contentType(); 257 | $this->meta['valid'] = $_contentType; 258 | 259 | $this->renderErrorAndExit(RequestContentTypeMisMatchError::error(), $_throwException); 260 | } 261 | } 262 | } -------------------------------------------------------------------------------- /rest/core/rest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest; 26 | 27 | /** 28 | * A Simple loader class for using the rest core 29 | */ 30 | 31 | include_once __DIR__ . '/errors.php'; 32 | include_once __DIR__ . '/mimetypes.php'; 33 | include_once __DIR__ . '/request.php'; 34 | include_once __DIR__ . '/response.php'; 35 | include_once __DIR__ . '/statuscodes.php'; 36 | include_once __DIR__ . '/methods.php'; 37 | include_once __DIR__ . '/headers.php'; 38 | 39 | use Rest\Errors; 40 | use Rest\MimeType; 41 | use Rest\StatusCode; 42 | use Rest\Request; 43 | use Rest\Response; 44 | use Rest\Method; 45 | use Rest\Header; 46 | -------------------------------------------------------------------------------- /rest/core/statuscodes.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is furnished 12 | * to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | namespace Rest; 26 | 27 | /** 28 | * status.codes.php 29 | * Contains the Status Codes for a Response 30 | */ 31 | 32 | class StatusCode { 33 | 34 | public static $statusCodes = array ( 35 | // Informational 1xx 36 | 100 => 'Continue', 37 | 101 => 'Switching Protocols', 38 | // Successful 2xx 39 | 200 => 'OK', 40 | 201 => 'Created', 41 | 202 => 'Accepted', 42 | 203 => 'Non-Authoritative Information', 43 | 204 => 'No Content', 44 | 205 => 'Reset Content', 45 | 206 => 'Partial Content', 46 | // Redirection 3xx 47 | 300 => 'Multiple Choices', 48 | 301 => 'Moved Permanently', 49 | 302 => 'Found', 50 | 303 => 'See Other', 51 | 304 => 'Not Modified', 52 | 305 => 'Use Proxy', 53 | 307 => 'Temporary Redirect', 54 | // Client Error 4xx 55 | 400 => 'Bad Request', 56 | 401 => 'Unauthorized', 57 | 402 => 'Payment Required', 58 | 403 => 'Forbidden', 59 | 404 => 'Not Found', 60 | 405 => 'Method Not Allowed', 61 | 406 => 'Not Acceptable', 62 | 407 => 'Proxy Authentication Required', 63 | 408 => 'Request Timeout', 64 | 409 => 'Conflict', 65 | 410 => 'Gone', 66 | 411 => 'Length Required', 67 | 412 => 'Precondition Failed', 68 | 413 => 'Request Entity Too Large', 69 | 414 => 'Request-URI Too Long', 70 | 415 => 'Unsupported Media Type', 71 | 416 => 'Request Range Not Satisfiable', 72 | 417 => 'Expectation Failed', 73 | // Server Error 5xx 74 | 500 => 'Internal Server Error', 75 | 501 => 'Not Implemented', 76 | 502 => 'Bad Gateway', 77 | 503 => 'Service Unavailable', 78 | 504 => 'Gateway Timeout', 79 | 505 => 'HTTP Version Not Supported' 80 | ); 81 | 82 | /** 83 | * Returns a status code name 84 | */ 85 | public static function nameForCode($_code) { 86 | $name = StatusCode::$statusCodes[$_code]; 87 | return $name; 88 | } 89 | 90 | /** 91 | * Returns a 200 OK Status 92 | */ 93 | public static function ok() { 94 | return 200; 95 | } 96 | 97 | /** 98 | * Returns a 201 Created Status 99 | */ 100 | public static function created() { 101 | return 201; 102 | } 103 | 104 | /** 105 | * Returns a 202 Accepted Status 106 | */ 107 | public static function accepted() { 108 | return 202; 109 | } 110 | 111 | /** 112 | * Returns a 400 Bad Request Status 113 | */ 114 | public static function badRequest() { 115 | return 400; 116 | } 117 | 118 | /** 119 | * Returns a 401 Unauthorized Status 120 | */ 121 | public static function unauthorized() { 122 | return 401; 123 | } 124 | 125 | /** 126 | * Returns a 403 Forbidden Status 127 | */ 128 | public static function forbidden() { 129 | return 403; 130 | } 131 | 132 | /** 133 | * Returns a 404 Not Found Status 134 | */ 135 | public static function notFound() { 136 | return 404; 137 | } 138 | 139 | /** 140 | * Returns a 405 Method Not Allowed Status 141 | */ 142 | public static function methodNotAllowed() { 143 | return 405; 144 | } 145 | 146 | /** 147 | * Returns a 409 Conflict Status 148 | */ 149 | public static function conflict() { 150 | return 409; 151 | } 152 | 153 | /** 154 | * Returns a 500 Internal Server Error Status 155 | */ 156 | public static function internalServerError() { 157 | return 500; 158 | } 159 | 160 | /** 161 | * Returns a 501 Not Implemented Status 162 | */ 163 | public static function notImplemented() { 164 | return 501; 165 | } 166 | } -------------------------------------------------------------------------------- /rest/languages/languages.php: -------------------------------------------------------------------------------- 1 | language; 12 | 13 | $output['_language']['id'] = $language->id; 14 | $output['_language']['code'] = $language->name; 15 | $output['_language']['name'] = $language->title; 16 | 17 | return $output; 18 | } 19 | } -------------------------------------------------------------------------------- /rest/login/errors.php: -------------------------------------------------------------------------------- 1 | message = __('The credentials you gave were not valid'); 19 | $error->info = __('User or password were incorrect or wrong format of params.'); 20 | $error->uuid = 'kLoginInvalidCredentials'; 21 | $error->number = 1000; 22 | 23 | return $error; 24 | } 25 | } --------------------------------------------------------------------------------