11 include_once (
'Class.QueryDb.php');
12 include_once (
'Class.DbObj.php');
13 include_once (
'Class.Log.php');
14 include_once (
'Class.User.php');
15 include_once (
"Class.SessionCache.php");
37 private $sendCookie =
true;
43 last_seen timestamp not null DEFAULT now() );
44 create unique index sessions_idx on sessions(id);
45 create index sessions_idx_userid on sessions(userid);";
54 include_once (
"config/sessionHandler.php");
56 parent::__construct();
58 $this->last_seen = strftime(
'%Y-%m-%d %H:%M:%S', time());
59 $this->sendCookie = ($sendCookie ===
true);
71 $createNewSession =
true;
75 $query->addQuery(
"id = '" . pg_escape_string(
$id) .
"'");
78 $this->Affect(
$list[0]);
80 $createNewSession =
false;
82 session_name($this->session_name);
85 @session_write_close();
91 if ($createNewSession) {
93 if ((!empty($_SERVER[
'PHP_AUTH_USER'])) && $u->SetLoginName($_SERVER[
'PHP_AUTH_USER'])) {
101 if (!empty($_SERVER[
'HTTP_HOST'])) {
110 if (!isset(
$_SERVER[
'SCRIPT_FILENAME'])) {
113 if (!isset(
$_SERVER[
'SCRIPT_NAME'])) {
123 if ($contextRoot ===
false) {
133 $scriptFilename = realpath(
$_SERVER[
'SCRIPT_FILENAME']);
134 if ($scriptFilename ===
false) {
144 $pos = strpos($scriptFilename, $contextRoot);
148 $relativeScriptFilename = substr($scriptFilename, strlen($contextRoot));
157 $scriptName =
$_SERVER[
'SCRIPT_NAME'];
158 $pos = strrpos($scriptName, $relativeScriptFilename);
159 $webRootLen = (strlen($scriptName) - strlen($relativeScriptFilename));
160 if ($pos !== $webRootLen) {
163 $webRoot = substr($scriptName, 0, $webRootLen);
170 if (is_string($coreUrlIndex)) {
171 $tokens = parse_url($coreUrlIndex);
172 if (!isset($tokens[
'path'])) {
175 $webRoot = $tokens[
'path'];
177 if (substr($webRoot, -1, 1) !==
'/') {
198 if (($webRoot = self::getWebRootPath_CoreUrlIndex()) !==
false) {
204 if (($webRoot = self::getWebRootPath_Apache()) !==
false) {
212 $webRootPath = self::getWebRootPath(
'/');
213 $cookiePath = preg_replace(
':/+:',
'/', $webRootPath);
214 $this->setcookie($this->
name,
$id, $ttl, $cookiePath, null, null,
true);
222 if (!empty($_SERVER[
'HTTP_HOST'])) {
223 session_name($this->
name);
224 session_id($this->
id);
228 $this->setcookie($this->
name,
false, time() - 3600, null, null, null,
true);
231 $this->status = self::SESSION_CT_CLOSE;
240 $this->
exec_query(sprintf(
"delete from sessions where name = '%s';", pg_escape_string($this->session_name)));
242 $this->
exec_query(sprintf(
"delete from sessions where name = '%s' and userid=%d;", pg_escape_string($this->session_name) , $uid));
244 $this->status = self::SESSION_CT_CLOSE;
252 if (!$uid > 0)
return '';
253 $this->
exec_query(
"delete from sessions where userid= '" . pg_escape_string($uid) .
"'");
254 $this->status = self::SESSION_CT_CLOSE;
260 $idsess = $this->
newId();
262 if (!empty($_SERVER[
'HTTP_HOST'])) {
263 session_name($this->session_name);
266 @session_write_close();
272 $this->userid = $uid;
273 $this->last_seen = strftime(
'%Y-%m-%d %H:%M:%S', time());
275 $this->log->debug(
"Nouvelle Session : {$this->id}");
281 function register($k =
"", $v =
"")
284 $this->status = self::SESSION_CT_ARGS;
288 if (!empty($_SERVER[
'HTTP_HOST'])) {
289 session_name($this->
name);
290 session_id($this->
id);
293 @session_write_close();
309 $sessionOpened =
false;
310 if (!isset($_SESSION)) {
311 session_name($this->
name);
312 session_id($this->
id);
314 $sessionOpened =
true;
316 if (isset($_SESSION[$k])) {
317 $val = $_SESSION[$k];
321 if ($sessionOpened) {
322 @session_write_close();
333 if ($this->
name && !empty($_SERVER[
'HTTP_HOST'])) {
334 session_name($this->
name);
335 session_id($this->
id);
337 unset($_SESSION[$k]);
338 @session_write_close();
351 $uKey = $this->
read(
'_uKey_',
false);
352 if ($uKey ===
false) {
354 $this->
register(
'_uKey_', $uKey);
363 $this->log->debug(
"newId");
364 $byteLength = (int)
getParam(
'CORE_SESSION_BYTE_LENGTH');
365 if ($byteLength < self::SESSION_MIN_BYTE_LENGTH) {
366 $byteLength = self::SESSION_MIN_BYTE_LENGTH;
368 return self::randomId($byteLength);
380 private static function randomId($byteLength)
383 $bytes = openssl_random_pseudo_bytes($byteLength, $strong);
384 if ($bytes ===
false || $strong ===
false) {
385 throw new \Dcp\Exception(sprintf(
"Unable to get cryptographically strong random bytes from openssl: your system might be broken or too old."));
387 return bin2hex($bytes);
398 if (!empty($_SERVER[
'HTTP_HOST'])) {
399 session_name($this->
name);
400 session_id($this->
id);
402 foreach ($_SESSION as $k => $v) {
403 if (preg_match(
"/^sessparam[0-9]+$/", $k)) {
404 if (isset($v[$paramName])) {
405 $_SESSION[$k][$paramName] = $paramValue;
409 @session_write_close();
419 return (time() + $ttliv);
426 if ($ttlParamName ==
'') {
428 $ttlParamName =
'CORE_GUEST_SESSIONTTL';
430 $ttlParamName =
'CORE_SESSIONTTL';
433 return intval(
getParam($ttlParamName, $default));
438 return getParam(
"CORE_SESSIONGCPROBABILITY", $default);
443 $this->last_seen = strftime(
'%Y-%m-%d %H:%M:%S', time());
452 return $this->
exec_query(sprintf(
"DELETE FROM sessions WHERE userid != %s AND last_seen < timestamp 'now()' - interval '%s seconds'",
Account::ANONYMOUS_ID, pg_escape_string($ttl)));
461 return $this->
exec_query(sprintf(
"DELETE FROM sessions WHERE userid = %s AND last_seen < timestamp 'now()' - interval '%s seconds'",
Account::ANONYMOUS_ID, pg_escape_string($ttl)));
468 $maxage =
getParam(
'CORE_SESSIONMAXAGE',
'');
470 return $this->
exec_query(sprintf(
"DELETE FROM sessions WHERE last_seen < timestamp 'now()' - interval '%s'", pg_escape_string($maxage)));
481 $p = rand() / getrandmax();
485 error_log(__CLASS__ .
"::" . __FUNCTION__ .
" " .
"Error cleaning up user sessions: " .
$err);
489 error_log(__CLASS__ .
"::" . __FUNCTION__ .
" " .
"Error cleaning up guest sessions: " .
$err);
493 error_log(__CLASS__ .
"::" . __FUNCTION__ .
" " .
"Error cleaning up max-aged sessions: " .
$err);
503 if ($u->SetLoginName($uid)) {
506 $err =
"Could not resolve login name '" . $uid .
"' to uid";
507 error_log(__CLASS__ .
"::" . __FUNCTION__ .
" " .
$err);
511 if ($this->userid != $uid) {
512 if (isset($_SESSION)) {
513 $sessionCopy = $_SESSION;
517 session_id($this->
id);
520 foreach ($sessionCopy as $k => $v) {
523 session_write_close();
528 $this->userid = $uid;
534 include_once (
'WHAT/Lib.Prefix.php');
538 $sessionDir = sprintf(
"%s/var/session", $pubdir);
539 if (!is_dir($sessionDir)) {
540 trigger_error(sprintf(
"Session directory '%s' does not exists.", $sessionDir));
544 if (!is_writable($sessionDir)) {
545 trigger_error(sprintf(
"Session directory '%s' is not writable.", $sessionDir));
554 include_once (
'FDL/Lib.Util.php');
568 include_once (
'WHAT/Lib.Prefix.php');
573 $sessionFile = sprintf(
"%s/var/session/sess_%s", $pubdir,
$sessid);
574 if (file_exists($sessionFile)) {
575 unlink($sessionFile);
590 if ($exceptSessionId ==
'') {
593 return $this->
exec_query(sprintf(
"DELETE FROM sessions WHERE userid = %d AND id != '%s'", $userId, pg_escape_string($exceptSessionId)));
595 private function setcookie(
$name,
$value = null, $expire = null,
$path = null, $domain = null, $secure = null, $httponly = null)
597 if ($this->sendCookie) {
598 if (
$path === null) {
599 $webRootPath = self::getWebRootPath(
'/');
600 $path = preg_replace(
':/+:',
'/', $webRootPath);
602 return setcookie(
$name,
$value, $expire,
$path, $domain, $secure, $httponly);
const SESSION_MIN_BYTE_LENGTH
open($uid=Account::ANONYMOUS_ID)
deleteGuestExpiredSessions()
deleteUserExpiredSessions()
exec_query($sql, $lvl=0, $prepare=false)
print $fam getTitle() $fam name
Add($nopost=false, $nopre=false)
deleteUserSessionsExcept($userId= '', $exceptSessionId= '')
static getParameterValue($application, $parameterName)
getSessionGcProbability($default="0.01")
replaceGlobalParam($paramName, $paramValue)
getSessionTTL($default=0, $ttlParamName= '')
sessionDirExistsAndIsWritable()
modify($nopost=false, $sfields="", $nopre=false)
static getWebRootPath($default=false)
static getWebRootPath_Apache()
getParam($name, $def="")
must be in core or global type
static getWebRootPath_CoreUrlIndex()
stringDateToUnixTs($isodate, $utc=false)
__construct($session_name=self::PARAMNAME, $sendCookie=true)
if(($docid!==0)&&(!is_numeric($docid))) $query
setCookieSession($id, $ttl=0)
if($file) if($subject==""&&$file) if($subject=="") $err
removeSessionFile($sessid=null)