2021-07-18 20:09:11 +00:00
< ? php
/**
2024-01-02 20:57:26 +00:00
* @ copyright Copyright ( C ) 2010 - 2024 , the Friendica project
2021-07-18 20:09:11 +00:00
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
*/
namespace Friendica\Module\Admin ;
use Friendica\Core\Renderer ;
use Friendica\DI ;
2021-10-23 10:11:38 +00:00
use Friendica\Core\Storage\Exception\InvalidClassStorageException ;
use Friendica\Core\Storage\Capability\ICanConfigureStorage ;
use Friendica\Core\Storage\Capability\ICanWriteToStorage ;
2021-07-18 20:09:11 +00:00
use Friendica\Module\BaseAdmin ;
use Friendica\Util\Strings ;
class Storage extends BaseAdmin
{
2021-11-28 12:44:42 +00:00
protected function post ( array $request = [])
2021-07-18 20:09:11 +00:00
{
self :: checkAdminAccess ();
self :: checkFormSecurityTokenRedirectOnError ( '/admin/storage' , 'admin_storage' );
2021-11-14 22:19:25 +00:00
$storagebackend = trim ( $this -> parameters [ 'name' ] ? ? '' );
2021-07-20 21:33:35 +00:00
2021-08-10 21:56:30 +00:00
try {
2021-10-27 18:16:34 +00:00
/** @var ICanConfigureStorage|false $newStorageConfig */
2021-10-04 08:25:29 +00:00
$newStorageConfig = DI :: storageManager () -> getConfigurationByName ( $storagebackend );
2021-08-10 21:56:30 +00:00
} catch ( InvalidClassStorageException $storageException ) {
2022-10-17 18:55:22 +00:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'Storage backend, %s is invalid.' , $storagebackend ));
2021-08-10 21:56:30 +00:00
DI :: baseUrl () -> redirect ( 'admin/storage' );
}
2021-07-18 20:09:11 +00:00
2021-10-04 08:25:29 +00:00
if ( $newStorageConfig !== false ) {
// save storage backend form
$storage_opts = $newStorageConfig -> getOptions ();
$storage_form_prefix = preg_replace ( '|[^a-zA-Z0-9]|' , '' , $storagebackend );
$storage_opts_data = [];
foreach ( $storage_opts as $name => $info ) {
$fieldname = $storage_form_prefix . '_' . $name ;
switch ( $info [ 0 ]) { // type
case 'checkbox' :
case 'yesno' :
$value = ! empty ( $_POST [ $fieldname ]);
break ;
default :
$value = $_POST [ $fieldname ] ? ? '' ;
}
$storage_opts_data [ $name ] = $value ;
2021-07-18 20:09:11 +00:00
}
2021-10-04 08:25:29 +00:00
unset ( $name );
unset ( $info );
2021-07-18 20:09:11 +00:00
2021-10-04 08:25:29 +00:00
$storage_form_errors = $newStorageConfig -> saveOptions ( $storage_opts_data );
if ( count ( $storage_form_errors )) {
foreach ( $storage_form_errors as $name => $err ) {
2022-10-17 18:55:22 +00:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'Storage backend %s error: %s' , $storage_opts [ $name ][ 1 ], $err ));
2021-10-04 08:25:29 +00:00
}
DI :: baseUrl () -> redirect ( 'admin/storage' );
2021-07-18 20:09:11 +00:00
}
2021-07-18 21:09:45 +00:00
}
2023-03-26 22:17:40 +00:00
if ( ! empty ( $_POST [ 'submit_save_set' ]) && DI :: config () -> isWritable ( 'storage' , 'name' ) ) {
2021-08-10 21:56:30 +00:00
try {
$newstorage = DI :: storageManager () -> getWritableStorageByName ( $storagebackend );
2021-08-01 12:00:48 +00:00
2021-08-10 21:56:30 +00:00
if ( ! DI :: storageManager () -> setBackend ( $newstorage )) {
2022-10-17 18:55:22 +00:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'Invalid storage backend setting value.' ));
2021-08-10 21:56:30 +00:00
}
} catch ( InvalidClassStorageException $storageException ) {
2022-10-17 18:55:22 +00:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'Invalid storage backend setting value.' ));
2021-07-20 21:33:35 +00:00
}
2021-07-18 20:09:11 +00:00
}
DI :: baseUrl () -> redirect ( 'admin/storage' );
}
2021-11-20 14:38:03 +00:00
protected function content ( array $request = []) : string
2021-07-18 20:09:11 +00:00
{
2021-11-14 19:46:25 +00:00
parent :: content ();
2021-07-18 20:09:11 +00:00
2021-07-31 18:56:32 +00:00
$current_storage_backend = DI :: storage ();
$available_storage_forms = [];
2021-07-18 20:09:11 +00:00
2021-08-15 22:10:37 +00:00
foreach ( DI :: storageManager () -> listBackends () as $name ) {
2021-07-18 20:09:11 +00:00
2021-07-18 20:36:06 +00:00
// build storage config form,
2021-07-18 21:09:45 +00:00
$storage_form_prefix = preg_replace ( '|[^a-zA-Z0-9]|' , '' , $name );
2021-07-18 20:09:11 +00:00
2021-10-04 08:25:29 +00:00
$storage_form = [];
$storageConfig = DI :: storageManager () -> getConfigurationByName ( $name );
if ( $storageConfig !== false ) {
foreach ( $storageConfig -> getOptions () as $option => $info ) {
2021-07-18 20:09:11 +00:00
2021-10-04 08:25:29 +00:00
$type = $info [ 0 ];
2023-03-22 03:17:09 +00:00
// Backward compatibility with yesno field description
2021-10-04 08:25:29 +00:00
if ( $type == 'yesno' ) {
$type = 'checkbox' ;
// Remove translated labels Yes No from field info
unset ( $info [ 4 ]);
}
$info [ 0 ] = $storage_form_prefix . '_' . $option ;
$info [ 'type' ] = $type ;
$info [ 'field' ] = 'field_' . $type . '.tpl' ;
$storage_form [ $option ] = $info ;
}
2021-07-18 20:36:06 +00:00
}
2021-07-20 21:33:35 +00:00
$available_storage_forms [] = [
'name' => $name ,
'prefix' => $storage_form_prefix ,
'form' => $storage_form ,
2021-10-23 10:11:38 +00:00
'active' => $current_storage_backend instanceof ICanWriteToStorage && $name === $current_storage_backend :: getName (),
2021-07-20 21:33:35 +00:00
];
2021-07-18 20:09:11 +00:00
}
$t = Renderer :: getMarkupTemplate ( 'admin/storage.tpl' );
return Renderer :: replaceMacros ( $t , [
2021-07-18 20:36:06 +00:00
'$title' => DI :: l10n () -> t ( 'Administration' ),
2021-09-01 12:00:23 +00:00
'$label_current' => DI :: l10n () -> t ( 'Current Storage Backend' ),
'$label_config' => DI :: l10n () -> t ( 'Storage Configuration' ),
2021-07-18 20:36:06 +00:00
'$page' => DI :: l10n () -> t ( 'Storage' ),
2021-07-20 21:41:38 +00:00
'$save' => DI :: l10n () -> t ( 'Save' ),
2021-09-02 21:27:35 +00:00
'$save_use' => DI :: l10n () -> t ( 'Save & Use storage backend' ),
'$use' => DI :: l10n () -> t ( 'Use storage backend' ),
2021-07-24 16:57:29 +00:00
'$save_reload' => DI :: l10n () -> t ( 'Save & Reload' ),
'$noconfig' => DI :: l10n () -> t ( 'This backend doesn\'t have custom settings' ),
2021-07-18 20:36:06 +00:00
'$form_security_token' => self :: getFormSecurityToken ( " admin_storage " ),
2023-03-26 22:17:40 +00:00
'$storagebackend_ro_txt' => ! DI :: config () -> isWritable ( 'storage' , 'name' ) ? DI :: l10n () -> t ( 'Changing the current backend is prohibited because it is set by an environment variable' ) : '' ,
2023-03-27 17:35:02 +00:00
'$is_writable' => DI :: config () -> isWritable ( 'storage' , 'name' ),
2021-10-23 10:11:38 +00:00
'$storagebackend' => $current_storage_backend instanceof ICanWriteToStorage ? $current_storage_backend :: getName () : DI :: l10n () -> t ( 'Database (legacy)' ),
2021-07-18 20:36:06 +00:00
'$availablestorageforms' => $available_storage_forms ,
2021-07-18 20:09:11 +00:00
]);
}
}