Add storage options form in admin/site page

This commit is contained in:
fabrixxm 2018-12-08 18:49:16 +01:00 committed by Hypolite Petovan
parent f7b6fef197
commit fdc6608af8
6 changed files with 169 additions and 16 deletions

View file

@ -51,4 +51,8 @@ class Database implements IStorage
{
return DBA::delete('storage', ['id' => $ref]);
}
}
public static function getOptions() { return []; }
public static function saveOptions($data) { return []; }
}

View file

@ -24,11 +24,11 @@ use Friendica\Util\Strings;
class Filesystem implements IStorage
{
// Default base folder
const DEFAULT_BASE_FOLDER = "storage";
const DEFAULT_BASE_FOLDER = 'storage';
private static function getBasePath()
{
return Config::get("storage", "filesystem_path", self::DEFAULT_BASE_FOLDER);
return Config::get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
}
/**
@ -43,7 +43,7 @@ class Filesystem implements IStorage
$fold2 = substr($ref, 2, 2);
$file = substr($ref, 4);
return "{$base}/{$fold1}/{$fold2}/{$file}";
return implode('/', [$base, $fold1, $fold2, $file]);
}
@ -57,8 +57,8 @@ class Filesystem implements IStorage
if (!is_dir($path)) {
if (!mkdir($path, 0770, true)) {
Logger::log("Failed to create dirs {$path}");
throw new StorageException(L10n::t("Filesystem storage failed to create '%s'. Check you write permissions.", $path));
Logger::log('Failed to create dirs ' . $path);
throw new StorageException(L10n::t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
killme();
}
}
@ -66,13 +66,13 @@ class Filesystem implements IStorage
$base = self::getBasePath();
while ($path !== $base) {
if (!is_file($path . "/index.html")) {
file_put_contents($path . "/index.html", "");
if (!is_file($path . '/index.html')) {
file_put_contents($path . '/index.html', '');
}
$path = dirname($path);
}
if (!is_file($path . "/index.html")) {
file_put_contents($path . "/index.html", "");
if (!is_file($path . '/index.html')) {
file_put_contents($path . '/index.html', '');
}
}
@ -80,15 +80,15 @@ class Filesystem implements IStorage
{
$file = self::pathForRef($ref);
if (!is_file($file)) {
return "";
return '';
}
return file_get_contents($file);
}
public static function put($data, $ref = "")
public static function put($data, $ref = '')
{
if ($ref === "") {
if ($ref === '') {
$ref = Strings::getRandomHex();
}
$file = self::pathForRef($ref);
@ -97,8 +97,8 @@ class Filesystem implements IStorage
$r = file_put_contents($file, $data);
if ($r === FALSE) {
Logger::log("Failed to write data to {$file}");
throw new StorageException(L10n::t("Filesystem storage failed to save data to '%s'. Check your write permissions", $file));
Logger::log('Failed to write data to ' . $file);
throw new StorageException(L10n::t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
killme();
}
return $ref;
@ -114,4 +114,28 @@ class Filesystem implements IStorage
return unlink($file);
}
public static function getOptions()
{
return [
'storagepath' => [
'input',
L10n::t('Storage base path'),
self::getBasePath(),
L10n::t('Folder were uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
]
];
}
public static function saveOptions($data)
{
$storagepath = defaults($data, 'storagepath', '');
if ($storagepath === '' || !is_dir($storagepath)) {
return [
'storagepath' => L10n::t('Enter a valid existing folder')
];
};
Config::set('storage', 'filesystem_path', $storagepath);
return [];
}
}

View file

@ -32,4 +32,58 @@ interface IStorage
* @return boolean True on success
*/
public static function delete($ref);
/**
* @brief Get info about storage options
*
* @return array
*
* This method return an array with informations about storage options
* from which the form presented to the user is build.
*
* The returned array is:
*
* [
* 'option1name' => [ ..info.. ],
* 'option2name' => [ ..info.. ],
* ...
* ]
*
* An empty array can be returned if backend doesn't have any options
*
* The info array for each option MUST be as follows:
*
* [
* 'type', // define the field used in form, and the type of data.
* // one of 'checkbox', 'combobox', 'custom', 'datetime',
* // 'input', 'intcheckbox', 'password', 'radio', 'richtext'
* // 'select', 'select_raw', 'textarea', 'yesno'
*
* 'label', // Translatable label of the field
* 'value', // Current value
* 'help text', // Translatable description for the field
* extra data // Optional. Depends on 'type':
* // select: array [ value => label ] of choices
* // intcheckbox: value of input element
* // select_raw: prebuild html string of < option > tags
* // yesno: array [ 'label no', 'label yes']
* ]
*
* See https://github.com/friendica/friendica/wiki/Quick-Template-Guide
*/
public static function getOptions();
/**
* @brief Validate and save options
*
* @param array $data Array [optionname => value] to be saved
*
* @return array Validation errors: [optionname => error message]
*
* Return array must be empty if no error.
*/
public static function saveOptions($data);
}