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 compatibility */
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 zend_hash_init(&ch->to_free->slist, 4, NULL, curl_free_slist, 0);
1147 ZVAL_UNDEF(&ch->postfields);
1148 }
1149
1150 /* }}} */
1151
1152 /* {{{ create_certinfo */
create_certinfo(struct curl_certinfo * ci,zval * listcode)1153 static void create_certinfo(struct curl_certinfo *ci, zval *listcode)
1154 {
1155 int i;
1156
1157 if (ci) {
1158 zval certhash;
1159
1160 for (i=0; i<ci->num_of_certs; i++) {
1161 struct curl_slist *slist;
1162
1163 array_init(&certhash);
1164 for (slist = ci->certinfo[i]; slist; slist = slist->next) {
1165 int len;
1166 char s[64];
1167 char *tmp;
1168 strncpy(s, slist->data, sizeof(s));
1169 s[sizeof(s)-1] = '\0';
1170 tmp = memchr(s, ':', sizeof(s));
1171 if(tmp) {
1172 *tmp = '\0';
1173 len = strlen(s);
1174 add_assoc_string(&certhash, s, &slist->data[len+1]);
1175 } else {
1176 php_error_docref(NULL, E_WARNING, "Could not extract hash key from certificate info");
1177 }
1178 }
1179 add_next_index_zval(listcode, &certhash);
1180 }
1181 }
1182 }
1183 /* }}} */
1184
1185 /* {{{ _php_curl_set_default_options()
1186 Set default options for a handle */
_php_curl_set_default_options(php_curl * ch)1187 static void _php_curl_set_default_options(php_curl *ch)
1188 {
1189 char *cainfo;
1190
1191 curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS, 1);
1192 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
1193 curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER, ch->err.str);
1194 curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write);
1195 curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
1196 curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION, curl_read);
1197 curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
1198 curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_header);
1199 curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
1200 curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1201 curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1202
1203 cainfo = INI_STR("openssl.cafile");
1204 if (!(cainfo && cainfo[0] != '\0')) {
1205 cainfo = INI_STR("curl.cainfo");
1206 }
1207 if (cainfo && cainfo[0] != '\0') {
1208 curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1209 }
1210
1211 #ifdef ZTS
1212 curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1213 #endif
1214 }
1215 /* }}} */
1216
1217 /* {{{ Initialize a cURL session */
PHP_FUNCTION(curl_init)1218 PHP_FUNCTION(curl_init)
1219 {
1220 php_curl *ch;
1221 CURL *cp;
1222 zend_string *url = NULL;
1223
1224 ZEND_PARSE_PARAMETERS_START(0,1)
1225 Z_PARAM_OPTIONAL
1226 Z_PARAM_STR_OR_NULL(url)
1227 ZEND_PARSE_PARAMETERS_END();
1228
1229 cp = curl_easy_init();
1230 if (!cp) {
1231 php_error_docref(NULL, E_WARNING, "Could not initialize a new cURL handle");
1232 RETURN_FALSE;
1233 }
1234
1235 ch = init_curl_handle_into_zval(return_value);
1236
1237 ch->cp = cp;
1238
1239 ch->handlers.write->method = PHP_CURL_STDOUT;
1240 ch->handlers.read->method = PHP_CURL_DIRECT;
1241 ch->handlers.write_header->method = PHP_CURL_IGNORE;
1242
1243 _php_curl_set_default_options(ch);
1244
1245 if (url) {
1246 if (php_curl_option_url(ch, url) == FAILURE) {
1247 zval_ptr_dtor(return_value);
1248 RETURN_FALSE;
1249 }
1250 }
1251 }
1252 /* }}} */
1253
php_curl_copy_fcc_with_option(php_curl * ch,CURLoption option,zend_fcall_info_cache * target_fcc,zend_fcall_info_cache * source_fcc)1254 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)
1255 {
1256 if (ZEND_FCC_INITIALIZED(*source_fcc)) {
1257 zend_fcc_dup(target_fcc, source_fcc);
1258 curl_easy_setopt(ch->cp, option, (void *) ch);
1259 }
1260 }
1261
_php_setup_easy_copy_handlers(php_curl * ch,php_curl * source)1262 void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
1263 {
1264 if (!Z_ISUNDEF(source->handlers.write->stream)) {
1265 Z_ADDREF(source->handlers.write->stream);
1266 }
1267 ch->handlers.write->stream = source->handlers.write->stream;
1268 ch->handlers.write->method = source->handlers.write->method;
1269 if (!Z_ISUNDEF(source->handlers.read->stream)) {
1270 Z_ADDREF(source->handlers.read->stream);
1271 }
1272 ch->handlers.read->stream = source->handlers.read->stream;
1273 ch->handlers.read->method = source->handlers.read->method;
1274 ch->handlers.write_header->method = source->handlers.write_header->method;
1275 if (!Z_ISUNDEF(source->handlers.write_header->stream)) {
1276 Z_ADDREF(source->handlers.write_header->stream);
1277 }
1278 ch->handlers.write_header->stream = source->handlers.write_header->stream;
1279
1280 ch->handlers.write->fp = source->handlers.write->fp;
1281 ch->handlers.write_header->fp = source->handlers.write_header->fp;
1282 ch->handlers.read->fp = source->handlers.read->fp;
1283 ch->handlers.read->res = source->handlers.read->res;
1284
1285 if (ZEND_FCC_INITIALIZED(source->handlers.read->fcc)) {
1286 zend_fcc_dup(&ch->handlers.read->fcc, &source->handlers.read->fcc);
1287 }
1288 if (ZEND_FCC_INITIALIZED(source->handlers.write->fcc)) {
1289 zend_fcc_dup(&ch->handlers.write->fcc, &source->handlers.write->fcc);
1290 }
1291 if (ZEND_FCC_INITIALIZED(source->handlers.write_header->fcc)) {
1292 zend_fcc_dup(&ch->handlers.write_header->fcc, &source->handlers.write_header->fcc);
1293 }
1294
1295 curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER, ch->err.str);
1296 curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
1297 curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
1298 curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
1299 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *) ch);
1300
1301 php_curl_copy_fcc_with_option(ch, CURLOPT_PROGRESSDATA, &ch->handlers.progress, &source->handlers.progress);
1302 php_curl_copy_fcc_with_option(ch, CURLOPT_XFERINFODATA, &ch->handlers.xferinfo, &source->handlers.xferinfo);
1303 php_curl_copy_fcc_with_option(ch, CURLOPT_FNMATCH_DATA, &ch->handlers.fnmatch, &source->handlers.fnmatch);
1304 php_curl_copy_fcc_with_option(ch, CURLOPT_DEBUGDATA, &ch->handlers.debug, &source->handlers.debug);
1305 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1306 php_curl_copy_fcc_with_option(ch, CURLOPT_PREREQDATA, &ch->handlers.prereq, &source->handlers.prereq);
1307 #endif
1308 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
1309 php_curl_copy_fcc_with_option(ch, CURLOPT_SSH_HOSTKEYDATA, &ch->handlers.sshhostkey, &source->handlers.sshhostkey);
1310 #endif
1311
1312 ZVAL_COPY(&ch->private_data, &source->private_data);
1313
1314 efree(ch->to_free);
1315 ch->to_free = source->to_free;
1316 efree(ch->clone);
1317 ch->clone = source->clone;
1318
1319 /* Keep track of cloned copies to avoid invoking curl destructors for every clone */
1320 (*source->clone)++;
1321 }
1322
read_cb(char * buffer,size_t size,size_t nitems,void * arg)1323 static size_t read_cb(char *buffer, size_t size, size_t nitems, void *arg) /* {{{ */
1324 {
1325 struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
1326 ssize_t numread;
1327
1328 if (cb_arg->stream == NULL) {
1329 if (!(cb_arg->stream = php_stream_open_wrapper(ZSTR_VAL(cb_arg->filename), "rb", IGNORE_PATH, NULL))) {
1330 return CURL_READFUNC_ABORT;
1331 }
1332 }
1333 numread = php_stream_read(cb_arg->stream, buffer, nitems * size);
1334 if (numread < 0) {
1335 php_stream_close(cb_arg->stream);
1336 cb_arg->stream = NULL;
1337 return CURL_READFUNC_ABORT;
1338 }
1339 return numread;
1340 }
1341 /* }}} */
1342
seek_cb(void * arg,curl_off_t offset,int origin)1343 static int seek_cb(void *arg, curl_off_t offset, int origin) /* {{{ */
1344 {
1345 struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
1346 int res;
1347
1348 if (cb_arg->stream == NULL) {
1349 return CURL_SEEKFUNC_CANTSEEK;
1350 }
1351 res = php_stream_seek(cb_arg->stream, offset, origin);
1352 return res == SUCCESS ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK;
1353 }
1354 /* }}} */
1355
free_cb(void * arg)1356 static void free_cb(void *arg) /* {{{ */
1357 {
1358 struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
1359
1360 if (cb_arg->stream != NULL) {
1361 php_stream_close(cb_arg->stream);
1362 cb_arg->stream = NULL;
1363 }
1364 }
1365 /* }}} */
1366
add_simple_field(curl_mime * mime,zend_string * string_key,zval * current)1367 static inline CURLcode add_simple_field(curl_mime *mime, zend_string *string_key, zval *current)
1368 {
1369 CURLcode error = CURLE_OK;
1370 curl_mimepart *part;
1371 CURLcode form_error;
1372 zend_string *postval, *tmp_postval;
1373
1374 postval = zval_get_tmp_string(current, &tmp_postval);
1375
1376 part = curl_mime_addpart(mime);
1377 if (part == NULL) {
1378 zend_tmp_string_release(tmp_postval);
1379 zend_string_release_ex(string_key, 0);
1380 return CURLE_OUT_OF_MEMORY;
1381 }
1382 if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
1383 || (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
1384 error = form_error;
1385 }
1386
1387 zend_tmp_string_release(tmp_postval);
1388
1389 return error;
1390 }
1391
build_mime_structure_from_hash(php_curl * ch,zval * zpostfields)1392 static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpostfields) /* {{{ */
1393 {
1394 HashTable *postfields = Z_ARRVAL_P(zpostfields);
1395 CURLcode error = CURLE_OK;
1396 zval *current;
1397 zend_string *string_key;
1398 zend_ulong num_key;
1399 curl_mime *mime = NULL;
1400 curl_mimepart *part;
1401 CURLcode form_error;
1402
1403 if (zend_hash_num_elements(postfields) > 0) {
1404 mime = curl_mime_init(ch->cp);
1405 if (mime == NULL) {
1406 return FAILURE;
1407 }
1408 }
1409
1410 ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
1411 zend_string *postval;
1412 /* Pretend we have a string_key here */
1413 if (!string_key) {
1414 string_key = zend_long_to_str(num_key);
1415 } else {
1416 zend_string_addref(string_key);
1417 }
1418
1419 ZVAL_DEREF(current);
1420 if (Z_TYPE_P(current) == IS_OBJECT &&
1421 instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
1422 /* new-style file upload */
1423 zval *prop, rv;
1424 char *type = NULL, *filename = NULL;
1425 struct mime_data_cb_arg *cb_arg;
1426 php_stream *stream;
1427 php_stream_statbuf ssb;
1428 size_t filesize = -1;
1429 curl_seek_callback seekfunc = seek_cb;
1430
1431 prop = zend_read_property_ex(curl_CURLFile_class, Z_OBJ_P(current), ZSTR_KNOWN(ZEND_STR_NAME), /* silent */ false, &rv);
1432 ZVAL_DEREF(prop);
1433 if (Z_TYPE_P(prop) != IS_STRING) {
1434 php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
1435 } else {
1436 postval = Z_STR_P(prop);
1437
1438 if (php_check_open_basedir(ZSTR_VAL(postval))) {
1439 return FAILURE;
1440 }
1441
1442 prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
1443 ZVAL_DEREF(prop);
1444 if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
1445 type = Z_STRVAL_P(prop);
1446 }
1447 prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
1448 ZVAL_DEREF(prop);
1449 if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
1450 filename = Z_STRVAL_P(prop);
1451 }
1452
1453 zval_ptr_dtor(&ch->postfields);
1454 ZVAL_COPY(&ch->postfields, zpostfields);
1455
1456 if ((stream = php_stream_open_wrapper(ZSTR_VAL(postval), "rb", STREAM_MUST_SEEK, NULL))) {
1457 if (!stream->readfilters.head && !php_stream_stat(stream, &ssb)) {
1458 filesize = ssb.sb.st_size;
1459 }
1460 } else {
1461 seekfunc = NULL;
1462 }
1463
1464 cb_arg = emalloc(sizeof *cb_arg);
1465 cb_arg->filename = zend_string_copy(postval);
1466 cb_arg->stream = stream;
1467
1468 part = curl_mime_addpart(mime);
1469 if (part == NULL) {
1470 zend_string_release_ex(string_key, 0);
1471 return FAILURE;
1472 }
1473 if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
1474 || (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
1475 || (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
1476 || (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
1477 error = form_error;
1478 }
1479 zend_llist_add_element(&ch->to_free->stream, &cb_arg);
1480 }
1481
1482 zend_string_release_ex(string_key, 0);
1483 continue;
1484 }
1485
1486 if (Z_TYPE_P(current) == IS_OBJECT && instanceof_function(Z_OBJCE_P(current), curl_CURLStringFile_class)) {
1487 /* new-style file upload from string */
1488 zval *prop, rv;
1489 char *type = NULL, *filename = NULL;
1490
1491 prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
1492 if (EG(exception)) {
1493 zend_string_release_ex(string_key, 0);
1494 return FAILURE;
1495 }
1496 ZVAL_DEREF(prop);
1497 ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
1498
1499 filename = Z_STRVAL_P(prop);
1500
1501 prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
1502 if (EG(exception)) {
1503 zend_string_release_ex(string_key, 0);
1504 return FAILURE;
1505 }
1506 ZVAL_DEREF(prop);
1507 ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
1508
1509 type = Z_STRVAL_P(prop);
1510
1511 prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
1512 if (EG(exception)) {
1513 zend_string_release_ex(string_key, 0);
1514 return FAILURE;
1515 }
1516 ZVAL_DEREF(prop);
1517 ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
1518
1519 postval = Z_STR_P(prop);
1520
1521 zval_ptr_dtor(&ch->postfields);
1522 ZVAL_COPY(&ch->postfields, zpostfields);
1523
1524 part = curl_mime_addpart(mime);
1525 if (part == NULL) {
1526 zend_string_release_ex(string_key, 0);
1527 return FAILURE;
1528 }
1529 if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
1530 || (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
1531 || (form_error = curl_mime_filename(part, filename)) != CURLE_OK
1532 || (form_error = curl_mime_type(part, type)) != CURLE_OK) {
1533 error = form_error;
1534 }
1535
1536 zend_string_release_ex(string_key, 0);
1537 continue;
1538 }
1539
1540 if (Z_TYPE_P(current) == IS_ARRAY) {
1541 zval *current_element;
1542
1543 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(current), current_element) {
1544 add_simple_field(mime, string_key, current_element);
1545 } ZEND_HASH_FOREACH_END();
1546
1547 zend_string_release_ex(string_key, 0);
1548 continue;
1549 }
1550
1551 add_simple_field(mime, string_key, current);
1552
1553 zend_string_release_ex(string_key, 0);
1554 } ZEND_HASH_FOREACH_END();
1555
1556 SAVE_CURL_ERROR(ch, error);
1557 if (error != CURLE_OK) {
1558 return FAILURE;
1559 }
1560
1561 if ((*ch->clone) == 1) {
1562 zend_llist_clean(&ch->to_free->post);
1563 }
1564 zend_llist_add_element(&ch->to_free->post, &mime);
1565 error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
1566
1567 SAVE_CURL_ERROR(ch, error);
1568 return error == CURLE_OK ? SUCCESS : FAILURE;
1569 }
1570 /* }}} */
1571
1572 /* {{{ Copy a cURL handle along with all of it's preferences */
PHP_FUNCTION(curl_copy_handle)1573 PHP_FUNCTION(curl_copy_handle)
1574 {
1575 php_curl *ch;
1576 CURL *cp;
1577 zval *zid;
1578 php_curl *dupch;
1579 zval *postfields;
1580
1581 ZEND_PARSE_PARAMETERS_START(1,1)
1582 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
1583 ZEND_PARSE_PARAMETERS_END();
1584
1585 ch = Z_CURL_P(zid);
1586
1587 cp = curl_easy_duphandle(ch->cp);
1588 if (!cp) {
1589 php_error_docref(NULL, E_WARNING, "Cannot duplicate cURL handle");
1590 RETURN_FALSE;
1591 }
1592
1593 dupch = init_curl_handle_into_zval(return_value);
1594 dupch->cp = cp;
1595
1596 _php_setup_easy_copy_handlers(dupch, ch);
1597
1598 postfields = &ch->postfields;
1599 if (Z_TYPE_P(postfields) != IS_UNDEF) {
1600 if (build_mime_structure_from_hash(dupch, postfields) == FAILURE) {
1601 zval_ptr_dtor(return_value);
1602 php_error_docref(NULL, E_WARNING, "Cannot rebuild mime structure");
1603 RETURN_FALSE;
1604 }
1605 }
1606 }
1607 /* }}} */
1608
php_curl_set_callable_handler(zend_fcall_info_cache * const handler_fcc,zval * callable,bool is_array_config,const char * option_name)1609 static bool php_curl_set_callable_handler(zend_fcall_info_cache *const handler_fcc, zval *callable, bool is_array_config, const char *option_name)
1610 {
1611 if (ZEND_FCC_INITIALIZED(*handler_fcc)) {
1612 zend_fcc_dtor(handler_fcc);
1613 }
1614
1615 if (Z_TYPE_P(callable) == IS_NULL) {
1616 return true;
1617 }
1618
1619 char *error = NULL;
1620 if (UNEXPECTED(!zend_is_callable_ex(callable, /* object */ NULL, /* check_flags */ 0, /* callable_name */ NULL, handler_fcc, /* error */ &error))) {
1621 if (!EG(exception)) {
1622 zend_argument_type_error(2 + !is_array_config, "must be a valid callback for option %s, %s", option_name, error);
1623 }
1624 efree(error);
1625 return false;
1626 }
1627 zend_fcc_addref(handler_fcc);
1628 return true;
1629 }
1630
1631
1632 #define HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(curl_ptr, constant_no_function, handler_type, default_method) \
1633 case constant_no_function##FUNCTION: { \
1634 bool result = php_curl_set_callable_handler(&curl_ptr->handlers.handler_type->fcc, zvalue, is_array_config, #constant_no_function "FUNCTION"); \
1635 if (!result) { \
1636 curl_ptr->handlers.handler_type->method = default_method; \
1637 return FAILURE; \
1638 } \
1639 if (!ZEND_FCC_INITIALIZED(curl_ptr->handlers.handler_type->fcc)) { \
1640 curl_ptr->handlers.handler_type->method = default_method; \
1641 return SUCCESS; \
1642 } \
1643 curl_ptr->handlers.handler_type->method = PHP_CURL_USER; \
1644 break; \
1645 }
1646
1647 #define HANDLE_CURL_OPTION_CALLABLE(curl_ptr, constant_no_function, handler_fcc, c_callback) \
1648 case constant_no_function##FUNCTION: { \
1649 bool result = php_curl_set_callable_handler(&curl_ptr->handler_fcc, zvalue, is_array_config, #constant_no_function "FUNCTION"); \
1650 if (!result) { \
1651 return FAILURE; \
1652 } \
1653 curl_easy_setopt(curl_ptr->cp, constant_no_function##FUNCTION, (c_callback)); \
1654 curl_easy_setopt(curl_ptr->cp, constant_no_function##DATA, curl_ptr); \
1655 break; \
1656 }
1657
_php_curl_setopt(php_curl * ch,zend_long option,zval * zvalue,bool is_array_config)1658 static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue, bool is_array_config) /* {{{ */
1659 {
1660 CURLcode error = CURLE_OK;
1661 zend_long lval;
1662
1663 switch (option) {
1664 /* Callable options */
1665 HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_WRITE, write, PHP_CURL_STDOUT);
1666 HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_HEADER, write_header, PHP_CURL_IGNORE);
1667 HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_READ, read, PHP_CURL_DIRECT);
1668
1669 HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_PROGRESS, handlers.progress, curl_progress);
1670 HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_XFERINFO, handlers.xferinfo, curl_xferinfo);
1671 HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_FNMATCH_, handlers.fnmatch, curl_fnmatch);
1672 HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_DEBUG, handlers.debug, curl_debug);
1673
1674 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1675 HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_PREREQ, handlers.prereq, curl_prereqfunction);
1676 #endif
1677 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
1678 HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_SSH_HOSTKEY, handlers.sshhostkey, curl_ssh_hostkeyfunction);
1679 #endif
1680
1681 /* Long options */
1682 case CURLOPT_SSL_VERIFYHOST:
1683 lval = zval_get_long(zvalue);
1684 if (lval == 1) {
1685 php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
1686 error = curl_easy_setopt(ch->cp, option, 2);
1687 break;
1688 }
1689 ZEND_FALLTHROUGH;
1690 case CURLOPT_AUTOREFERER:
1691 case CURLOPT_BUFFERSIZE:
1692 case CURLOPT_CONNECTTIMEOUT:
1693 case CURLOPT_COOKIESESSION:
1694 case CURLOPT_CRLF:
1695 case CURLOPT_DNS_CACHE_TIMEOUT:
1696 case CURLOPT_FAILONERROR:
1697 case CURLOPT_FILETIME:
1698 case CURLOPT_FORBID_REUSE:
1699 case CURLOPT_FRESH_CONNECT:
1700 case CURLOPT_FTP_USE_EPRT:
1701 case CURLOPT_FTP_USE_EPSV:
1702 case CURLOPT_HEADER:
1703 case CURLOPT_HTTPGET:
1704 case CURLOPT_HTTPPROXYTUNNEL:
1705 case CURLOPT_HTTP_VERSION:
1706 case CURLOPT_INFILESIZE:
1707 case CURLOPT_LOW_SPEED_LIMIT:
1708 case CURLOPT_LOW_SPEED_TIME:
1709 case CURLOPT_MAXCONNECTS:
1710 case CURLOPT_MAXREDIRS:
1711 case CURLOPT_NETRC:
1712 case CURLOPT_NOBODY:
1713 case CURLOPT_NOPROGRESS:
1714 case CURLOPT_NOSIGNAL:
1715 case CURLOPT_PORT:
1716 case CURLOPT_POST:
1717 case CURLOPT_PROXYPORT:
1718 case CURLOPT_PROXYTYPE:
1719 case CURLOPT_PUT:
1720 case CURLOPT_RESUME_FROM:
1721 case CURLOPT_SSLVERSION:
1722 case CURLOPT_SSL_VERIFYPEER:
1723 case CURLOPT_TIMECONDITION:
1724 case CURLOPT_TIMEOUT:
1725 case CURLOPT_TIMEVALUE:
1726 case CURLOPT_TRANSFERTEXT:
1727 case CURLOPT_UNRESTRICTED_AUTH:
1728 case CURLOPT_UPLOAD:
1729 case CURLOPT_VERBOSE:
1730 case CURLOPT_HTTPAUTH:
1731 case CURLOPT_FTP_CREATE_MISSING_DIRS:
1732 case CURLOPT_PROXYAUTH:
1733 case CURLOPT_SERVER_RESPONSE_TIMEOUT:
1734 case CURLOPT_IPRESOLVE:
1735 case CURLOPT_MAXFILESIZE:
1736 case CURLOPT_TCP_NODELAY:
1737 case CURLOPT_FTPSSLAUTH:
1738 case CURLOPT_IGNORE_CONTENT_LENGTH:
1739 case CURLOPT_FTP_SKIP_PASV_IP:
1740 case CURLOPT_FTP_FILEMETHOD:
1741 case CURLOPT_CONNECT_ONLY:
1742 case CURLOPT_LOCALPORT:
1743 case CURLOPT_LOCALPORTRANGE:
1744 case CURLOPT_SSL_SESSIONID_CACHE:
1745 case CURLOPT_FTP_SSL_CCC:
1746 case CURLOPT_SSH_AUTH_TYPES:
1747 case CURLOPT_CONNECTTIMEOUT_MS:
1748 case CURLOPT_HTTP_CONTENT_DECODING:
1749 case CURLOPT_HTTP_TRANSFER_DECODING:
1750 case CURLOPT_TIMEOUT_MS:
1751 case CURLOPT_NEW_DIRECTORY_PERMS:
1752 case CURLOPT_NEW_FILE_PERMS:
1753 case CURLOPT_USE_SSL:
1754 case CURLOPT_APPEND:
1755 case CURLOPT_DIRLISTONLY:
1756 case CURLOPT_PROXY_TRANSFER_MODE:
1757 case CURLOPT_ADDRESS_SCOPE:
1758 case CURLOPT_CERTINFO:
1759 case CURLOPT_PROTOCOLS:
1760 case CURLOPT_REDIR_PROTOCOLS:
1761 case CURLOPT_SOCKS5_GSSAPI_NEC:
1762 case CURLOPT_TFTP_BLKSIZE:
1763 case CURLOPT_FTP_USE_PRET:
1764 case CURLOPT_RTSP_CLIENT_CSEQ:
1765 case CURLOPT_RTSP_REQUEST:
1766 case CURLOPT_RTSP_SERVER_CSEQ:
1767 case CURLOPT_WILDCARDMATCH:
1768 case CURLOPT_GSSAPI_DELEGATION:
1769 case CURLOPT_ACCEPTTIMEOUT_MS:
1770 case CURLOPT_SSL_OPTIONS:
1771 case CURLOPT_TCP_KEEPALIVE:
1772 case CURLOPT_TCP_KEEPIDLE:
1773 case CURLOPT_TCP_KEEPINTVL:
1774 case CURLOPT_SASL_IR:
1775 case CURLOPT_EXPECT_100_TIMEOUT_MS:
1776 case CURLOPT_SSL_ENABLE_ALPN:
1777 case CURLOPT_SSL_ENABLE_NPN:
1778 case CURLOPT_HEADEROPT:
1779 case CURLOPT_SSL_VERIFYSTATUS:
1780 case CURLOPT_PATH_AS_IS:
1781 case CURLOPT_SSL_FALSESTART:
1782 case CURLOPT_PIPEWAIT:
1783 case CURLOPT_STREAM_WEIGHT:
1784 case CURLOPT_TFTP_NO_OPTIONS:
1785 case CURLOPT_TCP_FASTOPEN:
1786 case CURLOPT_KEEP_SENDING_ON_ERROR:
1787 case CURLOPT_PROXY_SSL_OPTIONS:
1788 case CURLOPT_PROXY_SSL_VERIFYHOST:
1789 case CURLOPT_PROXY_SSL_VERIFYPEER:
1790 case CURLOPT_PROXY_SSLVERSION:
1791 case CURLOPT_SUPPRESS_CONNECT_HEADERS:
1792 case CURLOPT_SOCKS5_AUTH:
1793 case CURLOPT_SSH_COMPRESSION:
1794 case CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS:
1795 case CURLOPT_DNS_SHUFFLE_ADDRESSES:
1796 case CURLOPT_HAPROXYPROTOCOL:
1797 case CURLOPT_DISALLOW_USERNAME_IN_URL:
1798 #if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
1799 case CURLOPT_UPKEEP_INTERVAL_MS:
1800 case CURLOPT_UPLOAD_BUFFERSIZE:
1801 #endif
1802 #if LIBCURL_VERSION_NUM >= 0x074000 /* Available since 7.64.0 */
1803 case CURLOPT_HTTP09_ALLOWED:
1804 #endif
1805 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1806 case CURLOPT_ALTSVC_CTRL:
1807 #endif
1808 #if LIBCURL_VERSION_NUM >= 0x074100 /* Available since 7.65.0 */
1809 case CURLOPT_MAXAGE_CONN:
1810 #endif
1811 #if LIBCURL_VERSION_NUM >= 0x074500 /* Available since 7.69.0 */
1812 case CURLOPT_MAIL_RCPT_ALLLOWFAILS:
1813 #endif
1814 #if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
1815 case CURLOPT_HSTS_CTRL:
1816 #endif
1817 #if LIBCURL_VERSION_NUM >= 0x074c00 /* Available since 7.76.0 */
1818 case CURLOPT_DOH_SSL_VERIFYHOST:
1819 case CURLOPT_DOH_SSL_VERIFYPEER:
1820 case CURLOPT_DOH_SSL_VERIFYSTATUS:
1821 #endif
1822 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1823 case CURLOPT_MAXLIFETIME_CONN:
1824 #endif
1825 #if LIBCURL_VERSION_NUM >= 0x075100 /* Available since 7.81.0 */
1826 case CURLOPT_MIME_OPTIONS:
1827 #endif
1828 #if LIBCURL_VERSION_NUM >= 0x075600 /* Available since 7.86.0 */
1829 case CURLOPT_WS_OPTIONS:
1830 #endif
1831 #if LIBCURL_VERSION_NUM >= 0x075700 /* Available since 7.87.0 */
1832 case CURLOPT_CA_CACHE_TIMEOUT:
1833 case CURLOPT_QUICK_EXIT:
1834 #endif
1835 #if LIBCURL_VERSION_NUM >= 0x080900 /* Available since 8.9.0 */
1836 case CURLOPT_TCP_KEEPCNT:
1837 #endif
1838 lval = zval_get_long(zvalue);
1839 if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
1840 (PG(open_basedir) && *PG(open_basedir)) && (lval & CURLPROTO_FILE)) {
1841 php_error_docref(NULL, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
1842 return FAILURE;
1843 }
1844 error = curl_easy_setopt(ch->cp, option, lval);
1845 break;
1846 case CURLOPT_SAFE_UPLOAD:
1847 if (!zend_is_true(zvalue)) {
1848 zend_value_error("%s(): Disabling safe uploads is no longer supported", get_active_function_name());
1849 return FAILURE;
1850 }
1851 break;
1852
1853 /* String options */
1854 case CURLOPT_CAINFO:
1855 case CURLOPT_CAPATH:
1856 case CURLOPT_COOKIE:
1857 case CURLOPT_EGDSOCKET:
1858 case CURLOPT_INTERFACE:
1859 case CURLOPT_PROXY:
1860 case CURLOPT_PROXYUSERPWD:
1861 case CURLOPT_REFERER:
1862 case CURLOPT_SSLCERTTYPE:
1863 case CURLOPT_SSLENGINE:
1864 case CURLOPT_SSLENGINE_DEFAULT:
1865 case CURLOPT_SSLKEY:
1866 case CURLOPT_SSLKEYPASSWD:
1867 case CURLOPT_SSLKEYTYPE:
1868 case CURLOPT_SSL_CIPHER_LIST:
1869 case CURLOPT_USERAGENT:
1870 case CURLOPT_USERPWD:
1871 case CURLOPT_COOKIELIST:
1872 case CURLOPT_FTP_ALTERNATIVE_TO_USER:
1873 case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
1874 case CURLOPT_PASSWORD:
1875 case CURLOPT_PROXYPASSWORD:
1876 case CURLOPT_PROXYUSERNAME:
1877 case CURLOPT_USERNAME:
1878 case CURLOPT_NOPROXY:
1879 case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1880 case CURLOPT_MAIL_FROM:
1881 case CURLOPT_RTSP_STREAM_URI:
1882 case CURLOPT_RTSP_TRANSPORT:
1883 case CURLOPT_TLSAUTH_TYPE:
1884 case CURLOPT_TLSAUTH_PASSWORD:
1885 case CURLOPT_TLSAUTH_USERNAME:
1886 case CURLOPT_TRANSFER_ENCODING:
1887 case CURLOPT_DNS_SERVERS:
1888 case CURLOPT_MAIL_AUTH:
1889 case CURLOPT_LOGIN_OPTIONS:
1890 case CURLOPT_PINNEDPUBLICKEY:
1891 case CURLOPT_PROXY_SERVICE_NAME:
1892 case CURLOPT_SERVICE_NAME:
1893 case CURLOPT_DEFAULT_PROTOCOL:
1894 case CURLOPT_PRE_PROXY:
1895 case CURLOPT_PROXY_CAINFO:
1896 case CURLOPT_PROXY_CAPATH:
1897 case CURLOPT_PROXY_CRLFILE:
1898 case CURLOPT_PROXY_KEYPASSWD:
1899 case CURLOPT_PROXY_PINNEDPUBLICKEY:
1900 case CURLOPT_PROXY_SSL_CIPHER_LIST:
1901 case CURLOPT_PROXY_SSLCERT:
1902 case CURLOPT_PROXY_SSLCERTTYPE:
1903 case CURLOPT_PROXY_SSLKEY:
1904 case CURLOPT_PROXY_SSLKEYTYPE:
1905 case CURLOPT_PROXY_TLSAUTH_PASSWORD:
1906 case CURLOPT_PROXY_TLSAUTH_TYPE:
1907 case CURLOPT_PROXY_TLSAUTH_USERNAME:
1908 case CURLOPT_ABSTRACT_UNIX_SOCKET:
1909 case CURLOPT_REQUEST_TARGET:
1910 case CURLOPT_PROXY_TLS13_CIPHERS:
1911 case CURLOPT_TLS13_CIPHERS:
1912 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1913 case CURLOPT_ALTSVC:
1914 #endif
1915 #if LIBCURL_VERSION_NUM >= 0x074200 /* Available since 7.66.0 */
1916 case CURLOPT_SASL_AUTHZID:
1917 #endif
1918 #if LIBCURL_VERSION_NUM >= 0x074700 /* Available since 7.71.0 */
1919 case CURLOPT_PROXY_ISSUERCERT:
1920 #endif
1921 #if LIBCURL_VERSION_NUM >= 0x074900 /* Available since 7.73.0 */
1922 case CURLOPT_SSL_EC_CURVES:
1923 #endif
1924 #if LIBCURL_VERSION_NUM >= 0x074b00 /* Available since 7.75.0 */
1925 case CURLOPT_AWS_SIGV4:
1926 #endif
1927 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1928 case CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256:
1929 #endif
1930 #if LIBCURL_VERSION_NUM >= 0x075500 /* Available since 7.85.0 */
1931 case CURLOPT_PROTOCOLS_STR:
1932 case CURLOPT_REDIR_PROTOCOLS_STR:
1933 #endif
1934 {
1935 zend_string *tmp_str;
1936 zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
1937 #if LIBCURL_VERSION_NUM >= 0x075500 /* Available since 7.85.0 */
1938 if ((option == CURLOPT_PROTOCOLS_STR || option == CURLOPT_REDIR_PROTOCOLS_STR) &&
1939 (PG(open_basedir) && *PG(open_basedir))
1940 && (php_memnistr(ZSTR_VAL(str), "file", sizeof("file") - 1, ZSTR_VAL(str) + ZSTR_LEN(str)) != NULL
1941 || php_memnistr(ZSTR_VAL(str), "all", sizeof("all") - 1, ZSTR_VAL(str) + ZSTR_LEN(str)) != NULL)) {
1942 zend_tmp_string_release(tmp_str);
1943 php_error_docref(NULL, E_WARNING, "The FILE protocol cannot be activated when an open_basedir is set");
1944 return FAILURE;
1945 }
1946 #endif
1947 zend_result ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str));
1948 zend_tmp_string_release(tmp_str);
1949 return ret;
1950 }
1951
1952 /* Curl nullable string options */
1953 case CURLOPT_CUSTOMREQUEST:
1954 case CURLOPT_FTPPORT:
1955 case CURLOPT_RANGE:
1956 case CURLOPT_FTP_ACCOUNT:
1957 case CURLOPT_RTSP_SESSION_ID:
1958 case CURLOPT_ACCEPT_ENCODING:
1959 case CURLOPT_DNS_INTERFACE:
1960 case CURLOPT_DNS_LOCAL_IP4:
1961 case CURLOPT_DNS_LOCAL_IP6:
1962 case CURLOPT_XOAUTH2_BEARER:
1963 case CURLOPT_UNIX_SOCKET_PATH:
1964 #if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
1965 case CURLOPT_DOH_URL:
1966 #endif
1967 #if LIBCURL_VERSION_NUM >= 0x074a00 /* Available since 7.74.0 */
1968 case CURLOPT_HSTS:
1969 #endif
1970 case CURLOPT_KRBLEVEL:
1971 {
1972 if (Z_ISNULL_P(zvalue)) {
1973 error = curl_easy_setopt(ch->cp, option, NULL);
1974 } else {
1975 zend_string *tmp_str;
1976 zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
1977 zend_result ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str));
1978 zend_tmp_string_release(tmp_str);
1979 return ret;
1980 }
1981 break;
1982 }
1983
1984 /* Curl private option */
1985 case CURLOPT_PRIVATE:
1986 {
1987 zval_ptr_dtor(&ch->private_data);
1988 ZVAL_COPY(&ch->private_data, zvalue);
1989 return SUCCESS;
1990 }
1991
1992 /* Curl url option */
1993 case CURLOPT_URL:
1994 {
1995 zend_string *tmp_str;
1996 zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
1997 zend_result ret = php_curl_option_url(ch, str);
1998 zend_tmp_string_release(tmp_str);
1999 return ret;
2000 }
2001
2002 /* Curl file handle options */
2003 case CURLOPT_FILE:
2004 case CURLOPT_INFILE:
2005 case CURLOPT_STDERR:
2006 case CURLOPT_WRITEHEADER: {
2007 FILE *fp = NULL;
2008 php_stream *what = NULL;
2009
2010 if (Z_TYPE_P(zvalue) != IS_NULL) {
2011 what = (php_stream *)zend_fetch_resource2_ex(zvalue, "File-Handle", php_file_le_stream(), php_file_le_pstream());
2012 if (!what) {
2013 return FAILURE;
2014 }
2015
2016 if (FAILURE == php_stream_cast(what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2017 return FAILURE;
2018 }
2019
2020 if (!fp) {
2021 return FAILURE;
2022 }
2023 }
2024
2025 error = CURLE_OK;
2026 switch (option) {
2027 case CURLOPT_FILE:
2028 if (!what) {
2029 if (!Z_ISUNDEF(ch->handlers.write->stream)) {
2030 zval_ptr_dtor(&ch->handlers.write->stream);
2031 ZVAL_UNDEF(&ch->handlers.write->stream);
2032 }
2033 ch->handlers.write->fp = NULL;
2034 ch->handlers.write->method = PHP_CURL_STDOUT;
2035 } else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2036 zval_ptr_dtor(&ch->handlers.write->stream);
2037 ch->handlers.write->fp = fp;
2038 ch->handlers.write->method = PHP_CURL_FILE;
2039 ZVAL_COPY(&ch->handlers.write->stream, zvalue);
2040 } else {
2041 zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
2042 return FAILURE;
2043 }
2044 break;
2045 case CURLOPT_WRITEHEADER:
2046 if (!what) {
2047 if (!Z_ISUNDEF(ch->handlers.write_header->stream)) {
2048 zval_ptr_dtor(&ch->handlers.write_header->stream);
2049 ZVAL_UNDEF(&ch->handlers.write_header->stream);
2050 }
2051 ch->handlers.write_header->fp = NULL;
2052 ch->handlers.write_header->method = PHP_CURL_IGNORE;
2053 } else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2054 zval_ptr_dtor(&ch->handlers.write_header->stream);
2055 ch->handlers.write_header->fp = fp;
2056 ch->handlers.write_header->method = PHP_CURL_FILE;
2057 ZVAL_COPY(&ch->handlers.write_header->stream, zvalue);
2058 } else {
2059 zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
2060 return FAILURE;
2061 }
2062 break;
2063 case CURLOPT_INFILE:
2064 if (!what) {
2065 if (!Z_ISUNDEF(ch->handlers.read->stream)) {
2066 zval_ptr_dtor(&ch->handlers.read->stream);
2067 ZVAL_UNDEF(&ch->handlers.read->stream);
2068 }
2069 ch->handlers.read->fp = NULL;
2070 ch->handlers.read->res = NULL;
2071 } else {
2072 zval_ptr_dtor(&ch->handlers.read->stream);
2073 ch->handlers.read->fp = fp;
2074 ch->handlers.read->res = Z_RES_P(zvalue);
2075 ZVAL_COPY(&ch->handlers.read->stream, zvalue);
2076 }
2077 break;
2078 case CURLOPT_STDERR:
2079 if (!what) {
2080 if (!Z_ISUNDEF(ch->handlers.std_err)) {
2081 zval_ptr_dtor(&ch->handlers.std_err);
2082 ZVAL_UNDEF(&ch->handlers.std_err);
2083 }
2084 } else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2085 zval_ptr_dtor(&ch->handlers.std_err);
2086 ZVAL_COPY(&ch->handlers.std_err, zvalue);
2087 } else {
2088 zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
2089 return FAILURE;
2090 }
2091 ZEND_FALLTHROUGH;
2092 default:
2093 error = curl_easy_setopt(ch->cp, option, fp);
2094 break;
2095 }
2096 break;
2097 }
2098
2099 /* Curl linked list options */
2100 case CURLOPT_HTTP200ALIASES:
2101 case CURLOPT_HTTPHEADER:
2102 case CURLOPT_POSTQUOTE:
2103 case CURLOPT_PREQUOTE:
2104 case CURLOPT_QUOTE:
2105 case CURLOPT_TELNETOPTIONS:
2106 case CURLOPT_MAIL_RCPT:
2107 case CURLOPT_RESOLVE:
2108 case CURLOPT_PROXYHEADER:
2109 case CURLOPT_CONNECT_TO:
2110 {
2111 zval *current;
2112 HashTable *ph;
2113 zend_string *val, *tmp_val;
2114 struct curl_slist *slist = NULL;
2115
2116 if (Z_TYPE_P(zvalue) != IS_ARRAY) {
2117 const char *name = NULL;
2118 switch (option) {
2119 case CURLOPT_HTTPHEADER:
2120 name = "CURLOPT_HTTPHEADER";
2121 break;
2122 case CURLOPT_QUOTE:
2123 name = "CURLOPT_QUOTE";
2124 break;
2125 case CURLOPT_HTTP200ALIASES:
2126 name = "CURLOPT_HTTP200ALIASES";
2127 break;
2128 case CURLOPT_POSTQUOTE:
2129 name = "CURLOPT_POSTQUOTE";
2130 break;
2131 case CURLOPT_PREQUOTE:
2132 name = "CURLOPT_PREQUOTE";
2133 break;
2134 case CURLOPT_TELNETOPTIONS:
2135 name = "CURLOPT_TELNETOPTIONS";
2136 break;
2137 case CURLOPT_MAIL_RCPT:
2138 name = "CURLOPT_MAIL_RCPT";
2139 break;
2140 case CURLOPT_RESOLVE:
2141 name = "CURLOPT_RESOLVE";
2142 break;
2143 case CURLOPT_PROXYHEADER:
2144 name = "CURLOPT_PROXYHEADER";
2145 break;
2146 case CURLOPT_CONNECT_TO:
2147 name = "CURLOPT_CONNECT_TO";
2148 break;
2149 }
2150
2151 zend_type_error("%s(): The %s option must have an array value", get_active_function_name(), name);
2152 return FAILURE;
2153 }
2154
2155 ph = Z_ARRVAL_P(zvalue);
2156 ZEND_HASH_FOREACH_VAL(ph, current) {
2157 ZVAL_DEREF(current);
2158 val = zval_get_tmp_string(current, &tmp_val);
2159 slist = curl_slist_append(slist, ZSTR_VAL(val));
2160 zend_tmp_string_release(tmp_val);
2161 if (!slist) {
2162 php_error_docref(NULL, E_WARNING, "Could not build curl_slist");
2163 return FAILURE;
2164 }
2165 } ZEND_HASH_FOREACH_END();
2166
2167 if (slist) {
2168 if ((*ch->clone) == 1) {
2169 zend_hash_index_update_ptr(&ch->to_free->slist, option, slist);
2170 } else {
2171 zend_hash_next_index_insert_ptr(&ch->to_free->slist, slist);
2172 }
2173 }
2174
2175 error = curl_easy_setopt(ch->cp, option, slist);
2176
2177 break;
2178 }
2179
2180 case CURLOPT_BINARYTRANSFER:
2181 case CURLOPT_DNS_USE_GLOBAL_CACHE:
2182 /* Do nothing, just backward compatibility */
2183 break;
2184
2185 case CURLOPT_FOLLOWLOCATION:
2186 lval = zend_is_true(zvalue);
2187 error = curl_easy_setopt(ch->cp, option, lval);
2188 break;
2189
2190 case CURLOPT_POSTFIELDS:
2191 if (Z_TYPE_P(zvalue) == IS_ARRAY) {
2192 if (zend_hash_num_elements(Z_ARRVAL_P(zvalue)) == 0) {
2193 /* no need to build the mime structure for empty hashtables;
2194 also works around https://github.com/curl/curl/issues/6455 */
2195 curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, "");
2196 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, 0);
2197 } else {
2198 return build_mime_structure_from_hash(ch, zvalue);
2199 }
2200 } else {
2201 zend_string *tmp_str;
2202 zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2203 /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2204 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2205 error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, ZSTR_VAL(str));
2206 zend_tmp_string_release(tmp_str);
2207 }
2208 break;
2209
2210 case CURLOPT_RETURNTRANSFER:
2211 if (zend_is_true(zvalue)) {
2212 ch->handlers.write->method = PHP_CURL_RETURN;
2213 } else {
2214 ch->handlers.write->method = PHP_CURL_STDOUT;
2215 }
2216 break;
2217
2218 /* Curl off_t options */
2219 case CURLOPT_MAX_RECV_SPEED_LARGE:
2220 case CURLOPT_MAX_SEND_SPEED_LARGE:
2221 case CURLOPT_MAXFILESIZE_LARGE:
2222 case CURLOPT_TIMEVALUE_LARGE:
2223 lval = zval_get_long(zvalue);
2224 error = curl_easy_setopt(ch->cp, option, (curl_off_t)lval);
2225 break;
2226
2227 case CURLOPT_POSTREDIR:
2228 lval = zval_get_long(zvalue);
2229 error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, lval & CURL_REDIR_POST_ALL);
2230 break;
2231
2232 /* the following options deal with files, therefore the open_basedir check
2233 * is required.
2234 */
2235 case CURLOPT_COOKIEFILE:
2236 case CURLOPT_COOKIEJAR:
2237 case CURLOPT_RANDOM_FILE:
2238 case CURLOPT_SSLCERT:
2239 case CURLOPT_NETRC_FILE:
2240 case CURLOPT_SSH_PRIVATE_KEYFILE:
2241 case CURLOPT_SSH_PUBLIC_KEYFILE:
2242 case CURLOPT_CRLFILE:
2243 case CURLOPT_ISSUERCERT:
2244 case CURLOPT_SSH_KNOWNHOSTS:
2245 {
2246 zend_string *tmp_str;
2247 zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2248 zend_result ret;
2249
2250 if (ZSTR_LEN(str) && php_check_open_basedir(ZSTR_VAL(str))) {
2251 zend_tmp_string_release(tmp_str);
2252 return FAILURE;
2253 }
2254
2255 ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str));
2256 zend_tmp_string_release(tmp_str);
2257 return ret;
2258 }
2259
2260 case CURLINFO_HEADER_OUT:
2261 if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2262 zend_value_error("CURLINFO_HEADER_OUT option must not be set when the CURLOPT_DEBUGFUNCTION option is set");
2263 return FAILURE;
2264 }
2265
2266 if (zend_is_true(zvalue)) {
2267 curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2268 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2269 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2270 } else {
2271 curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2272 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2273 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2274 }
2275 break;
2276
2277 case CURLOPT_SHARE:
2278 {
2279 if (Z_TYPE_P(zvalue) == IS_OBJECT && Z_OBJCE_P(zvalue) == curl_share_ce) {
2280 php_curlsh *sh = Z_CURL_SHARE_P(zvalue);
2281 curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2282
2283 if (ch->share) {
2284 OBJ_RELEASE(&ch->share->std);
2285 }
2286 GC_ADDREF(&sh->std);
2287 ch->share = sh;
2288 }
2289 }
2290 break;
2291
2292 /* Curl blob options */
2293 #if LIBCURL_VERSION_NUM >= 0x074700 /* Available since 7.71.0 */
2294 case CURLOPT_ISSUERCERT_BLOB:
2295 case CURLOPT_PROXY_ISSUERCERT_BLOB:
2296 case CURLOPT_PROXY_SSLCERT_BLOB:
2297 case CURLOPT_PROXY_SSLKEY_BLOB:
2298 case CURLOPT_SSLCERT_BLOB:
2299 case CURLOPT_SSLKEY_BLOB:
2300 #if LIBCURL_VERSION_NUM >= 0x074d00 /* Available since 7.77.0 */
2301 case CURLOPT_CAINFO_BLOB:
2302 case CURLOPT_PROXY_CAINFO_BLOB:
2303 #endif
2304 {
2305 zend_string *tmp_str;
2306 zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2307
2308 struct curl_blob stblob;
2309 stblob.data = ZSTR_VAL(str);
2310 stblob.len = ZSTR_LEN(str);
2311 stblob.flags = CURL_BLOB_COPY;
2312 error = curl_easy_setopt(ch->cp, option, &stblob);
2313
2314 zend_tmp_string_release(tmp_str);
2315 }
2316 break;
2317 #endif
2318
2319 default:
2320 if (is_array_config) {
2321 zend_argument_value_error(2, "must contain only valid cURL options");
2322 } else {
2323 zend_argument_value_error(2, "is not a valid cURL option");
2324 }
2325 error = CURLE_UNKNOWN_OPTION;
2326 break;
2327 }
2328
2329 SAVE_CURL_ERROR(ch, error);
2330 if (error != CURLE_OK) {
2331 return FAILURE;
2332 } else {
2333 return SUCCESS;
2334 }
2335 }
2336 /* }}} */
2337
2338 /* {{{ Set an option for a cURL transfer */
PHP_FUNCTION(curl_setopt)2339 PHP_FUNCTION(curl_setopt)
2340 {
2341 zval *zid, *zvalue;
2342 zend_long options;
2343 php_curl *ch;
2344
2345 ZEND_PARSE_PARAMETERS_START(3, 3)
2346 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2347 Z_PARAM_LONG(options)
2348 Z_PARAM_ZVAL(zvalue)
2349 ZEND_PARSE_PARAMETERS_END();
2350
2351 ch = Z_CURL_P(zid);
2352
2353 if (_php_curl_setopt(ch, options, zvalue, 0) == SUCCESS) {
2354 RETURN_TRUE;
2355 } else {
2356 RETURN_FALSE;
2357 }
2358 }
2359 /* }}} */
2360
2361 /* {{{ Set an array of option for a cURL transfer */
PHP_FUNCTION(curl_setopt_array)2362 PHP_FUNCTION(curl_setopt_array)
2363 {
2364 zval *zid, *arr, *entry;
2365 php_curl *ch;
2366 zend_ulong option;
2367 zend_string *string_key;
2368
2369 ZEND_PARSE_PARAMETERS_START(2, 2)
2370 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2371 Z_PARAM_ARRAY(arr)
2372 ZEND_PARSE_PARAMETERS_END();
2373
2374 ch = Z_CURL_P(zid);
2375
2376 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
2377 if (string_key) {
2378 zend_argument_value_error(2, "contains an invalid cURL option");
2379 RETURN_THROWS();
2380 }
2381
2382 ZVAL_DEREF(entry);
2383 if (_php_curl_setopt(ch, (zend_long) option, entry, 1) == FAILURE) {
2384 RETURN_FALSE;
2385 }
2386 } ZEND_HASH_FOREACH_END();
2387
2388 RETURN_TRUE;
2389 }
2390 /* }}} */
2391
2392 /* {{{ _php_curl_cleanup_handle(ch)
2393 Cleanup an execution phase */
_php_curl_cleanup_handle(php_curl * ch)2394 void _php_curl_cleanup_handle(php_curl *ch)
2395 {
2396 smart_str_free(&ch->handlers.write->buf);
2397 if (ch->header.str) {
2398 zend_string_release_ex(ch->header.str, 0);
2399 ch->header.str = NULL;
2400 }
2401
2402 memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
2403 ch->err.no = 0;
2404 }
2405 /* }}} */
2406
2407 /* {{{ Perform a cURL session */
PHP_FUNCTION(curl_exec)2408 PHP_FUNCTION(curl_exec)
2409 {
2410 CURLcode error;
2411 zval *zid;
2412 php_curl *ch;
2413
2414 ZEND_PARSE_PARAMETERS_START(1, 1)
2415 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2416 ZEND_PARSE_PARAMETERS_END();
2417
2418 ch = Z_CURL_P(zid);
2419
2420 _php_curl_verify_handlers(ch, /* reporterror */ true);
2421
2422 _php_curl_cleanup_handle(ch);
2423
2424 error = curl_easy_perform(ch->cp);
2425 SAVE_CURL_ERROR(ch, error);
2426
2427 if (error != CURLE_OK) {
2428 smart_str_free(&ch->handlers.write->buf);
2429 RETURN_FALSE;
2430 }
2431
2432 if (!Z_ISUNDEF(ch->handlers.std_err)) {
2433 php_stream *stream;
2434 stream = (php_stream*)zend_fetch_resource2_ex(&ch->handlers.std_err, NULL, php_file_le_stream(), php_file_le_pstream());
2435 if (stream) {
2436 php_stream_flush(stream);
2437 }
2438 }
2439
2440 if (ch->handlers.write->method == PHP_CURL_RETURN && ch->handlers.write->buf.s) {
2441 smart_str_0(&ch->handlers.write->buf);
2442 RETURN_STR_COPY(ch->handlers.write->buf.s);
2443 }
2444
2445 /* flush the file handle, so any remaining data is synched to disk */
2446 if (ch->handlers.write->method == PHP_CURL_FILE && ch->handlers.write->fp) {
2447 fflush(ch->handlers.write->fp);
2448 }
2449 if (ch->handlers.write_header->method == PHP_CURL_FILE && ch->handlers.write_header->fp) {
2450 fflush(ch->handlers.write_header->fp);
2451 }
2452
2453 if (ch->handlers.write->method == PHP_CURL_RETURN) {
2454 RETURN_EMPTY_STRING();
2455 } else {
2456 RETURN_TRUE;
2457 }
2458 }
2459 /* }}} */
2460
2461 /* {{{ Get information regarding a specific transfer */
PHP_FUNCTION(curl_getinfo)2462 PHP_FUNCTION(curl_getinfo)
2463 {
2464 zval *zid;
2465 php_curl *ch;
2466 zend_long option;
2467 bool option_is_null = 1;
2468
2469 ZEND_PARSE_PARAMETERS_START(1, 2)
2470 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2471 Z_PARAM_OPTIONAL
2472 Z_PARAM_LONG_OR_NULL(option, option_is_null)
2473 ZEND_PARSE_PARAMETERS_END();
2474
2475 ch = Z_CURL_P(zid);
2476
2477 if (option_is_null) {
2478 char *s_code;
2479 /* libcurl expects long datatype. So far no cases are known where
2480 it would be an issue. Using zend_long would truncate a 64-bit
2481 var on Win64, so the exact long datatype fits everywhere, as
2482 long as there's no 32-bit int overflow. */
2483 long l_code;
2484 double d_code;
2485 struct curl_certinfo *ci = NULL;
2486 zval listcode;
2487 curl_off_t co;
2488
2489 array_init(return_value);
2490
2491 if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
2492 CAAS("url", s_code);
2493 }
2494 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
2495 if (s_code != NULL) {
2496 CAAS("content_type", s_code);
2497 } else {
2498 zval retnull;
2499 ZVAL_NULL(&retnull);
2500 CAAZ("content_type", &retnull);
2501 }
2502 }
2503 if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
2504 CAAL("http_code", l_code);
2505 }
2506 if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
2507 CAAL("header_size", l_code);
2508 }
2509 if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
2510 CAAL("request_size", l_code);
2511 }
2512 if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
2513 CAAL("filetime", l_code);
2514 }
2515 if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
2516 CAAL("ssl_verify_result", l_code);
2517 }
2518 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
2519 CAAL("redirect_count", l_code);
2520 }
2521 if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
2522 CAAD("total_time", d_code);
2523 }
2524 if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
2525 CAAD("namelookup_time", d_code);
2526 }
2527 if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
2528 CAAD("connect_time", d_code);
2529 }
2530 if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
2531 CAAD("pretransfer_time", d_code);
2532 }
2533 if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
2534 CAAD("size_upload", d_code);
2535 }
2536 if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
2537 CAAD("size_download", d_code);
2538 }
2539 if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
2540 CAAD("speed_download", d_code);
2541 }
2542 if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
2543 CAAD("speed_upload", d_code);
2544 }
2545 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
2546 CAAD("download_content_length", d_code);
2547 }
2548 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
2549 CAAD("upload_content_length", d_code);
2550 }
2551 if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
2552 CAAD("starttransfer_time", d_code);
2553 }
2554 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
2555 CAAD("redirect_time", d_code);
2556 }
2557 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
2558 CAAS("redirect_url", s_code);
2559 }
2560 if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
2561 CAAS("primary_ip", s_code);
2562 }
2563 if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2564 array_init(&listcode);
2565 create_certinfo(ci, &listcode);
2566 CAAZ("certinfo", &listcode);
2567 }
2568 if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
2569 CAAL("primary_port", l_code);
2570 }
2571 if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
2572 CAAS("local_ip", s_code);
2573 }
2574 if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
2575 CAAL("local_port", l_code);
2576 }
2577 if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_VERSION, &l_code) == CURLE_OK) {
2578 CAAL("http_version", l_code);
2579 }
2580 if (curl_easy_getinfo(ch->cp, CURLINFO_PROTOCOL, &l_code) == CURLE_OK) {
2581 CAAL("protocol", l_code);
2582 }
2583 if (curl_easy_getinfo(ch->cp, CURLINFO_PROXY_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
2584 CAAL("ssl_verifyresult", l_code);
2585 }
2586 if (curl_easy_getinfo(ch->cp, CURLINFO_SCHEME, &s_code) == CURLE_OK) {
2587 CAAS("scheme", s_code);
2588 }
2589 if (curl_easy_getinfo(ch->cp, CURLINFO_APPCONNECT_TIME_T, &co) == CURLE_OK) {
2590 CAAL("appconnect_time_us", co);
2591 }
2592 if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME_T, &co) == CURLE_OK) {
2593 CAAL("connect_time_us", co);
2594 }
2595 if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME_T, &co) == CURLE_OK) {
2596 CAAL("namelookup_time_us", co);
2597 }
2598 if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME_T, &co) == CURLE_OK) {
2599 CAAL("pretransfer_time_us", co);
2600 }
2601 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME_T, &co) == CURLE_OK) {
2602 CAAL("redirect_time_us", co);
2603 }
2604 if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME_T, &co) == CURLE_OK) {
2605 CAAL("starttransfer_time_us", co);
2606 }
2607 #if LIBCURL_VERSION_NUM >= 0x080a00 /* Available since 8.10.0 */
2608 if (curl_easy_getinfo(ch->cp, CURLINFO_POSTTRANSFER_TIME_T, &co) == CURLE_OK) {
2609 CAAL("posttransfer_time_us", co);
2610 }
2611 #endif
2612 if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME_T, &co) == CURLE_OK) {
2613 CAAL("total_time_us", co);
2614 }
2615 if (ch->header.str) {
2616 CAASTR("request_header", ch->header.str);
2617 }
2618 #if LIBCURL_VERSION_NUM >= 0x074800 /* Available since 7.72.0 */
2619 if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_METHOD, &s_code) == CURLE_OK) {
2620 CAAS("effective_method", s_code);
2621 }
2622 #endif
2623 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
2624 if (curl_easy_getinfo(ch->cp, CURLINFO_CAPATH, &s_code) == CURLE_OK) {
2625 CAAS("capath", s_code);
2626 }
2627 if (curl_easy_getinfo(ch->cp, CURLINFO_CAINFO, &s_code) == CURLE_OK) {
2628 CAAS("cainfo", s_code);
2629 }
2630 #endif
2631 } else {
2632 switch (option) {
2633 case CURLINFO_HEADER_OUT:
2634 if (ch->header.str) {
2635 RETURN_STR_COPY(ch->header.str);
2636 } else {
2637 RETURN_FALSE;
2638 }
2639 case CURLINFO_CERTINFO: {
2640 struct curl_certinfo *ci = NULL;
2641
2642 array_init(return_value);
2643
2644 if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2645 create_certinfo(ci, return_value);
2646 } else {
2647 RETURN_FALSE;
2648 }
2649 break;
2650 }
2651 case CURLINFO_PRIVATE:
2652 if (!Z_ISUNDEF(ch->private_data)) {
2653 RETURN_COPY(&ch->private_data);
2654 } else {
2655 RETURN_FALSE;
2656 }
2657 break;
2658 default: {
2659 int type = CURLINFO_TYPEMASK & option;
2660 switch (type) {
2661 case CURLINFO_STRING:
2662 {
2663 char *s_code = NULL;
2664
2665 if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
2666 RETURN_STRING(s_code);
2667 } else {
2668 RETURN_FALSE;
2669 }
2670 break;
2671 }
2672 case CURLINFO_LONG:
2673 {
2674 zend_long code = 0;
2675
2676 if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
2677 RETURN_LONG(code);
2678 } else {
2679 RETURN_FALSE;
2680 }
2681 break;
2682 }
2683 case CURLINFO_DOUBLE:
2684 {
2685 double code = 0.0;
2686
2687 if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
2688 RETURN_DOUBLE(code);
2689 } else {
2690 RETURN_FALSE;
2691 }
2692 break;
2693 }
2694 case CURLINFO_SLIST:
2695 {
2696 struct curl_slist *slist;
2697 if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
2698 struct curl_slist *current = slist;
2699 array_init(return_value);
2700 while (current) {
2701 add_next_index_string(return_value, current->data);
2702 current = current->next;
2703 }
2704 curl_slist_free_all(slist);
2705 } else {
2706 RETURN_FALSE;
2707 }
2708 break;
2709 }
2710 case CURLINFO_OFF_T:
2711 {
2712 curl_off_t c_off;
2713 if (curl_easy_getinfo(ch->cp, option, &c_off) == CURLE_OK) {
2714 RETURN_LONG((long) c_off);
2715 } else {
2716 RETURN_FALSE;
2717 }
2718 break;
2719 }
2720 default:
2721 RETURN_FALSE;
2722 }
2723 }
2724 }
2725 }
2726 }
2727 /* }}} */
2728
2729 /* {{{ Return a string contain the last error for the current session */
PHP_FUNCTION(curl_error)2730 PHP_FUNCTION(curl_error)
2731 {
2732 zval *zid;
2733 php_curl *ch;
2734
2735 ZEND_PARSE_PARAMETERS_START(1, 1)
2736 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2737 ZEND_PARSE_PARAMETERS_END();
2738
2739 ch = Z_CURL_P(zid);
2740
2741 if (ch->err.no) {
2742 ch->err.str[CURL_ERROR_SIZE] = 0;
2743 if (strlen(ch->err.str) > 0) {
2744 RETURN_STRING(ch->err.str);
2745 } else {
2746 RETURN_STRING(curl_easy_strerror(ch->err.no));
2747 }
2748 } else {
2749 RETURN_EMPTY_STRING();
2750 }
2751 }
2752 /* }}} */
2753
2754 /* {{{ Return an integer containing the last error number */
PHP_FUNCTION(curl_errno)2755 PHP_FUNCTION(curl_errno)
2756 {
2757 zval *zid;
2758 php_curl *ch;
2759
2760 ZEND_PARSE_PARAMETERS_START(1,1)
2761 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2762 ZEND_PARSE_PARAMETERS_END();
2763
2764 ch = Z_CURL_P(zid);
2765
2766 RETURN_LONG(ch->err.no);
2767 }
2768 /* }}} */
2769
2770 /* {{{ Close a cURL session */
PHP_FUNCTION(curl_close)2771 PHP_FUNCTION(curl_close)
2772 {
2773 zval *zid;
2774 php_curl *ch;
2775
2776 ZEND_PARSE_PARAMETERS_START(1, 1)
2777 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2778 ZEND_PARSE_PARAMETERS_END();
2779
2780 ch = Z_CURL_P(zid);
2781
2782 if (ch->in_callback) {
2783 zend_throw_error(NULL, "%s(): Attempt to close cURL handle from a callback", get_active_function_name());
2784 RETURN_THROWS();
2785 }
2786 }
2787 /* }}} */
2788
curl_free_obj(zend_object * object)2789 static void curl_free_obj(zend_object *object)
2790 {
2791 php_curl *ch = curl_from_obj(object);
2792
2793 #if PHP_CURL_DEBUG
2794 fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
2795 #endif
2796
2797 if (!ch->cp) {
2798 /* Can happen if constructor throws. */
2799 zend_object_std_dtor(&ch->std);
2800 return;
2801 }
2802
2803 _php_curl_verify_handlers(ch, /* reporterror */ false);
2804
2805 curl_easy_cleanup(ch->cp);
2806
2807 /* cURL destructors should be invoked only by last curl handle */
2808 if (--(*ch->clone) == 0) {
2809 zend_llist_clean(&ch->to_free->post);
2810 zend_llist_clean(&ch->to_free->stream);
2811
2812 zend_hash_destroy(&ch->to_free->slist);
2813 efree(ch->to_free);
2814 efree(ch->clone);
2815 }
2816
2817 smart_str_free(&ch->handlers.write->buf);
2818 if (ZEND_FCC_INITIALIZED(ch->handlers.write->fcc)) {
2819 zend_fcc_dtor(&ch->handlers.write->fcc);
2820 }
2821 if (ZEND_FCC_INITIALIZED(ch->handlers.write_header->fcc)) {
2822 zend_fcc_dtor(&ch->handlers.write_header->fcc);
2823 }
2824 if (ZEND_FCC_INITIALIZED(ch->handlers.read->fcc)) {
2825 zend_fcc_dtor(&ch->handlers.read->fcc);
2826 }
2827 zval_ptr_dtor(&ch->handlers.std_err);
2828 if (ch->header.str) {
2829 zend_string_release_ex(ch->header.str, 0);
2830 }
2831
2832 zval_ptr_dtor(&ch->handlers.write_header->stream);
2833 zval_ptr_dtor(&ch->handlers.write->stream);
2834 zval_ptr_dtor(&ch->handlers.read->stream);
2835
2836 efree(ch->handlers.write);
2837 efree(ch->handlers.write_header);
2838 efree(ch->handlers.read);
2839
2840 if (ZEND_FCC_INITIALIZED(ch->handlers.progress)) {
2841 zend_fcc_dtor(&ch->handlers.progress);
2842 }
2843 if (ZEND_FCC_INITIALIZED(ch->handlers.xferinfo)) {
2844 zend_fcc_dtor(&ch->handlers.xferinfo);
2845 }
2846 if (ZEND_FCC_INITIALIZED(ch->handlers.fnmatch)) {
2847 zend_fcc_dtor(&ch->handlers.fnmatch);
2848 }
2849 if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2850 zend_fcc_dtor(&ch->handlers.debug);
2851 }
2852 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
2853 if (ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
2854 zend_fcc_dtor(&ch->handlers.prereq);
2855 }
2856 #endif
2857 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
2858 if (ZEND_FCC_INITIALIZED(ch->handlers.sshhostkey)) {
2859 zend_fcc_dtor(&ch->handlers.sshhostkey);
2860 }
2861 #endif
2862
2863 zval_ptr_dtor(&ch->postfields);
2864 zval_ptr_dtor(&ch->private_data);
2865
2866 if (ch->share) {
2867 OBJ_RELEASE(&ch->share->std);
2868 }
2869
2870 zend_object_std_dtor(&ch->std);
2871 }
2872 /* }}} */
2873
2874 /* {{{ return string describing error code */
PHP_FUNCTION(curl_strerror)2875 PHP_FUNCTION(curl_strerror)
2876 {
2877 zend_long code;
2878 const char *str;
2879
2880 ZEND_PARSE_PARAMETERS_START(1, 1)
2881 Z_PARAM_LONG(code)
2882 ZEND_PARSE_PARAMETERS_END();
2883
2884 str = curl_easy_strerror(code);
2885 if (str) {
2886 RETURN_STRING(str);
2887 } else {
2888 RETURN_NULL();
2889 }
2890 }
2891 /* }}} */
2892
2893 /* {{{ _php_curl_reset_handlers()
2894 Reset all handlers of a given php_curl */
_php_curl_reset_handlers(php_curl * ch)2895 static void _php_curl_reset_handlers(php_curl *ch)
2896 {
2897 if (!Z_ISUNDEF(ch->handlers.write->stream)) {
2898 zval_ptr_dtor(&ch->handlers.write->stream);
2899 ZVAL_UNDEF(&ch->handlers.write->stream);
2900 }
2901 ch->handlers.write->fp = NULL;
2902 ch->handlers.write->method = PHP_CURL_STDOUT;
2903
2904 if (!Z_ISUNDEF(ch->handlers.write_header->stream)) {
2905 zval_ptr_dtor(&ch->handlers.write_header->stream);
2906 ZVAL_UNDEF(&ch->handlers.write_header->stream);
2907 }
2908 ch->handlers.write_header->fp = NULL;
2909 ch->handlers.write_header->method = PHP_CURL_IGNORE;
2910
2911 if (!Z_ISUNDEF(ch->handlers.read->stream)) {
2912 zval_ptr_dtor(&ch->handlers.read->stream);
2913 ZVAL_UNDEF(&ch->handlers.read->stream);
2914 }
2915 ch->handlers.read->fp = NULL;
2916 ch->handlers.read->res = NULL;
2917 ch->handlers.read->method = PHP_CURL_DIRECT;
2918
2919 if (!Z_ISUNDEF(ch->handlers.std_err)) {
2920 zval_ptr_dtor(&ch->handlers.std_err);
2921 ZVAL_UNDEF(&ch->handlers.std_err);
2922 }
2923
2924 if (ZEND_FCC_INITIALIZED(ch->handlers.progress)) {
2925 zend_fcc_dtor(&ch->handlers.progress);
2926 }
2927
2928 if (ZEND_FCC_INITIALIZED(ch->handlers.xferinfo)) {
2929 zend_fcc_dtor(&ch->handlers.xferinfo);
2930 }
2931
2932 if (ZEND_FCC_INITIALIZED(ch->handlers.fnmatch)) {
2933 zend_fcc_dtor(&ch->handlers.fnmatch);
2934 }
2935
2936 if (ZEND_FCC_INITIALIZED(ch->handlers.debug)) {
2937 zend_fcc_dtor(&ch->handlers.debug);
2938 }
2939 #if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
2940 if (ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
2941 zend_fcc_dtor(&ch->handlers.prereq);
2942 }
2943 #endif
2944 #if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
2945 if (ZEND_FCC_INITIALIZED(ch->handlers.sshhostkey)) {
2946 zend_fcc_dtor(&ch->handlers.sshhostkey);
2947 }
2948 #endif
2949 }
2950 /* }}} */
2951
2952 /* {{{ Reset all options of a libcurl session handle */
PHP_FUNCTION(curl_reset)2953 PHP_FUNCTION(curl_reset)
2954 {
2955 zval *zid;
2956 php_curl *ch;
2957
2958 ZEND_PARSE_PARAMETERS_START(1, 1)
2959 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2960 ZEND_PARSE_PARAMETERS_END();
2961
2962 ch = Z_CURL_P(zid);
2963
2964 if (ch->in_callback) {
2965 zend_throw_error(NULL, "%s(): Attempt to reset cURL handle from a callback", get_active_function_name());
2966 RETURN_THROWS();
2967 }
2968
2969 curl_easy_reset(ch->cp);
2970 _php_curl_reset_handlers(ch);
2971 _php_curl_set_default_options(ch);
2972 }
2973 /* }}} */
2974
2975 /* {{{ URL encodes the given string */
PHP_FUNCTION(curl_escape)2976 PHP_FUNCTION(curl_escape)
2977 {
2978 zend_string *str;
2979 char *res;
2980 zval *zid;
2981 php_curl *ch;
2982
2983 ZEND_PARSE_PARAMETERS_START(2,2)
2984 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
2985 Z_PARAM_STR(str)
2986 ZEND_PARSE_PARAMETERS_END();
2987
2988 ch = Z_CURL_P(zid);
2989
2990 if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
2991 RETURN_FALSE;
2992 }
2993
2994 if ((res = curl_easy_escape(ch->cp, ZSTR_VAL(str), ZSTR_LEN(str)))) {
2995 RETVAL_STRING(res);
2996 curl_free(res);
2997 } else {
2998 RETURN_FALSE;
2999 }
3000 }
3001 /* }}} */
3002
3003 /* {{{ URL decodes the given string */
PHP_FUNCTION(curl_unescape)3004 PHP_FUNCTION(curl_unescape)
3005 {
3006 char *out = NULL;
3007 int out_len;
3008 zval *zid;
3009 zend_string *str;
3010 php_curl *ch;
3011
3012 ZEND_PARSE_PARAMETERS_START(2,2)
3013 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3014 Z_PARAM_STR(str)
3015 ZEND_PARSE_PARAMETERS_END();
3016
3017 ch = Z_CURL_P(zid);
3018
3019 if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
3020 RETURN_FALSE;
3021 }
3022
3023 if ((out = curl_easy_unescape(ch->cp, ZSTR_VAL(str), ZSTR_LEN(str), &out_len))) {
3024 RETVAL_STRINGL(out, out_len);
3025 curl_free(out);
3026 } else {
3027 RETURN_FALSE;
3028 }
3029 }
3030 /* }}} */
3031
3032 /* {{{ pause and unpause a connection */
PHP_FUNCTION(curl_pause)3033 PHP_FUNCTION(curl_pause)
3034 {
3035 zend_long bitmask;
3036 zval *zid;
3037 php_curl *ch;
3038
3039 ZEND_PARSE_PARAMETERS_START(2,2)
3040 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3041 Z_PARAM_LONG(bitmask)
3042 ZEND_PARSE_PARAMETERS_END();
3043
3044 ch = Z_CURL_P(zid);
3045
3046 RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3047 }
3048 /* }}} */
3049
3050 #if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
3051 /* {{{ perform connection upkeep checks */
PHP_FUNCTION(curl_upkeep)3052 PHP_FUNCTION(curl_upkeep)
3053 {
3054 CURLcode error;
3055 zval *zid;
3056 php_curl *ch;
3057
3058 ZEND_PARSE_PARAMETERS_START(1, 1)
3059 Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3060 ZEND_PARSE_PARAMETERS_END();
3061
3062 ch = Z_CURL_P(zid);
3063
3064 error = curl_easy_upkeep(ch->cp);
3065 SAVE_CURL_ERROR(ch, error);
3066
3067 RETURN_BOOL(error == CURLE_OK);
3068 }
3069 /*}}} */
3070 #endif
3071