-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved
Session
initialization (#1879)
- Loading branch information
Showing
6 changed files
with
179 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
system/src/Grav/Common/Service/MessagesServiceProvider.php
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
system/src/Grav/Common/Service/SessionServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* @package Grav.Common.Service | ||
* | ||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved. | ||
* @license MIT License; see LICENSE file for details. | ||
*/ | ||
|
||
namespace Grav\Common\Service; | ||
|
||
use Grav\Common\Config\Config; | ||
use Grav\Common\Debugger; | ||
use Grav\Common\Inflector; | ||
use Grav\Common\Session; | ||
use Grav\Common\Uri; | ||
use Pimple\Container; | ||
use Pimple\ServiceProviderInterface; | ||
use RocketTheme\Toolbox\Session\Message; | ||
|
||
class SessionServiceProvider implements ServiceProviderInterface | ||
{ | ||
public function register(Container $container) | ||
{ | ||
// Define session service. | ||
$container['session'] = function ($c) { | ||
/** @var Config $config */ | ||
$config = $c['config']; | ||
|
||
/** @var Uri $uri */ | ||
$uri = $c['uri']; | ||
|
||
// Get session parameters. | ||
$session_timeout = (int)$config->get('system.session.timeout', 1800); | ||
$session_path = $config->get('system.session.path'); | ||
if (null === $session_path) { | ||
$session_path = '/' . ltrim(Uri::filterPath($uri->rootUrl(false)), '/'); | ||
} | ||
$domain = $uri->host(); | ||
if ($domain === 'localhost') { | ||
$domain = ''; | ||
} | ||
|
||
// Get session options. | ||
$secure = (bool)$config->get('system.session.secure', false); | ||
$httponly = (bool)$config->get('system.session.httponly', true); | ||
$enabled = (bool)$config->get('system.session.enabled', false); | ||
|
||
// Activate admin if we're inside the admin path. | ||
$is_admin = false; | ||
if ($config->get('plugins.admin.enabled')) { | ||
$base = '/' . trim($config->get('plugins.admin.route'), '/'); | ||
|
||
// Uri::route() is not processed yet, let's quickly get what we need. | ||
$current_route = str_replace(Uri::filterPath($uri->rootUrl(false)), '', parse_url($uri->url(true), PHP_URL_PATH)); | ||
|
||
// Check no language, simple language prefix (en) and region specific language prefix (en-US). | ||
$pos = strpos($current_route, $base); | ||
if ($pos === 0 || $pos === 3 || $pos === 6) { | ||
$session_timeout = $config->get('plugins.admin.session.timeout', 1800); | ||
$enabled = $is_admin = true; | ||
} | ||
} | ||
|
||
// Fix for HUGE session timeouts. | ||
if ($session_timeout > 99999999999) { | ||
$session_timeout = 9999999999; | ||
} | ||
|
||
$inflector = new Inflector(); | ||
$session_name = $inflector->hyphenize($config->get('system.session.name', 'grav_site')) . '-' . substr(md5(GRAV_ROOT), 0, 7); | ||
if ($is_admin && $config->get('system.session.split', true)) { | ||
$session_name .= '-admin'; | ||
} | ||
|
||
// Define session service. | ||
$session = new Session($session_timeout, $session_path, $domain); | ||
$session->setName($session_name); | ||
$session->setSecure($secure); | ||
$session->setHttpOnly($httponly); | ||
$session->setAutoStart($enabled); | ||
|
||
return $session; | ||
}; | ||
|
||
// Define session message service. | ||
$container['messages'] = function ($c) { | ||
if (!isset($c['session']) || !$c['session']->started()) { | ||
/** @var Debugger $debugger */ | ||
$debugger = $c['debugger']; | ||
$debugger->addMessage('Inactive session: session messages may disappear', 'warming'); | ||
|
||
return new Message; | ||
} | ||
|
||
/** @var Session $session */ | ||
$session = $c['session']; | ||
|
||
if (!isset($session->messages)) { | ||
$session->messages = new Message; | ||
} | ||
|
||
return $session->messages; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters