1: <?php
2: namespace Ratchet\Http;
3: use Ratchet\ConnectionInterface;
4: use Ratchet\MessageComponentInterface;
5: use Psr\Http\Message\RequestInterface;
6:
7: 8: 9: 10: 11:
12: class OriginCheck implements HttpServerInterface {
13: use CloseResponseTrait;
14:
15: 16: 17:
18: protected $_component;
19:
20: public $allowedOrigins = [];
21:
22: 23: 24: 25:
26: public function __construct(MessageComponentInterface $component, array $allowed = []) {
27: $this->_component = $component;
28: $this->allowedOrigins += $allowed;
29: }
30:
31: 32: 33:
34: public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
35: $header = (string)$request->getHeader('Origin')[0];
36: $origin = parse_url($header, PHP_URL_HOST) ?: $header;
37:
38: if (!in_array($origin, $this->allowedOrigins)) {
39: return $this->close($conn, 403);
40: }
41:
42: return $this->_component->onOpen($conn, $request);
43: }
44:
45: 46: 47:
48: function onMessage(ConnectionInterface $from, $msg) {
49: return $this->_component->onMessage($from, $msg);
50: }
51:
52: 53: 54:
55: function onClose(ConnectionInterface $conn) {
56: return $this->_component->onClose($conn);
57: }
58:
59: 60: 61:
62: function onError(ConnectionInterface $conn, \Exception $e) {
63: return $this->_component->onError($conn, $e);
64: }
65: }