From a253234bd4f4f601e1fdbee0d11009f7009b3873 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Sun, 5 Apr 2020 14:53:22 +0200 Subject: [PATCH] Fixed installer errors --- app/Controllers/TagController.php | 2 +- app/Database/Migrator.php | 16 ++++------------ app/helpers.php | 7 ++----- bin/migrate | 12 +++--------- bootstrap/app.php | 5 +++++ bootstrap/container.php | 5 ----- composer.json | 5 ++--- composer.lock | 23 ++++++++++++----------- config.example.php | 2 +- docs/installation.md | 2 +- install/index.php | 21 ++++++++------------- resources/lang/it.lang.php | 2 +- resources/templates/upload/web.twig | 2 +- src/js/installer.js | 29 +++++++++++++++-------------- 14 files changed, 56 insertions(+), 77 deletions(-) diff --git a/app/Controllers/TagController.php b/app/Controllers/TagController.php index 6d103e0..58e3b49 100644 --- a/app/Controllers/TagController.php +++ b/app/Controllers/TagController.php @@ -73,6 +73,6 @@ class TagController extends Controller return make(ValidationHelper::class) ->failIf(empty(param($request, 'mediaId'))) ->failIf($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `id` = ?', param($request, 'mediaId'))->fetch()->count == 0) - ->failIf($this->session->get('admin', false) || $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', param($request, 'mediaId'))->fetch()->user_id !== $this->session->get('user_id')); + ->failIf(!$this->session->get('admin', false) && $this->database->query('SELECT `user_id` FROM `uploads` WHERE `id` = ? LIMIT 1', param($request, 'mediaId'))->fetch()->user_id != $this->session->get('user_id')); } } diff --git a/app/Database/Migrator.php b/app/Database/Migrator.php index 0ba89d4..a01710f 100644 --- a/app/Database/Migrator.php +++ b/app/Database/Migrator.php @@ -16,23 +16,17 @@ class Migrator * @var string */ private $schemaPath; - /** - * @var bool - */ - private $firstMigrate; /** * Migrator constructor. * - * @param DB $db - * @param string $schemaPath - * @param bool $firstMigrate + * @param DB $db + * @param string $schemaPath */ - public function __construct(DB $db, ?string $schemaPath, bool $firstMigrate = false) + public function __construct(DB $db, ?string $schemaPath) { $this->db = $db; $this->schemaPath = $schemaPath; - $this->firstMigrate = $firstMigrate; } public function migrate() @@ -43,9 +37,7 @@ class Migrator $this->firstMigrate = true; } - if ($this->firstMigrate) { - $this->db->getPdo()->exec(file_get_contents($this->schemaPath.DIRECTORY_SEPARATOR.'migrations.sql')); - } + $this->db->getPdo()->exec(file_get_contents($this->schemaPath.DIRECTORY_SEPARATOR.'migrations.sql')); $files = glob($this->schemaPath.'/'.$this->db->getCurrentDriver().'/*.sql'); diff --git a/app/helpers.php b/app/helpers.php index 6f38789..af107e0 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -464,14 +464,11 @@ if (!function_exists('dsnFromConfig')) { * * @param array $config * - * @param string $baseDir * @return string */ - function dsnFromConfig(array $config, $baseDir = BASE_DIR): string + function dsnFromConfig(array $config): string { - $dsn = $config['db']['connection'] === 'sqlite' ? $baseDir.$config['db']['dsn'] : $config['db']['dsn']; - - return $config['db']['connection'].':'.$dsn; + return $config['db']['connection'].':'.$config['db']['dsn']; } } diff --git a/bin/migrate b/bin/migrate index 62e5414..5409a4c 100644 --- a/bin/migrate +++ b/bin/migrate @@ -18,15 +18,9 @@ if (!$config) { chdir(__DIR__.'/../'); -$firstMigrate = false; -if ($config['db']['connection'] === 'sqlite' && !file_exists(__DIR__.'/../'.$config['db']['dsn'])) { - touch(__DIR__.'/../'.$config['db']['dsn']); - $firstMigrate = true; -} +$db = new DB(dsnFromConfig($config), $config['db']['username'], $config['db']['password']); -$db = new DB(dsnFromConfig($config, getcwd().DIRECTORY_SEPARATOR), $config['db']['username'], $config['db']['password']); - -$migrator = new Migrator($db, 'resources/schemas', $firstMigrate); +$migrator = new Migrator($db, 'resources/schemas'); $migrator->migrate(); if (isset($argv[1]) && $argv[1] === '--install') { @@ -34,7 +28,7 @@ if (isset($argv[1]) && $argv[1] === '--install') { } if (file_exists(__DIR__.'/../install') && (!isset($config['debug']) || !$config['debug'])) { - removeDirectory(__DIR__.'/../install'); + //removeDirectory(__DIR__.'/../install'); } echo 'If you are upgrading from a previous version, please run a "php bin\clean".'.PHP_EOL; diff --git a/bootstrap/app.php b/bootstrap/app.php index 2f009c2..b552f9c 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -6,6 +6,7 @@ use App\Factories\ViewFactory; use App\Middleware\InjectMiddleware; use App\Middleware\LangMiddleware; use App\Middleware\RememberMiddleware; +use App\Web\Session; use App\Web\View; use DI\Bridge\Slim\Bridge; use DI\ContainerBuilder; @@ -58,6 +59,10 @@ if (!$config['debug']) { } $builder->addDefinitions([ + Session::class => factory(function () { + return new Session('xbackbone_session', BASE_DIR.'resources/sessions'); + }), + 'session' => get(Session::class), View::class => factory(function (Container $container) { return ViewFactory::createAppInstance($container); }), diff --git a/bootstrap/container.php b/bootstrap/container.php index fd23552..cb284f8 100644 --- a/bootstrap/container.php +++ b/bootstrap/container.php @@ -36,11 +36,6 @@ return [ }), 'logger' => get(Logger::class), - Session::class => factory(function () { - return new Session('xbackbone_session', BASE_DIR.'resources/sessions'); - }), - 'session' => get(Session::class), - DB::class => factory(function (Container $container) { $config = $container->get('config'); diff --git a/composer.json b/composer.json index a612a42..c13bf6e 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "sergix44/xbackbone", - "version": "3.1", + "version": "3.1.RC1", "description": "A lightweight ShareX PHP backend", "type": "project", "require": { @@ -21,8 +21,7 @@ "slim/slim": "^4.0", "spatie/flysystem-dropbox": "^1.0", "superbalist/flysystem-google-storage": "^7.2", - "twig/twig": "^2.12", - "ext-ldap": "*" + "twig/twig": "^2.12" }, "config": { "optimize-autoloader": true, diff --git a/composer.lock b/composer.lock index 31bae8d..bad4178 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "17be54724e0928ed7049b18bbcb181b9", + "content-hash": "979e04cb72a48a50d7872132f8efa29f", "packages": [ { "name": "aws/aws-sdk-php", - "version": "3.133.46", + "version": "3.134.3", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "98d359e61b365f3040ca61c66ae8883334cf5d74" + "reference": "3de2711a47e7c3f5e93a5c83f019188fd23f852f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/98d359e61b365f3040ca61c66ae8883334cf5d74", - "reference": "98d359e61b365f3040ca61c66ae8883334cf5d74", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/3de2711a47e7c3f5e93a5c83f019188fd23f852f", + "reference": "3de2711a47e7c3f5e93a5c83f019188fd23f852f", "shasum": "" }, "require": { @@ -88,7 +88,7 @@ "s3", "sdk" ], - "time": "2020-03-27T18:15:32+00:00" + "time": "2020-04-03T18:11:51+00:00" }, { "name": "firebase/php-jwt", @@ -255,16 +255,16 @@ }, { "name": "google/cloud-storage", - "version": "v1.19.0", + "version": "v1.20.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-cloud-php-storage.git", - "reference": "49dc19608ebea54023c2b3910e72b91393235d82" + "reference": "d09ef3d47079e4a3cac1419856302cc5c07c5e1f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/googleapis/google-cloud-php-storage/zipball/49dc19608ebea54023c2b3910e72b91393235d82", - "reference": "49dc19608ebea54023c2b3910e72b91393235d82", + "url": "https://api.github.com/repos/googleapis/google-cloud-php-storage/zipball/d09ef3d47079e4a3cac1419856302cc5c07c5e1f", + "reference": "d09ef3d47079e4a3cac1419856302cc5c07c5e1f", "shasum": "" }, "require": { @@ -302,7 +302,7 @@ "Apache-2.0" ], "description": "Cloud Storage Client for PHP", - "time": "2020-03-25T23:00:37+00:00" + "time": "2020-03-30T20:36:58+00:00" }, { "name": "google/crc32", @@ -765,6 +765,7 @@ "serialize", "tokenizer" ], + "abandoned": "opis/closure", "time": "2018-03-21T22:21:57+00:00" }, { diff --git a/config.example.php b/config.example.php index d9e4667..028501e 100644 --- a/config.example.php +++ b/config.example.php @@ -4,7 +4,7 @@ return [ 'base_url' => 'https://localhost', // no trailing slash 'db' => [ 'connection' => 'sqlite', - 'dsn' => 'resources/database/xbackbone.db', + 'dsn' => realpath(__DIR__).'/resources/database/xbackbone.db', 'username' => null, 'password' => null, ], diff --git a/docs/installation.md b/docs/installation.md index cf35d29..eaeaa3b 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -44,7 +44,7 @@ return [ ], 'db' => [ 'connection' => 'sqlite', // current support for sqlite and mysql - 'dsn' => 'resources/database/xbackbone.db', + 'dsn' => realpath(__DIR__).'/resources/database/xbackbone.db', // if sqlite, this must be an absolute path 'username' => null, // username and password not needed for sqlite 'password' => null, ] diff --git a/install/index.php b/install/index.php index 2538b7e..2024e44 100644 --- a/install/index.php +++ b/install/index.php @@ -29,7 +29,7 @@ $config = [ 'debug' => true, 'db' => [ 'connection' => 'sqlite', - 'dsn' => realpath(__DIR__.'/../').implode(DIRECTORY_SEPARATOR, ['resources', 'database', 'xbackbone.db']), + 'dsn' => realpath(__DIR__.'/../').DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, ['resources', 'database', 'xbackbone.db']), 'username' => null, 'password' => null, ], @@ -51,6 +51,10 @@ $builder->addDefinitions([ return ViewFactory::createInstallerInstance($container); }), 'view' => get(View::class), + Session::class => factory(function () { + return new Session('xbackbone_session'); + }), + 'session' => get(Session::class), ]); $builder->addDefinitions(__DIR__.'/../bootstrap/container.php'); @@ -95,7 +99,6 @@ $app->get('/', function (Response $response, View $view, Session $session) use ( })->setName('install'); $app->post('/', function (Request $request, Response $response, Filesystem $storage, Session $session, DB $db) use (&$config) { - // Check if there is a previous installation, if not, setup the config file $installed = true; @@ -180,13 +183,7 @@ $app->post('/', function (Request $request, Response $response, Filesystem $stor // Build the dns string and run the migrations try { - $firstMigrate = false; - if ($config['db']['connection'] === 'sqlite' && !file_exists(__DIR__.'/../'.$config['db']['dsn'])) { - touch(__DIR__.'/../'.$config['db']['dsn']); - $firstMigrate = true; - } - - $migrator = new Migrator($db, __DIR__.'/../resources/schemas', $firstMigrate); + $migrator = new Migrator($db, __DIR__.'/../resources/schemas'); $migrator->migrate(); $migrator->reSyncQuotas($storage); } catch (PDOException $e) { @@ -201,7 +198,7 @@ $app->post('/', function (Request $request, Response $response, Filesystem $stor } // re-apply the previous theme if is present - $css = $db->query('SELECT `value` FROM `settings` WHERE `key` = \'css\'')->fetch()->value; + $css = $db->query('SELECT `value` FROM `settings` WHERE `key` = \'css\'')->fetch()->value ?? null; if ($css) { $content = file_get_contents($css); if ($content !== false) { @@ -230,9 +227,7 @@ $app->post('/', function (Request $request, Response $response, Filesystem $stor cleanDirectory(__DIR__.'/../resources/cache'); cleanDirectory(__DIR__.'/../resources/sessions'); - if (!isset($config['debug']) || !$config['debug']) { - removeDirectory(__DIR__.'/../install'); - } + //removeDirectory(__DIR__.'/../install'); // Installed successfully, destroy the installer session $session->destroy(); diff --git a/resources/lang/it.lang.php b/resources/lang/it.lang.php index 5dac85b..e49bd71 100644 --- a/resources/lang/it.lang.php +++ b/resources/lang/it.lang.php @@ -157,7 +157,7 @@ Se non sei stato tu a richiedere la reimpostazione della password, ignora sempli 'recaptcha_site_key' => 'reCAPTCHA Site Key', 'recaptcha_secret_key' => 'reCAPTCHA Secret Key', 'no_tags' => 'Nessun tag aggiunto', - 'upload_max_file_size' => 'La dimensione massima del file è attualmente %s.', + 'upload_max_file_size' => 'La dimensione massima di un file caricabile è %s.', 'ldap_cant_connect' => 'Impossibile connettersi al server di autenticazione LDAP.', 'user_create_password' => 'Se lasciato vuoto, si consiglia di inviare una notifica all\'utente via e-mail.', 'mail.new_account_text_with_pw' => 'Ciao %s! diff --git a/resources/templates/upload/web.twig b/resources/templates/upload/web.twig index 4e248a2..7963f08 100644 --- a/resources/templates/upload/web.twig +++ b/resources/templates/upload/web.twig @@ -6,7 +6,7 @@ {% include 'comp/navbar.twig' %}
{% include 'comp/alert.twig' %} -
+
{{ lang('upload_max_file_size', [max_file_size]) }}
diff --git a/src/js/installer.js b/src/js/installer.js index f89c057..7a51a79 100644 --- a/src/js/installer.js +++ b/src/js/installer.js @@ -1,18 +1,19 @@ $(document).ready(function () { var $connection = $('#connection'); - var $all_database_options = $('.hook-database'); - var $all_database_inputs = $('.hook-database-input'); + var $allDatabaseOptions = $('.hook-database'); + var $allDatabaseInputs = $('.hook-database-input'); + var sqliteDSN = $('#dsn').val(); - var $storage_driver = $('#storage_driver'); - var $all_storage_inputs = $('.hook-storage-input'); - var $all_storage_options = $('.hook-storage'); + var $storageDriver = $('#storage_driver'); + var $allStorageInputs = $('.hook-storage-input'); + var $allStorageOptions = $('.hook-storage'); $connection.change(function () { - $all_database_options.hide(); - $all_database_inputs.prop('required', ''); + $allDatabaseOptions.hide(); + $allDatabaseInputs.prop('required', ''); switch ($(this).val()) { case 'sqlite': - $('#dsn').val('resources/database/xbackbone.db'); + $('#dsn').val(sqliteDSN); break; case 'mysql': $('#dsn').val('host=localhost;port=3306;dbname=xbackbone'); @@ -22,9 +23,9 @@ $(document).ready(function () { } }); - $storage_driver.change(function () { - $all_storage_options.hide(); - $all_storage_inputs.prop('required', ''); + $storageDriver.change(function () { + $allStorageOptions.hide(); + $allStorageInputs.prop('required', ''); switch ($(this).val()) { case 'local': $('#storage_path').val($('#storage_path').data('default-local')).prop('required', 'required').parent().parent().show(); @@ -56,8 +57,8 @@ $(document).ready(function () { } }); - $all_database_options.hide(); - $all_storage_options.hide(); - $storage_driver.trigger('change'); + $allDatabaseOptions.hide(); + $allStorageOptions.hide(); + $storageDriver.trigger('change'); $connection.trigger('change'); }); \ No newline at end of file