Commit 9dbbb4dd authored by alexpott's avatar alexpott

Issue #1954180 by fietserwin, jthorson: LocalReadOnlyStream::stream_open()...

Issue #1954180 by fietserwin, jthorson: LocalReadOnlyStream::stream_open() refuses to open a file when 'rb' flag is passed in.
parent e5b7fb4b
...@@ -28,19 +28,20 @@ abstract class LocalReadOnlyStream extends LocalStream { ...@@ -28,19 +28,20 @@ abstract class LocalReadOnlyStream extends LocalStream {
* @param string $uri * @param string $uri
* A string containing the URI to the file to open. * A string containing the URI to the file to open.
* @param int $mode * @param int $mode
* The file mode (only "r" is supported for the read-only stream wrapper.) * The file mode, only strict readonly modes are supported.
* @param int $options * @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS. * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path * @param string $opened_path
* A string containing the path actually opened. * A string containing the path actually opened.
* *
* @return bool * @return bool
* Returns TRUE if $mode == "r" and the file was opened successfully. * TRUE if $mode denotes a readonly mode and the file was opened
* successfully, FALSE otherwise.
* *
* @see http://php.net/manual/streamwrapper.stream-open.php * @see http://php.net/manual/streamwrapper.stream-open.php
*/ */
public function stream_open($uri, $mode, $options, &$opened_path) { public function stream_open($uri, $mode, $options, &$opened_path) {
if ($mode != "r") { if (!in_array($mode, array('r', 'rb', 'rt'))) {
if ($options & STREAM_REPORT_ERRORS) { if ($options & STREAM_REPORT_ERRORS) {
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING); trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
} }
...@@ -114,6 +115,8 @@ public function stream_write($data) { ...@@ -114,6 +115,8 @@ public function stream_write($data) {
* Support for fflush(). * Support for fflush().
* *
* Nothing will be output to the file, as this is a read-only stream wrapper. * Nothing will be output to the file, as this is a read-only stream wrapper.
* However as stream_flush is called during stream_close we should not trigger
* an error.
* *
* @return bool * @return bool
* FALSE, as no data will be stored. * FALSE, as no data will be stored.
...@@ -121,7 +124,6 @@ public function stream_write($data) { ...@@ -121,7 +124,6 @@ public function stream_write($data) {
* @see http://php.net/manual/streamwrapper.stream-flush.php * @see http://php.net/manual/streamwrapper.stream-flush.php
*/ */
public function stream_flush() { public function stream_flush() {
trigger_error('stream_flush() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE; return FALSE;
} }
......
...@@ -63,19 +63,20 @@ function getUri() { ...@@ -63,19 +63,20 @@ function getUri() {
* @param string $uri * @param string $uri
* A string containing the URI to the file to open. * A string containing the URI to the file to open.
* @param int $mode * @param int $mode
* The file mode (only "r" is supported for the read-only stream wrapper.) * The file mode, only strict readonly modes are supported.
* @param int $options * @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS. * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path * @param string $opened_path
* A string containing the path actually opened. * A string containing the path actually opened.
* *
* @return bool * @return bool
* Returns TRUE if $mode == "r" and the file was opened successfully. * TRUE if $mode denotes a readonly mode and the file was opened
* successfully, FALSE otherwise.
* *
* @see http://php.net/manual/streamwrapper.stream-open.php * @see http://php.net/manual/streamwrapper.stream-open.php
*/ */
public function stream_open($uri, $mode, $options, &$opened_path) { public function stream_open($uri, $mode, $options, &$opened_path) {
if ($mode != "r") { if (!in_array($mode, array('r', 'rb', 'rt'))) {
if ($options & STREAM_REPORT_ERRORS) { if ($options & STREAM_REPORT_ERRORS) {
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING); trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
} }
...@@ -148,6 +149,8 @@ public function stream_write($data) { ...@@ -148,6 +149,8 @@ public function stream_write($data) {
* Support for fflush(). * Support for fflush().
* *
* Nothing will be output to the file, as this is a read-only stream wrapper. * Nothing will be output to the file, as this is a read-only stream wrapper.
* However as stream_flush is called during stream_close we should not trigger
* an error.
* *
* @return bool * @return bool
* FALSE, as no data will be stored. * FALSE, as no data will be stored.
...@@ -155,7 +158,6 @@ public function stream_write($data) { ...@@ -155,7 +158,6 @@ public function stream_write($data) {
* @see http://php.net/manual/streamwrapper.stream-flush.php * @see http://php.net/manual/streamwrapper.stream-flush.php
*/ */
public function stream_flush() { public function stream_flush() {
trigger_error('stream_flush() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE; return FALSE;
} }
......
...@@ -53,9 +53,17 @@ function testWriteFunctions() { ...@@ -53,9 +53,17 @@ function testWriteFunctions() {
$uri = $this->scheme . '://' . $filename; $uri = $this->scheme . '://' . $filename;
$instance = file_stream_wrapper_get_instance_by_scheme($this->scheme); $instance = file_stream_wrapper_get_instance_by_scheme($this->scheme);
// Attempt to open a file in write mode // Attempt to open a file in read/write mode
$handle = @fopen($uri, 'w+'); $handle = @fopen($uri, 'r+');
$this->assertFalse($handle, 'Unable to open a file for writing with the read-only stream wrapper.'); $this->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
// Attempt to open a file in binary read mode
$handle = fopen($uri, 'rb');
$this->assertTrue($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
$this->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
// Attempt to open a file in text read mode
$handle = fopen($uri, 'rt');
$this->assertTrue($handle, 'Able to open a file for reading in text mode with the read-only stream wrapper.');
$this->assertTrue(fclose($handle), 'Able to close file opened in text mode using the read_only stream wrapper.');
// Attempt to open a file in read mode // Attempt to open a file in read mode
$handle = fopen($uri, 'r'); $handle = fopen($uri, 'r');
$this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.'); $this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.');
...@@ -72,7 +80,7 @@ function testWriteFunctions() { ...@@ -72,7 +80,7 @@ function testWriteFunctions() {
// Attempt to flush output to the file // Attempt to flush output to the file
$this->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.'); $this->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.');
// Attempt to close the stream. (Suppress errors, as fclose triggers fflush.) // Attempt to close the stream. (Suppress errors, as fclose triggers fflush.)
$this->assertTrue(@fclose($handle), 'Able to close file using the read_only stream wrapper.'); $this->assertTrue(fclose($handle), 'Able to close file using the read_only stream wrapper.');
// Test the rename() function // Test the rename() function
$this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.'); $this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.');
// Test the unlink() function // Test the unlink() function
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment