Subversion Repositories Applications.framework

Rev

Rev 299 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 299 Rev 380
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
 * Zend Framework
4
 * Zend Framework
5
 *
5
 *
6
 * LICENSE
6
 * LICENSE
7
 *
7
 *
8
 * This source file is subject to the new BSD license that is bundled
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file LICENSE.txt.
9
 * with this package in the file LICENSE.txt.
10
 * It is also available through the world-wide-web at this URL:
10
 * It is also available through the world-wide-web at this URL:
11
 * http://framework.zend.com/license/new-bsd
11
 * http://framework.zend.com/license/new-bsd
12
 * If you did not receive a copy of the license and are unable to
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
13
 * obtain it through the world-wide-web, please send an email
14
 * to license@zend.com so we can send you a copy immediately.
14
 * to license@zend.com so we can send you a copy immediately.
15
 *
15
 *
16
 * @category   Zend
16
 * @category   Zend
17
 * @package    Zend_OpenId
17
 * @package    Zend_OpenId
18
 * @subpackage Zend_OpenId_Consumer
18
 * @subpackage Zend_OpenId_Consumer
19
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
20
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
21
 * @version    $Id: StorageFile.php 299 2011-01-18 14:03:46Z jpm $
21
 * @version    $Id: StorageFile.php 380 2011-10-14 09:14:02Z jpm $
22
 */
22
 */
23
 
23
 
24
/**
24
/**
25
 * External storage implemmentation using serialized files
25
 * External storage implemmentation using serialized files
26
 *
26
 *
27
 * @category   Zend
27
 * @category   Zend
28
 * @package    Zend_OpenId
28
 * @package    Zend_OpenId
29
 * @subpackage Zend_OpenId_Consumer
29
 * @subpackage Zend_OpenId_Consumer
30
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
30
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
31
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
32
 */
32
 */
33
class StorageFile extends Storage
33
class StorageFile extends Storage
34
{
34
{
35
 
35
 
36
    /**
36
    /**
37
     * Directory name to store data files in
37
     * Directory name to store data files in
38
     *
38
     *
39
     * @var string $_dir
39
     * @var string $_dir
40
     */
40
     */
41
    private $_dir;
41
    private $_dir;
42
 
42
 
43
    /**
43
    /**
44
     * Constructs storage object and creates storage directory
44
     * Constructs storage object and creates storage directory
45
     *
45
     *
46
     * @param string $dir directory name to store data files in
46
     * @param string $dir directory name to store data files in
47
     * @throws Zend_OpenId_Exception
47
     * @throws Zend_OpenId_Exception
48
     */
48
     */
49
    public function __construct($dir = null)
49
    public function __construct($dir = null)
50
    {
50
    {
51
        if ($dir === null) {
51
        if ($dir === null) {
52
            $tmp = getenv('TMP');
52
            $tmp = getenv('TMP');
53
            if (empty($tmp)) {
53
            if (empty($tmp)) {
54
                $tmp = getenv('TEMP');
54
                $tmp = getenv('TEMP');
55
                if (empty($tmp)) {
55
                if (empty($tmp)) {
56
                    $tmp = "/tmp";
56
                    $tmp = "/tmp";
57
                }
57
                }
58
            }
58
            }
59
            $user = get_current_user();
59
            $user = get_current_user();
60
            if (is_string($user) && !empty($user)) {
60
            if (is_string($user) && !empty($user)) {
61
                $tmp .= '/' . $user;
61
                $tmp .= '/' . $user;
62
            }
62
            }
63
            $dir = $tmp . '/openid/consumer';
63
            $dir = $tmp . '/openid/consumer';
64
        }
64
        }
65
        $this->_dir = $dir;
65
        $this->_dir = $dir;
66
        if (!is_dir($this->_dir)) {
66
        if (!is_dir($this->_dir)) {
67
            if (!@mkdir($this->_dir, 0700, 1)) {
67
            if (!@mkdir($this->_dir, 0700, 1)) {
68
                /**
68
                /**
69
                 * @see Zend_OpenId_Exception
69
                 * @see Zend_OpenId_Exception
70
                 */
70
                 */
71
                require_once 'Zend/OpenId/Exception.php';
71
                require_once 'Zend/OpenId/Exception.php';
72
                throw new Zend_OpenId_Exception(
72
                throw new Zend_OpenId_Exception(
73
                    'Cannot access storage directory ' . $dir,
73
                    'Cannot access storage directory ' . $dir,
74
                    Zend_OpenId_Exception::ERROR_STORAGE);
74
                    Zend_OpenId_Exception::ERROR_STORAGE);
75
            }
75
            }
76
        }
76
        }
77
        if (($f = fopen($this->_dir.'/assoc.lock', 'w+')) === null) {
77
        if (($f = fopen($this->_dir.'/assoc.lock', 'w+')) === null) {
78
            /**
78
            /**
79
             * @see Zend_OpenId_Exception
79
             * @see Zend_OpenId_Exception
80
             */
80
             */
81
            /*require_once 'Zend/OpenId/Exception.php';
81
            /*require_once 'Zend/OpenId/Exception.php';
82
            throw new Zend_OpenId_Exception(
82
            throw new Zend_OpenId_Exception(
83
                'Cannot create a lock file in the directory ' . $dir,
83
                'Cannot create a lock file in the directory ' . $dir,
84
                Zend_OpenId_Exception::ERROR_STORAGE);*/
84
                Zend_OpenId_Exception::ERROR_STORAGE);*/
85
        	
85
        	
86
        	trigger_error('Cannot create a lock file in the directory ', E_STRICT);
86
        	trigger_error('Cannot create a lock file in the directory ', E_STRICT);
87
        }
87
        }
88
        
88
        
89
        fclose($f);
89
        fclose($f);
90
        if (($f = fopen($this->_dir.'/discovery.lock', 'w+')) === null) {
90
        if (($f = fopen($this->_dir.'/discovery.lock', 'w+')) === null) {
91
            /**
91
            /**
92
             * @see Zend_OpenId_Exception
92
             * @see Zend_OpenId_Exception
93
             */
93
             */
94
            /*require_once 'Zend/OpenId/Exception.php';
94
            /*require_once 'Zend/OpenId/Exception.php';
95
            throw new Zend_OpenId_Exception(
95
            throw new Zend_OpenId_Exception(
96
                'Cannot create a lock file in the directory ' . $dir,
96
                'Cannot create a lock file in the directory ' . $dir,
97
                Zend_OpenId_Exception::ERROR_STORAGE);*/
97
                Zend_OpenId_Exception::ERROR_STORAGE);*/
98
        	trigger_error('Cannot create a lock file in the directory', E_STRICT);
98
        	trigger_error('Cannot create a lock file in the directory', E_STRICT);
99
        }
99
        }
100
        fclose($f);
100
        fclose($f);
101
        if (($f = fopen($this->_dir.'/nonce.lock', 'w+')) === null) {
101
        if (($f = fopen($this->_dir.'/nonce.lock', 'w+')) === null) {
102
            /**
102
            /**
103
             * @see Zend_OpenId_Exception
103
             * @see Zend_OpenId_Exception
104
             */
104
             */
105
            /*require_once 'Zend/OpenId/Exception.php';
105
            /*require_once 'Zend/OpenId/Exception.php';
106
            throw new Zend_OpenId_Exception(
106
            throw new Zend_OpenId_Exception(
107
                'Cannot create a lock file in the directory ' . $dir,
107
                'Cannot create a lock file in the directory ' . $dir,
108
                Zend_OpenId_Exception::ERROR_STORAGE);*/
108
                Zend_OpenId_Exception::ERROR_STORAGE);*/
109
        	trigger_error('Cannot create a lock file in the directory', E_STRICT);
109
        	trigger_error('Cannot create a lock file in the directory', E_STRICT);
110
        }
110
        }
111
        fclose($f);
111
        fclose($f);
112
    }
112
    }
113
 
113
 
114
    /**
114
    /**
115
     * Stores information about association identified by $url/$handle
115
     * Stores information about association identified by $url/$handle
116
     *
116
     *
117
     * @param string $url OpenID server URL
117
     * @param string $url OpenID server URL
118
     * @param string $handle assiciation handle
118
     * @param string $handle assiciation handle
119
     * @param string $macFunc HMAC function (sha1 or sha256)
119
     * @param string $macFunc HMAC function (sha1 or sha256)
120
     * @param string $secret shared secret
120
     * @param string $secret shared secret
121
     * @param long $expires expiration UNIX time
121
     * @param long $expires expiration UNIX time
122
     * @return bool
122
     * @return bool
123
     */
123
     */
124
    public function addAssociation($url, $handle, $macFunc, $secret, $expires)
124
    public function addAssociation($url, $handle, $macFunc, $secret, $expires)
125
    {
125
    {
126
        $name1 = $this->_dir . '/assoc_url_' . md5($url);
126
        $name1 = $this->_dir . '/assoc_url_' . md5($url);
127
        $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
127
        $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
128
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
128
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
129
        if ($lock === false) {
129
        if ($lock === false) {
130
            return false;
130
            return false;
131
        }
131
        }
132
        if (!flock($lock, LOCK_EX)) {
132
        if (!flock($lock, LOCK_EX)) {
133
            fclose($lock);
133
            fclose($lock);
134
            return false;
134
            return false;
135
        }
135
        }
136
        try {
136
        try {
137
            $f = @fopen($name1, 'w+');
137
            $f = @fopen($name1, 'w+');
138
            if ($f === false) {
138
            if ($f === false) {
139
                fclose($lock);
139
                fclose($lock);
140
                return false;
140
                return false;
141
            }
141
            }
142
            $data = serialize(array($url, $handle, $macFunc, $secret, $expires));
142
            $data = serialize(array($url, $handle, $macFunc, $secret, $expires));
143
            fwrite($f, $data);
143
            fwrite($f, $data);
144
            if (function_exists('symlink')) {
144
            if (function_exists('symlink')) {
145
                @unlink($name2);
145
                @unlink($name2);
146
                if (symlink($name1, $name2)) {
146
                if (symlink($name1, $name2)) {
147
                    fclose($f);
147
                    fclose($f);
148
                    fclose($lock);
148
                    fclose($lock);
149
                    return true;
149
                    return true;
150
                }
150
                }
151
            }
151
            }
152
            $f2 = @fopen($name2, 'w+');
152
            $f2 = @fopen($name2, 'w+');
153
            if ($f2) {
153
            if ($f2) {
154
                fwrite($f2, $data);
154
                fwrite($f2, $data);
155
                fclose($f2);
155
                fclose($f2);
156
                @unlink($name1);
156
                @unlink($name1);
157
                $ret = true;
157
                $ret = true;
158
            } else {
158
            } else {
159
                $ret = false;
159
                $ret = false;
160
            }
160
            }
161
            fclose($f);
161
            fclose($f);
162
            fclose($lock);
162
            fclose($lock);
163
            return $ret;
163
            return $ret;
164
        } catch (Exception $e) {
164
        } catch (Exception $e) {
165
            fclose($lock);
165
            fclose($lock);
166
            throw $e;
166
            throw $e;
167
        }
167
        }
168
    }
168
    }
169
 
169
 
170
    /**
170
    /**
171
     * Gets information about association identified by $url
171
     * Gets information about association identified by $url
172
     * Returns true if given association found and not expired and false
172
     * Returns true if given association found and not expired and false
173
     * otherwise
173
     * otherwise
174
     *
174
     *
175
     * @param string $url OpenID server URL
175
     * @param string $url OpenID server URL
176
     * @param string &$handle assiciation handle
176
     * @param string &$handle assiciation handle
177
     * @param string &$macFunc HMAC function (sha1 or sha256)
177
     * @param string &$macFunc HMAC function (sha1 or sha256)
178
     * @param string &$secret shared secret
178
     * @param string &$secret shared secret
179
     * @param long &$expires expiration UNIX time
179
     * @param long &$expires expiration UNIX time
180
     * @return bool
180
     * @return bool
181
     */
181
     */
182
    public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires)
182
    public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires)
183
    {
183
    {
184
        $name1 = $this->_dir . '/assoc_url_' . md5($url);
184
        $name1 = $this->_dir . '/assoc_url_' . md5($url);
185
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
185
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
186
        if ($lock === false) {
186
        if ($lock === false) {
187
            return false;
187
            return false;
188
        }
188
        }
189
        if (!flock($lock, LOCK_EX)) {
189
        if (!flock($lock, LOCK_EX)) {
190
            fclose($lock);
190
            fclose($lock);
191
            return false;
191
            return false;
192
        }
192
        }
193
        try {
193
        try {
194
            $f = @fopen($name1, 'r');
194
            $f = @fopen($name1, 'r');
195
            if ($f === false) {
195
            if ($f === false) {
196
                fclose($lock);
196
                fclose($lock);
197
                return false;
197
                return false;
198
            }
198
            }
199
            $ret = false;
199
            $ret = false;
200
            $data = stream_get_contents($f);
200
            $data = stream_get_contents($f);
201
            if (!empty($data)) {
201
            if (!empty($data)) {
202
                list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
202
                list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
203
                if ($url === $storedUrl && $expires > time()) {
203
                if ($url === $storedUrl && $expires > time()) {
204
                    $ret = true;
204
                    $ret = true;
205
                } else {
205
                } else {
206
                    $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
206
                    $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
207
                    fclose($f);
207
                    fclose($f);
208
                    @unlink($name2);
208
                    @unlink($name2);
209
                    @unlink($name1);
209
                    @unlink($name1);
210
                    fclose($lock);
210
                    fclose($lock);
211
                    return false;
211
                    return false;
212
                }
212
                }
213
            }
213
            }
214
            fclose($f);
214
            fclose($f);
215
            fclose($lock);
215
            fclose($lock);
216
            return $ret;
216
            return $ret;
217
        } catch (Exception $e) {
217
        } catch (Exception $e) {
218
            fclose($lock);
218
            fclose($lock);
219
            throw $e;
219
            throw $e;
220
        }
220
        }
221
    }
221
    }
222
 
222
 
223
    /**
223
    /**
224
     * Gets information about association identified by $handle
224
     * Gets information about association identified by $handle
225
     * Returns true if given association found and not expired and false
225
     * Returns true if given association found and not expired and false
226
     * otherwise
226
     * otherwise
227
     *
227
     *
228
     * @param string $handle assiciation handle
228
     * @param string $handle assiciation handle
229
     * @param string &$url OpenID server URL
229
     * @param string &$url OpenID server URL
230
     * @param string &$macFunc HMAC function (sha1 or sha256)
230
     * @param string &$macFunc HMAC function (sha1 or sha256)
231
     * @param string &$secret shared secret
231
     * @param string &$secret shared secret
232
     * @param long &$expires expiration UNIX time
232
     * @param long &$expires expiration UNIX time
233
     * @return bool
233
     * @return bool
234
     */
234
     */
235
    public function getAssociationByHandle($handle, &$url, &$macFunc, &$secret, &$expires)
235
    public function getAssociationByHandle($handle, &$url, &$macFunc, &$secret, &$expires)
236
    {
236
    {
237
        $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
237
        $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
238
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
238
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
239
        if ($lock === false) {
239
        if ($lock === false) {
240
            return false;
240
            return false;
241
        }
241
        }
242
        if (!flock($lock, LOCK_EX)) {
242
        if (!flock($lock, LOCK_EX)) {
243
            fclose($lock);
243
            fclose($lock);
244
            return false;
244
            return false;
245
        }
245
        }
246
        try {
246
        try {
247
            $f = @fopen($name2, 'r');
247
            $f = @fopen($name2, 'r');
248
            if ($f === false) {
248
            if ($f === false) {
249
                fclose($lock);
249
                fclose($lock);
250
                return false;
250
                return false;
251
            }
251
            }
252
            $ret = false;
252
            $ret = false;
253
            $data = stream_get_contents($f);
253
            $data = stream_get_contents($f);
254
            if (!empty($data)) {
254
            if (!empty($data)) {
255
                list($url, $storedHandle, $macFunc, $secret, $expires) = unserialize($data);
255
                list($url, $storedHandle, $macFunc, $secret, $expires) = unserialize($data);
256
                if ($handle === $storedHandle && $expires > time()) {
256
                if ($handle === $storedHandle && $expires > time()) {
257
                    $ret = true;
257
                    $ret = true;
258
                } else {
258
                } else {
259
                    fclose($f);
259
                    fclose($f);
260
                    @unlink($name2);
260
                    @unlink($name2);
261
                    $name1 = $this->_dir . '/assoc_url_' . md5($url);
261
                    $name1 = $this->_dir . '/assoc_url_' . md5($url);
262
                    @unlink($name1);
262
                    @unlink($name1);
263
                    fclose($lock);
263
                    fclose($lock);
264
                    return false;
264
                    return false;
265
                }
265
                }
266
            }
266
            }
267
            fclose($f);
267
            fclose($f);
268
            fclose($lock);
268
            fclose($lock);
269
            return $ret;
269
            return $ret;
270
        } catch (Exception $e) {
270
        } catch (Exception $e) {
271
            fclose($lock);
271
            fclose($lock);
272
            throw $e;
272
            throw $e;
273
        }
273
        }
274
    }
274
    }
275
 
275
 
276
    /**
276
    /**
277
     * Deletes association identified by $url
277
     * Deletes association identified by $url
278
     *
278
     *
279
     * @param string $url OpenID server URL
279
     * @param string $url OpenID server URL
280
     * @return bool
280
     * @return bool
281
     */
281
     */
282
    public function delAssociation($url)
282
    public function delAssociation($url)
283
    {
283
    {
284
        $name1 = $this->_dir . '/assoc_url_' . md5($url);
284
        $name1 = $this->_dir . '/assoc_url_' . md5($url);
285
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
285
        $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
286
        if ($lock === false) {
286
        if ($lock === false) {
287
            return false;
287
            return false;
288
        }
288
        }
289
        if (!flock($lock, LOCK_EX)) {
289
        if (!flock($lock, LOCK_EX)) {
290
            fclose($lock);
290
            fclose($lock);
291
            return false;
291
            return false;
292
        }
292
        }
293
        try {
293
        try {
294
            $f = @fopen($name1, 'r');
294
            $f = @fopen($name1, 'r');
295
            if ($f === false) {
295
            if ($f === false) {
296
                fclose($lock);
296
                fclose($lock);
297
                return false;
297
                return false;
298
            }
298
            }
299
            $data = stream_get_contents($f);
299
            $data = stream_get_contents($f);
300
            if (!empty($data)) {
300
            if (!empty($data)) {
301
                list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
301
                list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
302
                if ($url === $storedUrl) {
302
                if ($url === $storedUrl) {
303
                    $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
303
                    $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
304
                    fclose($f);
304
                    fclose($f);
305
                    @unlink($name2);
305
                    @unlink($name2);
306
                    @unlink($name1);
306
                    @unlink($name1);
307
                    fclose($lock);
307
                    fclose($lock);
308
                    return true;
308
                    return true;
309
                }
309
                }
310
            }
310
            }
311
            fclose($f);
311
            fclose($f);
312
            fclose($lock);
312
            fclose($lock);
313
            return true;
313
            return true;
314
        } catch (Exception $e) {
314
        } catch (Exception $e) {
315
            fclose($lock);
315
            fclose($lock);
316
            throw $e;
316
            throw $e;
317
        }
317
        }
318
    }
318
    }
319
 
319
 
320
    /**
320
    /**
321
     * Stores information discovered from identity $id
321
     * Stores information discovered from identity $id
322
     *
322
     *
323
     * @param string $id identity
323
     * @param string $id identity
324
     * @param string $realId discovered real identity URL
324
     * @param string $realId discovered real identity URL
325
     * @param string $server discovered OpenID server URL
325
     * @param string $server discovered OpenID server URL
326
     * @param float $version discovered OpenID protocol version
326
     * @param float $version discovered OpenID protocol version
327
     * @param long $expires expiration UNIX time
327
     * @param long $expires expiration UNIX time
328
     * @return bool
328
     * @return bool
329
     */
329
     */
330
    public function addDiscoveryInfo($id, $realId, $server, $version, $expires)
330
    public function addDiscoveryInfo($id, $realId, $server, $version, $expires)
331
    {
331
    {
332
        $name = $this->_dir . '/discovery_' . md5($id);
332
        $name = $this->_dir . '/discovery_' . md5($id);
333
        $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
333
        $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
334
        if ($lock === false) {
334
        if ($lock === false) {
335
            return false;
335
            return false;
336
        }
336
        }
337
        if (!flock($lock, LOCK_EX)) {
337
        if (!flock($lock, LOCK_EX)) {
338
            fclose($lock);
338
            fclose($lock);
339
            return false;
339
            return false;
340
        }
340
        }
341
        try {
341
        try {
342
            $f = @fopen($name, 'w+');
342
            $f = @fopen($name, 'w+');
343
            if ($f === false) {
343
            if ($f === false) {
344
                fclose($lock);
344
                fclose($lock);
345
                return false;
345
                return false;
346
            }
346
            }
347
            $data = serialize(array($id, $realId, $server, $version, $expires));
347
            $data = serialize(array($id, $realId, $server, $version, $expires));
348
            fwrite($f, $data);
348
            fwrite($f, $data);
349
            fclose($f);
349
            fclose($f);
350
            fclose($lock);
350
            fclose($lock);
351
            return true;
351
            return true;
352
        } catch (Exception $e) {
352
        } catch (Exception $e) {
353
            fclose($lock);
353
            fclose($lock);
354
            throw $e;
354
            throw $e;
355
        }
355
        }
356
    }
356
    }
357
 
357
 
358
    /**
358
    /**
359
     * Gets information discovered from identity $id
359
     * Gets information discovered from identity $id
360
     * Returns true if such information exists and false otherwise
360
     * Returns true if such information exists and false otherwise
361
     *
361
     *
362
     * @param string $id identity
362
     * @param string $id identity
363
     * @param string &$realId discovered real identity URL
363
     * @param string &$realId discovered real identity URL
364
     * @param string &$server discovered OpenID server URL
364
     * @param string &$server discovered OpenID server URL
365
     * @param float &$version discovered OpenID protocol version
365
     * @param float &$version discovered OpenID protocol version
366
     * @param long &$expires expiration UNIX time
366
     * @param long &$expires expiration UNIX time
367
     * @return bool
367
     * @return bool
368
     */
368
     */
369
    public function getDiscoveryInfo($id, &$realId, &$server, &$version, &$expires)
369
    public function getDiscoveryInfo($id, &$realId, &$server, &$version, &$expires)
370
    {
370
    {
371
        $name = $this->_dir . '/discovery_' . md5($id);
371
        $name = $this->_dir . '/discovery_' . md5($id);
372
        $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
372
        $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
373
        if ($lock === false) {
373
        if ($lock === false) {
374
            return false;
374
            return false;
375
        }
375
        }
376
        if (!flock($lock, LOCK_EX)) {
376
        if (!flock($lock, LOCK_EX)) {
377
            fclose($lock);
377
            fclose($lock);
378
            return false;
378
            return false;
379
        }
379
        }
380
        try {
380
        try {
381
            $f = @fopen($name, 'r');
381
            $f = @fopen($name, 'r');
382
            if ($f === false) {
382
            if ($f === false) {
383
                fclose($lock);
383
                fclose($lock);
384
                return false;
384
                return false;
385
            }
385
            }
386
            $ret = false;
386
            $ret = false;
387
            $data = stream_get_contents($f);
387
            $data = stream_get_contents($f);
388
            if (!empty($data)) {
388
            if (!empty($data)) {
389
                list($storedId, $realId, $server, $version, $expires) = unserialize($data);
389
                list($storedId, $realId, $server, $version, $expires) = unserialize($data);
390
                if ($id === $storedId && $expires > time()) {
390
                if ($id === $storedId && $expires > time()) {
391
                    $ret = true;
391
                    $ret = true;
392
                } else {
392
                } else {
393
                    fclose($f);
393
                    fclose($f);
394
                    @unlink($name);
394
                    @unlink($name);
395
                    fclose($lock);
395
                    fclose($lock);
396
                    return false;
396
                    return false;
397
                }
397
                }
398
            }
398
            }
399
            fclose($f);
399
            fclose($f);
400
            fclose($lock);
400
            fclose($lock);
401
            return $ret;
401
            return $ret;
402
        } catch (Exception $e) {
402
        } catch (Exception $e) {
403
            fclose($lock);
403
            fclose($lock);
404
            throw $e;
404
            throw $e;
405
        }
405
        }
406
    }
406
    }
407
 
407
 
408
    /**
408
    /**
409
     * Removes cached information discovered from identity $id
409
     * Removes cached information discovered from identity $id
410
     *
410
     *
411
     * @param string $id identity
411
     * @param string $id identity
412
     * @return bool
412
     * @return bool
413
     */
413
     */
414
    public function delDiscoveryInfo($id)
414
    public function delDiscoveryInfo($id)
415
    {
415
    {
416
        $name = $this->_dir . '/discovery_' . md5($id);
416
        $name = $this->_dir . '/discovery_' . md5($id);
417
        $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
417
        $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
418
        if ($lock === false) {
418
        if ($lock === false) {
419
            return false;
419
            return false;
420
        }
420
        }
421
        if (!flock($lock, LOCK_EX)) {
421
        if (!flock($lock, LOCK_EX)) {
422
            fclose($lock);
422
            fclose($lock);
423
            return false;
423
            return false;
424
        }
424
        }
425
        try {
425
        try {
426
            @unlink($name);
426
            @unlink($name);
427
            fclose($lock);
427
            fclose($lock);
428
            return true;
428
            return true;
429
        } catch (Exception $e) {
429
        } catch (Exception $e) {
430
            fclose($lock);
430
            fclose($lock);
431
            throw $e;
431
            throw $e;
432
        }
432
        }
433
    }
433
    }
434
 
434
 
435
    /**
435
    /**
436
     * The function checks the uniqueness of openid.response_nonce
436
     * The function checks the uniqueness of openid.response_nonce
437
     *
437
     *
438
     * @param string $provider openid.openid_op_endpoint field from authentication response
438
     * @param string $provider openid.openid_op_endpoint field from authentication response
439
     * @param  string $nonce openid.response_nonce field from authentication response
439
     * @param  string $nonce openid.response_nonce field from authentication response
440
     * @return bool
440
     * @return bool
441
     */
441
     */
442
    public function isUniqueNonce($provider, $nonce)
442
    public function isUniqueNonce($provider, $nonce)
443
    {
443
    {
444
        $name = $this->_dir . '/nonce_' . md5($provider.';'.$nonce);
444
        $name = $this->_dir . '/nonce_' . md5($provider.';'.$nonce);
445
        echo $name;
445
        echo $name;
446
        $lock = @fopen($this->_dir . '/nonce.lock', 'w+');
446
        $lock = @fopen($this->_dir . '/nonce.lock', 'w+');
447
        if ($lock === false) {
447
        if ($lock === false) {
448
            return false;
448
            return false;
449
        }
449
        }
450
        if (!flock($lock, LOCK_EX)) {
450
        if (!flock($lock, LOCK_EX)) {
451
            fclose($lock);
451
            fclose($lock);
452
            return false;
452
            return false;
453
        }
453
        }
454
        try {
454
        try {
455
            $f = @fopen($name, 'x');
455
            $f = @fopen($name, 'x');
456
            if ($f === false) {
456
            if ($f === false) {
457
                fclose($lock);
457
                fclose($lock);
458
                return false;
458
                return false;
459
            }
459
            }
460
            fwrite($f, $provider.';'.$nonce);
460
            fwrite($f, $provider.';'.$nonce);
461
            fclose($f);
461
            fclose($f);
462
            fclose($lock);
462
            fclose($lock);
463
            return true;
463
            return true;
464
        } catch (Exception $e) {
464
        } catch (Exception $e) {
465
            fclose($lock);
465
            fclose($lock);
466
            throw $e;
466
            throw $e;
467
        }
467
        }
468
    }
468
    }
469
 
469
 
470
    /**
470
    /**
471
     * Removes data from the uniqueness database that is older then given date
471
     * Removes data from the uniqueness database that is older then given date
472
     *
472
     *
473
     * @param mixed $date date of expired data
473
     * @param mixed $date date of expired data
474
     */
474
     */
475
    public function purgeNonces($date=null)
475
    public function purgeNonces($date=null)
476
    {
476
    {
477
        $lock = @fopen($this->_dir . '/nonce.lock', 'w+');
477
        $lock = @fopen($this->_dir . '/nonce.lock', 'w+');
478
        if ($lock !== false) {
478
        if ($lock !== false) {
479
            flock($lock, LOCK_EX);
479
            flock($lock, LOCK_EX);
480
        }
480
        }
481
        try {
481
        try {
482
            if (!is_int($date) && !is_string($date)) {
482
            if (!is_int($date) && !is_string($date)) {
483
                $nonceFiles = glob($this->_dir . '/nonce_*');
483
                $nonceFiles = glob($this->_dir . '/nonce_*');
484
                foreach ((array) $nonceFiles as $name) {
484
                foreach ((array) $nonceFiles as $name) {
485
                    @unlink($name);
485
                    @unlink($name);
486
                }
486
                }
487
                unset($nonceFiles);
487
                unset($nonceFiles);
488
            } else {
488
            } else {
489
                if (is_string($date)) {
489
                if (is_string($date)) {
490
                    $time = time($date);
490
                    $time = time($date);
491
                } else {
491
                } else {
492
                    $time = $date;
492
                    $time = $date;
493
                }
493
                }
494
                $nonceFiles = glob($this->_dir . '/nonce_*');
494
                $nonceFiles = glob($this->_dir . '/nonce_*');
495
                foreach ((array) $nonceFiles as $name) {
495
                foreach ((array) $nonceFiles as $name) {
496
                    if (filemtime($name) < $time) {
496
                    if (filemtime($name) < $time) {
497
                        @unlink($name);
497
                        @unlink($name);
498
                    }
498
                    }
499
                }
499
                }
500
                unset($nonceFiles);
500
                unset($nonceFiles);
501
            }
501
            }
502
            if ($lock !== false) {
502
            if ($lock !== false) {
503
                fclose($lock);
503
                fclose($lock);
504
            }
504
            }
505
        } catch (Exception $e) {
505
        } catch (Exception $e) {
506
            if ($lock !== false) {
506
            if ($lock !== false) {
507
                fclose($lock);
507
                fclose($lock);
508
            }
508
            }
509
            throw $e;
509
            throw $e;
510
        }
510
        }
511
    }
511
    }
512
}
512
}