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