Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/getgrav/grav into 2.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	system/src/Grav/Common/User/User.php
  • Loading branch information
mahagr committed Nov 13, 2017
2 parents b31490e + 31e3c1c commit aa2341d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

1. [](#new)
* Added `Medium::copy()` method to create a copy of a medium object
* Added new `force_lowercase_urls` functionality on routes and slugs
1. [](#bugfix)
* Fixed several issues related to `system.custom_base_url` that were broken [#1736](https://github.com/getgrav/grav/issues/1736)
* Dynamically added pages via `Pages::addPage()` were not firing `onPageProcessed()` event causing forms not to be processed
* Fixed `Page::active()` and `Page::activeChild()` to work with UTF-8 characters in the URL [#1727](https://github.com/getgrav/grav/issues/1727)
* Fixed typo in `modular.yaml` causing media to be ignored [#1725](https://github.com/getgrav/grav/issues/1725)
* Reverted `case_insensitive_urls` option as it was causing issues with taxonomy [#1733](https://github.com/getgrav/grav/pull/1733)
* Removed an extra `/` in `CompileFile.php` [#1693](https://github.com/getgrav/grav/pull/1693)
* Uri: Encode user and password to prevent issues in browsers
* Fixed "Invalid AJAX response" When using Built-in PHP Webserver in Windows [#1258](https://github.com/getgrav/grav-plugin-admin/issues/1258)
* Remove support for `config.user`, it was broken and bad practise

# v1.3.8
## 10/26/2017
Expand Down
12 changes: 12 additions & 0 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,18 @@ form:
validate:
type: bool

force_lowercase_urls:
type: toggle
label: PLUGIN_ADMIN.FORCE_LOWERCASE_URLS
highlight: 1
default: 1
help: PLUGIN_ADMIN.FORCE_LOWERCASE_URLS_HELP
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool

custom_base_url:
type: text
size: medium
Expand Down
1 change: 1 addition & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ param_sep: ':' # Parameter separator, use ';'
wrapped_site: false # For themes/plugins to know if Grav is wrapped by another platform
reverse_proxy_setup: false # Running in a reverse proxy scenario with different webserver ports than proxy
force_ssl: false # If enabled, Grav forces to be accessed via HTTPS (NOTE: Not an ideal solution)
force_lowercase_urls: true # If you want to support mixed cased URLs set this to false
custom_base_url: '' # Set the base_url manually, e.g. http://yoursite.com/yourpath
username_regex: '^[a-z0-9_-]{3,16}$' # Only lowercase chars, digits, dashes, underscores. 3 - 16 chars
pwd_regex: '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}' # At least one number, one uppercase and lowercase letter, and be at least 8+ chars
Expand Down
17 changes: 14 additions & 3 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function init(\SplFileInfo $file, $extension = null)
$config = Grav::instance()['config'];

$this->hide_home_route = $config->get('system.home.hide_in_urls', false);
$this->home_route = $config->get('system.home.alias');
$this->home_route = $this->adjustRouteCase($config->get('system.home.alias'));
$this->filePath($file->getPathName());
$this->modified($file->getMTime());
$this->id($this->modified() . md5($this->filePath()));
Expand Down Expand Up @@ -1558,7 +1558,7 @@ public function slug($var = null)
}

if (empty($this->slug)) {
$this->slug = strtolower(preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder));
$this->slug = $this->adjustRouteCase(preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder));
}


Expand Down Expand Up @@ -1748,7 +1748,7 @@ public function rawRoute($var = null)
if (empty($this->raw_route)) {
$baseRoute = $this->parent ? (string)$this->parent()->rawRoute() : null;

$slug = preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder);
$slug = $this->adjustRouteCase(preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder));

$this->raw_route = isset($baseRoute) ? $baseRoute . '/' . $slug : null;
}
Expand Down Expand Up @@ -2896,4 +2896,15 @@ protected function setPublishState()
}
}
}

protected function adjustRouteCase($route)
{
$case_insensitive = Grav::instance()['config']->get('system.force_lowercase_urls');

if ($case_insensitive) {
return strtolower($route);
} else {
return $route;
}
}
}
3 changes: 3 additions & 0 deletions system/src/Grav/Common/Page/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,9 @@ protected function buildSort($path, array $pages, $order_by = 'default', $manual
case 'basename':
$list[$key] = basename($key);
break;
case 'folder':
$list[$key] = $child->folder();
break;
case (is_string($header_query[0])):
$child_header = new Header((array)$child->header());
$header_value = $child_header->get($header_query[0]);
Expand Down
22 changes: 14 additions & 8 deletions system/src/Grav/Common/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ private function buildBaseUrl()
*/
private function buildRootPath()
{
$scriptPath = $_SERVER['PHP_SELF'];
// In Windows script path uses backslash, convert it:
$scriptPath = str_replace('\\', '/', $_SERVER['PHP_SELF']);
$rootPath = str_replace(' ', '%20', rtrim(substr($scriptPath, 0, strpos($scriptPath, 'index.php')), '/'));

// check if userdir in the path and workaround PHP bug with PHP_SELF
Expand Down Expand Up @@ -285,17 +286,21 @@ public function init()
$this->base .= ':' . (string)$this->port;
}

// Set some defaults
if ($grav['config']->get('system.custom_base_url')) {
$this->root_path = parse_url($grav['config']->get('system.custom_base_url'), PHP_URL_PATH);
$this->root = $grav['config']->get('system.custom_base_url');
// Handle custom base
$custom_base = rtrim($grav['config']->get('system.custom_base_url'), '/');

if ($custom_base) {
$custom_parts = parse_url($custom_base);
$orig_root_path = $this->root_path;
$this->root_path = isset($custom_parts['path']) ? rtrim($custom_parts['path'], '/') : '';
$this->root = isset($custom_parts['scheme']) ? $custom_base : $this->base . $this->root_path;
$this->uri = Utils::replaceFirstOccurrence($orig_root_path, $this->root_path, $this->uri);
} else {
$this->root = $this->base . $this->root_path;
}

$this->url = $this->base . $this->uri;

// get any params and remove them
$uri = str_replace($this->root, '', $this->url);

// remove the setup.php based base if set:
Expand All @@ -305,8 +310,9 @@ public function init()
}

// If configured to, redirect trailing slash URI's with a 302 redirect
if ($uri !== '/' && $config->get('system.pages.redirect_trailing_slash', false) && Utils::endsWith($uri, '/')) {
$grav->redirect(str_replace($this->root, '', rtrim($uri, '/')), 302);
$redirect = str_replace($this->root, '', rtrim($uri, '/'));
if ($redirect && $uri !== '/' && $redirect !== $this->base() && $config->get('system.pages.redirect_trailing_slash', false) && Utils::endsWith($uri, '/')) {
$grav->redirect($redirect, 302);
}

// process params
Expand Down
10 changes: 1 addition & 9 deletions system/src/Grav/Common/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Grav\Common\User;

use Grav\Common\Config\Config;
use Grav\Common\Data\Blueprints;
use Grav\Common\Data\Data;
use Grav\Common\File\CompiledYamlFile;
Expand All @@ -28,13 +27,11 @@ class User extends Data
*
* @return User
*/
public static function load($username, $setConfig = true)
public static function load($username)
{
$grav = Grav::instance();
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
/** @var Config $config */
$config = $grav['config'];

// force lowercase of username
$username = strtolower($username);
Expand All @@ -49,11 +46,6 @@ public static function load($username, $setConfig = true)
$user = new User($content, $blueprint);
$user->file($file);

if ($setConfig) {
// add user to config
$config->set('user', $user);
}

return $user;
}

Expand Down
42 changes: 41 additions & 1 deletion system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function contains($haystack, $needle)
}

/**
* Returns the substring of a string up to a specified needle. if not found, return the whole haytack
* Returns the substring of a string up to a specified needle. if not found, return the whole haystack
*
* @param $haystack
* @param $needle
Expand All @@ -96,6 +96,46 @@ public static function substrToString($haystack, $needle)
return $haystack;
}

/**
* Utility method to replace only the first occurrence in a string
*
* @param $search
* @param $replace
* @param $subject
* @return mixed
*/
public static function replaceFirstOccurrence($search, $replace, $subject)
{
if (!$search) {
return $subject;
}
$pos = strpos($subject, $search);
if ($pos !== false) {
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}

/**
* Utility method to replace only the last occurrence in a string
*
* @param $search
* @param $replace
* @param $subject
* @return mixed
*/
public static function replaceLastOccurrence($search, $replace, $subject)
{
$pos = strrpos($subject, $search);

if($pos !== false)
{
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}

return $subject;
}

/**
* Merge two objects into one.
*
Expand Down

0 comments on commit aa2341d

Please sign in to comment.