Add file uploads 5/...

This commit is contained in:
Visman 2023-07-08 00:21:08 +07:00
parent d15f66b849
commit dc7e1be75b
13 changed files with 209 additions and 2 deletions

View file

@ -0,0 +1,24 @@
<?php
/**
* This file is part of the ForkBB <https://github.com/forkbb>.
*
* @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
* @license The MIT License (MIT)
*/
declare(strict_types=1);
namespace ForkBB\Models\Attachment;
use ForkBB\Models\Manager;
use ForkBB\Models\User\User;
use RuntimeException;
class Attachments extends Manager
{
/**
* Ключ модели для контейнера
*/
protected string $cKey = 'Attachments';
}

View file

@ -345,7 +345,7 @@ class Groups extends Admin
}
/**
* Наводит порядок в расширениях
* Ограничивает макс. размер файла на основе \Core\Files
*/
public function vSizeCheck(Validator $v, int $size): int
{

View file

@ -684,6 +684,39 @@ class Install extends Admin
$this->c->DB->exec($query);
}
//attachments
$schema = [
'FIELDS' => [
'id' => ['SERIAL', false],
'uid' => ['INT(10) UNSIGNED', false, 0],
'created' => ['INT(10) UNSIGNED', false, 0],
'size_kb' => ['INT(10) UNSIGNED', false, 0],
'path' => ['VARCHAR(255)', false, ''],
],
'PRIMARY KEY' => ['id'],
'INDEXES' => [
'uid_idx' => ['uid'],
],
'ENGINE' => $this->DBEngine,
];
$this->c->DB->createTable('::attachments', $schema);
//attachments_pos
$schema = [
'FIELDS' => [
'id' => ['SERIAL', false],
'pid' => ['INT(10) UNSIGNED', false, 0],
],
'UNIQUE KEYS' => [
'id_pid_idx' => ['id', 'pid'],
],
'INDEXES' => [
'pid_idx' => ['pid'],
],
'ENGINE' => $this->DBEngine,
];
$this->c->DB->createTable('::attachments_pos', $schema);
// bans
$schema = [
'FIELDS' => [

View file

@ -619,6 +619,37 @@ class Update extends Admin
$this->c->DB->addField('::groups', 'g_up_size_kb', 'INT(10) UNSIGNED', false, 0);
$this->c->DB->addField('::groups', 'g_up_limit_mb', 'INT(10) UNSIGNED', false, 0);
//attachments
$schema = [
'FIELDS' => [
'id' => ['SERIAL', false],
'uid' => ['INT(10) UNSIGNED', false, 0],
'created' => ['INT(10) UNSIGNED', false, 0],
'size_kb' => ['INT(10) UNSIGNED', false, 0],
'path' => ['VARCHAR(255)', false, ''],
],
'PRIMARY KEY' => ['id'],
'INDEXES' => [
'uid_idx' => ['uid'],
],
];
$this->c->DB->createTable('::attachments', $schema);
//attachments_pos
$schema = [
'FIELDS' => [
'id' => ['SERIAL', false],
'pid' => ['INT(10) UNSIGNED', false, 0],
],
'UNIQUE KEYS' => [
'id_pid_idx' => ['id', 'pid'],
],
'INDEXES' => [
'pid_idx' => ['pid'],
],
];
$this->c->DB->createTable('::attachments_pos', $schema);
$coreConfig = new CoreConfig($this->configFile);
$coreConfig->add(
@ -627,6 +658,12 @@ class Update extends Admin
'AdminLogs'
);
$coreConfig->add(
'shared=>attachments',
'\\ForkBB\\Models\\Attachment\\Attachments::class',
'providerUser'
);
$coreConfig->save();
return null;

View file

@ -11,7 +11,7 @@ declare(strict_types=1);
namespace ForkBB\Models\Pages;
use ForkBB\Models\Model;
use function \ForkBB\__;
use function \ForkBB\{__, size};
trait PostFormTrait
{
@ -97,6 +97,26 @@ trait PostFormTrait
'fields' => $fieldset,
];
if ($this->userRules->useUpload) {
$autofocus = null;
$limit = $this->user->g_up_size_kb * 1024;
$form['enctype'] = 'multipart/form-data';
$form['hidden']['MAX_FILE_SIZE'] = $limit;
$form['sets']['uesm-attachments'] = [
'legend' => 'Attachments',
'fields' => [
'attachments[]' => [
'type' => 'file',
'help' => ['%1$s (%2$s max size)', \strtr($this->user->g_up_ext, [',' => ', ']), size($limit)],
'accept' => '.' . \strtr($this->user->g_up_ext, [',' => ',.']),
'multiple' => true,
]
],
];
}
$autofocus = null;
$fieldset = [];

View file

@ -103,6 +103,11 @@ trait PostValidatorTrait
{
$this->c->Lang->load('validator');
// обработка вложений + хак с добавление вложений в сообщение на лету
if (\is_string($attMessage = $this->attachmentsProc($marker, $args))) {
$_POST['message'] .= $attMessage;
}
$notPM = $this->fIndex !== self::FI_PM;
if ($this->user->isGuest) {
@ -259,4 +264,61 @@ trait PostValidatorTrait
return $enable;
}
/**
* Проверка вложений
*/
public function vCheckAttach(Validator $v, array $files): array
{
$exts = \array_flip(\explode(',', $this->user->g_up_ext));
$result = [];
foreach ($files as $file) {
if (isset($exts[$file->ext()])) {
$result[] = $file;
} else {
$v->addError(['The %s extension is not allowed', $file->ext()]);
}
}
return $result;
}
/**
* Обрабатывает загруженные файлы
*/
protected function attachmentsProc(string $marker, array $args): ?string
{
if (! $this->userRules->useUpload) {
return null;
}
$v = $this->c->Validator->reset()
->addValidators([
'check_attach' => [$this, 'vCheckAttach'],
])->addRules([
'token' => 'token:' . $marker,
'attachments' => "file:multiple|max:{$this->user->g_up_size_kb}|check_attach",
])->addAliases([
'attachments' => 'Attachments',
])->addArguments([
'token' => $args,
])->addMessages([
]);
if (! $v->validation($_FILES + $_POST)) {
$this->fIswev = $v->getErrors();
return null;
}
$attachments = $v->attachments;
$result = "\n";
foreach ($attachments as $a) {
$result .= ' ' . $a->name() . '.' . $a->ext();
}
return $result;
}
}

View file

@ -172,6 +172,7 @@ return [
],
],
'providerUser' => \ForkBB\Models\ProviderUser\ProviderUser::class,
'attachments' => \ForkBB\Models\Attachment\Attachments::class,
'Csrf' => [
'class' => \ForkBB\Core\Csrf::class,

View file

@ -77,3 +77,9 @@ msgstr "Create topic"
msgid "Re"
msgstr "Re: "
msgid "%1$s (%2$s max size)"
msgstr "%1$s (%2$s max size)"
msgid "Attachments"
msgstr "Attachments"

View file

@ -121,3 +121,9 @@ msgstr "Unsubscribe from email notifications of new posts"
msgid "Post list"
msgstr "Posts"
msgid "%1$s (%2$s max size)"
msgstr "%1$s (%2$s max size)"
msgid "Attachments"
msgstr "Attachments"

View file

@ -203,3 +203,6 @@ msgstr "The :alias field contains a link."
msgid "The :alias is not valid email"
msgstr "The :alias field contains an invalid email address."
msgid "The %s extension is not allowed"
msgstr "The .%s extension is not allowed."

View file

@ -77,3 +77,9 @@ msgstr "Создать тему"
msgid "Re"
msgstr "Re: "
msgid "%1$s (%2$s max size)"
msgstr "%1$s (макс. %2$s)"
msgid "Attachments"
msgstr "Вложения"

View file

@ -122,3 +122,9 @@ msgstr "Отказаться от получения уведомлений о
msgid "Post list"
msgstr "Сообщения"
msgid "%1$s (%2$s max size)"
msgstr "%1$s (макс. %2$s)"
msgid "Attachments"
msgstr "Вложения"

View file

@ -203,3 +203,6 @@ msgstr "Поле :alias содержит ссылку."
msgid "The :alias is not valid email"
msgstr "Поле :alias содержит неверный электронный адрес."
msgid "The %s extension is not allowed"
msgstr "Расширение .%s запрещено."