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