/ext-4.0.7/examples/restful/remote/lib/request.php

https://bitbucket.org/srogerf/javascript · PHP · 79 lines · 68 code · 6 blank · 5 comment · 20 complexity · 3ae38692a64164af68d5e42ef5267cfd MD5 · raw file

  1. <?php
  2. /**
  3. * @class Request
  4. */
  5. class Request {
  6. public $restful, $method, $controller, $action, $id, $params;
  7. public function __construct($params) {
  8. $this->restful = (isset($params["restful"])) ? $params["restful"] : false;
  9. $this->method = $_SERVER["REQUEST_METHOD"];
  10. $this->parseRequest();
  11. }
  12. public function isRestful() {
  13. return $this->restful;
  14. }
  15. protected function parseRequest() {
  16. if ($this->method == 'PUT') { // <-- Have to jump through hoops to get PUT data
  17. $raw = '';
  18. $httpContent = fopen('php://input', 'r');
  19. while ($kb = fread($httpContent, 1024)) {
  20. $raw .= $kb;
  21. }
  22. fclose($httpContent);
  23. $params = array();
  24. parse_str($raw, $params);
  25. if (isset($params['data'])) {
  26. $this->params = json_decode(stripslashes($params['data']));
  27. } else {
  28. $params = json_decode(stripslashes($raw));
  29. $this->params = $params;
  30. }
  31. } else {
  32. // grab JSON data if there...
  33. $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;
  34. if (isset($_REQUEST['data'])) {
  35. $this->params = json_decode(stripslashes($_REQUEST['data']));
  36. } else {
  37. $raw = '';
  38. $httpContent = fopen('php://input', 'r');
  39. while ($kb = fread($httpContent, 1024)) {
  40. $raw .= $kb;
  41. }
  42. $params = json_decode(stripslashes($raw));
  43. if ($params) {
  44. $this->params = $params;
  45. }
  46. }
  47. }
  48. // Quickndirty PATH_INFO parser
  49. if (isset($_SERVER["PATH_INFO"])){
  50. $cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/'; // /controller/action/id
  51. $ca = '/^\/([a-z]+\w)\/([a-z]+)$/'; // /controller/action
  52. $ci = '/^\/([a-z]+\w)\/([0-9]+)$/'; // /controller/id
  53. $c = '/^\/([a-z]+\w)$/'; // /controller
  54. $i = '/^\/([0-9]+)$/'; // /id
  55. $matches = array();
  56. if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
  57. $this->controller = $matches[1];
  58. $this->action = $matches[2];
  59. $this->id = $matches[3];
  60. } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
  61. $this->controller = $matches[1];
  62. $this->action = $matches[2];
  63. } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
  64. $this->controller = $matches[1];
  65. $this->id = $matches[2];
  66. } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
  67. $this->controller = $matches[1];
  68. } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
  69. $this->id = $matches[1];
  70. }
  71. }
  72. }
  73. }