Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 915 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 liveuser 1
<?php
2
namespace Ratchet\Server;
3
use Ratchet\MessageComponentInterface;
4
use Ratchet\ConnectionInterface;
5
 
6
class IpBlackList implements MessageComponentInterface {
7
    /**
8
     * @var array
9
     */
10
    protected $_blacklist = array();
11
 
12
    /**
13
     * @var \Ratchet\MessageComponentInterface
14
     */
15
    protected $_decorating;
16
 
17
    /**
18
     * @param \Ratchet\MessageComponentInterface $component
19
     */
20
    public function __construct(MessageComponentInterface $component) {
21
        $this->_decorating = $component;
22
    }
23
 
24
    /**
25
     * Add an address to the blacklist that will not be allowed to connect to your application
26
     * @param  string $ip IP address to block from connecting to your application
27
     * @return IpBlackList
28
     */
29
    public function blockAddress($ip) {
30
        $this->_blacklist[$ip] = true;
31
 
32
        return $this;
33
    }
34
 
35
    /**
36
     * Unblock an address so they can access your application again
37
     * @param string $ip IP address to unblock from connecting to your application
38
     * @return IpBlackList
39
     */
40
    public function unblockAddress($ip) {
41
        if (isset($this->_blacklist[$this->filterAddress($ip)])) {
42
            unset($this->_blacklist[$this->filterAddress($ip)]);
43
        }
44
 
45
        return $this;
46
    }
47
 
48
    /**
49
     * @param  string $address
50
     * @return bool
51
     */
52
    public function isBlocked($address) {
53
        return (isset($this->_blacklist[$this->filterAddress($address)]));
54
    }
55
 
56
    /**
57
     * Get an array of all the addresses blocked
58
     * @return array
59
     */
60
    public function getBlockedAddresses() {
61
        return array_keys($this->_blacklist);
62
    }
63
 
64
    /**
65
     * @param  string $address
66
     * @return string
67
     */
68
    public function filterAddress($address) {
69
        if (strstr($address, ':') && substr_count($address, '.') == 3) {
70
            list($address, $port) = explode(':', $address);
71
        }
72
 
73
        return $address;
74
    }
75
 
76
    /**
77
     * {@inheritdoc}
78
     */
79
    function onOpen(ConnectionInterface $conn) {
80
        if ($this->isBlocked($conn->remoteAddress)) {
81
            return $conn->close();
82
        }
83
 
84
        return $this->_decorating->onOpen($conn);
85
    }
86
 
87
    /**
88
     * {@inheritdoc}
89
     */
90
    function onMessage(ConnectionInterface $from, $msg) {
91
        return $this->_decorating->onMessage($from, $msg);
92
    }
93
 
94
    /**
95
     * {@inheritdoc}
96
     */
97
    function onClose(ConnectionInterface $conn) {
98
        if (!$this->isBlocked($conn->remoteAddress)) {
99
            $this->_decorating->onClose($conn);
100
        }
101
    }
102
 
103
    /**
104
     * {@inheritdoc}
105
     */
106
    function onError(ConnectionInterface $conn, \Exception $e) {
107
        if (!$this->isBlocked($conn->remoteAddress)) {
108
            $this->_decorating->onError($conn, $e);
109
        }
110
    }
111
}