bug38450_2.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. --TEST--
  2. Bug #38450 (constructor is not called for classes used in userspace stream wrappers)
  3. --FILE--
  4. <?php
  5. class VariableStream {
  6. var $position;
  7. var $varname;
  8. function __construct($var = null) {
  9. throw new Exception("constructor");
  10. }
  11. function stream_open($path, $mode, $options, &$opened_path)
  12. {
  13. $url = parse_url($path);
  14. $this->varname = $url["host"];
  15. $this->position = 0;
  16. return true;
  17. }
  18. function stream_read($count)
  19. {
  20. $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  21. $this->position += strlen($ret);
  22. return $ret;
  23. }
  24. function stream_write($data)
  25. {
  26. $left = substr($GLOBALS[$this->varname], 0, $this->position);
  27. $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
  28. $GLOBALS[$this->varname] = $left . $data . $right;
  29. $this->position += strlen($data);
  30. return strlen($data);
  31. }
  32. function stream_tell()
  33. {
  34. return $this->position;
  35. }
  36. function stream_eof()
  37. {
  38. return $this->position >= strlen($GLOBALS[$this->varname]);
  39. }
  40. function stream_seek($offset, $whence)
  41. {
  42. switch ($whence) {
  43. case SEEK_SET:
  44. if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
  45. $this->position = $offset;
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. break;
  51. case SEEK_CUR:
  52. if ($offset >= 0) {
  53. $this->position += $offset;
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. break;
  59. case SEEK_END:
  60. if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
  61. $this->position = strlen($GLOBALS[$this->varname]) + $offset;
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. break;
  67. default:
  68. return false;
  69. }
  70. }
  71. }
  72. stream_wrapper_register("var", "VariableStream")
  73. or die("Failed to register protocol");
  74. $myvar = "";
  75. $fp = fopen("var://myvar", "r+");
  76. fwrite($fp, "line1\n");
  77. fwrite($fp, "line2\n");
  78. fwrite($fp, "line3\n");
  79. rewind($fp);
  80. while (!feof($fp)) {
  81. echo fgets($fp);
  82. }
  83. fclose($fp);
  84. var_dump($myvar);
  85. echo "Done\n";
  86. ?>
  87. --EXPECTF--
  88. Fatal error: Uncaught Exception: constructor in %s:%d
  89. Stack trace:
  90. #0 [internal function]: VariableStream->__construct()
  91. #1 %s(%d): fopen('var://myvar', 'r+')
  92. #2 {main}
  93. thrown in %s on line %d