Merge branch 'release/1.6.12'

This commit is contained in:
Andy Miller
2019-08-14 16:19:43 -06:00
30 changed files with 775 additions and 374 deletions

View File

@@ -1,3 +1,32 @@
# v1.6.12
## 08/11/2019
1. [](#new)
* Added support for custom `FormFlash` save locations
* Added a new `Utils::arrayLower()` method for lowercasing arrays
* Support new GRAV_BASEDIR environment variable [#2541](https://github.com/getgrav/grav/pull/2541)
* Allow users to override plugin handler priorities [#2165](https://github.com/getgrav/grav/pull/2165)
1. [](#improved)
* Use new `Utils::getSupportedPageTypes()` to enforce `html,htm` at the front of the list [#2531](https://github.com/getgrav/grav/issues/2531)
* Updated vendor libraries
* Markdown filter is now page-aware so that it works with modular references [admin#1731](https://github.com/getgrav/grav-plugin-admin/issues/1731)
* Check of `GRAV_USER_INSTANCE` constant is already defined [#2621](https://github.com/getgrav/grav/pull/2621)
1. [](#bugfix)
* Fixed some potential issues when `$grav['user']` is not set
* Fixed error when calling `Media::add($name, null)`
* Fixed `url()` returning wrong path if using stream with grav root path in it, eg: `user-data://shop` when Grav is in `/shop`
* Fixed `url()` not returning a path to non-existing file (`user-data://shop` => `/user/data/shop`) if it is set to fail gracefully
* Fixed `url()` returning false on unknown streams, such as `ftp://domain.com`, they should be treated as external URL
* Fixed Flex User to have permissions to save and delete his own user
* Fixed new Flex User creation not being possible because of username could not be given
* Fixed fatal error 'Expiration date must be an integer, a DateInterval or null, "double" given' [#2529](https://github.com/getgrav/grav/issues/2529)
* Fixed non-existing Flex object having a bad media folder
* Fixed collections using `page@.self:` should allow modular pages if requested
* Fixed an error when trying to delete a file from non-existing Flex Object
* Fixed `FlexObject::exists()` failing sometimes just after the object has been saved
* Fixed CSV formatter not encoding strings with `"` and `,` properly
* Fixed var order in `Validation.php` [#2610](https://github.com/getgrav/grav/issues/2610)
# v1.6.11
## 06/21/2019

549
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,5 +12,7 @@ form:
type: text
label: PLUGIN_ADMIN.USERNAME
help: PLUGIN_ADMIN.USERNAME_HELP
unset-disabled@: true
unset-readonly@: true
validate:
required: true

View File

@@ -27,3 +27,13 @@ config:
title: Accounts
icon: fa-users
authorize: ['admin.users', 'admin.accounts', 'admin.super']
form:
fields:
username:
flex-disabled@: exists
disabled: false
flex-readonly@: exists
readonly: false
validate:
required: true

View File

@@ -8,7 +8,7 @@
// Some standard defines
define('GRAV', true);
define('GRAV_VERSION', '1.6.11');
define('GRAV_VERSION', '1.6.12');
define('GRAV_TESTING', false);
define('DS', '/');

View File

@@ -17,11 +17,22 @@ if (is_file($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $_SERVER['SCRIPT_N
return false;
}
$grav_index = 'index.php';
/* Check the GRAV_BASEDIR environment variable and use if set */
$grav_basedir = getenv('GRAV_BASEDIR') ?: '';
if (isset($grav_basedir)) {
$grav_index = ltrim($grav_basedir, '/') . DIRECTORY_SEPARATOR . $grav_index;
$grav_basedir = DIRECTORY_SEPARATOR . trim($grav_basedir, DIRECTORY_SEPARATOR);
define('GRAV_ROOT', str_replace(DIRECTORY_SEPARATOR, '/', getcwd()) . $grav_basedir);
}
$_SERVER = array_merge($_SERVER, $_ENV);
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . $grav_basedir .DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['SCRIPT_NAME'] = $grav_basedir . DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['PHP_SELF'] = $grav_basedir . DIRECTORY_SEPARATOR . 'index.php';
error_log(sprintf('%s:%d [%d]: %s', $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT'], http_response_code(), $_SERVER['REQUEST_URI']), 4);
require 'index.php';
require $grav_index;

View File

@@ -531,7 +531,6 @@ class Cache extends Getters
}
/**
* Set the cache lifetime programmatically
*
@@ -543,7 +542,7 @@ class Cache extends Getters
return;
}
$interval = $future - $this->now;
$interval = (int)($future - $this->now);
if ($interval > 0 && $interval < $this->getLifetime()) {
$this->lifetime = $interval;
}
@@ -558,7 +557,7 @@ class Cache extends Getters
public function getLifetime()
{
if ($this->lifetime === null) {
$this->lifetime = $this->config->get('system.cache.lifetime') ?: 604800; // 1 week default
$this->lifetime = (int)($this->config->get('system.cache.lifetime') ?: 604800); // 1 week default
}
return $this->lifetime;

View File

@@ -378,14 +378,12 @@ class Blueprint extends BlueprintForm
$grav = Grav::instance();
$actions = (array)$call['params'];
/** @var UserInterface $user */
if (isset($grav['user'])) {
$user = Grav::instance()['user'];
foreach ($actions as $action) {
if (!$user->authorize($action)) {
$this->addPropertyRecursive($field, 'validate', ['ignore' => true]);
return;
}
/** @var UserInterface|null $user */
$user = $grav['user'] ?? null;
foreach ($actions as $action) {
if (!$user || !$user->authorize($action)) {
$this->addPropertyRecursive($field, 'validate', ['ignore' => true]);
return;
}
}
}

View File

@@ -27,8 +27,9 @@ class Validation
if (!isset($field['type'])) {
$field['type'] = 'text';
}
$type = $validate['type'] ?? $field['type'];
$validate = (array)($field['validate'] ?? null);
$type = $validate['type'] ?? $field['type'];
$required = $validate['required'] ?? false;
// If value isn't required, we will stop validation if empty value is given.

View File

@@ -103,7 +103,7 @@ class Licenses
}
/**
* Get's the License File object
* Get the License File object
*
* @return \RocketTheme\Toolbox\File\FileInterface
*/

View File

@@ -234,7 +234,7 @@ class Language
}
/**
* Get's a URL prefix based on configuration
* Get a URL prefix based on configuration
*
* @param string|null $lang
* @return string

View File

@@ -154,6 +154,9 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
*/
public function add($name, $file)
{
if (!$file) {
return;
}
$this->offsetSet($name, $file);
switch ($file->type) {
case 'image':

View File

@@ -232,7 +232,7 @@ class ImageMedium extends Medium
}
/**
* Allows the ability to override the Inmage's Pretty name stored in cache
* Allows the ability to override the image's pretty name stored in cache
*
* @param string $name
*/

View File

@@ -1410,7 +1410,7 @@ class Page implements PageInterface
if (is_string($http_accept)) {
$negotiator = new Negotiator();
$supported_types = Grav::instance()['config']->get('system.pages.types', ['html', 'json']);
$supported_types = Utils::getSupportPageTypes(['html', 'json']);
$priorities = Utils::getMimeTypes($supported_types);
$media_type = $negotiator->getBest($http_accept, $priorities);
@@ -2938,7 +2938,7 @@ class Page implements PageInterface
case 'page':
case 'self':
$results = new Collection();
$results = $results->addPage($page)->nonModular();
$results = $results->addPage($page);
break;
case 'descendants':

View File

@@ -151,15 +151,32 @@ class Plugin implements EventSubscriberInterface, \ArrayAccess
if (\is_string($params)) {
$dispatcher->addListener($eventName, [$this, $params]);
} elseif (\is_string($params[0])) {
$dispatcher->addListener($eventName, [$this, $params[0]], $params[1] ?? 0);
$dispatcher->addListener($eventName, [$this, $params[0]], $this->getPriority($params, $eventName));
} else {
foreach ($params as $listener) {
$dispatcher->addListener($eventName, [$this, $listener[0]], $listener[1] ?? 0);
$dispatcher->addListener($eventName, [$this, $listener[0]], $this->getPriority($listener, $eventName));
}
}
}
}
/**
* @param array $params
* @param string $eventName
*/
private function getPriority($params, $eventName)
{
$grav = Grav::instance();
$override = implode('.', ["priorities", $this->name, $eventName, $params[0]]);
if ($grav['config']->get($override) !== null)
{
return $grav['config']->get($override);
} elseif (isset($params[1])) {
return $params[1];
}
return 0;
}
/**
* @param array $events
*/

View File

@@ -27,9 +27,10 @@ class AccountsServiceProvider implements ServiceProviderInterface
public function register(Container $container)
{
$container['accounts'] = function (Container $container) {
/** @var Debugger $debugger */
$debugger = $container['debugger'];
if ($container['config']->get('system.accounts.type') === 'flex') {
$type = strtolower(defined('GRAV_USER_INSTANCE') ? GRAV_USER_INSTANCE : $container['config']->get('system.accounts.type', 'data'));
if ($type === 'flex') {
/** @var Debugger $debugger */
$debugger = $container['debugger'];
$debugger->addMessage('User Accounts: Flex Directory');
return $this->flexAccounts($container);
}
@@ -46,7 +47,9 @@ class AccountsServiceProvider implements ServiceProviderInterface
protected function dataAccounts(Container $container)
{
define('GRAV_USER_INSTANCE', 'DATA');
if (!defined('GRAV_USER_INSTANCE')) {
define('GRAV_USER_INSTANCE', 'DATA');
}
// Use User class for backwards compatibility.
return new DataUser\UserCollection(User::class);
@@ -54,7 +57,9 @@ class AccountsServiceProvider implements ServiceProviderInterface
protected function flexAccounts(Container $container)
{
define('GRAV_USER_INSTANCE', 'FLEX');
if (!defined('GRAV_USER_INSTANCE')) {
define('GRAV_USER_INSTANCE', 'FLEX');
}
/** @var Config $config */
$config = $container['config'];

View File

@@ -84,7 +84,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
new \Twig_SimpleFilter('fieldName', [$this, 'fieldNameFilter']),
new \Twig_SimpleFilter('ksort', [$this, 'ksortFilter']),
new \Twig_SimpleFilter('ltrim', [$this, 'ltrimFilter']),
new \Twig_SimpleFilter('markdown', [$this, 'markdownFunction'], ['is_safe' => ['html']]),
new \Twig_SimpleFilter('markdown', [$this, 'markdownFunction'], ['needs_context' => true, 'is_safe' => ['html']]),
new \Twig_SimpleFilter('md5', [$this, 'md5Filter']),
new \Twig_SimpleFilter('base32_encode', [$this, 'base32EncodeFilter']),
new \Twig_SimpleFilter('base32_decode', [$this, 'base32DecodeFilter']),
@@ -455,7 +455,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
}
/**
* Gets a human readable output for cron sytnax
* Gets a human readable output for cron syntax
*
* @param $at
* @return string
@@ -616,9 +616,10 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
* @param bool $block Block or Line processing
* @return mixed|string
*/
public function markdownFunction($string, $block = true)
public function markdownFunction($context = false, $string, $block = true)
{
return Utils::processMarkdown($string, $block);
$page = $context['page'] ?? null;
return Utils::processMarkdown($string, $block, $page);
}
/**
@@ -1004,10 +1005,10 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
*/
public function authorize($action)
{
/** @var UserInterface $user */
$user = $this->grav['user'];
/** @var UserInterface|null $user */
$user = $this->grav['user'] ?? null;
if (!$user->authenticated || (isset($user->authorized) && !$user->authorized)) {
if (!$user || !$user->authenticated || (isset($user->authorized) && !$user->authorized)) {
return false;
}
@@ -1136,7 +1137,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
}
/**
* Get's the Exif data for a file
* Get the Exif data for a file
*
* @param string $image
* @param bool $raw

View File

@@ -1286,7 +1286,7 @@ class Uri
}
/**
* Get's post from either $_POST or JSON response object
* Get post from either $_POST or JSON response object
* By default returns all data, or can return a single item
*
* @param string $element
@@ -1345,7 +1345,7 @@ class Uri
*/
public function isValidExtension($extension)
{
$valid_page_types = implode('|', Grav::instance()['config']->get('system.pages.types'));
$valid_page_types = implode('|', Utils::getSupportPageTypes());
// Strip the file extension for valid page types
if (preg_match('/(' . $valid_page_types . ')/', $extension)) {

View File

@@ -9,6 +9,7 @@
namespace Grav\Common\User\FlexUser;
use Grav\Common\Data\Blueprint;
use Grav\Common\Grav;
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
use Grav\Common\Page\Media;
@@ -381,6 +382,31 @@ class User extends FlexObject implements UserInterface, MediaManipulationInterfa
return $this->getBlueprint()->extra($this->toArray());
}
/**
* @param string $name
* @return Blueprint
*/
public function getBlueprint(string $name = '')
{
$blueprint = clone parent::getBlueprint($name);
$blueprint->addDynamicHandler('flex', function (array &$field, $property, array &$call) {
$params = (array)$call['params'];
$method = array_shift($params);
if (method_exists($this, $method)) {
$value = $this->{$method}(...$params);
if (\is_array($value) && isset($field[$property]) && \is_array($field[$property])) {
$field[$property] = array_merge_recursive($field[$property], $value);
} else {
$field[$property] = $value;
}
}
});
return $blueprint->init();
}
/**
* Return unmodified data as raw string.
*
@@ -431,6 +457,20 @@ class User extends FlexObject implements UserInterface, MediaManipulationInterfa
return parent::save();
}
public function isAuthorized(string $action, string $scope = null, UserInterface $user = null): bool
{
if (null === $user) {
/** @var UserInterface $user */
$user = Grav::instance()['user'] ?? null;
}
if ($user instanceof User && $user->getStorageKey() === $this->getStorageKey()) {
return true;
}
return parent::isAuthorized($action, $scope, $user);
}
/**
* @return array
*/

View File

@@ -28,7 +28,7 @@ abstract class Utils
/**
* Simple helper method to make getting a Grav URL easier
*
* @param string $input
* @param string|object $input
* @param bool $domain
* @param bool $fail_gracefully
* @return bool|null|string
@@ -43,47 +43,80 @@ abstract class Utils
}
}
if (Grav::instance()['config']->get('system.absolute_urls', false)) {
$domain = true;
}
$input = (string)$input;
if (Uri::isExternal($input)) {
return $input;
}
$grav = Grav::instance();
/** @var Uri $uri */
$uri = Grav::instance()['uri'];
$uri = $grav['uri'];
$root = $uri->rootUrl();
$input = Utils::replaceFirstOccurrence($root, '', $input);
$input = ltrim((string)$input, '/');
if (Utils::contains((string)$input, '://')) {
if (static::contains((string)$input, '://')) {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$locator = $grav['locator'];
$parts = Uri::parseUrl($input);
if ($parts) {
try {
$resource = $locator->findResource("{$parts['scheme']}://{$parts['host']}{$parts['path']}", false);
} catch (\Exception $e) {
if ($fail_gracefully) {
return $input;
} else {
return false;
if (is_array($parts)) {
// Make sure we always have scheme, host, port and path.
$scheme = $parts['scheme'] ?? '';
$host = $parts['host'] ?? '';
$port = $parts['port'] ?? '';
$path = $parts['path'] ?? '';
if ($scheme && !$port) {
// If URL has a scheme, we need to check if it's one of Grav streams.
if (!$locator->schemeExists($scheme)) {
// If scheme does not exists as a stream, assume it's external.
return str_replace(' ', '%20', $input);
}
// Attempt to find the resource (because of parse_url() we need to put host back to path).
$resource = $locator->findResource("{$scheme}://{$host}{$path}", false);
if ($resource === false) {
if (!$fail_gracefully) {
return false;
}
// Return location where the file would be if it was saved.
$resource = $locator->findResource("{$scheme}://{$host}{$path}", false, true);
}
} elseif ($host || $port) {
// If URL doesn't have scheme but has host or port, it is external.
return str_replace(' ', '%20', $input);
}
if (!empty($resource)) {
// Add query string back.
if (isset($parts['query'])) {
$resource .= '?' . $parts['query'];
}
// Add fragment back.
if (isset($parts['fragment'])) {
$resource .= '#' . $parts['fragment'];
}
}
if ($resource && isset($parts['query'])) {
$resource = $resource . '?' . $parts['query'];
}
} else {
// Not a valid URL (can still be a stream).
$resource = $locator->findResource($input, false);
}
} else {
$root = $uri->rootUrl();
if (static::startsWith($input, $root)) {
$input = static::replaceFirstOccurrence($root, '', $input);
}
$input = ltrim($input, '/');
$resource = $input;
}
@@ -91,6 +124,8 @@ abstract class Utils
return false;
}
$domain = $domain ?: $grav['config']->get('system.absolute_urls', false);
return rtrim($uri->rootUrl($domain), '/') . '/' . ($resource ?? '');
}
@@ -288,6 +323,35 @@ abstract class Utils
return (object)array_merge((array)$obj1, (array)$obj2);
}
/**
* Lowercase an entire array. Useful when combined with `in_array()`
*
* @param array $a
* @return array|false
*/
public static function arrayLower(Array $a)
{
return array_map('mb_strtolower', $a);
}
/**
* Simple function to remove item/s in an array by value
*
* @param $search array
* @param $value string|array
* @return array
*/
public static function arrayRemoveValue(Array $search, $value)
{
foreach ((array) $value as $val) {
$key = array_search($val, $search);
if ($key !== false) {
unset($search[$key]);
}
}
return $search;
}
/**
* Recursive Merge with uniqueness
*
@@ -1070,12 +1134,9 @@ abstract class Utils
*/
private static function generateNonceString($action, $previousTick = false)
{
$username = '';
if (isset(Grav::instance()['user'])) {
$user = Grav::instance()['user'];
$username = $user->username;
}
$grav = Grav::instance();
$username = isset($grav['user']) ? $grav['user']->username : '';
$token = session_id();
$i = self::nonceTick();
@@ -1083,7 +1144,7 @@ abstract class Utils
$i--;
}
return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . Grav::instance()['config']->get('security.salt'));
return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . $grav['config']->get('security.salt'));
}
/**
@@ -1297,7 +1358,7 @@ abstract class Utils
}
/**
* Get's path based on a token
* Get path based on a token
*
* @param string $path
* @param PageInterface|null $page
@@ -1465,13 +1526,15 @@ abstract class Utils
*
* @param string $string
*
* @param bool $block Block or Line processing
* @param bool $block Block or Line processing
* @param null $page
* @return string
* @throws \Exception
*/
public static function processMarkdown($string, $block = true)
public static function processMarkdown($string, $block = true, $page = null)
{
$grav = Grav::instance();
$page = $grav['page'] ?? null;
$page = $page ?? $grav['page'] ?? null;
$defaults = [
'markdown' => $grav['config']->get('system.pages.markdown', []),
'images' => $grav['config']->get('system.images', [])
@@ -1534,4 +1597,23 @@ abstract class Utils
return $subnet;
}
/**
* Wrapper to ensure html, htm in the front of the supported page types
*
* @param array|null $defaults
* @return array|mixed
*/
public static function getSupportPageTypes(array $defaults = null)
{
$types = Grav::instance()['config']->get('system.pages.types', $defaults);
// remove html/htm
$types = static::arrayRemoveValue($types, ['html', 'htm']);
// put them back at the front
$types = array_merge(['html', 'htm'], $types);
return $types;
}
}

View File

@@ -437,7 +437,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
}
/**
* Implementes JsonSerializable interface.
* Implements JsonSerializable interface.
*
* @return array
*/

View File

@@ -84,7 +84,7 @@ class ArrayCollection extends BaseArrayCollection implements CollectionInterface
}
/**
* Implementes JsonSerializable interface.
* Implements JsonSerializable interface.
*
* @return array
*/

View File

@@ -53,11 +53,11 @@ class CsvFormatter extends AbstractFormatter
$header = array_keys(reset($data));
// Encode the field names
$string = implode($delimiter, $header). "\n";
$string = $this->encodeLine($header, $delimiter);
// Encode the data
foreach ($data as $row) {
$string .= implode($delimiter, $row). "\n";
$string .= $this->encodeLine($row, $delimiter);
}
return $string;
@@ -87,4 +87,23 @@ class CsvFormatter extends AbstractFormatter
return $list;
}
protected function encodeLine(array $line, $delimiter = null): string
{
foreach ($line as $key => &$value) {
$value = $this->escape((string)$value);
}
unset($value);
return implode($delimiter, $line). "\n";
}
protected function escape(string $value)
{
if (preg_match('/[,"\r\n]/u', $value)) {
$value = '"' . preg_replace('/"/', '""', $value) . '"';
}
return $value;
}
}

View File

@@ -97,7 +97,7 @@ class YamlFormatter extends AbstractFormatter
@ini_set('yaml.decode_php', $saved);
if ($decoded !== false) {
return $decoded;
return (array) $decoded;
}
}

View File

@@ -550,6 +550,15 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
}
}
// FIXME: For some reason locator caching isn't cleared for the file, investigate!
$locator = Grav::instance()['locator'];
$locator->clearCache();
// Make sure that the object exists before continuing (just in case).
if (!$this->exists()) {
throw new \RuntimeException('Saving failed: Object does not exist!');
}
if (method_exists($this, 'saveUpdatedMedia')) {
$this->saveUpdatedMedia();
}

View File

@@ -27,10 +27,10 @@ trait FlexAuthorizeTrait
{
if (null === $user) {
/** @var UserInterface $user */
$user = Grav::instance()['user'];
$user = Grav::instance()['user'] ?? null;
}
return $this->isAuthorizedAction($user, $action, $scope) || $this->isAuthorizedSuperAdmin($user);
return $user && ($this->isAuthorizedAction($user, $action, $scope) || $this->isAuthorizedSuperAdmin($user));
}
protected function isAuthorizedSuperAdmin(UserInterface $user): bool

View File

@@ -49,7 +49,7 @@ trait FlexMediaTrait
*/
public function getStorageFolder()
{
return $this->getFlexDirectory()->getStorageFolder($this->getStorageKey());
return $this->exists() ? $this->getFlexDirectory()->getStorageFolder($this->getStorageKey()) : '';
}
/**
@@ -57,7 +57,7 @@ trait FlexMediaTrait
*/
public function getMediaFolder()
{
return $this->getFlexDirectory()->getMediaFolder($this->getStorageKey());
return $this->exists() ? $this->getFlexDirectory()->getMediaFolder($this->getStorageKey()) : '';
}
/**
@@ -153,6 +153,12 @@ trait FlexMediaTrait
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$path = $media->getPath();
if (!$path) {
$language = $grav['language'];
throw new RuntimeException($language->translate('PLUGIN_ADMIN.FAILED_TO_MOVE_UPLOADED_FILE'), 400);
}
if ($locator->isStream($path)) {
$path = $locator->findResource($path, true, true);
$locator->clearCache($path);
@@ -202,12 +208,16 @@ trait FlexMediaTrait
}
$media = $this->getMedia();
$path = $media->getPath();
if (!$path) {
return;
}
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$targetPath = $media->getPath() . '/' . $dirname;
$targetFile = $media->getPath() . '/' . $filename;
$targetPath = $path . '/' . $dirname;
$targetFile = $path . '/' . $filename;
if ($locator->isStream($targetFile)) {
$targetPath = $locator->findResource($targetPath, true, true);
$targetFile = $locator->findResource($targetFile, true, true);

View File

@@ -44,7 +44,7 @@ class FormFlash implements FormFlashInterface
protected $uploadedFiles;
/** @var string[] */
protected $uploadObjects;
/** @var string|null */
/** @var string */
protected $folder;
/**
@@ -66,14 +66,13 @@ class FormFlash implements FormFlashInterface
$this->sessionId = $config['session_id'] ?? 'no-session';
$this->uniqueId = $config['unique_id'] ?? '';
$this->folder = $config['folder'] ?? 'tmp://forms';
$folder = $config['folder'] ?? ($this->sessionId ? 'tmp://forms/' . $this->sessionId : '');
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
if ($locator->isStream($this->folder)) {
$this->folder = $locator->findResource($this->folder, true, true);
}
$this->folder = $folder && $locator->isStream($folder) ? $locator->findResource($folder, true, true) : $folder;
$file = $this->getTmpIndex();
$this->exists = $file->exists();
@@ -203,7 +202,7 @@ class FormFlash implements FormFlashInterface
*/
public function save(): self
{
if (!$this->sessionId && $this->uniqueId) {
if (!($this->folder && $this->uniqueId)) {
return $this;
}
@@ -225,7 +224,7 @@ class FormFlash implements FormFlashInterface
*/
public function delete(): self
{
if ($this->sessionId && $this->uniqueId) {
if ($this->folder && $this->uniqueId) {
$this->removeTmpDir();
$this->files = [];
$this->exists = false;
@@ -434,7 +433,7 @@ class FormFlash implements FormFlashInterface
*/
public function getTmpDir(): string
{
return $this->sessionId && $this->uniqueId ? "{$this->folder}/{$this->sessionId}/{$this->uniqueId}" : '';
return $this->folder && $this->uniqueId ? "{$this->folder}/{$this->uniqueId}" : '';
}
/**
@@ -474,8 +473,8 @@ class FormFlash implements FormFlashInterface
*/
protected function addFileInternal(?string $field, string $name, array $data, array $crop = null): void
{
if (!$this->sessionId || !$this->uniqueId) {
throw new \RuntimeException('Cannot upload files: unique id not defined');
if (!($this->folder && $this->uniqueId)) {
throw new \RuntimeException('Cannot upload files: form flash folder not defined');
}
$field = $field ?: 'undefined';

View File

@@ -15,9 +15,11 @@ use Grav\Common\Data\ValidationException;
use Grav\Common\Form\FormFlash;
use Grav\Common\Grav;
use Grav\Common\Twig\Twig;
use Grav\Common\User\Interfaces\UserInterface;
use Grav\Common\Utils;
use Grav\Framework\ContentBlock\HtmlBlock;
use Grav\Framework\Form\Interfaces\FormInterface;
use Grav\Framework\Session\SessionInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Twig\Error\LoaderError;
@@ -338,10 +340,10 @@ trait FormTrait
if (null === $this->flash) {
$grav = Grav::instance();
$config = [
'session_id' => $this->getFlashId() ?? '',
'session_id' => $this->getSessionId(),
'unique_id' => $this->getUniqueId(),
'form_name' => $this->getName(),
'folder' => $this->getBlueprint()->get('form/folder') ?? 'tmp://forms'
'folder' => $this->getFlashFolder()
];
@@ -359,9 +361,8 @@ trait FormTrait
*/
public function getAllFlashes(): array
{
$id = $this->getFlashId();
$folder = ($this->getBlueprint()->get('form/folder') ?? 'tmp://forms') . "/{$id}";
if (!$id || !is_dir($folder)) {
$folder = $this->getFlashFolder();
if (!$folder || !is_dir($folder)) {
return [];
}
@@ -370,10 +371,12 @@ trait FormTrait
$list = [];
/** @var \SplFileInfo $file */
foreach (new \FilesystemIterator($folder) as $file) {
$uniqueId = $file->getFilename();
$config = [
'session_id' => $id,
'unique_id' => $file->getFilename(),
'form_name' => $name
'session_id' => $this->getSessionId(),
'unique_id' => $uniqueId,
'form_name' => $name,
'folder' => $this->getFlashFolder()
];
$flash = new FormFlash($config);
if ($flash->exists() && $flash->getFormName() === $name) {
@@ -409,28 +412,15 @@ trait FormTrait
return $block;
}
protected function getFlashId(): ?string
protected function getSessionId(): string
{
/** @var Grav $grav */
$grav = Grav::instance();
$rememberState = $this->getBlueprint()->get('form/remember_state');
if ($rememberState === 'user') {
$user = $grav['user'] ?? null;
if (isset($user)) {
return $user->username;
}
}
// Session Required for flash form
/** @var SessionInterface $session */
$session = $grav['session'] ?? null;
if (isset($session)) {
// By default store flash by the session id.
return $session->getId();
}
return null;
return $session ? ($session->getId() ?? '') : '';
}
protected function unsetFlash(): void
@@ -438,6 +428,35 @@ trait FormTrait
$this->flash = null;
}
protected function getFlashFolder(): ?string
{
$grav = Grav::instance();
/** @var UserInterface $user */
$user = $grav['user'] ?? null;
$userExists = $user && $user->exists();
$username = $userExists ? $user->username : null;
$mediaFolder = $userExists ? $user->getMediaFolder() : null;
$session = $grav['session'] ?? null;
$sessionId = $session ? $session->getId() : null;
// Fill template token keys/value pairs.
$dataMap = [
'[FORM_NAME]' => $this->getName(),
'[SESSIONID]' => $sessionId ?? '!!',
'[USERNAME]' => $username ?? '!!',
'[USERNAME_OR_SESSIONID]' => $username ?? $sessionId ?? '!!',
'[ACCOUNT]' => $mediaFolder ?? '!!'
];
$flashFolder = $this->getBlueprint()->get('form/flash_folder', 'tmp://forms/[SESSIONID]');
$path = str_replace(array_keys($dataMap), array_values($dataMap), $flashFolder);
// Make sure we only return valid paths.
return strpos($path, '!!') === false ? rtrim($path, '/') : null;
}
/**
* Set a single error.
*

View File

@@ -382,30 +382,60 @@ class UtilsTest extends \Codeception\TestCase\Test
// Fail hard
$this->assertSame(false, Utils::url('', true));
$this->assertSame(false, Utils::url(''));
$this->assertSame(false, Utils::url('foo://bar/baz'));
$this->assertSame(false, Utils::url(new stdClass()));
$this->assertSame(false, Utils::url(['foo','bar','baz']));
$this->assertSame(false, Utils::url('user://does/not/exist'));
// Fail Gracefully
$this->assertSame('/', Utils::url('/', false, true));
$this->assertSame('/', Utils::url('', false, true));
$this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', false, true));
$this->assertSame('/', Utils::url(new stdClass(), false, true));
$this->assertSame('/', Utils::url(['foo','bar','baz'], false, true));
$this->assertSame('/user/does/not/exist', Utils::url('user://does/not/exist', false, true));
// Simple paths
$this->assertSame('/', Utils::url('/'));
$this->assertSame('http://testing.dev/', Utils::url('/', true));
$this->assertSame('http://testing.dev/path1', Utils::url('/path1', true));
$this->assertSame('/path1', Utils::url('/path1'));
$this->assertSame('/path1/path2', Utils::url('/path1/path2'));
$this->assertSame('/random/path1/path2', Utils::url('/random/path1/path2'));
$this->assertSame('/foobar.jpg', Utils::url('/foobar.jpg'));
$this->assertSame('/path1/foobar.jpg', Utils::url('/path1/foobar.jpg'));
$this->assertSame('/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg'));
$this->assertSame('/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg'));
// Simple paths with domain
$this->assertSame('http://testing.dev/', Utils::url('/', true));
$this->assertSame('http://testing.dev/path1', Utils::url('/path1', true));
$this->assertSame('http://testing.dev/path1/path2', Utils::url('/path1/path2', true));
$this->assertSame('http://testing.dev/random/path1/path2', Utils::url('/random/path1/path2', true));
$this->assertSame('http://testing.dev/foobar.jpg', Utils::url('/foobar.jpg', true));
$this->assertSame('http://testing.dev/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
$this->assertSame('http://testing.dev/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg', true));
$this->assertSame('http://testing.dev/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg', true));
// Relative paths from Grav root.
$this->assertSame('/subdir', Utils::url('subdir'));
$this->assertSame('/subdir/path1', Utils::url('subdir/path1'));
$this->assertSame('/subdir/path1/path2', Utils::url('subdir/path1/path2'));
$this->assertSame('/path1', Utils::url('path1'));
$this->assertSame('/path1/path2', Utils::url('path1/path2'));
$this->assertSame('/foobar.jpg', Utils::url('foobar.jpg'));
$this->assertSame('http://testing.dev/foobar.jpg', Utils::url('foobar.jpg', true));
// Relative paths from Grav root with domain.
$this->assertSame('http://testing.dev/foobar.jpg', Utils::url('foobar.jpg', true));
$this->assertSame('http://testing.dev/foobar.jpg', Utils::url('/foobar.jpg', true));
$this->assertSame('http://testing.dev/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
$this->assertSame('/foobar.jpg', Utils::url('/foobar.jpg'));
$this->assertSame('/foobar.jpg', Utils::url('foobar.jpg'));
$this->assertSame('/path1/foobar.jpg', Utils::url('/path1/foobar.jpg'));
$this->assertSame('/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg'));
// All Non-existing streams should be treated as external URI / protocol.
$this->assertSame('http://domain.com/path', Utils::url('http://domain.com/path'));
$this->assertSame('ftp://domain.com/path', Utils::url('ftp://domain.com/path'));
$this->assertSame('sftp://domain.com/path', Utils::url('sftp://domain.com/path'));
$this->assertSame('ssh://domain.com', Utils::url('ssh://domain.com'));
$this->assertSame('pop://domain.com', Utils::url('pop://domain.com'));
$this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz'));
$this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', true));
// $this->assertSame('mailto:joe@domain.com', Utils::url('mailto:joe@domain.com', true)); // FIXME <-
}
public function testUrlWithRoot()
@@ -415,31 +445,69 @@ class UtilsTest extends \Codeception\TestCase\Test
// Fail hard
$this->assertSame(false, Utils::url('', true));
$this->assertSame(false, Utils::url(''));
$this->assertSame(false, Utils::url('foo://bar/baz'));
$this->assertSame(false, Utils::url(new stdClass()));
$this->assertSame(false, Utils::url(['foo','bar','baz']));
$this->assertSame(false, Utils::url('user://does/not/exist'));
// Fail Gracefully
$this->assertSame('/subdir/', Utils::url('/', false, true));
$this->assertSame('/subdir/', Utils::url('', false, true));
$this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', false, true));
$this->assertSame('/subdir/', Utils::url(new stdClass(), false, true));
$this->assertSame('/subdir/', Utils::url(['foo','bar','baz'], false, true));
$this->assertSame('/subdir/user/does/not/exist', Utils::url('user://does/not/exist', false, true));
$this->assertSame('http://testing.dev/subdir/', Utils::url('/', true));
$this->assertSame('http://testing.dev/subdir/path1', Utils::url('/path1', true));
$this->assertSame('http://testing.dev/subdir/path1', Utils::url('/subdir/path1', true));
// Simple paths
$this->assertSame('/subdir/', Utils::url('/'));
$this->assertSame('/subdir/path1', Utils::url('/path1'));
$this->assertSame('/subdir/path1/path2', Utils::url('/path1/path2'));
$this->assertSame('/subdir/path1/path2', Utils::url('/subdir/path1/path2'));
$this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('/subdir/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/path1/foobar.jpg', Utils::url('/subdir/path1/foobar.jpg', true));
$this->assertSame('/subdir/random/path1/path2', Utils::url('/random/path1/path2'));
$this->assertSame('/subdir/foobar.jpg', Utils::url('/foobar.jpg'));
$this->assertSame('/subdir/foobar.jpg', Utils::url('foobar.jpg'));
$this->assertSame('/subdir/foobar.jpg', Utils::url('/subdir/foobar.jpg'));
$this->assertSame('/subdir/path1/foobar.jpg', Utils::url('/path1/foobar.jpg'));
$this->assertSame('/subdir/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg'));
$this->assertSame('/subdir/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg'));
// Simple paths with domain
$this->assertSame('http://testing.dev/subdir/', Utils::url('/', true));
$this->assertSame('http://testing.dev/subdir/path1', Utils::url('/path1', true));
$this->assertSame('http://testing.dev/subdir/path1/path2', Utils::url('/path1/path2', true));
$this->assertSame('http://testing.dev/subdir/random/path1/path2', Utils::url('/random/path1/path2', true));
$this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg', true));
// Paths including the grav base.
$this->assertSame('/subdir/', Utils::url('/subdir'));
$this->assertSame('/subdir/path1', Utils::url('/subdir/path1'));
$this->assertSame('/subdir/path1/path2', Utils::url('/subdir/path1/path2'));
$this->assertSame('/subdir/foobar.jpg', Utils::url('/subdir/foobar.jpg'));
$this->assertSame('/subdir/path1/foobar.jpg', Utils::url('/subdir/path1/foobar.jpg'));
// Relative paths from Grav root with domain.
$this->assertSame('http://testing.dev/subdir/', Utils::url('/subdir', true));
$this->assertSame('http://testing.dev/subdir/path1', Utils::url('/subdir/path1', true));
$this->assertSame('http://testing.dev/subdir/path1/path2', Utils::url('/subdir/path1/path2', true));
$this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('/subdir/foobar.jpg', true));
$this->assertSame('http://testing.dev/subdir/path1/foobar.jpg', Utils::url('/subdir/path1/foobar.jpg', true));
// Relative paths from Grav root.
$this->assertSame('/subdir/subdir', Utils::url('subdir'));
$this->assertSame('/subdir/subdir/path1', Utils::url('subdir/path1'));
$this->assertSame('/subdir/subdir/path1/path2', Utils::url('subdir/path1/path2'));
$this->assertSame('/subdir/path1', Utils::url('path1'));
$this->assertSame('/subdir/path1/path2', Utils::url('path1/path2'));
$this->assertSame('/subdir/foobar.jpg', Utils::url('foobar.jpg'));
$this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('foobar.jpg', true));
// All Non-existing streams should be treated as external URI / protocol.
$this->assertSame('http://domain.com/path', Utils::url('http://domain.com/path'));
$this->assertSame('ftp://domain.com/path', Utils::url('ftp://domain.com/path'));
$this->assertSame('sftp://domain.com/path', Utils::url('sftp://domain.com/path'));
$this->assertSame('ssh://domain.com', Utils::url('ssh://domain.com'));
$this->assertSame('pop://domain.com', Utils::url('pop://domain.com'));
$this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz'));
$this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', true));
// $this->assertSame('mailto:joe@domain.com', Utils::url('mailto:joe@domain.com', true)); // FIXME <-
}
public function testUrlWithStreams()