summaryrefslogtreecommitdiff
path: root/ext/session/tests/save_handler.inc
blob: 56b3505284bb5db46997f53a3ffa0b5ddafeb1d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

DEFINE("SESSION_FILE_PREFIX" ,"session_test_");

/*
 * == General Return Value Rule ==
 *
 * Returning FALSE indicates FATAL error.
 * Exceptions are: gc(), validate_sid()
 *
 * == Session Data Lock ==
 *
 * Session data lock is mandatory. Lock must be exclusive. i.e. Block read also.
 *
 * == Collision Detection ==
 *
 * Collision detection is mandatory to reject attacker initialized session ID.
 * Coolision detection is absolute requirement for secure session.
 */


/* Open session data database */
function open($save_path, $session_name) {
    // string $save_path - Directory path, connection strings, etc. Default: session.save_path
    // string $session_name - Session ID cookie name. Default: session.name

    global $session_save_path, $name;
    $session_save_path = $save_path;
    $name = $session_name;
    echo "Open [${session_save_path},${session_name}]\n";

    // MUST return bool. Return TRUE for success.
    return true;
}

/* Close session data database */
function close() {
    // void parameter
    // NOTE: This function should unlock session data, if write() does not unlock it.

    global $session_save_path, $name;
    echo "Close [${session_save_path},${name}]\n";

    // MUST return bool. Return TRUE for success.
    return true;
}

/* Read session data */
function read($id) {
    // string $id - Session ID string
    // NOTE: All production session save handler MUST implement "exclusive" lock.
    //       e.g. Use "serializable transaction isolation level" with RDBMS.
    //       read() would be the best place for locking for most save handlers.

    global $session_save_path, $name, $session_id;
    $session_id = $id;
    echo "Read [${session_save_path},${id}]\n";
    $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
    // read MUST create file. Otherwise, strict mode will not work
    touch($session_file);

    // MUST return STRING for successful read().
    // Return FALSE only when there is error. i.e. Do not return FALSE
    // for non-existing session data for the $id.
    return (string) @file_get_contents($session_file);
}

/* Write session data */
function write($id, $session_data) {
    // string $id - Session ID string
    // string $session_data - Session data string serialized by session serializer.
    // NOTE: This function may unlock session data locked by read(). If write() is
    //       is not suitable place your handler to unlock. Unlock data at close().

    global $session_save_path, $name, $session_id;
    $session_id = $id;
    echo "Write [${session_save_path},${id},${session_data}]\n";
    $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
    if ($fp = fopen($session_file, "w")) {
        $return = fwrite($fp, $session_data);
        fclose($fp);
        return $return === FALSE ? FALSE : TRUE;
    }

    // MUST return bool. Return TRUE for success.
    return false;
}

/* Remove specified session */
function destroy($id) {
    // string $id - Session ID string

    global $session_save_path, $name;
    echo "Destroy [${session_save_path},${id}]\n";
    $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
    unlink($session_file);

    // MUST return bool. Return TRUE for success.
    // Return FALSE only when there is error. i.e. Do not return FALSE
    // for non-existing session data for the $id.
    return true;
}

/* Perform garbage collection */
function gc($maxlifetime) {
    // long $maxlifetime - GC TTL in seconds. Default: session.gc_maxlifetime

    global $session_save_path, $name;
    $gc_cnt = 0;
    $directory = opendir($session_save_path."/");
    $length = strlen(SESSION_FILE_PREFIX);
    while (($file = readdir($directory)) !== FALSE) {
        $qualified = ($session_save_path."/".$file);
        if (is_file($qualified) === TRUE) {
            if (substr($file, 0, $length) === SESSION_FILE_PREFIX && (filemtime($qualified) + $maxlifetime <= time() )) {
                unlink($qualified);
                $gc_cnt++;
            }
        }
    }
    closedir($directory);

    // SHOULD return long (number of deleted sessions).
    // Returning TRUE works also, but it will not report correct number of deleted sessions.
    // Return negative value for error. FALSE does not work because it's the same as 0.
    return $gc_cnt;
}

/* Create new secure session ID */
function create_sid() {
    // void parameter
    // NOTE: Defining create_sid() is mandatory because validate_sid() is mandatory for
    //       security reasons for production save handler.
    //       PHP 7.1 has session_create_id() for secure session ID generation. Older PHPs
    //       must generate secure session ID by yourself.
    //       e.g. hash('sha2', random_bytes(64)) or use /dev/urandom

    $id = ('PHPT-'.time());
    echo "CreateID [${id}]\n";

    // MUST return session ID string.
    // Return FALSE for error.
    return $id;
}

/* Check session ID collision */
function validate_sid($id) {
    // string $id - Session ID string

    global $session_save_path, $name;
    echo "ValidateID [${session_save_path},${id}]\n";
    $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
    $ret = file_exists($session_file);

    // MUST return bool. Return TRUE for collision.
    // NOTE: This handler is mandatory for session security.
    //       All save handlers MUST implement this handler.
    //       Check session ID collision, return TRUE when it collides.
    //       Otherwise, return FALSE.
    return $ret;
}

/* Update session data access time stamp WITHOUT writing $session_data */
function update($id, $session_data) {
    // string $id - Session ID string
    // string $session_data - Session data serialized by session serializer
    // NOTE: This handler is optional. If your session database cannot
    //       support time stamp updating, you must not define this.

    global $session_save_path, $name;
    echo "Update [${session_save_path},${id}]\n";
    $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
    $ret = touch($session_file);

    // MUST return bool. Return TRUE for success.
    return $ret;
}


function feature() {
    /* NOT IMPLEMENTED YET */
    /* TYPES: gc, create_sid, use_strict_mode, minimzie_lock, lazy_write
    /* VALUES: 0=unknown, 1=supported, 2=partially supported, 3=unsupported */
    return array('gc'=>0,
                 'create_sid'=>1,
                 'use_strict_mode'=>2,
                 'minimize_lock'=>3,
                 'lazy_write'=>4,
                 'invalid'=>5,
                 'another invalid'=>6
                 );
}

?>