setName($name); $this->_setCookieParameters(); } /** * Destructor, closes the session * * @access public */ public function __destruct() { $this->close(); } /** * Verify an existing session ID and create or resume the session if the existing session ID is valid * * @access public * @return boolean */ public function start() { $sane_session_id = true; if ( isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false)) ) { $sane_session_id = false; } elseif ( isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false)) ) { $sane_session_id = false; } elseif ( isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false)) ) { $sane_session_id = false; } if ( $sane_session_id === false ) { if ( isset($_COOKIE[$this->_name]) ) { setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain')); } osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false)); } elseif ( session_start() ) { $this->_is_started = true; $this->_id = session_id(); return true; } return false; } /** * Checks if the session has been started or not * * @access public * @return boolean */ public function hasStarted() { return $this->_is_started; } /** * Closes the session and writes the session data to the storage handler * * @access public */ public function close() { if ( $this->_is_started === true ) { $this->_is_started = false; return session_write_close(); } } /** * Deletes an existing session * * @access public */ public function destroy() { if ( $this->_is_started === true ) { if ( isset($_COOKIE[$this->_name]) ) { setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain')); } $this->_delete(); return session_destroy(); } } /** * Deletes an existing session from the storage handler * * @access protected */ protected function _delete() { if ( file_exists($this->_save_path . '/' . $this->_id) ) { @unlink($this->_save_path . '/' . $this->_id); } } /** * Delete an existing session and move the session data to a new session with a new session ID * * @access public */ public function recreate() { if ( $this->_is_started === true ) { return session_regenerate_id(true); } } /** * Return the session file based storage location * * @access public * @return string */ public function getSavePath() { return $this->_save_path; } /** * Return the session ID * * @access public * @return string */ public function getID() { return $this->_id; } /** * Return the name of the session * * @access public * @return string */ public function getName() { return $this->_name; } /** * Sets the name of the session * * @access public */ public function setName($name) { if ( empty($name) ) { $name = 'osCsid'; } session_name($name); $this->_name = session_name(); } /** * Sets the storage location for the file based storage handler * * @access public */ public function setSavePath($path) { if ( substr($path, -1) == '/' ) { $path = substr($path, 0, -1); } session_save_path($path); $this->_save_path = session_save_path(); } /** * Sets the cookie parameters for the session (lifetime, path, domain, secure, httponly) * * @access protected */ protected function _setCookieParameters($lifetime = 0, $path = null, $domain = null, $secure = false, $httponly = false) { global $request_type; if ( empty($path) ) { $path = (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH); } if ( empty($domain) ) { $domain = (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN); } return session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly); } /** * Returns the cookie parameters for the session (lifetime, path, domain, secure, httponly) * * @access public */ public function getCookieParameters($key = null) { if ( empty($this->_cookie_parameters) ) { $this->_cookie_parameters = session_get_cookie_params(); } if ( !empty($key) && isset($this->_cookie_parameters[$key]) ) { return $this->_cookie_parameters[$key]; } else { return false; } return $this->_cookie_parameters; } } ?>