2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage control
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Handles all manipulation of the session.
|
2007-08-17 07:45:15 +02:00
|
|
|
*
|
|
|
|
* The static methods are used to manipulate the currently active controller's session.
|
|
|
|
* The instance methods are used to manipulate a particular session. There can be more than one of these created.
|
|
|
|
*
|
|
|
|
* In order to support things like testing, the session is associated with a particular Controller. In normal usage, this is loaded from
|
|
|
|
* and saved to the regular PHP session, but for things like static-page-generation and unit-testing, you can create multiple Controllers,
|
|
|
|
* each with their own session.
|
|
|
|
*
|
|
|
|
* The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session data.
|
2007-07-19 12:40:28 +02:00
|
|
|
* This class is currently really basic and could do with a more well-thought-out implementation
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Session {
|
|
|
|
public static function set($name, $val) {
|
2007-08-17 07:45:15 +02:00
|
|
|
return Controller::curr()->getSession()->inst_set($name, $val);
|
|
|
|
}
|
|
|
|
public static function addToArray($name, $val) {
|
|
|
|
return Controller::curr()->getSession()->inst_addToArray($name, $val);
|
|
|
|
}
|
|
|
|
public static function get($name) {
|
|
|
|
return Controller::curr()->getSession()->inst_get($name);
|
|
|
|
}
|
|
|
|
public static function clear($name) {
|
|
|
|
return Controller::curr()->getSession()->inst_clear($name);
|
|
|
|
}
|
|
|
|
public static function getAll() {
|
2007-11-12 02:47:48 +01:00
|
|
|
return Controller::curr()->getSession()->inst_getAll();
|
|
|
|
}
|
|
|
|
public static function save() {
|
|
|
|
return Controller::curr()->getSession()->inst_save();
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session data
|
|
|
|
*/
|
|
|
|
protected $data = array();
|
2008-02-25 03:10:37 +01:00
|
|
|
protected $changedData = array();
|
2007-08-17 07:45:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new session object, with the given starting data
|
|
|
|
* @param $data Can be an array of data (such as $_SESSION) or another Session object to clone.
|
|
|
|
*/
|
|
|
|
function __construct($data) {
|
|
|
|
if($data instanceof Session) $data = $data->inst_getAll();
|
|
|
|
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function inst_set($name, $val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$names = explode('.', $name);
|
|
|
|
|
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
2007-08-17 07:45:15 +02:00
|
|
|
$var = &$this->data;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$this->changedData;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
foreach($names as $n) {
|
|
|
|
$var = &$var[$n];
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$diffVar[$n];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$var = $val;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_addToArray($name, $val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$names = explode('.', $name);
|
|
|
|
|
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
2007-08-17 07:45:15 +02:00
|
|
|
$var = &$this->data;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$this->changedData;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
foreach($names as $n) {
|
|
|
|
$var = &$var[$n];
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$diffVar[$n];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$var[] = $val;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar[sizeof($var)-1] = $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_get($name) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$names = explode('.', $name);
|
2007-08-10 00:06:39 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
if(!isset($this->data)) {
|
2007-08-10 00:06:39 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
$var = $this->data;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
foreach($names as $n) {
|
|
|
|
if(!isset($var[$n])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$var = $var[$n];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_clear($name) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$names = explode('.', $name);
|
|
|
|
|
|
|
|
// We still want to do this even if we have strict path checking for legacy code
|
2007-08-17 07:45:15 +02:00
|
|
|
$var = &$this->data;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$this->changedData;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
foreach($names as $n) {
|
|
|
|
$var = &$var[$n];
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = &$diffVar[$n];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$var = null;
|
2008-02-25 03:10:37 +01:00
|
|
|
$diffVar = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
public function inst_getAll() {
|
|
|
|
return $this->data;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Save data to session
|
|
|
|
* Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
|
|
|
|
*/
|
2007-11-12 02:47:48 +01:00
|
|
|
public function inst_save() {
|
2008-02-25 03:10:37 +01:00
|
|
|
$this->recursivelyApply($this->changedData, $_SESSION);
|
2007-11-19 04:12:29 +01:00
|
|
|
}
|
2007-11-12 02:47:48 +01:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Recursively apply the changes represented in $data to $dest.
|
|
|
|
* Used to update $_SESSION
|
|
|
|
*/
|
|
|
|
protected function recursivelyApply($data, &$dest) {
|
|
|
|
foreach($data as $k => $v) {
|
|
|
|
if(is_array($v)) {
|
|
|
|
if(!isset($dest[$k])) $dest[$k] = array();
|
|
|
|
$this->recursivelyApply($v, $dest[$k]);
|
|
|
|
} else {
|
|
|
|
$dest[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-25 02:06:39 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Sets the appropriate form message in session, with type. This will be shown once,
|
|
|
|
* for the form specified.
|
|
|
|
*
|
|
|
|
* @param formname the form name you wish to use ( usually $form->FormName() )
|
|
|
|
* @param messsage the message you wish to add to it
|
|
|
|
* @param type the type of message
|
|
|
|
*/
|
|
|
|
public static function setFormMessage($formname,$message,$type){
|
|
|
|
Session::set("FormInfo.$formname.message", $message);
|
|
|
|
Session::set("FormInfo.$formname.type", $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function start() {
|
|
|
|
if(!session_id() && !headers_sent()) {
|
|
|
|
session_set_cookie_params(0, Director::baseURL());
|
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|