requeststream.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copy of http://dk1.php.net/manual/en/stream.streamwrapper.example-1.php
  4. * Used to simulate php://input for Request tests
  5. */
  6. class RequestStream {
  7. protected $position;
  8. protected $varname;
  9. function stream_open($path, $mode, $options, &$opened_path) {
  10. $url = parse_url($path);
  11. $this->varname = $url["host"];
  12. $this->position = 0;
  13. return true;
  14. }
  15. function stream_read($count) {
  16. $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  17. $this->position += strlen($ret);
  18. return $ret;
  19. }
  20. function stream_write($data) {
  21. $left = substr($GLOBALS[$this->varname], 0, $this->position);
  22. $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
  23. $GLOBALS[$this->varname] = $left . $data . $right;
  24. $this->position += strlen($data);
  25. return strlen($data);
  26. }
  27. function stream_tell() {
  28. return $this->position;
  29. }
  30. function stream_eof() {
  31. return $this->position >= strlen($GLOBALS[$this->varname]);
  32. }
  33. function stream_seek($offset, $whence) {
  34. switch ($whence) {
  35. case SEEK_SET:
  36. if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
  37. $this->position = $offset;
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. break;
  43. case SEEK_CUR:
  44. if ($offset >= 0) {
  45. $this->position += $offset;
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. break;
  51. case SEEK_END:
  52. if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
  53. $this->position = strlen($GLOBALS[$this->varname]) + $offset;
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. break;
  59. default:
  60. return false;
  61. }
  62. }
  63. public function stream_stat() {
  64. $size = strlen($GLOBALS[$this->varname]);
  65. $time = time();
  66. $data = array(
  67. 'dev' => 0,
  68. 'ino' => 0,
  69. 'mode' => 0777,
  70. 'nlink' => 1,
  71. 'uid' => 0,
  72. 'gid' => 0,
  73. 'rdev' => '',
  74. 'size' => $size,
  75. 'atime' => $time,
  76. 'mtime' => $time,
  77. 'ctime' => $time,
  78. 'blksize' => -1,
  79. 'blocks' => -1,
  80. );
  81. return array_values($data) + $data;
  82. //return false;
  83. }
  84. function stream_metadata($path, $option, $var) {
  85. if($option == STREAM_META_TOUCH) {
  86. $url = parse_url($path);
  87. $varname = $url["host"];
  88. if(!isset($GLOBALS[$varname])) {
  89. $GLOBALS[$varname] = '';
  90. }
  91. return true;
  92. }
  93. return false;
  94. }
  95. }