/var/www/hkosl.com/securexpert/vendor/dapphp/securimage/StorageAdapter/Redis.php


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
<?php

namespace Securimage\StorageAdapter;

use 
Securimage\StorageAdapter\AdapterInterface;

class 
Redis implements AdapterInterface
{
    protected 
$server;
    protected 
$persistent;
    protected 
$dbindex;
    protected 
$expiration;

    protected 
$redis;

    public function 
__construct($options null)
    {
        if (!
class_exists('Redis')) {
            throw new \
Exception("Redis extension is not enabled.  Securimage Redis adapter cannot function");
        }

        if (!
is_array($options)) {
            throw new \
Exception("Options supplied to Securimage Redis adapter must be an array");
        }

        if (!isset(
$options['redis_server'])) {
            throw new \
Exception("'redis_server' option was supplied to StorageAdapter\Redis::__construct");
        }

        if (!
is_string($options['redis_server'])) {
            throw new \
Exception("'redis_server' option must be a string");
        }

        
$this->server     $options['redis_server'];
        
$this->persistent = @$options['redis_persistent'];
        
$this->expiration = ((int)$options['expiration'] > 0) ? (int)$options['expiration'] : 900;
        
$this->dbindex    = (isset($options['redis_dbindex'])) ? $options['redis_dbindex'] : null;

        
$this->bootstrap();
    }

    public function 
store($captchaId$captchaInfo)
    {
        
$hash = array();
        foreach(
$captchaInfo as $prop => $val) {
            
// convert CaptchaObject to array
            
$hash[$prop] = $val;
        }

        if (
$this->redis->hMSet($captchaId$hash)) {
            
$this->redis->setTimeout($captchaId$this->expiration);
            return 
true;
        } else {
            return 
false;
        }
    }

    public function 
storeAudioData($captchaId$audioData)
    {
        return 
$this->redis->hSet($captchaId'captchaAudioData'$audioData) !== false;
    }

    public function 
get($captchaId$what null)
    {
        
$result null;

        if (
is_null($what)) {
            
$result $this->redis->hGetAll($captchaId);
        } else {
            if (
is_string($what)) $what = array($what);
            if (!
is_array($what)) {
                
trigger_error(
                    
"'what' parameter passed to StorageAdapter\Redis::get was neither an array nor string",
                    
E_USER_WARNING
                
);
                return 
false;
            }

            
$result $this->redis->hMget($captchaId$what);
        }

        if (
$result) {
            
$info = new \Securimage\CaptchaObject;
            foreach(
$result as $key => $val) {
                
$info->$key $val;
            }

            return 
$info;
        }

        return 
null;
    }

    public function 
delete($captchaId)
    {
        return 
$this->redis->del($captchaId) > 0;
    }

    protected function 
bootstrap()
    {
        
$this->redis = new \Redis();

        
$connect 'connect';

        if (
$this->persistent) {
            
$connect 'pconnect';
        }

        
$server $this->server;
        
$parts  explode(':'$server2);
        
$server $parts[0];
        
$port   = (isset($parts[1]) ? $parts[1] : null);

        if (
$this->redis->$connect($server$port)) {
            
$this->redis->setOption(\Redis::OPT_PREFIX'securimage:');
            if (
$this->dbindex) {
                
$this->redis->select($this->dbindex);
            }
        }
    }

    protected function 
getKey($captchaId)
    {
        return 
'securimage:' $captchaId;
    }
}