source: branches/version-2_13-dev/data/class/helper/SC_Helper_Session.php @ 23493

Revision 23493, 4.5 KB checked in by pineray, 10 years ago (diff)

#2565 セッションに関するクラスや関数を整理

セッションハンドラの登録をファクトリーへ移動.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8/**
9 * セッション関連のヘルパークラス.
10 *
11 * @package Helper
12 * @author LOCKON CO.,LTD.
13 * @version $Id$
14 */
15class SC_Helper_Session
16{
17    /**
18     * トランザクショントークンを生成し, 取得する.
19     *
20     * 悪意のある不正な画面遷移を防止するため, 予測困難な文字列を生成して返す.
21     * 同時に, この文字列をセッションに保存する.
22     *
23     * この関数を使用するためには, 生成した文字列を次画面へ渡すパラメーターとして
24     * 出力する必要がある.
25     *
26     * 例)
27     * <input type='hidden' name='transactionid' value="この関数の返り値" />
28     *
29     * 遷移先のページで, LC_Page::isValidToken() の返り値をチェックすることにより,
30     * 画面遷移の妥当性が確認できる.
31     *
32     * @access protected
33     * @return string トランザクショントークンの文字列
34     */
35    public function getToken()
36    {
37        if (empty($_SESSION[TRANSACTION_ID_NAME])) {
38            $_SESSION[TRANSACTION_ID_NAME] = SC_Helper_Session_Ex::createToken();
39        }
40
41        return $_SESSION[TRANSACTION_ID_NAME];
42    }
43
44    /**
45     * トランザクショントークン用の予測困難な文字列を生成して返す.
46     *
47     * @access private
48     * @return string トランザクショントークン用の文字列
49     */
50    public function createToken()
51    {
52        return sha1(uniqid(rand(), true));
53    }
54
55    /**
56     * トランザクショントークンの妥当性をチェックする.
57     *
58     * 生成されたトランザクショントークンの妥当性をチェックする.
59     * この関数を使用するためには, 前画面のページクラスで LC_Page::getToken()
60     * を呼んでおく必要がある.
61     *
62     * トランザクショントークンは, SC_Helper_Session::getToken() が呼ばれた際に
63     * 生成される.
64     * 引数 $is_unset が false の場合は, トークンの妥当性検証が不正な場合か,
65     * セッションが破棄されるまで, トークンを保持する.
66     * 引数 $is_unset が true の場合は, 妥当性検証後に破棄される.
67     *
68     * @access protected
69     * @param boolean $is_unset 妥当性検証後, トークンを unset する場合 true;
70     *                          デフォルト値は false
71     * @return boolean トランザクショントークンが有効な場合 true
72     */
73    public function isValidToken($is_unset = false)
74    {
75        // token の妥当性チェック
76        $ret = $_REQUEST[TRANSACTION_ID_NAME] === $_SESSION[TRANSACTION_ID_NAME];
77
78        if ($is_unset || $ret === false) {
79            SC_Helper_Session_Ex::destroyToken();
80        }
81
82        return $ret;
83    }
84
85    /**
86     * トランザクショントークンを破棄する.
87     *
88     * @return void
89     */
90    public static function destroyToken()
91    {
92        unset($_SESSION[TRANSACTION_ID_NAME]);
93    }
94
95    /**
96     * 管理画面の認証を行う.
97     *
98     * mtb_auth_excludes へ登録されたページは, 認証を除外する.
99     *
100     * @return void
101     */
102    public static function adminAuthorization()
103    {
104        if (($script_path = realpath($_SERVER['SCRIPT_FILENAME'])) !== FALSE) {
105            $arrScriptPath = explode('/', str_replace('\\', '/', $script_path));
106            $arrAdminPath = explode('/', str_replace('\\', '/', substr(HTML_REALDIR . ADMIN_DIR, 0, -1)));
107            $arrDiff = array_diff_assoc($arrAdminPath, $arrScriptPath);
108            if (in_array(substr(ADMIN_DIR, 0, -1), $arrDiff)) {
109                return;
110            } else {
111                $masterData = new SC_DB_MasterData_Ex();
112                $arrExcludes = $masterData->getMasterData('mtb_auth_excludes');
113                foreach ($arrExcludes as $exclude) {
114                    $arrExcludesPath = explode('/', str_replace('\\', '/', HTML_REALDIR . ADMIN_DIR . $exclude));
115                    $arrDiff = array_diff_assoc($arrExcludesPath, $arrScriptPath);
116                    if (count($arrDiff) === 0) {
117                        return;
118                    }
119                }
120            }
121        }
122        SC_Utils_Ex::sfIsSuccess(new SC_Session_Ex());
123    }
124
125    /**
126     * セッションIDを新しいIDに書き換える
127     *
128     * @return bool
129     */
130    public static function regenerateSID()
131    {
132        return session_regenerate_id(true);
133    }
134}
Note: See TracBrowser for help on using the repository browser.