mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-04-20 02:44:56 +08:00
Compare commits
214 Commits
v24.10
...
developmen
Author | SHA1 | Date | |
---|---|---|---|
![]() |
fa566f156a | ||
![]() |
78a0a2f519 | ||
![]() |
42cbd6adef | ||
![]() |
6117349893 | ||
![]() |
1256320c72 | ||
![]() |
1ba0d26fdd | ||
![]() |
802f69cf35 | ||
![]() |
bb44334224 | ||
![]() |
9bfcadd95f | ||
![]() |
62c8eb3357 | ||
![]() |
c03e44124a | ||
![]() |
5c6671b3bf | ||
![]() |
abe7467ae5 | ||
![]() |
0ec0913846 | ||
![]() |
e980564fd6 | ||
![]() |
8a9215ecad | ||
![]() |
304a1d8f91 | ||
![]() |
dfbc78947f | ||
![]() |
4f5ad171ac | ||
![]() |
94b1cffa2d | ||
![]() |
13dae24cbe | ||
![]() |
6211d6bcfc | ||
![]() |
a384599cfa | ||
![]() |
dca14feaaa | ||
![]() |
d7ccb3ce6a | ||
![]() |
6548ea4a12 | ||
![]() |
c3a1fabbf0 | ||
![]() |
d2542d6265 | ||
![]() |
0e343c408f | ||
![]() |
5c78f8352e | ||
![]() |
35b45a2b8d | ||
![]() |
5050719ea3 | ||
![]() |
5508c171db | ||
![]() |
3b4d3430a5 | ||
![]() |
213a86e3c0 | ||
![]() |
2b746425c9 | ||
![]() |
5c15f4add2 | ||
![]() |
92ad81429f | ||
![]() |
f1b8e857bf | ||
![]() |
c291d27c19 | ||
![]() |
f4449928f8 | ||
![]() |
45a15b4792 | ||
![]() |
2291d78382 | ||
![]() |
7901ca9e6b | ||
![]() |
a7de251876 | ||
![]() |
7bd89316bc | ||
![]() |
b9306a9029 | ||
![]() |
a208c46b62 | ||
![]() |
a65701294e | ||
![]() |
69683d50ec | ||
![]() |
37d020c083 | ||
![]() |
ec79517493 | ||
![]() |
d938565839 | ||
![]() |
ccd94684eb | ||
![]() |
103a8a8e8e | ||
![]() |
c13ce18837 | ||
![]() |
7093daa49d | ||
![]() |
b897af2ed0 | ||
![]() |
d28278bba6 | ||
![]() |
12cc2f0689 | ||
![]() |
bf8a84a8b1 | ||
![]() |
4f5f7c10b1 | ||
![]() |
a34023f715 | ||
![]() |
b2ac3e0834 | ||
![]() |
5b0cb3dd50 | ||
![]() |
ac0cd9995d | ||
![]() |
7e03a973d8 | ||
![]() |
d89a2fdb15 | ||
![]() |
958b537a49 | ||
![]() |
8a66365d48 | ||
![]() |
04cca77ae6 | ||
![]() |
c091f67db3 | ||
![]() |
7f5fd16dc6 | ||
![]() |
0d1a237f81 | ||
![]() |
786a434c03 | ||
![]() |
25c4f4b02b | ||
![]() |
481580be17 | ||
![]() |
593645acfe | ||
![]() |
b9751807e7 | ||
![]() |
ee88832f1a | ||
![]() |
dbda82ef92 | ||
![]() |
ad8bc5fe21 | ||
![]() |
5bf75786c6 | ||
![]() |
cf9ccfcd5b | ||
![]() |
5116d83d38 | ||
![]() |
33b46882f3 | ||
![]() |
9a5c287470 | ||
![]() |
6effc6d262 | ||
![]() |
ff6c5aaecb | ||
![]() |
1ff2826678 | ||
![]() |
7e31725d48 | ||
![]() |
6d7ff59a89 | ||
![]() |
980a684b14 | ||
![]() |
d56eea9279 | ||
![]() |
2be504e0d2 | ||
![]() |
c84d999456 | ||
![]() |
01825ddb93 | ||
![]() |
1f88bc2a59 | ||
![]() |
ebe2ca7faf | ||
![]() |
f4005a139b | ||
![]() |
fca8f928a3 | ||
![]() |
ace8af077d | ||
![]() |
e50cd33277 | ||
![]() |
8486775edf | ||
![]() |
5887322178 | ||
![]() |
3f86937f74 | ||
![]() |
2f119d3033 | ||
![]() |
5f07f31c9f | ||
![]() |
a71aa241ad | ||
![]() |
97b201f61f | ||
![]() |
a8ef820443 | ||
![]() |
7e1a8e5ec6 | ||
![]() |
19ee1c9be7 | ||
![]() |
fcf0bf79a9 | ||
![]() |
0ece664475 | ||
![]() |
509af2463d | ||
![]() |
5632fef621 | ||
![]() |
8ec26e8083 | ||
![]() |
617b2edea0 | ||
![]() |
55d074f1a5 | ||
![]() |
7e6f6af463 | ||
![]() |
d00cf6e1ba | ||
![]() |
9fdd100f2d | ||
![]() |
57d8449660 | ||
![]() |
ebd4604f21 | ||
![]() |
36a4d79120 | ||
![]() |
f3fa63a5ae | ||
![]() |
5164375b18 | ||
![]() |
fec44452cb | ||
![]() |
18ab38a87b | ||
![]() |
0f9957bc03 | ||
![]() |
80f258c3c5 | ||
![]() |
90341e0e00 | ||
![]() |
3298374113 | ||
![]() |
227c5e155b | ||
![]() |
fdbbcf2b8a | ||
![]() |
0a07b0d162 | ||
![]() |
94165cc18f | ||
![]() |
f5ecd51461 | ||
![]() |
e9f906ce56 | ||
![]() |
4630f07282 | ||
![]() |
978acecdcf | ||
![]() |
bc1f1d92e5 | ||
![]() |
415cd6a360 | ||
![]() |
68ce340741 | ||
![]() |
bdca9fc1ce | ||
![]() |
edb684c72c | ||
![]() |
17f7afe12d | ||
![]() |
0a182a45ba | ||
![]() |
95d62e7f57 | ||
![]() |
9ecc91929a | ||
![]() |
f79c6aef8d | ||
![]() |
c0dff6d4a6 | ||
![]() |
59cfc087e1 | ||
![]() |
e2f6e50df4 | ||
![]() |
c2c64e207f | ||
![]() |
8645aeaa4a | ||
![]() |
7681e32dca | ||
![]() |
b7476a9e7f | ||
![]() |
306b8774c2 | ||
![]() |
c40ab4147e | ||
![]() |
48c101aa7a | ||
![]() |
378f0d595f | ||
![]() |
f12946d581 | ||
![]() |
d13e4d2eef | ||
![]() |
ac27e18933 | ||
![]() |
e5a6ccc4d4 | ||
![]() |
e42cdbe8e0 | ||
![]() |
a6ba8dd68f | ||
![]() |
7017a1cae5 | ||
![]() |
8120278b8c | ||
![]() |
73babcbfe3 | ||
![]() |
45189d9517 | ||
![]() |
7b84558ca1 | ||
![]() |
92cfde495e | ||
![]() |
14578c2257 | ||
![]() |
8f6f81948e | ||
![]() |
c6109c7087 | ||
![]() |
8ea3855e02 | ||
![]() |
74fce9640e | ||
![]() |
259aa829d4 | ||
![]() |
c4ec50d437 | ||
![]() |
b50b7b667d | ||
![]() |
fbeb2e23d4 | ||
![]() |
4b60c03caa | ||
![]() |
a56a28fbb7 | ||
![]() |
4051d5b803 | ||
![]() |
87242ce6cb | ||
![]() |
72d9ffd8b4 | ||
![]() |
f606711463 | ||
![]() |
d1f69feb4a | ||
![]() |
e4ca3bf132 | ||
![]() |
7aaf866064 | ||
![]() |
484342f26a | ||
![]() |
42ada66fdd | ||
![]() |
f732ef05d5 | ||
![]() |
4fb4fe0931 | ||
![]() |
06ffd8ee72 | ||
![]() |
90a8070518 | ||
![]() |
3e656efb00 | ||
![]() |
7c39dd5cba | ||
![]() |
21ccfa97dd | ||
![]() |
bf0262d7d1 | ||
![]() |
42b9700673 | ||
![]() |
42bd07d733 | ||
![]() |
6f1c54d018 | ||
![]() |
1930af91ce | ||
![]() |
e088d09e47 | ||
![]() |
209fa04752 | ||
![]() |
f41c02cbd7 | ||
![]() |
4dc75bad05 | ||
![]() |
a3d0f7478f | ||
![]() |
b9b5003239 | ||
![]() |
2e8d6ce7d9 |
@ -56,6 +56,7 @@ APP_PROXIES=null
|
||||
|
||||
# Database details
|
||||
# Host can contain a port (localhost:3306) or a separate DB_PORT option can be used.
|
||||
# An ipv6 address can be used via the square bracket format ([::1]).
|
||||
DB_HOST=localhost
|
||||
DB_PORT=3306
|
||||
DB_DATABASE=database_database
|
||||
|
35
.github/translators.txt
vendored
35
.github/translators.txt
vendored
@ -449,3 +449,38 @@ Avishay Rapp (AvishayRapp) :: Hebrew
|
||||
matthias4217 :: French
|
||||
Berke BOYLU2 (berkeboylu2) :: Turkish
|
||||
etwas7B :: German
|
||||
Mohammed srhiri (m.sghiri20) :: Arabic
|
||||
YongMin Kim (kym0118) :: Korean
|
||||
Rivo Zängov (Eraser) :: Estonian
|
||||
Francisco Rafael Fonseca (chicoraf) :: Portuguese, Brazilian
|
||||
ИEØ_ΙΙØZ (NEO_IIOZ) :: Chinese Traditional
|
||||
madnjpn (madnjpn.) :: Georgian
|
||||
Ásgeir Shiny Ásgeirsson (AsgeirShiny) :: Icelandic
|
||||
Mohammad Aftab Uddin (chirohorit) :: Bengali
|
||||
Yannis Karlaftis (meliseus) :: Greek
|
||||
felixxx :: German Informal
|
||||
randi (randi65535) :: Korean
|
||||
test65428 :: Greek
|
||||
zeronell :: Chinese Simplified
|
||||
julien Vinber (julienVinber) :: French
|
||||
Hyunwoo Park (oksure) :: Korean
|
||||
aram.rafeq.7 (aramrafeq2) :: Kurdish
|
||||
Raphael Moreno (RaphaelMoreno) :: Portuguese, Brazilian
|
||||
yn (user99) :: Arabic
|
||||
Pavel Zlatarov (pzlatarov) :: Bulgarian
|
||||
ingelres :: French
|
||||
mabdullah :: Arabic
|
||||
Skrabák Csaba (kekcsi) :: Hungarian
|
||||
Evert Meulie (Evert) :: Norwegian Bokmal
|
||||
Jasper Backer (jasperb) :: Dutch
|
||||
Alexandar Cavdarovski (ace.200112) :: Swedish
|
||||
구닥다리TV (yjj8353) :: Korean
|
||||
Onur Oskay (o.oskay) :: Turkish
|
||||
Sébastien Merveille (SebastienMerv) :: French
|
||||
Maxim Kouznetsov (masya.work) :: Hebrew
|
||||
neodvisnost :: Slovenian
|
||||
Soubi Agatsuma (bisouya) :: Hebrew
|
||||
Ilya Shaulov (ishaulov) :: Russian
|
||||
Konstantin Bobkov (b.konstantv) :: Russian
|
||||
Ruben Sutter (rubensutter) :: German
|
||||
jellium :: French
|
||||
|
4
.github/workflows/analyse-php.yml
vendored
4
.github/workflows/analyse-php.yml
vendored
@ -11,9 +11,9 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
if: ${{ github.ref != 'refs/heads/l10n_development' }}
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
|
6
.github/workflows/lint-php.yml
vendored
6
.github/workflows/lint-php.yml
vendored
@ -11,14 +11,14 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
if: ${{ github.ref != 'refs/heads/l10n_development' }}
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 8.1
|
||||
php-version: 8.3
|
||||
tools: phpcs
|
||||
|
||||
- name: Run formatting check
|
||||
|
6
.github/workflows/test-migrations.yml
vendored
6
.github/workflows/test-migrations.yml
vendored
@ -13,12 +13,12 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
if: ${{ github.ref != 'refs/heads/l10n_development' }}
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
matrix:
|
||||
php: ['8.1', '8.2', '8.3']
|
||||
php: ['8.2', '8.3', '8.4']
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
|
6
.github/workflows/test-php.yml
vendored
6
.github/workflows/test-php.yml
vendored
@ -13,12 +13,12 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
if: ${{ github.ref != 'refs/heads/l10n_development' }}
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
matrix:
|
||||
php: ['8.1', '8.2', '8.3']
|
||||
php: ['8.2', '8.3', '8.4']
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -32,3 +32,4 @@ webpack-stats.json
|
||||
phpstan.neon
|
||||
esbuild-meta.json
|
||||
.phpactor.json
|
||||
/*.zip
|
||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2024, Dan Brown and the BookStack Project contributors.
|
||||
Copyright (c) 2015-2025, Dan Brown and the BookStack project contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -8,27 +8,15 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ExternalBaseUserProvider implements UserProvider
|
||||
{
|
||||
/**
|
||||
* The user model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* LdapUserProvider constructor.
|
||||
*/
|
||||
public function __construct(string $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
public function __construct(
|
||||
protected string $model
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the model.
|
||||
*
|
||||
* @return Model
|
||||
*/
|
||||
public function createModel()
|
||||
public function createModel(): Model
|
||||
{
|
||||
$class = '\\' . ltrim($this->model, '\\');
|
||||
|
||||
@ -37,12 +25,8 @@ class ExternalBaseUserProvider implements UserProvider
|
||||
|
||||
/**
|
||||
* Retrieve a user by their unique identifier.
|
||||
*
|
||||
* @param mixed $identifier
|
||||
*
|
||||
* @return Authenticatable|null
|
||||
*/
|
||||
public function retrieveById($identifier)
|
||||
public function retrieveById(mixed $identifier): ?Authenticatable
|
||||
{
|
||||
return $this->createModel()->newQuery()->find($identifier);
|
||||
}
|
||||
@ -50,12 +34,9 @@ class ExternalBaseUserProvider implements UserProvider
|
||||
/**
|
||||
* Retrieve a user by their unique identifier and "remember me" token.
|
||||
*
|
||||
* @param mixed $identifier
|
||||
* @param string $token
|
||||
*
|
||||
* @return Authenticatable|null
|
||||
*/
|
||||
public function retrieveByToken($identifier, $token)
|
||||
public function retrieveByToken(mixed $identifier, $token): null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@ -75,12 +56,8 @@ class ExternalBaseUserProvider implements UserProvider
|
||||
|
||||
/**
|
||||
* Retrieve a user by the given credentials.
|
||||
*
|
||||
* @param array $credentials
|
||||
*
|
||||
* @return Authenticatable|null
|
||||
*/
|
||||
public function retrieveByCredentials(array $credentials)
|
||||
public function retrieveByCredentials(array $credentials): ?Authenticatable
|
||||
{
|
||||
// Search current user base by looking up a uid
|
||||
$model = $this->createModel();
|
||||
@ -92,15 +69,15 @@ class ExternalBaseUserProvider implements UserProvider
|
||||
|
||||
/**
|
||||
* Validate a user against the given credentials.
|
||||
*
|
||||
* @param Authenticatable $user
|
||||
* @param array $credentials
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateCredentials(Authenticatable $user, array $credentials)
|
||||
public function validateCredentials(Authenticatable $user, array $credentials): bool
|
||||
{
|
||||
// Should be done in the guard.
|
||||
return false;
|
||||
}
|
||||
|
||||
public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false)
|
||||
{
|
||||
// No action to perform, any passwords are external in the auth system
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ class Ldap
|
||||
*
|
||||
* @return \LDAP\Result|array|false
|
||||
*/
|
||||
public function search($ldapConnection, string $baseDn, string $filter, array $attributes = null)
|
||||
public function search($ldapConnection, string $baseDn, string $filter, array $attributes = [])
|
||||
{
|
||||
return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
|
||||
}
|
||||
@ -66,7 +66,7 @@ class Ldap
|
||||
*
|
||||
* @return \LDAP\Result|array|false
|
||||
*/
|
||||
public function read($ldapConnection, string $baseDn, string $filter, array $attributes = null)
|
||||
public function read($ldapConnection, string $baseDn, string $filter, array $attributes = [])
|
||||
{
|
||||
return ldap_read($ldapConnection, $baseDn, $filter, $attributes);
|
||||
}
|
||||
@ -87,7 +87,7 @@ class Ldap
|
||||
*
|
||||
* @param resource|\LDAP\Connection $ldapConnection
|
||||
*/
|
||||
public function searchAndGetEntries($ldapConnection, string $baseDn, string $filter, array $attributes = null): array|false
|
||||
public function searchAndGetEntries($ldapConnection, string $baseDn, string $filter, array $attributes = []): array|false
|
||||
{
|
||||
$search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
|
||||
|
||||
@ -99,7 +99,7 @@ class Ldap
|
||||
*
|
||||
* @param resource|\LDAP\Connection $ldapConnection
|
||||
*/
|
||||
public function bind($ldapConnection, string $bindRdn = null, string $bindPassword = null): bool
|
||||
public function bind($ldapConnection, ?string $bindRdn = null, ?string $bindPassword = null): bool
|
||||
{
|
||||
return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
|
||||
}
|
||||
|
@ -71,6 +71,26 @@ class LdapService
|
||||
return $users[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the user display name from the (potentially multiple) attributes defined by the configuration.
|
||||
*/
|
||||
protected function getUserDisplayName(array $userDetails, array $displayNameAttrs, string $defaultValue): string
|
||||
{
|
||||
$displayNameParts = [];
|
||||
foreach ($displayNameAttrs as $dnAttr) {
|
||||
$dnComponent = $this->getUserResponseProperty($userDetails, $dnAttr, null);
|
||||
if ($dnComponent) {
|
||||
$displayNameParts[] = $dnComponent;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($displayNameParts)) {
|
||||
return $defaultValue;
|
||||
}
|
||||
|
||||
return implode(' ', $displayNameParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the details of a user from LDAP using the given username.
|
||||
* User found via configurable user filter.
|
||||
@ -81,21 +101,25 @@ class LdapService
|
||||
{
|
||||
$idAttr = $this->config['id_attribute'];
|
||||
$emailAttr = $this->config['email_attribute'];
|
||||
$displayNameAttr = $this->config['display_name_attribute'];
|
||||
$displayNameAttrs = explode('|', $this->config['display_name_attribute']);
|
||||
$thumbnailAttr = $this->config['thumbnail_attribute'];
|
||||
|
||||
$user = $this->getUserWithAttributes($userName, array_filter([
|
||||
'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr,
|
||||
'cn', 'dn', $idAttr, $emailAttr, ...$displayNameAttrs, $thumbnailAttr,
|
||||
]));
|
||||
|
||||
if (is_null($user)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$userCn = $this->getUserResponseProperty($user, 'cn', null);
|
||||
$nameDefault = $this->getUserResponseProperty($user, 'cn', null);
|
||||
if (is_null($nameDefault)) {
|
||||
$nameDefault = ldap_explode_dn($user['dn'], 1)[0] ?? $user['dn'];
|
||||
}
|
||||
|
||||
$formatted = [
|
||||
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
|
||||
'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
|
||||
'name' => $this->getUserDisplayName($user, $displayNameAttrs, $nameDefault),
|
||||
'dn' => $user['dn'],
|
||||
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
|
||||
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,
|
||||
|
@ -5,6 +5,7 @@ namespace BookStack\Access;
|
||||
use BookStack\Access\Mfa\MfaSession;
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Exceptions\LoginAttemptException;
|
||||
use BookStack\Exceptions\LoginAttemptInvalidUserException;
|
||||
use BookStack\Exceptions\StoppedAuthenticationException;
|
||||
use BookStack\Facades\Activity;
|
||||
use BookStack\Facades\Theme;
|
||||
@ -29,10 +30,14 @@ class LoginService
|
||||
* a reason to (MFA or Unconfirmed Email).
|
||||
* Returns a boolean to indicate the current login result.
|
||||
*
|
||||
* @throws StoppedAuthenticationException
|
||||
* @throws StoppedAuthenticationException|LoginAttemptInvalidUserException
|
||||
*/
|
||||
public function login(User $user, string $method, bool $remember = false): void
|
||||
{
|
||||
if ($user->isGuest()) {
|
||||
throw new LoginAttemptInvalidUserException('Login not allowed for guest user');
|
||||
}
|
||||
|
||||
if ($this->awaitingEmailConfirmation($user) || $this->needsMfaVerification($user)) {
|
||||
$this->setLastLoginAttemptedForUser($user, $method, $remember);
|
||||
|
||||
@ -58,7 +63,7 @@ class LoginService
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function reattemptLoginFor(User $user)
|
||||
public function reattemptLoginFor(User $user): void
|
||||
{
|
||||
if ($user->id !== ($this->getLastLoginAttemptUser()->id ?? null)) {
|
||||
throw new Exception('Login reattempt user does align with current session state');
|
||||
@ -152,16 +157,40 @@ class LoginService
|
||||
*/
|
||||
public function attempt(array $credentials, string $method, bool $remember = false): bool
|
||||
{
|
||||
if ($this->areCredentialsForGuest($credentials)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = auth()->attempt($credentials, $remember);
|
||||
if ($result) {
|
||||
$user = auth()->user();
|
||||
auth()->logout();
|
||||
$this->login($user, $method, $remember);
|
||||
try {
|
||||
$this->login($user, $method, $remember);
|
||||
} catch (LoginAttemptInvalidUserException $e) {
|
||||
// Catch and return false for non-login accounts
|
||||
// so it looks like a normal invalid login.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given credentials are likely for the system guest account.
|
||||
*/
|
||||
protected function areCredentialsForGuest(array $credentials): bool
|
||||
{
|
||||
if (isset($credentials['email'])) {
|
||||
return User::query()->where('email', '=', $credentials['email'])
|
||||
->where('system_name', '=', 'public')
|
||||
->exists();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs the current user out of the application.
|
||||
* Returns an app post-redirect path.
|
||||
|
@ -11,7 +11,9 @@ class OidcUserinfoResponse implements ProvidesClaims
|
||||
|
||||
public function __construct(ResponseInterface $response, string $issuer, array $keys)
|
||||
{
|
||||
$contentType = $response->getHeader('Content-Type')[0];
|
||||
$contentTypeHeaderValue = $response->getHeader('Content-Type')[0] ?? '';
|
||||
$contentType = strtolower(trim(explode(';', $contentTypeHeaderValue, 2)[0]));
|
||||
|
||||
if ($contentType === 'application/json') {
|
||||
$this->claims = json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ class SocialDriverManager
|
||||
string $driverName,
|
||||
array $config,
|
||||
string $socialiteHandler,
|
||||
callable $configureForRedirect = null
|
||||
?callable $configureForRedirect = null
|
||||
) {
|
||||
$this->validDrivers[] = $driverName;
|
||||
config()->set('services.' . $driverName, $config);
|
||||
|
@ -67,6 +67,14 @@ class ActivityType
|
||||
const WEBHOOK_UPDATE = 'webhook_update';
|
||||
const WEBHOOK_DELETE = 'webhook_delete';
|
||||
|
||||
const IMPORT_CREATE = 'import_create';
|
||||
const IMPORT_RUN = 'import_run';
|
||||
const IMPORT_DELETE = 'import_delete';
|
||||
|
||||
const SORT_RULE_CREATE = 'sort_rule_create';
|
||||
const SORT_RULE_UPDATE = 'sort_rule_update';
|
||||
const SORT_RULE_DELETE = 'sort_rule_delete';
|
||||
|
||||
/**
|
||||
* Get all the possible values.
|
||||
*/
|
||||
|
@ -5,6 +5,7 @@ namespace BookStack\Activity\Controllers;
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Activity\Models\Activity;
|
||||
use BookStack\Http\Controller;
|
||||
use BookStack\Sorting\SortUrl;
|
||||
use BookStack\Util\SimpleListOptions;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@ -65,6 +66,7 @@ class AuditLogController extends Controller
|
||||
'filters' => $filters,
|
||||
'listOptions' => $listOptions,
|
||||
'activityTypes' => $types,
|
||||
'filterSortUrl' => new SortUrl('settings/audit', array_filter($request->except('page')))
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ class Comment extends Model implements Loggable
|
||||
use HasCreatorAndUpdater;
|
||||
|
||||
protected $fillable = ['parent_id'];
|
||||
protected $appends = ['created', 'updated'];
|
||||
|
||||
/**
|
||||
* Get the entity that this comment belongs to.
|
||||
@ -54,22 +53,6 @@ class Comment extends Model implements Loggable
|
||||
return $this->updated_at->timestamp > $this->created_at->timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get created date as a relative diff.
|
||||
*/
|
||||
public function getCreatedAttribute(): string
|
||||
{
|
||||
return $this->created_at->diffForHumans();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get updated date as a relative diff.
|
||||
*/
|
||||
public function getUpdatedAttribute(): string
|
||||
{
|
||||
return $this->updated_at->diffForHumans();
|
||||
}
|
||||
|
||||
public function logDescriptor(): string
|
||||
{
|
||||
return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->entity_type} (ID: {$this->entity_id})";
|
||||
|
@ -7,6 +7,7 @@ use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Permissions\PermissionApplicator;
|
||||
use BookStack\Users\Models\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
abstract class BaseNotificationHandler implements NotificationHandler
|
||||
{
|
||||
@ -36,7 +37,11 @@ abstract class BaseNotificationHandler implements NotificationHandler
|
||||
}
|
||||
|
||||
// Send the notification
|
||||
$user->notify(new $notification($detail, $initiator));
|
||||
try {
|
||||
$user->notify(new $notification($detail, $initiator));
|
||||
} catch (\Exception $exception) {
|
||||
Log::error("Failed to send email notification to user [id:{$user->id}] with error: {$exception->getMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace BookStack\Api;
|
||||
|
||||
use BookStack\Entities\Models\BookChild;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\Page;
|
||||
|
||||
class ApiEntityListFormatter
|
||||
{
|
||||
@ -20,8 +22,16 @@ class ApiEntityListFormatter
|
||||
* @var array<string|int, string|callable>
|
||||
*/
|
||||
protected array $fields = [
|
||||
'id', 'name', 'slug', 'book_id', 'chapter_id', 'draft',
|
||||
'template', 'priority', 'created_at', 'updated_at',
|
||||
'id',
|
||||
'name',
|
||||
'slug',
|
||||
'book_id',
|
||||
'chapter_id',
|
||||
'draft',
|
||||
'template',
|
||||
'priority',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
public function __construct(array $list)
|
||||
@ -62,6 +72,28 @@ class ApiEntityListFormatter
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Include parent book/chapter info in the formatted data.
|
||||
*/
|
||||
public function withParents(): self
|
||||
{
|
||||
$this->withField('book', function (Entity $entity) {
|
||||
if ($entity instanceof BookChild && $entity->book) {
|
||||
return $entity->book->only(['id', 'name', 'slug']);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
$this->withField('chapter', function (Entity $entity) {
|
||||
if ($entity instanceof Page && $entity->chapter) {
|
||||
return $entity->chapter->only(['id', 'name', 'slug']);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the data and return an array of formatted content.
|
||||
* @return array[]
|
||||
|
@ -42,4 +42,12 @@ class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the registration of Laravel's default email verification system
|
||||
*/
|
||||
protected function configureEmailVerification(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@ -85,5 +85,12 @@ class RouteServiceProvider extends ServiceProvider
|
||||
RateLimiter::for('public', function (Request $request) {
|
||||
return Limit::perMinute(10)->by($request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('exports', function (Request $request) {
|
||||
$user = user();
|
||||
$attempts = $user->isGuest() ? 4 : 10;
|
||||
$key = $user->isGuest() ? $request->ip() : $user->id;
|
||||
return Limit::perMinute($attempts)->by($key);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use BookStack\App\Model;
|
||||
use BookStack\Facades\Theme;
|
||||
use BookStack\Permissions\PermissionApplicator;
|
||||
use BookStack\Settings\SettingService;
|
||||
use BookStack\Users\Models\User;
|
||||
@ -42,9 +43,9 @@ function user(): User
|
||||
* Check if the current user has a permission. If an ownable element
|
||||
* is passed in the jointPermissions are checked against that particular item.
|
||||
*/
|
||||
function userCan(string $permission, Model $ownable = null): bool
|
||||
function userCan(string $permission, ?Model $ownable = null): bool
|
||||
{
|
||||
if ($ownable === null) {
|
||||
if (is_null($ownable)) {
|
||||
return user()->can($permission);
|
||||
}
|
||||
|
||||
@ -70,7 +71,7 @@ function userCanOnAny(string $action, string $entityClass = ''): bool
|
||||
*
|
||||
* @return mixed|SettingService
|
||||
*/
|
||||
function setting(string $key = null, $default = null)
|
||||
function setting(?string $key = null, mixed $default = null): mixed
|
||||
{
|
||||
$settingService = app()->make(SettingService::class);
|
||||
|
||||
@ -88,43 +89,10 @@ function setting(string $key = null, $default = null)
|
||||
*/
|
||||
function theme_path(string $path = ''): ?string
|
||||
{
|
||||
$theme = config('view.theme');
|
||||
|
||||
$theme = Theme::getTheme();
|
||||
if (!$theme) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a URL with multiple parameters for sorting purposes.
|
||||
* Works out the logic to set the correct sorting direction
|
||||
* Discards empty parameters and allows overriding.
|
||||
*/
|
||||
function sortUrl(string $path, array $data, array $overrideData = []): string
|
||||
{
|
||||
$queryStringSections = [];
|
||||
$queryData = array_merge($data, $overrideData);
|
||||
|
||||
// Change sorting direction is already sorted on current attribute
|
||||
if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
|
||||
$queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
|
||||
} elseif (isset($overrideData['sort'])) {
|
||||
$queryData['order'] = 'asc';
|
||||
}
|
||||
|
||||
foreach ($queryData as $name => $value) {
|
||||
$trimmedVal = trim($value);
|
||||
if ($trimmedVal === '') {
|
||||
continue;
|
||||
}
|
||||
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
||||
}
|
||||
|
||||
if (count($queryStringSections) === 0) {
|
||||
return url($path);
|
||||
}
|
||||
|
||||
return url($path . '?' . implode('&', $queryStringSections));
|
||||
}
|
||||
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Broadcasting configuration options.
|
||||
*
|
||||
* Changes to these config files are not supported by BookStack and may break upon updates.
|
||||
* Configuration should be altered via the `.env` file or environment variables.
|
||||
* Do not edit this file unless you're happy to maintain any changes yourself.
|
||||
*/
|
||||
|
||||
return [
|
||||
|
||||
// Default Broadcaster
|
||||
// This option controls the default broadcaster that will be used by the
|
||||
// framework when an event needs to be broadcast. This can be set to
|
||||
// any of the connections defined in the "connections" array below.
|
||||
'default' => 'null',
|
||||
|
||||
// Broadcast Connections
|
||||
// Here you may define all of the broadcast connections that will be used
|
||||
// to broadcast events to other systems or over websockets. Samples of
|
||||
// each available type of connection are provided inside this array.
|
||||
'connections' => [
|
||||
|
||||
// Default options removed since we don't use broadcasting.
|
||||
|
||||
'log' => [
|
||||
'driver' => 'log',
|
||||
],
|
||||
|
||||
'null' => [
|
||||
'driver' => 'null',
|
||||
],
|
||||
|
||||
],
|
||||
|
||||
];
|
@ -35,10 +35,6 @@ return [
|
||||
// Available caches stores
|
||||
'stores' => [
|
||||
|
||||
'apc' => [
|
||||
'driver' => 'apc',
|
||||
],
|
||||
|
||||
'array' => [
|
||||
'driver' => 'array',
|
||||
'serialize' => false,
|
||||
@ -49,6 +45,7 @@ return [
|
||||
'table' => 'cache',
|
||||
'connection' => null,
|
||||
'lock_connection' => null,
|
||||
'lock_table' => null,
|
||||
],
|
||||
|
||||
'file' => [
|
||||
|
@ -40,12 +40,16 @@ if (env('REDIS_SERVERS', false)) {
|
||||
|
||||
// MYSQL
|
||||
// Split out port from host if set
|
||||
$mysql_host = env('DB_HOST', 'localhost');
|
||||
$mysql_host_exploded = explode(':', $mysql_host);
|
||||
$mysql_port = env('DB_PORT', 3306);
|
||||
if (count($mysql_host_exploded) > 1) {
|
||||
$mysql_host = $mysql_host_exploded[0];
|
||||
$mysql_port = intval($mysql_host_exploded[1]);
|
||||
$mysqlHost = env('DB_HOST', 'localhost');
|
||||
$mysqlHostExploded = explode(':', $mysqlHost);
|
||||
$mysqlPort = env('DB_PORT', 3306);
|
||||
$mysqlHostIpv6 = str_starts_with($mysqlHost, '[');
|
||||
if ($mysqlHostIpv6 && str_contains($mysqlHost, ']:')) {
|
||||
$mysqlHost = implode(':', array_slice($mysqlHostExploded, 0, -1));
|
||||
$mysqlPort = intval(end($mysqlHostExploded));
|
||||
} else if (!$mysqlHostIpv6 && count($mysqlHostExploded) > 1) {
|
||||
$mysqlHost = $mysqlHostExploded[0];
|
||||
$mysqlPort = intval($mysqlHostExploded[1]);
|
||||
}
|
||||
|
||||
return [
|
||||
@ -61,12 +65,12 @@ return [
|
||||
'mysql' => [
|
||||
'driver' => 'mysql',
|
||||
'url' => env('DATABASE_URL'),
|
||||
'host' => $mysql_host,
|
||||
'host' => $mysqlHost,
|
||||
'database' => env('DB_DATABASE', 'forge'),
|
||||
'username' => env('DB_USERNAME', 'forge'),
|
||||
'password' => env('DB_PASSWORD', ''),
|
||||
'unix_socket' => env('DB_SOCKET', ''),
|
||||
'port' => $mysql_port,
|
||||
'port' => $mysqlPort,
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
// Prefixes are only semi-supported and may be unstable
|
||||
@ -88,7 +92,7 @@ return [
|
||||
'database' => 'bookstack-test',
|
||||
'username' => env('MYSQL_USER', 'bookstack-test'),
|
||||
'password' => env('MYSQL_PASSWORD', 'bookstack-test'),
|
||||
'port' => $mysql_port,
|
||||
'port' => $mysqlPort,
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => '',
|
||||
|
@ -114,6 +114,7 @@ return [
|
||||
* @var array
|
||||
*/
|
||||
'allowed_protocols' => [
|
||||
"data://" => ["rules" => []],
|
||||
'file://' => ['rules' => []],
|
||||
'http://' => ['rules' => []],
|
||||
'https://' => ['rules' => []],
|
||||
|
@ -33,12 +33,14 @@ return [
|
||||
'driver' => 'local',
|
||||
'root' => public_path(),
|
||||
'visibility' => 'public',
|
||||
'serve' => false,
|
||||
'throw' => true,
|
||||
],
|
||||
|
||||
'local_secure_attachments' => [
|
||||
'driver' => 'local',
|
||||
'root' => storage_path('uploads/files/'),
|
||||
'serve' => false,
|
||||
'throw' => true,
|
||||
],
|
||||
|
||||
@ -46,6 +48,7 @@ return [
|
||||
'driver' => 'local',
|
||||
'root' => storage_path('uploads/images/'),
|
||||
'visibility' => 'public',
|
||||
'serve' => false,
|
||||
'throw' => true,
|
||||
],
|
||||
|
||||
|
@ -38,7 +38,7 @@ return [
|
||||
'password' => env('MAIL_PASSWORD'),
|
||||
'verify_peer' => env('MAIL_VERIFY_SSL', true),
|
||||
'timeout' => null,
|
||||
'local_domain' => env('MAIL_EHLO_DOMAIN'),
|
||||
'local_domain' => null,
|
||||
'tls_required' => ($mailEncryption === 'tls' || $mailEncryption === 'ssl'),
|
||||
],
|
||||
|
||||
@ -64,12 +64,4 @@ return [
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
// Email markdown configuration
|
||||
'markdown' => [
|
||||
'theme' => 'default',
|
||||
'paths' => [
|
||||
resource_path('views/vendor/mail'),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
@ -23,6 +23,7 @@ return [
|
||||
|
||||
'database' => [
|
||||
'driver' => 'database',
|
||||
'connection' => null,
|
||||
'table' => 'jobs',
|
||||
'queue' => 'default',
|
||||
'retry_after' => 90,
|
||||
|
99
app/Console/Commands/AssignSortRuleCommand.php
Normal file
99
app/Console/Commands/AssignSortRuleCommand.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Console\Commands;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Sorting\BookSorter;
|
||||
use BookStack\Sorting\SortRule;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class AssignSortRuleCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'bookstack:assign-sort-rule
|
||||
{sort-rule=0: ID of the sort rule to apply}
|
||||
{--all-books : Apply to all books in the system}
|
||||
{--books-without-sort : Apply to only books without a sort rule already assigned}
|
||||
{--books-with-sort= : Apply to only books with the sort rule of given id}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Assign a sort rule to content in the system';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(BookSorter $sorter): int
|
||||
{
|
||||
$sortRuleId = intval($this->argument('sort-rule')) ?? 0;
|
||||
if ($sortRuleId === 0) {
|
||||
return $this->listSortRules();
|
||||
}
|
||||
|
||||
$rule = SortRule::query()->find($sortRuleId);
|
||||
if ($this->option('all-books')) {
|
||||
$query = Book::query();
|
||||
} else if ($this->option('books-without-sort')) {
|
||||
$query = Book::query()->whereNull('sort_rule_id');
|
||||
} else if ($this->option('books-with-sort')) {
|
||||
$sortId = intval($this->option('books-with-sort')) ?: 0;
|
||||
if (!$sortId) {
|
||||
$this->error("Provided --books-with-sort option value is invalid");
|
||||
return 1;
|
||||
}
|
||||
$query = Book::query()->where('sort_rule_id', $sortId);
|
||||
} else {
|
||||
$this->error("No option provided to specify target. Run with the -h option to see all available options.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!$rule) {
|
||||
$this->error("Sort rule of provided id {$sortRuleId} not found!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
$count = $query->clone()->count();
|
||||
$this->warn("This will apply sort rule [{$rule->id}: {$rule->name}] to {$count} book(s) and run the sort on each.");
|
||||
$confirmed = $this->confirm("Are you sure you want to continue?");
|
||||
|
||||
if (!$confirmed) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
$processed = 0;
|
||||
$query->chunkById(10, function ($books) use ($rule, $sorter, $count, &$processed) {
|
||||
$max = min($count, ($processed + 10));
|
||||
$this->info("Applying to {$processed}-{$max} of {$count} books");
|
||||
foreach ($books as $book) {
|
||||
$book->sort_rule_id = $rule->id;
|
||||
$book->save();
|
||||
$sorter->runBookAutoSort($book);
|
||||
}
|
||||
$processed = $max;
|
||||
});
|
||||
|
||||
$this->info("Sort applied to {$processed} book(s)!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function listSortRules(): int
|
||||
{
|
||||
|
||||
$rules = SortRule::query()->orderBy('id', 'asc')->get();
|
||||
$this->error("Sort rule ID required!");
|
||||
$this->warn("\nAvailable sort rules:");
|
||||
foreach ($rules as $rule) {
|
||||
$this->info("{$rule->id}: {$rule->name}");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
@ -49,6 +49,7 @@ class UpdateUrlCommand extends Command
|
||||
'chapters' => ['description_html'],
|
||||
'books' => ['description_html'],
|
||||
'bookshelves' => ['description_html'],
|
||||
'page_revisions' => ['html', 'text', 'markdown'],
|
||||
'images' => ['url'],
|
||||
'settings' => ['value'],
|
||||
'comments' => ['html', 'text'],
|
||||
@ -77,6 +78,12 @@ class UpdateUrlCommand extends Command
|
||||
$this->info('URL update procedure complete.');
|
||||
$this->info('============================================================================');
|
||||
$this->info('Be sure to run "php artisan cache:clear" to clear any old URLs in the cache.');
|
||||
|
||||
if (!str_starts_with($newUrl, url('/'))) {
|
||||
$this->warn('You still need to update your APP_URL env value. This is currently set to:');
|
||||
$this->warn(url('/'));
|
||||
}
|
||||
|
||||
$this->info('============================================================================');
|
||||
|
||||
return 0;
|
||||
|
@ -30,6 +30,7 @@ class BookApiController extends ApiController
|
||||
{
|
||||
$books = $this->queries
|
||||
->visibleForList()
|
||||
->with(['cover:id,name,url'])
|
||||
->addSelect(['created_by', 'updated_by']);
|
||||
|
||||
return $this->apiListingResponse($books, [
|
||||
|
@ -70,7 +70,7 @@ class BookController extends Controller
|
||||
/**
|
||||
* Show the form for creating a new book.
|
||||
*/
|
||||
public function create(string $shelfSlug = null)
|
||||
public function create(?string $shelfSlug = null)
|
||||
{
|
||||
$this->checkPermission('book-create-all');
|
||||
|
||||
@ -93,7 +93,7 @@ class BookController extends Controller
|
||||
* @throws ImageUploadException
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function store(Request $request, string $shelfSlug = null)
|
||||
public function store(Request $request, ?string $shelfSlug = null)
|
||||
{
|
||||
$this->checkPermission('book-create-all');
|
||||
$validated = $this->validate($request, [
|
||||
|
@ -26,6 +26,7 @@ class BookshelfApiController extends ApiController
|
||||
{
|
||||
$shelves = $this->queries
|
||||
->visibleForList()
|
||||
->with(['cover:id,name,url'])
|
||||
->addSelect(['created_by', 'updated_by']);
|
||||
|
||||
return $this->apiListingResponse($shelves, [
|
||||
|
@ -41,7 +41,7 @@ class PageController extends Controller
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(string $bookSlug, string $chapterSlug = null)
|
||||
public function create(string $bookSlug, ?string $chapterSlug = null)
|
||||
{
|
||||
if ($chapterSlug) {
|
||||
$parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
||||
@ -69,7 +69,7 @@ class PageController extends Controller
|
||||
*
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
|
||||
public function createAsGuest(Request $request, string $bookSlug, ?string $chapterSlug = null)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
|
@ -43,7 +43,6 @@ class PageRevisionController extends Controller
|
||||
->selectRaw("IF(markdown = '', false, true) as is_markdown")
|
||||
->with(['page.book', 'createdBy'])
|
||||
->reorder('id', $listOptions->getOrder())
|
||||
->reorder('created_at', $listOptions->getOrder())
|
||||
->paginate(50);
|
||||
|
||||
$this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
|
||||
@ -52,6 +51,7 @@ class PageRevisionController extends Controller
|
||||
'revisions' => $revisions,
|
||||
'page' => $page,
|
||||
'listOptions' => $listOptions,
|
||||
'oldestRevisionId' => $page->revisions()->min('id'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace BookStack\Entities\Models;
|
||||
|
||||
use BookStack\Sorting\SortRule;
|
||||
use BookStack\Uploads\Image;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@ -16,12 +17,14 @@ use Illuminate\Support\Collection;
|
||||
* @property string $description
|
||||
* @property int $image_id
|
||||
* @property ?int $default_template_id
|
||||
* @property ?int $sort_rule_id
|
||||
* @property Image|null $cover
|
||||
* @property \Illuminate\Database\Eloquent\Collection $chapters
|
||||
* @property \Illuminate\Database\Eloquent\Collection $pages
|
||||
* @property \Illuminate\Database\Eloquent\Collection $directPages
|
||||
* @property \Illuminate\Database\Eloquent\Collection $shelves
|
||||
* @property ?Page $defaultTemplate
|
||||
* @property ?SortRule $sortRule
|
||||
*/
|
||||
class Book extends Entity implements HasCoverImage
|
||||
{
|
||||
@ -82,6 +85,14 @@ class Book extends Entity implements HasCoverImage
|
||||
return $this->belongsTo(Page::class, 'default_template_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sort set assigned to this book, if existing.
|
||||
*/
|
||||
public function sortRule(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SortRule::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all pages within this book.
|
||||
*/
|
||||
|
@ -60,6 +60,7 @@ class Chapter extends BookChild
|
||||
|
||||
/**
|
||||
* Get the visible pages in this chapter.
|
||||
* @returns Collection<Page>
|
||||
*/
|
||||
public function getVisiblePages(): Collection
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ class QueryPopular
|
||||
) {
|
||||
}
|
||||
|
||||
public function run(int $count, int $page, array $filterModels = null): Collection
|
||||
public function run(int $count, int $page, array $filterModels): Collection
|
||||
{
|
||||
$query = $this->permissions
|
||||
->restrictEntityRelationQuery(View::query(), 'views', 'viewable_id', 'viewable_type')
|
||||
@ -26,7 +26,7 @@ class QueryPopular
|
||||
->groupBy('viewable_id', 'viewable_type')
|
||||
->orderBy('view_count', 'desc');
|
||||
|
||||
if ($filterModels) {
|
||||
if (!empty($filterModels)) {
|
||||
$query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels));
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ namespace BookStack\Entities\Repos;
|
||||
|
||||
use BookStack\Activity\TagRepo;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\BookChild;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\HasCoverImage;
|
||||
@ -12,6 +13,7 @@ use BookStack\Entities\Queries\PageQueries;
|
||||
use BookStack\Exceptions\ImageUploadException;
|
||||
use BookStack\References\ReferenceStore;
|
||||
use BookStack\References\ReferenceUpdater;
|
||||
use BookStack\Sorting\BookSorter;
|
||||
use BookStack\Uploads\ImageRepo;
|
||||
use BookStack\Util\HtmlDescriptionFilter;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
@ -24,6 +26,7 @@ class BaseRepo
|
||||
protected ReferenceUpdater $referenceUpdater,
|
||||
protected ReferenceStore $referenceStore,
|
||||
protected PageQueries $pageQueries,
|
||||
protected BookSorter $bookSorter,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -134,6 +137,18 @@ class BaseRepo
|
||||
$entity->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the parent of the given entity, if any auto sort actions are set for it.
|
||||
* Typical ran during create/update/insert events.
|
||||
*/
|
||||
public function sortParent(Entity $entity): void
|
||||
{
|
||||
if ($entity instanceof BookChild) {
|
||||
$book = $entity->book;
|
||||
$this->bookSorter->runBookAutoSort($book);
|
||||
}
|
||||
}
|
||||
|
||||
protected function updateDescription(Entity $entity, array $input): void
|
||||
{
|
||||
if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
|
||||
|
@ -8,6 +8,7 @@ use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Tools\TrashCan;
|
||||
use BookStack\Exceptions\ImageUploadException;
|
||||
use BookStack\Facades\Activity;
|
||||
use BookStack\Sorting\SortRule;
|
||||
use BookStack\Uploads\ImageRepo;
|
||||
use Exception;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
@ -33,6 +34,12 @@ class BookRepo
|
||||
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
|
||||
Activity::add(ActivityType::BOOK_CREATE, $book);
|
||||
|
||||
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
|
||||
if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
|
||||
$book->sort_rule_id = $defaultBookSortSetting;
|
||||
$book->save();
|
||||
}
|
||||
|
||||
return $book;
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,8 @@ class ChapterRepo
|
||||
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
|
||||
Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
|
||||
|
||||
$this->baseRepo->sortParent($chapter);
|
||||
|
||||
return $chapter;
|
||||
}
|
||||
|
||||
@ -50,6 +52,8 @@ class ChapterRepo
|
||||
|
||||
Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
|
||||
|
||||
$this->baseRepo->sortParent($chapter);
|
||||
|
||||
return $chapter;
|
||||
}
|
||||
|
||||
@ -88,6 +92,8 @@ class ChapterRepo
|
||||
$chapter->rebuildPermissions();
|
||||
Activity::add(ActivityType::CHAPTER_MOVE, $chapter);
|
||||
|
||||
$this->baseRepo->sortParent($chapter);
|
||||
|
||||
return $parent;
|
||||
}
|
||||
}
|
||||
|
@ -83,10 +83,22 @@ class PageRepo
|
||||
$draft->refresh();
|
||||
|
||||
Activity::add(ActivityType::PAGE_CREATE, $draft);
|
||||
$this->baseRepo->sortParent($draft);
|
||||
|
||||
return $draft;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly update the content for the given page from the provided input.
|
||||
* Used for direct content access in a way that performs required changes
|
||||
* (Search index & reference regen) without performing an official update.
|
||||
*/
|
||||
public function setContentFromInput(Page $page, array $input): void
|
||||
{
|
||||
$this->updateTemplateStatusAndContentFromInput($page, $input);
|
||||
$this->baseRepo->update($page, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a page in the system.
|
||||
*/
|
||||
@ -117,11 +129,12 @@ class PageRepo
|
||||
}
|
||||
|
||||
Activity::add(ActivityType::PAGE_UPDATE, $page);
|
||||
$this->baseRepo->sortParent($page);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input)
|
||||
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
|
||||
{
|
||||
if (isset($input['template']) && userCan('templates-manage')) {
|
||||
$page->template = ($input['template'] === 'true');
|
||||
@ -232,6 +245,8 @@ class PageRepo
|
||||
Activity::add(ActivityType::PAGE_RESTORE, $page);
|
||||
Activity::add(ActivityType::REVISION_RESTORE, $revision);
|
||||
|
||||
$this->baseRepo->sortParent($page);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
@ -261,6 +276,8 @@ class PageRepo
|
||||
|
||||
Activity::add(ActivityType::PAGE_MOVE, $page);
|
||||
|
||||
$this->baseRepo->sortParent($page);
|
||||
|
||||
return $parent;
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ class RevisionRepo
|
||||
/**
|
||||
* Store a new revision in the system for the given page.
|
||||
*/
|
||||
public function storeNewForPage(Page $page, string $summary = null): PageRevision
|
||||
public function storeNewForPage(Page $page, ?string $summary = null): PageRevision
|
||||
{
|
||||
$revision = new PageRevision();
|
||||
|
||||
|
@ -8,6 +8,8 @@ use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Queries\EntityQueries;
|
||||
use BookStack\Sorting\BookSortMap;
|
||||
use BookStack\Sorting\BookSortMapItem;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class BookContents
|
||||
@ -103,211 +105,4 @@ class BookContents
|
||||
|
||||
return $query->where('book_id', '=', $this->book->id)->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the books content using the given sort map.
|
||||
* Returns a list of books that were involved in the operation.
|
||||
*
|
||||
* @returns Book[]
|
||||
*/
|
||||
public function sortUsingMap(BookSortMap $sortMap): array
|
||||
{
|
||||
// Load models into map
|
||||
$modelMap = $this->loadModelsFromSortMap($sortMap);
|
||||
|
||||
// Sort our changes from our map to be chapters first
|
||||
// Since they need to be process to ensure book alignment for child page changes.
|
||||
$sortMapItems = $sortMap->all();
|
||||
usort($sortMapItems, function (BookSortMapItem $itemA, BookSortMapItem $itemB) {
|
||||
$aScore = $itemA->type === 'page' ? 2 : 1;
|
||||
$bScore = $itemB->type === 'page' ? 2 : 1;
|
||||
|
||||
return $aScore - $bScore;
|
||||
});
|
||||
|
||||
// Perform the sort
|
||||
foreach ($sortMapItems as $item) {
|
||||
$this->applySortUpdates($item, $modelMap);
|
||||
}
|
||||
|
||||
/** @var Book[] $booksInvolved */
|
||||
$booksInvolved = array_values(array_filter($modelMap, function (string $key) {
|
||||
return str_starts_with($key, 'book:');
|
||||
}, ARRAY_FILTER_USE_KEY));
|
||||
|
||||
// Update permissions of books involved
|
||||
foreach ($booksInvolved as $book) {
|
||||
$book->rebuildPermissions();
|
||||
}
|
||||
|
||||
return $booksInvolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Using the given sort map item, detect changes for the related model
|
||||
* and update it if required. Changes where permissions are lacking will
|
||||
* be skipped and not throw an error.
|
||||
*
|
||||
* @param array<string, Entity> $modelMap
|
||||
*/
|
||||
protected function applySortUpdates(BookSortMapItem $sortMapItem, array $modelMap): void
|
||||
{
|
||||
/** @var BookChild $model */
|
||||
$model = $modelMap[$sortMapItem->type . ':' . $sortMapItem->id] ?? null;
|
||||
if (!$model) {
|
||||
return;
|
||||
}
|
||||
|
||||
$priorityChanged = $model->priority !== $sortMapItem->sort;
|
||||
$bookChanged = $model->book_id !== $sortMapItem->parentBookId;
|
||||
$chapterChanged = ($model instanceof Page) && $model->chapter_id !== $sortMapItem->parentChapterId;
|
||||
|
||||
// Stop if there's no change
|
||||
if (!$priorityChanged && !$bookChanged && !$chapterChanged) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currentParentKey = 'book:' . $model->book_id;
|
||||
if ($model instanceof Page && $model->chapter_id) {
|
||||
$currentParentKey = 'chapter:' . $model->chapter_id;
|
||||
}
|
||||
|
||||
$currentParent = $modelMap[$currentParentKey] ?? null;
|
||||
/** @var Book $newBook */
|
||||
$newBook = $modelMap['book:' . $sortMapItem->parentBookId] ?? null;
|
||||
/** @var ?Chapter $newChapter */
|
||||
$newChapter = $sortMapItem->parentChapterId ? ($modelMap['chapter:' . $sortMapItem->parentChapterId] ?? null) : null;
|
||||
|
||||
if (!$this->isSortChangePermissible($sortMapItem, $model, $currentParent, $newBook, $newChapter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Action the required changes
|
||||
if ($bookChanged) {
|
||||
$model->changeBook($newBook->id);
|
||||
}
|
||||
|
||||
if ($model instanceof Page && $chapterChanged) {
|
||||
$model->chapter_id = $newChapter->id ?? 0;
|
||||
}
|
||||
|
||||
if ($priorityChanged) {
|
||||
$model->priority = $sortMapItem->sort;
|
||||
}
|
||||
|
||||
if ($chapterChanged || $priorityChanged) {
|
||||
$model->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permissions to apply the given sorting change.
|
||||
* Is quite complex since items can gain a different parent change. Acts as a:
|
||||
* - Update of old parent element (Change of content/order).
|
||||
* - Update of sorted/moved element.
|
||||
* - Deletion of element (Relative to parent upon move).
|
||||
* - Creation of element within parent (Upon move to new parent).
|
||||
*/
|
||||
protected function isSortChangePermissible(BookSortMapItem $sortMapItem, BookChild $model, ?Entity $currentParent, ?Entity $newBook, ?Entity $newChapter): bool
|
||||
{
|
||||
// Stop if we can't see the current parent or new book.
|
||||
if (!$currentParent || !$newBook) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hasNewParent = $newBook->id !== $model->book_id || ($model instanceof Page && $model->chapter_id !== ($sortMapItem->parentChapterId ?? 0));
|
||||
if ($model instanceof Chapter) {
|
||||
$hasPermission = userCan('book-update', $currentParent)
|
||||
&& userCan('book-update', $newBook)
|
||||
&& userCan('chapter-update', $model)
|
||||
&& (!$hasNewParent || userCan('chapter-create', $newBook))
|
||||
&& (!$hasNewParent || userCan('chapter-delete', $model));
|
||||
|
||||
if (!$hasPermission) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($model instanceof Page) {
|
||||
$parentPermission = ($currentParent instanceof Chapter) ? 'chapter-update' : 'book-update';
|
||||
$hasCurrentParentPermission = userCan($parentPermission, $currentParent);
|
||||
|
||||
// This needs to check if there was an intended chapter location in the original sort map
|
||||
// rather than inferring from the $newChapter since that variable may be null
|
||||
// due to other reasons (Visibility).
|
||||
$newParent = $sortMapItem->parentChapterId ? $newChapter : $newBook;
|
||||
if (!$newParent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hasPageEditPermission = userCan('page-update', $model);
|
||||
$newParentInRightLocation = ($newParent instanceof Book || ($newParent instanceof Chapter && $newParent->book_id === $newBook->id));
|
||||
$newParentPermission = ($newParent instanceof Chapter) ? 'chapter-update' : 'book-update';
|
||||
$hasNewParentPermission = userCan($newParentPermission, $newParent);
|
||||
|
||||
$hasDeletePermissionIfMoving = (!$hasNewParent || userCan('page-delete', $model));
|
||||
$hasCreatePermissionIfMoving = (!$hasNewParent || userCan('page-create', $newParent));
|
||||
|
||||
$hasPermission = $hasCurrentParentPermission
|
||||
&& $newParentInRightLocation
|
||||
&& $hasNewParentPermission
|
||||
&& $hasPageEditPermission
|
||||
&& $hasDeletePermissionIfMoving
|
||||
&& $hasCreatePermissionIfMoving;
|
||||
|
||||
if (!$hasPermission) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load models from the database into the given sort map.
|
||||
*
|
||||
* @return array<string, Entity>
|
||||
*/
|
||||
protected function loadModelsFromSortMap(BookSortMap $sortMap): array
|
||||
{
|
||||
$modelMap = [];
|
||||
$ids = [
|
||||
'chapter' => [],
|
||||
'page' => [],
|
||||
'book' => [],
|
||||
];
|
||||
|
||||
foreach ($sortMap->all() as $sortMapItem) {
|
||||
$ids[$sortMapItem->type][] = $sortMapItem->id;
|
||||
$ids['book'][] = $sortMapItem->parentBookId;
|
||||
if ($sortMapItem->parentChapterId) {
|
||||
$ids['chapter'][] = $sortMapItem->parentChapterId;
|
||||
}
|
||||
}
|
||||
|
||||
$pages = $this->queries->pages->visibleForList()->whereIn('id', array_unique($ids['page']))->get();
|
||||
/** @var Page $page */
|
||||
foreach ($pages as $page) {
|
||||
$modelMap['page:' . $page->id] = $page;
|
||||
$ids['book'][] = $page->book_id;
|
||||
if ($page->chapter_id) {
|
||||
$ids['chapter'][] = $page->chapter_id;
|
||||
}
|
||||
}
|
||||
|
||||
$chapters = $this->queries->chapters->visibleForList()->whereIn('id', array_unique($ids['chapter']))->get();
|
||||
/** @var Chapter $chapter */
|
||||
foreach ($chapters as $chapter) {
|
||||
$modelMap['chapter:' . $chapter->id] = $chapter;
|
||||
$ids['book'][] = $chapter->book_id;
|
||||
}
|
||||
|
||||
$books = $this->queries->books->visibleForList()->whereIn('id', array_unique($ids['book']))->get();
|
||||
/** @var Book $book */
|
||||
foreach ($books as $book) {
|
||||
$modelMap['book:' . $book->id] = $book;
|
||||
}
|
||||
|
||||
return $modelMap;
|
||||
}
|
||||
}
|
||||
|
@ -18,17 +18,12 @@ use Illuminate\Http\UploadedFile;
|
||||
|
||||
class Cloner
|
||||
{
|
||||
protected PageRepo $pageRepo;
|
||||
protected ChapterRepo $chapterRepo;
|
||||
protected BookRepo $bookRepo;
|
||||
protected ImageService $imageService;
|
||||
|
||||
public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
|
||||
{
|
||||
$this->pageRepo = $pageRepo;
|
||||
$this->chapterRepo = $chapterRepo;
|
||||
$this->bookRepo = $bookRepo;
|
||||
$this->imageService = $imageService;
|
||||
public function __construct(
|
||||
protected PageRepo $pageRepo,
|
||||
protected ChapterRepo $chapterRepo,
|
||||
protected BookRepo $bookRepo,
|
||||
protected ImageService $imageService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,10 +104,10 @@ class PageIncludeParser
|
||||
|
||||
if ($currentOffset < $tagStartOffset) {
|
||||
$previousText = substr($text, $currentOffset, $tagStartOffset - $currentOffset);
|
||||
$textNode->parentNode->insertBefore(new DOMText($previousText), $textNode);
|
||||
$textNode->parentNode->insertBefore($this->doc->createTextNode($previousText), $textNode);
|
||||
}
|
||||
|
||||
$node = $textNode->parentNode->insertBefore(new DOMText($tagOuterContent), $textNode);
|
||||
$node = $textNode->parentNode->insertBefore($this->doc->createTextNode($tagOuterContent), $textNode);
|
||||
$includeTags[] = new PageIncludeTag($tagInnerContent, $node);
|
||||
$currentOffset = $tagStartOffset + strlen($tagOuterContent);
|
||||
}
|
||||
|
7
app/Exceptions/LoginAttemptInvalidUserException.php
Normal file
7
app/Exceptions/LoginAttemptInvalidUserException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exceptions;
|
||||
|
||||
class LoginAttemptInvalidUserException extends LoginAttemptException
|
||||
{
|
||||
}
|
7
app/Exceptions/ZipExportException.php
Normal file
7
app/Exceptions/ZipExportException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exceptions;
|
||||
|
||||
class ZipExportException extends \Exception
|
||||
{
|
||||
}
|
13
app/Exceptions/ZipImportException.php
Normal file
13
app/Exceptions/ZipImportException.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exceptions;
|
||||
|
||||
class ZipImportException extends \Exception
|
||||
{
|
||||
public function __construct(
|
||||
public array $errors
|
||||
) {
|
||||
$message = "Import failed with errors:" . implode("\n", $this->errors);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
12
app/Exceptions/ZipValidationException.php
Normal file
12
app/Exceptions/ZipValidationException.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exceptions;
|
||||
|
||||
class ZipValidationException extends \Exception
|
||||
{
|
||||
public function __construct(
|
||||
public array $errors
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Entities\Queries\BookQueries;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Exports\ExportFormatter;
|
||||
use BookStack\Http\ApiController;
|
||||
use Throwable;
|
||||
|
@ -1,9 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Entities\Queries\BookQueries;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Exceptions\NotFoundException;
|
||||
use BookStack\Exports\ExportFormatter;
|
||||
use BookStack\Exports\ZipExports\ZipExportBuilder;
|
||||
use BookStack\Http\Controller;
|
||||
use Throwable;
|
||||
|
||||
@ -14,6 +16,7 @@ class BookExportController extends Controller
|
||||
protected ExportFormatter $exportFormatter,
|
||||
) {
|
||||
$this->middleware('can:content-export');
|
||||
$this->middleware('throttle:exports');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,4 +66,16 @@ class BookExportController extends Controller
|
||||
|
||||
return $this->download()->directly($textContent, $bookSlug . '.md');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a book to a contained ZIP export file.
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function zip(string $bookSlug, ZipExportBuilder $builder)
|
||||
{
|
||||
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
||||
$zip = $builder->buildForBook($book);
|
||||
|
||||
return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', true);
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Entities\Queries\ChapterQueries;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Exports\ExportFormatter;
|
||||
use BookStack\Http\ApiController;
|
||||
use Throwable;
|
||||
|
@ -1,10 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Entities\Queries\ChapterQueries;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Exceptions\NotFoundException;
|
||||
use BookStack\Exports\ExportFormatter;
|
||||
use BookStack\Exports\ZipExports\ZipExportBuilder;
|
||||
use BookStack\Http\Controller;
|
||||
use Throwable;
|
||||
|
||||
@ -15,6 +16,7 @@ class ChapterExportController extends Controller
|
||||
protected ExportFormatter $exportFormatter,
|
||||
) {
|
||||
$this->middleware('can:content-export');
|
||||
$this->middleware('throttle:exports');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,4 +72,16 @@ class ChapterExportController extends Controller
|
||||
|
||||
return $this->download()->directly($chapterText, $chapterSlug . '.md');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a book to a contained ZIP export file.
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
|
||||
{
|
||||
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
||||
$zip = $builder->buildForChapter($chapter);
|
||||
|
||||
return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', true);
|
||||
}
|
||||
}
|
110
app/Exports/Controllers/ImportController.php
Normal file
110
app/Exports/Controllers/ImportController.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Exceptions\ZipImportException;
|
||||
use BookStack\Exceptions\ZipValidationException;
|
||||
use BookStack\Exports\ImportRepo;
|
||||
use BookStack\Http\Controller;
|
||||
use BookStack\Uploads\AttachmentService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ImportController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected ImportRepo $imports,
|
||||
) {
|
||||
$this->middleware('can:content-import');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the view to start a new import, and also list out the existing
|
||||
* in progress imports that are visible to the user.
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$imports = $this->imports->getVisibleImports();
|
||||
|
||||
$this->setPageTitle(trans('entities.import'));
|
||||
|
||||
return view('exports.import', [
|
||||
'imports' => $imports,
|
||||
'zipErrors' => session()->pull('validation_errors') ?? [],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload, validate and store an import file.
|
||||
*/
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'file' => ['required', ...AttachmentService::getFileValidationRules()]
|
||||
]);
|
||||
|
||||
$file = $request->file('file');
|
||||
try {
|
||||
$import = $this->imports->storeFromUpload($file);
|
||||
} catch (ZipValidationException $exception) {
|
||||
return redirect('/import')->with('validation_errors', $exception->errors);
|
||||
}
|
||||
|
||||
return redirect($import->getUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a pending import, with a form to allow progressing
|
||||
* with the import process.
|
||||
*/
|
||||
public function show(int $id)
|
||||
{
|
||||
$import = $this->imports->findVisible($id);
|
||||
|
||||
$this->setPageTitle(trans('entities.import_continue'));
|
||||
|
||||
return view('exports.import-show', [
|
||||
'import' => $import,
|
||||
'data' => $import->decodeMetadata(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the import process against an uploaded import ZIP.
|
||||
*/
|
||||
public function run(int $id, Request $request)
|
||||
{
|
||||
$import = $this->imports->findVisible($id);
|
||||
$parent = null;
|
||||
|
||||
if ($import->type === 'page' || $import->type === 'chapter') {
|
||||
session()->setPreviousUrl($import->getUrl());
|
||||
$data = $this->validate($request, [
|
||||
'parent' => ['required', 'string'],
|
||||
]);
|
||||
$parent = $data['parent'];
|
||||
}
|
||||
|
||||
try {
|
||||
$entity = $this->imports->runImport($import, $parent);
|
||||
} catch (ZipImportException $exception) {
|
||||
session()->flush();
|
||||
$this->showErrorNotification(trans('errors.import_zip_failed_notification'));
|
||||
return redirect($import->getUrl())->with('import_errors', $exception->errors);
|
||||
}
|
||||
|
||||
return redirect($entity->getUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an active pending import from the filesystem and database.
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
$import = $this->imports->findVisible($id);
|
||||
$this->imports->deleteImport($import);
|
||||
|
||||
return redirect('/import');
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Entities\Queries\PageQueries;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Exports\ExportFormatter;
|
||||
use BookStack\Http\ApiController;
|
||||
use Throwable;
|
||||
|
@ -1,11 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Exports\Controllers;
|
||||
|
||||
use BookStack\Entities\Queries\PageQueries;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Entities\Tools\PageContent;
|
||||
use BookStack\Exceptions\NotFoundException;
|
||||
use BookStack\Exports\ExportFormatter;
|
||||
use BookStack\Exports\ZipExports\ZipExportBuilder;
|
||||
use BookStack\Http\Controller;
|
||||
use Throwable;
|
||||
|
||||
@ -16,6 +17,7 @@ class PageExportController extends Controller
|
||||
protected ExportFormatter $exportFormatter,
|
||||
) {
|
||||
$this->middleware('can:content-export');
|
||||
$this->middleware('throttle:exports');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,4 +76,16 @@ class PageExportController extends Controller
|
||||
|
||||
return $this->download()->directly($pageText, $pageSlug . '.md');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a page to a contained ZIP export file.
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
|
||||
{
|
||||
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
|
||||
$zip = $builder->buildForPage($page);
|
||||
|
||||
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', true);
|
||||
}
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Tools;
|
||||
namespace BookStack\Exports;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Tools\BookContents;
|
||||
use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
|
||||
use BookStack\Entities\Tools\PageContent;
|
||||
use BookStack\Uploads\ImageService;
|
||||
use BookStack\Util\CspService;
|
||||
use BookStack\Util\HtmlDocument;
|
||||
@ -315,7 +317,12 @@ class ExportFormatter
|
||||
public function chapterToMarkdown(Chapter $chapter): string
|
||||
{
|
||||
$text = '# ' . $chapter->name . "\n\n";
|
||||
$text .= $chapter->description . "\n\n";
|
||||
|
||||
$description = (new HtmlToMarkdown($chapter->descriptionHtml()))->convert();
|
||||
if ($description) {
|
||||
$text .= $description . "\n\n";
|
||||
}
|
||||
|
||||
foreach ($chapter->pages as $page) {
|
||||
$text .= $this->pageToMarkdown($page) . "\n\n";
|
||||
}
|
||||
@ -330,6 +337,12 @@ class ExportFormatter
|
||||
{
|
||||
$bookTree = (new BookContents($book))->getTree(false, true);
|
||||
$text = '# ' . $book->name . "\n\n";
|
||||
|
||||
$description = (new HtmlToMarkdown($book->descriptionHtml()))->convert();
|
||||
if ($description) {
|
||||
$text .= $description . "\n\n";
|
||||
}
|
||||
|
||||
foreach ($bookTree as $bookChild) {
|
||||
if ($bookChild instanceof Chapter) {
|
||||
$text .= $this->chapterToMarkdown($bookChild) . "\n\n";
|
66
app/Exports/Import.php
Normal file
66
app/Exports/Import.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports;
|
||||
|
||||
use BookStack\Activity\Models\Loggable;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use BookStack\Users\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $path
|
||||
* @property string $name
|
||||
* @property int $size - ZIP size in bytes
|
||||
* @property string $type
|
||||
* @property string $metadata
|
||||
* @property int $created_by
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
* @property User $createdBy
|
||||
*/
|
||||
class Import extends Model implements Loggable
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public function getSizeString(): string
|
||||
{
|
||||
$mb = round($this->size / 1000000, 2);
|
||||
return "{$mb} MB";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL to view/continue this import.
|
||||
*/
|
||||
public function getUrl(string $path = ''): string
|
||||
{
|
||||
$path = ltrim($path, '/');
|
||||
return url("/import/{$this->id}" . ($path ? '/' . $path : ''));
|
||||
}
|
||||
|
||||
public function logDescriptor(): string
|
||||
{
|
||||
return "({$this->id}) {$this->name}";
|
||||
}
|
||||
|
||||
public function createdBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function decodeMetadata(): ZipExportBook|ZipExportChapter|ZipExportPage|null
|
||||
{
|
||||
$metadataArray = json_decode($this->metadata, true);
|
||||
return match ($this->type) {
|
||||
'book' => ZipExportBook::fromArray($metadataArray),
|
||||
'chapter' => ZipExportChapter::fromArray($metadataArray),
|
||||
'page' => ZipExportPage::fromArray($metadataArray),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
137
app/Exports/ImportRepo.php
Normal file
137
app/Exports/ImportRepo.php
Normal file
@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Queries\EntityQueries;
|
||||
use BookStack\Exceptions\FileUploadException;
|
||||
use BookStack\Exceptions\ZipExportException;
|
||||
use BookStack\Exceptions\ZipImportException;
|
||||
use BookStack\Exceptions\ZipValidationException;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use BookStack\Exports\ZipExports\ZipExportReader;
|
||||
use BookStack\Exports\ZipExports\ZipExportValidator;
|
||||
use BookStack\Exports\ZipExports\ZipImportRunner;
|
||||
use BookStack\Facades\Activity;
|
||||
use BookStack\Uploads\FileStorage;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
class ImportRepo
|
||||
{
|
||||
public function __construct(
|
||||
protected FileStorage $storage,
|
||||
protected ZipImportRunner $importer,
|
||||
protected EntityQueries $entityQueries,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<Import>
|
||||
*/
|
||||
public function getVisibleImports(): Collection
|
||||
{
|
||||
$query = Import::query();
|
||||
|
||||
if (!userCan('settings-manage')) {
|
||||
$query->where('created_by', user()->id);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public function findVisible(int $id): Import
|
||||
{
|
||||
$query = Import::query();
|
||||
|
||||
if (!userCan('settings-manage')) {
|
||||
$query->where('created_by', user()->id);
|
||||
}
|
||||
|
||||
return $query->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FileUploadException
|
||||
* @throws ZipValidationException
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
public function storeFromUpload(UploadedFile $file): Import
|
||||
{
|
||||
$zipPath = $file->getRealPath();
|
||||
$reader = new ZipExportReader($zipPath);
|
||||
|
||||
$errors = (new ZipExportValidator($reader))->validate();
|
||||
if ($errors) {
|
||||
throw new ZipValidationException($errors);
|
||||
}
|
||||
|
||||
$exportModel = $reader->decodeDataToExportModel();
|
||||
|
||||
$import = new Import();
|
||||
$import->type = match (get_class($exportModel)) {
|
||||
ZipExportPage::class => 'page',
|
||||
ZipExportChapter::class => 'chapter',
|
||||
ZipExportBook::class => 'book',
|
||||
};
|
||||
|
||||
$import->name = $exportModel->name;
|
||||
$import->created_by = user()->id;
|
||||
$import->size = filesize($zipPath);
|
||||
|
||||
$exportModel->metadataOnly();
|
||||
$import->metadata = json_encode($exportModel);
|
||||
|
||||
$path = $this->storage->uploadFile(
|
||||
$file,
|
||||
'uploads/files/imports/',
|
||||
'',
|
||||
'zip'
|
||||
);
|
||||
|
||||
$import->path = $path;
|
||||
$import->save();
|
||||
|
||||
Activity::add(ActivityType::IMPORT_CREATE, $import);
|
||||
|
||||
return $import;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipImportException
|
||||
*/
|
||||
public function runImport(Import $import, ?string $parent = null): Entity
|
||||
{
|
||||
$parentModel = null;
|
||||
if ($import->type === 'page' || $import->type === 'chapter') {
|
||||
$parentModel = $parent ? $this->entityQueries->findVisibleByStringIdentifier($parent) : null;
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$model = $this->importer->run($import, $parentModel);
|
||||
} catch (ZipImportException $e) {
|
||||
DB::rollBack();
|
||||
$this->importer->revertStoredFiles();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
$this->deleteImport($import);
|
||||
Activity::add(ActivityType::IMPORT_RUN, $import);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function deleteImport(Import $import): void
|
||||
{
|
||||
$this->storage->delete($import->path);
|
||||
$import->delete();
|
||||
|
||||
Activity::add(ActivityType::IMPORT_DELETE, $import);
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Tools;
|
||||
namespace BookStack\Exports;
|
||||
|
||||
use BookStack\Exceptions\PdfExportException;
|
||||
use Knp\Snappy\Pdf as SnappyPdf;
|
||||
use Dompdf\Dompdf;
|
||||
use Knp\Snappy\Pdf as SnappyPdf;
|
||||
use Symfony\Component\Process\Exception\ProcessTimedOutException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
@ -90,18 +90,28 @@ class PdfGenerator
|
||||
$process = Process::fromShellCommandline($command);
|
||||
$process->setTimeout($timeout);
|
||||
|
||||
$cleanup = function () use ($inputHtml, $outputPdf) {
|
||||
foreach ([$inputHtml, $outputPdf] as $file) {
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
$process->run();
|
||||
} catch (ProcessTimedOutException $e) {
|
||||
$cleanup();
|
||||
throw new PdfExportException("PDF Export via command failed due to timeout at {$timeout} second(s)");
|
||||
}
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
$cleanup();
|
||||
throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
|
||||
}
|
||||
|
||||
$pdfContents = file_get_contents($outputPdf);
|
||||
unlink($outputPdf);
|
||||
$cleanup();
|
||||
|
||||
if ($pdfContents === false) {
|
||||
throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");
|
66
app/Exports/ZipExports/Models/ZipExportAttachment.php
Normal file
66
app/Exports/ZipExports/Models/ZipExportAttachment.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Exports\ZipExports\ZipExportFiles;
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
use BookStack\Uploads\Attachment;
|
||||
|
||||
class ZipExportAttachment extends ZipExportModel
|
||||
{
|
||||
public ?int $id = null;
|
||||
public string $name;
|
||||
public ?string $link = null;
|
||||
public ?string $file = null;
|
||||
|
||||
public function metadataOnly(): void
|
||||
{
|
||||
$this->link = $this->file = null;
|
||||
}
|
||||
|
||||
public static function fromModel(Attachment $model, ZipExportFiles $files): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->id = $model->id;
|
||||
$instance->name = $model->name;
|
||||
|
||||
if ($model->external) {
|
||||
$instance->link = $model->path;
|
||||
} else {
|
||||
$instance->file = $files->referenceForAttachment($model);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromModelArray(array $attachmentArray, ZipExportFiles $files): array
|
||||
{
|
||||
return array_values(array_map(function (Attachment $attachment) use ($files) {
|
||||
return self::fromModel($attachment, $files);
|
||||
}, $attachmentArray));
|
||||
}
|
||||
|
||||
public static function validate(ZipValidationHelper $context, array $data): array
|
||||
{
|
||||
$rules = [
|
||||
'id' => ['nullable', 'int', $context->uniqueIdRule('attachment')],
|
||||
'name' => ['required', 'string', 'min:1'],
|
||||
'link' => ['required_without:file', 'nullable', 'string'],
|
||||
'file' => ['required_without:link', 'nullable', 'string', $context->fileReferenceRule()],
|
||||
];
|
||||
|
||||
return $context->validateData($data, $rules);
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$model = new self();
|
||||
|
||||
$model->id = $data['id'] ?? null;
|
||||
$model->name = $data['name'];
|
||||
$model->link = $data['link'] ?? null;
|
||||
$model->file = $data['file'] ?? null;
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
118
app/Exports/ZipExports/Models/ZipExportBook.php
Normal file
118
app/Exports/ZipExports/Models/ZipExportBook.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Exports\ZipExports\ZipExportFiles;
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
|
||||
class ZipExportBook extends ZipExportModel
|
||||
{
|
||||
public ?int $id = null;
|
||||
public string $name;
|
||||
public ?string $description_html = null;
|
||||
public ?string $cover = null;
|
||||
/** @var ZipExportChapter[] */
|
||||
public array $chapters = [];
|
||||
/** @var ZipExportPage[] */
|
||||
public array $pages = [];
|
||||
/** @var ZipExportTag[] */
|
||||
public array $tags = [];
|
||||
|
||||
public function metadataOnly(): void
|
||||
{
|
||||
$this->description_html = $this->cover = null;
|
||||
|
||||
foreach ($this->chapters as $chapter) {
|
||||
$chapter->metadataOnly();
|
||||
}
|
||||
foreach ($this->pages as $page) {
|
||||
$page->metadataOnly();
|
||||
}
|
||||
foreach ($this->tags as $tag) {
|
||||
$tag->metadataOnly();
|
||||
}
|
||||
}
|
||||
|
||||
public function children(): array
|
||||
{
|
||||
$children = [
|
||||
...$this->pages,
|
||||
...$this->chapters,
|
||||
];
|
||||
|
||||
usort($children, function ($a, $b) {
|
||||
return ($a->priority ?? 0) - ($b->priority ?? 0);
|
||||
});
|
||||
|
||||
return $children;
|
||||
}
|
||||
|
||||
public static function fromModel(Book $model, ZipExportFiles $files): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->id = $model->id;
|
||||
$instance->name = $model->name;
|
||||
$instance->description_html = $model->descriptionHtml();
|
||||
|
||||
if ($model->cover) {
|
||||
$instance->cover = $files->referenceForImage($model->cover);
|
||||
}
|
||||
|
||||
$instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
|
||||
|
||||
$chapters = [];
|
||||
$pages = [];
|
||||
|
||||
$children = $model->getDirectVisibleChildren()->all();
|
||||
foreach ($children as $child) {
|
||||
if ($child instanceof Chapter) {
|
||||
$chapters[] = $child;
|
||||
} else if ($child instanceof Page && !$child->draft) {
|
||||
$pages[] = $child;
|
||||
}
|
||||
}
|
||||
|
||||
$instance->pages = ZipExportPage::fromModelArray($pages, $files);
|
||||
$instance->chapters = ZipExportChapter::fromModelArray($chapters, $files);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function validate(ZipValidationHelper $context, array $data): array
|
||||
{
|
||||
$rules = [
|
||||
'id' => ['nullable', 'int', $context->uniqueIdRule('book')],
|
||||
'name' => ['required', 'string', 'min:1'],
|
||||
'description_html' => ['nullable', 'string'],
|
||||
'cover' => ['nullable', 'string', $context->fileReferenceRule()],
|
||||
'tags' => ['array'],
|
||||
'pages' => ['array'],
|
||||
'chapters' => ['array'],
|
||||
];
|
||||
|
||||
$errors = $context->validateData($data, $rules);
|
||||
$errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
|
||||
$errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
|
||||
$errors['chapters'] = $context->validateRelations($data['chapters'] ?? [], ZipExportChapter::class);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$model = new self();
|
||||
|
||||
$model->id = $data['id'] ?? null;
|
||||
$model->name = $data['name'];
|
||||
$model->description_html = $data['description_html'] ?? null;
|
||||
$model->cover = $data['cover'] ?? null;
|
||||
$model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
|
||||
$model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
|
||||
$model->chapters = ZipExportChapter::fromManyArray($data['chapters'] ?? []);
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
95
app/Exports/ZipExports/Models/ZipExportChapter.php
Normal file
95
app/Exports/ZipExports/Models/ZipExportChapter.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Exports\ZipExports\ZipExportFiles;
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
|
||||
class ZipExportChapter extends ZipExportModel
|
||||
{
|
||||
public ?int $id = null;
|
||||
public string $name;
|
||||
public ?string $description_html = null;
|
||||
public ?int $priority = null;
|
||||
/** @var ZipExportPage[] */
|
||||
public array $pages = [];
|
||||
/** @var ZipExportTag[] */
|
||||
public array $tags = [];
|
||||
|
||||
public function metadataOnly(): void
|
||||
{
|
||||
$this->description_html = null;
|
||||
|
||||
foreach ($this->pages as $page) {
|
||||
$page->metadataOnly();
|
||||
}
|
||||
foreach ($this->tags as $tag) {
|
||||
$tag->metadataOnly();
|
||||
}
|
||||
}
|
||||
|
||||
public function children(): array
|
||||
{
|
||||
return $this->pages;
|
||||
}
|
||||
|
||||
public static function fromModel(Chapter $model, ZipExportFiles $files): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->id = $model->id;
|
||||
$instance->name = $model->name;
|
||||
$instance->description_html = $model->descriptionHtml();
|
||||
$instance->priority = $model->priority;
|
||||
$instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
|
||||
|
||||
$pages = $model->getVisiblePages()->filter(fn (Page $page) => !$page->draft)->all();
|
||||
$instance->pages = ZipExportPage::fromModelArray($pages, $files);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Chapter[] $chapterArray
|
||||
* @return self[]
|
||||
*/
|
||||
public static function fromModelArray(array $chapterArray, ZipExportFiles $files): array
|
||||
{
|
||||
return array_values(array_map(function (Chapter $chapter) use ($files) {
|
||||
return self::fromModel($chapter, $files);
|
||||
}, $chapterArray));
|
||||
}
|
||||
|
||||
public static function validate(ZipValidationHelper $context, array $data): array
|
||||
{
|
||||
$rules = [
|
||||
'id' => ['nullable', 'int', $context->uniqueIdRule('chapter')],
|
||||
'name' => ['required', 'string', 'min:1'],
|
||||
'description_html' => ['nullable', 'string'],
|
||||
'priority' => ['nullable', 'int'],
|
||||
'tags' => ['array'],
|
||||
'pages' => ['array'],
|
||||
];
|
||||
|
||||
$errors = $context->validateData($data, $rules);
|
||||
$errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
|
||||
$errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$model = new self();
|
||||
|
||||
$model->id = $data['id'] ?? null;
|
||||
$model->name = $data['name'];
|
||||
$model->description_html = $data['description_html'] ?? null;
|
||||
$model->priority = isset($data['priority']) ? intval($data['priority']) : null;
|
||||
$model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
|
||||
$model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
57
app/Exports/ZipExports/Models/ZipExportImage.php
Normal file
57
app/Exports/ZipExports/Models/ZipExportImage.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Exports\ZipExports\ZipExportFiles;
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
use BookStack\Uploads\Image;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ZipExportImage extends ZipExportModel
|
||||
{
|
||||
public ?int $id = null;
|
||||
public string $name;
|
||||
public string $file;
|
||||
public string $type;
|
||||
|
||||
public static function fromModel(Image $model, ZipExportFiles $files): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->id = $model->id;
|
||||
$instance->name = $model->name;
|
||||
$instance->type = $model->type;
|
||||
$instance->file = $files->referenceForImage($model);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function metadataOnly(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public static function validate(ZipValidationHelper $context, array $data): array
|
||||
{
|
||||
$acceptedImageTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
|
||||
$rules = [
|
||||
'id' => ['nullable', 'int', $context->uniqueIdRule('image')],
|
||||
'name' => ['required', 'string', 'min:1'],
|
||||
'file' => ['required', 'string', $context->fileReferenceRule($acceptedImageTypes)],
|
||||
'type' => ['required', 'string', Rule::in(['gallery', 'drawio'])],
|
||||
];
|
||||
|
||||
return $context->validateData($data, $rules);
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$model = new self();
|
||||
|
||||
$model->id = $data['id'] ?? null;
|
||||
$model->name = $data['name'];
|
||||
$model->file = $data['file'];
|
||||
$model->type = $data['type'];
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
57
app/Exports/ZipExports/Models/ZipExportModel.php
Normal file
57
app/Exports/ZipExports/Models/ZipExportModel.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
use JsonSerializable;
|
||||
|
||||
abstract class ZipExportModel implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Handle the serialization to JSON.
|
||||
* For these exports, we filter out optional (represented as nullable) fields
|
||||
* just to clean things up and prevent confusion to avoid null states in the
|
||||
* resulting export format itself.
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
$publicProps = get_object_vars(...)->__invoke($this);
|
||||
return array_filter($publicProps, fn ($value) => $value !== null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the given array of data intended for this model.
|
||||
* Return an array of validation errors messages.
|
||||
* Child items can be considered in the validation result by returning a keyed
|
||||
* item in the array for its own validation messages.
|
||||
*/
|
||||
abstract public static function validate(ZipValidationHelper $context, array $data): array;
|
||||
|
||||
/**
|
||||
* Decode the array of data into this export model.
|
||||
*/
|
||||
abstract public static function fromArray(array $data): self;
|
||||
|
||||
/**
|
||||
* Decode an array of array data into an array of export models.
|
||||
* @param array[] $data
|
||||
* @return self[]
|
||||
*/
|
||||
public static function fromManyArray(array $data): array
|
||||
{
|
||||
$results = [];
|
||||
foreach ($data as $item) {
|
||||
$results[] = static::fromArray($item);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove additional content in this model to reduce it down
|
||||
* to just essential id/name values for identification.
|
||||
*
|
||||
* The result of this may be something that does not pass validation, but is
|
||||
* simple for the purpose of creating a contents.
|
||||
*/
|
||||
abstract public function metadataOnly(): void;
|
||||
}
|
104
app/Exports/ZipExports/Models/ZipExportPage.php
Normal file
104
app/Exports/ZipExports/Models/ZipExportPage.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Tools\PageContent;
|
||||
use BookStack\Exports\ZipExports\ZipExportFiles;
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
|
||||
class ZipExportPage extends ZipExportModel
|
||||
{
|
||||
public ?int $id = null;
|
||||
public string $name;
|
||||
public ?string $html = null;
|
||||
public ?string $markdown = null;
|
||||
public ?int $priority = null;
|
||||
/** @var ZipExportAttachment[] */
|
||||
public array $attachments = [];
|
||||
/** @var ZipExportImage[] */
|
||||
public array $images = [];
|
||||
/** @var ZipExportTag[] */
|
||||
public array $tags = [];
|
||||
|
||||
public function metadataOnly(): void
|
||||
{
|
||||
$this->html = $this->markdown = null;
|
||||
|
||||
foreach ($this->attachments as $attachment) {
|
||||
$attachment->metadataOnly();
|
||||
}
|
||||
foreach ($this->images as $image) {
|
||||
$image->metadataOnly();
|
||||
}
|
||||
foreach ($this->tags as $tag) {
|
||||
$tag->metadataOnly();
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromModel(Page $model, ZipExportFiles $files): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->id = $model->id;
|
||||
$instance->name = $model->name;
|
||||
$instance->html = (new PageContent($model))->render();
|
||||
$instance->priority = $model->priority;
|
||||
|
||||
if (!empty($model->markdown)) {
|
||||
$instance->markdown = $model->markdown;
|
||||
}
|
||||
|
||||
$instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
|
||||
$instance->attachments = ZipExportAttachment::fromModelArray($model->attachments()->get()->all(), $files);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Page[] $pageArray
|
||||
* @return self[]
|
||||
*/
|
||||
public static function fromModelArray(array $pageArray, ZipExportFiles $files): array
|
||||
{
|
||||
return array_values(array_map(function (Page $page) use ($files) {
|
||||
return self::fromModel($page, $files);
|
||||
}, $pageArray));
|
||||
}
|
||||
|
||||
public static function validate(ZipValidationHelper $context, array $data): array
|
||||
{
|
||||
$rules = [
|
||||
'id' => ['nullable', 'int', $context->uniqueIdRule('page')],
|
||||
'name' => ['required', 'string', 'min:1'],
|
||||
'html' => ['nullable', 'string'],
|
||||
'markdown' => ['nullable', 'string'],
|
||||
'priority' => ['nullable', 'int'],
|
||||
'attachments' => ['array'],
|
||||
'images' => ['array'],
|
||||
'tags' => ['array'],
|
||||
];
|
||||
|
||||
$errors = $context->validateData($data, $rules);
|
||||
$errors['attachments'] = $context->validateRelations($data['attachments'] ?? [], ZipExportAttachment::class);
|
||||
$errors['images'] = $context->validateRelations($data['images'] ?? [], ZipExportImage::class);
|
||||
$errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$model = new self();
|
||||
|
||||
$model->id = $data['id'] ?? null;
|
||||
$model->name = $data['name'];
|
||||
$model->html = $data['html'] ?? null;
|
||||
$model->markdown = $data['markdown'] ?? null;
|
||||
$model->priority = isset($data['priority']) ? intval($data['priority']) : null;
|
||||
$model->attachments = ZipExportAttachment::fromManyArray($data['attachments'] ?? []);
|
||||
$model->images = ZipExportImage::fromManyArray($data['images'] ?? []);
|
||||
$model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
51
app/Exports/ZipExports/Models/ZipExportTag.php
Normal file
51
app/Exports/ZipExports/Models/ZipExportTag.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports\Models;
|
||||
|
||||
use BookStack\Activity\Models\Tag;
|
||||
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
||||
|
||||
class ZipExportTag extends ZipExportModel
|
||||
{
|
||||
public string $name;
|
||||
public ?string $value = null;
|
||||
|
||||
public function metadataOnly(): void
|
||||
{
|
||||
$this->value = null;
|
||||
}
|
||||
|
||||
public static function fromModel(Tag $model): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->name = $model->name;
|
||||
$instance->value = $model->value;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromModelArray(array $tagArray): array
|
||||
{
|
||||
return array_values(array_map(self::fromModel(...), $tagArray));
|
||||
}
|
||||
|
||||
public static function validate(ZipValidationHelper $context, array $data): array
|
||||
{
|
||||
$rules = [
|
||||
'name' => ['required', 'string', 'min:1'],
|
||||
'value' => ['nullable', 'string'],
|
||||
];
|
||||
|
||||
return $context->validateData($data, $rules);
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$model = new self();
|
||||
|
||||
$model->name = $data['name'];
|
||||
$model->value = $data['value'] ?? null;
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
117
app/Exports/ZipExports/ZipExportBuilder.php
Normal file
117
app/Exports/ZipExports/ZipExportBuilder.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Exceptions\ZipExportException;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use ZipArchive;
|
||||
|
||||
class ZipExportBuilder
|
||||
{
|
||||
protected array $data = [];
|
||||
|
||||
public function __construct(
|
||||
protected ZipExportFiles $files,
|
||||
protected ZipExportReferences $references,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
public function buildForPage(Page $page): string
|
||||
{
|
||||
$exportPage = ZipExportPage::fromModel($page, $this->files);
|
||||
$this->data['page'] = $exportPage;
|
||||
|
||||
$this->references->addPage($exportPage);
|
||||
|
||||
return $this->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
public function buildForChapter(Chapter $chapter): string
|
||||
{
|
||||
$exportChapter = ZipExportChapter::fromModel($chapter, $this->files);
|
||||
$this->data['chapter'] = $exportChapter;
|
||||
|
||||
$this->references->addChapter($exportChapter);
|
||||
|
||||
return $this->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
public function buildForBook(Book $book): string
|
||||
{
|
||||
$exportBook = ZipExportBook::fromModel($book, $this->files);
|
||||
$this->data['book'] = $exportBook;
|
||||
|
||||
$this->references->addBook($exportBook);
|
||||
|
||||
return $this->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
protected function build(): string
|
||||
{
|
||||
$this->references->buildReferences($this->files);
|
||||
|
||||
$this->data['exported_at'] = date(DATE_ATOM);
|
||||
$this->data['instance'] = [
|
||||
'id' => setting('instance-id', ''),
|
||||
'version' => trim(file_get_contents(base_path('version'))),
|
||||
];
|
||||
|
||||
$zipFile = tempnam(sys_get_temp_dir(), 'bszip-');
|
||||
$zip = new ZipArchive();
|
||||
$opened = $zip->open($zipFile, ZipArchive::CREATE);
|
||||
if ($opened !== true) {
|
||||
throw new ZipExportException('Failed to create zip file for export.');
|
||||
}
|
||||
|
||||
$zip->addFromString('data.json', json_encode($this->data));
|
||||
$zip->addEmptyDir('files');
|
||||
|
||||
$toRemove = [];
|
||||
$addedNames = [];
|
||||
|
||||
try {
|
||||
$this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove, &$addedNames) {
|
||||
$entryName = "files/$fileRef";
|
||||
$zip->addFile($filePath, $entryName);
|
||||
$toRemove[] = $filePath;
|
||||
$addedNames[] = $entryName;
|
||||
});
|
||||
} catch (\Exception $exception) {
|
||||
// Cleanup the files we've processed so far and respond back with error
|
||||
foreach ($toRemove as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
foreach ($addedNames as $name) {
|
||||
$zip->deleteName($name);
|
||||
}
|
||||
$zip->close();
|
||||
unlink($zipFile);
|
||||
throw new ZipExportException("Failed to add files for ZIP export, received error: " . $exception->getMessage());
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
|
||||
foreach ($toRemove as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
return $zipFile;
|
||||
}
|
||||
}
|
107
app/Exports/ZipExports/ZipExportFiles.php
Normal file
107
app/Exports/ZipExports/ZipExportFiles.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\Uploads\Attachment;
|
||||
use BookStack\Uploads\AttachmentService;
|
||||
use BookStack\Uploads\Image;
|
||||
use BookStack\Uploads\ImageService;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ZipExportFiles
|
||||
{
|
||||
/**
|
||||
* References for attachments by attachment ID.
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected array $attachmentRefsById = [];
|
||||
|
||||
/**
|
||||
* References for images by image ID.
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected array $imageRefsById = [];
|
||||
|
||||
public function __construct(
|
||||
protected AttachmentService $attachmentService,
|
||||
protected ImageService $imageService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gain a reference to the given attachment instance.
|
||||
* This is expected to be a file-based attachment that the user
|
||||
* has visibility of, no permission/access checks are performed here.
|
||||
*/
|
||||
public function referenceForAttachment(Attachment $attachment): string
|
||||
{
|
||||
if (isset($this->attachmentRefsById[$attachment->id])) {
|
||||
return $this->attachmentRefsById[$attachment->id];
|
||||
}
|
||||
|
||||
$existingFiles = $this->getAllFileNames();
|
||||
do {
|
||||
$fileName = Str::random(20) . '.' . $attachment->extension;
|
||||
} while (in_array($fileName, $existingFiles));
|
||||
|
||||
$this->attachmentRefsById[$attachment->id] = $fileName;
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gain a reference to the given image instance.
|
||||
* This is expected to be an image that the user has visibility of,
|
||||
* no permission/access checks are performed here.
|
||||
*/
|
||||
public function referenceForImage(Image $image): string
|
||||
{
|
||||
if (isset($this->imageRefsById[$image->id])) {
|
||||
return $this->imageRefsById[$image->id];
|
||||
}
|
||||
|
||||
$existingFiles = $this->getAllFileNames();
|
||||
$extension = pathinfo($image->path, PATHINFO_EXTENSION);
|
||||
do {
|
||||
$fileName = Str::random(20) . '.' . $extension;
|
||||
} while (in_array($fileName, $existingFiles));
|
||||
|
||||
$this->imageRefsById[$image->id] = $fileName;
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
protected function getAllFileNames(): array
|
||||
{
|
||||
return array_merge(
|
||||
array_values($this->attachmentRefsById),
|
||||
array_values($this->imageRefsById),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract each of the ZIP export tracked files.
|
||||
* Calls the given callback for each tracked file, passing a temporary
|
||||
* file reference of the file contents, and the zip-local tracked reference.
|
||||
*/
|
||||
public function extractEach(callable $callback): void
|
||||
{
|
||||
foreach ($this->attachmentRefsById as $attachmentId => $ref) {
|
||||
$attachment = Attachment::query()->find($attachmentId);
|
||||
$stream = $this->attachmentService->streamAttachmentFromStorage($attachment);
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'bszipfile-');
|
||||
$tmpFileStream = fopen($tmpFile, 'w');
|
||||
stream_copy_to_stream($stream, $tmpFileStream);
|
||||
$callback($tmpFile, $ref);
|
||||
}
|
||||
|
||||
foreach ($this->imageRefsById as $imageId => $ref) {
|
||||
$image = Image::query()->find($imageId);
|
||||
$stream = $this->imageService->getImageStream($image);
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'bszipimage-');
|
||||
$tmpFileStream = fopen($tmpFile, 'w');
|
||||
stream_copy_to_stream($stream, $tmpFileStream);
|
||||
$callback($tmpFile, $ref);
|
||||
}
|
||||
}
|
||||
}
|
111
app/Exports/ZipExports/ZipExportReader.php
Normal file
111
app/Exports/ZipExports/ZipExportReader.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\Exceptions\ZipExportException;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use BookStack\Util\WebSafeMimeSniffer;
|
||||
use ZipArchive;
|
||||
|
||||
class ZipExportReader
|
||||
{
|
||||
protected ZipArchive $zip;
|
||||
protected bool $open = false;
|
||||
|
||||
public function __construct(
|
||||
protected string $zipPath,
|
||||
) {
|
||||
$this->zip = new ZipArchive();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
protected function open(): void
|
||||
{
|
||||
if ($this->open) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate file exists
|
||||
if (!file_exists($this->zipPath) || !is_readable($this->zipPath)) {
|
||||
throw new ZipExportException(trans('errors.import_zip_cant_read'));
|
||||
}
|
||||
|
||||
// Validate file is valid zip
|
||||
$opened = $this->zip->open($this->zipPath, ZipArchive::RDONLY);
|
||||
if ($opened !== true) {
|
||||
throw new ZipExportException(trans('errors.import_zip_cant_read'));
|
||||
}
|
||||
|
||||
$this->open = true;
|
||||
}
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
if ($this->open) {
|
||||
$this->zip->close();
|
||||
$this->open = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
public function readData(): array
|
||||
{
|
||||
$this->open();
|
||||
|
||||
// Validate json data exists, including metadata
|
||||
$jsonData = $this->zip->getFromName('data.json') ?: '';
|
||||
$importData = json_decode($jsonData, true);
|
||||
if (!$importData) {
|
||||
throw new ZipExportException(trans('errors.import_zip_cant_decode_data'));
|
||||
}
|
||||
|
||||
return $importData;
|
||||
}
|
||||
|
||||
public function fileExists(string $fileName): bool
|
||||
{
|
||||
return $this->zip->statName("files/{$fileName}") !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|resource
|
||||
*/
|
||||
public function streamFile(string $fileName)
|
||||
{
|
||||
return $this->zip->getStream("files/{$fileName}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sniff the mime type from the file of given name.
|
||||
*/
|
||||
public function sniffFileMime(string $fileName): string
|
||||
{
|
||||
$stream = $this->streamFile($fileName);
|
||||
$sniffContent = fread($stream, 2000);
|
||||
|
||||
return (new WebSafeMimeSniffer())->sniff($sniffContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipExportException
|
||||
*/
|
||||
public function decodeDataToExportModel(): ZipExportBook|ZipExportChapter|ZipExportPage
|
||||
{
|
||||
$data = $this->readData();
|
||||
if (isset($data['book'])) {
|
||||
return ZipExportBook::fromArray($data['book']);
|
||||
} else if (isset($data['chapter'])) {
|
||||
return ZipExportChapter::fromArray($data['chapter']);
|
||||
} else if (isset($data['page'])) {
|
||||
return ZipExportPage::fromArray($data['page']);
|
||||
}
|
||||
|
||||
throw new ZipExportException("Could not identify content in ZIP file data.");
|
||||
}
|
||||
}
|
159
app/Exports/ZipExports/ZipExportReferences.php
Normal file
159
app/Exports/ZipExports/ZipExportReferences.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\App\Model;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportAttachment;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportImage;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportModel;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use BookStack\Uploads\Attachment;
|
||||
use BookStack\Uploads\Image;
|
||||
|
||||
class ZipExportReferences
|
||||
{
|
||||
/** @var ZipExportPage[] */
|
||||
protected array $pages = [];
|
||||
/** @var ZipExportChapter[] */
|
||||
protected array $chapters = [];
|
||||
/** @var ZipExportBook[] */
|
||||
protected array $books = [];
|
||||
|
||||
/** @var ZipExportAttachment[] */
|
||||
protected array $attachments = [];
|
||||
|
||||
/** @var ZipExportImage[] */
|
||||
protected array $images = [];
|
||||
|
||||
public function __construct(
|
||||
protected ZipReferenceParser $parser,
|
||||
) {
|
||||
}
|
||||
|
||||
public function addPage(ZipExportPage $page): void
|
||||
{
|
||||
if ($page->id) {
|
||||
$this->pages[$page->id] = $page;
|
||||
}
|
||||
|
||||
foreach ($page->attachments as $attachment) {
|
||||
if ($attachment->id) {
|
||||
$this->attachments[$attachment->id] = $attachment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addChapter(ZipExportChapter $chapter): void
|
||||
{
|
||||
if ($chapter->id) {
|
||||
$this->chapters[$chapter->id] = $chapter;
|
||||
}
|
||||
|
||||
foreach ($chapter->pages as $page) {
|
||||
$this->addPage($page);
|
||||
}
|
||||
}
|
||||
|
||||
public function addBook(ZipExportBook $book): void
|
||||
{
|
||||
if ($book->id) {
|
||||
$this->books[$book->id] = $book;
|
||||
}
|
||||
|
||||
foreach ($book->pages as $page) {
|
||||
$this->addPage($page);
|
||||
}
|
||||
|
||||
foreach ($book->chapters as $chapter) {
|
||||
$this->addChapter($chapter);
|
||||
}
|
||||
}
|
||||
|
||||
public function buildReferences(ZipExportFiles $files): void
|
||||
{
|
||||
$createHandler = function (ZipExportModel $zipModel) use ($files) {
|
||||
return function (Model $model) use ($files, $zipModel) {
|
||||
return $this->handleModelReference($model, $zipModel, $files);
|
||||
};
|
||||
};
|
||||
|
||||
// Parse page content first
|
||||
foreach ($this->pages as $page) {
|
||||
$handler = $createHandler($page);
|
||||
$page->html = $this->parser->parseLinks($page->html ?? '', $handler);
|
||||
if ($page->markdown) {
|
||||
$page->markdown = $this->parser->parseLinks($page->markdown, $handler);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse chapter description HTML
|
||||
foreach ($this->chapters as $chapter) {
|
||||
if ($chapter->description_html) {
|
||||
$handler = $createHandler($chapter);
|
||||
$chapter->description_html = $this->parser->parseLinks($chapter->description_html, $handler);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse book description HTML
|
||||
foreach ($this->books as $book) {
|
||||
if ($book->description_html) {
|
||||
$handler = $createHandler($book);
|
||||
$book->description_html = $this->parser->parseLinks($book->description_html, $handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function handleModelReference(Model $model, ZipExportModel $exportModel, ZipExportFiles $files): ?string
|
||||
{
|
||||
// Handle attachment references
|
||||
// No permission check needed here since they would only already exist in this
|
||||
// reference context if already allowed via their entity access.
|
||||
if ($model instanceof Attachment) {
|
||||
if (isset($this->attachments[$model->id])) {
|
||||
return "[[bsexport:attachment:{$model->id}]]";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle image references
|
||||
if ($model instanceof Image) {
|
||||
// Only handle gallery and drawio images
|
||||
if ($model->type !== 'gallery' && $model->type !== 'drawio') {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle simple links outside of page content
|
||||
if (!($exportModel instanceof ZipExportPage) && isset($this->images[$model->id])) {
|
||||
return "[[bsexport:image:{$model->id}]]";
|
||||
}
|
||||
|
||||
// Find and include images if in visibility
|
||||
$page = $model->getPage();
|
||||
if ($page && userCan('view', $page)) {
|
||||
if (!isset($this->images[$model->id])) {
|
||||
$exportImage = ZipExportImage::fromModel($model, $files);
|
||||
$this->images[$model->id] = $exportImage;
|
||||
$exportModel->images[] = $exportImage;
|
||||
}
|
||||
return "[[bsexport:image:{$model->id}]]";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle entity references
|
||||
if ($model instanceof Book && isset($this->books[$model->id])) {
|
||||
return "[[bsexport:book:{$model->id}]]";
|
||||
} else if ($model instanceof Chapter && isset($this->chapters[$model->id])) {
|
||||
return "[[bsexport:chapter:{$model->id}]]";
|
||||
} else if ($model instanceof Page && isset($this->pages[$model->id])) {
|
||||
return "[[bsexport:page:{$model->id}]]";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
57
app/Exports/ZipExports/ZipExportValidator.php
Normal file
57
app/Exports/ZipExports/ZipExportValidator.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\Exceptions\ZipExportException;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
|
||||
class ZipExportValidator
|
||||
{
|
||||
public function __construct(
|
||||
protected ZipExportReader $reader,
|
||||
) {
|
||||
}
|
||||
|
||||
public function validate(): array
|
||||
{
|
||||
try {
|
||||
$importData = $this->reader->readData();
|
||||
} catch (ZipExportException $exception) {
|
||||
return ['format' => $exception->getMessage()];
|
||||
}
|
||||
|
||||
$helper = new ZipValidationHelper($this->reader);
|
||||
|
||||
if (isset($importData['book'])) {
|
||||
$modelErrors = ZipExportBook::validate($helper, $importData['book']);
|
||||
$keyPrefix = 'book';
|
||||
} else if (isset($importData['chapter'])) {
|
||||
$modelErrors = ZipExportChapter::validate($helper, $importData['chapter']);
|
||||
$keyPrefix = 'chapter';
|
||||
} else if (isset($importData['page'])) {
|
||||
$modelErrors = ZipExportPage::validate($helper, $importData['page']);
|
||||
$keyPrefix = 'page';
|
||||
} else {
|
||||
return ['format' => trans('errors.import_zip_no_data')];
|
||||
}
|
||||
|
||||
return $this->flattenModelErrors($modelErrors, $keyPrefix);
|
||||
}
|
||||
|
||||
protected function flattenModelErrors(array $errors, string $keyPrefix): array
|
||||
{
|
||||
$flattened = [];
|
||||
|
||||
foreach ($errors as $key => $error) {
|
||||
if (is_array($error)) {
|
||||
$flattened = array_merge($flattened, $this->flattenModelErrors($error, $keyPrefix . '.' . $key));
|
||||
} else {
|
||||
$flattened[$keyPrefix . '.' . $key] = $error;
|
||||
}
|
||||
}
|
||||
|
||||
return $flattened;
|
||||
}
|
||||
}
|
37
app/Exports/ZipExports/ZipFileReferenceRule.php
Normal file
37
app/Exports/ZipExports/ZipFileReferenceRule.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class ZipFileReferenceRule implements ValidationRule
|
||||
{
|
||||
public function __construct(
|
||||
protected ZipValidationHelper $context,
|
||||
protected array $acceptedMimes,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (!$this->context->zipReader->fileExists($value)) {
|
||||
$fail('validation.zip_file')->translate();
|
||||
}
|
||||
|
||||
if (!empty($this->acceptedMimes)) {
|
||||
$fileMime = $this->context->zipReader->sniffFileMime($value);
|
||||
if (!in_array($fileMime, $this->acceptedMimes)) {
|
||||
$fail('validation.zip_file_mime')->translate([
|
||||
'attribute' => $attribute,
|
||||
'validTypes' => implode(',', $this->acceptedMimes),
|
||||
'foundType' => $fileMime
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
161
app/Exports/ZipExports/ZipImportReferences.php
Normal file
161
app/Exports/ZipExports/ZipImportReferences.php
Normal file
@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\App\Model;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Repos\BaseRepo;
|
||||
use BookStack\Entities\Repos\PageRepo;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use BookStack\Uploads\Attachment;
|
||||
use BookStack\Uploads\Image;
|
||||
use BookStack\Uploads\ImageResizer;
|
||||
|
||||
class ZipImportReferences
|
||||
{
|
||||
/** @var Page[] */
|
||||
protected array $pages = [];
|
||||
/** @var Chapter[] */
|
||||
protected array $chapters = [];
|
||||
/** @var Book[] */
|
||||
protected array $books = [];
|
||||
/** @var Attachment[] */
|
||||
protected array $attachments = [];
|
||||
/** @var Image[] */
|
||||
protected array $images = [];
|
||||
|
||||
/** @var array<string, Model> */
|
||||
protected array $referenceMap = [];
|
||||
|
||||
/** @var array<int, ZipExportPage> */
|
||||
protected array $zipExportPageMap = [];
|
||||
/** @var array<int, ZipExportChapter> */
|
||||
protected array $zipExportChapterMap = [];
|
||||
/** @var array<int, ZipExportBook> */
|
||||
protected array $zipExportBookMap = [];
|
||||
|
||||
public function __construct(
|
||||
protected ZipReferenceParser $parser,
|
||||
protected BaseRepo $baseRepo,
|
||||
protected PageRepo $pageRepo,
|
||||
protected ImageResizer $imageResizer,
|
||||
) {
|
||||
}
|
||||
|
||||
protected function addReference(string $type, Model $model, ?int $importId): void
|
||||
{
|
||||
if ($importId) {
|
||||
$key = $type . ':' . $importId;
|
||||
$this->referenceMap[$key] = $model;
|
||||
}
|
||||
}
|
||||
|
||||
public function addPage(Page $page, ZipExportPage $exportPage): void
|
||||
{
|
||||
$this->pages[] = $page;
|
||||
$this->zipExportPageMap[$page->id] = $exportPage;
|
||||
$this->addReference('page', $page, $exportPage->id);
|
||||
}
|
||||
|
||||
public function addChapter(Chapter $chapter, ZipExportChapter $exportChapter): void
|
||||
{
|
||||
$this->chapters[] = $chapter;
|
||||
$this->zipExportChapterMap[$chapter->id] = $exportChapter;
|
||||
$this->addReference('chapter', $chapter, $exportChapter->id);
|
||||
}
|
||||
|
||||
public function addBook(Book $book, ZipExportBook $exportBook): void
|
||||
{
|
||||
$this->books[] = $book;
|
||||
$this->zipExportBookMap[$book->id] = $exportBook;
|
||||
$this->addReference('book', $book, $exportBook->id);
|
||||
}
|
||||
|
||||
public function addAttachment(Attachment $attachment, ?int $importId): void
|
||||
{
|
||||
$this->attachments[] = $attachment;
|
||||
$this->addReference('attachment', $attachment, $importId);
|
||||
}
|
||||
|
||||
public function addImage(Image $image, ?int $importId): void
|
||||
{
|
||||
$this->images[] = $image;
|
||||
$this->addReference('image', $image, $importId);
|
||||
}
|
||||
|
||||
protected function handleReference(string $type, int $id): ?string
|
||||
{
|
||||
$key = $type . ':' . $id;
|
||||
$model = $this->referenceMap[$key] ?? null;
|
||||
if ($model instanceof Entity) {
|
||||
return $model->getUrl();
|
||||
} else if ($model instanceof Image) {
|
||||
if ($model->type === 'gallery') {
|
||||
$this->imageResizer->loadGalleryThumbnailsForImage($model, false);
|
||||
return $model->thumbs['display'] ?? $model->url;
|
||||
}
|
||||
|
||||
return $model->url;
|
||||
} else if ($model instanceof Attachment) {
|
||||
return $model->getUrl(false);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function replaceReferences(): void
|
||||
{
|
||||
foreach ($this->books as $book) {
|
||||
$exportBook = $this->zipExportBookMap[$book->id];
|
||||
$content = $exportBook->description_html ?? '';
|
||||
$parsed = $this->parser->parseReferences($content, $this->handleReference(...));
|
||||
|
||||
$this->baseRepo->update($book, [
|
||||
'description_html' => $parsed,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($this->chapters as $chapter) {
|
||||
$exportChapter = $this->zipExportChapterMap[$chapter->id];
|
||||
$content = $exportChapter->description_html ?? '';
|
||||
$parsed = $this->parser->parseReferences($content, $this->handleReference(...));
|
||||
|
||||
$this->baseRepo->update($chapter, [
|
||||
'description_html' => $parsed,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($this->pages as $page) {
|
||||
$exportPage = $this->zipExportPageMap[$page->id];
|
||||
$contentType = $exportPage->markdown ? 'markdown' : 'html';
|
||||
$content = $exportPage->markdown ?: ($exportPage->html ?: '');
|
||||
$parsed = $this->parser->parseReferences($content, $this->handleReference(...));
|
||||
|
||||
$this->pageRepo->setContentFromInput($page, [
|
||||
$contentType => $parsed,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Image[]
|
||||
*/
|
||||
public function images(): array
|
||||
{
|
||||
return $this->images;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attachment[]
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
return $this->attachments;
|
||||
}
|
||||
}
|
364
app/Exports/ZipExports/ZipImportRunner.php
Normal file
364
app/Exports/ZipExports/ZipImportRunner.php
Normal file
@ -0,0 +1,364 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Repos\BookRepo;
|
||||
use BookStack\Entities\Repos\ChapterRepo;
|
||||
use BookStack\Entities\Repos\PageRepo;
|
||||
use BookStack\Exceptions\ZipExportException;
|
||||
use BookStack\Exceptions\ZipImportException;
|
||||
use BookStack\Exports\Import;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportAttachment;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportImage;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportTag;
|
||||
use BookStack\Uploads\Attachment;
|
||||
use BookStack\Uploads\AttachmentService;
|
||||
use BookStack\Uploads\FileStorage;
|
||||
use BookStack\Uploads\Image;
|
||||
use BookStack\Uploads\ImageService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
|
||||
class ZipImportRunner
|
||||
{
|
||||
protected array $tempFilesToCleanup = [];
|
||||
|
||||
public function __construct(
|
||||
protected FileStorage $storage,
|
||||
protected PageRepo $pageRepo,
|
||||
protected ChapterRepo $chapterRepo,
|
||||
protected BookRepo $bookRepo,
|
||||
protected ImageService $imageService,
|
||||
protected AttachmentService $attachmentService,
|
||||
protected ZipImportReferences $references,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the import.
|
||||
* Performs re-validation on zip, validation on parent provided, and permissions for importing
|
||||
* the planned content, before running the import process.
|
||||
* Returns the top-level entity item which was imported.
|
||||
* @throws ZipImportException
|
||||
*/
|
||||
public function run(Import $import, ?Entity $parent = null): Entity
|
||||
{
|
||||
$zipPath = $this->getZipPath($import);
|
||||
$reader = new ZipExportReader($zipPath);
|
||||
|
||||
$errors = (new ZipExportValidator($reader))->validate();
|
||||
if ($errors) {
|
||||
throw new ZipImportException([
|
||||
trans('errors.import_validation_failed'),
|
||||
...$errors,
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$exportModel = $reader->decodeDataToExportModel();
|
||||
} catch (ZipExportException $e) {
|
||||
throw new ZipImportException([$e->getMessage()]);
|
||||
}
|
||||
|
||||
// Validate parent type
|
||||
if ($exportModel instanceof ZipExportBook && ($parent !== null)) {
|
||||
throw new ZipImportException(["Must not have a parent set for a Book import."]);
|
||||
} else if ($exportModel instanceof ZipExportChapter && !($parent instanceof Book)) {
|
||||
throw new ZipImportException(["Parent book required for chapter import."]);
|
||||
} else if ($exportModel instanceof ZipExportPage && !($parent instanceof Book || $parent instanceof Chapter)) {
|
||||
throw new ZipImportException(["Parent book or chapter required for page import."]);
|
||||
}
|
||||
|
||||
$this->ensurePermissionsPermitImport($exportModel, $parent);
|
||||
|
||||
if ($exportModel instanceof ZipExportBook) {
|
||||
$entity = $this->importBook($exportModel, $reader);
|
||||
} else if ($exportModel instanceof ZipExportChapter) {
|
||||
$entity = $this->importChapter($exportModel, $parent, $reader);
|
||||
} else if ($exportModel instanceof ZipExportPage) {
|
||||
$entity = $this->importPage($exportModel, $parent, $reader);
|
||||
} else {
|
||||
throw new ZipImportException(['No importable data found in import data.']);
|
||||
}
|
||||
|
||||
$this->references->replaceReferences();
|
||||
|
||||
$reader->close();
|
||||
$this->cleanup();
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert any files which have been stored during this import process.
|
||||
* Considers files only, and avoids the database under the
|
||||
* assumption that the database may already have been
|
||||
* reverted as part of a transaction rollback.
|
||||
*/
|
||||
public function revertStoredFiles(): void
|
||||
{
|
||||
foreach ($this->references->images() as $image) {
|
||||
$this->imageService->destroyFileAtPath($image->type, $image->path);
|
||||
}
|
||||
|
||||
foreach ($this->references->attachments() as $attachment) {
|
||||
if (!$attachment->external) {
|
||||
$this->attachmentService->deleteFileInStorage($attachment);
|
||||
}
|
||||
}
|
||||
|
||||
$this->cleanup();
|
||||
}
|
||||
|
||||
protected function cleanup(): void
|
||||
{
|
||||
foreach ($this->tempFilesToCleanup as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
$this->tempFilesToCleanup = [];
|
||||
}
|
||||
|
||||
protected function importBook(ZipExportBook $exportBook, ZipExportReader $reader): Book
|
||||
{
|
||||
$book = $this->bookRepo->create([
|
||||
'name' => $exportBook->name,
|
||||
'description_html' => $exportBook->description_html ?? '',
|
||||
'image' => $exportBook->cover ? $this->zipFileToUploadedFile($exportBook->cover, $reader) : null,
|
||||
'tags' => $this->exportTagsToInputArray($exportBook->tags ?? []),
|
||||
]);
|
||||
|
||||
if ($book->cover) {
|
||||
$this->references->addImage($book->cover, null);
|
||||
}
|
||||
|
||||
$children = [
|
||||
...$exportBook->chapters,
|
||||
...$exportBook->pages,
|
||||
];
|
||||
|
||||
usort($children, function (ZipExportPage|ZipExportChapter $a, ZipExportPage|ZipExportChapter $b) {
|
||||
return ($a->priority ?? 0) - ($b->priority ?? 0);
|
||||
});
|
||||
|
||||
foreach ($children as $child) {
|
||||
if ($child instanceof ZipExportChapter) {
|
||||
$this->importChapter($child, $book, $reader);
|
||||
} else if ($child instanceof ZipExportPage) {
|
||||
$this->importPage($child, $book, $reader);
|
||||
}
|
||||
}
|
||||
|
||||
$this->references->addBook($book, $exportBook);
|
||||
|
||||
return $book;
|
||||
}
|
||||
|
||||
protected function importChapter(ZipExportChapter $exportChapter, Book $parent, ZipExportReader $reader): Chapter
|
||||
{
|
||||
$chapter = $this->chapterRepo->create([
|
||||
'name' => $exportChapter->name,
|
||||
'description_html' => $exportChapter->description_html ?? '',
|
||||
'tags' => $this->exportTagsToInputArray($exportChapter->tags ?? []),
|
||||
], $parent);
|
||||
|
||||
$exportPages = $exportChapter->pages;
|
||||
usort($exportPages, function (ZipExportPage $a, ZipExportPage $b) {
|
||||
return ($a->priority ?? 0) - ($b->priority ?? 0);
|
||||
});
|
||||
|
||||
foreach ($exportPages as $exportPage) {
|
||||
$this->importPage($exportPage, $chapter, $reader);
|
||||
}
|
||||
|
||||
$this->references->addChapter($chapter, $exportChapter);
|
||||
|
||||
return $chapter;
|
||||
}
|
||||
|
||||
protected function importPage(ZipExportPage $exportPage, Book|Chapter $parent, ZipExportReader $reader): Page
|
||||
{
|
||||
$page = $this->pageRepo->getNewDraftPage($parent);
|
||||
|
||||
foreach ($exportPage->attachments as $exportAttachment) {
|
||||
$this->importAttachment($exportAttachment, $page, $reader);
|
||||
}
|
||||
|
||||
foreach ($exportPage->images as $exportImage) {
|
||||
$this->importImage($exportImage, $page, $reader);
|
||||
}
|
||||
|
||||
$this->pageRepo->publishDraft($page, [
|
||||
'name' => $exportPage->name,
|
||||
'markdown' => $exportPage->markdown,
|
||||
'html' => $exportPage->html,
|
||||
'tags' => $this->exportTagsToInputArray($exportPage->tags ?? []),
|
||||
]);
|
||||
|
||||
$this->references->addPage($page, $exportPage);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
protected function importAttachment(ZipExportAttachment $exportAttachment, Page $page, ZipExportReader $reader): Attachment
|
||||
{
|
||||
if ($exportAttachment->file) {
|
||||
$file = $this->zipFileToUploadedFile($exportAttachment->file, $reader);
|
||||
$attachment = $this->attachmentService->saveNewUpload($file, $page->id);
|
||||
$attachment->name = $exportAttachment->name;
|
||||
$attachment->save();
|
||||
} else {
|
||||
$attachment = $this->attachmentService->saveNewFromLink(
|
||||
$exportAttachment->name,
|
||||
$exportAttachment->link ?? '',
|
||||
$page->id,
|
||||
);
|
||||
}
|
||||
|
||||
$this->references->addAttachment($attachment, $exportAttachment->id);
|
||||
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
protected function importImage(ZipExportImage $exportImage, Page $page, ZipExportReader $reader): Image
|
||||
{
|
||||
$mime = $reader->sniffFileMime($exportImage->file);
|
||||
$extension = explode('/', $mime)[1];
|
||||
|
||||
$file = $this->zipFileToUploadedFile($exportImage->file, $reader);
|
||||
$image = $this->imageService->saveNewFromUpload(
|
||||
$file,
|
||||
$exportImage->type,
|
||||
$page->id,
|
||||
null,
|
||||
null,
|
||||
true,
|
||||
$exportImage->name . '.' . $extension,
|
||||
);
|
||||
|
||||
$image->name = $exportImage->name;
|
||||
$image->save();
|
||||
|
||||
$this->references->addImage($image, $exportImage->id);
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
protected function exportTagsToInputArray(array $exportTags): array
|
||||
{
|
||||
$tags = [];
|
||||
|
||||
/** @var ZipExportTag $tag */
|
||||
foreach ($exportTags as $tag) {
|
||||
$tags[] = ['name' => $tag->name, 'value' => $tag->value ?? ''];
|
||||
}
|
||||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
protected function zipFileToUploadedFile(string $fileName, ZipExportReader $reader): UploadedFile
|
||||
{
|
||||
$tempPath = tempnam(sys_get_temp_dir(), 'bszipextract');
|
||||
$fileStream = $reader->streamFile($fileName);
|
||||
$tempStream = fopen($tempPath, 'wb');
|
||||
stream_copy_to_stream($fileStream, $tempStream);
|
||||
fclose($tempStream);
|
||||
|
||||
$this->tempFilesToCleanup[] = $tempPath;
|
||||
|
||||
return new UploadedFile($tempPath, $fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipImportException
|
||||
*/
|
||||
protected function ensurePermissionsPermitImport(ZipExportPage|ZipExportChapter|ZipExportBook $exportModel, Book|Chapter|null $parent = null): void
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
$chapters = [];
|
||||
$pages = [];
|
||||
$images = [];
|
||||
$attachments = [];
|
||||
|
||||
if ($exportModel instanceof ZipExportBook) {
|
||||
if (!userCan('book-create-all')) {
|
||||
$errors[] = trans('errors.import_perms_books');
|
||||
}
|
||||
array_push($pages, ...$exportModel->pages);
|
||||
array_push($chapters, ...$exportModel->chapters);
|
||||
} else if ($exportModel instanceof ZipExportChapter) {
|
||||
$chapters[] = $exportModel;
|
||||
} else if ($exportModel instanceof ZipExportPage) {
|
||||
$pages[] = $exportModel;
|
||||
}
|
||||
|
||||
foreach ($chapters as $chapter) {
|
||||
array_push($pages, ...$chapter->pages);
|
||||
}
|
||||
|
||||
if (count($chapters) > 0) {
|
||||
$permission = 'chapter-create' . ($parent ? '' : '-all');
|
||||
if (!userCan($permission, $parent)) {
|
||||
$errors[] = trans('errors.import_perms_chapters');
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($pages as $page) {
|
||||
array_push($attachments, ...$page->attachments);
|
||||
array_push($images, ...$page->images);
|
||||
}
|
||||
|
||||
if (count($pages) > 0) {
|
||||
if ($parent) {
|
||||
if (!userCan('page-create', $parent)) {
|
||||
$errors[] = trans('errors.import_perms_pages');
|
||||
}
|
||||
} else {
|
||||
$hasPermission = userCan('page-create-all') || userCan('page-create-own');
|
||||
if (!$hasPermission) {
|
||||
$errors[] = trans('errors.import_perms_pages');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($images) > 0) {
|
||||
if (!userCan('image-create-all')) {
|
||||
$errors[] = trans('errors.import_perms_images');
|
||||
}
|
||||
}
|
||||
|
||||
if (count($attachments) > 0) {
|
||||
if (!userCan('attachment-create-all')) {
|
||||
$errors[] = trans('errors.import_perms_attachments');
|
||||
}
|
||||
}
|
||||
|
||||
if (count($errors)) {
|
||||
throw new ZipImportException($errors);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getZipPath(Import $import): string
|
||||
{
|
||||
if (!$this->storage->isRemote()) {
|
||||
return $this->storage->getSystemPath($import->path);
|
||||
}
|
||||
|
||||
$tempFilePath = tempnam(sys_get_temp_dir(), 'bszip-import-');
|
||||
$tempFile = fopen($tempFilePath, 'wb');
|
||||
$stream = $this->storage->getReadStream($import->path);
|
||||
stream_copy_to_stream($stream, $tempFile);
|
||||
fclose($tempFile);
|
||||
|
||||
$this->tempFilesToCleanup[] = $tempFilePath;
|
||||
|
||||
return $tempFilePath;
|
||||
}
|
||||
}
|
140
app/Exports/ZipExports/ZipReferenceParser.php
Normal file
140
app/Exports/ZipExports/ZipReferenceParser.php
Normal file
@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\App\Model;
|
||||
use BookStack\Entities\Queries\EntityQueries;
|
||||
use BookStack\References\ModelResolvers\AttachmentModelResolver;
|
||||
use BookStack\References\ModelResolvers\BookLinkModelResolver;
|
||||
use BookStack\References\ModelResolvers\ChapterLinkModelResolver;
|
||||
use BookStack\References\ModelResolvers\CrossLinkModelResolver;
|
||||
use BookStack\References\ModelResolvers\ImageModelResolver;
|
||||
use BookStack\References\ModelResolvers\PageLinkModelResolver;
|
||||
use BookStack\References\ModelResolvers\PagePermalinkModelResolver;
|
||||
use BookStack\Uploads\ImageStorage;
|
||||
|
||||
class ZipReferenceParser
|
||||
{
|
||||
/**
|
||||
* @var CrossLinkModelResolver[]|null
|
||||
*/
|
||||
protected ?array $modelResolvers = null;
|
||||
|
||||
public function __construct(
|
||||
protected EntityQueries $queries
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and replace references in the given content.
|
||||
* Calls the handler for each model link detected and replaces the link
|
||||
* with the handler return value if provided.
|
||||
* Returns the resulting content with links replaced.
|
||||
* @param callable(Model):(string|null) $handler
|
||||
*/
|
||||
public function parseLinks(string $content, callable $handler): string
|
||||
{
|
||||
$linkRegex = $this->getLinkRegex();
|
||||
$matches = [];
|
||||
preg_match_all($linkRegex, $content, $matches);
|
||||
|
||||
if (count($matches) < 2) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
foreach ($matches[1] as $link) {
|
||||
$model = $this->linkToModel($link);
|
||||
if ($model) {
|
||||
$result = $handler($model);
|
||||
if ($result !== null) {
|
||||
$content = str_replace($link, $result, $content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and replace references in the given content.
|
||||
* Calls the handler for each reference detected and replaces the link
|
||||
* with the handler return value if provided.
|
||||
* Returns the resulting content string with references replaced.
|
||||
* @param callable(string $type, int $id):(string|null) $handler
|
||||
*/
|
||||
public function parseReferences(string $content, callable $handler): string
|
||||
{
|
||||
$referenceRegex = '/\[\[bsexport:([a-z]+):(\d+)]]/';
|
||||
$matches = [];
|
||||
preg_match_all($referenceRegex, $content, $matches);
|
||||
|
||||
if (count($matches) < 3) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < count($matches[0]); $i++) {
|
||||
$referenceText = $matches[0][$i];
|
||||
$type = strtolower($matches[1][$i]);
|
||||
$id = intval($matches[2][$i]);
|
||||
$result = $handler($type, $id);
|
||||
if ($result !== null) {
|
||||
$content = str_replace($referenceText, $result, $content);
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempt to resolve the given link to a model using the instance model resolvers.
|
||||
*/
|
||||
protected function linkToModel(string $link): ?Model
|
||||
{
|
||||
foreach ($this->getModelResolvers() as $resolver) {
|
||||
$model = $resolver->resolve($link);
|
||||
if (!is_null($model)) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getModelResolvers(): array
|
||||
{
|
||||
if (isset($this->modelResolvers)) {
|
||||
return $this->modelResolvers;
|
||||
}
|
||||
|
||||
$this->modelResolvers = [
|
||||
new PagePermalinkModelResolver($this->queries->pages),
|
||||
new PageLinkModelResolver($this->queries->pages),
|
||||
new ChapterLinkModelResolver($this->queries->chapters),
|
||||
new BookLinkModelResolver($this->queries->books),
|
||||
new ImageModelResolver(),
|
||||
new AttachmentModelResolver(),
|
||||
];
|
||||
|
||||
return $this->modelResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the regex to identify links we should handle in content.
|
||||
*/
|
||||
protected function getLinkRegex(): string
|
||||
{
|
||||
$urls = [rtrim(url('/'), '/')];
|
||||
$imageUrl = rtrim(ImageStorage::getPublicUrl('/'), '/');
|
||||
if ($urls[0] !== $imageUrl) {
|
||||
$urls[] = $imageUrl;
|
||||
}
|
||||
|
||||
|
||||
$urlBaseRegex = implode('|', array_map(function ($url) {
|
||||
return preg_quote($url, '/');
|
||||
}, $urls));
|
||||
|
||||
return "/(({$urlBaseRegex}).*?)[\\t\\n\\f>\"'=?#()]/";
|
||||
}
|
||||
}
|
26
app/Exports/ZipExports/ZipUniqueIdRule.php
Normal file
26
app/Exports/ZipExports/ZipUniqueIdRule.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class ZipUniqueIdRule implements ValidationRule
|
||||
{
|
||||
public function __construct(
|
||||
protected ZipValidationHelper $context,
|
||||
protected string $modelType,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if ($this->context->hasIdBeenUsed($this->modelType, $value)) {
|
||||
$fail('validation.zip_unique')->translate(['attribute' => $attribute]);
|
||||
}
|
||||
}
|
||||
}
|
77
app/Exports/ZipExports/ZipValidationHelper.php
Normal file
77
app/Exports/ZipExports/ZipValidationHelper.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Exports\ZipExports;
|
||||
|
||||
use BookStack\Exports\ZipExports\Models\ZipExportModel;
|
||||
use Illuminate\Validation\Factory;
|
||||
|
||||
class ZipValidationHelper
|
||||
{
|
||||
protected Factory $validationFactory;
|
||||
|
||||
/**
|
||||
* Local store of validated IDs (in format "<type>:<id>". Example: "book:2")
|
||||
* which we can use to check uniqueness.
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
protected array $validatedIds = [];
|
||||
|
||||
public function __construct(
|
||||
public ZipExportReader $zipReader,
|
||||
) {
|
||||
$this->validationFactory = app(Factory::class);
|
||||
}
|
||||
|
||||
public function validateData(array $data, array $rules): array
|
||||
{
|
||||
$messages = $this->validationFactory->make($data, $rules)->errors()->messages();
|
||||
|
||||
foreach ($messages as $key => $message) {
|
||||
$messages[$key] = implode("\n", $message);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
public function fileReferenceRule(array $acceptedMimes = []): ZipFileReferenceRule
|
||||
{
|
||||
return new ZipFileReferenceRule($this, $acceptedMimes);
|
||||
}
|
||||
|
||||
public function uniqueIdRule(string $type): ZipUniqueIdRule
|
||||
{
|
||||
return new ZipUniqueIdRule($this, $type);
|
||||
}
|
||||
|
||||
public function hasIdBeenUsed(string $type, mixed $id): bool
|
||||
{
|
||||
$key = $type . ':' . $id;
|
||||
if (isset($this->validatedIds[$key])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->validatedIds[$key] = true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an array of relation data arrays that are expected
|
||||
* to be for the given ZipExportModel.
|
||||
* @param class-string<ZipExportModel> $model
|
||||
*/
|
||||
public function validateRelations(array $relations, string $model): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach ($relations as $key => $relationData) {
|
||||
if (is_array($relationData)) {
|
||||
$results[$key] = $model::validate($this, $relationData);
|
||||
} else {
|
||||
$results[$key] = [trans('validation.zip_model_expected', ['type' => gettype($relationData)])];
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
@ -152,10 +152,8 @@ abstract class Controller extends BaseController
|
||||
|
||||
/**
|
||||
* Log an activity in the system.
|
||||
*
|
||||
* @param string|Loggable $detail
|
||||
*/
|
||||
protected function logActivity(string $type, $detail = ''): void
|
||||
protected function logActivity(string $type, string|Loggable $detail = ''): void
|
||||
{
|
||||
Activity::add($type, $detail);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
class DownloadResponseFactory
|
||||
{
|
||||
public function __construct(
|
||||
protected Request $request
|
||||
protected Request $request,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -35,6 +35,33 @@ class DownloadResponseFactory
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a response that downloads the given file via a stream.
|
||||
* Has the option to delete the provided file once the stream is closed.
|
||||
*/
|
||||
public function streamedFileDirectly(string $filePath, string $fileName, bool $deleteAfter = false): StreamedResponse
|
||||
{
|
||||
$fileSize = filesize($filePath);
|
||||
$stream = fopen($filePath, 'r');
|
||||
|
||||
if ($deleteAfter) {
|
||||
// Delete the given file if it still exists after the app terminates
|
||||
$callback = function () use ($filePath) {
|
||||
if (file_exists($filePath)) {
|
||||
unlink($filePath);
|
||||
}
|
||||
};
|
||||
|
||||
// We watch both app terminate and php shutdown to cover both normal app termination
|
||||
// as well as other potential scenarios (connection termination).
|
||||
app()->terminating($callback);
|
||||
register_shutdown_function($callback);
|
||||
}
|
||||
|
||||
return $this->streamedDirectly($stream, $fileName, $fileSize);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a file download response that provides the file with a content-type
|
||||
* correct for the file, in a way so the browser can show the content in browser,
|
||||
@ -43,7 +70,7 @@ class DownloadResponseFactory
|
||||
public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse
|
||||
{
|
||||
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request);
|
||||
$mime = $rangeStream->sniffMime();
|
||||
$mime = $rangeStream->sniffMime(pathinfo($fileName, PATHINFO_EXTENSION));
|
||||
$headers = array_merge($this->getHeaders($fileName, $fileSize, $mime), $rangeStream->getResponseHeaders());
|
||||
|
||||
return response()->stream(
|
||||
@ -53,6 +80,22 @@ class DownloadResponseFactory
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a response that provides the given file via a stream with detected content-type.
|
||||
* Has the option to delete the provided file once the stream is closed.
|
||||
*/
|
||||
public function streamedFileInline(string $filePath, ?string $fileName = null): StreamedResponse
|
||||
{
|
||||
$fileSize = filesize($filePath);
|
||||
$stream = fopen($filePath, 'r');
|
||||
|
||||
if ($fileName === null) {
|
||||
$fileName = basename($filePath);
|
||||
}
|
||||
|
||||
return $this->streamedInline($stream, $fileName, $fileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the common headers to provide for a download response.
|
||||
*/
|
||||
|
@ -7,6 +7,13 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class PreventResponseCaching
|
||||
{
|
||||
/**
|
||||
* Paths to ignore when preventing response caching.
|
||||
*/
|
||||
protected array $ignoredPathPrefixes = [
|
||||
'theme/',
|
||||
];
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
@ -20,6 +27,13 @@ class PreventResponseCaching
|
||||
/** @var Response $response */
|
||||
$response = $next($request);
|
||||
|
||||
$path = $request->path();
|
||||
foreach ($this->ignoredPathPrefixes as $ignoredPath) {
|
||||
if (str_starts_with($path, $ignoredPath)) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
$response->headers->set('Cache-Control', 'no-cache, no-store, private');
|
||||
$response->headers->set('Expires', 'Sun, 12 Jul 2015 19:01:00 GMT');
|
||||
|
||||
|
@ -32,12 +32,12 @@ class RangeSupportedStream
|
||||
/**
|
||||
* Sniff a mime type from the stream.
|
||||
*/
|
||||
public function sniffMime(): string
|
||||
public function sniffMime(string $extension = ''): string
|
||||
{
|
||||
$offset = min(2000, $this->fileSize);
|
||||
$this->sniffContent = fread($this->stream, $offset);
|
||||
|
||||
return (new WebSafeMimeSniffer())->sniff($this->sniffContent);
|
||||
return (new WebSafeMimeSniffer())->sniff($this->sniffContent, $extension);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,7 +92,7 @@ class RangeSupportedStream
|
||||
if ($start < 0 || $start > $end) {
|
||||
$this->responseStatus = 416;
|
||||
$this->responseHeaders['Content-Range'] = sprintf('bytes */%s', $this->fileSize);
|
||||
} elseif ($end - $start < $this->fileSize - 1) {
|
||||
} else {
|
||||
$this->responseLength = $end < $this->fileSize ? $end - $start + 1 : -1;
|
||||
$this->responseOffset = $start;
|
||||
$this->responseStatus = 206;
|
||||
|
22
app/References/ModelResolvers/AttachmentModelResolver.php
Normal file
22
app/References/ModelResolvers/AttachmentModelResolver.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\References\ModelResolvers;
|
||||
|
||||
use BookStack\Uploads\Attachment;
|
||||
|
||||
class AttachmentModelResolver implements CrossLinkModelResolver
|
||||
{
|
||||
public function resolve(string $link): ?Attachment
|
||||
{
|
||||
$pattern = '/^' . preg_quote(url('/attachments'), '/') . '\/(\d+)/';
|
||||
$matches = [];
|
||||
$match = preg_match($pattern, $link, $matches);
|
||||
if (!$match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = intval($matches[1]);
|
||||
|
||||
return Attachment::query()->find($id);
|
||||
}
|
||||
}
|
58
app/References/ModelResolvers/ImageModelResolver.php
Normal file
58
app/References/ModelResolvers/ImageModelResolver.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\References\ModelResolvers;
|
||||
|
||||
use BookStack\Uploads\Image;
|
||||
use BookStack\Uploads\ImageStorage;
|
||||
|
||||
class ImageModelResolver implements CrossLinkModelResolver
|
||||
{
|
||||
protected ?string $pattern = null;
|
||||
|
||||
public function resolve(string $link): ?Image
|
||||
{
|
||||
$pattern = $this->getUrlPattern();
|
||||
$matches = [];
|
||||
$match = preg_match($pattern, $link, $matches);
|
||||
if (!$match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$path = $matches[2];
|
||||
|
||||
// Strip thumbnail element from path if existing
|
||||
$originalPathSplit = array_filter(explode('/', $path), function (string $part) {
|
||||
$resizedDir = (str_starts_with($part, 'thumbs-') || str_starts_with($part, 'scaled-'));
|
||||
$missingExtension = !str_contains($part, '.');
|
||||
|
||||
return !($resizedDir && $missingExtension);
|
||||
});
|
||||
|
||||
// Build a database-format image path and search for the image entry
|
||||
$fullPath = '/uploads/images/' . ltrim(implode('/', $originalPathSplit), '/');
|
||||
|
||||
return Image::query()->where('path', '=', $fullPath)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the regex pattern to identify image URLs.
|
||||
* Caches the pattern since it requires looking up to settings/config.
|
||||
*/
|
||||
protected function getUrlPattern(): string
|
||||
{
|
||||
if ($this->pattern) {
|
||||
return $this->pattern;
|
||||
}
|
||||
|
||||
$urls = [url('/uploads/images')];
|
||||
$baseImageUrl = ImageStorage::getPublicUrl('/uploads/images');
|
||||
if ($baseImageUrl !== $urls[0]) {
|
||||
$urls[] = $baseImageUrl;
|
||||
}
|
||||
|
||||
$imageUrlRegex = implode('|', array_map(fn ($url) => preg_quote($url, '/'), $urls));
|
||||
$this->pattern = '/^(' . $imageUrlRegex . ')\/(.+)/';
|
||||
|
||||
return $this->pattern;
|
||||
}
|
||||
}
|
@ -9,21 +9,18 @@ use Illuminate\Http\Request;
|
||||
|
||||
class SearchApiController extends ApiController
|
||||
{
|
||||
protected SearchRunner $searchRunner;
|
||||
protected SearchResultsFormatter $resultsFormatter;
|
||||
|
||||
protected $rules = [
|
||||
'all' => [
|
||||
'query' => ['required'],
|
||||
'page' => ['integer', 'min:1'],
|
||||
'count' => ['integer', 'min:1', 'max:100'],
|
||||
'query' => ['required'],
|
||||
'page' => ['integer', 'min:1'],
|
||||
'count' => ['integer', 'min:1', 'max:100'],
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(SearchRunner $searchRunner, SearchResultsFormatter $resultsFormatter)
|
||||
{
|
||||
$this->searchRunner = $searchRunner;
|
||||
$this->resultsFormatter = $resultsFormatter;
|
||||
public function __construct(
|
||||
protected SearchRunner $searchRunner,
|
||||
protected SearchResultsFormatter $resultsFormatter
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,16 +47,16 @@ class SearchApiController extends ApiController
|
||||
$this->resultsFormatter->format($results['results']->all(), $options);
|
||||
|
||||
$data = (new ApiEntityListFormatter($results['results']->all()))
|
||||
->withType()->withTags()
|
||||
->withType()->withTags()->withParents()
|
||||
->withField('preview_html', function (Entity $entity) {
|
||||
return [
|
||||
'name' => (string) $entity->getAttribute('preview_name'),
|
||||
'name' => (string) $entity->getAttribute('preview_name'),
|
||||
'content' => (string) $entity->getAttribute('preview_content'),
|
||||
];
|
||||
})->format();
|
||||
|
||||
return response()->json([
|
||||
'data' => $data,
|
||||
'data' => $data,
|
||||
'total' => $results['total'],
|
||||
]);
|
||||
}
|
||||
|
@ -16,7 +16,13 @@ class SearchIndex
|
||||
/**
|
||||
* A list of delimiter characters used to break-up parsed content into terms for indexing.
|
||||
*/
|
||||
public static string $delimiters = " \n\t.,!?:;()[]{}<>`'\"";
|
||||
public static string $delimiters = " \n\t.-,!?:;()[]{}<>`'\"«»";
|
||||
|
||||
/**
|
||||
* A list of delimiter which could be commonly used within a single term and also indicate a break between terms.
|
||||
* The indexer will index the full term with these delimiters, plus the terms split via these delimiters.
|
||||
*/
|
||||
public static string $softDelimiters = ".-";
|
||||
|
||||
public function __construct(
|
||||
protected EntityProvider $entityProvider
|
||||
@ -30,7 +36,7 @@ class SearchIndex
|
||||
{
|
||||
$this->deleteEntityTerms($entity);
|
||||
$terms = $this->entityToTermDataArray($entity);
|
||||
SearchTerm::query()->insert($terms);
|
||||
$this->insertTerms($terms);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,10 +52,7 @@ class SearchIndex
|
||||
array_push($terms, ...$entityTerms);
|
||||
}
|
||||
|
||||
$chunkedTerms = array_chunk($terms, 500);
|
||||
foreach ($chunkedTerms as $termChunk) {
|
||||
SearchTerm::query()->insert($termChunk);
|
||||
}
|
||||
$this->insertTerms($terms);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,6 +102,19 @@ class SearchIndex
|
||||
$entity->searchTerms()->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the given terms into the database.
|
||||
* Chunks through the given terms to remain within database limits.
|
||||
* @param array[] $terms
|
||||
*/
|
||||
protected function insertTerms(array $terms): void
|
||||
{
|
||||
$chunkedTerms = array_chunk($terms, 500);
|
||||
foreach ($chunkedTerms as $termChunk) {
|
||||
SearchTerm::query()->insert($termChunk);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a scored term array from the given text, where the keys are the terms
|
||||
* and the values are their scores.
|
||||
@ -186,15 +202,36 @@ class SearchIndex
|
||||
protected function textToTermCountMap(string $text): array
|
||||
{
|
||||
$tokenMap = []; // {TextToken => OccurrenceCount}
|
||||
$splitChars = static::$delimiters;
|
||||
$token = strtok($text, $splitChars);
|
||||
$softDelims = static::$softDelimiters;
|
||||
$tokenizer = new SearchTextTokenizer($text, static::$delimiters);
|
||||
$extendedToken = '';
|
||||
$extendedLen = 0;
|
||||
|
||||
$token = $tokenizer->next();
|
||||
|
||||
while ($token !== false) {
|
||||
if (!isset($tokenMap[$token])) {
|
||||
$tokenMap[$token] = 0;
|
||||
$delim = $tokenizer->previousDelimiter();
|
||||
|
||||
if ($delim && str_contains($softDelims, $delim) && $token !== '') {
|
||||
$extendedToken .= $delim . $token;
|
||||
$extendedLen++;
|
||||
} else {
|
||||
if ($extendedLen > 1) {
|
||||
$tokenMap[$extendedToken] = ($tokenMap[$extendedToken] ?? 0) + 1;
|
||||
}
|
||||
$extendedToken = $token;
|
||||
$extendedLen = 1;
|
||||
}
|
||||
$tokenMap[$token]++;
|
||||
$token = strtok($splitChars);
|
||||
|
||||
if ($token) {
|
||||
$tokenMap[$token] = ($tokenMap[$token] ?? 0) + 1;
|
||||
}
|
||||
|
||||
$token = $tokenizer->next();
|
||||
}
|
||||
|
||||
if ($extendedLen > 1) {
|
||||
$tokenMap[$extendedToken] = ($tokenMap[$extendedToken] ?? 0) + 1;
|
||||
}
|
||||
|
||||
return $tokenMap;
|
||||
|
@ -181,7 +181,7 @@ class SearchOptions
|
||||
protected static function parseStandardTermString(string $termString): array
|
||||
{
|
||||
$terms = explode(' ', $termString);
|
||||
$indexDelimiters = SearchIndex::$delimiters;
|
||||
$indexDelimiters = implode('', array_diff(str_split(SearchIndex::$delimiters), str_split(SearchIndex::$softDelimiters)));
|
||||
$parsed = [
|
||||
'terms' => [],
|
||||
'exacts' => [],
|
||||
|
70
app/Search/SearchTextTokenizer.php
Normal file
70
app/Search/SearchTextTokenizer.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Search;
|
||||
|
||||
/**
|
||||
* A custom text tokenizer which records & provides insight needed for our search indexing.
|
||||
* We used to use basic strtok() but this class does the following which that lacked:
|
||||
* - Tracks and provides the current/previous delimiter that we've stopped at.
|
||||
* - Returns empty tokens upon parsing a delimiter.
|
||||
*/
|
||||
class SearchTextTokenizer
|
||||
{
|
||||
protected int $currentIndex = 0;
|
||||
protected int $length;
|
||||
protected string $currentDelimiter = '';
|
||||
protected string $previousDelimiter = '';
|
||||
|
||||
public function __construct(
|
||||
protected string $text,
|
||||
protected string $delimiters = ' '
|
||||
) {
|
||||
$this->length = strlen($this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current delimiter to be found.
|
||||
*/
|
||||
public function currentDelimiter(): string
|
||||
{
|
||||
return $this->currentDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the previous delimiter found.
|
||||
*/
|
||||
public function previousDelimiter(): string
|
||||
{
|
||||
return $this->previousDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next token between delimiters.
|
||||
* Returns false if there's no further tokens.
|
||||
*/
|
||||
public function next(): string|false
|
||||
{
|
||||
$token = '';
|
||||
|
||||
for ($i = $this->currentIndex; $i < $this->length; $i++) {
|
||||
$char = $this->text[$i];
|
||||
if (str_contains($this->delimiters, $char)) {
|
||||
$this->previousDelimiter = $this->currentDelimiter;
|
||||
$this->currentDelimiter = $char;
|
||||
$this->currentIndex = $i + 1;
|
||||
return $token;
|
||||
}
|
||||
|
||||
$token .= $char;
|
||||
}
|
||||
|
||||
if ($token) {
|
||||
$this->currentIndex = $this->length;
|
||||
$this->previousDelimiter = $this->currentDelimiter;
|
||||
$this->currentDelimiter = '';
|
||||
return $token;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -9,8 +9,6 @@ use Illuminate\Http\Request;
|
||||
|
||||
class SettingController extends Controller
|
||||
{
|
||||
protected array $settingCategories = ['features', 'customization', 'registration'];
|
||||
|
||||
/**
|
||||
* Handle requests to the settings index path.
|
||||
*/
|
||||
@ -31,7 +29,7 @@ class SettingController extends Controller
|
||||
// Get application version
|
||||
$version = trim(file_get_contents(base_path('version')));
|
||||
|
||||
return view('settings.' . $category, [
|
||||
return view('settings.categories.' . $category, [
|
||||
'category' => $category,
|
||||
'version' => $version,
|
||||
'guestUser' => User::getGuest(),
|
||||
@ -59,7 +57,7 @@ class SettingController extends Controller
|
||||
|
||||
protected function ensureCategoryExists(string $category): void
|
||||
{
|
||||
if (!in_array($category, $this->settingCategories)) {
|
||||
if (!view()->exists('settings.categories.' . $category)) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Controllers;
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Entities\Queries\BookQueries;
|
||||
use BookStack\Entities\Tools\BookContents;
|
||||
use BookStack\Entities\Tools\BookSortMap;
|
||||
use BookStack\Facades\Activity;
|
||||
use BookStack\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
@ -45,25 +44,40 @@ class BookSortController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts a book using a given mapping array.
|
||||
* Update the sort options of a book, setting the auto-sort and/or updating
|
||||
* child order via mapping.
|
||||
*/
|
||||
public function update(Request $request, string $bookSlug)
|
||||
public function update(Request $request, BookSorter $sorter, string $bookSlug)
|
||||
{
|
||||
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
|
||||
$this->checkOwnablePermission('book-update', $book);
|
||||
$loggedActivityForBook = false;
|
||||
|
||||
// Return if no map sent
|
||||
if (!$request->filled('sort-tree')) {
|
||||
return redirect($book->getUrl());
|
||||
// Sort via map
|
||||
if ($request->filled('sort-tree')) {
|
||||
$sortMap = BookSortMap::fromJson($request->get('sort-tree'));
|
||||
$booksInvolved = $sorter->sortUsingMap($sortMap);
|
||||
|
||||
// Rebuild permissions and add activity for involved books.
|
||||
foreach ($booksInvolved as $bookInvolved) {
|
||||
Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
|
||||
if ($bookInvolved->id === $book->id) {
|
||||
$loggedActivityForBook = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sortMap = BookSortMap::fromJson($request->get('sort-tree'));
|
||||
$bookContents = new BookContents($book);
|
||||
$booksInvolved = $bookContents->sortUsingMap($sortMap);
|
||||
|
||||
// Rebuild permissions and add activity for involved books.
|
||||
foreach ($booksInvolved as $bookInvolved) {
|
||||
Activity::add(ActivityType::BOOK_SORT, $bookInvolved);
|
||||
if ($request->filled('auto-sort')) {
|
||||
$sortSetId = intval($request->get('auto-sort')) ?: null;
|
||||
if ($sortSetId && SortRule::query()->find($sortSetId) === null) {
|
||||
$sortSetId = null;
|
||||
}
|
||||
$book->sort_rule_id = $sortSetId;
|
||||
$book->save();
|
||||
$sorter->runBookAutoSort($book);
|
||||
if (!$loggedActivityForBook) {
|
||||
Activity::add(ActivityType::BOOK_SORT, $book);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect($book->getUrl());
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Tools;
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
class BookSortMap
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Entities\Tools;
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
class BookSortMapItem
|
||||
{
|
284
app/Sorting/BookSorter.php
Normal file
284
app/Sorting/BookSorter.php
Normal file
@ -0,0 +1,284 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use BookStack\App\Model;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\BookChild;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Queries\EntityQueries;
|
||||
|
||||
class BookSorter
|
||||
{
|
||||
public function __construct(
|
||||
protected EntityQueries $queries,
|
||||
) {
|
||||
}
|
||||
|
||||
public function runBookAutoSortForAllWithSet(SortRule $set): void
|
||||
{
|
||||
$set->books()->chunk(50, function ($books) {
|
||||
foreach ($books as $book) {
|
||||
$this->runBookAutoSort($book);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the auto-sort for a book if the book has a sort set applied to it.
|
||||
* This does not consider permissions since the sort operations are centrally
|
||||
* managed by admins so considered permitted if existing and assigned.
|
||||
*/
|
||||
public function runBookAutoSort(Book $book): void
|
||||
{
|
||||
$set = $book->sortRule;
|
||||
if (!$set) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sortFunctions = array_map(function (SortRuleOperation $op) {
|
||||
return $op->getSortFunction();
|
||||
}, $set->getOperations());
|
||||
|
||||
$chapters = $book->chapters()
|
||||
->with('pages:id,name,priority,created_at,updated_at,chapter_id')
|
||||
->get(['id', 'name', 'priority', 'created_at', 'updated_at']);
|
||||
|
||||
/** @var (Chapter|Book)[] $topItems */
|
||||
$topItems = [
|
||||
...$book->directPages()->get(['id', 'name', 'priority', 'created_at', 'updated_at']),
|
||||
...$chapters,
|
||||
];
|
||||
|
||||
foreach ($sortFunctions as $sortFunction) {
|
||||
usort($topItems, $sortFunction);
|
||||
}
|
||||
|
||||
foreach ($topItems as $index => $topItem) {
|
||||
$topItem->priority = $index + 1;
|
||||
$topItem::withoutTimestamps(fn () => $topItem->save());
|
||||
}
|
||||
|
||||
foreach ($chapters as $chapter) {
|
||||
$pages = $chapter->pages->all();
|
||||
foreach ($sortFunctions as $sortFunction) {
|
||||
usort($pages, $sortFunction);
|
||||
}
|
||||
|
||||
foreach ($pages as $index => $page) {
|
||||
$page->priority = $index + 1;
|
||||
$page::withoutTimestamps(fn () => $page->save());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort the books content using the given sort map.
|
||||
* Returns a list of books that were involved in the operation.
|
||||
*
|
||||
* @returns Book[]
|
||||
*/
|
||||
public function sortUsingMap(BookSortMap $sortMap): array
|
||||
{
|
||||
// Load models into map
|
||||
$modelMap = $this->loadModelsFromSortMap($sortMap);
|
||||
|
||||
// Sort our changes from our map to be chapters first
|
||||
// Since they need to be process to ensure book alignment for child page changes.
|
||||
$sortMapItems = $sortMap->all();
|
||||
usort($sortMapItems, function (BookSortMapItem $itemA, BookSortMapItem $itemB) {
|
||||
$aScore = $itemA->type === 'page' ? 2 : 1;
|
||||
$bScore = $itemB->type === 'page' ? 2 : 1;
|
||||
|
||||
return $aScore - $bScore;
|
||||
});
|
||||
|
||||
// Perform the sort
|
||||
foreach ($sortMapItems as $item) {
|
||||
$this->applySortUpdates($item, $modelMap);
|
||||
}
|
||||
|
||||
/** @var Book[] $booksInvolved */
|
||||
$booksInvolved = array_values(array_filter($modelMap, function (string $key) {
|
||||
return str_starts_with($key, 'book:');
|
||||
}, ARRAY_FILTER_USE_KEY));
|
||||
|
||||
// Update permissions of books involved
|
||||
foreach ($booksInvolved as $book) {
|
||||
$book->rebuildPermissions();
|
||||
}
|
||||
|
||||
return $booksInvolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Using the given sort map item, detect changes for the related model
|
||||
* and update it if required. Changes where permissions are lacking will
|
||||
* be skipped and not throw an error.
|
||||
*
|
||||
* @param array<string, Entity> $modelMap
|
||||
*/
|
||||
protected function applySortUpdates(BookSortMapItem $sortMapItem, array $modelMap): void
|
||||
{
|
||||
/** @var BookChild $model */
|
||||
$model = $modelMap[$sortMapItem->type . ':' . $sortMapItem->id] ?? null;
|
||||
if (!$model) {
|
||||
return;
|
||||
}
|
||||
|
||||
$priorityChanged = $model->priority !== $sortMapItem->sort;
|
||||
$bookChanged = $model->book_id !== $sortMapItem->parentBookId;
|
||||
$chapterChanged = ($model instanceof Page) && $model->chapter_id !== $sortMapItem->parentChapterId;
|
||||
|
||||
// Stop if there's no change
|
||||
if (!$priorityChanged && !$bookChanged && !$chapterChanged) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currentParentKey = 'book:' . $model->book_id;
|
||||
if ($model instanceof Page && $model->chapter_id) {
|
||||
$currentParentKey = 'chapter:' . $model->chapter_id;
|
||||
}
|
||||
|
||||
$currentParent = $modelMap[$currentParentKey] ?? null;
|
||||
/** @var Book $newBook */
|
||||
$newBook = $modelMap['book:' . $sortMapItem->parentBookId] ?? null;
|
||||
/** @var ?Chapter $newChapter */
|
||||
$newChapter = $sortMapItem->parentChapterId ? ($modelMap['chapter:' . $sortMapItem->parentChapterId] ?? null) : null;
|
||||
|
||||
if (!$this->isSortChangePermissible($sortMapItem, $model, $currentParent, $newBook, $newChapter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Action the required changes
|
||||
if ($bookChanged) {
|
||||
$model->changeBook($newBook->id);
|
||||
}
|
||||
|
||||
if ($model instanceof Page && $chapterChanged) {
|
||||
$model->chapter_id = $newChapter->id ?? 0;
|
||||
}
|
||||
|
||||
if ($priorityChanged) {
|
||||
$model->priority = $sortMapItem->sort;
|
||||
}
|
||||
|
||||
if ($chapterChanged || $priorityChanged) {
|
||||
$model::withoutTimestamps(fn () => $model->save());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permissions to apply the given sorting change.
|
||||
* Is quite complex since items can gain a different parent change. Acts as a:
|
||||
* - Update of old parent element (Change of content/order).
|
||||
* - Update of sorted/moved element.
|
||||
* - Deletion of element (Relative to parent upon move).
|
||||
* - Creation of element within parent (Upon move to new parent).
|
||||
*/
|
||||
protected function isSortChangePermissible(BookSortMapItem $sortMapItem, BookChild $model, ?Entity $currentParent, ?Entity $newBook, ?Entity $newChapter): bool
|
||||
{
|
||||
// Stop if we can't see the current parent or new book.
|
||||
if (!$currentParent || !$newBook) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hasNewParent = $newBook->id !== $model->book_id || ($model instanceof Page && $model->chapter_id !== ($sortMapItem->parentChapterId ?? 0));
|
||||
if ($model instanceof Chapter) {
|
||||
$hasPermission = userCan('book-update', $currentParent)
|
||||
&& userCan('book-update', $newBook)
|
||||
&& userCan('chapter-update', $model)
|
||||
&& (!$hasNewParent || userCan('chapter-create', $newBook))
|
||||
&& (!$hasNewParent || userCan('chapter-delete', $model));
|
||||
|
||||
if (!$hasPermission) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($model instanceof Page) {
|
||||
$parentPermission = ($currentParent instanceof Chapter) ? 'chapter-update' : 'book-update';
|
||||
$hasCurrentParentPermission = userCan($parentPermission, $currentParent);
|
||||
|
||||
// This needs to check if there was an intended chapter location in the original sort map
|
||||
// rather than inferring from the $newChapter since that variable may be null
|
||||
// due to other reasons (Visibility).
|
||||
$newParent = $sortMapItem->parentChapterId ? $newChapter : $newBook;
|
||||
if (!$newParent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hasPageEditPermission = userCan('page-update', $model);
|
||||
$newParentInRightLocation = ($newParent instanceof Book || ($newParent instanceof Chapter && $newParent->book_id === $newBook->id));
|
||||
$newParentPermission = ($newParent instanceof Chapter) ? 'chapter-update' : 'book-update';
|
||||
$hasNewParentPermission = userCan($newParentPermission, $newParent);
|
||||
|
||||
$hasDeletePermissionIfMoving = (!$hasNewParent || userCan('page-delete', $model));
|
||||
$hasCreatePermissionIfMoving = (!$hasNewParent || userCan('page-create', $newParent));
|
||||
|
||||
$hasPermission = $hasCurrentParentPermission
|
||||
&& $newParentInRightLocation
|
||||
&& $hasNewParentPermission
|
||||
&& $hasPageEditPermission
|
||||
&& $hasDeletePermissionIfMoving
|
||||
&& $hasCreatePermissionIfMoving;
|
||||
|
||||
if (!$hasPermission) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load models from the database into the given sort map.
|
||||
*
|
||||
* @return array<string, Entity>
|
||||
*/
|
||||
protected function loadModelsFromSortMap(BookSortMap $sortMap): array
|
||||
{
|
||||
$modelMap = [];
|
||||
$ids = [
|
||||
'chapter' => [],
|
||||
'page' => [],
|
||||
'book' => [],
|
||||
];
|
||||
|
||||
foreach ($sortMap->all() as $sortMapItem) {
|
||||
$ids[$sortMapItem->type][] = $sortMapItem->id;
|
||||
$ids['book'][] = $sortMapItem->parentBookId;
|
||||
if ($sortMapItem->parentChapterId) {
|
||||
$ids['chapter'][] = $sortMapItem->parentChapterId;
|
||||
}
|
||||
}
|
||||
|
||||
$pages = $this->queries->pages->visibleForList()->whereIn('id', array_unique($ids['page']))->get();
|
||||
/** @var Page $page */
|
||||
foreach ($pages as $page) {
|
||||
$modelMap['page:' . $page->id] = $page;
|
||||
$ids['book'][] = $page->book_id;
|
||||
if ($page->chapter_id) {
|
||||
$ids['chapter'][] = $page->chapter_id;
|
||||
}
|
||||
}
|
||||
|
||||
$chapters = $this->queries->chapters->visibleForList()->whereIn('id', array_unique($ids['chapter']))->get();
|
||||
/** @var Chapter $chapter */
|
||||
foreach ($chapters as $chapter) {
|
||||
$modelMap['chapter:' . $chapter->id] = $chapter;
|
||||
$ids['book'][] = $chapter->book_id;
|
||||
}
|
||||
|
||||
$books = $this->queries->books->visibleForList()->whereIn('id', array_unique($ids['book']))->get();
|
||||
/** @var Book $book */
|
||||
foreach ($books as $book) {
|
||||
$modelMap['book:' . $book->id] = $book;
|
||||
}
|
||||
|
||||
return $modelMap;
|
||||
}
|
||||
}
|
63
app/Sorting/SortRule.php
Normal file
63
app/Sorting/SortRule.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use BookStack\Activity\Models\Loggable;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $sequence
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
*/
|
||||
class SortRule extends Model implements Loggable
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return SortRuleOperation[]
|
||||
*/
|
||||
public function getOperations(): array
|
||||
{
|
||||
return SortRuleOperation::fromSequence($this->sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SortRuleOperation[] $options
|
||||
*/
|
||||
public function setOperations(array $options): void
|
||||
{
|
||||
$values = array_map(fn (SortRuleOperation $opt) => $opt->value, $options);
|
||||
$this->sequence = implode(',', $values);
|
||||
}
|
||||
|
||||
public function logDescriptor(): string
|
||||
{
|
||||
return "({$this->id}) {$this->name}";
|
||||
}
|
||||
|
||||
public function getUrl(): string
|
||||
{
|
||||
return url("/settings/sorting/rules/{$this->id}");
|
||||
}
|
||||
|
||||
public function books(): HasMany
|
||||
{
|
||||
return $this->hasMany(Book::class);
|
||||
}
|
||||
|
||||
public static function allByName(): Collection
|
||||
{
|
||||
return static::query()
|
||||
->withCount('books')
|
||||
->orderBy('name', 'asc')
|
||||
->get();
|
||||
}
|
||||
}
|
114
app/Sorting/SortRuleController.php
Normal file
114
app/Sorting/SortRuleController.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SortRuleController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('can:settings-manage');
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->setPageTitle(trans('settings.sort_rule_create'));
|
||||
|
||||
return view('settings.sort-rules.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'name' => ['required', 'string', 'min:1', 'max:200'],
|
||||
'sequence' => ['required', 'string', 'min:1'],
|
||||
]);
|
||||
|
||||
$operations = SortRuleOperation::fromSequence($request->input('sequence'));
|
||||
if (count($operations) === 0) {
|
||||
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
|
||||
}
|
||||
|
||||
$rule = new SortRule();
|
||||
$rule->name = $request->input('name');
|
||||
$rule->setOperations($operations);
|
||||
$rule->save();
|
||||
|
||||
$this->logActivity(ActivityType::SORT_RULE_CREATE, $rule);
|
||||
|
||||
return redirect('/settings/sorting');
|
||||
}
|
||||
|
||||
public function edit(string $id)
|
||||
{
|
||||
$rule = SortRule::query()->findOrFail($id);
|
||||
|
||||
$this->setPageTitle(trans('settings.sort_rule_edit'));
|
||||
|
||||
return view('settings.sort-rules.edit', ['rule' => $rule]);
|
||||
}
|
||||
|
||||
public function update(string $id, Request $request, BookSorter $bookSorter)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'name' => ['required', 'string', 'min:1', 'max:200'],
|
||||
'sequence' => ['required', 'string', 'min:1'],
|
||||
]);
|
||||
|
||||
$rule = SortRule::query()->findOrFail($id);
|
||||
$operations = SortRuleOperation::fromSequence($request->input('sequence'));
|
||||
if (count($operations) === 0) {
|
||||
return redirect($rule->getUrl())->withInput()->withErrors(['sequence' => 'No operations set.']);
|
||||
}
|
||||
|
||||
$rule->name = $request->input('name');
|
||||
$rule->setOperations($operations);
|
||||
$changedSequence = $rule->isDirty('sequence');
|
||||
$rule->save();
|
||||
|
||||
$this->logActivity(ActivityType::SORT_RULE_UPDATE, $rule);
|
||||
|
||||
if ($changedSequence) {
|
||||
$bookSorter->runBookAutoSortForAllWithSet($rule);
|
||||
}
|
||||
|
||||
return redirect('/settings/sorting');
|
||||
}
|
||||
|
||||
public function destroy(string $id, Request $request)
|
||||
{
|
||||
$rule = SortRule::query()->findOrFail($id);
|
||||
$confirmed = $request->input('confirm') === 'true';
|
||||
$booksAssigned = $rule->books()->count();
|
||||
$warnings = [];
|
||||
|
||||
if ($booksAssigned > 0) {
|
||||
if ($confirmed) {
|
||||
$rule->books()->update(['sort_rule_id' => null]);
|
||||
} else {
|
||||
$warnings[] = trans('settings.sort_rule_delete_warn_books', ['count' => $booksAssigned]);
|
||||
}
|
||||
}
|
||||
|
||||
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
|
||||
if ($defaultBookSortSetting === intval($id)) {
|
||||
if ($confirmed) {
|
||||
setting()->remove('sorting-book-default');
|
||||
} else {
|
||||
$warnings[] = trans('settings.sort_rule_delete_warn_default');
|
||||
}
|
||||
}
|
||||
|
||||
if (count($warnings) > 0) {
|
||||
return redirect($rule->getUrl() . '#delete')->withErrors(['delete' => $warnings]);
|
||||
}
|
||||
|
||||
$rule->delete();
|
||||
$this->logActivity(ActivityType::SORT_RULE_DELETE, $rule);
|
||||
|
||||
return redirect('/settings/sorting');
|
||||
}
|
||||
}
|
69
app/Sorting/SortRuleOperation.php
Normal file
69
app/Sorting/SortRuleOperation.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
enum SortRuleOperation: string
|
||||
{
|
||||
case NameAsc = 'name_asc';
|
||||
case NameDesc = 'name_desc';
|
||||
case NameNumericAsc = 'name_numeric_asc';
|
||||
case NameNumericDesc = 'name_numeric_desc';
|
||||
case CreatedDateAsc = 'created_date_asc';
|
||||
case CreatedDateDesc = 'created_date_desc';
|
||||
case UpdateDateAsc = 'updated_date_asc';
|
||||
case UpdateDateDesc = 'updated_date_desc';
|
||||
case ChaptersFirst = 'chapters_first';
|
||||
case ChaptersLast = 'chapters_last';
|
||||
|
||||
/**
|
||||
* Provide a translated label string for this option.
|
||||
*/
|
||||
public function getLabel(): string
|
||||
{
|
||||
$key = $this->value;
|
||||
$label = '';
|
||||
if (str_ends_with($key, '_asc')) {
|
||||
$key = substr($key, 0, -4);
|
||||
$label = trans('settings.sort_rule_op_asc');
|
||||
} elseif (str_ends_with($key, '_desc')) {
|
||||
$key = substr($key, 0, -5);
|
||||
$label = trans('settings.sort_rule_op_desc');
|
||||
}
|
||||
|
||||
$label = trans('settings.sort_rule_op_' . $key) . ' ' . $label;
|
||||
return trim($label);
|
||||
}
|
||||
|
||||
public function getSortFunction(): callable
|
||||
{
|
||||
$camelValue = Str::camel($this->value);
|
||||
return SortSetOperationComparisons::$camelValue(...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SortRuleOperation[]
|
||||
*/
|
||||
public static function allExcluding(array $operations): array
|
||||
{
|
||||
$all = SortRuleOperation::cases();
|
||||
$filtered = array_filter($all, function (SortRuleOperation $operation) use ($operations) {
|
||||
return !in_array($operation, $operations);
|
||||
});
|
||||
return array_values($filtered);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a set of operations from a string sequence representation.
|
||||
* (values seperated by commas).
|
||||
* @return SortRuleOperation[]
|
||||
*/
|
||||
public static function fromSequence(string $sequence): array
|
||||
{
|
||||
$strOptions = explode(',', $sequence);
|
||||
$options = array_map(fn ($val) => SortRuleOperation::tryFrom($val), $strOptions);
|
||||
return array_filter($options);
|
||||
}
|
||||
}
|
72
app/Sorting/SortSetOperationComparisons.php
Normal file
72
app/Sorting/SortSetOperationComparisons.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use voku\helper\ASCII;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
|
||||
/**
|
||||
* Sort comparison function for each of the possible SortSetOperation values.
|
||||
* Method names should be camelCase names for the SortSetOperation enum value.
|
||||
*/
|
||||
class SortSetOperationComparisons
|
||||
{
|
||||
public static function nameAsc(Entity $a, Entity $b): int
|
||||
{
|
||||
return strtolower(ASCII::to_transliterate($a->name, null)) <=> strtolower(ASCII::to_transliterate($b->name, null));
|
||||
}
|
||||
|
||||
public static function nameDesc(Entity $a, Entity $b): int
|
||||
{
|
||||
return strtolower(ASCII::to_transliterate($b->name, null)) <=> strtolower(ASCII::to_transliterate($a->name, null));
|
||||
}
|
||||
|
||||
public static function nameNumericAsc(Entity $a, Entity $b): int
|
||||
{
|
||||
$numRegex = '/^\d+(\.\d+)?/';
|
||||
$aMatches = [];
|
||||
$bMatches = [];
|
||||
preg_match($numRegex, $a->name, $aMatches);
|
||||
preg_match($numRegex, $b->name, $bMatches);
|
||||
$aVal = floatval(($aMatches[0] ?? 0));
|
||||
$bVal = floatval(($bMatches[0] ?? 0));
|
||||
|
||||
return $aVal <=> $bVal;
|
||||
}
|
||||
|
||||
public static function nameNumericDesc(Entity $a, Entity $b): int
|
||||
{
|
||||
return -(static::nameNumericAsc($a, $b));
|
||||
}
|
||||
|
||||
public static function createdDateAsc(Entity $a, Entity $b): int
|
||||
{
|
||||
return $a->created_at->unix() <=> $b->created_at->unix();
|
||||
}
|
||||
|
||||
public static function createdDateDesc(Entity $a, Entity $b): int
|
||||
{
|
||||
return $b->created_at->unix() <=> $a->created_at->unix();
|
||||
}
|
||||
|
||||
public static function updatedDateAsc(Entity $a, Entity $b): int
|
||||
{
|
||||
return $a->updated_at->unix() <=> $b->updated_at->unix();
|
||||
}
|
||||
|
||||
public static function updatedDateDesc(Entity $a, Entity $b): int
|
||||
{
|
||||
return $b->updated_at->unix() <=> $a->updated_at->unix();
|
||||
}
|
||||
|
||||
public static function chaptersFirst(Entity $a, Entity $b): int
|
||||
{
|
||||
return ($b instanceof Chapter ? 1 : 0) - (($a instanceof Chapter) ? 1 : 0);
|
||||
}
|
||||
|
||||
public static function chaptersLast(Entity $a, Entity $b): int
|
||||
{
|
||||
return ($a instanceof Chapter ? 1 : 0) - (($b instanceof Chapter) ? 1 : 0);
|
||||
}
|
||||
}
|
49
app/Sorting/SortUrl.php
Normal file
49
app/Sorting/SortUrl.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
/**
|
||||
* Generate a URL with multiple parameters for sorting purposes.
|
||||
* Works out the logic to set the correct sorting direction
|
||||
* Discards empty parameters and allows overriding.
|
||||
*/
|
||||
class SortUrl
|
||||
{
|
||||
public function __construct(
|
||||
protected string $path,
|
||||
protected array $data,
|
||||
protected array $overrideData = []
|
||||
) {
|
||||
}
|
||||
|
||||
public function withOverrideData(array $overrideData = []): self
|
||||
{
|
||||
return new self($this->path, $this->data, $overrideData);
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
$queryStringSections = [];
|
||||
$queryData = array_merge($this->data, $this->overrideData);
|
||||
|
||||
// Change sorting direction if already sorted on current attribute
|
||||
if (isset($this->overrideData['sort']) && $this->overrideData['sort'] === $this->data['sort']) {
|
||||
$queryData['order'] = ($this->data['order'] === 'asc') ? 'desc' : 'asc';
|
||||
} elseif (isset($this->overrideData['sort'])) {
|
||||
$queryData['order'] = 'asc';
|
||||
}
|
||||
|
||||
foreach ($queryData as $name => $value) {
|
||||
$trimmedVal = trim($value);
|
||||
if ($trimmedVal !== '') {
|
||||
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($queryStringSections) === 0) {
|
||||
return url($this->path);
|
||||
}
|
||||
|
||||
return url($this->path . '?' . implode('&', $queryStringSections));
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user