Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .idea/Session.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/php.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Basic usage:
## Registering Error Handler (optional)
```php
#This method must be implemented before Session::start
Session::registerErrorHandler(function($error, $error_code)
Session::registerErrorHandler(function(string $error, int $error_code)
{
#Debug::Log($error);
});
Expand Down Expand Up @@ -118,3 +118,37 @@ $session = Session::start($optional_session_namespace, $auto_save);
```
Which allows uncommitted (forgot to commit) changes to saves automatically. Is set to `false`, uncommitted changes will be discarded.


## Change Log *v1.03.0*
- `start` method now accepts `null` arg as namespace.
- Default session driver is now set to `file`.
- Auto delete session after rotate is now defaulted to `false`.
- A `setConfigPath` method has been added.
```php
#This method must be implemented before Session::start
Session::setConfigPath('my/config/path/config.php');
```
- A set queue has been added
```php
$session->name = 'foo';
$session->name = 'foo1';

var_dump($session->name); # Outputs 'foo1'

$session->name('foo')
$session->name('foo1');

var_dump($session->name); # Outputs ['foo', 'foo1'];

# Array
$session->name = ['foo', 'bar', ...];
# is same as
$session->name('foo', 'bar', ...);
```

When flash are placed using the new queue method, they will be dispatched one after another on each request
```php
$session->flash->message('invalid 1', 'invalid 2');
```
With the above `invalid 1` will disptach on first load/reload and `invalid 2` on second.

42 changes: 26 additions & 16 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,48 @@
class Session
{
private static $initialized = [];

private static $started = false;

private static $class = null;

private static $ssl_enabled = true;

public static $write = false;

private static $custom_config = null;
public static $id = '';

private static function init()
{
$DS = DIRECTORY_SEPARATOR;
$path = __DIR__ . $DS . 'Session' . $DS;
$config = include($path . 'config.php');
$path = __DIR__ . "{$DS}Session{$DS}";
$config = include(self::$custom_config ?? $path . 'config.php');

self::$initialized = $config;
$driver = $config['driver'];
self::$class = ucfirst($driver);

if(! is_dir($path . self::$class))
{
throw new \RuntimeException('No driver found for ' . self::$class);
throw new RuntimeException('No driver found for ' . self::$class);
}

self::$ssl_enabled = self::$initialized['encrypt_data'];
if (self::$ssl_enabled && ! extension_loaded('openssl'))
{
throw new \RuntimeException('The openssl extension is missing. Please check your PHP configuration.');
throw new RuntimeException('The openssl extension is missing. Please check your PHP configuration.');
}


if (self::$class != 'File' && self::$class != 'Cookie')
{
if (! extension_loaded(self::$class))
{
throw new \RuntimeException('The ' . self::$class . ' extension is missing. Please check your PHP configuration.');
throw new RuntimeException('The ' . self::$class . ' extension is missing. Please check your PHP configuration.');
}
}

session_cache_limiter($config['cache_limiter']);
$secured = $config['secure'];
if ($secured !== true && $secured !== false && $secured !== null)
{
throw new \RuntimeException('config.secure expected value to be a boolean or null');
throw new RuntimeException('config.secure expected value to be a boolean or null');
}

if ($secured == null)
Expand Down Expand Up @@ -145,11 +141,11 @@ public static function id(string $id = ''): string
{
if (self::$started)
{
throw new \RuntimeException('Session is active. The session id must be set before Session::start().');
throw new RuntimeException('Session is active. The session id must be set before Session::start().');
}
elseif (headers_sent($filename, $line_num))
{
throw new \RuntimeException(sprintf('ID must be set before any output is sent to the browser (file: %s, line: %s)', $filename, $line_num));
throw new RuntimeException(sprintf('ID must be set before any output is sent to the browser (file: %s, line: %s)', $filename, $line_num));
}
elseif (preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $id) < 1)
{
Expand All @@ -165,22 +161,35 @@ public static function id(string $id = ''): string
}


/**
* Sets a file where config settings will be loaded from.
*
* @param string $path_to_file
*/
public static function setConfigFile(string $path_to_file)
{
if (! is_file($path_to_file))
{
throw new RuntimeException('config was not found in (' . $path_to_file . ') or not enough permission.');
}
self::$custom_config = $path_to_file;
}
/**
* starts a new session
*
* @param string $namespace
* @param bool $auto_commit (Alternative for https://github.com/Ghostff/Session/issues/4)
* @return Save
*/
public static function start(string $namespace = '__GLOBAL', bool $auto_commit = true): Save
public static function start(string $namespace = null, bool $auto_commit = true): Save
{
if (empty(self::$initialized))
{
self::init();
}

self::$started = true;
self::$initialized['namespace'] = $namespace;
self::$initialized['namespace'] = $namespace ?? '__GLOBAL';
$handler = new Save(self::$initialized);
if ($auto_commit)
{
Expand Down Expand Up @@ -296,6 +305,7 @@ public static function encrypt(string $data): string
$iv = substr($salted, 32,16);

$encrypted_data = openssl_encrypt($data, 'AES-256-CBC', $key, 1, $iv);

return base64_encode($salt . $encrypted_data);
}
}
}
7 changes: 3 additions & 4 deletions src/Session/Cookie/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
declare(strict_types=1);

namespace Session\Cookie;
use Session;

use Session, SessionHandlerInterface;

class Handler implements \SessionHandlerInterface
class Handler implements SessionHandlerInterface
{

public function open($savePath, $sessionName)
{
return true;
Expand Down Expand Up @@ -87,4 +86,4 @@ public function gc($max_life_time): bool
{
return true;
}
}
}
7 changes: 3 additions & 4 deletions src/Session/File/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@

declare(strict_types=1);


namespace Session\File;
use Session;

use Session, SessionHandlerInterface;

class Handler implements \SessionHandlerInterface
class Handler implements SessionHandlerInterface
{

private $savePath;
Expand Down Expand Up @@ -100,4 +99,4 @@ public function gc($max_life_time): bool

return true;
}
}
}
13 changes: 4 additions & 9 deletions src/Session/Memcached/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,23 @@
*
*/


declare(strict_types=1);

namespace Session\Memcached;
use Session, Memcached;

use Memcached, RuntimeException, Session, SessionHandlerInterface;

class Handler implements \SessionHandlerInterface
class Handler implements SessionHandlerInterface
{
private $config = [];

private $conn = null;

private $expire = 0;

private $name = null;

public function __construct(array $config)
{
if (! isset($config['memcached']))
{
throw new \RuntimeException('No memcached configuration found in config file.');
throw new RuntimeException('No memcached configuration found in config file.');
}

$this->expire = $config['expiration'];
Expand Down Expand Up @@ -112,4 +107,4 @@ public function gc($max_life_time): bool
{
return true;
}
}
}
15 changes: 5 additions & 10 deletions src/Session/Pdo/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,23 @@
*
*/


declare(strict_types=1);

namespace Session\Pdo;
use PDO, Session;

use PDO, PDOException, RuntimeException, Session, SessionHandlerInterface;

class Handler implements \SessionHandlerInterface
class Handler implements SessionHandlerInterface
{

private $conn = null;

private $table = null;

private $persistent = false;


public function __construct(array $config)
{
if (! isset($config['pdo']))
{
throw new \RuntimeException('No pdo configuration found in config file.');
throw new RuntimeException('No pdo configuration found in config file.');
}

$config = $config['pdo'];
Expand All @@ -77,7 +72,7 @@ public function __construct(array $config)
{
$this->conn->query('SELECT 1 FROM `' . $table . '` LIMIT 1');
}
catch (\PDOException $e)
catch (PDOException $e)
{
$this->conn->query('CREATE TABLE `' . $table . '` (
`id` varchar(250) NOT NULL,
Expand Down Expand Up @@ -151,4 +146,4 @@ public function gc($max_life_time): bool
$statement = null;
return $completed;
}
}
}
13 changes: 4 additions & 9 deletions src/Session/Redis/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,23 @@
*
*/


declare(strict_types=1);

namespace Session\Redis;
use Session, Redis;

use Redis, RuntimeException, Session, SessionHandlerInterface;

class Handler implements \SessionHandlerInterface
class Handler implements SessionHandlerInterface
{
private $config = [];

private $conn = null;

private $expire = 0;

private $name = null;

public function __construct(array $config)
{
if (! isset($config['redis']))
{
throw new \RuntimeException('No Redis configuration found in config file.');
throw new RuntimeException('No Redis configuration found in config file.');
}

$this->expire = $config['expiration'];
Expand Down Expand Up @@ -114,4 +109,4 @@ public function gc($max_life_time): bool
{
return true;
}
}
}
Loading