Commit 3250f84a authored by Dries's avatar Dries

Issue #1849570 by das-peter, marcingy: remove file.inc ties and fix unlink...

Issue #1849570 by das-peter, marcingy: remove file.inc ties and fix unlink while at it in MTimeProtectedFastFileStorage .
parent c1bf2494
......@@ -22,11 +22,11 @@ class FileStorage implements PhpStorageInterface {
/**
* Constructs this FileStorage object.
*
* @param $configuration
* @param array $configuration
* An associative array, containing at least these two keys:
* - directory: The directory where the files should be stored.
* - bin: The storage bin. Multiple storage objects can be instantiated with the
* same configuration, but for different bins..
* - bin: The storage bin. Multiple storage objects can be instantiated with
* the same configuration, but for different bins..
*/
public function __construct(array $configuration) {
$this->directory = $configuration['directory'] . '/' . $configuration['bin'];
......@@ -62,7 +62,10 @@ public function save($name, $code) {
*/
public function delete($name) {
$path = $this->getFullPath($name);
return @unlink($path);
if (file_exists($path)) {
return $this->unlink($path);
}
return FALSE;
}
/**
......@@ -75,27 +78,49 @@ protected function getFullPath($name) {
/**
* Implements Drupal\Component\PhpStorage\PhpStorageInterface::writeable().
*/
function writeable() {
public function writeable() {
return TRUE;
}
/**
* Implements Drupal\Component\PhpStorage\PhpStorageInterface::deleteAll().
*/
function deleteAll() {
// @todo remove this to properly decouple this class from Drupal.
if (!function_exists('file_unmanaged_delete_recursive')) {
include_once DRUPAL_ROOT . '/core/includes/file.inc';
}
return file_unmanaged_delete_recursive($this->directory, array(__CLASS__, 'filePreDeleteCallback'));
public function deleteAll() {
return $this->unlink($this->directory);
}
/**
* Ensures files and directories are deletable.
* Deletes files and/or directories in the specified path.
*
* If the specified path is a directory the method will
* call itself recursively to process the contents. Once the contents have
* been removed the directory will also be removed.
*
* @param string $path
* A string containing either a file or directory path.
*
* @return boolean
* TRUE for success or if path does not exist, FALSE in the event of an
* error.
*/
public static function filePreDeleteCallback($path) {
protected function unlink($path) {
if (file_exists($path)) {
// Ensure the file / folder is writable.
chmod($path, 0700);
if (is_dir($path)) {
$dir = dir($path);
while (($entry = $dir->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$this->unlink($path . '/' . $entry);
}
$dir->close();
return @rmdir($path);
}
return @unlink($path);
}
// If there's nothing to delete return TRUE anyway.
return TRUE;
}
}
......@@ -4,6 +4,7 @@
* @file
* Definition of Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage.
*/
namespace Drupal\Component\PhpStorage;
use DirectoryIterator;
......@@ -40,6 +41,9 @@
*/
class MTimeProtectedFastFileStorage extends FileStorage {
/**
* The .htaccess code to make a directory private.
*/
const HTACCESS="SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nDeny from all\nOptions None\nOptions +FollowSymLinks";
/**
......@@ -52,7 +56,7 @@ class MTimeProtectedFastFileStorage extends FileStorage {
/**
* Constructs this MTimeProtectedFastFileStorage object.
*
* @param $configuration
* @param array $configuration
* An associated array, containing at least these keys (the rest are
* ignored):
* - directory: The directory where the files should be stored.
......@@ -110,7 +114,7 @@ public function save($name, $data) {
rename($full_path, $temporary_path);
// Make sure to not loop infinitely on a hopelessly slow filesystem.
if ($i > 10) {
unlink($temporary_path);
$this->unlink($temporary_path);
return FALSE;
}
}
......@@ -155,14 +159,14 @@ protected function ensureDirectory() {
/**
* Removes everything in a directory, leaving it empty.
*
* @param $directory
* @param string $directory
* The directory to be emptied out.
*/
protected function cleanDirectory($directory) {
chmod($directory, 0700);
foreach (new DirectoryIterator($directory) as $fileinfo) {
if (!$fileinfo->isDot()) {
unlink($fileinfo->getPathName());
$this->unlink($fileinfo->getPathName());
}
}
}
......@@ -185,8 +189,9 @@ protected function cleanDirectory($directory) {
* @param int $directory_mtime
* (optional) The mtime of $directory. Can be passed to avoid an extra
* filesystem call when the mtime of the directory is already known.
*
* @return string
* The full path where the file is or should be stored.
* The full path where the file is or should be stored.
*/
protected function getFullPath($name, &$directory = NULL, &$directory_mtime = NULL) {
if (!isset($directory)) {
......@@ -212,4 +217,5 @@ protected function getUncachedMTime($directory) {
clearstatcache(TRUE, $directory);
return filemtime($directory);
}
}
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