From 43658a4ed41090b696fead31c68a0760788e37f8 Mon Sep 17 00:00:00 2001 From: catch Date: Mon, 7 Jan 2013 13:12:52 +0000 Subject: [PATCH] Issue #1308054 by jthorson, brianV: Added an abstract DrupalReadOnlyStreamWrapper that other stream wrappers can extend. --- .../StreamWrapper/LocalReadOnlyStream.php | 225 +++++++++++++++ .../Core/StreamWrapper/ReadOnlyStream.php | 259 ++++++++++++++++++ .../file/tests/file_test/file_test.module | 5 + .../file_test/DummyReadOnlyStreamWrapper.php | 39 +++ .../Tests/File/ReadOnlyStreamWrapperTest.php | 94 +++++++ 5 files changed, 622 insertions(+) create mode 100644 core/lib/Drupal/Core/StreamWrapper/LocalReadOnlyStream.php create mode 100644 core/lib/Drupal/Core/StreamWrapper/ReadOnlyStream.php create mode 100644 core/modules/file/tests/file_test/lib/Drupal/file_test/DummyReadOnlyStreamWrapper.php create mode 100644 core/modules/system/lib/Drupal/system/Tests/File/ReadOnlyStreamWrapperTest.php diff --git a/core/lib/Drupal/Core/StreamWrapper/LocalReadOnlyStream.php b/core/lib/Drupal/Core/StreamWrapper/LocalReadOnlyStream.php new file mode 100644 index 00000000000..1b14280f3fa --- /dev/null +++ b/core/lib/Drupal/Core/StreamWrapper/LocalReadOnlyStream.php @@ -0,0 +1,225 @@ +uri = $uri; + $path = $this->getLocalPath(); + $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode); + if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) { + $opened_path = $path; + } + + return (bool) $this->handle; + } + + /** + * Support for flock(). + * + * An exclusive lock attempt will be rejected, as this is a read-only stream + * wrapper. + * + * @param int $operation + * One of the following: + * - LOCK_SH to acquire a shared lock (reader). + * - LOCK_EX to acquire an exclusive lock (writer). + * - LOCK_UN to release a lock (shared or exclusive). + * - LOCK_NB added as a bitmask if you don't want flock() to block while + * locking (not supported on Windows). + * + * @return bool + * Return FALSE for an exclusive lock (writer), as this is a read-only + * stream wrapper. Return the result of flock() for other valid operations. + * Defaults to TRUE if an invalid operation is passed. + * + * @see http://php.net/manual/streamwrapper.stream-lock.php + */ + public function stream_lock($operation) { + // Disallow exclusive lock or non-blocking lock requests + if (in_array($operation, array(LOCK_EX, LOCK_EX|LOCK_NB))) { + trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + if (in_array($operation, array(LOCK_SH, LOCK_UN, LOCK_SH|LOCK_NB))) { + return flock($this->handle, $operation); + } + + return TRUE; + } + + + /** + * Support for fwrite(), file_put_contents() etc. + * + * Data will not be written as this is a read-only stream wrapper. + * + * @param string $data + * The string to be written. + * + * @return bool + * FALSE as data will not be written. + * + * @see http://php.net/manual/en/streamwrapper.stream-write.php + */ + public function stream_write($data) { + trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for fflush(). + * + * Nothing will be output to the file, as this is a read-only stream wrapper. + * + * @return bool + * FALSE, as no data will be stored. + * + * @see http://php.net/manual/streamwrapper.stream-flush.php + */ + public function stream_flush() { + trigger_error('stream_flush() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for unlink(). + * + * The file will not be deleted from the stream as this is a read-only stream + * wrapper. + * + * @param string $uri + * A string containing the uri to the resource to delete. + * + * @return bool + * TRUE so that file_delete() will remove db reference to file. File is not + * actually deleted. + * + * @see http://php.net/manual/en/streamwrapper.unlink.php + */ + public function unlink($uri) { + trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING); + return TRUE; + } + + /** + * Support for rename(). + * + * The file will not be renamed as this is a read-only stream wrapper. + * + * @param string $from_uri, + * The uri to the file to rename. + * @param string $to_uri + * The new uri for file. + * + * @return bool + * FALSE as file will never be renamed. + * + * @see http://php.net/manual/en/streamwrapper.rename.php + */ + public function rename($from_uri, $to_uri) { + trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for mkdir(). + * + * Directory will never be created as this is a read-only stream wrapper. + * + * @param string $uri + * A string containing the URI to the directory to create. + * @param int $mode + * Permission flags - see mkdir(). + * @param int $options + * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE. + * + * @return bool + * FALSE as directory will never be created. + * + * @see http://php.net/manual/en/streamwrapper.mkdir.php + */ + public function mkdir($uri, $mode, $options) { + trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for rmdir(). + * + * Directory will never be deleted as this is a read-only stream wrapper. + * + * @param string $uri + * A string containing the URI to the directory to delete. + * @param int $options + * A bit mask of STREAM_REPORT_ERRORS. + * + * @return bool + * FALSE as directory will never be deleted. + * + * @see http://php.net/manual/en/streamwrapper.rmdir.php + */ + public function rmdir($uri, $options) { + trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for chmod(). + * + * Does not change file permissions as this is a read-only stream wrapper. + * + * @param int $mode + * Permission flags - see chmod(). + * + * @return bool + * FALSE as the permission change will never be allowed. + */ + public function chmod($mode) { + trigger_error('chmod() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } +} diff --git a/core/lib/Drupal/Core/StreamWrapper/ReadOnlyStream.php b/core/lib/Drupal/Core/StreamWrapper/ReadOnlyStream.php new file mode 100644 index 00000000000..2ebcc0650d1 --- /dev/null +++ b/core/lib/Drupal/Core/StreamWrapper/ReadOnlyStream.php @@ -0,0 +1,259 @@ +uri = $uri; + } + + /** + * Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::getUri(). + */ + function getUri() { + return $this->uri; + } + + /** + * Support for fopen(), file_get_contents(), etc. + * + * Any write modes will be rejected, as this is a read-only stream wrapper. + * + * @param string $uri + * A string containing the URI to the file to open. + * @param int $mode + * The file mode (only "r" is supported for the read-only stream wrapper.) + * @param int $options + * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS. + * @param string $opened_path + * A string containing the path actually opened. + * + * @return bool + * Returns TRUE if $mode == "r" and the file was opened successfully. + * + * @see http://php.net/manual/streamwrapper.stream-open.php + */ + public function stream_open($uri, $mode, $options, &$opened_path) { + if ($mode != "r") { + if ($options & STREAM_REPORT_ERRORS) { + trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING); + } + return FALSE; + } + + $this->uri = $uri; + $path = $this->getLocalPath(); + $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode); + + if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) { + $opened_path = $path; + } + + return (bool) $this->handle; + } + + /** + * Support for flock(). + * + * An exclusive lock attempt will be rejected, as this is a read-only stream + * wrapper. + * + * @param int $operation + * One of the following: + * - LOCK_SH to acquire a shared lock (reader). + * - LOCK_EX to acquire an exclusive lock (writer). + * - LOCK_UN to release a lock (shared or exclusive). + * - LOCK_NB if you don't want flock() to block while locking (not + * supported on Windows). + * + * @return bool + * Return FALSE for an exclusive lock (writer), as this is a read-only + * stream wrapper. Return the result of flock() for other valid operations. + * Defaults to TRUE if an invalid operation is passed. + * + * @see http://php.net/manual/streamwrapper.stream-lock.php + */ + public function stream_lock($operation) { + if (in_array($operation, array(LOCK_EX, LOCK_EX|LOCK_NB))) { + trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + if (in_array($operation, array(LOCK_SH, LOCK_UN, LOCK_SH|LOCK_NB))) { + return flock($this->handle, $operation); + } + + return TRUE; + } + + /** + * Support for fwrite(), file_put_contents() etc. + * + * Data will not be written as this is a read-only stream wrapper. + * + * @param string $data + * The string to be written. + * + * @return bool + * FALSE as data will not be written. + * + * @see http://php.net/manual/en/streamwrapper.stream-write.php + */ + public function stream_write($data) { + trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for fflush(). + * + * Nothing will be output to the file, as this is a read-only stream wrapper. + * + * @return bool + * FALSE, as no data will be stored. + * + * @see http://php.net/manual/streamwrapper.stream-flush.php + */ + public function stream_flush() { + trigger_error('stream_flush() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for unlink(). + * + * The file will not be deleted from the stream as this is a read-only stream + * wrapper. + * + * @param string $uri + * A string containing the uri to the resource to delete. + * + * @return bool + * TRUE so that file_delete() will remove db reference to file. File is not + * actually deleted. + * + * @see http://php.net/manual/en/streamwrapper.unlink.php + */ + public function unlink($uri) { + trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING); + return TRUE; + } + + /** + * Support for rename(). + * + * This file will not be renamed as this is a read-only stream wrapper. + * + * @param string $from_uri, + * The uri to the file to rename. + * @param string $to_uri + * The new uri for file. + * + * @return bool + * FALSE as file will never be renamed. + * + * @see http://php.net/manual/en/streamwrapper.rename.php + */ + public function rename($from_uri, $to_uri) { + trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for mkdir(). + * + * Directory will never be created as this is a read-only stream wrapper. + * + * @param string $uri + * A string containing the URI to the directory to create. + * @param int $mode + * Permission flags - see mkdir(). + * @param int $options + * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE. + * + * @return bool + * FALSE as directory will never be created. + * + * @see http://php.net/manual/en/streamwrapper.mkdir.php + */ + public function mkdir($uri, $mode, $options) { + trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for rmdir(). + * + * Directory will never be deleted as this is a read-only stream wrapper. + * + * @param string $uri + * A string containing the URI to the directory to delete. + * @param int $options + * A bit mask of STREAM_REPORT_ERRORS. + * + * @return bool + * FALSE as directory will never be deleted. + * + * @see http://php.net/manual/en/streamwrapper.rmdir.php + */ + public function rmdir($uri, $options) { + trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } + + /** + * Support for chmod(). + * + * Does not change file permissions as this is a read-only stream wrapper. + * + * @param int $mode + * Permission flags - see chmod(). + * + * @return bool + * FALSE as the permission change will never be allowed. + */ + public function chmod($mode) { + trigger_error('chmod() not supported for read-only stream wrappers', E_USER_WARNING); + return FALSE; + } +} diff --git a/core/modules/file/tests/file_test/file_test.module b/core/modules/file/tests/file_test/file_test.module index 20039e72ef3..1d924ded4e1 100644 --- a/core/modules/file/tests/file_test/file_test.module +++ b/core/modules/file/tests/file_test/file_test.module @@ -42,6 +42,11 @@ function file_test_stream_wrappers() { 'class' => 'Drupal\file_test\DummyRemoteStreamWrapper', 'description' => t('Dummy wrapper for simpletest (remote).'), ), + 'dummy-readonly' => array( + 'name' => t('Dummy files (readonly)'), + 'class' => 'Drupal\file_test\DummyReadOnlyStreamWrapper', + 'description' => t('Dummy wrapper for simpletest (readonly).'), + ), ); } diff --git a/core/modules/file/tests/file_test/lib/Drupal/file_test/DummyReadOnlyStreamWrapper.php b/core/modules/file/tests/file_test/lib/Drupal/file_test/DummyReadOnlyStreamWrapper.php new file mode 100644 index 00000000000..b5adf01d55a --- /dev/null +++ b/core/modules/file/tests/file_test/lib/Drupal/file_test/DummyReadOnlyStreamWrapper.php @@ -0,0 +1,39 @@ + 'Read only stream wrapper', + 'description' => 'Tests the read-only stream wrapper write functions.', + 'group' => 'File API', + ); + } + + function setUp() { + parent::setUp(); + drupal_static_reset('file_get_stream_wrappers'); + } + + function tearDown() { + parent::tearDown(); + stream_wrapper_unregister($this->scheme); + } + + /** + * Test write functionality of the read-only stream wrapper. + */ + function testWriteFunctions() { + // Generate a test file + $filename = $this->randomName(); + $filepath = conf_path() . '/files/' . $filename; + file_put_contents($filepath, $filename); + + // Generate a read-only stream wrapper instance + $uri = $this->scheme . '://' . $filename; + $instance = file_stream_wrapper_get_instance_by_scheme($this->scheme); + + // Attempt to open a file in write mode + $handle = @fopen($uri, 'w+'); + $this->assertFalse($handle, 'Unable to open a file for writing with the read-only stream wrapper.'); + // Attempt to open a file in read mode + $handle = fopen($uri, 'r'); + $this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.'); + // Attempt to change file permissions + $this->assertFalse(@drupal_chmod($uri, 0777), 'Unable to change file permissions when using read-only stream wrapper.'); + // Attempt to acquire an exclusive lock for writing + $this->assertFalse(@flock($handle, LOCK_EX | LOCK_NB), 'Unable to acquire an exclusive lock using the read-only stream wrapper.'); + // Attempt to obtain a shared lock + $this->assertTrue(flock($handle, LOCK_SH | LOCK_NB), 'Able to acquire a shared lock using the read-only stream wrapper.'); + // Attempt to release a shared lock + $this->assertTrue(flock($handle, LOCK_UN | LOCK_NB), 'Able to release a shared lock using the read-only stream wrapper.'); + // Attempt to write to the file + $this->assertFalse(@fwrite($handle, $this->randomName()), 'Unable to write to file using the read-only stream wrapper.'); + // Attempt to flush output to the file + $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.) + $this->assertTrue(@fclose($handle), 'Able to close file using the read_only stream wrapper.'); + // Test the rename() function + $this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.'); + // Test the unlink() function + $this->assertTrue(@drupal_unlink($uri), 'Able to unlink file using read-only stream wrapper.'); + $this->assertTrue(file_exists($filepath), 'Unlink File was not actually deleted.'); + + // Test the mkdir() function by attempting to create a directory. + $dirname = $this->randomName(); + $dir = conf_path() . '/files/' . $dirname; + $readonlydir = $this->scheme . '://' . $dirname; + $this->assertFalse(@drupal_mkdir($readonlydir, 0775, 0), 'Unable to create directory with read-only stream wrapper.'); + // Create a temporary directory for testing purposes + $this->assertTrue(drupal_mkdir($dir), 'Test directory created.'); + // Test the rmdir() function by attempting to remove the directory. + $this->assertFalse(@drupal_rmdir($readonlydir), 'Unable to delete directory with read-only stream wrapper.'); + // Remove the temporary directory. + drupal_rmdir($dir); + } +}