id = $id; $this->data = $data; } /** * This magic function is called on serialization of this class. It returns a list of the names of the * variables which should be serialized. * * @return List of variables which should be serialized. */ private function __sleep() { return array('id', 'data'); } /** * This function retrieves the specified key from this storage object. * * @param $key The key we should retrieve the value of. * @return The value of the specified key, or NULL of the key wasn't found. */ public function get($key) { if(!array_key_exists($key, $this->data)) { return NULL; } return $this->data[$key]; } /** * This function sets the specified key to the specified value in this * storage object. * * @param $key The key we should set. * @param $value The value we should set the key to. */ public function set($key, $value) { $this->data[$key] = $value; /* Register the shutdown function if it isn't registered yet. */ if(!$this->shutdownFunctionRegistered) { $this->registerShutdownFunction(); } } /** * This function stores this storage object to the memcache servers. */ public function save() { /* Write to the memcache servers. */ SimpleSAML_Memcache::set($this->id, serialize($this)); } /** * This function determines whether the argument is a valid id. * A valid id is a string containing lowercase alphanumeric * characters. * * @param $id The id we should validate. * @return TRUE if the id is valid, FALSE otherwise. */ private static function isValidID($id) { if(!is_string($id)) { return FALSE; } if(strlen($id) < 1) { return FALSE; } if(preg_match('/[^0-9a-z]/', $id)) { return FALSE; } return TRUE; } /** * Register our shutdown function. */ private function registerShutdownFunction() { register_shutdown_function(array($this, 'shutdown')); $this->shutdownFunctionRegistered = TRUE; } /** * Shutdown function. Calls save and updates flag indicating that the function has been called. * * This function is only public because this is a requirement of the way callbacks work in PHP. */ public function shutdown() { $this->save(); $this->shutdownFunctionRegistered = FALSE; } } ?>