Skip to content

Commit 788ff12

Browse files
authored
replace custom HTTPS socket code with libcurl (hestiacp#3160)
* replace custom HTTPS socket code with libcurl several reasons, for one, "$result = fread($fp, 2048);" is not the correct way to read the result, what if its more than 2048 bytes? or what if its less, and the server doesn't close the connection, then you risk a stalling read taking much longer than than required, the correct way is to parse out the "Content-Length" header and read that many bytes (which curl does, the custom https socket code didn't), and.. its just simpler and easier to read curl code than custom https socket code~ * formatting * PR feedback hestiacp#3160 (comment) * PR feedback hestiacp#3160 (comment) * PR feedback/useragent
1 parent fe985ed commit 788ff12

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

install/common/roundcube/hestia.php

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author HestiaCP <info@hestiacp.com>
88
*/
99
class rcube_hestia_password {
10-
function save($curpass, $passwd) {
10+
public function save($curpass, $passwd) {
1111
$rcmail = rcmail::get_instance();
1212
$hestia_host = $rcmail->config->get("password_hestia_host");
1313

@@ -25,45 +25,29 @@ function save($curpass, $passwd) {
2525
"password" => $curpass,
2626
"new" => $passwd,
2727
];
28-
29-
$postdata = http_build_query($postvars);
30-
31-
$send = "POST /reset/mail/ HTTP/1.1" . PHP_EOL;
32-
$send .= "Host: " . $hestia_host . PHP_EOL;
33-
$send .= "User-Agent: PHP Script" . PHP_EOL;
34-
$send .= "Content-length: " . strlen($postdata) . PHP_EOL;
35-
$send .= "Content-type: application/x-www-form-urlencoded" . PHP_EOL;
36-
$send .= "Connection: close" . PHP_EOL;
37-
$send .= PHP_EOL;
38-
$send .= $postdata . PHP_EOL . PHP_EOL;
39-
40-
//$fp = fsockopen('ssl://' . $hestia_host, $hestia_port);
41-
$errno = "";
42-
$errstr = "";
43-
$context = stream_context_create();
44-
45-
$result = stream_context_set_option($context, "ssl", "verify_peer", false);
46-
$result = stream_context_set_option($context, "ssl", "verify_peer_name", false);
47-
$result = stream_context_set_option($context, "ssl", "verify_host", false);
48-
$result = stream_context_set_option($context, "ssl", "allow_self_signed", true);
49-
50-
$fp = stream_socket_client(
51-
"ssl://" . $hestia_host . ":" . $hestia_port,
52-
$errno,
53-
$errstr,
54-
60,
55-
STREAM_CLIENT_CONNECT,
56-
$context,
57-
);
58-
fputs($fp, $send);
59-
$result = fread($fp, 2048);
60-
fclose($fp);
61-
62-
$fp = fopen("/tmp/roundcube.log", "w");
63-
fwrite($fp, "test ok");
64-
fwrite($fp, "\n");
65-
fclose($fp);
66-
28+
$url = "https://{$hestia_host}:{$hestia_port}/reset/mail/";
29+
$ch = curl_init();
30+
if (
31+
false ===
32+
curl_setopt_array($ch, [
33+
CURLOPT_URL => $url,
34+
CURLOPT_RETURNTRANSFER => true,
35+
CURLOPT_HEADER => true,
36+
CURLOPT_POST => true,
37+
CURLOPT_POSTFIELDS => http_build_query($postvars),
38+
CURLOPT_USERAGENT => "Hestia Control Panel Password Driver",
39+
CURLOPT_SSL_VERIFYPEER => false,
40+
CURLOPT_SSL_VERIFYHOST => false,
41+
])
42+
) {
43+
// should never happen
44+
throw new Exception("curl_setopt_array() failed: " . curl_error($ch));
45+
}
46+
$result = curl_exec($ch);
47+
if (curl_errno($ch) !== CURLE_OK) {
48+
throw new Exception("curl_exec() failed: " . curl_error($ch));
49+
}
50+
curl_close($ch);
6751
if (strpos($result, "ok") && !strpos($result, "error")) {
6852
return PASSWORD_SUCCESS;
6953
} else {

0 commit comments

Comments
 (0)