Issue #1308054 by jthorson, brianV: Added an abstract DrupalReadOnlyStreamWrapper that other stream wrappers can extend.

8.0.x
catch 2013-01-07 13:12:52 +00:00
parent 9fd8864cf4
commit 43658a4ed4
5 changed files with 622 additions and 0 deletions

View File

@ -0,0 +1,225 @@
<?php
/**
* @file
* Definition of Drupal\Core\StreamWrapper\LocalReadOnlyStream.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a read-only Drupal stream wrapper base class for local files.
*
* This class extends the complete stream wrapper implementation in LocalStream.
* URIs such as "public://example.txt" are expanded to a normal filesystem path
* such as "sites/default/files/example.txt" and then PHP filesystem functions
* are invoked.
*
* Drupal\Core\StreamWrapper\LocalReadOnlyStream implementations need to
* implement at least the getDirectoryPath() and getExternalUrl() methods.
*/
abstract class LocalReadOnlyStream extends LocalStream {
/**
* 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 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;
}
}

View File

@ -0,0 +1,259 @@
<?php
/**
* @file
* Definition of Drupal\Core\StreamWrapper\ReadOnlyStream.
*/
namespace Drupal\Core\StreamWrapper;
/**
* Defines a read-only Drupal stream wrapper base class.
*
* This class provides a minimal-read only stream wrapper implementation.
* Specifically, it only implements the writing classes and read classes where
* we need to restrict 'write-capable' arguments.
*
* Drupal\Core\StreamWrapper\ReadOnlyStream implementations need to implement
* all the read-related classes.
*/
abstract class ReadOnlyStream implements StreamWrapperInterface {
/**
* Stream context resource.
*
* @var resource
*/
public $context;
/**
* A generic resource handle.
*
* @var resource
*/
public $handle = NULL;
/**
* Instance URI (stream).
*
* A stream is referenced as "scheme://target".
*
* @var string
*/
protected $uri;
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::setUri().
*/
function setUri($uri) {
$this->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;
}
}

View File

@ -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).'),
),
);
}

View File

@ -0,0 +1,39 @@
<?php
/**
* @file
* Definition of Drupal\file_test\DummyReadOnlyStreamWrapper.
*/
namespace Drupal\file_test;
use Drupal\Core\StreamWrapper\LocalReadOnlyStream;
/**
* Helper class for testing the stream wrapper registry.
*
* Dummy stream wrapper implementation (dummy-readonly://).
*/
class DummyReadOnlyStreamWrapper extends LocalReadOnlyStream {
function getDirectoryPath() {
return variable_get('stream_public_path', 'sites/default/files');
}
/**
* Override getInternalUri().
*
* Return a dummy path for testing.
*/
function getInternalUri() {
return '/dummy/example.txt';
}
/**
* Override getExternalUrl().
*
* Return the HTML URI of a public file.
*/
function getExternalUrl() {
return '/dummy/example.txt';
}
}

View File

@ -0,0 +1,94 @@
<?php
/**
* @file
* Definition of Drupal\system\Tests\File\ReadOnlyStreamWrapperTest.
*/
namespace Drupal\system\Tests\File;
/**
* Tests that files can not be written using ReadOnlyStreamWrapper functions.
*/
class ReadOnlyStreamWrapperTest extends FileTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('file_test');
protected $scheme = 'dummy-readonly';
protected $classname = 'Drupal\file_test\DummyReadOnlyStreamWrapper';
public static function getInfo() {
return array(
'name' => '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);
}
}