Passed
Pull Request — develop (#20)
by Pieter van der
17:16
created

Tiqr_StateStorage_File::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.9666
cc 4
nc 4
nop 1
crap 4
1
<?php 
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Ivo Jansch <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2011 SURFnet BV
18
 */
19
20
21
/**
22
 * File based implementation to store session state data. 
23
 * Note that it is more secure to use a memory based storage such as memcached.
24
 * This implementation is mostly for demo, test or experimental setups that 
25
 * do not have access to a memcache instance.
26
 * 
27
 * This StateStorage implementation has no options, files are always stored
28
 * in /tmp and prefixed with tiqr_state_*
29
 * 
30
 * @author ivo
31
 *
32
 */
33
class Tiqr_StateStorage_File extends Tiqr_StateStorage_Abstract
34
{
35
    /**
36
     * (non-PHPdoc)
37
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue()
38
     */
39 3
    public function setValue($key, $value, $expire=0)
40
    {   
41 3
        $envelope = array("expire"=>$expire,
42 3
                          "createdAt"=>time(),
43 3
                          "value"=>$value);
44 3
        $filename = $this->_stateFilename($key);
45
        
46 3
        file_put_contents($filename, serialize($envelope));
47
        
48 3
        return $key;
49
    }
50
51
    /**
52
     * (non-PHPdoc)
53
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue()
54
     */
55 3
    public function unsetValue($key)
56
    {
57 3
        $filename = $this->_stateFilename($key);
58 3
        if (file_exists($filename)) {
59 3
            unlink($filename);
60
        }
61 3
    }
62
    
63
    /**
64
     * (non-PHPdoc)
65
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue()
66
     */
67 3
    public function getValue($key)
68
    {
69 3
        $filename = $this->_stateFilename($key);
70 3
        if (file_exists($filename)) {
71 3
            $envelope = unserialize(file_get_contents($filename));
72 3
            if ($envelope["expire"]!=0) {
73
                 // This data is time-limited. If it's too old we discard it.
74 3
                 if (time()-$envelope["createdAt"] > $envelope["expire"]) {
75 1
                     $this->unsetValue($key); 
76 1
                     return NULL;
77
                 }
78
            }
79 3
            return $envelope["value"];
80
        }
81 3
        return NULL;
82
    }
83
    
84
    /**
85
     * Determine the name of a temporary file to hold the contents of $key
86
     * @param String $key The key for which to store data.
87
     */
88 3
    protected function _stateFilename($key)
89
    {
90 3
        return "/tmp/tiqr_state_".strtr(base64_encode($key), '+/', '-_');
91
    }
92
    
93
}
94