1 | <?php |
||
2 | /** |
||
3 | * sysPass |
||
4 | * |
||
5 | * @author nuxsmin |
||
6 | * @link https://syspass.org |
||
7 | * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org |
||
8 | * |
||
9 | * This file is part of sysPass. |
||
10 | * |
||
11 | * sysPass is free software: you can redistribute it and/or modify |
||
12 | * it under the terms of the GNU General Public License as published by |
||
13 | * the Free Software Foundation, either version 3 of the License, or |
||
14 | * (at your option) any later version. |
||
15 | * |
||
16 | * sysPass is distributed in the hope that it will be useful, |
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
19 | * GNU General Public License for more details. |
||
20 | * |
||
21 | * You should have received a copy of the GNU General Public License |
||
22 | * along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
||
23 | */ |
||
24 | |||
25 | namespace SP\Util; |
||
26 | |||
27 | use SP\Core\Exceptions\SPException; |
||
28 | |||
29 | /** |
||
30 | * Class Connection para crear conexiones TCP o UDP |
||
31 | * |
||
32 | * @package SP\Util |
||
33 | */ |
||
34 | final class Connection implements ConnectionInterface |
||
35 | { |
||
36 | /** |
||
37 | * @var resource |
||
38 | */ |
||
39 | protected $socket; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $host = ''; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $port = 0; |
||
50 | /** |
||
51 | * Código de error del socket |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $errorno = 0; |
||
56 | /** |
||
57 | * Mensaje de error del socket |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $errorstr = ''; |
||
62 | |||
63 | /** |
||
64 | * @param $host string El host a conectar |
||
65 | * @param $port string El puerto a conectar |
||
66 | */ |
||
67 | public function __construct($host, $port) |
||
68 | { |
||
69 | $this->host = gethostbyname($host); |
||
70 | $this->port = $port; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Obtener un socket |
||
75 | * |
||
76 | * @param $type int EL tipo de socket TCP/UDP |
||
77 | * |
||
78 | * @return resource |
||
79 | * @throws SPException |
||
80 | */ |
||
81 | public function getSocket($type) |
||
82 | { |
||
83 | switch ($type) { |
||
84 | case self::TYPE_TCP: |
||
85 | $this->socket = $this->getTCPSocket(); |
||
86 | break; |
||
87 | case self::TYPE_UDP: |
||
88 | $this->socket = $this->getUDPSocket(); |
||
89 | break; |
||
90 | default: |
||
91 | $this->socket = $this->getTCPSocket(); |
||
92 | break; |
||
93 | } |
||
94 | |||
95 | if ($this->socket === false) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
96 | throw new SPException($this->getSocketError(), SPException::WARNING); |
||
97 | } |
||
98 | |||
99 | stream_set_timeout($this->socket, self::SOCKET_TIMEOUT); |
||
100 | |||
101 | return $this->socket; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Obtener un socket del tipo TCP |
||
106 | * |
||
107 | * @return resource |
||
108 | */ |
||
109 | private function getTCPSocket() |
||
110 | { |
||
111 | return stream_socket_client('tcp://' . $this->host . ':' . $this->port, $this->errorno, $this->errorstr, self::SOCKET_TIMEOUT); |
||
0 ignored issues
–
show
The expression
return stream_socket_cli..., self::SOCKET_TIMEOUT) could also return false which is incompatible with the documented return type resource . Did you maybe forget to handle an error condition?
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled. ![]() |
|||
112 | // return @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Obtener un socket del tipo UDP |
||
117 | * |
||
118 | * @return resource |
||
119 | */ |
||
120 | private function getUDPSocket() |
||
121 | { |
||
122 | return stream_socket_client('udp://' . $this->host . ':' . $this->port, $this->errorno, $this->errorstr, self::SOCKET_TIMEOUT); |
||
0 ignored issues
–
show
The expression
return stream_socket_cli..., self::SOCKET_TIMEOUT) could also return false which is incompatible with the documented return type resource . Did you maybe forget to handle an error condition?
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled. ![]() |
|||
123 | // return @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Obtener el último error del socket |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getSocketError() |
||
132 | { |
||
133 | return sprintf('%s (%d)', $this->errorstr, $this->errorno); |
||
134 | // return socket_strerror(socket_last_error($this->_socket)); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Cerrar el socket |
||
139 | */ |
||
140 | public function closeSocket() |
||
141 | { |
||
142 | fclose($this->socket); |
||
143 | // @socket_close($this->_socket); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Enviar un mensaje al socket |
||
148 | * |
||
149 | * @param $message string El mensaje a enviar |
||
150 | * |
||
151 | * @return int|bool |
||
152 | * @throws SPException |
||
153 | */ |
||
154 | public function send($message) |
||
155 | { |
||
156 | if (!is_resource($this->socket)) { |
||
157 | throw new SPException(__u('Socket not initialized'), SPException::WARNING); |
||
158 | } |
||
159 | |||
160 | $nBytes = @fwrite($this->socket, $message); |
||
161 | // $nBytes = @socket_sendto($this->_socket, $message, strlen($message), 0, $this->_host, $this->port); |
||
162 | |||
163 | if ($nBytes === false) { |
||
164 | throw new SPException(__u('Error while sending the data'), SPException::WARNING, $this->getSocketError()); |
||
165 | } |
||
166 | |||
167 | return $nBytes; |
||
168 | } |
||
169 | } |