xref: /php-src/ext/curl/interface.c (revision 99bceda0)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Sterling Hughes <sterling@php.net>                           |
14    +----------------------------------------------------------------------+
15 */
16 
17 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #include "php.h"
24 #include "Zend/zend_exceptions.h"
25 
26 #include <stdio.h>
27 #include <string.h>
28 
29 #ifdef PHP_WIN32
30 #include <winsock2.h>
31 #include <sys/types.h>
32 #endif
33 
34 #include <curl/curl.h>
35 #include <curl/easy.h>
36 
37 /* As of curl 7.11.1 this is no longer defined inside curl.h */
38 #ifndef HttpPost
39 #define HttpPost curl_httppost
40 #endif
41 
42 /* {{{ cruft for thread safe SSL crypto locks */
43 #if defined(ZTS) && defined(HAVE_CURL_OLD_OPENSSL)
44 # if defined(HAVE_OPENSSL_CRYPTO_H)
45 #  define PHP_CURL_NEED_OPENSSL_TSL
46 #  include <openssl/crypto.h>
47 # else
48 #  warning \
49 	"libcurl was compiled with OpenSSL support, but configure could not find " \
50 	"openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
51 	"cause random crashes on SSL requests"
52 # endif
53 #endif /* ZTS && HAVE_CURL_OLD_OPENSSL */
54 /* }}} */
55 
56 #include "zend_smart_str.h"
57 #include "ext/standard/info.h"
58 #include "ext/standard/file.h"
59 #include "ext/standard/url.h"
60 #include "curl_private.h"
61 
62 #ifdef __GNUC__
63 /* don't complain about deprecated CURLOPT_* we're exposing to PHP; we
64    need to keep using those to avoid breaking PHP API compatibiltiy */
65 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
66 #endif
67 
68 #include "curl_arginfo.h"
69 
70 #ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
71 static MUTEX_T *php_curl_openssl_tsl = NULL;
72 
73 /* Locking callbacks are no longer used since OpenSSL 1.1. Mark the functions as unused to
74  * avoid warnings due to this. */
php_curl_ssl_lock(int mode,int n,const char * file,int line)75 static ZEND_ATTRIBUTE_UNUSED void php_curl_ssl_lock(int mode, int n, const char * file, int line)
76 {
77 	if (mode & CRYPTO_LOCK) {
78 		tsrm_mutex_lock(php_curl_openssl_tsl[n]);
79 	} else {
80 		tsrm_mutex_unlock(php_curl_openssl_tsl[n]);
81 	}
82 }
83 
php_curl_ssl_id(void)84 static ZEND_ATTRIBUTE_UNUSED unsigned long php_curl_ssl_id(void)
85 {
86 	return (unsigned long) tsrm_thread_id();
87 }
88 #endif
89 /* }}} */
90 
91 #define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s) - 1, (zend_long) v);
92 #define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s) - 1, (double) v);
93 #define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s) - 1, (char *) (v ? v : ""));
94 #define CAASTR(s, v) add_assoc_str_ex(return_value, s, sizeof(s) - 1, \
95 		v ? zend_string_copy(v) : ZSTR_EMPTY_ALLOC());
96 #define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s) -1 , (zval *) v);
97 
98 #if defined(PHP_WIN32) || defined(__GNUC__)
99 # define php_curl_ret(__ret) RETVAL_FALSE; return __ret;
100 #else
101 # define php_curl_ret(__ret) RETVAL_FALSE; return;
102 #endif
103 
php_curl_option_str(php_curl * ch,zend_long option,const char * str,const size_t len)104 static zend_result php_curl_option_str(php_curl *ch, zend_long option, const char *str, const size_t len)
105 {
106 	if (zend_char_has_nul_byte(str, len)) {
107 		zend_value_error("%s(): cURL option must not contain any null bytes", get_active_function_name());
108 		return FAILURE;
109 	}
110 
111 	CURLcode error = curl_easy_setopt(ch->cp, option, str);
112 	SAVE_CURL_ERROR(ch, error);
113 
114 	return error == CURLE_OK ? SUCCESS : FAILURE;
115 }
116 
php_curl_option_url(php_curl * ch,const zend_string * url)117 static zend_result php_curl_option_url(php_curl *ch, const zend_string *url) /* {{{ */
118 {
119 	/* Disable file:// if open_basedir are used */
120 	if (PG(open_basedir) && *PG(open_basedir)) {
121 		curl_easy_setopt(ch->cp, CURLOPT_PROTOCOLS, CURLPROTO_ALL & ~CURLPROTO_FILE);
122 	}
123 
124 #ifdef PHP_WIN32
125 	if (
126 		zend_string_starts_with_literal_ci(url, "file://")
127 		&& '/' != ZSTR_VAL(url)[sizeof("file://") - 1]
128 		&& ZSTR_LEN(url) < MAXPATHLEN - 2
129 	) {
130 		char _tmp[MAXPATHLEN] = {0};
131 
132 		memmove(_tmp, "file:///", sizeof("file:///") - 1);
133 		memmove(_tmp + sizeof("file:///") - 1, ZSTR_VAL(url) + sizeof("file://") - 1, ZSTR_LEN(url) - sizeof("file://") + 1);
134 
135 		return php_curl_option_str(ch, CURLOPT_URL, _tmp, ZSTR_LEN(url) + 1);
136 	}
137 #endif
138 
139 	return php_curl_option_str(ch, CURLOPT_URL, ZSTR_VAL(url), ZSTR_LEN(url));
140 }
141 /* }}} */
142 
_php_curl_verify_handlers(php_curl * ch,bool reporterror)143 void _php_curl_verify_handlers(php_curl *ch, bool reporterror) /* {{{ */
144 {
145 	php_stream *stream;
146 
147 	ZEND_ASSERT(ch);
148 
149 	if (!Z_ISUNDEF(ch->handlers.std_err)) {
150 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers.std_err, NULL, php_file_le_stream(), php_file_le_pstream());
151 		if (stream == NULL) {
152 			if (reporterror) {
153 				php_error_docref(NULL, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr");
154 			}
155 			zval_ptr_dtor(&ch->handlers.std_err);
156 			ZVAL_UNDEF(&ch->handlers.std_err);
157 
158 			curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
159 		}
160 	}
161 	if (ch->handlers.read && !Z_ISUNDEF(ch->handlers.read->stream)) {
162 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers.read->stream, NULL, php_file_le_stream(), php_file_le_pstream());
163 		if (stream == NULL) {
164 			if (reporterror) {
165 				php_error_docref(NULL, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default");
166 			}
167 			zval_ptr_dtor(&ch->handlers.read->stream);
168 			ZVAL_UNDEF(&ch->handlers.read->stream);
169 			ch->handlers.read->res = NULL;
170 			ch->handlers.read->fp = 0;
171 
172 			curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
173 		}
174 	}
175 	if (ch->handlers.write_header && !Z_ISUNDEF(ch->handlers.write_header->stream)) {
176 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers.write_header->stream, NULL, php_file_le_stream(), php_file_le_pstream());
177 		if (stream == NULL) {
178 			if (reporterror) {
179 				php_error_docref(NULL, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default");
180 			}
181 			zval_ptr_dtor(&ch->handlers.write_header->stream);
182 			ZVAL_UNDEF(&ch->handlers.write_header->stream);
183 			ch->handlers.write_header->fp = 0;
184 
185 			ch->handlers.write_header->method = PHP_CURL_IGNORE;
186 			curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
187 		}
188 	}
189 	if (ch->handlers.write && !Z_ISUNDEF(ch->handlers.write->stream)) {
190 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers.write->stream, NULL, php_file_le_stream(), php_file_le_pstream());
191 		if (stream == NULL) {
192 			if (reporterror) {
193 				php_error_docref(NULL, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default");
194 			}
195 			zval_ptr_dtor(&ch->handlers.write->stream);
196 			ZVAL_UNDEF(&ch->handlers.write->stream);
197 			ch->handlers.write->fp = 0;
198 
199 			ch->handlers.write->method = PHP_CURL_STDOUT;
200 			curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
201 		}
202 	}
203 	return;
204 }
205 /* }}} */
206 
207 /* {{{ curl_module_entry */
208 zend_module_entry curl_module_entry = {
209 	STANDARD_MODULE_HEADER,
210 	"curl",
211 	ext_functions,
212 	PHP_MINIT(curl),
213 	PHP_MSHUTDOWN(curl),
214 	NULL,
215 	NULL,
216 	PHP_MINFO(curl),
217 	PHP_CURL_VERSION,
218 	STANDARD_MODULE_PROPERTIES
219 };
220 /* }}} */
221 
222 #ifdef COMPILE_DL_CURL
223 ZEND_GET_MODULE (curl)
224 #endif
225 
226 /* CurlHandle class */
227 
228 zend_class_entry *curl_ce;
229 zend_class_entry *curl_share_ce;
230 static zend_object_handlers curl_object_handlers;
231 
232 static zend_object *curl_create_object(zend_class_entry *class_type);
233 static void curl_free_obj(zend_object *object);
234 static HashTable *curl_get_gc(zend_object *object, zval **table, int *n);
235 static zend_function *curl_get_constructor(zend_object *object);
236 static zend_object *curl_clone_obj(zend_object *object);
237 php_curl *init_curl_handle_into_zval(zval *curl);
238 static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpostfields);
239 
240 /* {{{ PHP_INI_BEGIN */
241 PHP_INI_BEGIN()
242 	PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
PHP_INI_END()243 PHP_INI_END()
244 /* }}} */
245 
246 /* {{{ PHP_MINFO_FUNCTION */
247 PHP_MINFO_FUNCTION(curl)
248 {
249 	curl_version_info_data *d;
250 	char **p;
251 	char str[1024];
252 	size_t n = 0;
253 
254 	d = curl_version_info(CURLVERSION_NOW);
255 	php_info_print_table_start();
256 	php_info_print_table_row(2, "cURL support",    "enabled");
257 	php_info_print_table_row(2, "cURL Information", d->version);
258 	snprintf(str, sizeof(str), "%d", d->age);
259 	php_info_print_table_row(2, "Age", str);
260 
261 	/* To update on each new cURL release using src/main.c in cURL sources */
262 	/* make sure to sync this list with curl_version as well */
263 	if (d->features) {
264 		struct feat {
265 			const char *name;
266 			int bitmask;
267 		};
268 
269 		unsigned int i;
270 
271 		static const struct feat feats[] = {
272 			{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
273 			{"CharConv", CURL_VERSION_CONV},
274 			{"Debug", CURL_VERSION_DEBUG},
275 			{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
276 			{"IDN", CURL_VERSION_IDN},
277 			{"IPv6", CURL_VERSION_IPV6},
278 			{"krb4", CURL_VERSION_KERBEROS4},
279 			{"Largefile", CURL_VERSION_LARGEFILE},
280 			{"libz", CURL_VERSION_LIBZ},
281 			{"NTLM", CURL_VERSION_NTLM},
282 			{"NTLMWB", CURL_VERSION_NTLM_WB},
283 			{"SPNEGO", CURL_VERSION_SPNEGO},
284 			{"SSL",  CURL_VERSION_SSL},
285 			{"SSPI",  CURL_VERSION_SSPI},
286 			{"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
287 			{"HTTP2", CURL_VERSION_HTTP2},
288 			{"GSSAPI", CURL_VERSION_GSSAPI},
289 			{"KERBEROS5", CURL_VERSION_KERBEROS5},
290 			{"UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS},
291 			{"PSL", CURL_VERSION_PSL},
292 			{"HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY},
293 			{"MULTI_SSL", CURL_VERSION_MULTI_SSL},
294 			{"BROTLI", CURL_VERSION_BROTLI},
295 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
296 			{"ALTSVC", CURL_VERSION_ALTSVC},
297 #endif
298 #if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
299 			{"HTTP3", CURL_VERSION_HTTP3},
300 #endif
301 #if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
302 			{"UNICODE", CURL_VERSION_UNICODE},
303 			{"ZSTD", CURL_VERSION_ZSTD},
304 #endif
305 #if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
306 			{"HSTS", CURL_VERSION_HSTS},
307 #endif
308 #if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
309 			{"GSASL", CURL_VERSION_GSASL},
310 #endif
311 			{NULL, 0}
312 		};
313 
314 		php_info_print_table_row(1, "Features");
315 		for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
316 			if (feats[i].name) {
317 				php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
318 			}
319 		}
320 	}
321 
322 	n = 0;
323 	p = (char **) d->protocols;
324 	while (*p != NULL) {
325 			n += snprintf(str + n, sizeof(str) - n, "%s%s", *p, *(p + 1) != NULL ? ", " : "");
326 			p++;
327 	}
328 	php_info_print_table_row(2, "Protocols", str);
329 
330 	php_info_print_table_row(2, "Host", d->host);
331 
332 	if (d->ssl_version) {
333 		php_info_print_table_row(2, "SSL Version", d->ssl_version);
334 	}
335 
336 	if (d->libz_version) {
337 		php_info_print_table_row(2, "ZLib Version", d->libz_version);
338 	}
339 
340 #if defined(CURLVERSION_SECOND) && CURLVERSION_NOW >= CURLVERSION_SECOND
341 	if (d->ares) {
342 		php_info_print_table_row(2, "ZLib Version", d->ares);
343 	}
344 #endif
345 
346 #if defined(CURLVERSION_THIRD) && CURLVERSION_NOW >= CURLVERSION_THIRD
347 	if (d->libidn) {
348 		php_info_print_table_row(2, "libIDN Version", d->libidn);
349 	}
350 #endif
351 
352 	if (d->iconv_ver_num) {
353 		php_info_print_table_row(2, "IconV Version", d->iconv_ver_num);
354 	}
355 
356 	if (d->libssh_version) {
357 		php_info_print_table_row(2, "libSSH Version", d->libssh_version);
358 	}
359 
360 	php_info_print_table_end();
361 
362 	DISPLAY_INI_ENTRIES();
363 }
364 /* }}} */
365 
366 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(curl)367 PHP_MINIT_FUNCTION(curl)
368 {
369 	REGISTER_INI_ENTRIES();
370 
371 	register_curl_symbols(module_number);
372 
373 #ifdef PHP_CURL_NEED_OPENSSL_TSL
374 	if (!CRYPTO_get_id_callback()) {
375 		int i, c = CRYPTO_num_locks();
376 
377 		php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
378 		if (!php_curl_openssl_tsl) {
379 			return FAILURE;
380 		}
381 
382 		for (i = 0; i < c; ++i) {
383 			php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
384 		}
385 
386 		CRYPTO_set_id_callback(php_curl_ssl_id);
387 		CRYPTO_set_locking_callback(php_curl_ssl_lock);
388 	}
389 #endif
390 
391 	if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
392 		return FAILURE;
393 	}
394 
395 	curl_ce = register_class_CurlHandle();
396 	curl_ce->create_object = curl_create_object;
397 	curl_ce->default_object_handlers = &curl_object_handlers;
398 
399 	memcpy(&curl_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
400 	curl_object_handlers.offset = XtOffsetOf(php_curl, std);
401 	curl_object_handlers.free_obj = curl_free_obj;
402 	curl_object_handlers.get_gc = curl_get_gc;
403 	curl_object_handlers.get_constructor = curl_get_constructor;
404 	curl_object_handlers.clone_obj = curl_clone_obj;
405 	curl_object_handlers.cast_object = curl_cast_object;
406 	curl_object_handlers.compare = zend_objects_not_comparable;
407 
408 	curl_multi_ce = register_class_CurlMultiHandle();
409 	curl_multi_register_handlers();
410 
411 	curl_share_ce = register_class_CurlShareHandle();
412 	curl_share_register_handlers();
413 	curlfile_register_class();
414 
415 	return SUCCESS;
416 }
417 /* }}} */
418 
419 /* CurlHandle class */
420 
curl_create_object(zend_class_entry * class_type)421 static zend_object *curl_create_object(zend_class_entry *class_type) {
422 	php_curl *intern = zend_object_alloc(sizeof(php_curl), class_type);
423 
424 	zend_object_std_init(&intern->std, class_type);
425 	object_properties_init(&intern->std, class_type);
426 
427 	return &intern->std;
428 }
429 
curl_get_constructor(zend_object * object)430 static zend_function *curl_get_constructor(zend_object *object) {
431 	zend_throw_error(NULL, "Cannot directly construct CurlHandle, use curl_init() instead");
432 	return NULL;
433 }
434 
curl_clone_obj(zend_object * object)435 static zend_object *curl_clone_obj(zend_object *object) {
436 	php_curl *ch;
437 	CURL *cp;
438 	zval *postfields;
439 	zend_object *clone_object;
440 	php_curl *clone_ch;
441 
442 	clone_object = curl_create_object(curl_ce);
443 	clone_ch = curl_from_obj(clone_object);
444 	init_curl_handle(clone_ch);
445 
446 	ch = curl_from_obj(object);
447 	cp = curl_easy_duphandle(ch->cp);
448 	if (!cp) {
449 		zend_throw_exception(NULL, "Failed to clone CurlHandle", 0);
450 		return &clone_ch->std;
451 	}
452 
453 	clone_ch->cp = cp;
454 	_php_setup_easy_copy_handlers(clone_ch, ch);
455 
456 	postfields = &clone_ch->postfields;
457 	if (Z_TYPE_P(postfields) != IS_UNDEF) {
458 		if (build_mime_structure_from_hash(clone_ch, postfields) == FAILURE) {
459 			zend_throw_exception(NULL, "Failed to clone CurlHandle", 0);
460 			return &clone_ch->std;
461 		}
462 	}
463 
464 	return &clone_ch->std;
465 }
466 
curl_get_gc(zend_object * object,zval ** table,int * n)467 static HashTable *curl_get_gc(zend_object *object, zval **table, int *n)
468 {
469 	php_curl *curl = curl_from_obj(object);
470 
471 	zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
472 
473 	zend_get_gc_buffer_add_zval(gc_buffer, &curl->postfields);
474 	if (curl->handlers.read) {
475 		if (ZEND_FCC_INITIALIZED(curl->handlers.read->fcc)) {
476 			zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.read->fcc);
477 		}
478 		zend_get_gc_buffer_add_zval(gc_buffer, &curl->handlers.read->stream);
479 	}
480 
481 	if (curl->handlers.write) {
482 		if (ZEND_FCC_INITIALIZED(curl->handlers.write->fcc)) {
483 			zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.write->fcc);
484 		}
485 		zend_get_gc_buffer_add_zval(gc_buffer, &curl->handlers.write->stream);
486 	}
487 
488 	if (curl->handlers.write_header) {
489 		if (ZEND_FCC_INITIALIZED(curl->handlers.write_header->fcc)) {
490 			zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.write_header->fcc);
491 		}
492 		zend_get_gc_buffer_add_zval(gc_buffer, &curl->handlers.write_header->stream);
493 	}
494 
495 	if (ZEND_FCC_INITIALIZED(curl->handlers.progress)) {
496 		zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.progress);
497 	}
498 
499 	if (ZEND_FCC_INITIALIZED(curl->handlers.xferinfo)) {
500 		zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.xferinfo);
501 	}
502 
503 	if (ZEND_FCC_INITIALIZED(curl->handlers.fnmatch)) {
504 		zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.fnmatch);
505 	}
506 
507 	if (ZEND_FCC_INITIALIZED(curl->handlers.debug)) {
508 		zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.debug);
509 	}
510 
511 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
512 	if (ZEND_FCC_INITIALIZED(curl->handlers.prereq)) {
513 		zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.prereq);
514 	}
515 #endif
516 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
517 	if (ZEND_FCC_INITIALIZED(curl->handlers.sshhostkey)) {
518 		zend_get_gc_buffer_add_fcc(gc_buffer, &curl->handlers.sshhostkey);
519 	}
520 #endif
521 
522 	zend_get_gc_buffer_add_zval(gc_buffer, &curl->handlers.std_err);
523 	zend_get_gc_buffer_add_zval(gc_buffer, &curl->private_data);
524 
525 	zend_get_gc_buffer_use(gc_buffer, table, n);
526 
527 	return zend_std_get_properties(object);
528 }
529 
curl_cast_object(zend_object * obj,zval * result,int type)530 zend_result curl_cast_object(zend_object *obj, zval *result, int type)
531 {
532 	if (type == IS_LONG) {
533 		/* For better backward compatibility, make (int) $curl_handle return the object ID,
534 		 * similar to how it previously returned the resource ID. */
535 		ZVAL_LONG(result, obj->handle);
536 		return SUCCESS;
537 	}
538 
539 	return zend_std_cast_object_tostring(obj, result, type);
540 }
541 
542 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(curl)543 PHP_MSHUTDOWN_FUNCTION(curl)
544 {
545 	curl_global_cleanup();
546 #ifdef PHP_CURL_NEED_OPENSSL_TSL
547 	if (php_curl_openssl_tsl) {
548 		int i, c = CRYPTO_num_locks();
549 
550 		CRYPTO_set_id_callback(NULL);
551 		CRYPTO_set_locking_callback(NULL);
552 
553 		for (i = 0; i < c; ++i) {
554 			tsrm_mutex_free(php_curl_openssl_tsl[i]);
555 		}
556 
557 		free(php_curl_openssl_tsl);
558 		php_curl_openssl_tsl = NULL;
559 	}
560 #endif
561 	UNREGISTER_INI_ENTRIES();
562 	return SUCCESS;
563 }
564 /* }}} */
565 
566 /* {{{ curl_write */
curl_write(char * data,size_t size,size_t nmemb,void * ctx)567 static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
568 {
569 	php_curl *ch = (php_curl *) ctx;
570 	php_curl_write *write_handler = ch->handlers.write;
571 	size_t length = size * nmemb;
572 
573 #if PHP_CURL_DEBUG
574 	fprintf(stderr, "curl_write() called\n");
575 	fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
576 #endif
577 
578 	switch (write_handler->method) {
579 		case PHP_CURL_STDOUT:
580 			PHPWRITE(data, length);
581 			break;
582 		case PHP_CURL_FILE:
583 			return fwrite(data, size, nmemb, write_handler->fp);
584 		case PHP_CURL_RETURN:
585 			if (length > 0) {
586 				smart_str_appendl(&write_handler->buf, data, (int) length);
587 			}
588 			break;
589 		case PHP_CURL_USER: {
590 			zval argv[2];
591 			zval retval;
592 
593 			GC_ADDREF(&ch->std);
594 			ZVAL_OBJ(&argv[0], &ch->std);
595 			ZVAL_STRINGL(&argv[1], data, length);
596 
597 			ch->in_callback = true;
598 			zend_call_known_fcc(&write_handler->fcc, &retval, /* param_count */ 2, argv, /* named_params */ NULL);
599 			ch->in_callback = false;
600 			if (!Z_ISUNDEF(retval)) {
601 				_php_curl_verify_handlers(ch, /* reporterror */ true);
602 				/* TODO Check callback returns an int or something castable to int */
603 				length = zval_get_long(&retval);
604 			}
605 
606 			zval_ptr_dtor(&argv[0]);
607 			zval_ptr_dtor(&argv[1]);
608 			break;
609 		}
610 	}
611 
612 	return length;
613 }
614 /* }}} */
615 
616 /* {{{ curl_fnmatch */
curl_fnmatch(void * ctx,const char * pattern,const char * string)617 static int curl_fnmatch(void *ctx, const char *pattern, const char *string)
618 {
619 	php_curl *ch = (php_curl *) ctx;
620 	int rval = CURL_FNMATCHFUNC_FAIL;
621 	zval argv[3];
622 	zval retval;
623 
624 	GC_ADDREF(&ch->std);
625 	ZVAL_OBJ(&argv[0], &ch->std);
626 	ZVAL_STRING(&argv[1], pattern);
627 	ZVAL_STRING(&argv[2], string);
628 
629 	ch->in_callback = true;
630 	zend_call_known_fcc(&ch->handlers.fnmatch, &retval, /* param_count */ 3, argv, /* named_params */ NULL);
631 	ch->in_callback = false;
632 
633 	if (!Z_ISUNDEF(retval)) {
634 		_php_curl_verify_handlers(ch, /* reporterror */ true);
635 		/* TODO Check callback returns an int or something castable to int */
636 		rval = zval_get_long(&retval);
637 	}
638 	zval_ptr_dtor(&argv[0]);
639 	zval_ptr_dtor(&argv[1]);
640 	zval_ptr_dtor(&argv[2]);
641 	return rval;
642 }
643 /* }}} */
644 
645 /* {{{ curl_progress */
curl_progress(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)646 static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
647 {
648 	php_curl *ch = (php_curl *)clientp;
649 	size_t	rval = 0;
650 
651 #if PHP_CURL_DEBUG
652 	fprintf(stderr, "curl_progress() called\n");
653 	fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
654 #endif
655 
656 	zval args[5];
657 	zval retval;
658 
659 	GC_ADDREF(&ch->std);
660 	ZVAL_OBJ(&args[0], &ch->std);
661 	ZVAL_LONG(&args[1], (zend_long)dltotal);
662 	ZVAL_LONG(&args[2], (zend_long)dlnow);
663 	ZVAL_LONG(&args[3], (zend_long)ultotal);
664 	ZVAL_LONG(&args[4], (zend_long)ulnow);
665 
666 	ch->in_callback = true;
667 	zend_call_known_fcc(&ch->handlers.progress, &retval, /* param_count */ 5, args, /* named_params */ NULL);
668 	ch->in_callback = false;
669 
670 	if (!Z_ISUNDEF(retval)) {
671 		_php_curl_verify_handlers(ch, /* reporterror */ true);
672 		/* TODO Check callback returns an int or something castable to int */
673 		if (0 != zval_get_long(&retval)) {
674 			rval = 1;
675 		}
676 	}
677 
678 	zval_ptr_dtor(&args[0]);
679 	return rval;
680 }
681 /* }}} */
682 
683 /* {{{ curl_xferinfo */
curl_xferinfo(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)684 static size_t curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
685 {
686 	php_curl *ch = (php_curl *)clientp;
687 	size_t rval = 0;
688 
689 #if PHP_CURL_DEBUG
690 	fprintf(stderr, "curl_xferinfo() called\n");
691 	fprintf(stderr, "clientp = %x, dltotal = %ld, dlnow = %ld, ultotal = %ld, ulnow = %ld\n", clientp, dltotal, dlnow, ultotal, ulnow);
692 #endif
693 
694 	zval argv[5];
695 	zval retval;
696 
697 	GC_ADDREF(&ch->std);
698 	ZVAL_OBJ(&argv[0], &ch->std);
699 	ZVAL_LONG(&argv[1], dltotal);
700 	ZVAL_LONG(&argv[2], dlnow);
701 	ZVAL_LONG(&argv[3], ultotal);
702 	ZVAL_LONG(&argv[4], ulnow);
703 
704 	ch->in_callback = true;
705 	zend_call_known_fcc(&ch->handlers.xferinfo, &retval, /* param_count */ 5, argv, /* named_params */ NULL);
706 	ch->in_callback = false;
707 
708 	if (!Z_ISUNDEF(retval)) {
709 		_php_curl_verify_handlers(ch, /* reporterror */ true);
710 		/* TODO Check callback returns an int or something castable to int */
711 		if (0 != zval_get_long(&retval)) {
712 			rval = 1;
713 		}
714 	}
715 
716 	zval_ptr_dtor(&argv[0]);
717 	return rval;
718 }
719 /* }}} */
720 
721 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
curl_prereqfunction(void * clientp,char * conn_primary_ip,char * conn_local_ip,int conn_primary_port,int conn_local_port)722 static int curl_prereqfunction(void *clientp, char *conn_primary_ip, char *conn_local_ip, int conn_primary_port, int conn_local_port)
723 {
724 	php_curl *ch = (php_curl *)clientp;
725 	int rval = CURL_PREREQFUNC_OK;
726 
727 	// when CURLOPT_PREREQFUNCTION is set to null, curl_prereqfunction still
728 	// gets called. Return CURL_PREREQFUNC_OK immediately in this case to avoid
729 	// zend_call_known_fcc() with an uninitialized FCC.
730 	if (!ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
731     	return rval;
732     }
733 
734 #if PHP_CURL_DEBUG
735 	fprintf(stderr, "curl_prereqfunction() called\n");
736 	fprintf(stderr, "conn_primary_ip = %s, conn_local_ip = %s, conn_primary_port = %d, conn_local_port = %d\n", conn_primary_ip, conn_local_ip, conn_primary_port, conn_local_port);
737 #endif
738 
739 	zval args[5];
740 	zval retval;
741 
742 	GC_ADDREF(&ch->std);
743 	ZVAL_OBJ(&args[0], &ch->std);
744 	ZVAL_STRING(&args[1], conn_primary_ip);
745 	ZVAL_STRING(&args[2], conn_local_ip);
746 	ZVAL_LONG(&args[3], conn_primary_port);
747 	ZVAL_LONG(&args[4], conn_local_port);
748 
749 	ch->in_callback = true;
750 	zend_call_known_fcc(&ch->handlers.prereq, &retval, /* param_count */ 5, args, /* named_params */ NULL);
751 	ch->in_callback = false;
752 
753 	if (!Z_ISUNDEF(retval)) {
754 		_php_curl_verify_handlers(ch, /* reporterror */ true);
755 		if (Z_TYPE(retval) == IS_LONG) {
756 			zend_long retval_long = Z_LVAL(retval);
757 			if (retval_long == CURL_PREREQFUNC_OK || retval_long == CURL_PREREQFUNC_ABORT) {
758 				rval = retval_long;
759 			} else {
760 				zend_value_error("The CURLOPT_PREREQFUNCTION callback must return either CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT");
761 			}
762 		} else {
763 			zend_type_error("The CURLOPT_PREREQFUNCTION callback must return either CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT");
764 		}
765 	}
766 
767 	zval_ptr_dtor(&args[0]);
768 	zval_ptr_dtor(&args[1]);
769 	zval_ptr_dtor(&args[2]);
770 
771 	return rval;
772 }
773 #endif
774 
775 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
curl_ssh_hostkeyfunction(void * clientp,int keytype,const char * key,size_t keylen)776 static int curl_ssh_hostkeyfunction(void *clientp, int keytype, const char *key, size_t keylen)
777 {
778 	php_curl *ch = (php_curl *)clientp;
779 	int rval = CURLKHMATCH_MISMATCH; /* cancel connection in case of an exception */
780 
781 #if PHP_CURL_DEBUG
782 	fprintf(stderr, "curl_ssh_hostkeyfunction() called\n");
783 	fprintf(stderr, "clientp = %x, keytype = %d, key = %s, keylen = %zu\n", clientp, keytype, key, keylen);
784 #endif
785 
786 	zval args[4];
787 	zval retval;
788 
789 	GC_ADDREF(&ch->std);
790 	ZVAL_OBJ(&args[0], &ch->std);
791 	ZVAL_LONG(&args[1], keytype);
792 	ZVAL_STRINGL(&args[2], key, keylen);
793 	ZVAL_LONG(&args[3], keylen);
794 
795 	ch->in_callback = true;
796 	zend_call_known_fcc(&ch->handlers.sshhostkey, &retval, /* param_count */ 4, args, /* named_params */ NULL);
797 	ch->in_callback = false;
798 
799 	if (!Z_ISUNDEF(retval)) {
800 		_php_curl_verify_handlers(ch, /* reporterror */ true);
801 		if (Z_TYPE(retval) == IS_LONG) {
802 			zend_long retval_long = Z_LVAL(retval);
803 			if (retval_long == CURLKHMATCH_OK || retval_long == CURLKHMATCH_MISMATCH) {
804 				rval = retval_long;
805 			} else {
806 				zend_throw_error(NULL, "The CURLOPT_SSH_HOSTKEYFUNCTION callback must return either CURLKHMATCH_OK or CURLKHMATCH_MISMATCH");
807 			}
808 		} else {
809 			zend_throw_error(NULL, "The CURLOPT_SSH_HOSTKEYFUNCTION callback must return either CURLKHMATCH_OK or CURLKHMATCH_MISMATCH");
810 		}
811 	}
812 
813 	zval_ptr_dtor(&args[0]);
814 	zval_ptr_dtor(&args[2]);
815 	return rval;
816 }
817 #endif
818 
819 /* {{{ curl_read */
curl_read(char * data,size_t size,size_t nmemb,void * ctx)820 static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
821 {
822 	php_curl *ch = (php_curl *)ctx;
823 	php_curl_read *read_handler = ch->handlers.read;
824 	int length = 0;
825 
826 	switch (read_handler->method) {
827 		case PHP_CURL_DIRECT:
828 			if (read_handler->fp) {
829 				length = fread(data, size, nmemb, read_handler->fp);
830 			}
831 			break;
832 		case PHP_CURL_USER: {
833 			zval argv[3];
834 			zval retval;
835 
836 			GC_ADDREF(&ch->std);
837 			ZVAL_OBJ(&argv[0], &ch->std);
838 			if (read_handler->res) {
839 				GC_ADDREF(read_handler->res);
840 				ZVAL_RES(&argv[1], read_handler->res);
841 			} else {
842 				ZVAL_NULL(&argv[1]);
843 			}
844 			ZVAL_LONG(&argv[2], (int)size * nmemb);
845 
846 			ch->in_callback = true;
847 			zend_call_known_fcc(&read_handler->fcc, &retval, /* param_count */ 3, argv, /* named_params */ NULL);
848 			ch->in_callback = false;
849 			if (!Z_ISUNDEF(retval)) {
850 				_php_curl_verify_handlers(ch, /* reporterror */ true);
851 				if (Z_TYPE(retval) == IS_STRING) {
852 					length = MIN((size * nmemb), Z_STRLEN(retval));
853 					memcpy(data, Z_STRVAL(retval), length);
854 				} else if (Z_TYPE(retval) == IS_LONG) {
855 					length = Z_LVAL_P(&retval);
856 				}
857 				// TODO Do type error if invalid type?
858 				zval_ptr_dtor(&retval);
859 			}
860 
861 			zval_ptr_dtor(&argv[0]);
862 			zval_ptr_dtor(&argv[1]);
863 			break;
864 		}
865 	}
866 
867 	return length;
868 }
869 /* }}} */
870 
871 /* {{{ curl_write_header */
curl_write_header(char * data,size_t size,size_t nmemb,void * ctx)872 static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
873 {
874 	php_curl *ch = (php_curl *) ctx;
875 	php_curl_write *write_handler = ch->handlers.write_header;
876 	size_t length = size * nmemb;
877 
878 	switch (write_handler->method) {
879 		case PHP_CURL_STDOUT:
880 			/* Handle special case write when we're returning the entire transfer
881 			 */
882 			if (ch->handlers.write->method == PHP_CURL_RETURN && length > 0) {
883 				smart_str_appendl(&ch->handlers.write->buf, data, (int) length);
884 			} else {
885 				PHPWRITE(data, length);
886 			}
887 			break;
888 		case PHP_CURL_FILE:
889 			return fwrite(data, size, nmemb, write_handler->fp);
890 		case PHP_CURL_USER: {
891 			zval argv[2];
892 			zval retval;
893 
894 			GC_ADDREF(&ch->std);
895 			ZVAL_OBJ(&argv[0], &ch->std);
896 			ZVAL_STRINGL(&argv[1], data, length);
897 
898 			ch->in_callback = true;
899 			zend_call_known_fcc(&write_handler->fcc, &retval, /* param_count */ 2, argv, /* named_params */ NULL);
900 			ch->in_callback = false;
901 			if (!Z_ISUNDEF(retval)) {
902 				// TODO: Check for valid int type for return value
903 				_php_curl_verify_handlers(ch, /* reporterror */ true);
904 				length = zval_get_long(&retval);
905 			}
906 			zval_ptr_dtor(&argv[0]);
907 			zval_ptr_dtor(&argv[1]);
908 			break;
909 		}
910 
911 		case PHP_CURL_IGNORE:
912 			return length;
913 
914 		default:
915 			return -1;
916 	}
917 
918 	return length;
919 }
920 /* }}} */
921 
curl_debug(CURL * handle,curl_infotype type,char * data,size_t size,void * clientp)922 static int curl_debug(CURL *handle, curl_infotype type, char *data, size_t size, void *clientp) /* {{{ */
923 {
924 	php_curl *ch = (php_curl *)clientp;
925 
926     #if PHP_CURL_DEBUG
927     	fprintf(stderr, "curl_debug() called\n");
928     	fprintf(stderr, "type = %d, data = %s\n", type, data);
929     #endif
930 
931 	// Implicitly store the headers for compatibility with CURLINFO_HEADER_OUT
932 	// used as a Curl option. Previously, setting CURLINFO_HEADER_OUT set curl_debug
933 	// as the CURLOPT_DEBUGFUNCTION and stored the debug data when type is set to
934 	// CURLINFO_HEADER_OUT. For backward compatibility, we now store the headers
935 	// but also call the user-callback function if available.
936     if (type == CURLINFO_HEADER_OUT) {
937     	if (ch->header.str) {
938     		zend_string_release_ex(ch->header.str, 0);
939     	}
940     	ch->header.str = zend_string_init(data, size, 0);
941     }
942 
943     if (!ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
944        	return 0;
945     }
946 
947     zval args[3];
948 
949     GC_ADDREF(&ch->std);
950     ZVAL_OBJ(&args[0], &ch->std);
951     ZVAL_LONG(&args[1], type);
952     ZVAL_STRINGL(&args[2], data, size);
953 
954     ch->in_callback = true;
955     zend_call_known_fcc(&ch->handlers.debug, NULL, /* param_count */ 3, args, /* named_params */ NULL);
956     ch->in_callback = false;
957 
958     zval_ptr_dtor(&args[0]);
959     zval_ptr_dtor(&args[2]);
960 
961     return 0;
962 }
963 /* }}} */
964 
965 /* {{{ curl_free_post */
curl_free_post(void ** post)966 static void curl_free_post(void **post)
967 {
968 	curl_mime_free((curl_mime *)*post);
969 }
970 /* }}} */
971 
972 struct mime_data_cb_arg {
973 	zend_string *filename;
974 	php_stream *stream;
975 };
976 
977 /* {{{ curl_free_cb_arg */
curl_free_cb_arg(void ** cb_arg_p)978 static void curl_free_cb_arg(void **cb_arg_p)
979 {
980 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) *cb_arg_p;
981 
982 	ZEND_ASSERT(cb_arg->stream == NULL);
983 	zend_string_release(cb_arg->filename);
984 	efree(cb_arg);
985 }
986 /* }}} */
987 
988 /* {{{ curl_free_slist */
curl_free_slist(zval * el)989 static void curl_free_slist(zval *el)
990 {
991 	curl_slist_free_all(((struct curl_slist *)Z_PTR_P(el)));
992 }
993 /* }}} */
994 
995 /* {{{ Return cURL version information. */
PHP_FUNCTION(curl_version)996 PHP_FUNCTION(curl_version)
997 {
998 	curl_version_info_data *d;
999 
1000 	ZEND_PARSE_PARAMETERS_NONE();
1001 
1002 	d = curl_version_info(CURLVERSION_NOW);
1003 	if (d == NULL) {
1004 		RETURN_FALSE;
1005 	}
1006 
1007 	array_init(return_value);
1008 
1009 	CAAL("version_number", d->version_num);
1010 	CAAL("age", d->age);
1011 	CAAL("features", d->features);
1012 	/* Add an array of features */
1013 	{
1014 		struct feat {
1015 			const char *name;
1016 			int bitmask;
1017 		};
1018 
1019 		unsigned int i;
1020 		zval feature_list;
1021 		array_init(&feature_list);
1022 
1023 		/* Sync this list with PHP_MINFO_FUNCTION(curl) as well */
1024 		static const struct feat feats[] = {
1025 			{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
1026 			{"CharConv", CURL_VERSION_CONV},
1027 			{"Debug", CURL_VERSION_DEBUG},
1028 			{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
1029 			{"IDN", CURL_VERSION_IDN},
1030 			{"IPv6", CURL_VERSION_IPV6},
1031 			{"krb4", CURL_VERSION_KERBEROS4},
1032 			{"Largefile", CURL_VERSION_LARGEFILE},
1033 			{"libz", CURL_VERSION_LIBZ},
1034 			{"NTLM", CURL_VERSION_NTLM},
1035 			{"NTLMWB", CURL_VERSION_NTLM_WB},
1036 			{"SPNEGO", CURL_VERSION_SPNEGO},
1037 			{"SSL",  CURL_VERSION_SSL},
1038 			{"SSPI",  CURL_VERSION_SSPI},
1039 			{"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
1040 			{"HTTP2", CURL_VERSION_HTTP2},
1041 			{"GSSAPI", CURL_VERSION_GSSAPI},
1042 			{"KERBEROS5", CURL_VERSION_KERBEROS5},
1043 			{"UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS},
1044 			{"PSL", CURL_VERSION_PSL},
1045 			{"HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY},
1046 			{"MULTI_SSL", CURL_VERSION_MULTI_SSL},
1047 			{"BROTLI", CURL_VERSION_BROTLI},
1048 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1049 			{"ALTSVC", CURL_VERSION_ALTSVC},
1050 #endif
1051 #if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
1052 			{"HTTP3", CURL_VERSION_HTTP3},
1053 #endif
1054 #if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
1055 			{"UNICODE", CURL_VERSION_UNICODE},
1056 			{"ZSTD", CURL_VERSION_ZSTD},
1057 #endif
1058 #if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
1059 			{"HSTS", CURL_VERSION_HSTS},
1060 #endif
1061 #if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
1062 			{"GSASL", CURL_VERSION_GSASL},
1063 #endif
1064 		};
1065 
1066 		for(i = 0; i < sizeof(feats) / sizeof(feats[0]); i++) {
1067 			if (feats[i].name) {
1068 				add_assoc_bool(&feature_list, feats[i].name, d->features & feats[i].bitmask ? true : false);
1069 			}
1070 		}
1071 
1072 		CAAZ("feature_list", &feature_list);
1073 	}
1074 	CAAL("ssl_version_number", d->ssl_version_num);
1075 	CAAS("version", d->version);
1076 	CAAS("host", d->host);
1077 	CAAS("ssl_version", d->ssl_version);
1078 	CAAS("libz_version", d->libz_version);
1079 	/* Add an array of protocols */
1080 	{
1081 		char **p = (char **) d->protocols;
1082 		zval protocol_list;
1083 
1084 		array_init(&protocol_list);
1085 
1086 		while (*p != NULL) {
1087 			add_next_index_string(&protocol_list, *p);
1088 			p++;
1089 		}
1090 		CAAZ("protocols", &protocol_list);
1091 	}
1092 	if (d->age >= 1) {
1093 		CAAS("ares", d->ares);
1094 		CAAL("ares_num", d->ares_num);
1095 	}
1096 	if (d->age >= 2) {
1097 		CAAS("libidn", d->libidn);
1098 	}
1099 	if (d->age >= 3) {
1100 		CAAL("iconv_ver_num", d->iconv_ver_num);
1101 		CAAS("libssh_version", d->libssh_version);
1102 	}
1103 	if (d->age >= 4) {
1104 		CAAL("brotli_ver_num", d->brotli_ver_num);
1105 		CAAS("brotli_version", d->brotli_version);
1106 	}
1107 }
1108 /* }}} */
1109 
init_curl_handle_into_zval(zval * curl)1110 php_curl *init_curl_handle_into_zval(zval *curl)
1111 {
1112 	php_curl *ch;
1113 
1114 	object_init_ex(curl, curl_ce);
1115 	ch = Z_CURL_P(curl);
1116 
1117 	init_curl_handle(ch);
1118 
1119 	return ch;
1120 }
1121 
init_curl_handle(php_curl * ch)1122 void init_curl_handle(php_curl *ch)
1123 {
1124 	ch->to_free = ecalloc(1, sizeof(struct _php_curl_free));
1125 	ch->handlers.write = ecalloc(1, sizeof(php_curl_write));
1126 	ch->handlers.write_header = ecalloc(1, sizeof(php_curl_write));
1127 	ch->handlers.read = ecalloc(1, sizeof(php_curl_read));
1128 	ch->handlers.progress = empty_fcall_info_cache;
1129 	ch->handlers.xferinfo = empty_fcall_info_cache;
1130 	ch->handlers.fnmatch = empty_fcall_info_cache;
1131 	ch->handlers.debug = empty_fcall_info_cache;
1132 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1133 	ch->handlers.prereq = empty_fcall_info_cache;
1134 #endif
1135 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
1136 	ch->handlers.sshhostkey = empty_fcall_info_cache;
1137 #endif
1138 	ch->clone = emalloc(sizeof(uint32_t));
1139 	*ch->clone = 1;
1140 
1141 	memset(&ch->err, 0, sizeof(struct _php_curl_error));
1142 
1143 	zend_llist_init(&ch->to_free->post,  sizeof(struct HttpPost *), (llist_dtor_func_t)curl_free_post,   0);
1144 	zend_llist_init(&ch->to_free->stream, sizeof(struct mime_data_cb_arg *), (llist_dtor_func_t)curl_free_cb_arg, 0);
1145 
1146 	ch->to_free->slist = emalloc(sizeof(HashTable));
1147 	zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
1148 	ZVAL_UNDEF(&ch->postfields);
1149 }
1150 
1151 /* }}} */
1152 
1153 /* {{{ create_certinfo */
create_certinfo(struct curl_certinfo * ci,zval * listcode)1154 static void create_certinfo(struct curl_certinfo *ci, zval *listcode)
1155 {
1156 	int i;
1157 
1158 	if (ci) {
1159 		zval certhash;
1160 
1161 		for (i=0; i<ci->num_of_certs; i++) {
1162 			struct curl_slist *slist;
1163 
1164 			array_init(&certhash);
1165 			for (slist = ci->certinfo[i]; slist; slist = slist->next) {
1166 				int len;
1167 				char s[64];
1168 				char *tmp;
1169 				strncpy(s, slist->data, sizeof(s));
1170 				s[sizeof(s)-1] = '\0';
1171 				tmp = memchr(s, ':', sizeof(s));
1172 				if(tmp) {
1173 					*tmp = '\0';
1174 					len = strlen(s);
1175 					add_assoc_string(&certhash, s, &slist->data[len+1]);
1176 				} else {
1177 					php_error_docref(NULL, E_WARNING, "Could not extract hash key from certificate info");
1178 				}
1179 			}
1180 			add_next_index_zval(listcode, &certhash);
1181 		}
1182 	}
1183 }
1184 /* }}} */
1185 
1186 /* {{{ _php_curl_set_default_options()
1187    Set default options for a handle */
_php_curl_set_default_options(php_curl * ch)1188 static void _php_curl_set_default_options(php_curl *ch)
1189 {
1190 	char *cainfo;
1191 
1192 	curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
1193 	curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
1194 	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1195 	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
1196 	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1197 	curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
1198 	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1199 	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
1200 	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1201 	curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1202 	curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1203 
1204 	cainfo = INI_STR("openssl.cafile");
1205 	if (!(cainfo && cainfo[0] != '\0')) {
1206 		cainfo = INI_STR("curl.cainfo");
1207 	}
1208 	if (cainfo && cainfo[0] != '\0') {
1209 		curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1210 	}
1211 
1212 #ifdef ZTS
1213 	curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1214 #endif
1215 }
1216 /* }}} */
1217 
1218 /* {{{ Initialize a cURL session */
PHP_FUNCTION(curl_init)1219 PHP_FUNCTION(curl_init)
1220 {
1221 	php_curl *ch;
1222 	CURL 	 *cp;
1223 	zend_string *url = NULL;
1224 
1225 	ZEND_PARSE_PARAMETERS_START(0,1)
1226 		Z_PARAM_OPTIONAL
1227 		Z_PARAM_STR_OR_NULL(url)
1228 	ZEND_PARSE_PARAMETERS_END();
1229 
1230 	cp = curl_easy_init();
1231 	if (!cp) {
1232 		php_error_docref(NULL, E_WARNING, "Could not initialize a new cURL handle");
1233 		RETURN_FALSE;
1234 	}
1235 
1236 	ch = init_curl_handle_into_zval(return_value);
1237 
1238 	ch->cp = cp;
1239 
1240 	ch->handlers.write->method = PHP_CURL_STDOUT;
1241 	ch->handlers.read->method  = PHP_CURL_DIRECT;
1242 	ch->handlers.write_header->method = PHP_CURL_IGNORE;
1243 
1244 	_php_curl_set_default_options(ch);
1245 
1246 	if (url) {
1247 		if (php_curl_option_url(ch, url) == FAILURE) {
1248 			zval_ptr_dtor(return_value);
1249 			RETURN_FALSE;
1250 		}
1251 	}
1252 }
1253 /* }}} */
1254 
php_curl_copy_fcc_with_option(php_curl * ch,CURLoption option,zend_fcall_info_cache * target_fcc,zend_fcall_info_cache * source_fcc)1255 static void php_curl_copy_fcc_with_option(php_curl *ch, CURLoption option, zend_fcall_info_cache *target_fcc, zend_fcall_info_cache *source_fcc)
1256 {
1257 	if (ZEND_FCC_INITIALIZED(*source_fcc)) {
1258 		zend_fcc_dup(target_fcc, source_fcc);
1259 		curl_easy_setopt(ch->cp, option, (void *) ch);
1260 	}
1261 }
1262 
_php_setup_easy_copy_handlers(php_curl * ch,php_curl * source)1263 void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
1264 {
1265 	if (!Z_ISUNDEF(source->handlers.write->stream)) {
1266 		Z_ADDREF(source->handlers.write->stream);
1267 	}
1268 	ch->handlers.write->stream = source->handlers.write->stream;
1269 	ch->handlers.write->method = source->handlers.write->method;
1270 	if (!Z_ISUNDEF(source->handlers.read->stream)) {
1271 		Z_ADDREF(source->handlers.read->stream);
1272 	}
1273 	ch->handlers.read->stream  = source->handlers.read->stream;
1274 	ch->handlers.read->method  = source->handlers.read->method;
1275 	ch->handlers.write_header->method = source->handlers.write_header->method;
1276 	if (!Z_ISUNDEF(source->handlers.write_header->stream)) {
1277 		Z_ADDREF(source->handlers.write_header->stream);
1278 	}
1279 	ch->handlers.write_header->stream = source->handlers.write_header->stream;
1280 
1281 	ch->handlers.write->fp = source->handlers.write->fp;
1282 	ch->handlers.write_header->fp = source->handlers.write_header->fp;
1283 	ch->handlers.read->fp = source->handlers.read->fp;
1284 	ch->handlers.read->res = source->handlers.read->res;
1285 
1286 	if (ZEND_FCC_INITIALIZED(source->handlers.read->fcc)) {
1287 		zend_fcc_dup(&source->handlers.read->fcc, &source->handlers.read->fcc);
1288 	}
1289 	if (ZEND_FCC_INITIALIZED(source->handlers.write->fcc)) {
1290 		zend_fcc_dup(&source->handlers.write->fcc, &source->handlers.write->fcc);
1291 	}
1292 	if (ZEND_FCC_INITIALIZED(source->handlers.write_header->fcc)) {
1293 		zend_fcc_dup(&source->handlers.write_header->fcc, &source->handlers.write_header->fcc);
1294 	}
1295 
1296 	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1297 	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1298 	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1299 	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1300 	curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA,         (void *) ch);
1301 
1302 	php_curl_copy_fcc_with_option(ch, CURLOPT_PROGRESSDATA, &ch->handlers.progress, &source->handlers.progress);
1303 	php_curl_copy_fcc_with_option(ch, CURLOPT_XFERINFODATA, &ch->handlers.xferinfo, &source->handlers.xferinfo);
1304 	php_curl_copy_fcc_with_option(ch, CURLOPT_FNMATCH_DATA, &ch->handlers.fnmatch, &source->handlers.fnmatch);
1305 	php_curl_copy_fcc_with_option(ch, CURLOPT_DEBUGDATA, &ch->handlers.debug, &source->handlers.debug);
1306 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1307 	php_curl_copy_fcc_with_option(ch, CURLOPT_PREREQDATA, &ch->handlers.prereq, &source->handlers.prereq);
1308 #endif
1309 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
1310 	php_curl_copy_fcc_with_option(ch, CURLOPT_SSH_HOSTKEYDATA, &ch->handlers.sshhostkey, &source->handlers.sshhostkey);
1311 #endif
1312 
1313 	ZVAL_COPY(&ch->private_data, &source->private_data);
1314 
1315 	efree(ch->to_free->slist);
1316 	efree(ch->to_free);
1317 	ch->to_free = source->to_free;
1318 	efree(ch->clone);
1319 	ch->clone = source->clone;
1320 
1321 	/* Keep track of cloned copies to avoid invoking curl destructors for every clone */
1322 	(*source->clone)++;
1323 }
1324 
read_cb(char * buffer,size_t size,size_t nitems,void * arg)1325 static size_t read_cb(char *buffer, size_t size, size_t nitems, void *arg) /* {{{ */
1326 {
1327 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
1328 	ssize_t numread;
1329 
1330 	if (cb_arg->stream == NULL) {
1331 		if (!(cb_arg->stream = php_stream_open_wrapper(ZSTR_VAL(cb_arg->filename), "rb", IGNORE_PATH, NULL))) {
1332 			return CURL_READFUNC_ABORT;
1333 		}
1334 	}
1335 	numread = php_stream_read(cb_arg->stream, buffer, nitems * size);
1336 	if (numread < 0) {
1337 		php_stream_close(cb_arg->stream);
1338 		cb_arg->stream = NULL;
1339 		return CURL_READFUNC_ABORT;
1340 	}
1341 	return numread;
1342 }
1343 /* }}} */
1344 
seek_cb(void * arg,curl_off_t offset,int origin)1345 static int seek_cb(void *arg, curl_off_t offset, int origin) /* {{{ */
1346 {
1347 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
1348 	int res;
1349 
1350 	if (cb_arg->stream == NULL) {
1351 		return CURL_SEEKFUNC_CANTSEEK;
1352 	}
1353 	res = php_stream_seek(cb_arg->stream, offset, origin);
1354 	return res == SUCCESS ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK;
1355 }
1356 /* }}} */
1357 
free_cb(void * arg)1358 static void free_cb(void *arg) /* {{{ */
1359 {
1360 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
1361 
1362 	if (cb_arg->stream != NULL) {
1363 		php_stream_close(cb_arg->stream);
1364 		cb_arg->stream = NULL;
1365 	}
1366 }
1367 /* }}} */
1368 
add_simple_field(curl_mime * mime,zend_string * string_key,zval * current)1369 static inline CURLcode add_simple_field(curl_mime *mime, zend_string *string_key, zval *current)
1370 {
1371 	CURLcode error = CURLE_OK;
1372 	curl_mimepart *part;
1373 	CURLcode form_error;
1374 	zend_string *postval, *tmp_postval;
1375 
1376 	postval = zval_get_tmp_string(current, &tmp_postval);
1377 
1378 	part = curl_mime_addpart(mime);
1379 	if (part == NULL) {
1380 		zend_tmp_string_release(tmp_postval);
1381 		zend_string_release_ex(string_key, 0);
1382 		return CURLE_OUT_OF_MEMORY;
1383 	}
1384 	if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
1385 		|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
1386 		error = form_error;
1387 	}
1388 
1389 	zend_tmp_string_release(tmp_postval);
1390 
1391 	return error;
1392 }
1393 
build_mime_structure_from_hash(php_curl * ch,zval * zpostfields)1394 static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpostfields) /* {{{ */
1395 {
1396 	HashTable *postfields = Z_ARRVAL_P(zpostfields);
1397 	CURLcode error = CURLE_OK;
1398 	zval *current;
1399 	zend_string *string_key;
1400 	zend_ulong num_key;
1401 	curl_mime *mime = NULL;
1402 	curl_mimepart *part;
1403 	CURLcode form_error;
1404 
1405 	if (zend_hash_num_elements(postfields) > 0) {
1406 		mime = curl_mime_init(ch->cp);
1407 		if (mime == NULL) {
1408 			return FAILURE;
1409 		}
1410 	}
1411 
1412 	ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
1413 		zend_string *postval;
1414 		/* Pretend we have a string_key here */
1415 		if (!string_key) {
1416 			string_key = zend_long_to_str(num_key);
1417 		} else {
1418 			zend_string_addref(string_key);
1419 		}
1420 
1421 		ZVAL_DEREF(current);
1422 		if (Z_TYPE_P(current) == IS_OBJECT &&
1423 				instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
1424 			/* new-style file upload */
1425 			zval *prop, rv;
1426 			char *type = NULL, *filename = NULL;
1427 			struct mime_data_cb_arg *cb_arg;
1428 			php_stream *stream;
1429 			php_stream_statbuf ssb;
1430 			size_t filesize = -1;
1431 			curl_seek_callback seekfunc = seek_cb;
1432 
1433 			prop = zend_read_property_ex(curl_CURLFile_class, Z_OBJ_P(current), ZSTR_KNOWN(ZEND_STR_NAME), /* silent */ false, &rv);
1434 			ZVAL_DEREF(prop);
1435 			if (Z_TYPE_P(prop) != IS_STRING) {
1436 				php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
1437 			} else {
1438 				postval = Z_STR_P(prop);
1439 
1440 				if (php_check_open_basedir(ZSTR_VAL(postval))) {
1441 					return FAILURE;
1442 				}
1443 
1444 				prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
1445 				ZVAL_DEREF(prop);
1446 				if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
1447 					type = Z_STRVAL_P(prop);
1448 				}
1449 				prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
1450 				ZVAL_DEREF(prop);
1451 				if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
1452 					filename = Z_STRVAL_P(prop);
1453 				}
1454 
1455 				zval_ptr_dtor(&ch->postfields);
1456 				ZVAL_COPY(&ch->postfields, zpostfields);
1457 
1458 				if ((stream = php_stream_open_wrapper(ZSTR_VAL(postval), "rb", STREAM_MUST_SEEK, NULL))) {
1459 					if (!stream->readfilters.head && !php_stream_stat(stream, &ssb)) {
1460 						filesize = ssb.sb.st_size;
1461 					}
1462 				} else {
1463 					seekfunc = NULL;
1464 				}
1465 
1466 				cb_arg = emalloc(sizeof *cb_arg);
1467 				cb_arg->filename = zend_string_copy(postval);
1468 				cb_arg->stream = stream;
1469 
1470 				part = curl_mime_addpart(mime);
1471 				if (part == NULL) {
1472 					zend_string_release_ex(string_key, 0);
1473 					return FAILURE;
1474 				}
1475 				if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
1476 					|| (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
1477 					|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
1478 					|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
1479 					error = form_error;
1480 				}
1481 				zend_llist_add_element(&ch->to_free->stream, &cb_arg);
1482 			}
1483 
1484 			zend_string_release_ex(string_key, 0);
1485 			continue;
1486 		}
1487 
1488 		if (Z_TYPE_P(current) == IS_OBJECT && instanceof_function(Z_OBJCE_P(current), curl_CURLStringFile_class)) {
1489 			/* new-style file upload from string */
1490 			zval *prop, rv;
1491 			char *type = NULL, *filename = NULL;
1492 
1493 			prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
1494 			if (EG(exception)) {
1495 				zend_string_release_ex(string_key, 0);
1496 				return FAILURE;
1497 			}
1498 			ZVAL_DEREF(prop);
1499 			ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
1500 
1501 			filename = Z_STRVAL_P(prop);
1502 
1503 			prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
1504 			if (EG(exception)) {
1505 				zend_string_release_ex(string_key, 0);
1506 				return FAILURE;
1507 			}
1508 			ZVAL_DEREF(prop);
1509 			ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
1510 
1511 			type = Z_STRVAL_P(prop);
1512 
1513 			prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
1514 			if (EG(exception)) {
1515 				zend_string_release_ex(string_key, 0);
1516 				return FAILURE;
1517 			}
1518 			ZVAL_DEREF(prop);
1519 			ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
1520 
1521 			postval = Z_STR_P(prop);
1522 
1523 			zval_ptr_dtor(&ch->postfields);
1524 			ZVAL_COPY(&ch->postfields, zpostfields);
1525 
1526 			part = curl_mime_addpart(mime);
1527 			if (part == NULL) {
1528 				zend_string_release_ex(string_key, 0);
1529 				return FAILURE;
1530 			}
1531 			if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
1532 				|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
1533 				|| (form_error = curl_mime_filename(part, filename)) != CURLE_OK
1534 				|| (form_error = curl_mime_type(part, type)) != CURLE_OK) {
1535 				error = form_error;
1536 			}
1537 
1538 			zend_string_release_ex(string_key, 0);
1539 			continue;
1540 		}
1541 
1542 		if (Z_TYPE_P(current) == IS_ARRAY) {
1543 			zval *current_element;
1544 
1545 			ZEND_HASH_FOREACH_VAL(HASH_OF(current), current_element) {
1546 				add_simple_field(mime, string_key, current_element);
1547 			} ZEND_HASH_FOREACH_END();
1548 
1549 			zend_string_release_ex(string_key, 0);
1550 			continue;
1551 		}
1552 
1553 		add_simple_field(mime, string_key, current);
1554 
1555 		zend_string_release_ex(string_key, 0);
1556 	} ZEND_HASH_FOREACH_END();
1557 
1558 	SAVE_CURL_ERROR(ch, error);
1559 	if (error != CURLE_OK) {
1560 		return FAILURE;
1561 	}
1562 
1563 	if ((*ch->clone) == 1) {
1564 		zend_llist_clean(&ch->to_free->post);
1565 	}
1566 	zend_llist_add_element(&ch->to_free->post, &mime);
1567 	error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
1568 
1569 	SAVE_CURL_ERROR(ch, error);
1570 	return error == CURLE_OK ? SUCCESS : FAILURE;
1571 }
1572 /* }}} */
1573 
1574 /* {{{ Copy a cURL handle along with all of it's preferences */
PHP_FUNCTION(curl_copy_handle)1575 PHP_FUNCTION(curl_copy_handle)
1576 {
1577 	php_curl	*ch;
1578 	CURL		*cp;
1579 	zval		*zid;
1580 	php_curl	*dupch;
1581 	zval		*postfields;
1582 
1583 	ZEND_PARSE_PARAMETERS_START(1,1)
1584 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
1585 	ZEND_PARSE_PARAMETERS_END();
1586 
1587 	ch = Z_CURL_P(zid);
1588 
1589 	cp = curl_easy_duphandle(ch->cp);
1590 	if (!cp) {
1591 		php_error_docref(NULL, E_WARNING, "Cannot duplicate cURL handle");
1592 		RETURN_FALSE;
1593 	}
1594 
1595 	dupch = init_curl_handle_into_zval(return_value);
1596 	dupch->cp = cp;
1597 
1598 	_php_setup_easy_copy_handlers(dupch, ch);
1599 
1600 	postfields = &ch->postfields;
1601 	if (Z_TYPE_P(postfields) != IS_UNDEF) {
1602 		if (build_mime_structure_from_hash(dupch, postfields) == FAILURE) {
1603 			zval_ptr_dtor(return_value);
1604 			php_error_docref(NULL, E_WARNING, "Cannot rebuild mime structure");
1605 			RETURN_FALSE;
1606 		}
1607 	}
1608 }
1609 /* }}} */
1610 
php_curl_set_callable_handler(zend_fcall_info_cache * const handler_fcc,zval * callable,bool is_array_config,const char * option_name)1611 static bool php_curl_set_callable_handler(zend_fcall_info_cache *const handler_fcc, zval *callable, bool is_array_config, const char *option_name)
1612 {
1613 	if (ZEND_FCC_INITIALIZED(*handler_fcc)) {
1614 		zend_fcc_dtor(handler_fcc);
1615 	}
1616 
1617 	if (Z_TYPE_P(callable) == IS_NULL) {
1618 		return true;
1619 	}
1620 
1621 	char *error = NULL;
1622 	if (UNEXPECTED(!zend_is_callable_ex(callable, /* object */ NULL, /* check_flags */ 0, /* callable_name */ NULL, handler_fcc, /* error */ &error))) {
1623 		if (!EG(exception)) {
1624 			zend_argument_type_error(2 + !is_array_config, "must be a valid callback for option %s, %s", option_name, error);
1625 		}
1626 		efree(error);
1627 		return false;
1628 	}
1629 	zend_fcc_addref(handler_fcc);
1630 	return true;
1631 }
1632 
1633 
1634 #define HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(curl_ptr, constant_no_function, handler_type) \
1635 	case constant_no_function##FUNCTION: { \
1636 		bool result = php_curl_set_callable_handler(&curl_ptr->handlers.handler_type->fcc, zvalue, is_array_config, #constant_no_function "FUNCTION"); \
1637 		if (!result) { \
1638 			return FAILURE; \
1639 		} \
1640 		curl_ptr->handlers.handler_type->method = PHP_CURL_USER; \
1641 		break; \
1642 	}
1643 
1644 #define HANDLE_CURL_OPTION_CALLABLE(curl_ptr, constant_no_function, handler_fcc, c_callback) \
1645 	case constant_no_function##FUNCTION: { \
1646 		bool result = php_curl_set_callable_handler(&curl_ptr->handler_fcc, zvalue, is_array_config, #constant_no_function "FUNCTION"); \
1647 		if (!result) { \
1648 			return FAILURE; \
1649 		} \
1650 		curl_easy_setopt(curl_ptr->cp, constant_no_function##FUNCTION, (c_callback)); \
1651 		curl_easy_setopt(curl_ptr->cp, constant_no_function##DATA, curl_ptr); \
1652 		break; \
1653 	}
1654 
_php_curl_setopt(php_curl * ch,zend_long option,zval * zvalue,bool is_array_config)1655 static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue, bool is_array_config) /* {{{ */
1656 {
1657 	CURLcode error = CURLE_OK;
1658 	zend_long lval;
1659 
1660 	switch (option) {
1661 		/* Callable options */
1662 		HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_WRITE, write);
1663 		HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_HEADER, write_header);
1664 		HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_READ, read);
1665 
1666 		HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_PROGRESS, handlers.progress, curl_progress);
1667 		HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_XFERINFO, handlers.xferinfo, curl_xferinfo);
1668 		HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_FNMATCH_, handlers.fnmatch, curl_fnmatch);
1669 		HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_DEBUG, handlers.debug, curl_debug);
1670 
1671 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1672 		HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_PREREQ, handlers.prereq, curl_prereqfunction);
1673 #endif
1674 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
1675 		HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_SSH_HOSTKEY, handlers.sshhostkey, curl_ssh_hostkeyfunction);
1676 #endif
1677 
1678 		/* Long options */
1679 		case CURLOPT_SSL_VERIFYHOST:
1680 			lval = zval_get_long(zvalue);
1681 			if (lval == 1) {
1682 				php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
1683 				error = curl_easy_setopt(ch->cp, option, 2);
1684 				break;
1685 			}
1686 			ZEND_FALLTHROUGH;
1687 		case CURLOPT_AUTOREFERER:
1688 		case CURLOPT_BUFFERSIZE:
1689 		case CURLOPT_CONNECTTIMEOUT:
1690 		case CURLOPT_COOKIESESSION:
1691 		case CURLOPT_CRLF:
1692 		case CURLOPT_DNS_CACHE_TIMEOUT:
1693 		case CURLOPT_FAILONERROR:
1694 		case CURLOPT_FILETIME:
1695 		case CURLOPT_FORBID_REUSE:
1696 		case CURLOPT_FRESH_CONNECT:
1697 		case CURLOPT_FTP_USE_EPRT:
1698 		case CURLOPT_FTP_USE_EPSV:
1699 		case CURLOPT_HEADER:
1700 		case CURLOPT_HTTPGET:
1701 		case CURLOPT_HTTPPROXYTUNNEL:
1702 		case CURLOPT_HTTP_VERSION:
1703 		case CURLOPT_INFILESIZE:
1704 		case CURLOPT_LOW_SPEED_LIMIT:
1705 		case CURLOPT_LOW_SPEED_TIME:
1706 		case CURLOPT_MAXCONNECTS:
1707 		case CURLOPT_MAXREDIRS:
1708 		case CURLOPT_NETRC:
1709 		case CURLOPT_NOBODY:
1710 		case CURLOPT_NOPROGRESS:
1711 		case CURLOPT_NOSIGNAL:
1712 		case CURLOPT_PORT:
1713 		case CURLOPT_POST:
1714 		case CURLOPT_PROXYPORT:
1715 		case CURLOPT_PROXYTYPE:
1716 		case CURLOPT_PUT:
1717 		case CURLOPT_RESUME_FROM:
1718 		case CURLOPT_SSLVERSION:
1719 		case CURLOPT_SSL_VERIFYPEER:
1720 		case CURLOPT_TIMECONDITION:
1721 		case CURLOPT_TIMEOUT:
1722 		case CURLOPT_TIMEVALUE:
1723 		case CURLOPT_TRANSFERTEXT:
1724 		case CURLOPT_UNRESTRICTED_AUTH:
1725 		case CURLOPT_UPLOAD:
1726 		case CURLOPT_VERBOSE:
1727 		case CURLOPT_HTTPAUTH:
1728 		case CURLOPT_FTP_CREATE_MISSING_DIRS:
1729 		case CURLOPT_PROXYAUTH:
1730 		case CURLOPT_SERVER_RESPONSE_TIMEOUT:
1731 		case CURLOPT_IPRESOLVE:
1732 		case CURLOPT_MAXFILESIZE:
1733 		case CURLOPT_TCP_NODELAY:
1734 		case CURLOPT_FTPSSLAUTH:
1735 		case CURLOPT_IGNORE_CONTENT_LENGTH:
1736 		case CURLOPT_FTP_SKIP_PASV_IP:
1737 		case CURLOPT_FTP_FILEMETHOD:
1738 		case CURLOPT_CONNECT_ONLY:
1739 		case CURLOPT_LOCALPORT:
1740 		case CURLOPT_LOCALPORTRANGE:
1741 		case CURLOPT_SSL_SESSIONID_CACHE:
1742 		case CURLOPT_FTP_SSL_CCC:
1743 		case CURLOPT_SSH_AUTH_TYPES:
1744 		case CURLOPT_CONNECTTIMEOUT_MS:
1745 		case CURLOPT_HTTP_CONTENT_DECODING:
1746 		case CURLOPT_HTTP_TRANSFER_DECODING:
1747 		case CURLOPT_TIMEOUT_MS:
1748 		case CURLOPT_NEW_DIRECTORY_PERMS:
1749 		case CURLOPT_NEW_FILE_PERMS:
1750 		case CURLOPT_USE_SSL:
1751 		case CURLOPT_APPEND:
1752 		case CURLOPT_DIRLISTONLY:
1753 		case CURLOPT_PROXY_TRANSFER_MODE:
1754 		case CURLOPT_ADDRESS_SCOPE:
1755 		case CURLOPT_CERTINFO:
1756 		case CURLOPT_PROTOCOLS:
1757 		case CURLOPT_REDIR_PROTOCOLS:
1758 		case CURLOPT_SOCKS5_GSSAPI_NEC:
1759 		case CURLOPT_TFTP_BLKSIZE:
1760 		case CURLOPT_FTP_USE_PRET:
1761 		case CURLOPT_RTSP_CLIENT_CSEQ:
1762 		case CURLOPT_RTSP_REQUEST:
1763 		case CURLOPT_RTSP_SERVER_CSEQ:
1764 		case CURLOPT_WILDCARDMATCH:
1765 		case CURLOPT_GSSAPI_DELEGATION:
1766 		case CURLOPT_ACCEPTTIMEOUT_MS:
1767 		case CURLOPT_SSL_OPTIONS:
1768 		case CURLOPT_TCP_KEEPALIVE:
1769 		case CURLOPT_TCP_KEEPIDLE:
1770 		case CURLOPT_TCP_KEEPINTVL:
1771 		case CURLOPT_SASL_IR:
1772 		case CURLOPT_EXPECT_100_TIMEOUT_MS:
1773 		case CURLOPT_SSL_ENABLE_ALPN:
1774 		case CURLOPT_SSL_ENABLE_NPN:
1775 		case CURLOPT_HEADEROPT:
1776 		case CURLOPT_SSL_VERIFYSTATUS:
1777 		case CURLOPT_PATH_AS_IS:
1778 		case CURLOPT_SSL_FALSESTART:
1779 		case CURLOPT_PIPEWAIT:
1780 		case CURLOPT_STREAM_WEIGHT:
1781 		case CURLOPT_TFTP_NO_OPTIONS:
1782 		case CURLOPT_TCP_FASTOPEN:
1783 		case CURLOPT_KEEP_SENDING_ON_ERROR:
1784 		case CURLOPT_PROXY_SSL_OPTIONS:
1785 		case CURLOPT_PROXY_SSL_VERIFYHOST:
1786 		case CURLOPT_PROXY_SSL_VERIFYPEER:
1787 		case CURLOPT_PROXY_SSLVERSION:
1788 		case CURLOPT_SUPPRESS_CONNECT_HEADERS:
1789 		case CURLOPT_SOCKS5_AUTH:
1790 		case CURLOPT_SSH_COMPRESSION:
1791 		case CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS:
1792 		case CURLOPT_DNS_SHUFFLE_ADDRESSES:
1793 		case CURLOPT_HAPROXYPROTOCOL:
1794 		case CURLOPT_DISALLOW_USERNAME_IN_URL:
1795 #if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
1796 		case CURLOPT_UPKEEP_INTERVAL_MS:
1797 		case CURLOPT_UPLOAD_BUFFERSIZE:
1798 #endif
1799 #if LIBCURL_VERSION_NUM >= 0x074000 /* Available since 7.64.0 */
1800 		case CURLOPT_HTTP09_ALLOWED:
1801 #endif
1802 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1803 		case CURLOPT_ALTSVC_CTRL:
1804 #endif
1805 #if LIBCURL_VERSION_NUM >= 0x074100 /* Available since 7.65.0 */
1806 		case CURLOPT_MAXAGE_CONN:
1807 #endif
1808 #if LIBCURL_VERSION_NUM >= 0x074500 /* Available since 7.69.0 */
1809 		case CURLOPT_MAIL_RCPT_ALLLOWFAILS:
1810 #endif
1811 #if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
1812 		case CURLOPT_HSTS_CTRL:
1813 #endif
1814 #if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
1815 		case CURLOPT_DOH_SSL_VERIFYHOST:
1816 		case CURLOPT_DOH_SSL_VERIFYPEER:
1817 		case CURLOPT_DOH_SSL_VERIFYSTATUS:
1818 #endif
1819 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1820 		case CURLOPT_MAXLIFETIME_CONN:
1821 #endif
1822 #if LIBCURL_VERSION_NUM >= 0x075100 /* Available since 7.81.0 */
1823 		case CURLOPT_MIME_OPTIONS:
1824 #endif
1825 #if LIBCURL_VERSION_NUM >= 0x075600 /* Available since 7.86.0 */
1826 		case CURLOPT_WS_OPTIONS:
1827 #endif
1828 #if LIBCURL_VERSION_NUM >= 0x075700 /* Available since 7.87.0 */
1829 		case CURLOPT_CA_CACHE_TIMEOUT:
1830 		case CURLOPT_QUICK_EXIT:
1831 #endif
1832 #if LIBCURL_VERSION_NUM >= 0x080900 /* Available since 8.9.0 */
1833 		case CURLOPT_TCP_KEEPCNT:
1834 #endif
1835 			lval = zval_get_long(zvalue);
1836 			if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
1837 				(PG(open_basedir) && *PG(open_basedir)) && (lval & CURLPROTO_FILE)) {
1838 					php_error_docref(NULL, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
1839 					return FAILURE;
1840 			}
1841 			error = curl_easy_setopt(ch->cp, option, lval);
1842 			break;
1843 		case CURLOPT_SAFE_UPLOAD:
1844 			if (!zend_is_true(zvalue)) {
1845 				zend_value_error("%s(): Disabling safe uploads is no longer supported", get_active_function_name());
1846 				return FAILURE;
1847 			}
1848 			break;
1849 
1850 		/* String options */
1851 		case CURLOPT_CAINFO:
1852 		case CURLOPT_CAPATH:
1853 		case CURLOPT_COOKIE:
1854 		case CURLOPT_EGDSOCKET:
1855 		case CURLOPT_INTERFACE:
1856 		case CURLOPT_PROXY:
1857 		case CURLOPT_PROXYUSERPWD:
1858 		case CURLOPT_REFERER:
1859 		case CURLOPT_SSLCERTTYPE:
1860 		case CURLOPT_SSLENGINE:
1861 		case CURLOPT_SSLENGINE_DEFAULT:
1862 		case CURLOPT_SSLKEY:
1863 		case CURLOPT_SSLKEYPASSWD:
1864 		case CURLOPT_SSLKEYTYPE:
1865 		case CURLOPT_SSL_CIPHER_LIST:
1866 		case CURLOPT_USERAGENT:
1867 		case CURLOPT_USERPWD:
1868 		case CURLOPT_COOKIELIST:
1869 		case CURLOPT_FTP_ALTERNATIVE_TO_USER:
1870 		case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
1871 		case CURLOPT_PASSWORD:
1872 		case CURLOPT_PROXYPASSWORD:
1873 		case CURLOPT_PROXYUSERNAME:
1874 		case CURLOPT_USERNAME:
1875 		case CURLOPT_NOPROXY:
1876 		case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1877 		case CURLOPT_MAIL_FROM:
1878 		case CURLOPT_RTSP_STREAM_URI:
1879 		case CURLOPT_RTSP_TRANSPORT:
1880 		case CURLOPT_TLSAUTH_TYPE:
1881 		case CURLOPT_TLSAUTH_PASSWORD:
1882 		case CURLOPT_TLSAUTH_USERNAME:
1883 		case CURLOPT_TRANSFER_ENCODING:
1884 		case CURLOPT_DNS_SERVERS:
1885 		case CURLOPT_MAIL_AUTH:
1886 		case CURLOPT_LOGIN_OPTIONS:
1887 		case CURLOPT_PINNEDPUBLICKEY:
1888 		case CURLOPT_PROXY_SERVICE_NAME:
1889 		case CURLOPT_SERVICE_NAME:
1890 		case CURLOPT_DEFAULT_PROTOCOL:
1891 		case CURLOPT_PRE_PROXY:
1892 		case CURLOPT_PROXY_CAINFO:
1893 		case CURLOPT_PROXY_CAPATH:
1894 		case CURLOPT_PROXY_CRLFILE:
1895 		case CURLOPT_PROXY_KEYPASSWD:
1896 		case CURLOPT_PROXY_PINNEDPUBLICKEY:
1897 		case CURLOPT_PROXY_SSL_CIPHER_LIST:
1898 		case CURLOPT_PROXY_SSLCERT:
1899 		case CURLOPT_PROXY_SSLCERTTYPE:
1900 		case CURLOPT_PROXY_SSLKEY:
1901 		case CURLOPT_PROXY_SSLKEYTYPE:
1902 		case CURLOPT_PROXY_TLSAUTH_PASSWORD:
1903 		case CURLOPT_PROXY_TLSAUTH_TYPE:
1904 		case CURLOPT_PROXY_TLSAUTH_USERNAME:
1905 		case CURLOPT_ABSTRACT_UNIX_SOCKET:
1906 		case CURLOPT_REQUEST_TARGET:
1907 		case CURLOPT_PROXY_TLS13_CIPHERS:
1908 		case CURLOPT_TLS13_CIPHERS:
1909 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1910 		case CURLOPT_ALTSVC:
1911 #endif
1912 #if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
1913 		case CURLOPT_SASL_AUTHZID:
1914 #endif
1915 #if LIBCURL_VERSION_NUM >= 0x074700 /* Available since 7.71.0 */
1916 		case CURLOPT_PROXY_ISSUERCERT:
1917 #endif
1918 #if LIBCURL_VERSION_NUM >= 0x074900 /* Available since 7.73.0 */
1919 		case CURLOPT_SSL_EC_CURVES:
1920 #endif
1921 #if LIBCURL_VERSION_NUM >= 0x074b00 /* Available since 7.75.0 */
1922 		case CURLOPT_AWS_SIGV4:
1923 #endif
1924 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1925 		case CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256:
1926 #endif
1927 #if LIBCURL_VERSION_NUM >= 0x075500 /* Available since 7.85.0 */
1928 		case CURLOPT_PROTOCOLS_STR:
1929 		case CURLOPT_REDIR_PROTOCOLS_STR:
1930 #endif
1931 		{
1932 			zend_string *tmp_str;
1933 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
1934 #if LIBCURL_VERSION_NUM >= 0x075500 /* Available since 7.85.0 */
1935 			if ((option == CURLOPT_PROTOCOLS_STR || option == CURLOPT_REDIR_PROTOCOLS_STR) &&
1936 				(PG(open_basedir) && *PG(open_basedir)) && php_memnistr(ZSTR_VAL(str), "file", sizeof("file") - 1, ZSTR_VAL(str) + ZSTR_LEN(str)) != NULL) {
1937 					php_error_docref(NULL, E_WARNING, "The FILE protocol cannot be activated when an open_basedir is set");
1938 					return FAILURE;
1939 			}
1940 #endif
1941 			zend_result ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str));
1942 			zend_tmp_string_release(tmp_str);
1943 			return ret;
1944 		}
1945 
1946 		/* Curl nullable string options */
1947 		case CURLOPT_CUSTOMREQUEST:
1948 		case CURLOPT_FTPPORT:
1949 		case CURLOPT_RANGE:
1950 		case CURLOPT_FTP_ACCOUNT:
1951 		case CURLOPT_RTSP_SESSION_ID:
1952 		case CURLOPT_ACCEPT_ENCODING:
1953 		case CURLOPT_DNS_INTERFACE:
1954 		case CURLOPT_DNS_LOCAL_IP4:
1955 		case CURLOPT_DNS_LOCAL_IP6:
1956 		case CURLOPT_XOAUTH2_BEARER:
1957 		case CURLOPT_UNIX_SOCKET_PATH:
1958 #if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
1959 		case CURLOPT_DOH_URL:
1960 #endif
1961 #if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
1962 		case CURLOPT_HSTS:
1963 #endif
1964 		case CURLOPT_KRBLEVEL:
1965 		{
1966 			if (Z_ISNULL_P(zvalue)) {
1967 				error = curl_easy_setopt(ch->cp, option, NULL);
1968 			} else {
1969 				zend_string *tmp_str;
1970 				zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
1971 				zend_result ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str));
1972 				zend_tmp_string_release(tmp_str);
1973 				return ret;
1974 			}
1975 			break;
1976 		}
1977 
1978 		/* Curl private option */
1979 		case CURLOPT_PRIVATE:
1980 		{
1981 			zval_ptr_dtor(&ch->private_data);
1982 			ZVAL_COPY(&ch->private_data, zvalue);
1983 			return SUCCESS;
1984 		}
1985 
1986 		/* Curl url option */
1987 		case CURLOPT_URL:
1988 		{
1989 			zend_string *tmp_str;
1990 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
1991 			zend_result ret = php_curl_option_url(ch, str);
1992 			zend_tmp_string_release(tmp_str);
1993 			return ret;
1994 		}
1995 
1996 		/* Curl file handle options */
1997 		case CURLOPT_FILE:
1998 		case CURLOPT_INFILE:
1999 		case CURLOPT_STDERR:
2000 		case CURLOPT_WRITEHEADER: {
2001 			FILE *fp = NULL;
2002 			php_stream *what = NULL;
2003 
2004 			if (Z_TYPE_P(zvalue) != IS_NULL) {
2005 				what = (php_stream *)zend_fetch_resource2_ex(zvalue, "File-Handle", php_file_le_stream(), php_file_le_pstream());
2006 				if (!what) {
2007 					return FAILURE;
2008 				}
2009 
2010 				if (FAILURE == php_stream_cast(what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2011 					return FAILURE;
2012 				}
2013 
2014 				if (!fp) {
2015 					return FAILURE;
2016 				}
2017 			}
2018 
2019 			error = CURLE_OK;
2020 			switch (option) {
2021 				case CURLOPT_FILE:
2022 					if (!what) {
2023 						if (!Z_ISUNDEF(ch->handlers.write->stream)) {
2024 							zval_ptr_dtor(&ch->handlers.write->stream);
2025 							ZVAL_UNDEF(&ch->handlers.write->stream);
2026 						}
2027 						ch->handlers.write->fp = NULL;
2028 						ch->handlers.write->method = PHP_CURL_STDOUT;
2029 					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2030 						zval_ptr_dtor(&ch->handlers.write->stream);
2031 						ch->handlers.write->fp = fp;
2032 						ch->handlers.write->method = PHP_CURL_FILE;
2033 						ZVAL_COPY(&ch->handlers.write->stream, zvalue);
2034 					} else {
2035 						zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
2036 						return FAILURE;
2037 					}
2038 					break;
2039 				case CURLOPT_WRITEHEADER:
2040 					if (!what) {
2041 						if (!Z_ISUNDEF(ch->handlers.write_header->stream)) {
2042 							zval_ptr_dtor(&ch->handlers.write_header->stream);
2043 							ZVAL_UNDEF(&ch->handlers.write_header->stream);
2044 						}
2045 						ch->handlers.write_header->fp = NULL;
2046 						ch->handlers.write_header->method = PHP_CURL_IGNORE;
2047 					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2048 						zval_ptr_dtor(&ch->handlers.write_header->stream);
2049 						ch->handlers.write_header->fp = fp;
2050 						ch->handlers.write_header->method = PHP_CURL_FILE;
2051 						ZVAL_COPY(&ch->handlers.write_header->stream, zvalue);
2052 					} else {
2053 						zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
2054 						return FAILURE;
2055 					}
2056 					break;
2057 				case CURLOPT_INFILE:
2058 					if (!what) {
2059 						if (!Z_ISUNDEF(ch->handlers.read->stream)) {
2060 							zval_ptr_dtor(&ch->handlers.read->stream);
2061 							ZVAL_UNDEF(&ch->handlers.read->stream);
2062 						}
2063 						ch->handlers.read->fp = NULL;
2064 						ch->handlers.read->res = NULL;
2065 					} else {
2066 						zval_ptr_dtor(&ch->handlers.read->stream);
2067 						ch->handlers.read->fp = fp;
2068 						ch->handlers.read->res = Z_RES_P(zvalue);
2069 						ZVAL_COPY(&ch->handlers.read->stream, zvalue);
2070 					}
2071 					break;
2072 				case CURLOPT_STDERR:
2073 					if (!what) {
2074 						if (!Z_ISUNDEF(ch->handlers.std_err)) {
2075 							zval_ptr_dtor(&ch->handlers.std_err);
2076 							ZVAL_UNDEF(&ch->handlers.std_err);
2077 						}
2078 					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2079 						zval_ptr_dtor(&ch->handlers.std_err);
2080 						ZVAL_COPY(&ch->handlers.std_err, zvalue);
2081 					} else {
2082 						zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
2083 						return FAILURE;
2084 					}
2085 					ZEND_FALLTHROUGH;
2086 				default:
2087 					error = curl_easy_setopt(ch->cp, option, fp);
2088 					break;
2089 			}
2090 			break;
2091 		}
2092 
2093 		/* Curl linked list options */
2094 		case CURLOPT_HTTP200ALIASES:
2095 		case CURLOPT_HTTPHEADER:
2096 		case CURLOPT_POSTQUOTE:
2097 		case CURLOPT_PREQUOTE:
2098 		case CURLOPT_QUOTE:
2099 		case CURLOPT_TELNETOPTIONS:
2100 		case CURLOPT_MAIL_RCPT:
2101 		case CURLOPT_RESOLVE:
2102 		case CURLOPT_PROXYHEADER:
2103 		case CURLOPT_CONNECT_TO:
2104 		{
2105 			zval *current;
2106 			HashTable *ph;
2107 			zend_string *val, *tmp_val;
2108 			struct curl_slist *slist = NULL;
2109 
2110 			if (Z_TYPE_P(zvalue) != IS_ARRAY) {
2111 				const char *name = NULL;
2112 				switch (option) {
2113 					case CURLOPT_HTTPHEADER:
2114 						name = "CURLOPT_HTTPHEADER";
2115 						break;
2116 					case CURLOPT_QUOTE:
2117 						name = "CURLOPT_QUOTE";
2118 						break;
2119 					case CURLOPT_HTTP200ALIASES:
2120 						name = "CURLOPT_HTTP200ALIASES";
2121 						break;
2122 					case CURLOPT_POSTQUOTE:
2123 						name = "CURLOPT_POSTQUOTE";
2124 						break;
2125 					case CURLOPT_PREQUOTE:
2126 						name = "CURLOPT_PREQUOTE";
2127 						break;
2128 					case CURLOPT_TELNETOPTIONS:
2129 						name = "CURLOPT_TELNETOPTIONS";
2130 						break;
2131 					case CURLOPT_MAIL_RCPT:
2132 						name = "CURLOPT_MAIL_RCPT";
2133 						break;
2134 					case CURLOPT_RESOLVE:
2135 						name = "CURLOPT_RESOLVE";
2136 						break;
2137 					case CURLOPT_PROXYHEADER:
2138 						name = "CURLOPT_PROXYHEADER";
2139 						break;
2140 					case CURLOPT_CONNECT_TO:
2141 						name = "CURLOPT_CONNECT_TO";
2142 						break;
2143 				}
2144 
2145 				zend_type_error("%s(): The %s option must have an array value", get_active_function_name(), name);
2146 				return FAILURE;
2147 			}
2148 
2149 			ph = Z_ARRVAL_P(zvalue);
2150 			ZEND_HASH_FOREACH_VAL(ph, current) {
2151 				ZVAL_DEREF(current);
2152 				val = zval_get_tmp_string(current, &tmp_val);
2153 				slist = curl_slist_append(slist, ZSTR_VAL(val));
2154 				zend_tmp_string_release(tmp_val);
2155 				if (!slist) {
2156 					php_error_docref(NULL, E_WARNING, "Could not build curl_slist");
2157 					return FAILURE;
2158 				}
2159 			} ZEND_HASH_FOREACH_END();
2160 
2161 			if (slist) {
2162 				if ((*ch->clone) == 1) {
2163 					zend_hash_index_update_ptr(ch->to_free->slist, option, slist);
2164 				} else {
2165 					zend_hash_next_index_insert_ptr(ch->to_free->slist, slist);
2166 				}
2167 			}
2168 
2169 			error = curl_easy_setopt(ch->cp, option, slist);
2170 
2171 			break;
2172 		}
2173 
2174 		case CURLOPT_BINARYTRANSFER:
2175 		case CURLOPT_DNS_USE_GLOBAL_CACHE:
2176 			/* Do nothing, just backward compatibility */
2177 			break;
2178 
2179 		case CURLOPT_FOLLOWLOCATION:
2180 			lval = zend_is_true(zvalue);
2181 			error = curl_easy_setopt(ch->cp, option, lval);
2182 			break;
2183 
2184 		case CURLOPT_POSTFIELDS:
2185 			if (Z_TYPE_P(zvalue) == IS_ARRAY) {
2186 				if (zend_hash_num_elements(HASH_OF(zvalue)) == 0) {
2187 					/* no need to build the mime structure for empty hashtables;
2188 					   also works around https://github.com/curl/curl/issues/6455 */
2189 					curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, "");
2190 					error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, 0);
2191 				} else {
2192 					return build_mime_structure_from_hash(ch, zvalue);
2193 				}
2194 			} else {
2195 				zend_string *tmp_str;
2196 				zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2197 				/* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2198 				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2199 				error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, ZSTR_VAL(str));
2200 				zend_tmp_string_release(tmp_str);
2201 			}
2202 			break;
2203 
2204 		case CURLOPT_RETURNTRANSFER:
2205 			if (zend_is_true(zvalue)) {
2206 				ch->handlers.write->method = PHP_CURL_RETURN;
2207 			} else {
2208 				ch->handlers.write->method = PHP_CURL_STDOUT;
2209 			}
2210 			break;
2211 
2212 		/* Curl off_t options */
2213 		case CURLOPT_MAX_RECV_SPEED_LARGE:
2214 		case CURLOPT_MAX_SEND_SPEED_LARGE:
2215 		case CURLOPT_MAXFILESIZE_LARGE:
2216 		case CURLOPT_TIMEVALUE_LARGE:
2217 			lval = zval_get_long(zvalue);
2218 			error = curl_easy_setopt(ch->cp, option, (curl_off_t)lval);
2219 			break;
2220 
2221 		case CURLOPT_POSTREDIR:
2222 			lval = zval_get_long(zvalue);
2223 			error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, lval & CURL_REDIR_POST_ALL);
2224 			break;
2225 
2226 		/* the following options deal with files, therefore the open_basedir check
2227 		 * is required.
2228 		 */
2229 		case CURLOPT_COOKIEFILE:
2230 		case CURLOPT_COOKIEJAR:
2231 		case CURLOPT_RANDOM_FILE:
2232 		case CURLOPT_SSLCERT:
2233 		case CURLOPT_NETRC_FILE:
2234 		case CURLOPT_SSH_PRIVATE_KEYFILE:
2235 		case CURLOPT_SSH_PUBLIC_KEYFILE:
2236 		case CURLOPT_CRLFILE:
2237 		case CURLOPT_ISSUERCERT:
2238 		case CURLOPT_SSH_KNOWNHOSTS:
2239 		{
2240 			zend_string *tmp_str;
2241 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2242 			zend_result ret;
2243 
2244 			if (ZSTR_LEN(str) && php_check_open_basedir(ZSTR_VAL(str))) {
2245 				zend_tmp_string_release(tmp_str);
2246 				return FAILURE;
2247 			}
2248 
2249 			ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str));
2250 			zend_tmp_string_release(tmp_str);
2251 			return ret;
2252 		}
2253 
2254 		case CURLINFO_HEADER_OUT:
2255 			if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2256                 zend_value_error("CURLINFO_HEADER_OUT option must not be set when the CURLOPT_DEBUGFUNCTION option is set");
2257                 return FAILURE;
2258             }
2259 
2260 			if (zend_is_true(zvalue)) {
2261 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2262 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2263 				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2264 			} else {
2265 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2266 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2267 				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2268 			}
2269 			break;
2270 
2271 		case CURLOPT_SHARE:
2272 			{
2273 				if (Z_TYPE_P(zvalue) == IS_OBJECT && Z_OBJCE_P(zvalue) == curl_share_ce) {
2274 					php_curlsh *sh = Z_CURL_SHARE_P(zvalue);
2275 					curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2276 
2277 					if (ch->share) {
2278 						OBJ_RELEASE(&ch->share->std);
2279 					}
2280 					GC_ADDREF(&sh->std);
2281 					ch->share = sh;
2282 				}
2283 			}
2284 			break;
2285 
2286 		/* Curl blob options */
2287 #if LIBCURL_VERSION_NUM >= 0x074700 /* Available since 7.71.0 */
2288 		case CURLOPT_ISSUERCERT_BLOB:
2289 		case CURLOPT_PROXY_ISSUERCERT_BLOB:
2290 		case CURLOPT_PROXY_SSLCERT_BLOB:
2291 		case CURLOPT_PROXY_SSLKEY_BLOB:
2292 		case CURLOPT_SSLCERT_BLOB:
2293 		case CURLOPT_SSLKEY_BLOB:
2294 #if LIBCURL_VERSION_NUM >= 0x074d00 /* Available since 7.77.0 */
2295 		case CURLOPT_CAINFO_BLOB:
2296 		case CURLOPT_PROXY_CAINFO_BLOB:
2297 #endif
2298 			{
2299 				zend_string *tmp_str;
2300 				zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2301 
2302 				struct curl_blob stblob;
2303 				stblob.data = ZSTR_VAL(str);
2304 				stblob.len = ZSTR_LEN(str);
2305 				stblob.flags = CURL_BLOB_COPY;
2306 				error = curl_easy_setopt(ch->cp, option, &stblob);
2307 
2308 				zend_tmp_string_release(tmp_str);
2309 			}
2310 			break;
2311 #endif
2312 
2313 		default:
2314 			if (is_array_config) {
2315 				zend_argument_value_error(2, "must contain only valid cURL options");
2316 			} else {
2317 				zend_argument_value_error(2, "is not a valid cURL option");
2318 			}
2319 			error = CURLE_UNKNOWN_OPTION;
2320 			break;
2321 	}
2322 
2323 	SAVE_CURL_ERROR(ch, error);
2324 	if (error != CURLE_OK) {
2325 		return FAILURE;
2326 	} else {
2327 		return SUCCESS;
2328 	}
2329 }
2330 /* }}} */
2331 
2332 /* {{{ Set an option for a cURL transfer */
PHP_FUNCTION(curl_setopt)2333 PHP_FUNCTION(curl_setopt)
2334 {
2335 	zval       *zid, *zvalue;
2336 	zend_long        options;
2337 	php_curl   *ch;
2338 
2339 	ZEND_PARSE_PARAMETERS_START(3, 3)
2340 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2341 		Z_PARAM_LONG(options)
2342 		Z_PARAM_ZVAL(zvalue)
2343 	ZEND_PARSE_PARAMETERS_END();
2344 
2345 	ch = Z_CURL_P(zid);
2346 
2347 	if (_php_curl_setopt(ch, options, zvalue, 0) == SUCCESS) {
2348 		RETURN_TRUE;
2349 	} else {
2350 		RETURN_FALSE;
2351 	}
2352 }
2353 /* }}} */
2354 
2355 /* {{{ Set an array of option for a cURL transfer */
PHP_FUNCTION(curl_setopt_array)2356 PHP_FUNCTION(curl_setopt_array)
2357 {
2358 	zval		*zid, *arr, *entry;
2359 	php_curl	*ch;
2360 	zend_ulong	option;
2361 	zend_string	*string_key;
2362 
2363 	ZEND_PARSE_PARAMETERS_START(2, 2)
2364 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2365 		Z_PARAM_ARRAY(arr)
2366 	ZEND_PARSE_PARAMETERS_END();
2367 
2368 	ch = Z_CURL_P(zid);
2369 
2370 	ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
2371 		if (string_key) {
2372 			zend_argument_value_error(2, "contains an invalid cURL option");
2373 			RETURN_THROWS();
2374 		}
2375 
2376 		ZVAL_DEREF(entry);
2377 		if (_php_curl_setopt(ch, (zend_long) option, entry, 1) == FAILURE) {
2378 			RETURN_FALSE;
2379 		}
2380 	} ZEND_HASH_FOREACH_END();
2381 
2382 	RETURN_TRUE;
2383 }
2384 /* }}} */
2385 
2386 /* {{{ _php_curl_cleanup_handle(ch)
2387    Cleanup an execution phase */
_php_curl_cleanup_handle(php_curl * ch)2388 void _php_curl_cleanup_handle(php_curl *ch)
2389 {
2390 	smart_str_free(&ch->handlers.write->buf);
2391 	if (ch->header.str) {
2392 		zend_string_release_ex(ch->header.str, 0);
2393 		ch->header.str = NULL;
2394 	}
2395 
2396 	memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
2397 	ch->err.no = 0;
2398 }
2399 /* }}} */
2400 
2401 /* {{{ Perform a cURL session */
PHP_FUNCTION(curl_exec)2402 PHP_FUNCTION(curl_exec)
2403 {
2404 	CURLcode	error;
2405 	zval		*zid;
2406 	php_curl	*ch;
2407 
2408 	ZEND_PARSE_PARAMETERS_START(1, 1)
2409 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2410 	ZEND_PARSE_PARAMETERS_END();
2411 
2412 	ch = Z_CURL_P(zid);
2413 
2414 	_php_curl_verify_handlers(ch, /* reporterror */ true);
2415 
2416 	_php_curl_cleanup_handle(ch);
2417 
2418 	error = curl_easy_perform(ch->cp);
2419 	SAVE_CURL_ERROR(ch, error);
2420 
2421 	if (error != CURLE_OK) {
2422 		smart_str_free(&ch->handlers.write->buf);
2423 		RETURN_FALSE;
2424 	}
2425 
2426 	if (!Z_ISUNDEF(ch->handlers.std_err)) {
2427 		php_stream  *stream;
2428 		stream = (php_stream*)zend_fetch_resource2_ex(&ch->handlers.std_err, NULL, php_file_le_stream(), php_file_le_pstream());
2429 		if (stream) {
2430 			php_stream_flush(stream);
2431 		}
2432 	}
2433 
2434 	if (ch->handlers.write->method == PHP_CURL_RETURN && ch->handlers.write->buf.s) {
2435 		smart_str_0(&ch->handlers.write->buf);
2436 		RETURN_STR_COPY(ch->handlers.write->buf.s);
2437 	}
2438 
2439 	/* flush the file handle, so any remaining data is synched to disk */
2440 	if (ch->handlers.write->method == PHP_CURL_FILE && ch->handlers.write->fp) {
2441 		fflush(ch->handlers.write->fp);
2442 	}
2443 	if (ch->handlers.write_header->method == PHP_CURL_FILE && ch->handlers.write_header->fp) {
2444 		fflush(ch->handlers.write_header->fp);
2445 	}
2446 
2447 	if (ch->handlers.write->method == PHP_CURL_RETURN) {
2448 		RETURN_EMPTY_STRING();
2449 	} else {
2450 		RETURN_TRUE;
2451 	}
2452 }
2453 /* }}} */
2454 
2455 /* {{{ Get information regarding a specific transfer */
PHP_FUNCTION(curl_getinfo)2456 PHP_FUNCTION(curl_getinfo)
2457 {
2458 	zval		*zid;
2459 	php_curl	*ch;
2460 	zend_long	option;
2461 	bool option_is_null = 1;
2462 
2463 	ZEND_PARSE_PARAMETERS_START(1, 2)
2464 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2465 		Z_PARAM_OPTIONAL
2466 		Z_PARAM_LONG_OR_NULL(option, option_is_null)
2467 	ZEND_PARSE_PARAMETERS_END();
2468 
2469 	ch = Z_CURL_P(zid);
2470 
2471 	if (option_is_null) {
2472 		char *s_code;
2473 		/* libcurl expects long datatype. So far no cases are known where
2474 		   it would be an issue. Using zend_long would truncate a 64-bit
2475 		   var on Win64, so the exact long datatype fits everywhere, as
2476 		   long as there's no 32-bit int overflow. */
2477 		long l_code;
2478 		double d_code;
2479 		struct curl_certinfo *ci = NULL;
2480 		zval listcode;
2481 		curl_off_t co;
2482 
2483 		array_init(return_value);
2484 
2485 		if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
2486 			CAAS("url", s_code);
2487 		}
2488 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
2489 			if (s_code != NULL) {
2490 				CAAS("content_type", s_code);
2491 			} else {
2492 				zval retnull;
2493 				ZVAL_NULL(&retnull);
2494 				CAAZ("content_type", &retnull);
2495 			}
2496 		}
2497 		if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
2498 			CAAL("http_code", l_code);
2499 		}
2500 		if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
2501 			CAAL("header_size", l_code);
2502 		}
2503 		if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
2504 			CAAL("request_size", l_code);
2505 		}
2506 		if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
2507 			CAAL("filetime", l_code);
2508 		}
2509 		if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
2510 			CAAL("ssl_verify_result", l_code);
2511 		}
2512 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
2513 			CAAL("redirect_count", l_code);
2514 		}
2515 		if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
2516 			CAAD("total_time", d_code);
2517 		}
2518 		if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
2519 			CAAD("namelookup_time", d_code);
2520 		}
2521 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
2522 			CAAD("connect_time", d_code);
2523 		}
2524 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
2525 			CAAD("pretransfer_time", d_code);
2526 		}
2527 		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
2528 			CAAD("size_upload", d_code);
2529 		}
2530 		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
2531 			CAAD("size_download", d_code);
2532 		}
2533 		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
2534 			CAAD("speed_download", d_code);
2535 		}
2536 		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
2537 			CAAD("speed_upload", d_code);
2538 		}
2539 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
2540 			CAAD("download_content_length", d_code);
2541 		}
2542 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
2543 			CAAD("upload_content_length", d_code);
2544 		}
2545 		if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
2546 			CAAD("starttransfer_time", d_code);
2547 		}
2548 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
2549 			CAAD("redirect_time", d_code);
2550 		}
2551 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
2552 			CAAS("redirect_url", s_code);
2553 		}
2554 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
2555 			CAAS("primary_ip", s_code);
2556 		}
2557 		if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2558 			array_init(&listcode);
2559 			create_certinfo(ci, &listcode);
2560 			CAAZ("certinfo", &listcode);
2561 		}
2562 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
2563 			CAAL("primary_port", l_code);
2564 		}
2565 		if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
2566 			CAAS("local_ip", s_code);
2567 		}
2568 		if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
2569 			CAAL("local_port", l_code);
2570 		}
2571 		if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_VERSION, &l_code) == CURLE_OK) {
2572 			CAAL("http_version", l_code);
2573 		}
2574 		if (curl_easy_getinfo(ch->cp, CURLINFO_PROTOCOL, &l_code) == CURLE_OK) {
2575 			CAAL("protocol", l_code);
2576 		}
2577 		if (curl_easy_getinfo(ch->cp, CURLINFO_PROXY_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
2578 			CAAL("ssl_verifyresult", l_code);
2579 		}
2580 		if (curl_easy_getinfo(ch->cp, CURLINFO_SCHEME, &s_code) == CURLE_OK) {
2581 			CAAS("scheme", s_code);
2582 		}
2583 		if (curl_easy_getinfo(ch->cp, CURLINFO_APPCONNECT_TIME_T, &co) == CURLE_OK) {
2584 			CAAL("appconnect_time_us", co);
2585 		}
2586 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME_T, &co) == CURLE_OK) {
2587 			CAAL("connect_time_us", co);
2588 		}
2589 		if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME_T, &co) == CURLE_OK) {
2590 			CAAL("namelookup_time_us", co);
2591 		}
2592 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME_T, &co) == CURLE_OK) {
2593 			CAAL("pretransfer_time_us", co);
2594 		}
2595 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME_T, &co) == CURLE_OK) {
2596 			CAAL("redirect_time_us", co);
2597 		}
2598 		if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME_T, &co) == CURLE_OK) {
2599 			CAAL("starttransfer_time_us", co);
2600 		}
2601 #if LIBCURL_VERSION_NUM >= 0x080a00 /* Available since 8.10.0 */
2602 		if (curl_easy_getinfo(ch->cp, CURLINFO_POSTTRANSFER_TIME_T, &co) == CURLE_OK) {
2603 			CAAL("posttransfer_time_us", co);
2604 		}
2605 #endif
2606 		if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME_T, &co) == CURLE_OK) {
2607 			CAAL("total_time_us", co);
2608 		}
2609 		if (ch->header.str) {
2610 			CAASTR("request_header", ch->header.str);
2611 		}
2612 #if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
2613 		if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_METHOD, &s_code) == CURLE_OK) {
2614 			CAAS("effective_method", s_code);
2615 		}
2616 #endif
2617 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
2618 		if (curl_easy_getinfo(ch->cp, CURLINFO_CAPATH, &s_code) == CURLE_OK) {
2619 			CAAS("capath", s_code);
2620 		}
2621 		if (curl_easy_getinfo(ch->cp, CURLINFO_CAINFO, &s_code) == CURLE_OK) {
2622 			CAAS("cainfo", s_code);
2623 		}
2624 #endif
2625 	} else {
2626 		switch (option) {
2627 			case CURLINFO_HEADER_OUT:
2628 				if (ch->header.str) {
2629 					RETURN_STR_COPY(ch->header.str);
2630 				} else {
2631 					RETURN_FALSE;
2632 				}
2633 			case CURLINFO_CERTINFO: {
2634 				struct curl_certinfo *ci = NULL;
2635 
2636 				array_init(return_value);
2637 
2638 				if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2639 					create_certinfo(ci, return_value);
2640 				} else {
2641 					RETURN_FALSE;
2642 				}
2643 				break;
2644 			}
2645 			case CURLINFO_PRIVATE:
2646 				if (!Z_ISUNDEF(ch->private_data)) {
2647 					RETURN_COPY(&ch->private_data);
2648 				} else {
2649 					RETURN_FALSE;
2650 				}
2651 				break;
2652 			default: {
2653 				int type = CURLINFO_TYPEMASK & option;
2654 				switch (type) {
2655 					case CURLINFO_STRING:
2656 					{
2657 						char *s_code = NULL;
2658 
2659 						if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
2660 							RETURN_STRING(s_code);
2661 						} else {
2662 							RETURN_FALSE;
2663 						}
2664 						break;
2665 					}
2666 					case CURLINFO_LONG:
2667 					{
2668 						zend_long code = 0;
2669 
2670 						if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
2671 							RETURN_LONG(code);
2672 						} else {
2673 							RETURN_FALSE;
2674 						}
2675 						break;
2676 					}
2677 					case CURLINFO_DOUBLE:
2678 					{
2679 						double code = 0.0;
2680 
2681 						if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
2682 							RETURN_DOUBLE(code);
2683 						} else {
2684 							RETURN_FALSE;
2685 						}
2686 						break;
2687 					}
2688 					case CURLINFO_SLIST:
2689 					{
2690 						struct curl_slist *slist;
2691 						if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
2692 							struct curl_slist *current = slist;
2693 							array_init(return_value);
2694 							while (current) {
2695 								add_next_index_string(return_value, current->data);
2696 								current = current->next;
2697 							}
2698 							curl_slist_free_all(slist);
2699 						} else {
2700 							RETURN_FALSE;
2701 						}
2702 						break;
2703 					}
2704 					case CURLINFO_OFF_T:
2705 					{
2706 						curl_off_t c_off;
2707 						if (curl_easy_getinfo(ch->cp, option, &c_off) == CURLE_OK) {
2708 							RETURN_LONG((long) c_off);
2709 						} else {
2710 							RETURN_FALSE;
2711 						}
2712 						break;
2713 					}
2714 					default:
2715 						RETURN_FALSE;
2716 				}
2717 			}
2718 		}
2719 	}
2720 }
2721 /* }}} */
2722 
2723 /* {{{ Return a string contain the last error for the current session */
PHP_FUNCTION(curl_error)2724 PHP_FUNCTION(curl_error)
2725 {
2726 	zval		*zid;
2727 	php_curl	*ch;
2728 
2729 	ZEND_PARSE_PARAMETERS_START(1, 1)
2730 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2731 	ZEND_PARSE_PARAMETERS_END();
2732 
2733 	ch = Z_CURL_P(zid);
2734 
2735 	if (ch->err.no) {
2736 		ch->err.str[CURL_ERROR_SIZE] = 0;
2737 		if (strlen(ch->err.str) > 0) {
2738 			RETURN_STRING(ch->err.str);
2739 		} else {
2740 			RETURN_STRING(curl_easy_strerror(ch->err.no));
2741 		}
2742 	} else {
2743 		RETURN_EMPTY_STRING();
2744 	}
2745 }
2746 /* }}} */
2747 
2748 /* {{{ Return an integer containing the last error number */
PHP_FUNCTION(curl_errno)2749 PHP_FUNCTION(curl_errno)
2750 {
2751 	zval		*zid;
2752 	php_curl	*ch;
2753 
2754 	ZEND_PARSE_PARAMETERS_START(1,1)
2755 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2756 	ZEND_PARSE_PARAMETERS_END();
2757 
2758 	ch = Z_CURL_P(zid);
2759 
2760 	RETURN_LONG(ch->err.no);
2761 }
2762 /* }}} */
2763 
2764 /* {{{ Close a cURL session */
PHP_FUNCTION(curl_close)2765 PHP_FUNCTION(curl_close)
2766 {
2767 	zval		*zid;
2768 	php_curl	*ch;
2769 
2770 	ZEND_PARSE_PARAMETERS_START(1, 1)
2771 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2772 	ZEND_PARSE_PARAMETERS_END();
2773 
2774 	ch = Z_CURL_P(zid);
2775 
2776 	if (ch->in_callback) {
2777 		zend_throw_error(NULL, "%s(): Attempt to close cURL handle from a callback", get_active_function_name());
2778 		RETURN_THROWS();
2779 	}
2780 }
2781 /* }}} */
2782 
curl_free_obj(zend_object * object)2783 static void curl_free_obj(zend_object *object)
2784 {
2785 	php_curl *ch = curl_from_obj(object);
2786 
2787 #if PHP_CURL_DEBUG
2788 	fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
2789 #endif
2790 
2791 	if (!ch->cp) {
2792 		/* Can happen if constructor throws. */
2793 		zend_object_std_dtor(&ch->std);
2794 		return;
2795 	}
2796 
2797 	_php_curl_verify_handlers(ch, /* reporterror */ false);
2798 
2799 	curl_easy_cleanup(ch->cp);
2800 
2801 	/* cURL destructors should be invoked only by last curl handle */
2802 	if (--(*ch->clone) == 0) {
2803 		zend_llist_clean(&ch->to_free->post);
2804 		zend_llist_clean(&ch->to_free->stream);
2805 
2806 		zend_hash_destroy(ch->to_free->slist);
2807 		efree(ch->to_free->slist);
2808 		efree(ch->to_free);
2809 		efree(ch->clone);
2810 	}
2811 
2812 	smart_str_free(&ch->handlers.write->buf);
2813 	if (ZEND_FCC_INITIALIZED(ch->handlers.write->fcc)) {
2814 		zend_fcc_dtor(&ch->handlers.write->fcc);
2815 	}
2816 	if (ZEND_FCC_INITIALIZED(ch->handlers.write_header->fcc)) {
2817 		zend_fcc_dtor(&ch->handlers.write_header->fcc);
2818 	}
2819 	if (ZEND_FCC_INITIALIZED(ch->handlers.read->fcc)) {
2820 		zend_fcc_dtor(&ch->handlers.read->fcc);
2821 	}
2822 	zval_ptr_dtor(&ch->handlers.std_err);
2823 	if (ch->header.str) {
2824 		zend_string_release_ex(ch->header.str, 0);
2825 	}
2826 
2827 	zval_ptr_dtor(&ch->handlers.write_header->stream);
2828 	zval_ptr_dtor(&ch->handlers.write->stream);
2829 	zval_ptr_dtor(&ch->handlers.read->stream);
2830 
2831 	efree(ch->handlers.write);
2832 	efree(ch->handlers.write_header);
2833 	efree(ch->handlers.read);
2834 
2835 	if (ZEND_FCC_INITIALIZED(ch->handlers.progress)) {
2836 		zend_fcc_dtor(&ch->handlers.progress);
2837 	}
2838 	if (ZEND_FCC_INITIALIZED(ch->handlers.xferinfo)) {
2839 		zend_fcc_dtor(&ch->handlers.xferinfo);
2840 	}
2841 	if (ZEND_FCC_INITIALIZED(ch->handlers.fnmatch)) {
2842 		zend_fcc_dtor(&ch->handlers.fnmatch);
2843 	}
2844 	if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2845 		zend_fcc_dtor(&ch->handlers.debug);
2846 	}
2847 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
2848 	if (ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
2849 		zend_fcc_dtor(&ch->handlers.prereq);
2850 	}
2851 #endif
2852 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
2853 	if (ZEND_FCC_INITIALIZED(ch->handlers.sshhostkey)) {
2854 		zend_fcc_dtor(&ch->handlers.sshhostkey);
2855 	}
2856 #endif
2857 
2858 	zval_ptr_dtor(&ch->postfields);
2859 	zval_ptr_dtor(&ch->private_data);
2860 
2861 	if (ch->share) {
2862 		OBJ_RELEASE(&ch->share->std);
2863 	}
2864 
2865 	zend_object_std_dtor(&ch->std);
2866 }
2867 /* }}} */
2868 
2869 /* {{{ return string describing error code */
PHP_FUNCTION(curl_strerror)2870 PHP_FUNCTION(curl_strerror)
2871 {
2872 	zend_long code;
2873 	const char *str;
2874 
2875 	ZEND_PARSE_PARAMETERS_START(1, 1)
2876 		Z_PARAM_LONG(code)
2877 	ZEND_PARSE_PARAMETERS_END();
2878 
2879 	str = curl_easy_strerror(code);
2880 	if (str) {
2881 		RETURN_STRING(str);
2882 	} else {
2883 		RETURN_NULL();
2884 	}
2885 }
2886 /* }}} */
2887 
2888 /* {{{ _php_curl_reset_handlers()
2889    Reset all handlers of a given php_curl */
_php_curl_reset_handlers(php_curl * ch)2890 static void _php_curl_reset_handlers(php_curl *ch)
2891 {
2892 	if (!Z_ISUNDEF(ch->handlers.write->stream)) {
2893 		zval_ptr_dtor(&ch->handlers.write->stream);
2894 		ZVAL_UNDEF(&ch->handlers.write->stream);
2895 	}
2896 	ch->handlers.write->fp = NULL;
2897 	ch->handlers.write->method = PHP_CURL_STDOUT;
2898 
2899 	if (!Z_ISUNDEF(ch->handlers.write_header->stream)) {
2900 		zval_ptr_dtor(&ch->handlers.write_header->stream);
2901 		ZVAL_UNDEF(&ch->handlers.write_header->stream);
2902 	}
2903 	ch->handlers.write_header->fp = NULL;
2904 	ch->handlers.write_header->method = PHP_CURL_IGNORE;
2905 
2906 	if (!Z_ISUNDEF(ch->handlers.read->stream)) {
2907 		zval_ptr_dtor(&ch->handlers.read->stream);
2908 		ZVAL_UNDEF(&ch->handlers.read->stream);
2909 	}
2910 	ch->handlers.read->fp = NULL;
2911 	ch->handlers.read->res = NULL;
2912 	ch->handlers.read->method  = PHP_CURL_DIRECT;
2913 
2914 	if (!Z_ISUNDEF(ch->handlers.std_err)) {
2915 		zval_ptr_dtor(&ch->handlers.std_err);
2916 		ZVAL_UNDEF(&ch->handlers.std_err);
2917 	}
2918 
2919 	if (ZEND_FCC_INITIALIZED(ch->handlers.progress)) {
2920 		zend_fcc_dtor(&ch->handlers.progress);
2921 	}
2922 
2923 	if (ZEND_FCC_INITIALIZED(ch->handlers.xferinfo)) {
2924 		zend_fcc_dtor(&ch->handlers.xferinfo);
2925 	}
2926 
2927 	if (ZEND_FCC_INITIALIZED(ch->handlers.fnmatch)) {
2928 		zend_fcc_dtor(&ch->handlers.fnmatch);
2929 	}
2930 
2931 	if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2932 		zend_fcc_dtor(&ch->handlers.debug);
2933 	}
2934 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
2935 	if (ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
2936 		zend_fcc_dtor(&ch->handlers.prereq);
2937 	}
2938 #endif
2939 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
2940 	if (ZEND_FCC_INITIALIZED(ch->handlers.sshhostkey)) {
2941 		zend_fcc_dtor(&ch->handlers.sshhostkey);
2942 	}
2943 #endif
2944 }
2945 /* }}} */
2946 
2947 /* {{{ Reset all options of a libcurl session handle */
PHP_FUNCTION(curl_reset)2948 PHP_FUNCTION(curl_reset)
2949 {
2950 	zval       *zid;
2951 	php_curl   *ch;
2952 
2953 	ZEND_PARSE_PARAMETERS_START(1, 1)
2954 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2955 	ZEND_PARSE_PARAMETERS_END();
2956 
2957 	ch = Z_CURL_P(zid);
2958 
2959 	if (ch->in_callback) {
2960 		zend_throw_error(NULL, "%s(): Attempt to reset cURL handle from a callback", get_active_function_name());
2961 		RETURN_THROWS();
2962 	}
2963 
2964 	curl_easy_reset(ch->cp);
2965 	_php_curl_reset_handlers(ch);
2966 	_php_curl_set_default_options(ch);
2967 }
2968 /* }}} */
2969 
2970 /* {{{ URL encodes the given string */
PHP_FUNCTION(curl_escape)2971 PHP_FUNCTION(curl_escape)
2972 {
2973 	zend_string *str;
2974 	char        *res;
2975 	zval        *zid;
2976 	php_curl    *ch;
2977 
2978 	ZEND_PARSE_PARAMETERS_START(2,2)
2979 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2980 		Z_PARAM_STR(str)
2981 	ZEND_PARSE_PARAMETERS_END();
2982 
2983 	ch = Z_CURL_P(zid);
2984 
2985 	if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
2986 		RETURN_FALSE;
2987 	}
2988 
2989 	if ((res = curl_easy_escape(ch->cp, ZSTR_VAL(str), ZSTR_LEN(str)))) {
2990 		RETVAL_STRING(res);
2991 		curl_free(res);
2992 	} else {
2993 		RETURN_FALSE;
2994 	}
2995 }
2996 /* }}} */
2997 
2998 /* {{{ URL decodes the given string */
PHP_FUNCTION(curl_unescape)2999 PHP_FUNCTION(curl_unescape)
3000 {
3001 	char        *out = NULL;
3002 	int          out_len;
3003 	zval        *zid;
3004 	zend_string *str;
3005 	php_curl    *ch;
3006 
3007 	ZEND_PARSE_PARAMETERS_START(2,2)
3008 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3009 		Z_PARAM_STR(str)
3010 	ZEND_PARSE_PARAMETERS_END();
3011 
3012 	ch = Z_CURL_P(zid);
3013 
3014 	if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
3015 		RETURN_FALSE;
3016 	}
3017 
3018 	if ((out = curl_easy_unescape(ch->cp, ZSTR_VAL(str), ZSTR_LEN(str), &out_len))) {
3019 		RETVAL_STRINGL(out, out_len);
3020 		curl_free(out);
3021 	} else {
3022 		RETURN_FALSE;
3023 	}
3024 }
3025 /* }}} */
3026 
3027 /* {{{ pause and unpause a connection */
PHP_FUNCTION(curl_pause)3028 PHP_FUNCTION(curl_pause)
3029 {
3030 	zend_long       bitmask;
3031 	zval       *zid;
3032 	php_curl   *ch;
3033 
3034 	ZEND_PARSE_PARAMETERS_START(2,2)
3035 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3036 		Z_PARAM_LONG(bitmask)
3037 	ZEND_PARSE_PARAMETERS_END();
3038 
3039 	ch = Z_CURL_P(zid);
3040 
3041 	RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3042 }
3043 /* }}} */
3044 
3045 #if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
3046 /* {{{ perform connection upkeep checks */
PHP_FUNCTION(curl_upkeep)3047 PHP_FUNCTION(curl_upkeep)
3048 {
3049 	CURLcode	error;
3050 	zval		*zid;
3051 	php_curl	*ch;
3052 
3053 	ZEND_PARSE_PARAMETERS_START(1, 1)
3054 		Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3055 	ZEND_PARSE_PARAMETERS_END();
3056 
3057 	ch = Z_CURL_P(zid);
3058 
3059 	error = curl_easy_upkeep(ch->cp);
3060 	SAVE_CURL_ERROR(ch, error);
3061 
3062 	RETURN_BOOL(error == CURLE_OK);
3063 }
3064 /*}}} */
3065 #endif
3066