/var/www/hkosl.com/securexpert/vendor/dapphp/securimage/StorageAdapter/Memcached.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
<?php

namespace Securimage\StorageAdapter;

use 
Securimage\StorageAdapter\AdapterInterface;

class 
Memcached implements AdapterInterface
{
    protected 
$memcached_servers;
    protected 
$persistent;
    protected 
$expiration;

    protected 
$memcached;

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

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

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

        if (!
is_array($options['memcached_servers'])) {
            throw new \
Exception("'memcached_servers' option must be an array of servers");
        }

        
$this->memcached_servers $options['memcached_servers'];
        
$this->persistent        = @$options['memcached_persistent'];
        
$this->expiration        = ((int)$options['expiration'] > 0) ? (int)$options['expiration'] : 900;

        
$this->bootstrap();
    }

    public function 
store($captchaId$captchaInfo)
    {
        return 
$this->memcached->set($this->getKey($captchaId), $captchaInfo$this->expiration);
    }

    public function 
storeAudioData($captchaId$audioData)
    {
        
$info $this->get($captchaId);

        if (
$info && $this->delete($captchaId)) {
            
$info->captchaAudioData $audioData;

            return 
$this->store($captchaId$audioData);
        }

        return 
false;
    }

    public function 
get($captchaId$what null)
    {
        
$result $this->memcached->get($this->getKey($captchaId));

        if (!
$result) {
            if (
$this->memcached->getResultCode() != \Memcached::RES_NOTFOUND) {
                
trigger_error(
                    
sprintf("Securimage Memcached::get failed with error %d: %s",
                        
$this->memcached->getResultCode(),
                        
$this->memcached->getResultMessage()
                    ),
                    
E_USER_WARNING
                
);
            }
        } else {
            return 
$result;
        }

        return 
null;
    }

    public function 
delete($captchaId)
    {
        return 
$this->memcached->delete($this->getKey($captchaId));
    }

    protected function 
bootstrap()
    {
        
$this->memcached = new \Memcached($this->persistent);

        foreach(
$this->memcached_servers as $server) {
            
$parts explode(':'$server);
            
$host  $parts[0];
            
$port  = (isset($parts[1]) ? $parts[1] : 11211);

            
$this->memcached->addServer($host$port);
        }
    }

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