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