diff options
Diffstat (limited to 'api/Slim/Session')
-rwxr-xr-x | api/Slim/Session/Flash.php | 192 | ||||
-rwxr-xr-x | api/Slim/Session/Handler.php | 125 | ||||
-rwxr-xr-x | api/Slim/Session/Handler/Cookies.php | 71 |
3 files changed, 388 insertions, 0 deletions
diff --git a/api/Slim/Session/Flash.php b/api/Slim/Session/Flash.php new file mode 100755 index 0000000..2f71deb --- /dev/null +++ b/api/Slim/Session/Flash.php @@ -0,0 +1,192 @@ +<?php +/** + * Slim - a micro PHP 5 framework + * + * @author Josh Lockhart <info@joshlockhart.com> + * @copyright 2011 Josh Lockhart + * @link http://www.slimframework.com + * @license http://www.slimframework.com/license + * @version 1.5.0 + * + * MIT LICENSE + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * Flash Messaging + * + * This class enables Flash messaging. Messages are persisted in $_SESSION + * with a user-defined key. + * + * USAGE: + * + * 1. Set Flash message to be shown on the next request + * + * Slim::flash('error', 'The object could not be saved'); + * + * 2. Set Flash message to be shown on the current request + * + * Slim::flashNow('error', 'The object could not be saved'); + * + * 3. Keep old Flash messages for the next request + * + * Slim::flashKeep(); + * + * @package Slim + * @author Josh Lockhart + * @since Version 1.3 + */ +class Slim_Session_Flash implements ArrayAccess { + + /** + * @var string Key used to identify flash information in $_SESSION array + */ + protected $sessionKey = 'flash'; + + /** + * @var array[array] Storage for flash messages + */ + protected $messages = array( + 'prev' => array(), //flash messages from prev request + 'next' => array(), //flash messages for next request + 'now' => array() //flash messages for current request + ); + + /** + * Constructor + * + * Establishes Flash session key and loads existing + * Flash messages from the $_SESSION. + * + * @param string $sessionKey + * @return void + */ + public function __construct( $sessionKey = null ) { + if ( !is_null($sessionKey) ) { + $this->setSessionKey($sessionKey); + } + $this->load(); + } + + /** + * Set the $_SESSION key used to access Flash messages + * @param string $key + * @throws RuntimeException If session key is null + * @return Slim_Session_Flash + */ + public function setSessionKey( $key ) { + if ( is_null($key) ) { + throw new RuntimeException('Session key cannot be null'); + } + $this->sessionKey = (string)$key; + return $this; + } + + /** + * Get the $_SESSION key used to access Flash messages + * @return string + */ + public function getSessionKey() { + return $this->sessionKey; + } + + /** + * Set a Flash message for the current request + * @param string $key + * @param string $value + * @return Slim_Session_Flash + */ + public function now( $key, $value ) { + $this->messages['now'][(string)$key] = $value; + return $this->save(); + } + + /** + * Set a Flash message for the next request + * @param string $key + * @param string $value + * @return Slim_Session_Flash + */ + public function set( $key, $value ) { + $this->messages['next'][(string)$key] = $value; + return $this->save(); + } + + /** + * Get Flash messages intended for the current request's View + * @return array[String] + */ + public function getMessages() { + return array_merge($this->messages['prev'], $this->messages['now']); + } + + /** + * Load Flash messages from $_SESSION + * @return Slim_Session_Flash + */ + public function load() { + $this->messages['prev'] = isset($_SESSION[$this->sessionKey]) ? $_SESSION[$this->sessionKey] : array(); + return $this; + } + + /** + * Transfer Flash messages from the previous request + * so they are available to the next request. + * @return Slim_Session_Flash + */ + public function keep() { + foreach ( $this->messages['prev'] as $key => $val ) { + $this->messages['next'][$key] = $val; + } + return $this->save(); + } + + /** + * Save Flash messages to $_SESSION + * @return Slim_Session_Flash + */ + public function save() { + $_SESSION[$this->sessionKey] = $this->messages['next']; + return $this; + } + + /***** ARRAY ACCESS INTERFACE *****/ + + public function offsetExists( $offset ) { + $messages = $this->getMessages(); + return isset($messages[$offset]); + } + + public function offsetGet( $offset ) { + $messages = $this->getMessages(); + return isset($messages[$offset]) ? $messages[$offset] : null; + } + + public function offsetSet( $offset, $value ) { + $this->now($offset, $value); + } + + public function offsetUnset( $offset ) { + unset($this->messages['prev'][$offset]); + unset($this->messages['now'][$offset]); + } + +}
\ No newline at end of file diff --git a/api/Slim/Session/Handler.php b/api/Slim/Session/Handler.php new file mode 100755 index 0000000..1e76c3f --- /dev/null +++ b/api/Slim/Session/Handler.php @@ -0,0 +1,125 @@ +<?php +/** + * Slim - a micro PHP 5 framework + * + * @author Josh Lockhart + * @link http://www.slimframework.com + * @copyright 2011 Josh Lockhart + * + * MIT LICENSE + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * Abstract Session Handler + * + * This abstract class should be extended by each concrete + * session handler. This class defines the contractual class interface + * methods that must be implemented in concrete subclasses. This class + * also provides the final `register` method used by Slim itself to + * actually register the concrete session handler with PHP. + * + * @package Slim + * @author Josh Lockhart + * @since Version 1.3 + */ +abstract class Slim_Session_Handler { + + /** + * @var Slim + */ + protected $app; + + /** + * Register session handler + * + * @return bool + */ + final public function register( Slim $app ) { + $this->app = $app; + return session_set_save_handler( + array($this, 'open'), + array($this, 'close'), + array($this, 'read'), + array($this, 'write'), + array($this, 'destroy'), + array($this, 'gc') + ); + } + + /** + * Open session + * + * @param string $savePath + * @param string $sessionName + * @return mixed + */ + abstract public function open( $savePath, $sessionName ); + + /** + * Close session + * + * @return mixed + */ + abstract public function close(); + + /** + * Read session data with ID + * + * @param string $id The session identifier + * @return string + */ + abstract public function read( $id ); + + /** + * Write session data with ID + * + * The "write" handler is not executed until after the output stream is + * closed. Thus, output from debugging statements in the "write" handler + * will never be seen in the browser. If debugging output is necessary, it + * is suggested that the debug output be written to a file instead. + * + * @param string $id The session identifier + * @param mixed $sessionData The session data + * @return mixed + */ + abstract public function write( $id, $sessionData ); + + /** + * Destroy session with ID + * + * @param string $id The session identifier + * @return mixed + */ + abstract public function destroy( $id ); + + /** + * Session garbage collection + * + * Executed when the PHP session garbage collector is invoked; should + * remove all session data older than the `$maxLifetime`. + * + * @param int $maxLifetime + * @return mixed + */ + abstract public function gc( $maxLifetime ); + +}
\ No newline at end of file diff --git a/api/Slim/Session/Handler/Cookies.php b/api/Slim/Session/Handler/Cookies.php new file mode 100755 index 0000000..2358540 --- /dev/null +++ b/api/Slim/Session/Handler/Cookies.php @@ -0,0 +1,71 @@ +<?php +/** + * Slim - a micro PHP 5 framework + * + * @author Josh Lockhart + * @link http://www.slimframework.com + * @copyright 2011 Josh Lockhart + * + * MIT LICENSE + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * Session Cookie Handler + * + * This class is used as an adapter for PHP's $_SESSION handling. + * Session data will be written to and read from signed, encrypted + * cookies. If the current PHP installation does not have the `mcrypt` + * extension, session data will be written to signed but unencrypted + * cookies; however, the session cookies will still be secure and will + * become invalid if manually edited after set by PHP. + * + * @package Slim + * @author Josh Lockhart + * @since Version 1.3 + */ +class Slim_Session_Handler_Cookies extends Slim_Session_Handler { + + public function open( $savePath, $sessionName ) { + return true; + } + + public function close() { + return true; //Not used + } + + public function read( $id ) { + return $this->app->getEncryptedCookie($id); + } + + public function write( $id, $sessionData ) { + $this->app->setEncryptedCookie($id, $sessionData, 0); + } + + public function destroy( $id ) { + $this->app->deleteCookie($id); + } + + public function gc( $maxLifetime ) { + return true; //Not used + } + +}
\ No newline at end of file |