00001 <?php
00002
00003 ini_set('session.use_cookies', 1);
00004 ini_set('session.use_only_cookies', 1);
00005 ini_set('session.bug_compat_42', 1);
00006 ini_set('session.use_trans_sid', 0);
00007
00008
00009
00010
00011
00012
00013
00014 class Session {
00015 const STARTED_BY_GYRO = '_GYSS__';
00016 const FINGERPRINT = '_GYRO_FINGERPRINT_';
00017
00018 private static $handler;
00019
00020
00021
00022
00023
00024
00025 public static function set_handler($handler) {
00026 self::$handler = $handler;
00027 }
00028
00029
00030
00031
00032
00033
00034 private static function init_handler() {
00035 if (self::$handler) {
00036 session_set_save_handler(
00037 array(self::$handler, 'open'),
00038 array(self::$handler, 'close'),
00039 array(self::$handler, 'read'),
00040 array(self::$handler, 'write'),
00041 array(self::$handler, 'destroy'),
00042 array(self::$handler, 'gc')
00043 );
00044 }
00045 }
00046
00047
00048
00049
00050 public static function start($id = false) {
00051 if (!self::is_started()) {
00052 if (!headers_sent()) {
00053 self::do_start($id);
00054 }
00055 }
00056 }
00057
00058
00059
00060
00061 public static function start_existing() {
00062 if (!self::is_started()) {
00063 $name = session_name();
00064 if (Cookie::exists($name)) {
00065 self::do_start();
00066 }
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 private static function do_start($id = false) {
00079 $headers = Common::get_headers();
00080
00081
00082
00083 if (!self::is_started()) {
00084 session_cache_limiter('');
00085 }
00086 self::do_start_and_verify($id);
00087 Common::header_restore($headers);
00088
00089 $cookie_params = session_get_cookie_params();
00090 if (!isset($cookie_params['httponly'])) {
00091 $cookie_params['httponly'] = false;
00092 }
00093 $lifetime = $cookie_params['lifetime'];
00094 $expire = empty($lifetime) ? null : time() + $lifetime;
00095 setcookie(
00096 session_name(), session_id(), $expire,
00097 $cookie_params['path'], $cookie_params['domain'],
00098 $cookie_params['secure'], $cookie_params['httponly']
00099 );
00100 }
00101
00102
00103
00104
00105 private static function do_start_and_verify($id = false) {
00106 if ($id) {
00107 session_id($id);
00108 }
00109 self::init_handler();
00110 session_start();
00111 if (!isset($_SESSION[self::STARTED_BY_GYRO])) {
00112
00113 if (empty($id)) {
00114
00115
00116 session_regenerate_id(true);
00117 }
00118 $_SESSION[self::STARTED_BY_GYRO] = true;
00119 }
00120 }
00121
00122 public static function clear() {
00123 $_SESSION = array();
00124 self::restart();
00125 }
00126
00127 public static function end() {
00128 session_destroy();
00129 }
00130
00131
00132
00133
00134 public static function restart($id = false) {
00135 if (!headers_sent()) {
00136 if ($id) {
00137 $backup = array();
00138 if (self::is_started()) {
00139 $backup = $_SESSION;
00140 self::end();
00141 }
00142 self::do_start($id);
00143 $_SESSION = $backup;
00144 }
00145 else {
00146 session_regenerate_id(true);
00147 }
00148 }
00149 }
00150
00151
00152
00153
00154 public static function is_started() {
00155 return (session_id() !== '');
00156 }
00157
00158
00159
00160
00161 public static function get_session_id() {
00162 return session_id();
00163 }
00164
00165
00166
00167
00168
00169
00170 public static function cookies_enabled() {
00171 self::start();
00172 if ( isset($_SESSION["cookiesenabled"]) ) {
00173 return true;
00174 }
00175
00176
00177
00178 if ( isset($_GET["cookietest"]) ) {
00179
00180 if ( empty($_COOKIE["cookietest"]) ) {
00181 return false;
00182 }
00183 else {
00184 $_SESSION["cookiesenabled"] = true;
00185
00186 setcookie("cookietest", "", time() - 3600);
00187 Url::current().replace_query_paramter('cookietest', '').redirect();
00188 }
00189 }
00190 else {
00191 setcookie("cookietest", "Just a test to see if cookies are enabled", 0);
00192 Url::current().replace_query_paramter('cookietest', '1').redirect();
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 public static function push($name, $value) {
00203 self::start();
00204 $_SESSION[$name] = $value;
00205 }
00206
00207
00208
00209
00210 public static function push_if_empty($name, $value) {
00211 self::start();
00212 if (!isset($_SESSION[$name])) {
00213 $_SESSION[$name] = $value;
00214 }
00215 }
00216
00217
00218
00219
00220 public static function push_to_array($name, $value) {
00221 self::start();
00222 $arr = self::peek($name);
00223 if (!is_array($arr)) {
00224 $arr = array();
00225 }
00226 $arr[] = $value;
00227 $_SESSION[$name] = $arr;
00228 }
00229
00230
00231
00232
00233 public static function push_to_array_assoc($name, $value, $key) {
00234 self::start();
00235 $arr = self::peek($name);
00236 if (!is_array($arr)) {
00237 $arr = array();
00238 }
00239 $arr[$key] = $value;
00240 $_SESSION[$name] = $arr;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 public static function peek($name) {
00250 self::start_existing();
00251 if (isset($_SESSION[$name])) {
00252 return $_SESSION[$name];
00253 } else {
00254 return false;
00255 }
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 static public function pull($name) {
00265 self::start_existing();
00266 $ret = false;
00267 if (isset($_SESSION[$name])) {
00268 $ret = $_SESSION[$name];
00269 unset($_SESSION[$name]);
00270 }
00271 return $ret;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281 static public function pull_from_array($name, $index) {
00282 self::start_existing();
00283 $ret = false;
00284 if (isset($_SESSION[$name][$index])) {
00285 $ret = $_SESSION[$name][$index];
00286 unset($_SESSION[$name][$index]);
00287 }
00288 return $ret;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298 public static function set_from($url) {
00299 if ( isset($url) ) {
00300 self::push('from', $url);
00301 }
00302 else {
00303 self::push('from', new Url(Config::get_url(Config::URL_DEFAULT_PAGE)));
00304 }
00305 }
00306
00307
00308
00309
00310
00311
00312 public static function get_from($default = false) {
00313 $ret = self::peek('from');
00314 if (empty($ret)) {
00315 $ret = ($default === false) ? Config::get_url(Config::URL_DEFAULT_PAGE) : $default;
00316 }
00317 return $ret;
00318 }
00319
00320
00321 public static function set_status($status) {
00322 self::push("status", $status);
00323 }
00324
00325
00326 public static function get_status() {
00327 return self::pull("status");
00328 }
00329
00330 public function has_status() {
00331 return (self::peek('status') != false);
00332 }
00333 }