Passed
Push — master ( beaa4b...da3f2a )
by Shahrad
02:02
created
src/Contracts/MessageContract.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -14,12 +14,12 @@
 block discarded – undo
14 14
 interface MessageContract
15 15
 {
16 16
 
17
-	/**
18
-	 * @param ConnectionContract $recv
19
-	 * @param mixed $msg
20
-	 * @return mixed
21
-	 * @throws WebSocketException
22
-	 */
23
-	public function onMessage(ConnectionContract $recv, mixed $msg): mixed;
17
+    /**
18
+     * @param ConnectionContract $recv
19
+     * @param mixed $msg
20
+     * @return mixed
21
+     * @throws WebSocketException
22
+     */
23
+    public function onMessage(ConnectionContract $recv, mixed $msg): mixed;
24 24
 
25 25
 }
Please login to merge, or discard this patch.
src/Contracts/WebSocketContract.php 1 patch
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -14,24 +14,24 @@
 block discarded – undo
14 14
 interface WebSocketContract
15 15
 {
16 16
 
17
-	/**
18
-	 * @param ConnectionContract $conn
19
-	 * @return mixed
20
-	 */
21
-	public function onOpen(ConnectionContract $conn): mixed;
17
+    /**
18
+     * @param ConnectionContract $conn
19
+     * @return mixed
20
+     */
21
+    public function onOpen(ConnectionContract $conn): mixed;
22 22
 
23
-	/**
24
-	 * @param ConnectionContract $conn
25
-	 * @return mixed
26
-	 * @throws WebSocketException
27
-	 */
28
-	public function onClose(ConnectionContract $conn): mixed;
23
+    /**
24
+     * @param ConnectionContract $conn
25
+     * @return mixed
26
+     * @throws WebSocketException
27
+     */
28
+    public function onClose(ConnectionContract $conn): mixed;
29 29
 
30
-	/**
31
-	 * @param ConnectionContract $conn
32
-	 * @param WebSocketException $ex
33
-	 * @return mixed
34
-	 */
35
-	public function onError(ConnectionContract $conn, WebSocketException $ex): mixed;
30
+    /**
31
+     * @param ConnectionContract $conn
32
+     * @param WebSocketException $ex
33
+     * @return mixed
34
+     */
35
+    public function onError(ConnectionContract $conn, WebSocketException $ex): mixed;
36 36
 
37 37
 }
Please login to merge, or discard this patch.
src/Enums/CurlInfo.php 1 patch
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -12,215 +12,215 @@
 block discarded – undo
12 12
 class CurlInfo
13 13
 {
14 14
 
15
-	/**
16
-	 * @var string
17
-	 */
18
-	public string $URL;
19
-
20
-	/**
21
-	 * @var string
22
-	 */
23
-	public string $CONTENT_TYPE;
24
-
25
-	/**
26
-	 * @var int
27
-	 */
28
-	public int $HTTP_CODE;
29
-
30
-	/**
31
-	 * @var int
32
-	 */
33
-	public int $HEADER_SIZE;
34
-
35
-	/**
36
-	 * @var int
37
-	 */
38
-	public int $REQUEST_SIZE;
39
-
40
-	/**
41
-	 * @var int
42
-	 */
43
-	public int $FILETIME;
44
-
45
-	/**
46
-	 * @var int
47
-	 */
48
-	public int $SSL_VERIFY_RESULT;
49
-
50
-	/**
51
-	 * @var int
52
-	 */
53
-	public int $REDIRECT_COUNT;
54
-
55
-	/**
56
-	 * @var float
57
-	 */
58
-	public float $TOTAL_TIME;
59
-
60
-	/**
61
-	 * @var float
62
-	 */
63
-	public float $NAMELOOKUP_TIME;
64
-
65
-	/**
66
-	 * @var float
67
-	 */
68
-	public float $CONNECT_TIME;
69
-
70
-	/**
71
-	 * @var float
72
-	 */
73
-	public float $PRETRANSFER_TIME;
74
-
75
-	/**
76
-	 * @var int
77
-	 */
78
-	public int $SIZE_UPLOAD;
79
-
80
-	/**
81
-	 * @var int
82
-	 */
83
-	public int $SIZE_DOWNLOAD;
84
-
85
-	/**
86
-	 * @var int
87
-	 */
88
-	public int $SPEED_DOWNLOAD;
89
-
90
-	/**
91
-	 * @var int
92
-	 */
93
-	public int $SPEED_UPLOAD;
94
-
95
-	/**
96
-	 * @var int
97
-	 */
98
-	public int $DOWNLOAD_CONTENT_LENGTH;
99
-
100
-	/**
101
-	 * @var int
102
-	 */
103
-	public int $UPLOAD_CONTENT_LENGTH;
104
-
105
-	/**
106
-	 * @var float
107
-	 */
108
-	public float $STARTTRANSFER_TIME;
109
-
110
-	/**
111
-	 * @var int
112
-	 */
113
-	public int $REDIRECT_TIME;
114
-
115
-	/**
116
-	 * @var string
117
-	 */
118
-	public string $REDIRECT_URL;
119
-
120
-	/**
121
-	 * @var string
122
-	 */
123
-	public string $PRIMARY_IP;
124
-
125
-	/**
126
-	 * @var array
127
-	 */
128
-	public array $CERTINFO;
129
-
130
-	/**
131
-	 * @var int
132
-	 */
133
-	public int $PRIMARY_PORT;
134
-
135
-	/**
136
-	 * @var string
137
-	 */
138
-	public string $LOCAL_IP;
139
-
140
-	/**
141
-	 * @var int
142
-	 */
143
-	public int $LOCAL_PORT;
144
-
145
-	/**
146
-	 * @var int
147
-	 */
148
-	public int $HTTP_VERSION;
149
-
150
-	/**
151
-	 * @var int
152
-	 */
153
-	public int $PROTOCOL;
154
-
155
-	/**
156
-	 * @var int
157
-	 */
158
-	public int $SSL_VERIFYRESULT;
159
-
160
-	/**
161
-	 * @var string
162
-	 */
163
-	public string $SCHEME;
164
-
165
-	/**
166
-	 * @var int
167
-	 */
168
-	public int $APPCONNECT_TIME_US;
169
-
170
-	/**
171
-	 * @var int
172
-	 */
173
-	public int $CONNECT_TIME_US;
174
-
175
-	/**
176
-	 * @var int
177
-	 */
178
-	public int $NAMELOOKUP_TIME_US;
179
-
180
-	/**
181
-	 * @var int
182
-	 */
183
-	public int $PRETRANSFER_TIME_US;
184
-
185
-	/**
186
-	 * @var int
187
-	 */
188
-	public int $REDIRECT_TIME_US;
189
-
190
-	/**
191
-	 * @var int
192
-	 */
193
-	public int $STARTTRANSFER_TIME_US;
194
-
195
-	/**
196
-	 * @var int
197
-	 */
198
-	public int $TOTAL_TIME_US;
199
-
200
-	/**
201
-	 * The Constructor
202
-	 *
203
-	 * @param array $curlInfo
204
-	 * @return void
205
-	 */
206
-	public function __construct(array $curlInfo = [])
207
-	{
208
-		$this->set($curlInfo);
209
-	}
210
-
211
-	/**
212
-	 * Set CurlInfo
213
-	 *
214
-	 * @param array $curlInfo
215
-	 * @return void
216
-	 */
217
-	public function set(array $curlInfo): void
218
-	{
219
-		foreach ($curlInfo as $key => $value) {
220
-			if (property_exists($this, strtoupper($key))) {
221
-				$this->{strtoupper($key)} = $value;
222
-			}
223
-		}
224
-	}
15
+    /**
16
+     * @var string
17
+     */
18
+    public string $URL;
19
+
20
+    /**
21
+     * @var string
22
+     */
23
+    public string $CONTENT_TYPE;
24
+
25
+    /**
26
+     * @var int
27
+     */
28
+    public int $HTTP_CODE;
29
+
30
+    /**
31
+     * @var int
32
+     */
33
+    public int $HEADER_SIZE;
34
+
35
+    /**
36
+     * @var int
37
+     */
38
+    public int $REQUEST_SIZE;
39
+
40
+    /**
41
+     * @var int
42
+     */
43
+    public int $FILETIME;
44
+
45
+    /**
46
+     * @var int
47
+     */
48
+    public int $SSL_VERIFY_RESULT;
49
+
50
+    /**
51
+     * @var int
52
+     */
53
+    public int $REDIRECT_COUNT;
54
+
55
+    /**
56
+     * @var float
57
+     */
58
+    public float $TOTAL_TIME;
59
+
60
+    /**
61
+     * @var float
62
+     */
63
+    public float $NAMELOOKUP_TIME;
64
+
65
+    /**
66
+     * @var float
67
+     */
68
+    public float $CONNECT_TIME;
69
+
70
+    /**
71
+     * @var float
72
+     */
73
+    public float $PRETRANSFER_TIME;
74
+
75
+    /**
76
+     * @var int
77
+     */
78
+    public int $SIZE_UPLOAD;
79
+
80
+    /**
81
+     * @var int
82
+     */
83
+    public int $SIZE_DOWNLOAD;
84
+
85
+    /**
86
+     * @var int
87
+     */
88
+    public int $SPEED_DOWNLOAD;
89
+
90
+    /**
91
+     * @var int
92
+     */
93
+    public int $SPEED_UPLOAD;
94
+
95
+    /**
96
+     * @var int
97
+     */
98
+    public int $DOWNLOAD_CONTENT_LENGTH;
99
+
100
+    /**
101
+     * @var int
102
+     */
103
+    public int $UPLOAD_CONTENT_LENGTH;
104
+
105
+    /**
106
+     * @var float
107
+     */
108
+    public float $STARTTRANSFER_TIME;
109
+
110
+    /**
111
+     * @var int
112
+     */
113
+    public int $REDIRECT_TIME;
114
+
115
+    /**
116
+     * @var string
117
+     */
118
+    public string $REDIRECT_URL;
119
+
120
+    /**
121
+     * @var string
122
+     */
123
+    public string $PRIMARY_IP;
124
+
125
+    /**
126
+     * @var array
127
+     */
128
+    public array $CERTINFO;
129
+
130
+    /**
131
+     * @var int
132
+     */
133
+    public int $PRIMARY_PORT;
134
+
135
+    /**
136
+     * @var string
137
+     */
138
+    public string $LOCAL_IP;
139
+
140
+    /**
141
+     * @var int
142
+     */
143
+    public int $LOCAL_PORT;
144
+
145
+    /**
146
+     * @var int
147
+     */
148
+    public int $HTTP_VERSION;
149
+
150
+    /**
151
+     * @var int
152
+     */
153
+    public int $PROTOCOL;
154
+
155
+    /**
156
+     * @var int
157
+     */
158
+    public int $SSL_VERIFYRESULT;
159
+
160
+    /**
161
+     * @var string
162
+     */
163
+    public string $SCHEME;
164
+
165
+    /**
166
+     * @var int
167
+     */
168
+    public int $APPCONNECT_TIME_US;
169
+
170
+    /**
171
+     * @var int
172
+     */
173
+    public int $CONNECT_TIME_US;
174
+
175
+    /**
176
+     * @var int
177
+     */
178
+    public int $NAMELOOKUP_TIME_US;
179
+
180
+    /**
181
+     * @var int
182
+     */
183
+    public int $PRETRANSFER_TIME_US;
184
+
185
+    /**
186
+     * @var int
187
+     */
188
+    public int $REDIRECT_TIME_US;
189
+
190
+    /**
191
+     * @var int
192
+     */
193
+    public int $STARTTRANSFER_TIME_US;
194
+
195
+    /**
196
+     * @var int
197
+     */
198
+    public int $TOTAL_TIME_US;
199
+
200
+    /**
201
+     * The Constructor
202
+     *
203
+     * @param array $curlInfo
204
+     * @return void
205
+     */
206
+    public function __construct(array $curlInfo = [])
207
+    {
208
+        $this->set($curlInfo);
209
+    }
210
+
211
+    /**
212
+     * Set CurlInfo
213
+     *
214
+     * @param array $curlInfo
215
+     * @return void
216
+     */
217
+    public function set(array $curlInfo): void
218
+    {
219
+        foreach ($curlInfo as $key => $value) {
220
+            if (property_exists($this, strtoupper($key))) {
221
+                $this->{strtoupper($key)} = $value;
222
+            }
223
+        }
224
+    }
225 225
 
226 226
 }
227 227
\ No newline at end of file
Please login to merge, or discard this patch.
src/WebSocket.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -16,17 +16,17 @@
 block discarded – undo
16 16
 class WebSocket extends WscMain
17 17
 {
18 18
 
19
-	/**
20
-	 * Sets parameters for Web Socket Client intercommunication
21
-	 *
22
-	 * @param string $url string representation of a socket utf, ex.: tcp://www.example.com:8000 or udp://example.com:13
23
-	 * @param WSConfig $config Client configuration settings e.g.: connection - timeout, ssl options, fragment message size to send etc.
24
-	 * @throws Exception
25
-	 */
26
-	public function __construct(string $url, WSConfig $config)
27
-	{
28
-		$this->socketUrl = $url;
29
-		$this->connect($config);
30
-	}
19
+    /**
20
+     * Sets parameters for Web Socket Client intercommunication
21
+     *
22
+     * @param string $url string representation of a socket utf, ex.: tcp://www.example.com:8000 or udp://example.com:13
23
+     * @param WSConfig $config Client configuration settings e.g.: connection - timeout, ssl options, fragment message size to send etc.
24
+     * @throws Exception
25
+     */
26
+    public function __construct(string $url, WSConfig $config)
27
+    {
28
+        $this->socketUrl = $url;
29
+        $this->connect($config);
30
+    }
31 31
 
32 32
 }
Please login to merge, or discard this patch.
src/Middleware.php 3 patches
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -15,127 +15,127 @@
 block discarded – undo
15 15
 class Middleware
16 16
 {
17 17
 
18
-	/**
19
-	 * Create curl handler.
20
-	 *
21
-	 * @param ?string $method
22
-	 * @param string $uri
23
-	 * @param array|HttpOptions $options
24
-	 *
25
-	 * @return \CurlHandle|false
26
-	 */
27
-	public static function create_curl_handler(?string $method, string $uri, array|HttpOptions $options = []): \CurlHandle|false
28
-	{
29
-		$handler = curl_init();
30
-		if (is_resource($handler) || !$handler) {
31
-			return false;
32
-		}
33
-
34
-		if (gettype($options) === 'array') {
35
-			$options = new HttpOptions($options);
36
-		}
37
-
38
-		if (count($options->getQuery()) > 0) {
39
-			if (!str_contains($uri, '?')) {
40
-				$uri .= '?';
41
-			}
42
-			$uri .= $options->getQueryString();
43
-		}
44
-
45
-		curl_setopt($handler, CURLOPT_URL, $uri);
46
-
47
-		self::set_curl_options($method, $handler, $options);
48
-
49
-		return $handler;
50
-	}
51
-
52
-	/**
53
-	 * Setup curl options based on the given method and our options.
54
-	 *
55
-	 * @param \CurlHandle $cHandler
56
-	 * @param ?string $method
57
-	 * @param HttpOptions $options
58
-	 *
59
-	 * @return void
60
-	 */
61
-	public static function set_curl_options(?string $method, \CurlHandle $cHandler, HttpOptions $options): void
62
-	{
63
-		curl_setopt($cHandler, CURLOPT_HEADER, true);
64
-		curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'GET');
65
-
66
-		# Fetch the header
67
-		$fetchedHeaders = [];
68
-		foreach ($options->getHeaders() as $header => $value) {
69
-			$fetchedHeaders[] = $header . ': ' . $value;
70
-		}
71
-
72
-		# Set headers
73
-		curl_setopt($cHandler, CURLOPT_HTTPHEADER, $fetchedHeaders ?? []);
74
-
75
-		# Add body if we have one.
76
-		if ($options->getBody()) {
77
-			curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'POST');
78
-			curl_setopt($cHandler, CURLOPT_POSTFIELDS, $options->getBody());
79
-			curl_setopt($cHandler, CURLOPT_POST, true);
80
-		}
81
-
82
-		# Check for a proxy
83
-		if ($options->getProxy() != null) {
84
-			curl_setopt($cHandler, CURLOPT_PROXY, $options->getProxy()->getHost());
85
-			curl_setopt($cHandler, CURLOPT_PROXYUSERPWD, $options->getProxy()->getAuth());
86
-			if ($options->getProxy()->type !== null) {
87
-				curl_setopt($cHandler, CURLOPT_PROXYTYPE, $options->getProxy()->type);
88
-			}
89
-		}
90
-
91
-		curl_setopt($cHandler, CURLOPT_RETURNTRANSFER, true);
92
-		curl_setopt($cHandler, CURLOPT_FOLLOWLOCATION, true);
93
-
94
-		# Add and override the custom curl options.
95
-		foreach ($options->getCurlOptions() as $option => $value) {
96
-			curl_setopt($cHandler, $option, $value);
97
-		}
98
-
99
-		# if we have a timeout, set it.
100
-		curl_setopt($cHandler, CURLOPT_TIMEOUT, $options->getTimeout());
101
-
102
-		# If self-signed certs are allowed, set it.
103
-		if ((bool)getenv('HAS_SELF_SIGNED_CERT') === true) {
104
-			curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false);
105
-			curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false);
106
-		}
107
-
108
-		(new Middleware())->handle_media($cHandler, $options);
109
-	}
110
-
111
-	/**
112
-	 * Handle the media
113
-	 *
114
-	 * @param \CurlHandle $handler
115
-	 * @param HttpOptions $options
116
-	 * @return void
117
-	 */
118
-	private function handle_media(\CurlHandle $handler, HttpOptions $options): void
119
-	{
120
-		if (count($options->getMultipart()) > 0) {
121
-			curl_setopt($handler, CURLOPT_POST, true);
122
-			curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'POST');
123
-
124
-			$form_data = new FormData();
125
-			foreach ($options->getMultipart() as $key => $value) {
126
-				$form_data->addFile($key, $value);
127
-			}
128
-
129
-			$headers = [];
130
-			foreach ($options->getHeaders() as $header => $value) {
131
-				if (Toolkit::insensitiveString($header, 'content-type')) continue;
132
-				$headers[] = $header . ': ' . $value;
133
-			}
134
-			$headers[] = 'Content-Type: multipart/form-data';
135
-
136
-			curl_setopt($handler, CURLOPT_HTTPHEADER, $headers);
137
-			curl_setopt($handler, CURLOPT_POSTFIELDS, $form_data->getFiles());
138
-		}
139
-	}
18
+    /**
19
+     * Create curl handler.
20
+     *
21
+     * @param ?string $method
22
+     * @param string $uri
23
+     * @param array|HttpOptions $options
24
+     *
25
+     * @return \CurlHandle|false
26
+     */
27
+    public static function create_curl_handler(?string $method, string $uri, array|HttpOptions $options = []): \CurlHandle|false
28
+    {
29
+        $handler = curl_init();
30
+        if (is_resource($handler) || !$handler) {
31
+            return false;
32
+        }
33
+
34
+        if (gettype($options) === 'array') {
35
+            $options = new HttpOptions($options);
36
+        }
37
+
38
+        if (count($options->getQuery()) > 0) {
39
+            if (!str_contains($uri, '?')) {
40
+                $uri .= '?';
41
+            }
42
+            $uri .= $options->getQueryString();
43
+        }
44
+
45
+        curl_setopt($handler, CURLOPT_URL, $uri);
46
+
47
+        self::set_curl_options($method, $handler, $options);
48
+
49
+        return $handler;
50
+    }
51
+
52
+    /**
53
+     * Setup curl options based on the given method and our options.
54
+     *
55
+     * @param \CurlHandle $cHandler
56
+     * @param ?string $method
57
+     * @param HttpOptions $options
58
+     *
59
+     * @return void
60
+     */
61
+    public static function set_curl_options(?string $method, \CurlHandle $cHandler, HttpOptions $options): void
62
+    {
63
+        curl_setopt($cHandler, CURLOPT_HEADER, true);
64
+        curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'GET');
65
+
66
+        # Fetch the header
67
+        $fetchedHeaders = [];
68
+        foreach ($options->getHeaders() as $header => $value) {
69
+            $fetchedHeaders[] = $header . ': ' . $value;
70
+        }
71
+
72
+        # Set headers
73
+        curl_setopt($cHandler, CURLOPT_HTTPHEADER, $fetchedHeaders ?? []);
74
+
75
+        # Add body if we have one.
76
+        if ($options->getBody()) {
77
+            curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'POST');
78
+            curl_setopt($cHandler, CURLOPT_POSTFIELDS, $options->getBody());
79
+            curl_setopt($cHandler, CURLOPT_POST, true);
80
+        }
81
+
82
+        # Check for a proxy
83
+        if ($options->getProxy() != null) {
84
+            curl_setopt($cHandler, CURLOPT_PROXY, $options->getProxy()->getHost());
85
+            curl_setopt($cHandler, CURLOPT_PROXYUSERPWD, $options->getProxy()->getAuth());
86
+            if ($options->getProxy()->type !== null) {
87
+                curl_setopt($cHandler, CURLOPT_PROXYTYPE, $options->getProxy()->type);
88
+            }
89
+        }
90
+
91
+        curl_setopt($cHandler, CURLOPT_RETURNTRANSFER, true);
92
+        curl_setopt($cHandler, CURLOPT_FOLLOWLOCATION, true);
93
+
94
+        # Add and override the custom curl options.
95
+        foreach ($options->getCurlOptions() as $option => $value) {
96
+            curl_setopt($cHandler, $option, $value);
97
+        }
98
+
99
+        # if we have a timeout, set it.
100
+        curl_setopt($cHandler, CURLOPT_TIMEOUT, $options->getTimeout());
101
+
102
+        # If self-signed certs are allowed, set it.
103
+        if ((bool)getenv('HAS_SELF_SIGNED_CERT') === true) {
104
+            curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false);
105
+            curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false);
106
+        }
107
+
108
+        (new Middleware())->handle_media($cHandler, $options);
109
+    }
110
+
111
+    /**
112
+     * Handle the media
113
+     *
114
+     * @param \CurlHandle $handler
115
+     * @param HttpOptions $options
116
+     * @return void
117
+     */
118
+    private function handle_media(\CurlHandle $handler, HttpOptions $options): void
119
+    {
120
+        if (count($options->getMultipart()) > 0) {
121
+            curl_setopt($handler, CURLOPT_POST, true);
122
+            curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'POST');
123
+
124
+            $form_data = new FormData();
125
+            foreach ($options->getMultipart() as $key => $value) {
126
+                $form_data->addFile($key, $value);
127
+            }
128
+
129
+            $headers = [];
130
+            foreach ($options->getHeaders() as $header => $value) {
131
+                if (Toolkit::insensitiveString($header, 'content-type')) continue;
132
+                $headers[] = $header . ': ' . $value;
133
+            }
134
+            $headers[] = 'Content-Type: multipart/form-data';
135
+
136
+            curl_setopt($handler, CURLOPT_HTTPHEADER, $headers);
137
+            curl_setopt($handler, CURLOPT_POSTFIELDS, $form_data->getFiles());
138
+        }
139
+    }
140 140
 
141 141
 }
142 142
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -100,7 +100,7 @@
 block discarded – undo
100 100
 		curl_setopt($cHandler, CURLOPT_TIMEOUT, $options->getTimeout());
101 101
 
102 102
 		# If self-signed certs are allowed, set it.
103
-		if ((bool)getenv('HAS_SELF_SIGNED_CERT') === true) {
103
+		if ((bool) getenv('HAS_SELF_SIGNED_CERT') === true) {
104 104
 			curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false);
105 105
 			curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false);
106 106
 		}
Please login to merge, or discard this patch.
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -128,7 +128,9 @@
 block discarded – undo
128 128
 
129 129
 			$headers = [];
130 130
 			foreach ($options->getHeaders() as $header => $value) {
131
-				if (Toolkit::insensitiveString($header, 'content-type')) continue;
131
+				if (Toolkit::insensitiveString($header, 'content-type')) {
132
+				    continue;
133
+				}
132 134
 				$headers[] = $header . ': ' . $value;
133 135
 			}
134 136
 			$headers[] = 'Content-Type: multipart/form-data';
Please login to merge, or discard this patch.
src/Utils/Toolkit.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -87,7 +87,7 @@
 block discarded – undo
87 87
      */
88 88
     public static function insensitiveString(string $string, string $value): bool
89 89
     {
90
-        return (bool)preg_match_all('/' . $value . '/i', $string);
90
+        return (bool) preg_match_all('/' . $value . '/i', $string);
91 91
     }
92 92
 
93 93
 }
94 94
\ No newline at end of file
Please login to merge, or discard this patch.
src/Utils/WSConfig.php 1 patch
Indentation   +308 added lines, -308 removed lines patch added patch discarded remove patch
@@ -14,313 +14,313 @@
 block discarded – undo
14 14
 class WSConfig
15 15
 {
16 16
 
17
-	/**
18
-	 * @var string
19
-	 */
20
-	private string $scheme;
21
-
22
-	/**
23
-	 * @var string
24
-	 */
25
-	private string $host;
26
-
27
-	/**
28
-	 * @var string
29
-	 */
30
-	private string $user;
31
-
32
-	/**
33
-	 * @var string
34
-	 */
35
-	private string $password;
36
-
37
-	/**
38
-	 * @var string
39
-	 */
40
-	private string $port;
41
-
42
-	/**
43
-	 * @var int
44
-	 */
45
-	private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT;
46
-
47
-	/**
48
-	 * @var array
49
-	 */
50
-	private array $headers = [];
51
-
52
-	/**
53
-	 * @var int
54
-	 */
55
-	private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE;
56
-
57
-	/**
58
-	 * @var null|resource
59
-	 */
60
-	private $context;
61
-
62
-	/**
63
-	 * @var bool
64
-	 */
65
-	private bool $hasProxy = false;
66
-
67
-	/**
68
-	 * @var string
69
-	 */
70
-	private string $proxyIp;
71
-
72
-	/**
73
-	 * @var string
74
-	 */
75
-	private string $proxyPort;
76
-
77
-	/**
78
-	 * @var string|null
79
-	 */
80
-	private ?string $proxyAuth;
81
-
82
-	/**
83
-	 * @var array
84
-	 */
85
-	private array $contextOptions = [];
86
-
87
-	/**
88
-	 * @return int
89
-	 */
90
-	public function getTimeout(): int
91
-	{
92
-		return $this->timeout;
93
-	}
94
-
95
-	/**
96
-	 * @param int $timeout
97
-	 * @return WSConfig
98
-	 */
99
-	public function setTimeout(int $timeout): WSConfig
100
-	{
101
-		$this->timeout = $timeout;
102
-		return $this;
103
-	}
104
-
105
-	/**
106
-	 * @return array
107
-	 */
108
-	public function getHeaders(): array
109
-	{
110
-		return $this->headers;
111
-	}
112
-
113
-	/**
114
-	 * @param array $headers
115
-	 * @return WSConfig
116
-	 */
117
-	public function setHeaders(array $headers): WSConfig
118
-	{
119
-		$this->headers = $headers;
120
-		return $this;
121
-	}
122
-
123
-	/**
124
-	 * @return int
125
-	 */
126
-	public function getFragmentSize(): int
127
-	{
128
-		return $this->fragmentSize;
129
-	}
130
-
131
-	/**
132
-	 * @param int $fragmentSize
133
-	 * @return WSConfig
134
-	 */
135
-	public function setFragmentSize(int $fragmentSize): WSConfig
136
-	{
137
-		$this->fragmentSize = $fragmentSize;
138
-		return $this;
139
-	}
140
-
141
-	/**
142
-	 * @return mixed
143
-	 */
144
-	public function getContext(): mixed
145
-	{
146
-		return $this->context;
147
-	}
148
-
149
-	/**
150
-	 * @param mixed $context
151
-	 * @return WSConfig
152
-	 */
153
-	public function setContext(mixed $context): WSConfig
154
-	{
155
-		$this->context = $context;
156
-		return $this;
157
-	}
158
-
159
-	/**
160
-	 * @return mixed
161
-	 */
162
-	public function getScheme(): string
163
-	{
164
-		return $this->scheme;
165
-	}
166
-
167
-	/**
168
-	 * @param string $scheme
169
-	 * @return WSConfig
170
-	 */
171
-	public function setScheme(string $scheme): WSConfig
172
-	{
173
-		$this->scheme = $scheme;
174
-		return $this;
175
-	}
176
-
177
-	/**
178
-	 * @return string
179
-	 */
180
-	public function getHost(): string
181
-	{
182
-		return $this->host;
183
-	}
184
-
185
-	/**
186
-	 * @param string $host
187
-	 * @return WSConfig
188
-	 */
189
-	public function setHost(string $host): WSConfig
190
-	{
191
-		$this->host = $host;
192
-		return $this;
193
-	}
194
-
195
-	/**
196
-	 * @return string
197
-	 */
198
-	public function getUser(): string
199
-	{
200
-		return $this->user;
201
-	}
202
-
203
-	/**
204
-	 * @param array $urlParts
205
-	 * @return WSConfig
206
-	 */
207
-	public function setUser(array $urlParts): WSConfig
208
-	{
209
-		$this->user = $urlParts['user'] ?? '';
210
-		return $this;
211
-	}
212
-
213
-	/**
214
-	 * @return string
215
-	 */
216
-	public function getPassword(): string
217
-	{
218
-		return $this->password;
219
-	}
220
-
221
-	/**
222
-	 * @param array $urlParts
223
-	 * @return WSConfig
224
-	 */
225
-	public function setPassword(array $urlParts): WSConfig
226
-	{
227
-		$this->password = $urlParts['pass'] ?? '';
228
-		return $this;
229
-	}
230
-
231
-	/**
232
-	 * @return string
233
-	 */
234
-	public function getPort(): string
235
-	{
236
-		return $this->port;
237
-	}
238
-
239
-	/**
240
-	 * @param array $urlParts
241
-	 * @return WSConfig
242
-	 */
243
-	public function setPort(array $urlParts): WSConfig
244
-	{
245
-		$this->port = $urlParts['port'] ?? ($this->scheme === 'wss' ? '443' : '80');
246
-		return $this;
247
-	}
248
-
249
-	/**
250
-	 * @return array
251
-	 */
252
-	public function getContextOptions(): array
253
-	{
254
-		return $this->contextOptions;
255
-	}
256
-
257
-	/**
258
-	 * @param array $contextOptions
259
-	 * @return WSConfig
260
-	 */
261
-	public function setContextOptions(array $contextOptions): WSConfig
262
-	{
263
-		$this->contextOptions = $contextOptions;
264
-		return $this;
265
-	}
266
-
267
-	/**
268
-	 * @param string $ip
269
-	 * @param string $port
270
-	 * @return WSConfig
271
-	 */
272
-	public function setProxy(string $ip, string $port): WSConfig
273
-	{
274
-		$this->hasProxy = true;
275
-		$this->proxyIp = $ip;
276
-		$this->proxyPort = $port;
277
-
278
-		return $this;
279
-	}
280
-
281
-	/**
282
-	 * Sets auth for proxy
283
-	 *
284
-	 * @param string $userName
285
-	 * @param string $password
286
-	 * @return WSConfig
287
-	 */
288
-	public function setProxyAuth(string $userName, string $password): WSConfig
289
-	{
290
-		$this->proxyAuth = (empty($userName) === false && empty($password) === false) ? base64_encode($userName . ':' . $password) : null;
291
-		return $this;
292
-	}
293
-
294
-	/**
295
-	 * @return bool
296
-	 */
297
-	public function hasProxy(): bool
298
-	{
299
-		return $this->hasProxy;
300
-	}
301
-
302
-	/**
303
-	 * @return string|null
304
-	 */
305
-	public function getProxyIp(): ?string
306
-	{
307
-		return $this->proxyIp;
308
-	}
309
-
310
-	/**
311
-	 * @return string|null
312
-	 */
313
-	public function getProxyPort(): ?string
314
-	{
315
-		return $this->proxyPort;
316
-	}
317
-
318
-	/**
319
-	 * @return string|null
320
-	 */
321
-	public function getProxyAuth(): ?string
322
-	{
323
-		return $this->proxyAuth;
324
-	}
17
+    /**
18
+     * @var string
19
+     */
20
+    private string $scheme;
21
+
22
+    /**
23
+     * @var string
24
+     */
25
+    private string $host;
26
+
27
+    /**
28
+     * @var string
29
+     */
30
+    private string $user;
31
+
32
+    /**
33
+     * @var string
34
+     */
35
+    private string $password;
36
+
37
+    /**
38
+     * @var string
39
+     */
40
+    private string $port;
41
+
42
+    /**
43
+     * @var int
44
+     */
45
+    private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT;
46
+
47
+    /**
48
+     * @var array
49
+     */
50
+    private array $headers = [];
51
+
52
+    /**
53
+     * @var int
54
+     */
55
+    private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE;
56
+
57
+    /**
58
+     * @var null|resource
59
+     */
60
+    private $context;
61
+
62
+    /**
63
+     * @var bool
64
+     */
65
+    private bool $hasProxy = false;
66
+
67
+    /**
68
+     * @var string
69
+     */
70
+    private string $proxyIp;
71
+
72
+    /**
73
+     * @var string
74
+     */
75
+    private string $proxyPort;
76
+
77
+    /**
78
+     * @var string|null
79
+     */
80
+    private ?string $proxyAuth;
81
+
82
+    /**
83
+     * @var array
84
+     */
85
+    private array $contextOptions = [];
86
+
87
+    /**
88
+     * @return int
89
+     */
90
+    public function getTimeout(): int
91
+    {
92
+        return $this->timeout;
93
+    }
94
+
95
+    /**
96
+     * @param int $timeout
97
+     * @return WSConfig
98
+     */
99
+    public function setTimeout(int $timeout): WSConfig
100
+    {
101
+        $this->timeout = $timeout;
102
+        return $this;
103
+    }
104
+
105
+    /**
106
+     * @return array
107
+     */
108
+    public function getHeaders(): array
109
+    {
110
+        return $this->headers;
111
+    }
112
+
113
+    /**
114
+     * @param array $headers
115
+     * @return WSConfig
116
+     */
117
+    public function setHeaders(array $headers): WSConfig
118
+    {
119
+        $this->headers = $headers;
120
+        return $this;
121
+    }
122
+
123
+    /**
124
+     * @return int
125
+     */
126
+    public function getFragmentSize(): int
127
+    {
128
+        return $this->fragmentSize;
129
+    }
130
+
131
+    /**
132
+     * @param int $fragmentSize
133
+     * @return WSConfig
134
+     */
135
+    public function setFragmentSize(int $fragmentSize): WSConfig
136
+    {
137
+        $this->fragmentSize = $fragmentSize;
138
+        return $this;
139
+    }
140
+
141
+    /**
142
+     * @return mixed
143
+     */
144
+    public function getContext(): mixed
145
+    {
146
+        return $this->context;
147
+    }
148
+
149
+    /**
150
+     * @param mixed $context
151
+     * @return WSConfig
152
+     */
153
+    public function setContext(mixed $context): WSConfig
154
+    {
155
+        $this->context = $context;
156
+        return $this;
157
+    }
158
+
159
+    /**
160
+     * @return mixed
161
+     */
162
+    public function getScheme(): string
163
+    {
164
+        return $this->scheme;
165
+    }
166
+
167
+    /**
168
+     * @param string $scheme
169
+     * @return WSConfig
170
+     */
171
+    public function setScheme(string $scheme): WSConfig
172
+    {
173
+        $this->scheme = $scheme;
174
+        return $this;
175
+    }
176
+
177
+    /**
178
+     * @return string
179
+     */
180
+    public function getHost(): string
181
+    {
182
+        return $this->host;
183
+    }
184
+
185
+    /**
186
+     * @param string $host
187
+     * @return WSConfig
188
+     */
189
+    public function setHost(string $host): WSConfig
190
+    {
191
+        $this->host = $host;
192
+        return $this;
193
+    }
194
+
195
+    /**
196
+     * @return string
197
+     */
198
+    public function getUser(): string
199
+    {
200
+        return $this->user;
201
+    }
202
+
203
+    /**
204
+     * @param array $urlParts
205
+     * @return WSConfig
206
+     */
207
+    public function setUser(array $urlParts): WSConfig
208
+    {
209
+        $this->user = $urlParts['user'] ?? '';
210
+        return $this;
211
+    }
212
+
213
+    /**
214
+     * @return string
215
+     */
216
+    public function getPassword(): string
217
+    {
218
+        return $this->password;
219
+    }
220
+
221
+    /**
222
+     * @param array $urlParts
223
+     * @return WSConfig
224
+     */
225
+    public function setPassword(array $urlParts): WSConfig
226
+    {
227
+        $this->password = $urlParts['pass'] ?? '';
228
+        return $this;
229
+    }
230
+
231
+    /**
232
+     * @return string
233
+     */
234
+    public function getPort(): string
235
+    {
236
+        return $this->port;
237
+    }
238
+
239
+    /**
240
+     * @param array $urlParts
241
+     * @return WSConfig
242
+     */
243
+    public function setPort(array $urlParts): WSConfig
244
+    {
245
+        $this->port = $urlParts['port'] ?? ($this->scheme === 'wss' ? '443' : '80');
246
+        return $this;
247
+    }
248
+
249
+    /**
250
+     * @return array
251
+     */
252
+    public function getContextOptions(): array
253
+    {
254
+        return $this->contextOptions;
255
+    }
256
+
257
+    /**
258
+     * @param array $contextOptions
259
+     * @return WSConfig
260
+     */
261
+    public function setContextOptions(array $contextOptions): WSConfig
262
+    {
263
+        $this->contextOptions = $contextOptions;
264
+        return $this;
265
+    }
266
+
267
+    /**
268
+     * @param string $ip
269
+     * @param string $port
270
+     * @return WSConfig
271
+     */
272
+    public function setProxy(string $ip, string $port): WSConfig
273
+    {
274
+        $this->hasProxy = true;
275
+        $this->proxyIp = $ip;
276
+        $this->proxyPort = $port;
277
+
278
+        return $this;
279
+    }
280
+
281
+    /**
282
+     * Sets auth for proxy
283
+     *
284
+     * @param string $userName
285
+     * @param string $password
286
+     * @return WSConfig
287
+     */
288
+    public function setProxyAuth(string $userName, string $password): WSConfig
289
+    {
290
+        $this->proxyAuth = (empty($userName) === false && empty($password) === false) ? base64_encode($userName . ':' . $password) : null;
291
+        return $this;
292
+    }
293
+
294
+    /**
295
+     * @return bool
296
+     */
297
+    public function hasProxy(): bool
298
+    {
299
+        return $this->hasProxy;
300
+    }
301
+
302
+    /**
303
+     * @return string|null
304
+     */
305
+    public function getProxyIp(): ?string
306
+    {
307
+        return $this->proxyIp;
308
+    }
309
+
310
+    /**
311
+     * @return string|null
312
+     */
313
+    public function getProxyPort(): ?string
314
+    {
315
+        return $this->proxyPort;
316
+    }
317
+
318
+    /**
319
+     * @return string|null
320
+     */
321
+    public function getProxyAuth(): ?string
322
+    {
323
+        return $this->proxyAuth;
324
+    }
325 325
 
326 326
 }
Please login to merge, or discard this patch.
src/Utils/WscMain.php 2 patches
Indentation   +416 added lines, -416 removed lines patch added patch discarded remove patch
@@ -19,421 +19,421 @@
 block discarded – undo
19 19
 class WscMain implements WscCommonsContract
20 20
 {
21 21
 
22
-	use WSClientTrait;
23
-
24
-	/**
25
-	 * @var resource|bool
26
-	 */
27
-	private $socket;
28
-
29
-	/**
30
-	 * @var bool
31
-	 */
32
-	private bool $isConnected = false;
33
-
34
-	/**
35
-	 * @var bool
36
-	 */
37
-	private bool $isClosing = false;
38
-
39
-	/**
40
-	 * @var string
41
-	 */
42
-	private string $lastOpcode;
43
-
44
-	/**
45
-	 * @var float|int
46
-	 */
47
-	private float|int $closeStatus;
48
-
49
-	/**
50
-	 * @var string|null
51
-	 */
52
-	private ?string $hugePayload;
53
-
54
-	/**
55
-	 * @var array|int[]
56
-	 */
57
-	private static array $opcodes = [
58
-		CommonsContract::EVENT_TYPE_CONTINUATION => 0,
59
-		CommonsContract::EVENT_TYPE_TEXT => 1,
60
-		CommonsContract::EVENT_TYPE_BINARY => 2,
61
-		CommonsContract::EVENT_TYPE_CLOSE => 8,
62
-		CommonsContract::EVENT_TYPE_PING => 9,
63
-		CommonsContract::EVENT_TYPE_PONG => 10,
64
-	];
65
-
66
-	/**
67
-	 * @var string
68
-	 */
69
-	protected string $socketUrl = '';
70
-
71
-	/**
72
-	 * @var WSConfig
73
-	 */
74
-	protected WSConfig $config;
75
-
76
-	/**
77
-	 * @param WSConfig $config
78
-	 * @throws \Exception
79
-	 */
80
-	protected function connect(WSConfig $config): void
81
-	{
82
-		$this->config = $config;
83
-		$urlParts = parse_url($this->socketUrl);
84
-
85
-		$this->config->setScheme($urlParts['scheme']);
86
-		$this->config->setHost($urlParts['host']);
87
-		$this->config->setUser($urlParts);
88
-		$this->config->setPassword($urlParts);
89
-		$this->config->setPort($urlParts);
90
-
91
-		$pathWithQuery = $this->getPathWithQuery($urlParts);
92
-		$hostUri = $this->getHostUri($this->config);
93
-
94
-		$context = $this->getStreamContext();
95
-		if ($this->config->hasProxy()) {
96
-			$this->socket = $this->proxy();
97
-		} else {
98
-			$this->socket = @stream_socket_client(
99
-				$hostUri . ':' . $this->config->getPort(),
100
-				$errno,
101
-				$errstr,
102
-				$this->config->getTimeout(),
103
-				STREAM_CLIENT_CONNECT,
104
-				$context
105
-			);
106
-		}
107
-
108
-		if ($this->socket === false) {
109
-			throw new ConnectionException(
110
-				"Could not open socket to \"{$this->config->getHost()}:{$this->config->getPort()}\": $errstr ($errno).",
111
-				CommonsContract::CLIENT_COULD_NOT_OPEN_SOCKET
112
-			);
113
-		}
114
-
115
-		stream_set_timeout($this->socket, $this->config->getTimeout());
116
-
117
-		$key = $this->generateKey();
118
-		$headers = [
119
-			'Host' => $this->config->getHost() . ':' . $this->config->getPort(),
120
-			'User-Agent' => 'websocket-client-php',
121
-			'Connection' => 'Upgrade',
122
-			'Upgrade' => 'WebSocket',
123
-			'Sec-WebSocket-Key' => $key,
124
-			'Sec-Websocket-Version' => '13',
125
-		];
126
-
127
-		if ($this->config->getUser() || $this->config->getPassword()) {
128
-			$headers['authorization'] = 'Basic ' . base64_encode($this->config->getUser() . ':' . $this->config->getPassword()) . "\r\n";
129
-		}
130
-
131
-		if (!empty($this->config->getHeaders())) {
132
-			$headers = array_merge($headers, $this->config->getHeaders());
133
-		}
134
-
135
-		$header = $this->getHeaders($pathWithQuery, $headers);
136
-
137
-		$this->write($header);
138
-
139
-		$this->validateResponse($this->config, $pathWithQuery, $key);
140
-		$this->isConnected = true;
141
-	}
142
-
143
-	/**
144
-	 * Init a proxy connection
145
-	 *
146
-	 * @return resource|false
147
-	 * @throws InvalidArgumentException
148
-	 * @throws ConnectionException
149
-	 */
150
-	private function proxy()
151
-	{
152
-		$sock = @stream_socket_client(
153
-			WscCommonsContract::TCP_SCHEME . $this->config->getProxyIp() . ':' . $this->config->getProxyPort(),
154
-			$errno,
155
-			$errstr,
156
-			$this->config->getTimeout(),
157
-			STREAM_CLIENT_CONNECT,
158
-			$this->getStreamContext()
159
-		);
160
-		$write = "CONNECT {$this->config->getProxyIp()}:{$this->config->getProxyPort()} HTTP/1.1\r\n";
161
-		$auth = $this->config->getProxyAuth();
162
-		if ($auth !== NULL) {
163
-			$write .= "Proxy-Authorization: Basic {$auth}\r\n";
164
-		}
165
-		$write .= "\r\n";
166
-		fwrite($sock, $write);
167
-		$resp = fread($sock, 1024);
168
-
169
-		if (preg_match(self::PROXY_MATCH_RESP, $resp) === 1) {
170
-			return $sock;
171
-		}
172
-
173
-		throw new ConnectionException('Failed to connect to the host via proxy');
174
-	}
175
-
176
-	/**
177
-	 * @return mixed
178
-	 * @throws \InvalidArgumentException
179
-	 */
180
-	private function getStreamContext(): mixed
181
-	{
182
-		if ($this->config->getContext() !== null) {
183
-			// Suppress the error since we'll catch it below
184
-			if (@get_resource_type($this->config->getContext()) === 'stream-context') {
185
-				return $this->config->getContext();
186
-			}
187
-
188
-			throw new \InvalidArgumentException(
189
-				'Stream context is invalid',
190
-				CommonsContract::CLIENT_INVALID_STREAM_CONTEXT
191
-			);
192
-		}
193
-
194
-		return stream_context_create($this->config->getContextOptions());
195
-	}
196
-
197
-	/**
198
-	 * @param mixed $urlParts
199
-	 * @return string
200
-	 */
201
-	private function getPathWithQuery(mixed $urlParts): string
202
-	{
203
-		$path = isset($urlParts['path']) ? $urlParts['path'] : '/';
204
-		$query = isset($urlParts['query']) ? $urlParts['query'] : '';
205
-		$fragment = isset($urlParts['fragment']) ? $urlParts['fragment'] : '';
206
-		$pathWithQuery = $path;
207
-		if (!empty($query)) {
208
-			$pathWithQuery .= '?' . $query;
209
-		}
210
-		if (!empty($fragment)) {
211
-			$pathWithQuery .= '#' . $fragment;
212
-		}
213
-
214
-		return $pathWithQuery;
215
-	}
216
-
217
-	/**
218
-	 * @param string $pathWithQuery
219
-	 * @param array $headers
220
-	 * @return string
221
-	 */
222
-	private function getHeaders(string $pathWithQuery, array $headers): string
223
-	{
224
-		return 'GET ' . $pathWithQuery . " HTTP/1.1\r\n"
225
-			. implode(
226
-				"\r\n",
227
-				array_map(
228
-					function ($key, $value) {
229
-						return "$key: $value";
230
-					},
231
-					array_keys($headers),
232
-					$headers
233
-				)
234
-			)
235
-			. "\r\n\r\n";
236
-	}
237
-
238
-	/**
239
-	 * @return string
240
-	 */
241
-	public function getLastOpcode(): string
242
-	{
243
-		return $this->lastOpcode;
244
-	}
245
-
246
-	/**
247
-	 * @return int
248
-	 */
249
-	public function getCloseStatus(): int
250
-	{
251
-		return $this->closeStatus;
252
-	}
253
-
254
-	/**
255
-	 * @return bool
256
-	 */
257
-	public function isConnected(): bool
258
-	{
259
-		return $this->isConnected;
260
-	}
261
-
262
-	/**
263
-	 * @param int $timeout
264
-	 * @param null $microSecs
265
-	 * @return WscMain
266
-	 */
267
-	public function setTimeout(int $timeout, $microSecs = null): WscMain
268
-	{
269
-		$this->config->setTimeout($timeout);
270
-		if ($this->socket && get_resource_type($this->socket) === 'stream') {
271
-			stream_set_timeout($this->socket, $timeout, $microSecs);
272
-		}
273
-
274
-		return $this;
275
-	}
276
-
277
-	/**
278
-	 * Sends message to opened socket connection client->server
279
-	 *
280
-	 * @param $payload
281
-	 * @param string $opcode
282
-	 * @throws \Exception
283
-	 */
284
-	public function send($payload, string $opcode = CommonsContract::EVENT_TYPE_TEXT): void
285
-	{
286
-		if (!$this->isConnected) {
287
-			$this->connect(new WSConfig());
288
-		}
289
-
290
-		if (array_key_exists($opcode, self::$opcodes) === false) {
291
-			throw new BadOpcodeException(
292
-				"Bad opcode '$opcode'.  Try 'text' or 'binary'.",
293
-				CommonsContract::CLIENT_BAD_OPCODE
294
-			);
295
-		}
296
-
297
-		$payloadLength = strlen($payload);
298
-		$fragmentCursor = 0;
299
-
300
-		while ($payloadLength > $fragmentCursor) {
301
-			$subPayload = substr($payload, $fragmentCursor, $this->config->getFragmentSize());
302
-			$fragmentCursor += $this->config->getFragmentSize();
303
-			$final = $payloadLength <= $fragmentCursor;
304
-			$this->sendFragment($final, $subPayload, $opcode, true);
305
-			$opcode = 'continuation';
306
-		}
307
-	}
308
-
309
-	/**
310
-	 * Receives message client<-server
311
-	 *
312
-	 * @return null|string
313
-	 * @throws \Exception
314
-	 */
315
-	public function receive(): ?string
316
-	{
317
-		if (!$this->isConnected) {
318
-			$this->connect(new WSConfig());
319
-		}
320
-
321
-		$this->hugePayload = '';
322
-
323
-		$response = null;
324
-		while ($response === null) {
325
-			$response = $this->receiveFragment();
326
-		}
327
-
328
-		return $response;
329
-	}
330
-
331
-	/**
332
-	 * Tell the socket to close.
333
-	 *
334
-	 * @param integer $status http://tools.ietf.org/html/rfc6455#section-7.4
335
-	 * @param string $message A closing message, max 125 bytes.
336
-	 * @return bool|null|string
337
-	 * @throws \Exception
338
-	 */
339
-	public function close(int $status = 1000, string $message = 'ttfn'): bool|null|string
340
-	{
341
-		$statusBin = sprintf('%016b', $status);
342
-		$statusStr = '';
343
-
344
-		foreach (str_split($statusBin, 8) as $binstr) {
345
-			$statusStr .= chr(bindec($binstr));
346
-		}
347
-
348
-		$this->send($statusStr . $message, CommonsContract::EVENT_TYPE_CLOSE);
349
-		$this->isClosing = true;
350
-
351
-		return $this->receive(); // Receiving a close frame will close the socket now.
352
-	}
353
-
354
-	/**
355
-	 * @param string $data
356
-	 * @throws ConnectionException
357
-	 */
358
-	protected function write(string $data): void
359
-	{
360
-		$written = fwrite($this->socket, $data);
361
-
362
-		if ($written < strlen($data)) {
363
-			throw new ConnectionException(
364
-				"Could only write $written out of " . strlen($data) . ' bytes.',
365
-				CommonsContract::CLIENT_COULD_ONLY_WRITE_LESS
366
-			);
367
-		}
368
-	}
369
-
370
-	/**
371
-	 * @param int $len
372
-	 * @return string
373
-	 * @throws ConnectionException
374
-	 */
375
-	protected function read(int $len): string
376
-	{
377
-		$data = '';
378
-		while (($dataLen = strlen($data)) < $len) {
379
-			$buff = fread($this->socket, $len - $dataLen);
380
-
381
-			if ($buff === false) {
382
-				$metadata = stream_get_meta_data($this->socket);
383
-				throw new ConnectionException(
384
-					'Broken frame, read ' . strlen($data) . ' of stated '
385
-					. $len . ' bytes.  Stream state: '
386
-					. json_encode($metadata),
387
-					CommonsContract::CLIENT_BROKEN_FRAME
388
-				);
389
-			}
390
-
391
-			if ($buff === '') {
392
-				$metadata = stream_get_meta_data($this->socket);
393
-				throw new ConnectionException(
394
-					'Empty read; connection dead?  Stream state: ' . json_encode($metadata),
395
-					CommonsContract::CLIENT_EMPTY_READ
396
-				);
397
-			}
398
-			$data .= $buff;
399
-		}
400
-
401
-		return $data;
402
-	}
403
-
404
-	/**
405
-	 * Helper to convert a binary to a string of '0' and '1'.
406
-	 *
407
-	 * @param string $string
408
-	 * @return string
409
-	 */
410
-	protected static function sprintB(string $string): string
411
-	{
412
-		$return = '';
413
-		$strLen = strlen($string);
414
-		for ($i = 0; $i < $strLen; $i++) {
415
-			$return .= sprintf('%08b', ord($string[$i]));
416
-		}
417
-
418
-		return $return;
419
-	}
420
-
421
-	/**
422
-	 * Sec-WebSocket-Key generator
423
-	 *
424
-	 * @return string   the 16 character length key
425
-	 * @throws \Exception
426
-	 */
427
-	private function generateKey(): string
428
-	{
429
-		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$&/()=[]{}0123456789';
430
-		$key = '';
431
-		$chLen = strlen($chars);
432
-		for ($i = 0; $i < self::KEY_GEN_LENGTH; $i++) {
433
-			$key .= $chars[random_int(0, $chLen - 1)];
434
-		}
435
-
436
-		return base64_encode($key);
437
-	}
22
+    use WSClientTrait;
23
+
24
+    /**
25
+     * @var resource|bool
26
+     */
27
+    private $socket;
28
+
29
+    /**
30
+     * @var bool
31
+     */
32
+    private bool $isConnected = false;
33
+
34
+    /**
35
+     * @var bool
36
+     */
37
+    private bool $isClosing = false;
38
+
39
+    /**
40
+     * @var string
41
+     */
42
+    private string $lastOpcode;
43
+
44
+    /**
45
+     * @var float|int
46
+     */
47
+    private float|int $closeStatus;
48
+
49
+    /**
50
+     * @var string|null
51
+     */
52
+    private ?string $hugePayload;
53
+
54
+    /**
55
+     * @var array|int[]
56
+     */
57
+    private static array $opcodes = [
58
+        CommonsContract::EVENT_TYPE_CONTINUATION => 0,
59
+        CommonsContract::EVENT_TYPE_TEXT => 1,
60
+        CommonsContract::EVENT_TYPE_BINARY => 2,
61
+        CommonsContract::EVENT_TYPE_CLOSE => 8,
62
+        CommonsContract::EVENT_TYPE_PING => 9,
63
+        CommonsContract::EVENT_TYPE_PONG => 10,
64
+    ];
65
+
66
+    /**
67
+     * @var string
68
+     */
69
+    protected string $socketUrl = '';
70
+
71
+    /**
72
+     * @var WSConfig
73
+     */
74
+    protected WSConfig $config;
75
+
76
+    /**
77
+     * @param WSConfig $config
78
+     * @throws \Exception
79
+     */
80
+    protected function connect(WSConfig $config): void
81
+    {
82
+        $this->config = $config;
83
+        $urlParts = parse_url($this->socketUrl);
84
+
85
+        $this->config->setScheme($urlParts['scheme']);
86
+        $this->config->setHost($urlParts['host']);
87
+        $this->config->setUser($urlParts);
88
+        $this->config->setPassword($urlParts);
89
+        $this->config->setPort($urlParts);
90
+
91
+        $pathWithQuery = $this->getPathWithQuery($urlParts);
92
+        $hostUri = $this->getHostUri($this->config);
93
+
94
+        $context = $this->getStreamContext();
95
+        if ($this->config->hasProxy()) {
96
+            $this->socket = $this->proxy();
97
+        } else {
98
+            $this->socket = @stream_socket_client(
99
+                $hostUri . ':' . $this->config->getPort(),
100
+                $errno,
101
+                $errstr,
102
+                $this->config->getTimeout(),
103
+                STREAM_CLIENT_CONNECT,
104
+                $context
105
+            );
106
+        }
107
+
108
+        if ($this->socket === false) {
109
+            throw new ConnectionException(
110
+                "Could not open socket to \"{$this->config->getHost()}:{$this->config->getPort()}\": $errstr ($errno).",
111
+                CommonsContract::CLIENT_COULD_NOT_OPEN_SOCKET
112
+            );
113
+        }
114
+
115
+        stream_set_timeout($this->socket, $this->config->getTimeout());
116
+
117
+        $key = $this->generateKey();
118
+        $headers = [
119
+            'Host' => $this->config->getHost() . ':' . $this->config->getPort(),
120
+            'User-Agent' => 'websocket-client-php',
121
+            'Connection' => 'Upgrade',
122
+            'Upgrade' => 'WebSocket',
123
+            'Sec-WebSocket-Key' => $key,
124
+            'Sec-Websocket-Version' => '13',
125
+        ];
126
+
127
+        if ($this->config->getUser() || $this->config->getPassword()) {
128
+            $headers['authorization'] = 'Basic ' . base64_encode($this->config->getUser() . ':' . $this->config->getPassword()) . "\r\n";
129
+        }
130
+
131
+        if (!empty($this->config->getHeaders())) {
132
+            $headers = array_merge($headers, $this->config->getHeaders());
133
+        }
134
+
135
+        $header = $this->getHeaders($pathWithQuery, $headers);
136
+
137
+        $this->write($header);
138
+
139
+        $this->validateResponse($this->config, $pathWithQuery, $key);
140
+        $this->isConnected = true;
141
+    }
142
+
143
+    /**
144
+     * Init a proxy connection
145
+     *
146
+     * @return resource|false
147
+     * @throws InvalidArgumentException
148
+     * @throws ConnectionException
149
+     */
150
+    private function proxy()
151
+    {
152
+        $sock = @stream_socket_client(
153
+            WscCommonsContract::TCP_SCHEME . $this->config->getProxyIp() . ':' . $this->config->getProxyPort(),
154
+            $errno,
155
+            $errstr,
156
+            $this->config->getTimeout(),
157
+            STREAM_CLIENT_CONNECT,
158
+            $this->getStreamContext()
159
+        );
160
+        $write = "CONNECT {$this->config->getProxyIp()}:{$this->config->getProxyPort()} HTTP/1.1\r\n";
161
+        $auth = $this->config->getProxyAuth();
162
+        if ($auth !== NULL) {
163
+            $write .= "Proxy-Authorization: Basic {$auth}\r\n";
164
+        }
165
+        $write .= "\r\n";
166
+        fwrite($sock, $write);
167
+        $resp = fread($sock, 1024);
168
+
169
+        if (preg_match(self::PROXY_MATCH_RESP, $resp) === 1) {
170
+            return $sock;
171
+        }
172
+
173
+        throw new ConnectionException('Failed to connect to the host via proxy');
174
+    }
175
+
176
+    /**
177
+     * @return mixed
178
+     * @throws \InvalidArgumentException
179
+     */
180
+    private function getStreamContext(): mixed
181
+    {
182
+        if ($this->config->getContext() !== null) {
183
+            // Suppress the error since we'll catch it below
184
+            if (@get_resource_type($this->config->getContext()) === 'stream-context') {
185
+                return $this->config->getContext();
186
+            }
187
+
188
+            throw new \InvalidArgumentException(
189
+                'Stream context is invalid',
190
+                CommonsContract::CLIENT_INVALID_STREAM_CONTEXT
191
+            );
192
+        }
193
+
194
+        return stream_context_create($this->config->getContextOptions());
195
+    }
196
+
197
+    /**
198
+     * @param mixed $urlParts
199
+     * @return string
200
+     */
201
+    private function getPathWithQuery(mixed $urlParts): string
202
+    {
203
+        $path = isset($urlParts['path']) ? $urlParts['path'] : '/';
204
+        $query = isset($urlParts['query']) ? $urlParts['query'] : '';
205
+        $fragment = isset($urlParts['fragment']) ? $urlParts['fragment'] : '';
206
+        $pathWithQuery = $path;
207
+        if (!empty($query)) {
208
+            $pathWithQuery .= '?' . $query;
209
+        }
210
+        if (!empty($fragment)) {
211
+            $pathWithQuery .= '#' . $fragment;
212
+        }
213
+
214
+        return $pathWithQuery;
215
+    }
216
+
217
+    /**
218
+     * @param string $pathWithQuery
219
+     * @param array $headers
220
+     * @return string
221
+     */
222
+    private function getHeaders(string $pathWithQuery, array $headers): string
223
+    {
224
+        return 'GET ' . $pathWithQuery . " HTTP/1.1\r\n"
225
+            . implode(
226
+                "\r\n",
227
+                array_map(
228
+                    function ($key, $value) {
229
+                        return "$key: $value";
230
+                    },
231
+                    array_keys($headers),
232
+                    $headers
233
+                )
234
+            )
235
+            . "\r\n\r\n";
236
+    }
237
+
238
+    /**
239
+     * @return string
240
+     */
241
+    public function getLastOpcode(): string
242
+    {
243
+        return $this->lastOpcode;
244
+    }
245
+
246
+    /**
247
+     * @return int
248
+     */
249
+    public function getCloseStatus(): int
250
+    {
251
+        return $this->closeStatus;
252
+    }
253
+
254
+    /**
255
+     * @return bool
256
+     */
257
+    public function isConnected(): bool
258
+    {
259
+        return $this->isConnected;
260
+    }
261
+
262
+    /**
263
+     * @param int $timeout
264
+     * @param null $microSecs
265
+     * @return WscMain
266
+     */
267
+    public function setTimeout(int $timeout, $microSecs = null): WscMain
268
+    {
269
+        $this->config->setTimeout($timeout);
270
+        if ($this->socket && get_resource_type($this->socket) === 'stream') {
271
+            stream_set_timeout($this->socket, $timeout, $microSecs);
272
+        }
273
+
274
+        return $this;
275
+    }
276
+
277
+    /**
278
+     * Sends message to opened socket connection client->server
279
+     *
280
+     * @param $payload
281
+     * @param string $opcode
282
+     * @throws \Exception
283
+     */
284
+    public function send($payload, string $opcode = CommonsContract::EVENT_TYPE_TEXT): void
285
+    {
286
+        if (!$this->isConnected) {
287
+            $this->connect(new WSConfig());
288
+        }
289
+
290
+        if (array_key_exists($opcode, self::$opcodes) === false) {
291
+            throw new BadOpcodeException(
292
+                "Bad opcode '$opcode'.  Try 'text' or 'binary'.",
293
+                CommonsContract::CLIENT_BAD_OPCODE
294
+            );
295
+        }
296
+
297
+        $payloadLength = strlen($payload);
298
+        $fragmentCursor = 0;
299
+
300
+        while ($payloadLength > $fragmentCursor) {
301
+            $subPayload = substr($payload, $fragmentCursor, $this->config->getFragmentSize());
302
+            $fragmentCursor += $this->config->getFragmentSize();
303
+            $final = $payloadLength <= $fragmentCursor;
304
+            $this->sendFragment($final, $subPayload, $opcode, true);
305
+            $opcode = 'continuation';
306
+        }
307
+    }
308
+
309
+    /**
310
+     * Receives message client<-server
311
+     *
312
+     * @return null|string
313
+     * @throws \Exception
314
+     */
315
+    public function receive(): ?string
316
+    {
317
+        if (!$this->isConnected) {
318
+            $this->connect(new WSConfig());
319
+        }
320
+
321
+        $this->hugePayload = '';
322
+
323
+        $response = null;
324
+        while ($response === null) {
325
+            $response = $this->receiveFragment();
326
+        }
327
+
328
+        return $response;
329
+    }
330
+
331
+    /**
332
+     * Tell the socket to close.
333
+     *
334
+     * @param integer $status http://tools.ietf.org/html/rfc6455#section-7.4
335
+     * @param string $message A closing message, max 125 bytes.
336
+     * @return bool|null|string
337
+     * @throws \Exception
338
+     */
339
+    public function close(int $status = 1000, string $message = 'ttfn'): bool|null|string
340
+    {
341
+        $statusBin = sprintf('%016b', $status);
342
+        $statusStr = '';
343
+
344
+        foreach (str_split($statusBin, 8) as $binstr) {
345
+            $statusStr .= chr(bindec($binstr));
346
+        }
347
+
348
+        $this->send($statusStr . $message, CommonsContract::EVENT_TYPE_CLOSE);
349
+        $this->isClosing = true;
350
+
351
+        return $this->receive(); // Receiving a close frame will close the socket now.
352
+    }
353
+
354
+    /**
355
+     * @param string $data
356
+     * @throws ConnectionException
357
+     */
358
+    protected function write(string $data): void
359
+    {
360
+        $written = fwrite($this->socket, $data);
361
+
362
+        if ($written < strlen($data)) {
363
+            throw new ConnectionException(
364
+                "Could only write $written out of " . strlen($data) . ' bytes.',
365
+                CommonsContract::CLIENT_COULD_ONLY_WRITE_LESS
366
+            );
367
+        }
368
+    }
369
+
370
+    /**
371
+     * @param int $len
372
+     * @return string
373
+     * @throws ConnectionException
374
+     */
375
+    protected function read(int $len): string
376
+    {
377
+        $data = '';
378
+        while (($dataLen = strlen($data)) < $len) {
379
+            $buff = fread($this->socket, $len - $dataLen);
380
+
381
+            if ($buff === false) {
382
+                $metadata = stream_get_meta_data($this->socket);
383
+                throw new ConnectionException(
384
+                    'Broken frame, read ' . strlen($data) . ' of stated '
385
+                    . $len . ' bytes.  Stream state: '
386
+                    . json_encode($metadata),
387
+                    CommonsContract::CLIENT_BROKEN_FRAME
388
+                );
389
+            }
390
+
391
+            if ($buff === '') {
392
+                $metadata = stream_get_meta_data($this->socket);
393
+                throw new ConnectionException(
394
+                    'Empty read; connection dead?  Stream state: ' . json_encode($metadata),
395
+                    CommonsContract::CLIENT_EMPTY_READ
396
+                );
397
+            }
398
+            $data .= $buff;
399
+        }
400
+
401
+        return $data;
402
+    }
403
+
404
+    /**
405
+     * Helper to convert a binary to a string of '0' and '1'.
406
+     *
407
+     * @param string $string
408
+     * @return string
409
+     */
410
+    protected static function sprintB(string $string): string
411
+    {
412
+        $return = '';
413
+        $strLen = strlen($string);
414
+        for ($i = 0; $i < $strLen; $i++) {
415
+            $return .= sprintf('%08b', ord($string[$i]));
416
+        }
417
+
418
+        return $return;
419
+    }
420
+
421
+    /**
422
+     * Sec-WebSocket-Key generator
423
+     *
424
+     * @return string   the 16 character length key
425
+     * @throws \Exception
426
+     */
427
+    private function generateKey(): string
428
+    {
429
+        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$&/()=[]{}0123456789';
430
+        $key = '';
431
+        $chLen = strlen($chars);
432
+        for ($i = 0; $i < self::KEY_GEN_LENGTH; $i++) {
433
+            $key .= $chars[random_int(0, $chLen - 1)];
434
+        }
435
+
436
+        return base64_encode($key);
437
+    }
438 438
 
439 439
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -225,7 +225,7 @@
 block discarded – undo
225 225
 			. implode(
226 226
 				"\r\n",
227 227
 				array_map(
228
-					function ($key, $value) {
228
+					function($key, $value) {
229 229
 						return "$key: $value";
230 230
 					},
231 231
 					array_keys($headers),
Please login to merge, or discard this patch.
src/FormData.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -12,67 +12,67 @@
 block discarded – undo
12 12
 class FormData
13 13
 {
14 14
 
15
-	/**
16
-	 * @var array
17
-	 */
18
-	private array $files = [];
15
+    /**
16
+     * @var array
17
+     */
18
+    private array $files = [];
19 19
 
20
-	/**
21
-	 * FromData constructor.
22
-	 *
23
-	 * @param string|array $file_path
24
-	 */
25
-	public function __construct(string|array $file_path = [])
26
-	{
27
-		if (is_string($file_path)) {
28
-			$file_path = [$file_path];
29
-		}
20
+    /**
21
+     * FromData constructor.
22
+     *
23
+     * @param string|array $file_path
24
+     */
25
+    public function __construct(string|array $file_path = [])
26
+    {
27
+        if (is_string($file_path)) {
28
+            $file_path = [$file_path];
29
+        }
30 30
 
31
-		foreach ($file_path as $name => $path) {
32
-			$this->addFile($name, $path);
33
-		}
34
-	}
31
+        foreach ($file_path as $name => $path) {
32
+            $this->addFile($name, $path);
33
+        }
34
+    }
35 35
 
36
-	/**
37
-	 * Get the field has passed through the class
38
-	 *
39
-	 * @return array
40
-	 */
41
-	public function getFiles(): array
42
-	{
43
-		return $this->files ?? [];
44
-	}
36
+    /**
37
+     * Get the field has passed through the class
38
+     *
39
+     * @return array
40
+     */
41
+    public function getFiles(): array
42
+    {
43
+        return $this->files ?? [];
44
+    }
45 45
 
46
-	/**
47
-	 * This method will create an array with instances of CURLFile class
48
-	 *
49
-	 * @param string|array $file_path
50
-	 * @return array
51
-	 */
52
-	public static function create(string|array $file_path): array
53
-	{
54
-		return (new FormData($file_path))->getFiles();
55
-	}
46
+    /**
47
+     * This method will create an array with instances of CURLFile class
48
+     *
49
+     * @param string|array $file_path
50
+     * @return array
51
+     */
52
+    public static function create(string|array $file_path): array
53
+    {
54
+        return (new FormData($file_path))->getFiles();
55
+    }
56 56
 
57
-	/**
58
-	 * @param string $name
59
-	 * @param string|\CURLFile $file
60
-	 * @return $this
61
-	 */
62
-	public function addFile(string $name, string|\CURLFile $file): FormData
63
-	{
64
-		if ($file instanceof \CURLFile) {
65
-			$this->files[$name] = $file;
66
-			return $this;
67
-		}
57
+    /**
58
+     * @param string $name
59
+     * @param string|\CURLFile $file
60
+     * @return $this
61
+     */
62
+    public function addFile(string $name, string|\CURLFile $file): FormData
63
+    {
64
+        if ($file instanceof \CURLFile) {
65
+            $this->files[$name] = $file;
66
+            return $this;
67
+        }
68 68
 
69
-		$this->files[$name] = new \CURLFile(
70
-			realpath($file),
71
-			Client::get_file_type($file),
72
-			basename($file)
73
-		);
69
+        $this->files[$name] = new \CURLFile(
70
+            realpath($file),
71
+            Client::get_file_type($file),
72
+            basename($file)
73
+        );
74 74
 
75
-		return $this;
76
-	}
75
+        return $this;
76
+    }
77 77
 
78 78
 }
79 79
\ No newline at end of file
Please login to merge, or discard this patch.