|
| 1 | +<?php |
| 2 | + |
| 3 | +function serve_json($doc, $options = 0) { |
| 4 | + set_content_type('application/json'); |
| 5 | + $doc = json_encode($doc, $options); |
| 6 | + if (array_key_exists('HTTP_JSONP_CALLBACK', $_SERVER) && strlen($_SERVER['HTTP_JSONP_CALLBACK'])>0) { |
| 7 | + $callback = $_SERVER['HTTP_JSONP_CALLBACK']; |
| 8 | + printf("%s(\"%s\");\n", $callback, addslashes($doc)); |
| 9 | + } else { |
| 10 | + echo $doc; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function get_request_uri() { |
| 15 | + return $_SERVER['REDIRECT_URL']; |
| 16 | +} |
| 17 | + |
| 18 | +function request_method_matches($methods) { |
| 19 | + return in_array($_SERVER['REQUEST_METHOD'], $methods); |
| 20 | +} |
| 21 | + |
| 22 | +function route($regexp, $methods, $callback) { |
| 23 | + |
| 24 | + /* Sanitize regexp */ |
| 25 | + if (!preg_match('/^\^(.)+$/', $regexp)) { |
| 26 | + $regexp = sprintf("^%s", $regexp); |
| 27 | + } |
| 28 | + if (!preg_match('/^(.)+\$$/', $regexp)) { |
| 29 | + $regexp = sprintf("%s$", $regexp); |
| 30 | + } |
| 31 | + $regexp = str_replace("/", "\/", $regexp); |
| 32 | + $regexp = sprintf("/%s/", $regexp); |
| 33 | + |
| 34 | + /* Create array of accepted HTTP methods */ |
| 35 | + $methods = split(",", $methods) ; |
| 36 | + foreach($methods as $k=>$v) { |
| 37 | + $methods[$k] = trim(strtoupper($v)); |
| 38 | + } |
| 39 | + |
| 40 | + /* Match */ |
| 41 | + $uri = get_request_uri(); |
| 42 | + if (request_method_matches($methods) && preg_match($regexp, $uri, $params)) { |
| 43 | + $callback($params); |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +function status_code_map() { |
| 49 | + return array( |
| 50 | + 200 => 'OK', |
| 51 | + 201 => 'Created', |
| 52 | + 202 => 'Accepted', |
| 53 | + 203 => 'Non', |
| 54 | + 204 => 'No Content', |
| 55 | + 205 => 'Reset Content', |
| 56 | + 206 => 'Partial Content', |
| 57 | + 300 => 'Multiple Choices', |
| 58 | + 301 => 'Moved Permanently', |
| 59 | + 302 => 'Found', |
| 60 | + 303 => 'See Other', |
| 61 | + 304 => 'Not Modified', |
| 62 | + 305 => 'Use Proxy', |
| 63 | + 307 => 'Temporary Redirect', |
| 64 | + 400 => 'Bad Request', |
| 65 | + 401 => 'Unauthorized', |
| 66 | + 402 => 'Payment Required', |
| 67 | + 403 => 'Forbidden', |
| 68 | + 404 => 'Not Found', |
| 69 | + 405 => 'Method Not Allowed', |
| 70 | + 406 => 'Not Acceptable', |
| 71 | + 407 => 'Proxy Authentication Required', |
| 72 | + 408 => 'Request Timeout', |
| 73 | + 409 => 'Conflict', |
| 74 | + 410 => 'Gone', |
| 75 | + 411 => 'Length Required', |
| 76 | + 412 => 'Precondition Failed', |
| 77 | + 413 => 'Request Entity Too Large', |
| 78 | + 414 => 'Request', |
| 79 | + 415 => 'Unsupported Media Type', |
| 80 | + 416 => 'Requested Range Not Satisfiable', |
| 81 | + 417 => 'Expectation Failed', |
| 82 | + 500 => 'Internal Server Error', |
| 83 | + 501 => 'Not Implemented', |
| 84 | + 502 => 'Bad Gateway', |
| 85 | + 503 => 'Service Unavailable', |
| 86 | + 504 => 'Gateway Timeout', |
| 87 | + 505 => 'HTTP Version Not Supported', |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +function set_content_type($content_type) { |
| 92 | + header("Content-Type: $content_type"); |
| 93 | +} |
| 94 | + |
| 95 | +function set_status_code($status_code) { |
| 96 | + $map = status_code_map(); |
| 97 | + header(sprintf("HTTP/1.1 %d %s", $status_code, strtoupper($map[$status_code]))); |
| 98 | +} |
| 99 | + |
| 100 | +function all_verbs_array() { |
| 101 | + return array( |
| 102 | + "OPTIONS", |
| 103 | + "GET", |
| 104 | + "HEAD", |
| 105 | + "POST", |
| 106 | + "PUT", |
| 107 | + "DELETE", |
| 108 | + "TRACE", |
| 109 | + "CONNECT" |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +function all_verbs() { |
| 114 | + return implode(", ", all_verbs_array()); |
| 115 | +} |
| 116 | + |
| 117 | +?> |
0 commit comments