2018-11-21 09:38:54 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Model/Storage/Filesystem.php
|
2020-01-19 07:05:23 +01:00
|
|
|
* Storage backend system
|
2018-11-21 09:38:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Storage;
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
use Friendica\Core\Config\IConfiguration;
|
|
|
|
use Friendica\Core\L10n\L10n;
|
2018-11-21 09:38:54 +01:00
|
|
|
use Friendica\Util\Strings;
|
2020-01-05 01:58:49 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-11-21 09:38:54 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Filesystem based storage backend
|
2018-11-21 09:38:54 +01:00
|
|
|
*
|
|
|
|
* This class manage data on filesystem.
|
|
|
|
* Base folder for storage is set in storage.filesystem_path.
|
|
|
|
* Best would be for storage folder to be outside webserver folder, we are using a
|
|
|
|
* folder relative to code tree root as default to ease things for users in shared hostings.
|
|
|
|
* Each new resource gets a value as reference and is saved in a
|
|
|
|
* folder tree stucture created from that value.
|
|
|
|
*/
|
2020-01-06 17:42:28 +01:00
|
|
|
class Filesystem extends AbstractStorage
|
2018-11-21 09:38:54 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
const NAME = 'Filesystem';
|
|
|
|
|
2018-11-21 09:38:54 +01:00
|
|
|
// Default base folder
|
2018-12-08 18:49:16 +01:00
|
|
|
const DEFAULT_BASE_FOLDER = 'storage';
|
2018-11-21 09:38:54 +01:00
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/** @var IConfiguration */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $basePath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filesystem constructor.
|
|
|
|
*
|
|
|
|
* @param IConfiguration $config
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param L10n $l10n
|
|
|
|
*/
|
|
|
|
public function __construct(IConfiguration $config, LoggerInterface $logger, L10n $l10n)
|
2018-11-21 09:38:54 +01:00
|
|
|
{
|
2020-01-06 17:42:28 +01:00
|
|
|
parent::__construct($l10n, $logger);
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->config = $config;
|
|
|
|
|
|
|
|
$path = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
|
|
|
|
$this->basePath = rtrim($path, '/');
|
2018-11-21 09:38:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Split data ref and return file path
|
2020-01-05 01:58:49 +01:00
|
|
|
*
|
|
|
|
* @param string $reference Data reference
|
|
|
|
*
|
2018-11-21 09:38:54 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-05 01:58:49 +01:00
|
|
|
private function pathForRef(string $reference)
|
2018-11-21 09:38:54 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
$fold1 = substr($reference, 0, 2);
|
|
|
|
$fold2 = substr($reference, 2, 2);
|
|
|
|
$file = substr($reference, 4);
|
2018-11-21 09:38:54 +01:00
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
return implode('/', [$this->basePath, $fold1, $fold2, $file]);
|
2018-11-21 09:38:54 +01:00
|
|
|
}
|
|
|
|
|
2018-11-21 16:36:11 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Create dirctory tree to store file, with .htaccess and index.html files
|
2020-01-05 01:58:49 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $file Path and filename
|
2020-01-05 01:58:49 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws StorageException
|
2018-11-21 16:36:11 +01:00
|
|
|
*/
|
2020-01-05 01:58:49 +01:00
|
|
|
private function createFoldersForFile(string $file)
|
2018-11-21 16:36:11 +01:00
|
|
|
{
|
|
|
|
$path = dirname($file);
|
|
|
|
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
if (!mkdir($path, 0770, true)) {
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->logger->warning('Failed to create dir.', ['path' => $path]);
|
|
|
|
throw new StorageException($this->l10n->t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
|
2018-11-21 16:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
while ($path !== $this->basePath) {
|
2018-12-08 18:49:16 +01:00
|
|
|
if (!is_file($path . '/index.html')) {
|
|
|
|
file_put_contents($path . '/index.html', '');
|
2018-11-21 16:36:11 +01:00
|
|
|
}
|
2019-01-29 12:35:30 +01:00
|
|
|
chmod($path . '/index.html', 0660);
|
|
|
|
chmod($path, 0770);
|
2018-11-21 16:36:11 +01:00
|
|
|
$path = dirname($path);
|
|
|
|
}
|
2018-12-08 18:49:16 +01:00
|
|
|
if (!is_file($path . '/index.html')) {
|
|
|
|
file_put_contents($path . '/index.html', '');
|
2019-01-29 12:35:30 +01:00
|
|
|
chmod($path . '/index.html', 0660);
|
2018-11-21 16:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function get(string $reference)
|
2018-11-21 09:38:54 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
$file = $this->pathForRef($reference);
|
2018-11-21 15:10:47 +01:00
|
|
|
if (!is_file($file)) {
|
2018-12-08 18:49:16 +01:00
|
|
|
return '';
|
2018-11-21 15:10:47 +01:00
|
|
|
}
|
2018-11-21 09:38:54 +01:00
|
|
|
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function put(string $data, string $reference = '')
|
2018-11-21 09:38:54 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
if ($reference === '') {
|
|
|
|
$reference = Strings::getRandomHex();
|
2018-11-21 09:38:54 +01:00
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
$file = $this->pathForRef($reference);
|
2018-11-21 09:38:54 +01:00
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->createFoldersForFile($file);
|
2018-11-21 09:38:54 +01:00
|
|
|
|
2020-01-17 20:45:21 +01:00
|
|
|
$result = file_put_contents($file, $data);
|
|
|
|
|
|
|
|
// just in case the result is REALLY false, not zero or empty or anything else, throw the exception
|
|
|
|
if ($result === false) {
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->logger->warning('Failed to write data.', ['file' => $file]);
|
|
|
|
throw new StorageException($this->l10n->t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
|
2018-11-21 09:38:54 +01:00
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
|
2019-01-29 12:35:30 +01:00
|
|
|
chmod($file, 0660);
|
2020-01-05 01:58:49 +01:00
|
|
|
return $reference;
|
2018-11-21 09:38:54 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete(string $reference)
|
2018-11-21 09:38:54 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
$file = $this->pathForRef($reference);
|
2018-11-21 15:10:47 +01:00
|
|
|
// return true if file doesn't exists. we want to delete it: success with zero work!
|
2018-11-29 08:34:00 +01:00
|
|
|
if (!is_file($file)) {
|
2018-11-21 15:10:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-21 09:38:54 +01:00
|
|
|
return unlink($file);
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getOptions()
|
2018-12-08 18:49:16 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'storagepath' => [
|
|
|
|
'input',
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->l10n->t('Storage base path'),
|
|
|
|
$this->basePath,
|
|
|
|
$this->l10n->t('Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
|
2018-12-08 18:49:16 +01:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function saveOptions(array $data)
|
2018-12-08 18:49:16 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
$storagePath = $data['storagepath'] ?? '';
|
|
|
|
if ($storagePath === '' || !is_dir($storagePath)) {
|
2018-12-08 18:49:16 +01:00
|
|
|
return [
|
2020-01-05 01:58:49 +01:00
|
|
|
'storagepath' => $this->l10n->t('Enter a valid existing folder')
|
2018-12-08 18:49:16 +01:00
|
|
|
];
|
|
|
|
};
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->config->set('storage', 'filesystem_path', $storagePath);
|
|
|
|
$this->basePath = $storagePath;
|
2018-12-08 18:49:16 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-01-06 17:42:28 +01:00
|
|
|
public static function getName()
|
2020-01-05 01:58:49 +01:00
|
|
|
{
|
|
|
|
return self::NAME;
|
|
|
|
}
|
2018-11-21 15:10:47 +01:00
|
|
|
}
|