|
|
<?php |
|
|
|
|
|
/* |
|
|
* To change this license header, choose License Headers in Project Properties. |
|
|
* To change this template file, choose Tools | Templates |
|
|
* and open the template in the editor. |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Description of SessionHandler |
|
|
* |
|
|
* @author victor |
|
|
*/ |
|
|
class AppSessionHandler { |
|
|
|
|
|
private $savePath; |
|
|
private static $FILE_NAME = "sess_%s"; |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public function __construct() { |
|
|
$this->savePath = ini_get("session.save_path"); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
* @param string $id |
|
|
* @return string |
|
|
*/ |
|
|
private function getFilePath($id) { |
|
|
return $this->savePath . DIRECTORY_SEPARATOR . sprintf(static::$FILE_NAME, $id); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
* @return array |
|
|
*/ |
|
|
public function getActiveSessions() { |
|
|
$allSessions = []; |
|
|
$it = new FilesystemIterator($this->savePath, FilesystemIterator::SKIP_DOTS); |
|
|
foreach ($it as $fileinfo) { |
|
|
$fileName = $fileinfo->getFilename(); |
|
|
$fullPath = $fileinfo->getPath() . DIRECTORY_SEPARATOR . $fileName; |
|
|
$sessionName = substr($fileName, 5, strlen($fileName)); |
|
|
if (!file_exists($fullPath) || !is_file($fullPath)) { |
|
|
continue; |
|
|
} |
|
|
$contents = file_get_contents($fullPath); |
|
|
$sessionData = static::unserialize($contents); |
|
|
if (!isset($sessionData['SSlogin']) || empty($sessionData['SSlogin'])) { |
|
|
continue; |
|
|
} |
|
|
$walk = static::recursive('utf8_encode', $sessionData); |
|
|
$allSessions[$sessionName] = $walk; |
|
|
$parts = parse_url($sessionData['UltimaRequisicao']); |
|
|
parse_str($parts['query'], $query); |
|
|
if ($sessionName != session_id()) { |
|
|
$allSessions[$sessionName]['sessionId'] = $sessionName; |
|
|
} |
|
|
$allSessions[$sessionName]['UltimaRotina'] = GetDispProgSel($query['idProg']); |
|
|
} |
|
|
return $allSessions; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Desloga uma sess<EFBFBD>o ativa |
|
|
* @param type $sessionId |
|
|
* @throws \OutOfBoundsException |
|
|
*/ |
|
|
public function logout($sessionId = null, $idProgRedirect = 12100) { |
|
|
$currentSession = session_id(); |
|
|
if (is_null($sessionId)) { |
|
|
$sessionId = session_id(); |
|
|
} |
|
|
if (!in_array($sessionId, array_keys($this->getActiveSessions()))) { |
|
|
throw new \OutOfBoundsException("A sess<EFBFBD>o {$sessionId} n<EFBFBD>o existe"); |
|
|
} |
|
|
session_id($sessionId); |
|
|
session_start(); |
|
|
|
|
|
// Unset all of the session variables. |
|
|
$_SESSION = array(); |
|
|
|
|
|
// If it's desired to kill the session, also delete the session cookie. |
|
|
// Note: This will destroy the session, and not just the session data! |
|
|
if (ini_get("session.use_cookies")) { |
|
|
$params = session_get_cookie_params(); |
|
|
$path = $domain = $secure = $httponly = null; |
|
|
extract($params); |
|
|
setcookie(session_name(), '', time() - 42000, $path, $domain, $secure, $httponly); |
|
|
} |
|
|
session_destroy(); |
|
|
|
|
|
session_id($currentSession); |
|
|
session_start(); |
|
|
|
|
|
header('Location: index.php?idProg=' . $idProgRedirect); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
* @param callback $callback |
|
|
* @param array $array |
|
|
* @return array |
|
|
*/ |
|
|
private static function recursive($callback, $array) { |
|
|
$func = function ($item) use (&$func, &$callback) { |
|
|
return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item); |
|
|
}; |
|
|
|
|
|
return array_map($func, $array); |
|
|
} |
|
|
|
|
|
public static function unserialize($session_data) { |
|
|
$method = ini_get("session.serialize_handler"); |
|
|
switch ($method) { |
|
|
case "php": |
|
|
return self::unserialize_php($session_data); |
|
|
break; |
|
|
case "php_binary": |
|
|
return self::unserialize_phpbinary($session_data); |
|
|
break; |
|
|
default: |
|
|
throw new Exception("Unsupported session.serialize_handler: {$method}. Supported: php, php_binary"); |
|
|
} |
|
|
} |
|
|
|
|
|
private static function unserialize_php($session_data) { |
|
|
$return_data = array(); |
|
|
$offset = 0; |
|
|
while ($offset < strlen($session_data)) { |
|
|
if (!strstr(substr($session_data, $offset), "|")) { |
|
|
throw new Exception("invalid data, remaining: " . substr($session_data, $offset)); |
|
|
} |
|
|
$pos = strpos($session_data, "|", $offset); |
|
|
$num = $pos - $offset; |
|
|
$varname = substr($session_data, $offset, $num); |
|
|
$offset += $num + 1; |
|
|
$data = unserialize(substr($session_data, $offset)); |
|
|
$return_data[$varname] = $data; |
|
|
$offset += strlen(serialize($data)); |
|
|
} |
|
|
return $return_data; |
|
|
} |
|
|
|
|
|
private static function unserialize_phpbinary($session_data) { |
|
|
$return_data = array(); |
|
|
$offset = 0; |
|
|
while ($offset < strlen($session_data)) { |
|
|
$num = ord($session_data[$offset]); |
|
|
$offset += 1; |
|
|
$varname = substr($session_data, $offset, $num); |
|
|
$offset += $num; |
|
|
$data = unserialize(substr($session_data, $offset)); |
|
|
$return_data[$varname] = $data; |
|
|
$offset += strlen(serialize($data)); |
|
|
} |
|
|
return $return_data; |
|
|
} |
|
|
|
|
|
}
|
|
|
|