Commit | Line | Data |
---|---|---|
158bd3ca TD |
1 | <?php |
2 | namespace wcf\system\io; | |
3 | use wcf\system\exception\SystemException; | |
4 | ||
5 | /** | |
6 | * The RemoteFile class opens a connection to a remote host as a file. | |
7 | * | |
8 | * @author Marcel Werk | |
ca4ba303 | 9 | * @copyright 2001-2014 WoltLab GmbH |
158bd3ca TD |
10 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
11 | * @package com.woltlab.wcf | |
12 | * @subpackage system.io | |
9f959ced | 13 | * @category Community Framework |
158bd3ca TD |
14 | */ |
15 | class RemoteFile extends File { | |
16 | /** | |
17 | * host address | |
9f959ced | 18 | * @var string |
158bd3ca TD |
19 | */ |
20 | protected $host = ''; | |
21 | ||
22 | /** | |
23 | * port | |
9f959ced | 24 | * @var integer |
158bd3ca TD |
25 | */ |
26 | protected $port = 0; | |
27 | ||
28 | /** | |
29 | * error number | |
9f959ced | 30 | * @var integer |
158bd3ca TD |
31 | */ |
32 | protected $errorNumber = 0; | |
33 | ||
34 | /** | |
35 | * error description | |
9f959ced | 36 | * @var string |
158bd3ca TD |
37 | */ |
38 | protected $errorDesc = ''; | |
39 | ||
40 | /** | |
41 | * Opens a new connection to a remote host. | |
42 | * | |
39bea7dd MS |
43 | * @param string $host |
44 | * @param integer $port | |
45 | * @param integer $timeout | |
158bd3ca TD |
46 | * @param array $options |
47 | */ | |
f0a66666 | 48 | public function __construct($host, $port, $timeout = 30) { |
158bd3ca TD |
49 | $this->host = $host; |
50 | $this->port = $port; | |
0bc0d339 | 51 | |
96214982 | 52 | $this->resource = @fsockopen($host, $port, $this->errorNumber, $this->errorDesc, $timeout); |
158bd3ca | 53 | if ($this->resource === false) { |
70c4d9fa | 54 | throw new SystemException('Can not connect to ' . $host, 0, $this->errorDesc); |
158bd3ca TD |
55 | } |
56 | } | |
57 | ||
58 | /** | |
59 | * Returns the error number of the last error. | |
60 | * | |
9f959ced | 61 | * @return integer |
158bd3ca TD |
62 | */ |
63 | public function getErrorNumber() { | |
64 | return $this->errorNumber; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Returns the error description of the last error. | |
69 | * | |
70 | * @return string | |
71 | */ | |
72 | public function getErrorDesc() { | |
73 | return $this->errorDesc; | |
74 | } | |
4a92e525 TD |
75 | |
76 | /** | |
77 | * Switches TLS support for this connection. | |
78 | * Usually used in combination with 'STARTTLS' | |
79 | * | |
80 | * @param boolean $enable Whether TLS support should be enabled | |
81 | * @return boolean True on success, false otherwise | |
82 | */ | |
83 | public function setTLS($enable) { | |
84 | if (!$this->hasTLSSupport()) return false; | |
85 | ||
86 | return stream_socket_enable_crypto($this->resource, $enable, STREAM_CRYPTO_METHOD_TLS_CLIENT); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Returns whether TLS support is available. | |
91 | * | |
92 | * @return boolean | |
93 | */ | |
94 | public function hasTLSSupport() { | |
95 | return function_exists('stream_socket_enable_crypto'); | |
96 | } | |
dcb3a44c | 97 | } |