1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WebSockets |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi |
7
|
|
|
* @license MIT License, see license.txt |
8
|
|
|
*/ |
9
|
|
|
namespace cs\modules\WebSockets; |
10
|
|
|
use |
11
|
|
|
cs\Language, |
12
|
|
|
Ratchet\ConnectionInterface, |
13
|
|
|
Ratchet\Http\HttpServerInterface, |
14
|
|
|
Guzzle\Http\Message\RequestInterface; |
15
|
|
|
|
16
|
|
|
class Connection_properties_injector implements HttpServerInterface { |
17
|
|
|
private $delegate; |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
|
public function __construct (HttpServerInterface $delegate) { |
22
|
|
|
$this->delegate = $delegate; |
23
|
|
|
} |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function onOpen (ConnectionInterface $conn, RequestInterface $request = null) { |
28
|
|
|
$L = Language::instance(); |
29
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
30
|
|
|
$ip = $this->ip( |
31
|
|
|
[ |
32
|
|
|
$conn->remoteAddress, |
33
|
|
|
$request->getHeader('X-Forwarded-For'), |
34
|
|
|
$request->getHeader('Client-IP'), |
35
|
|
|
$request->getHeader('X-Forwarded'), |
36
|
|
|
$request->getHeader('X-Cluster-Client-IP'), |
37
|
|
|
$request->getHeader('Forwarded-For'), |
38
|
|
|
$request->getHeader('Forwarded') |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
$conn->user_agent = $request->getHeader('User-Agent'); |
42
|
|
|
$conn->session_id = $request->getCookie('session'); |
43
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
44
|
|
|
$conn->remote_addr = $conn->remoteAddress; |
45
|
|
|
$conn->ip = $ip; |
46
|
|
|
$conn->language = $L->url_language($request->getPath()) ?: $L->clanguage; |
47
|
|
|
$this->delegate->onOpen($conn, $request); |
48
|
|
|
} |
49
|
|
|
/** |
50
|
|
|
* The best guessed IP of client (based on all known headers), `127.0.0.1` by default |
51
|
|
|
* |
52
|
|
|
* @param string[] $source_ips |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function ip ($source_ips) { |
57
|
|
|
foreach ($source_ips as $ip) { |
58
|
|
|
$ip = trim(explode(',', $ip)[0]); |
59
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
60
|
|
|
return $ip; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return '127.0.0.1'; |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function onMessage (ConnectionInterface $from, $msg) { |
69
|
|
|
$this->delegate->onMessage($from, $msg); |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function onClose (ConnectionInterface $conn) { |
75
|
|
|
$this->delegate->onClose($conn); |
76
|
|
|
} |
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function onError (ConnectionInterface $conn, \Exception $e) { |
81
|
|
|
$this->delegate->onError($conn, $e); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|