<?php /*~ startsession.inc.php
.---------------------------------------------------------------------------.
|  Software: Shared Session Library                                         |
|   Version: 0.1                                                            |
|   Contact: via http://www.uniwits.com/contact                             |
|      Info: http://www.uniwits.com/                                        |
|   Support: http://www.uniwits.com/support                                 |
| ------------------------------------------------------------------------- |
|     Admin: Yuxiang Mao (project admininistrator)                          |
|   Authors: Yuxiang Mao (Uniwits) contact@uniwits.com                      |
|   Founder: Yuxiang Mao                                                    |
| Copyright (c) 2010-2012, Yuxiang Mao. All Rights Reserved.                |
| ------------------------------------------------------------------------- |
|   License: Distributed under Uniwits Shareware License (USL)              |
|            http://www.uniwits.com/licenses/shareware/                     |
| This program is distributed as a part of software provided by Uniwits     |
| System WITHOUT ANY WARRANTY; without even the implied warranty of         |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                      |
| ------------------------------------------------------------------------- |
*/ function start_session_with_adjustments($Config) { session_set_save_handler( "session_storage_open", "session_storage_close", "session_storage_read", "session_storage_write", "session_storage_destroy", "session_storage_gc" ); if ($Config->SESSION_SAVE_PATH) session_save_path(realpath($Config->SESSION_SAVE_PATH)); $session_name=$Config->SESSION_NAME; $reset_session_id=TGET('RESETSESSID'); session_set_cookie_params( NOCOOKIE?$Config->SESSION_LIFTTIME_NOCOOKIE:$Config->SESSION_LIFTTIME, $Config->SESSION_COOKIE_PATH, $Config->SESSION_DOMAIN, $Config->SESSION_SECURED ); session_name($session_name); if (NOCOOKIE) { emit_lifelong_cookie('COOKIE-OK',1); $session_id=TGET('SESSIONID'); if (!session_check_validity($session_id,$Config->SESSION_LIFTTIME_NOCOOKIE,session_save_path())) $session_id=null; if (!$session_id || $reset_session_id) { if ($reset_session_id && session_check_validity($reset_session_id,$Config->SESSION_LIFTTIME_NOCOOKIE,session_save_path())) { $session_id=$reset_session_id; session_id($session_id); session_start(); } else { session_start(); $session_id=session_id(); } $request_uri=$_SERVER['REQUEST_URI']; $request_uri=ereg_replace('/m(-[a-zA-Z0-9]+)?/','/m-'.urlencode($session_id).'/',$request_uri); $request_uri=ereg_replace('((\?)|&)?RESETSESSID=[a-zA-Z0-9]+','\\2',$request_uri); $_SESSION['NOCOOKIE']=1; header("Location: ".$request_uri); exit; } session_id($session_id); session_start(); } else { if (TGET('COOKIE_TEST')) { if (!$_COOKIE['COOKIE-OK']) { $uri=TGET('uri'); if ($uri[0]=='/') $uri="/m".$uri; else $uri="/m/".$uri; header("Location: ".$uri); exit; } else { header("Location: ".TGET('uri')); exit; } } else { if (!$_COOKIE['COOKIE-OK']) { emit_lifelong_cookie('COOKIE-OK',1); header("Location: "."./?&COOKIE_TEST=1&uri=".urlencode($_SERVER['REQUEST_URI'])); exit; } } if (TGET('SESSIONID')) { $request_uri=$_SERVER['REQUEST_URI']; $request_uri=ereg_replace('/m(-[a-zA-Z0-9]+)?/','/',$request_uri); header("Location: ".$request_uri); exit; } if ($reset_session_id && session_check_validity($reset_session_id,$Config->SESSION_LIFTTIME,session_save_path())) { $request_uri=$_SERVER['REQUEST_URI']; $request_uri=ereg_replace('((\?)|&)?RESETSESSID=[a-zA-Z0-9]+','\\2',$request_uri); emit_cookie($session_name,$reset_session_id,$Config->SESSION_LIFTTIME); header("Location: ".$request_uri); exit; } $session_id=$_COOKIE[$session_name]; if ($session_id) { if (!session_check_validity($session_id,$Config->SESSION_LIFTTIME,session_save_path())) unset($_COOKIE[$session_name]); } $is_new_session=!$session_id; session_start(); emit_cookie($session_name,session_id(),$Config->SESSION_LIFTTIME); } } function session_storage_localpath($id) { $uaid=(string)base_convert(get_user_agent_id_for_tracking($id),16,36); if (strlen($uaid)<3) $uaid.='000'; return '/'.$id[0].'/'.$id[1].'/'.$id[2].'/'.$uaid[0].'/'.$uaid[1].'/sess_'.substr($id,3).'_'.substr($uaid,2); } function session_storage_path($id,$session_save_path=false) { global $SESSION_SAVE_PATH; if (!$session_save_path) $session_save_path=$SESSION_SAVE_PATH; return $session_save_path.session_storage_localpath($id); } function session_check_validity($id,$lifetimeseconds,$session_save_path) { $sess_file =session_storage_path($id,$session_save_path); $atime=@fileatime($sess_file); if ($atime===false) return false; if (time()-$atime<=$lifetimeseconds) return true; @unlink($sess_file); return false; } function session_storage_open($save_path, $session_name) { global $SESSION_SAVE_PATH; $SESSION_SAVE_PATH=$save_path; return true; } function session_storage_close() { return true; } function session_storage_read($id) { $sess_file =session_storage_path($id); return (string) @file_get_contents($sess_file); } function session_storage_write($id, $sess_data) { $sess_file =session_storage_path($id); $written=@file_put_contents($sess_file,$sess_data); if ($written===false) { $dir=dirname($sess_file); @mkdir($dir,0755,true); $written=@file_put_contents($sess_file,$sess_data); } return $written; } function session_storage_destroy($id) { $sess_file =session_storage_path($id); return @unlink($sess_file); } function session_storage_gc($maxlifetime) { return true; } ?>