diff options
| author | jvoisin | 2019-01-14 19:29:25 +0000 |
|---|---|---|
| committer | GitHub | 2019-01-14 19:29:25 +0000 |
| commit | e79f7e3bd992c7f0915ef9afe7afb6d79740527a (patch) | |
| tree | f881c25694eb00da2331a9ab280ec1c24a5662ab /src/tests/session_encryption/set_custom_session_handler.phpt | |
| parent | c943db586ac46b686b49bdf61d8473e39dd93000 (diff) | |
Reorganize the testsuite
Splitting the testsuite in several components makes it easier to manage and comprehend.
This was also needed some some tests aren't passing on Alpine Linux, but we still want to run
as many of them as we can on this platform.
Diffstat (limited to 'src/tests/session_encryption/set_custom_session_handler.phpt')
| -rw-r--r-- | src/tests/session_encryption/set_custom_session_handler.phpt | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/tests/session_encryption/set_custom_session_handler.phpt b/src/tests/session_encryption/set_custom_session_handler.phpt new file mode 100644 index 0000000..5b46fbc --- /dev/null +++ b/src/tests/session_encryption/set_custom_session_handler.phpt | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | --TEST-- | ||
| 2 | Set a custom session handler | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | session.save_path = "/tmp" | ||
| 8 | --ENV-- | ||
| 9 | return <<<EOF | ||
| 10 | REMOTE_ADDR=127.0.0.1 | ||
| 11 | EOF; | ||
| 12 | --FILE-- | ||
| 13 | <?php | ||
| 14 | class FileSessionHandler { | ||
| 15 | private $savePath; | ||
| 16 | |||
| 17 | function open($savePath, $sessionName) { | ||
| 18 | $this->savePath = $savePath; | ||
| 19 | if (!is_dir($this->savePath)) { | ||
| 20 | mkdir($this->savePath, 0777); | ||
| 21 | } | ||
| 22 | |||
| 23 | return true; | ||
| 24 | } | ||
| 25 | |||
| 26 | function close() { | ||
| 27 | return true; | ||
| 28 | } | ||
| 29 | |||
| 30 | function read($id) { | ||
| 31 | return (string)@file_get_contents("$this->savePath/sess_$id"); | ||
| 32 | } | ||
| 33 | |||
| 34 | function write($id, $data) { | ||
| 35 | return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true; | ||
| 36 | } | ||
| 37 | |||
| 38 | function destroy($id) { | ||
| 39 | $file = "$this->savePath/sess_$id"; | ||
| 40 | if (file_exists($file)) { | ||
| 41 | unlink($file); | ||
| 42 | } | ||
| 43 | |||
| 44 | return true; | ||
| 45 | } | ||
| 46 | |||
| 47 | function gc($maxlifetime) { | ||
| 48 | foreach (glob("$this->savePath/sess_*") as $file) { | ||
| 49 | if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { | ||
| 50 | unlink($file); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | return true; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | $handler = new FileSessionHandler(); | ||
| 58 | session_set_save_handler( | ||
| 59 | array($handler, 'open'), | ||
| 60 | array($handler, 'close'), | ||
| 61 | array($handler, 'read'), | ||
| 62 | array($handler, 'write'), | ||
| 63 | array($handler, 'destroy'), | ||
| 64 | array($handler, 'gc') | ||
| 65 | ); | ||
| 66 | |||
| 67 | // the following prevents unexpected effects when using objects as save handlers | ||
| 68 | register_shutdown_function('session_write_close'); | ||
| 69 | |||
| 70 | session_start(); | ||
| 71 | // proceed to set and retrieve values by key from $_SESSION | ||
| 72 | --EXPECTF-- | ||
