Upgrade to 3.3.1

This commit is contained in:
Bastian Allgeier
2019-11-26 11:04:08 +01:00
parent 23c67beafb
commit 3a82406bed
156 changed files with 437 additions and 121 deletions

View File

@@ -174,21 +174,19 @@ class Api
// set PHP locales based on *user* language
// so that e.g. strftime() gets formatted correctly
if (is_a($user, 'Kirby\Cms\User') === true) {
$locale = $language = $user->language();
$language = $user->language();
// if it's not already a full locale, "fake" one
// and assume that the country equals the language
if (Str::contains($locale, '_') !== true) {
$locale .= '_' . strtoupper($locale);
}
// get the locale from the translation
$translation = $user->kirby()->translation($language);
$locale = ($translation !== null)? $translation->locale() : $language;
// provide some variants as fallbacks to be
// compatible with as many systems as possible
$locales = [
$locale,
$locale . '.UTF-8',
$locale . '.UTF8',
$locale . '.ISO8859-1',
$locale,
$language,
setlocale(LC_ALL, 0) // fall back to the previously defined locale
];
@@ -225,7 +223,7 @@ class Api
* @param array|null $collection
* @return \Kirby\Api\Collection
*
* @throws NotFoundException If no collection for `$name` exists
* @throws \Kirby\Exception\NotFoundException If no collection for `$name` exists
*/
public function collection(string $name, $collection = null)
{
@@ -254,7 +252,7 @@ class Api
* @param mixed ...$args
* @return mixed
*
* @throws NotFoundException If no data for `$key` exists
* @throws \Kirby\Exception\NotFoundException If no data for `$key` exists
*/
public function data($key = null, ...$args)
{
@@ -302,7 +300,7 @@ class Api
* @param mixed $object
* @return \Kirby\Api\Model
*
* @throws NotFoundException If no model for `$name` exists
* @throws \Kirby\Exception\NotFoundException If no model for `$name` exists
*/
public function model(string $name, $object = null)
{
@@ -414,7 +412,7 @@ class Api
* @param mixed $object
* @return \Kirby\Api\Model|\Kirby\Api\Collection
*
* @throws NotFoundException If `$object` cannot be resolved
* @throws \Kirby\Exception\NotFoundException If `$object` cannot be resolved
*/
public function resolve($object)
{
@@ -705,8 +703,8 @@ class Api
* @param bool $single
* @return array
*
* @throws Exception If request has no files
* @throws Exception If there was an error with the upload
* @throws \Exception If request has no files
* @throws \Exception If there was an error with the upload
*/
public function upload(Closure $callback, $single = false): array
{

View File

@@ -67,7 +67,7 @@ class App
protected $routes;
protected $router;
protected $server;
protected $session;
protected $sessionHandler;
protected $site;
protected $system;
protected $urls;
@@ -784,6 +784,7 @@ class App
/**
* Returns the nonce, which is used
* in the panel for inline scripts
* @since 3.3.0
*
* @return string
*/
@@ -1105,8 +1106,18 @@ class App
*/
public function session(array $options = [])
{
$this->session = $this->session ?? new AutoSession($this->root('sessions'), $this->options['session'] ?? []);
return $this->session->get($options);
return $this->sessionHandler()->get($options);
}
/**
* Returns the session handler
*
* @return \Kirby\Session\AutoSession
*/
public function sessionHandler()
{
$this->sessionHandler = $this->sessionHandler ?? new AutoSession($this->root('sessions'), $this->option('session', []));
return $this->sessionHandler;
}
/**

View File

@@ -198,9 +198,9 @@ class Auth
* @param bool $long
* @return \Kirby\Cms\User
*
* @throws PermissionException If the rate limit was exceeded or if any other error occured with debug mode off
* @throws NotFoundException If the email was invalid
* @throws InvalidArgumentException If the password is not valid (via `$user->login()`)
* @throws \Kirby\Exception\PermissionException If the rate limit was exceeded or if any other error occured with debug mode off
* @throws \Kirby\Exception\NotFoundException If the email was invalid
* @throws \Kirby\Exception\InvalidArgumentException If the password is not valid (via `$user->login()`)
*/
public function login(string $email, string $password, bool $long = false)
{
@@ -214,10 +214,22 @@ class Auth
$user = $this->validatePassword($email, $password);
$user->loginPasswordless($options);
return $user;
}
/**
* Sets a user object as the current user in the cache
* @internal
*
* @param \Kirby\Cms\User $user
* @return void
*/
public function setUser(User $user): void
{
// stop impersonating
$this->impersonate = null;
return $this->user = $user;
$this->user = $user;
}
/**
@@ -228,9 +240,9 @@ class Auth
* @param string $password
* @return \Kirby\Cms\User
*
* @throws PermissionException If the rate limit was exceeded or if any other error occured with debug mode off
* @throws NotFoundException If the email was invalid
* @throws InvalidArgumentException If the password is not valid (via `$user->login()`)
* @throws \Kirby\Exception\PermissionException If the rate limit was exceeded or if any other error occured with debug mode off
* @throws \Kirby\Exception\NotFoundException If the email was invalid
* @throws \Kirby\Exception\InvalidArgumentException If the password is not valid (via `$user->login()`)
*/
public function validatePassword(string $email, string $password)
{
@@ -334,20 +346,30 @@ class Auth
/**
* Logout the current user
*
* @return bool
* @return void
*/
public function logout(): bool
public function logout(): void
{
// stop impersonating
// stop impersonating;
// ensures that we log out the actually logged in user
$this->impersonate = null;
// logout the current user if it exists
if ($user = $this->user()) {
$user->logout();
}
}
/**
* Clears the cached user data after logout
* @internal
*
* @return void
*/
public function flush(): void
{
$this->impersonate = null;
$this->user = null;
return true;
}
/**
@@ -415,7 +437,8 @@ class Auth
*
* @param \Kirby\Session\Session|array|null $session
* @return \Kirby\Cms\User
* @throws
*
* @throws \Throwable If an authentication error occured
*/
public function user($session = null)
{

View File

@@ -405,6 +405,7 @@ abstract class ModelWithContent extends Model
* Returns an array of all actions
* that can be performed in the Panel
* This also checks for the lock status
* @since 3.3.0
*
* @param array $unlock An array of options that will be force-unlocked
* @return array

View File

@@ -438,6 +438,7 @@ class Pages extends Collection
/**
* Filter all pages by excluding the given template
* @since 3.3.0
*
* @param string|array $templates
* @return \Kirby\Cms\Pages

View File

@@ -4,6 +4,7 @@ namespace Kirby\Cms;
use Exception;
use Kirby\Data\Data;
use Kirby\Toolkit\Str;
/**
* Wrapper around Kirby's localization files,
@@ -149,6 +150,21 @@ class Translation
}
}
/**
* Returns the PHP locale of the translation
*
* @return string
*/
public function locale(): string
{
$default = $this->code;
if (Str::contains($default, '_') !== true) {
$default .= '_' . strtoupper($this->code);
}
return $this->get('translation.locale', $default);
}
/**
* Returns the human-readable translation name.
*

View File

@@ -5,7 +5,6 @@ namespace Kirby\Cms;
use Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Exception\PermissionException;
use Kirby\Session\Session;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Str;
@@ -408,7 +407,7 @@ class User extends ModelWithContent
* @param \Kirby\Session\Session|array $session Session options or session object to set the user in
* @return bool
*
* @throws PermissionException If the password is not valid
* @throws \Kirby\Exception\PermissionException If the password is not valid
*/
public function login(string $password, $session = null): bool
{
@@ -434,6 +433,7 @@ class User extends ModelWithContent
$session->regenerateToken(); // privilege change
$session->data()->set('user.id', $this->id());
$this->kirby()->auth()->setUser($this);
$kirby->trigger('user.login:after', $this, $session);
}
@@ -451,8 +451,12 @@ class User extends ModelWithContent
$kirby->trigger('user.logout:before', $this, $session);
// remove the user from the session for future requests
$session->data()->remove('user.id');
// clear the cached user object from the app state of the current request
$this->kirby()->auth()->flush();
if ($session->data()->get() === []) {
// session is now empty, we might as well destroy it
$session->destroy();
@@ -872,9 +876,9 @@ class User extends ModelWithContent
* @param string $password
* @return bool
*
* @throws NotFoundException If the user has no password
* @throws InvalidArgumentException If the entered password is not valid
* @throws InvalidArgumentException If the entered password does not match the user password
* @throws \Kirby\Exception\NotFoundException If the user has no password
* @throws \Kirby\Exception\InvalidArgumentException If the entered password is not valid
* @throws \Kirby\Exception\InvalidArgumentException If the entered password does not match the user password
*/
public function validatePassword(string $password = null): bool
{

View File

@@ -5,6 +5,7 @@ namespace Kirby\Exception;
/**
* ErrorPageException
* Thrown to trigger the CMS error page
* @since 3.3.0
*
* @package Kirby Exception
* @author Lukas Bestle <lukas@getkirby.com>

View File

@@ -180,6 +180,7 @@ class Visitor
/**
* Returns the MIME type from the provided list that
* is most accepted (= preferred) by the visitor
* @since 3.3.0
*
* @param string ...$mimeTypes MIME types to query for
* @return string|null Preferred MIME type
@@ -206,6 +207,7 @@ class Visitor
/**
* Returns true if the visitor prefers a JSON response over
* an HTML response based on the `Accept` request header
* @since 3.3.0
*
* @return bool
*/

View File

@@ -6,6 +6,7 @@ use Exception;
use Kirby\Http\Response;
use Kirby\Toolkit\File;
use Kirby\Toolkit\Html;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Mime;
use Kirby\Toolkit\V;
@@ -205,30 +206,35 @@ class Image extends File
{
if (($rules['mime'] ?? null) !== null) {
if (Mime::isAccepted($this->mime(), $rules['mime']) !== true) {
throw new Exception(sprintf('Invalid mime type: %s', $this->mime()));
throw new Exception(I18n::template('error.file.mime.invalid', [
'mime' => $this->mime()
]));
}
}
$rules = array_change_key_case($rules);
$validations = [
'maxsize' => ['size', 'max', 'The file is too large'],
'minsize' => ['size', 'min', 'The file is too small'],
'maxwidth' => ['width', 'max', 'The width of the image must not exceed %s pixels'],
'minwidth' => ['width', 'min', 'The width of the image must be at least %s pixels'],
'maxheight' => ['height', 'max', 'The height of the image must not exceed %s pixels'],
'minheight' => ['height', 'min', 'The height of the image must be at least %s pixels'],
'orientation' => ['orientation', 'same', 'The orientation of the image must be "%s"']
'maxsize' => ['size', 'max'],
'minsize' => ['size', 'min'],
'maxwidth' => ['width', 'max'],
'minwidth' => ['width', 'min'],
'maxheight' => ['height', 'max'],
'minheight' => ['height', 'min'],
'orientation' => ['orientation', 'same']
];
foreach ($validations as $key => $arguments) {
if (isset($rules[$key]) === true && $rules[$key] !== null) {
$rule = $rules[$key] ?? null;
if ($rule !== null) {
$property = $arguments[0];
$validator = $arguments[1];
$message = $arguments[2];
if (V::$validator($this->$property(), $rules[$key]) === false) {
throw new Exception(sprintf($message, $rules[$key]));
if (V::$validator($this->$property(), $rule) === false) {
throw new Exception(I18n::template('error.file.' . $key, [
$property => $rule
]));
}
}
}

View File

@@ -144,6 +144,18 @@ class AutoSession
return $this->sessions->create($options);
}
/**
* Returns the specified Session object
* @since 3.3.1
*
* @param string $token Session token, either including or without the key
* @return \Kirby\Session\Session
*/
public function getManually(string $token)
{
return $this->sessions->get($token, 'manual');
}
/**
* Deletes all expired sessions
*

View File

@@ -544,6 +544,7 @@ class Collection extends Iterator implements Countable
/**
* Returns a Collection with the intersection of the given elements
* @since 3.3.0
*
* @param \Kirby\Toolkit\Collection $other
* @return \Kirby\Toolkit\Collection
@@ -555,6 +556,7 @@ class Collection extends Iterator implements Countable
/**
* Checks if there is an intersection between the given collection and this collection
* @since 3.3.0
*
* @param \Kirby\Toolkit\Collection $other
* @return bool
@@ -1060,6 +1062,7 @@ class Collection extends Iterator implements Countable
* is true. If the first parameter is false, the Closure will not be executed.
* You may pass another Closure as the third parameter to the when method.
* This Closure will execute if the first parameter evaluates as false
* @since 3.3.0
*
* @param mixed $condition
* @param Closure $callback

View File

@@ -242,6 +242,7 @@ class Mime
/**
* Tests if a MIME wildcard pattern from an `Accept` header
* matches a given type
* @since 3.3.0
*
* @param string $test
* @param string $wildcard