Skip to content

Commit

Permalink
Load and set session cookie timeout from risoluto.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
hayakawa committed Aug 30, 2016
1 parent b21de4a commit bdc64e6
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions risoluto/lib/vendor/Risoluto/Session.php
Expand Up @@ -47,40 +47,45 @@ class Session
*
* @access public
*
* @param string $path セッションファイル保存ディレクトリ
* @param string $name セッション名
* @param string $path セッションファイル保存ディレクトリ
* @param string $name セッション名
*
* @return boolean セッション開始結果(true:正常終了/false:異常終了)
*/
public function start( $path = '', $name = '' )
public function start($path = '', $name = '')
{
// セッション保存ディレクトリが指定されていたらその値を採用
if (!empty( $path )) {
if (!empty($path)) {
$this->sesspath = $path;
}
// セッション名が指定されていたらその値を採用
if (!empty( $name )) {
if (!empty($name)) {
$this->sessname = $name;
}

// セッション保存ディレクトリをセット
if (!empty( $this->sesspath ) and is_writable( $this->sesspath )) {
session_save_path( $this->sesspath );
if (!empty($this->sesspath) and is_writable($this->sesspath)) {
session_save_path($this->sesspath);
// 指定されていないか書き込めないならfalseを返す
} else {
return false;
}

// セッション名の指定
session_name( $this->sessname );
session_name($this->sessname);

// セッションが存在しない場合の処理
if (empty( $_COOKIE[ $this->sessname ] )) {
if (empty($_COOKIE[$this->sessname])) {
// 生成したセッションIDを付与する
$base = $this->genRand();
session_id( $base );
session_id($base);
} // end of if

// セッションタイムアウトの秒数をコンフィグから取得しセット
$conf = new Conf;
$conf->parse( RISOLUTO_CONF . 'risoluto.ini' );
session_set_cookie_params($conf->getIni( 'SESSION', 'timeout' ));

// セッションの開始
return session_start();
}
Expand All @@ -97,12 +102,12 @@ public function start( $path = '', $name = '' )
*
* @return boolean セッション再開始結果(true:正常終了/false:異常終了)
*/
public function restart( $path = '', $name = '' )
public function restart($path = '', $name = '')
{
// セッションを終了してスタートさせる
$this->end();

return $this->start( $path, $name );
return $this->start($path, $name);
}

/**
Expand All @@ -119,20 +124,20 @@ public function restart( $path = '', $name = '' )
public function end()
{
// クッキーを削除
if (isset( $_COOKIE[ $this->sessname ] )) {
setcookie( $this->sessname, '', time() - 42000, '/' );
if (isset($_COOKIE[$this->sessname])) {
setcookie($this->sessname, '', time() - 42000, '/');
}

// スーパーグローバルな$_COOKIEと$_SESSIONをクリア
$_COOKIE[ $this->sessname ] = [ ];
$_SESSION = [ ];
$_COOKIE[$this->sessname] = [];
$_SESSION = [];

// セッションファイルを削除
$target = $this->sesspath . 'sess_' . session_id();

clearstatcache( true );
if (file_exists( $target ) and is_file( $target ) and is_writeable( $target )) {
unlink( $target );
clearstatcache(true);
if (file_exists($target) and is_file($target) and is_writeable($target)) {
unlink($target);
}

return session_destroy();
Expand All @@ -147,14 +152,14 @@ public function end()
* @access public
*
* @param string $destination 格納先セッション変数名
* @param mixed $val 格納する値(number or string)
* @param mixed $val 格納する値(number or string)
*
* @return boolean 常にtrue
*/
public function store( $destination, $val )
public function store($destination, $val)
{
if (isset( $destination ) and isset( $val )) {
$_SESSION[ $destination ] = $val;
if (isset($destination) and isset($val)) {
$_SESSION[$destination] = $val;
}

return true;
Expand All @@ -172,10 +177,10 @@ public function store( $destination, $val )
*
* @return mixed 取得した値
*/
public function load( $from )
public function load($from)
{
if (isset( $from ) and isset( $_SESSION[ $from ] )) {
return $_SESSION[ $from ];
if (isset($from) and isset($_SESSION[$from])) {
return $_SESSION[$from];
} else {
return null;
}
Expand All @@ -192,9 +197,9 @@ public function load( $from )
*
* @return boolean 存在状況(true:存在する/false:存在しない)
*/
public function isThere( $chkName )
public function isThere($chkName)
{
return isset( $_SESSION[ $chkName ] );
return isset($_SESSION[$chkName]);
}

/**
Expand All @@ -208,10 +213,10 @@ public function isThere( $chkName )
*
* @return boolean 常にtrue
*/
public function revoke( $chkName )
public function revoke($chkName)
{
if (isset( $_SESSION[ $chkName ] )) {
unset( $_SESSION[ $chkName ] );
if (isset($_SESSION[$chkName])) {
unset($_SESSION[$chkName]);
}

return true;
Expand All @@ -231,10 +236,10 @@ public function revoke( $chkName )
public function revokeAll()
{
// セッション変数が存在するかをチェック
if (isset( $_SESSION )) {
if (isset($_SESSION)) {
// すべての値を抹消する
foreach ($_SESSION as $key) {
$this->revoke( $key );
$this->revoke($key);
}
}

Expand All @@ -254,16 +259,16 @@ public function revokeAll()
*/
public function genRand()
{
if (function_exists( 'openssl_random_pseudo_bytes' )) {
if (function_exists('openssl_random_pseudo_bytes')) {
// openssl_random_pseudo_bytes()が使用できない場合はそれを使って乱数を生成
$retval = bin2hex( openssl_random_pseudo_bytes( 32 ) );
$retval = bin2hex(openssl_random_pseudo_bytes(32));
} else {
// openssl_random_pseudo_bytes()が使用できない場合は
// システムよりマイクロセコンドの精度で時刻情報を取得し乱数を生成
list( $usec, $sec ) = explode( " ", microtime() );
$seed = (double)$sec + ( (double)$usec * 100000 );
list($usec, $sec) = explode(" ", microtime());
$seed = (double)$sec + ((double)$usec * 100000);

mt_srand( $seed );
mt_srand($seed);
$retval = mt_rand();
}

Expand Down

0 comments on commit bdc64e6

Please sign in to comment.