diff --git a/README.md b/README.md index 49935ca..74b2702 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ $app->get('/', function () use ($app) { }); ``` +## TODO + +Tests! + ## License MIT diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..637b570 --- /dev/null +++ b/composer.json @@ -0,0 +1,35 @@ +{ + "name": "bryanjhv/slim-session", + "description": "Session middleware and helper for Slim framework 2.", + "version": "1.0.0", + "type": "library", + "keywords": [ + "slim", + "session", + "middleware", + "helper" + ], + "homepage": "https://github.com/bryanjhv/slim-session", + "license": "MIT", + "authors": [ + { + "name": "Bryan Horna", + "email": "bryanjhv@gmail.com", + "homepage": "http://bryanjhv.me", + "role": "developer" + } + ], + "support": { + "source": "https://github.com/bryanjhv/slim-session.git" + }, + "require": { + "slim/slim": "~2.0" + }, + "autoload": { + "psr-4": { + "": "src/" + } + }, + "minimum-stability": "stable", + "prefer-stable": true +} diff --git a/src/Slim/Middleware/Session.php b/src/Slim/Middleware/Session.php new file mode 100644 index 0000000..32e51b6 --- /dev/null +++ b/src/Slim/Middleware/Session.php @@ -0,0 +1,113 @@ + '20 minutes', + 'path' => '/', + 'domain' => null, + 'secure' => false, + 'httponly' => false, + 'name' => 'slim_session', + 'autorefresh' => false + ); + $settings = array_merge($defaults, $settings); + if (is_string($lifetime = $settings['lifetime'])) { + $settings['lifetime'] = strtotime($lifetime) - time(); + } + $this->settings = $settings; + + ini_set('session.gc_probability', 1); + ini_set('session.gc_divisor', 1); + ini_set('session.gc_maxlifetime', 30 * 24 * 60 * 60); + } + + /** + * Call + */ + public function call() + { + $this->registerHelper(); + $this->startSession(); + $this->next->call(); + } + + /** + * Register helper + * + * It registers a session helper singleton to $app->session, so you can use + * that to manage sessions or instantiate the helper class for yourself. + */ + protected function registerHelper() + { + $this->app->container->singleton('session', function () { + return new \SlimSession\Helper; + }); + } + + /** + * Start session + */ + protected function startSession() + { + if (session_id()) { + return; + } + + $settings = $this->settings; + $name = $settings['name']; + + session_set_cookie_params( + $settings['lifetime'], + $settings['path'], + $settings['domain'], + $settings['secure'], + $settings['httponly'] + ); + session_name($name); + session_cache_limiter(false); + session_start(); + + if ($settings['autorefresh'] && isset($_COOKIE[$name])) { + setcookie( + $name, + $_COOKIE[$name], + time() + $settings['lifetime'], + $settings['path'], + $settings['domain'], + $settings['secure'], + $settings['httponly'] + ); + } + } +} diff --git a/src/SlimSession/Helper.php b/src/SlimSession/Helper.php new file mode 100644 index 0000000..8a08288 --- /dev/null +++ b/src/SlimSession/Helper.php @@ -0,0 +1,158 @@ +exists($key) + ? $_SESSION[$key] + : $default; + } + + /** + * Set a session variable. + * + * @param string $key + * @param mixed $value + */ + public function set($key, $value) + { + $_SESSION[$key] = $value; + } + + /** + * Delete a session variable. + * + * @param string $key + */ + public function delete($key) + { + if ($this->exists($key)) { + unset($_SESSION[$key]); + } + } + + /** + * Clear all session variables. + */ + public function clear() + { + $_SESSION = array(); + } + + /** + * Check if a session variable is set. + * + * @param string $key + * + * @return bool + */ + protected function exists($key) + { + return array_key_exists($key, $_SESSION); + } + + /** + * Get or regenerate current session ID. + * + * @param bool $new + * + * @return string + */ + public static function id($new = false) + { + if ($new && session_id()) { + session_regenerate_id(true); + } + + return session_id() ?: ''; + } + + /** + * Destroy the session. + */ + public static function destroy() + { + if (self::id()) { + session_unset(); + session_destroy(); + session_write_close(); + + if (ini_get('session.use_cookies')) { + $params = session_get_cookie_params(); + setcookie( + session_name(), + '', + time() - 4200, + $params['path'], + $params['domain'], + $params['secure'], + $params['httponly'] + ); + } + } + } + + /** + * Magic method for get. + * + * @param string $key + * + * @return mixed + */ + public function __get($key) + { + return $this->get($key); + } + + /** + * Magic method for set. + * + * @param string $key + * @param mixed $value + */ + public function __set($key, $value) + { + $this->set($key, $value); + } + + /** + * Magic method for delete. + * + * @param string $key + */ + public function __unset($key) + { + $this->delete($key); + } + + /** + * Magic method for exists. + * + * @param string $key + * + * @return bool + */ + public function __isset($key) + { + return $this->exists($key); + } +}