1<?php
2namespace DALMP\Sessions;
3
4/**
5 * Sessions\Files
6 *
7 * @author Nicolas Embriz <nbari@dalmp.com>
8 * @package DALMP
9 * @license BSD License
10 * @version 3.0.3
11 */
12class Files implements \SessionHandlerInterface
13{
14    /**
15     * path to store sessions
16     *
17     * @access private
18     * @var string
19     */
20    private $sessions_dir;
21
22    /**
23     * constructor
24     *
25     * @param string $dir
26     */
27    public function __construct($sessions_dir = false)
28    {
29        if (!$sessions_dir) {
30            $sessions_dir = defined('DALMP_SESSIONS_DIR') ? DALMP_SESSIONS_DIR : '/tmp/dalmp_sessions';
31        }
32
33        if (!is_writable($sessions_dir)) {
34            if (!is_dir($sessions_dir) && !mkdir($sessions_dir, 0700, true)) {
35                throw new \InvalidArgumentException($sessions_dir . ' not accessible');
36            }
37        }
38
39        $this->sessions_dir = $dir;
40    }
41
42    public function close()
43    {
44        return true;
45    }
46
47    public function destroy($session_id)
48    {
49        $sess_path = sprintf('%s/%s/%s/%s', $this->sessions_dir, substr($session_id, 0, 2), substr($session_id, 2, 2),  substr($session_id, 4, 2));
50        $sess_file = sprintf('%s/%s', $sess_path , "{$session_id}.sess");
51
52        if (file_exists($sess_file)) {
53            unlink($sess_file);
54        }
55
56        return true;
57    }
58
59    public function gc($maxlifetime)
60    {
61        $session_files = $this->rsearch($this->sessions_dir, '#^.*\.sess$#');
62
63        foreach ($session_files as $file) {
64            if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
65                unlink($file);
66            }
67        }
68
69        return true;
70    }
71
72    public function open($save_path, $name)
73    {
74        return true;
75    }
76
77    public function read($session_id)
78    {
79        $sess_path = sprintf('%s/%s/%s/%s', $this->sessions_dir, substr($session_id, 0, 2), substr($session_id, 2, 2),  substr($session_id, 4, 2));
80
81        if (!is_dir($sess_path) && !mkdir($sess_path, 0700, true)) {
82            throw new \Exception("$sess_path  not accessible");
83        }
84
85        $sess_file = sprintf('%s/%s', $sess_path , "{$session_id}.sess");
86
87        return (string) @file_get_contents($sess_file);
88    }
89
90    public function write($session_id, $session_data)
91    {
92        $sess_path = sprintf('%s/%s/%s/%s', $this->sessions_dir, substr($session_id, 0, 2), substr($session_id, 2, 2),  substr($session_id, 4, 2));
93
94        if (!is_dir($sess_path) && !mkdir($sess_path, 0700, true)) {
95            throw new \Exception("$sess_path  not accessible");
96        }
97
98        $sess_file = sprintf('%s/%s', $sess_path , "{$session_id}.sess");
99
100        return file_put_contents($sess_file, $session_data) === false ? false : true;
101    }
102
103    /**
104     * getSessionsRefs
105     *
106     * @param  int   $expiry
107     * @return array of sessions containing any reference
108     */
109    public function getSessionsRefs($expired_sessions = false)
110    {
111        $refs = array();
112
113        return false;
114    }
115
116    /**
117     * getSessionRef
118     *
119     * @param  string $ref
120     * @return array  of session containing a specific reference
121     */
122    public function getSessionRef($ref)
123    {
124        return false;
125    }
126
127    /**
128     * delSessionsRef - delete sessions containing a specific reference
129     *
130     * @param  string  $ref
131     * @return boolean
132     */
133    public function delSessionRef($ref)
134    {
135        return false;
136    }
137
138    /**
139     * recursive dir search
140     *
141     * @param string $folder
142     * @param string $pattern example: '#^.*\.sess$#'
143     */
144    public function rsearch($folder, $pattern)
145    {
146        $dir = new RecursiveDirectoryIterator($folder);
147        $ite = new RecursiveIteratorIterator($dir);
148        $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
149        $fileList = array();
150        foreach ($files as $file) {
151            $fileList = array_merge($fileList, $file);
152        }
153
154        return $fileList;
155    }
156
157}
158