1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2016 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Sterling Hughes <sterling@php.net>                           |
16   +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20
21#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "php.h"
28
29#if HAVE_CURL
30
31#include <stdio.h>
32#include <string.h>
33
34#ifdef PHP_WIN32
35#include <winsock2.h>
36#include <sys/types.h>
37#endif
38
39#include <curl/curl.h>
40#include <curl/easy.h>
41
42/* As of curl 7.11.1 this is no longer defined inside curl.h */
43#ifndef HttpPost
44#define HttpPost curl_httppost
45#endif
46
47/* {{{ cruft for thread safe SSL crypto locks */
48#if defined(ZTS) && defined(HAVE_CURL_SSL)
49# ifdef PHP_WIN32
50#  define PHP_CURL_NEED_OPENSSL_TSL
51#  include <openssl/crypto.h>
52# else /* !PHP_WIN32 */
53#  if defined(HAVE_CURL_OPENSSL)
54#   if defined(HAVE_OPENSSL_CRYPTO_H)
55#    define PHP_CURL_NEED_OPENSSL_TSL
56#    include <openssl/crypto.h>
57#   else
58#    warning \
59	"libcurl was compiled with OpenSSL support, but configure could not find " \
60	"openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
61	"cause random crashes on SSL requests"
62#   endif
63#  elif defined(HAVE_CURL_GNUTLS)
64#   if defined(HAVE_GCRYPT_H)
65#    define PHP_CURL_NEED_GNUTLS_TSL
66#    include <gcrypt.h>
67#   else
68#    warning \
69	"libcurl was compiled with GnuTLS support, but configure could not find " \
70	"gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
71	"cause random crashes on SSL requests"
72#   endif
73#  else
74#   warning \
75	"libcurl was compiled with SSL support, but configure could not determine which" \
76	"library was used; thus no SSL crypto locking callbacks will be set, which may " \
77	"cause random crashes on SSL requests"
78#  endif /* HAVE_CURL_OPENSSL || HAVE_CURL_GNUTLS */
79# endif /* PHP_WIN32 */
80#endif /* ZTS && HAVE_CURL_SSL */
81/* }}} */
82
83#define SMART_STR_PREALLOC 4096
84
85#include "zend_smart_str.h"
86#include "ext/standard/info.h"
87#include "ext/standard/file.h"
88#include "ext/standard/url.h"
89#include "php_curl.h"
90
91int  le_curl;
92int  le_curl_multi_handle;
93int  le_curl_share_handle;
94
95#ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
96static MUTEX_T *php_curl_openssl_tsl = NULL;
97
98static void php_curl_ssl_lock(int mode, int n, const char * file, int line)
99{
100	if (mode & CRYPTO_LOCK) {
101		tsrm_mutex_lock(php_curl_openssl_tsl[n]);
102	} else {
103		tsrm_mutex_unlock(php_curl_openssl_tsl[n]);
104	}
105}
106
107static unsigned long php_curl_ssl_id(void)
108{
109	return (unsigned long) tsrm_thread_id();
110}
111#endif
112/* }}} */
113
114#ifdef PHP_CURL_NEED_GNUTLS_TSL /* {{{ */
115static int php_curl_ssl_mutex_create(void **m)
116{
117	if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
118		return SUCCESS;
119	} else {
120		return FAILURE;
121	}
122}
123
124static int php_curl_ssl_mutex_destroy(void **m)
125{
126	tsrm_mutex_free(*((MUTEX_T *) m));
127	return SUCCESS;
128}
129
130static int php_curl_ssl_mutex_lock(void **m)
131{
132	return tsrm_mutex_lock(*((MUTEX_T *) m));
133}
134
135static int php_curl_ssl_mutex_unlock(void **m)
136{
137	return tsrm_mutex_unlock(*((MUTEX_T *) m));
138}
139
140static struct gcry_thread_cbs php_curl_gnutls_tsl = {
141	GCRY_THREAD_OPTION_USER,
142	NULL,
143	php_curl_ssl_mutex_create,
144	php_curl_ssl_mutex_destroy,
145	php_curl_ssl_mutex_lock,
146	php_curl_ssl_mutex_unlock
147};
148#endif
149/* }}} */
150
151static void _php_curl_close_ex(php_curl *ch);
152static void _php_curl_close(zend_resource *rsrc);
153
154
155#define SAVE_CURL_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
156
157#define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s) - 1, (zend_long) v);
158#define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s) - 1, (double) v);
159#define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s) - 1, (char *) (v ? v : ""));
160#define CAASTR(s, v) add_assoc_str_ex(return_value, s, sizeof(s) - 1, \
161		v ? zend_string_copy(v) : ZSTR_EMPTY_ALLOC());
162#define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s) -1 , (zval *) v);
163
164#if defined(PHP_WIN32) || defined(__GNUC__)
165# define php_curl_ret(__ret) RETVAL_FALSE; return __ret;
166#else
167# define php_curl_ret(__ret) RETVAL_FALSE; return;
168#endif
169
170static int php_curl_option_str(php_curl *ch, zend_long option, const char *str, const int len, zend_bool make_copy)
171{
172	CURLcode error = CURLE_OK;
173
174	if (strlen(str) != len) {
175		php_error_docref(NULL, E_WARNING, "Curl option contains invalid characters (\\0)");
176		return FAILURE;
177	}
178
179#if LIBCURL_VERSION_NUM >= 0x071100
180	if (make_copy) {
181#endif
182		char *copystr;
183
184		/* Strings passed to libcurl as 'char *' arguments, are copied by the library since 7.17.0 */
185		copystr = estrndup(str, len);
186		error = curl_easy_setopt(ch->cp, option, copystr);
187		zend_llist_add_element(&ch->to_free->str, &copystr);
188#if LIBCURL_VERSION_NUM >= 0x071100
189	} else {
190		error = curl_easy_setopt(ch->cp, option, str);
191	}
192#endif
193
194	SAVE_CURL_ERROR(ch, error)
195
196	return error == CURLE_OK ? SUCCESS : FAILURE;
197}
198
199static int php_curl_option_url(php_curl *ch, const char *url, const int len) /* {{{ */
200{
201	/* Disable file:// if open_basedir are used */
202	if (PG(open_basedir) && *PG(open_basedir)) {
203#if LIBCURL_VERSION_NUM >= 0x071304
204		curl_easy_setopt(ch->cp, CURLOPT_PROTOCOLS, CURLPROTO_ALL & ~CURLPROTO_FILE);
205#else
206		php_url *uri;
207
208		if (!(uri = php_url_parse_ex(url, len))) {
209			php_error_docref(NULL, E_WARNING, "Invalid URL '%s'", url);
210			return FAILURE;
211		}
212
213		if (uri->scheme && !strncasecmp("file", uri->scheme, sizeof("file"))) {
214			php_error_docref(NULL, E_WARNING, "Protocol 'file' disabled in cURL");
215			php_url_free(uri);
216			return FAILURE;
217		}
218		php_url_free(uri);
219#endif
220	}
221
222	return php_curl_option_str(ch, CURLOPT_URL, url, len, 0);
223}
224/* }}} */
225
226void _php_curl_verify_handlers(php_curl *ch, int reporterror) /* {{{ */
227{
228	php_stream *stream;
229
230	ZEND_ASSERT(ch && ch->handlers);
231
232	if (!Z_ISUNDEF(ch->handlers->std_err)) {
233		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->std_err, NULL, php_file_le_stream(), php_file_le_pstream());
234		if (stream == NULL) {
235			if (reporterror) {
236				php_error_docref(NULL, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr");
237			}
238			zval_ptr_dtor(&ch->handlers->std_err);
239			ZVAL_UNDEF(&ch->handlers->std_err);
240
241			curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
242		}
243	}
244	if (ch->handlers->read && !Z_ISUNDEF(ch->handlers->read->stream)) {
245		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->read->stream, NULL, php_file_le_stream(), php_file_le_pstream());
246		if (stream == NULL) {
247			if (reporterror) {
248				php_error_docref(NULL, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default");
249			}
250			zval_ptr_dtor(&ch->handlers->read->stream);
251			ZVAL_UNDEF(&ch->handlers->read->stream);
252			ch->handlers->read->res = NULL;
253			ch->handlers->read->fp = 0;
254
255			curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
256		}
257	}
258	if (ch->handlers->write_header && !Z_ISUNDEF(ch->handlers->write_header->stream)) {
259		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->write_header->stream, NULL, php_file_le_stream(), php_file_le_pstream());
260		if (stream == NULL) {
261			if (reporterror) {
262				php_error_docref(NULL, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default");
263			}
264			zval_ptr_dtor(&ch->handlers->write_header->stream);
265			ZVAL_UNDEF(&ch->handlers->write_header->stream);
266			ch->handlers->write_header->fp = 0;
267
268			ch->handlers->write_header->method = PHP_CURL_IGNORE;
269			curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
270		}
271	}
272	if (ch->handlers->write && !Z_ISUNDEF(ch->handlers->write->stream)) {
273		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->write->stream, NULL, php_file_le_stream(), php_file_le_pstream());
274		if (stream == NULL) {
275			if (reporterror) {
276				php_error_docref(NULL, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default");
277			}
278			zval_ptr_dtor(&ch->handlers->write->stream);
279			ZVAL_UNDEF(&ch->handlers->write->stream);
280			ch->handlers->write->fp = 0;
281
282			ch->handlers->write->method = PHP_CURL_STDOUT;
283			curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
284		}
285	}
286	return;
287}
288/* }}} */
289
290/* {{{ arginfo */
291ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_version, 0, 0, 0)
292	ZEND_ARG_INFO(0, version)
293ZEND_END_ARG_INFO()
294
295ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_init, 0, 0, 0)
296	ZEND_ARG_INFO(0, url)
297ZEND_END_ARG_INFO()
298
299ZEND_BEGIN_ARG_INFO(arginfo_curl_copy_handle, 0)
300	ZEND_ARG_INFO(0, ch)
301ZEND_END_ARG_INFO()
302
303ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt, 0)
304	ZEND_ARG_INFO(0, ch)
305	ZEND_ARG_INFO(0, option)
306	ZEND_ARG_INFO(0, value)
307ZEND_END_ARG_INFO()
308
309ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt_array, 0)
310	ZEND_ARG_INFO(0, ch)
311	ZEND_ARG_ARRAY_INFO(0, options, 0)
312ZEND_END_ARG_INFO()
313
314ZEND_BEGIN_ARG_INFO(arginfo_curl_exec, 0)
315	ZEND_ARG_INFO(0, ch)
316ZEND_END_ARG_INFO()
317
318ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_getinfo, 0, 0, 1)
319	ZEND_ARG_INFO(0, ch)
320	ZEND_ARG_INFO(0, option)
321ZEND_END_ARG_INFO()
322
323ZEND_BEGIN_ARG_INFO(arginfo_curl_error, 0)
324	ZEND_ARG_INFO(0, ch)
325ZEND_END_ARG_INFO()
326
327ZEND_BEGIN_ARG_INFO(arginfo_curl_errno, 0)
328	ZEND_ARG_INFO(0, ch)
329ZEND_END_ARG_INFO()
330
331ZEND_BEGIN_ARG_INFO(arginfo_curl_close, 0)
332	ZEND_ARG_INFO(0, ch)
333ZEND_END_ARG_INFO()
334
335#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
336ZEND_BEGIN_ARG_INFO(arginfo_curl_reset, 0)
337	ZEND_ARG_INFO(0, ch)
338ZEND_END_ARG_INFO()
339#endif
340
341#if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
342ZEND_BEGIN_ARG_INFO(arginfo_curl_escape, 0)
343	ZEND_ARG_INFO(0, ch)
344	ZEND_ARG_INFO(0, str)
345ZEND_END_ARG_INFO()
346
347ZEND_BEGIN_ARG_INFO(arginfo_curl_unescape, 0)
348	ZEND_ARG_INFO(0, ch)
349	ZEND_ARG_INFO(0, str)
350ZEND_END_ARG_INFO()
351
352ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_setopt, 0)
353	ZEND_ARG_INFO(0, sh)
354	ZEND_ARG_INFO(0, option)
355	ZEND_ARG_INFO(0, value)
356ZEND_END_ARG_INFO()
357#endif
358
359ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
360ZEND_END_ARG_INFO()
361
362ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_add_handle, 0)
363	ZEND_ARG_INFO(0, mh)
364	ZEND_ARG_INFO(0, ch)
365ZEND_END_ARG_INFO()
366
367ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_remove_handle, 0)
368	ZEND_ARG_INFO(0, mh)
369	ZEND_ARG_INFO(0, ch)
370ZEND_END_ARG_INFO()
371
372ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_select, 0, 0, 1)
373	ZEND_ARG_INFO(0, mh)
374	ZEND_ARG_INFO(0, timeout)
375ZEND_END_ARG_INFO()
376
377ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_exec, 0, 0, 1)
378	ZEND_ARG_INFO(0, mh)
379	ZEND_ARG_INFO(1, still_running)
380ZEND_END_ARG_INFO()
381
382ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_getcontent, 0)
383	ZEND_ARG_INFO(0, ch)
384ZEND_END_ARG_INFO()
385
386ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_info_read, 0, 0, 1)
387	ZEND_ARG_INFO(0, mh)
388	ZEND_ARG_INFO(1, msgs_in_queue)
389ZEND_END_ARG_INFO()
390
391ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_close, 0)
392	ZEND_ARG_INFO(0, mh)
393ZEND_END_ARG_INFO()
394
395#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
396ZEND_BEGIN_ARG_INFO(arginfo_curl_strerror, 0)
397	ZEND_ARG_INFO(0, errornum)
398ZEND_END_ARG_INFO()
399
400ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_strerror, 0)
401	ZEND_ARG_INFO(0, errornum)
402ZEND_END_ARG_INFO()
403#endif
404
405ZEND_BEGIN_ARG_INFO(arginfo_curl_share_init, 0)
406ZEND_END_ARG_INFO()
407
408ZEND_BEGIN_ARG_INFO(arginfo_curl_share_close, 0)
409	ZEND_ARG_INFO(0, sh)
410ZEND_END_ARG_INFO()
411
412ZEND_BEGIN_ARG_INFO(arginfo_curl_share_setopt, 0)
413	ZEND_ARG_INFO(0, sh)
414	ZEND_ARG_INFO(0, option)
415	ZEND_ARG_INFO(0, value)
416ZEND_END_ARG_INFO()
417
418#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
419ZEND_BEGIN_ARG_INFO(arginfo_curl_pause, 0)
420	ZEND_ARG_INFO(0, ch)
421	ZEND_ARG_INFO(0, bitmask)
422ZEND_END_ARG_INFO()
423#endif
424
425ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1)
426	ZEND_ARG_INFO(0, filename)
427	ZEND_ARG_INFO(0, mimetype)
428	ZEND_ARG_INFO(0, postname)
429ZEND_END_ARG_INFO()
430/* }}} */
431
432/* {{{ curl_functions[]
433 */
434const zend_function_entry curl_functions[] = {
435	PHP_FE(curl_init,                arginfo_curl_init)
436	PHP_FE(curl_copy_handle,         arginfo_curl_copy_handle)
437	PHP_FE(curl_version,             arginfo_curl_version)
438	PHP_FE(curl_setopt,              arginfo_curl_setopt)
439	PHP_FE(curl_setopt_array,        arginfo_curl_setopt_array)
440	PHP_FE(curl_exec,                arginfo_curl_exec)
441	PHP_FE(curl_getinfo,             arginfo_curl_getinfo)
442	PHP_FE(curl_error,               arginfo_curl_error)
443	PHP_FE(curl_errno,               arginfo_curl_errno)
444	PHP_FE(curl_close,               arginfo_curl_close)
445#if LIBCURL_VERSION_NUM >= 0x070c00 /* 7.12.0 */
446	PHP_FE(curl_strerror,            arginfo_curl_strerror)
447	PHP_FE(curl_multi_strerror,      arginfo_curl_multi_strerror)
448#endif
449#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
450	PHP_FE(curl_reset,               arginfo_curl_reset)
451#endif
452#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
453	PHP_FE(curl_escape,              arginfo_curl_escape)
454	PHP_FE(curl_unescape,            arginfo_curl_unescape)
455#endif
456#if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
457	PHP_FE(curl_pause,               arginfo_curl_pause)
458#endif
459	PHP_FE(curl_multi_init,          arginfo_curl_multi_init)
460	PHP_FE(curl_multi_add_handle,    arginfo_curl_multi_add_handle)
461	PHP_FE(curl_multi_remove_handle, arginfo_curl_multi_remove_handle)
462	PHP_FE(curl_multi_select,        arginfo_curl_multi_select)
463	PHP_FE(curl_multi_exec,          arginfo_curl_multi_exec)
464	PHP_FE(curl_multi_getcontent,    arginfo_curl_multi_getcontent)
465	PHP_FE(curl_multi_info_read,     arginfo_curl_multi_info_read)
466	PHP_FE(curl_multi_close,         arginfo_curl_multi_close)
467#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
468	PHP_FE(curl_multi_setopt,        arginfo_curl_multi_setopt)
469#endif
470	PHP_FE(curl_share_init,          arginfo_curl_share_init)
471	PHP_FE(curl_share_close,         arginfo_curl_share_close)
472	PHP_FE(curl_share_setopt,        arginfo_curl_share_setopt)
473	PHP_FE(curl_file_create,         arginfo_curlfile_create)
474	PHP_FE_END
475};
476/* }}} */
477
478/* {{{ curl_module_entry
479 */
480zend_module_entry curl_module_entry = {
481	STANDARD_MODULE_HEADER,
482	"curl",
483	curl_functions,
484	PHP_MINIT(curl),
485	PHP_MSHUTDOWN(curl),
486	NULL,
487	NULL,
488	PHP_MINFO(curl),
489	PHP_CURL_VERSION,
490	STANDARD_MODULE_PROPERTIES
491};
492/* }}} */
493
494#ifdef COMPILE_DL_CURL
495ZEND_GET_MODULE (curl)
496#endif
497
498/* {{{ PHP_INI_BEGIN */
499PHP_INI_BEGIN()
500	PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
501PHP_INI_END()
502/* }}} */
503
504/* {{{ PHP_MINFO_FUNCTION
505 */
506PHP_MINFO_FUNCTION(curl)
507{
508	curl_version_info_data *d;
509	char **p;
510	char str[1024];
511	size_t n = 0;
512
513	d = curl_version_info(CURLVERSION_NOW);
514	php_info_print_table_start();
515	php_info_print_table_row(2, "cURL support",    "enabled");
516	php_info_print_table_row(2, "cURL Information", d->version);
517	sprintf(str, "%d", d->age);
518	php_info_print_table_row(2, "Age", str);
519
520	/* To update on each new cURL release using src/main.c in cURL sources */
521	if (d->features) {
522		struct feat {
523			const char *name;
524			int bitmask;
525		};
526
527		unsigned int i;
528
529		static const struct feat feats[] = {
530#if LIBCURL_VERSION_NUM >= 0x070a07 /* 7.10.7 */
531			{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
532#endif
533#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
534			{"CharConv", CURL_VERSION_CONV},
535#endif
536#if LIBCURL_VERSION_NUM >= 0x070a06 /* 7.10.6 */
537			{"Debug", CURL_VERSION_DEBUG},
538			{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
539#endif
540#if LIBCURL_VERSION_NUM >= 0x070c00 /* 7.12.0 */
541			{"IDN", CURL_VERSION_IDN},
542#endif
543			{"IPv6", CURL_VERSION_IPV6},
544			{"krb4", CURL_VERSION_KERBEROS4},
545#if LIBCURL_VERSION_NUM >= 0x070b01 /* 7.11.1 */
546			{"Largefile", CURL_VERSION_LARGEFILE},
547#endif
548			{"libz", CURL_VERSION_LIBZ},
549#if LIBCURL_VERSION_NUM >= 0x070a06 /* 7.10.6 */
550			{"NTLM", CURL_VERSION_NTLM},
551#endif
552#if LIBCURL_VERSION_NUM >= 0x071600 /* 7.22.0 */
553			{"NTLMWB", CURL_VERSION_NTLM_WB},
554#endif
555#if LIBCURL_VERSION_NUM >= 0x070a08 /* 7.10.8 */
556			{"SPNEGO", CURL_VERSION_SPNEGO},
557#endif
558			{"SSL",  CURL_VERSION_SSL},
559#if LIBCURL_VERSION_NUM >= 0x070d02 /* 7.13.2 */
560			{"SSPI",  CURL_VERSION_SSPI},
561#endif
562#if LIBCURL_VERSION_NUM >= 0x071504 /* 7.21.4 */
563			{"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
564#endif
565#if LIBCURL_VERSION_NUM >= 0x072100 /* 7.33.0 */
566			{"HTTP2", CURL_VERSION_HTTP2},
567#endif
568#if LIBCURL_VERSION_NUM >= 0x072600 /* 7.38.0 */
569			{"GSSAPI", CURL_VERSION_GSSAPI},
570#endif
571#if LIBCURL_VERSION_NUM >= 0x072800 /* 7.40.0 */
572			{"KERBEROS5", CURL_VERSION_KERBEROS5},
573			{"UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS},
574#endif
575#if LIBCURL_VERSION_NUM >= 0x072f00 /* 7.47.0 */
576			{"PSL", CURL_VERSION_PSL},
577#endif
578			{NULL, 0}
579		};
580
581		php_info_print_table_row(1, "Features");
582		for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
583			if (feats[i].name) {
584				php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
585			}
586		}
587	}
588
589	n = 0;
590	p = (char **) d->protocols;
591	while (*p != NULL) {
592			n += sprintf(str + n, "%s%s", *p, *(p + 1) != NULL ? ", " : "");
593			p++;
594	}
595	php_info_print_table_row(2, "Protocols", str);
596
597	php_info_print_table_row(2, "Host", d->host);
598
599	if (d->ssl_version) {
600		php_info_print_table_row(2, "SSL Version", d->ssl_version);
601	}
602
603	if (d->libz_version) {
604		php_info_print_table_row(2, "ZLib Version", d->libz_version);
605	}
606
607#if defined(CURLVERSION_SECOND) && CURLVERSION_NOW >= CURLVERSION_SECOND
608	if (d->ares) {
609		php_info_print_table_row(2, "ZLib Version", d->ares);
610	}
611#endif
612
613#if defined(CURLVERSION_THIRD) && CURLVERSION_NOW >= CURLVERSION_THIRD
614	if (d->libidn) {
615		php_info_print_table_row(2, "libIDN Version", d->libidn);
616	}
617#endif
618
619#if LIBCURL_VERSION_NUM >= 0x071300
620
621	if (d->iconv_ver_num) {
622		php_info_print_table_row(2, "IconV Version", d->iconv_ver_num);
623	}
624
625	if (d->libssh_version) {
626		php_info_print_table_row(2, "libSSH Version", d->libssh_version);
627	}
628#endif
629	php_info_print_table_end();
630}
631/* }}} */
632
633#define REGISTER_CURL_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)
634
635/* {{{ PHP_MINIT_FUNCTION
636 */
637PHP_MINIT_FUNCTION(curl)
638{
639	le_curl = zend_register_list_destructors_ex(_php_curl_close, NULL, "curl", module_number);
640	le_curl_multi_handle = zend_register_list_destructors_ex(_php_curl_multi_close, NULL, "curl_multi", module_number);
641	le_curl_share_handle = zend_register_list_destructors_ex(_php_curl_share_close, NULL, "curl_share", module_number);
642
643	REGISTER_INI_ENTRIES();
644
645	/* See http://curl.haxx.se/lxr/source/docs/libcurl/symbols-in-versions
646	   or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
647	   of options and which version they were introduced */
648
649	/* Constants for curl_setopt() */
650	REGISTER_CURL_CONSTANT(CURLOPT_AUTOREFERER);
651	REGISTER_CURL_CONSTANT(CURLOPT_BINARYTRANSFER);
652	REGISTER_CURL_CONSTANT(CURLOPT_BUFFERSIZE);
653	REGISTER_CURL_CONSTANT(CURLOPT_CAINFO);
654	REGISTER_CURL_CONSTANT(CURLOPT_CAPATH);
655	REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT);
656	REGISTER_CURL_CONSTANT(CURLOPT_COOKIE);
657	REGISTER_CURL_CONSTANT(CURLOPT_COOKIEFILE);
658	REGISTER_CURL_CONSTANT(CURLOPT_COOKIEJAR);
659	REGISTER_CURL_CONSTANT(CURLOPT_COOKIESESSION);
660	REGISTER_CURL_CONSTANT(CURLOPT_CRLF);
661	REGISTER_CURL_CONSTANT(CURLOPT_CUSTOMREQUEST);
662	REGISTER_CURL_CONSTANT(CURLOPT_DNS_CACHE_TIMEOUT);
663	REGISTER_CURL_CONSTANT(CURLOPT_DNS_USE_GLOBAL_CACHE);
664	REGISTER_CURL_CONSTANT(CURLOPT_EGDSOCKET);
665	REGISTER_CURL_CONSTANT(CURLOPT_ENCODING);
666	REGISTER_CURL_CONSTANT(CURLOPT_FAILONERROR);
667	REGISTER_CURL_CONSTANT(CURLOPT_FILE);
668	REGISTER_CURL_CONSTANT(CURLOPT_FILETIME);
669	REGISTER_CURL_CONSTANT(CURLOPT_FOLLOWLOCATION);
670	REGISTER_CURL_CONSTANT(CURLOPT_FORBID_REUSE);
671	REGISTER_CURL_CONSTANT(CURLOPT_FRESH_CONNECT);
672	REGISTER_CURL_CONSTANT(CURLOPT_FTPAPPEND);
673	REGISTER_CURL_CONSTANT(CURLOPT_FTPLISTONLY);
674	REGISTER_CURL_CONSTANT(CURLOPT_FTPPORT);
675	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPRT);
676	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPSV);
677	REGISTER_CURL_CONSTANT(CURLOPT_HEADER);
678	REGISTER_CURL_CONSTANT(CURLOPT_HEADERFUNCTION);
679	REGISTER_CURL_CONSTANT(CURLOPT_HTTP200ALIASES);
680	REGISTER_CURL_CONSTANT(CURLOPT_HTTPGET);
681	REGISTER_CURL_CONSTANT(CURLOPT_HTTPHEADER);
682	REGISTER_CURL_CONSTANT(CURLOPT_HTTPPROXYTUNNEL);
683	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_VERSION);
684	REGISTER_CURL_CONSTANT(CURLOPT_INFILE);
685	REGISTER_CURL_CONSTANT(CURLOPT_INFILESIZE);
686	REGISTER_CURL_CONSTANT(CURLOPT_INTERFACE);
687	REGISTER_CURL_CONSTANT(CURLOPT_KRB4LEVEL);
688	REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_LIMIT);
689	REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_TIME);
690	REGISTER_CURL_CONSTANT(CURLOPT_MAXCONNECTS);
691	REGISTER_CURL_CONSTANT(CURLOPT_MAXREDIRS);
692	REGISTER_CURL_CONSTANT(CURLOPT_NETRC);
693	REGISTER_CURL_CONSTANT(CURLOPT_NOBODY);
694	REGISTER_CURL_CONSTANT(CURLOPT_NOPROGRESS);
695	REGISTER_CURL_CONSTANT(CURLOPT_NOSIGNAL);
696	REGISTER_CURL_CONSTANT(CURLOPT_PORT);
697	REGISTER_CURL_CONSTANT(CURLOPT_POST);
698	REGISTER_CURL_CONSTANT(CURLOPT_POSTFIELDS);
699	REGISTER_CURL_CONSTANT(CURLOPT_POSTQUOTE);
700	REGISTER_CURL_CONSTANT(CURLOPT_PREQUOTE);
701	REGISTER_CURL_CONSTANT(CURLOPT_PRIVATE);
702	REGISTER_CURL_CONSTANT(CURLOPT_PROGRESSFUNCTION);
703	REGISTER_CURL_CONSTANT(CURLOPT_PROXY);
704	REGISTER_CURL_CONSTANT(CURLOPT_PROXYPORT);
705	REGISTER_CURL_CONSTANT(CURLOPT_PROXYTYPE);
706	REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERPWD);
707	REGISTER_CURL_CONSTANT(CURLOPT_PUT);
708	REGISTER_CURL_CONSTANT(CURLOPT_QUOTE);
709	REGISTER_CURL_CONSTANT(CURLOPT_RANDOM_FILE);
710	REGISTER_CURL_CONSTANT(CURLOPT_RANGE);
711	REGISTER_CURL_CONSTANT(CURLOPT_READDATA);
712	REGISTER_CURL_CONSTANT(CURLOPT_READFUNCTION);
713	REGISTER_CURL_CONSTANT(CURLOPT_REFERER);
714	REGISTER_CURL_CONSTANT(CURLOPT_RESUME_FROM);
715	REGISTER_CURL_CONSTANT(CURLOPT_RETURNTRANSFER);
716	REGISTER_CURL_CONSTANT(CURLOPT_SHARE);
717	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERT);
718	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTPASSWD);
719	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTTYPE);
720	REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE);
721	REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE_DEFAULT);
722	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEY);
723	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYPASSWD);
724	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYTYPE);
725	REGISTER_CURL_CONSTANT(CURLOPT_SSLVERSION);
726	REGISTER_CURL_CONSTANT(CURLOPT_SSL_CIPHER_LIST);
727	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYHOST);
728	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYPEER);
729	REGISTER_CURL_CONSTANT(CURLOPT_STDERR);
730	REGISTER_CURL_CONSTANT(CURLOPT_TELNETOPTIONS);
731	REGISTER_CURL_CONSTANT(CURLOPT_TIMECONDITION);
732	REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT);
733	REGISTER_CURL_CONSTANT(CURLOPT_TIMEVALUE);
734	REGISTER_CURL_CONSTANT(CURLOPT_TRANSFERTEXT);
735	REGISTER_CURL_CONSTANT(CURLOPT_UNRESTRICTED_AUTH);
736	REGISTER_CURL_CONSTANT(CURLOPT_UPLOAD);
737	REGISTER_CURL_CONSTANT(CURLOPT_URL);
738	REGISTER_CURL_CONSTANT(CURLOPT_USERAGENT);
739	REGISTER_CURL_CONSTANT(CURLOPT_USERPWD);
740	REGISTER_CURL_CONSTANT(CURLOPT_VERBOSE);
741	REGISTER_CURL_CONSTANT(CURLOPT_WRITEFUNCTION);
742	REGISTER_CURL_CONSTANT(CURLOPT_WRITEHEADER);
743
744	/* */
745	REGISTER_CURL_CONSTANT(CURLE_ABORTED_BY_CALLBACK);
746	REGISTER_CURL_CONSTANT(CURLE_BAD_CALLING_ORDER);
747	REGISTER_CURL_CONSTANT(CURLE_BAD_CONTENT_ENCODING);
748	REGISTER_CURL_CONSTANT(CURLE_BAD_DOWNLOAD_RESUME);
749	REGISTER_CURL_CONSTANT(CURLE_BAD_FUNCTION_ARGUMENT);
750	REGISTER_CURL_CONSTANT(CURLE_BAD_PASSWORD_ENTERED);
751	REGISTER_CURL_CONSTANT(CURLE_COULDNT_CONNECT);
752	REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_HOST);
753	REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_PROXY);
754	REGISTER_CURL_CONSTANT(CURLE_FAILED_INIT);
755	REGISTER_CURL_CONSTANT(CURLE_FILE_COULDNT_READ_FILE);
756	REGISTER_CURL_CONSTANT(CURLE_FTP_ACCESS_DENIED);
757	REGISTER_CURL_CONSTANT(CURLE_FTP_BAD_DOWNLOAD_RESUME);
758	REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_GET_HOST);
759	REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_RECONNECT);
760	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_GET_SIZE);
761	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_RETR_FILE);
762	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_ASCII);
763	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_BINARY);
764	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_STOR_FILE);
765	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_USE_REST);
766	REGISTER_CURL_CONSTANT(CURLE_FTP_PARTIAL_FILE);
767	REGISTER_CURL_CONSTANT(CURLE_FTP_PORT_FAILED);
768	REGISTER_CURL_CONSTANT(CURLE_FTP_QUOTE_ERROR);
769	REGISTER_CURL_CONSTANT(CURLE_FTP_USER_PASSWORD_INCORRECT);
770	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_227_FORMAT);
771	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASS_REPLY);
772	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASV_REPLY);
773	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_SERVER_REPLY);
774	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_USER_REPLY);
775	REGISTER_CURL_CONSTANT(CURLE_FTP_WRITE_ERROR);
776	REGISTER_CURL_CONSTANT(CURLE_FUNCTION_NOT_FOUND);
777	REGISTER_CURL_CONSTANT(CURLE_GOT_NOTHING);
778	REGISTER_CURL_CONSTANT(CURLE_HTTP_NOT_FOUND);
779	REGISTER_CURL_CONSTANT(CURLE_HTTP_PORT_FAILED);
780	REGISTER_CURL_CONSTANT(CURLE_HTTP_POST_ERROR);
781	REGISTER_CURL_CONSTANT(CURLE_HTTP_RANGE_ERROR);
782	REGISTER_CURL_CONSTANT(CURLE_HTTP_RETURNED_ERROR);
783	REGISTER_CURL_CONSTANT(CURLE_LDAP_CANNOT_BIND);
784	REGISTER_CURL_CONSTANT(CURLE_LDAP_SEARCH_FAILED);
785	REGISTER_CURL_CONSTANT(CURLE_LIBRARY_NOT_FOUND);
786	REGISTER_CURL_CONSTANT(CURLE_MALFORMAT_USER);
787	REGISTER_CURL_CONSTANT(CURLE_OBSOLETE);
788	REGISTER_CURL_CONSTANT(CURLE_OK);
789	REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEDOUT);
790	REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEOUTED);
791	REGISTER_CURL_CONSTANT(CURLE_OUT_OF_MEMORY);
792	REGISTER_CURL_CONSTANT(CURLE_PARTIAL_FILE);
793	REGISTER_CURL_CONSTANT(CURLE_READ_ERROR);
794	REGISTER_CURL_CONSTANT(CURLE_RECV_ERROR);
795	REGISTER_CURL_CONSTANT(CURLE_SEND_ERROR);
796	REGISTER_CURL_CONSTANT(CURLE_SHARE_IN_USE);
797	REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT);
798	REGISTER_CURL_CONSTANT(CURLE_SSL_CERTPROBLEM);
799	REGISTER_CURL_CONSTANT(CURLE_SSL_CIPHER);
800	REGISTER_CURL_CONSTANT(CURLE_SSL_CONNECT_ERROR);
801	REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_NOTFOUND);
802	REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_SETFAILED);
803	REGISTER_CURL_CONSTANT(CURLE_SSL_PEER_CERTIFICATE);
804	REGISTER_CURL_CONSTANT(CURLE_TELNET_OPTION_SYNTAX);
805	REGISTER_CURL_CONSTANT(CURLE_TOO_MANY_REDIRECTS);
806	REGISTER_CURL_CONSTANT(CURLE_UNKNOWN_TELNET_OPTION);
807	REGISTER_CURL_CONSTANT(CURLE_UNSUPPORTED_PROTOCOL);
808	REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT);
809	REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT_USER);
810	REGISTER_CURL_CONSTANT(CURLE_WRITE_ERROR);
811
812	/* cURL info constants */
813	REGISTER_CURL_CONSTANT(CURLINFO_CONNECT_TIME);
814	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
815	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_UPLOAD);
816	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_TYPE);
817	REGISTER_CURL_CONSTANT(CURLINFO_EFFECTIVE_URL);
818	REGISTER_CURL_CONSTANT(CURLINFO_FILETIME);
819	REGISTER_CURL_CONSTANT(CURLINFO_HEADER_OUT);
820	REGISTER_CURL_CONSTANT(CURLINFO_HEADER_SIZE);
821	REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CODE);
822	REGISTER_CURL_CONSTANT(CURLINFO_LASTONE);
823	REGISTER_CURL_CONSTANT(CURLINFO_NAMELOOKUP_TIME);
824	REGISTER_CURL_CONSTANT(CURLINFO_PRETRANSFER_TIME);
825	REGISTER_CURL_CONSTANT(CURLINFO_PRIVATE);
826	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_COUNT);
827	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME);
828	REGISTER_CURL_CONSTANT(CURLINFO_REQUEST_SIZE);
829	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_DOWNLOAD);
830	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_UPLOAD);
831	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_DOWNLOAD);
832	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_UPLOAD);
833	REGISTER_CURL_CONSTANT(CURLINFO_SSL_VERIFYRESULT);
834	REGISTER_CURL_CONSTANT(CURLINFO_STARTTRANSFER_TIME);
835	REGISTER_CURL_CONSTANT(CURLINFO_TOTAL_TIME);
836
837	/* Other */
838	REGISTER_CURL_CONSTANT(CURLMSG_DONE);
839	REGISTER_CURL_CONSTANT(CURLVERSION_NOW);
840
841	/* Curl Multi Constants */
842	REGISTER_CURL_CONSTANT(CURLM_BAD_EASY_HANDLE);
843	REGISTER_CURL_CONSTANT(CURLM_BAD_HANDLE);
844	REGISTER_CURL_CONSTANT(CURLM_CALL_MULTI_PERFORM);
845	REGISTER_CURL_CONSTANT(CURLM_INTERNAL_ERROR);
846	REGISTER_CURL_CONSTANT(CURLM_OK);
847	REGISTER_CURL_CONSTANT(CURLM_OUT_OF_MEMORY);
848#if LIBCURL_VERSION_NUM >= 0x072001 /* Available since 7.32.1 */
849	REGISTER_CURL_CONSTANT(CURLM_ADDED_ALREADY);
850#endif
851
852	/* Curl proxy constants */
853	REGISTER_CURL_CONSTANT(CURLPROXY_HTTP);
854	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4);
855	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5);
856
857	/* Curl Share constants */
858	REGISTER_CURL_CONSTANT(CURLSHOPT_NONE);
859	REGISTER_CURL_CONSTANT(CURLSHOPT_SHARE);
860	REGISTER_CURL_CONSTANT(CURLSHOPT_UNSHARE);
861
862	/* Curl Http Version constants (CURLOPT_HTTP_VERSION) */
863	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_0);
864	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_1);
865	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_NONE);
866
867	/* Curl Lock constants */
868	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_COOKIE);
869	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_DNS);
870	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_SSL_SESSION);
871
872	/* Curl NETRC constants (CURLOPT_NETRC) */
873	REGISTER_CURL_CONSTANT(CURL_NETRC_IGNORED);
874	REGISTER_CURL_CONSTANT(CURL_NETRC_OPTIONAL);
875	REGISTER_CURL_CONSTANT(CURL_NETRC_REQUIRED);
876
877	/* Curl SSL Version constants (CURLOPT_SSLVERSION) */
878	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_DEFAULT);
879	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_SSLv2);
880	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_SSLv3);
881	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1);
882
883	/* Curl TIMECOND constants (CURLOPT_TIMECONDITION) */
884	REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFMODSINCE);
885	REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFUNMODSINCE);
886	REGISTER_CURL_CONSTANT(CURL_TIMECOND_LASTMOD);
887	REGISTER_CURL_CONSTANT(CURL_TIMECOND_NONE);
888
889	/* Curl version constants */
890	REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
891	REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS4);
892	REGISTER_CURL_CONSTANT(CURL_VERSION_LIBZ);
893	REGISTER_CURL_CONSTANT(CURL_VERSION_SSL);
894
895#if LIBCURL_VERSION_NUM >= 0x070a06 /* Available since 7.10.6 */
896	REGISTER_CURL_CONSTANT(CURLOPT_HTTPAUTH);
897	/* http authentication options */
898	REGISTER_CURL_CONSTANT(CURLAUTH_ANY);
899	REGISTER_CURL_CONSTANT(CURLAUTH_ANYSAFE);
900	REGISTER_CURL_CONSTANT(CURLAUTH_BASIC);
901	REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST);
902	REGISTER_CURL_CONSTANT(CURLAUTH_GSSNEGOTIATE);
903	REGISTER_CURL_CONSTANT(CURLAUTH_NONE);
904	REGISTER_CURL_CONSTANT(CURLAUTH_NTLM);
905#endif
906
907#if LIBCURL_VERSION_NUM >= 0x070a07 /* Available since 7.10.7 */
908	REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CONNECTCODE);
909	REGISTER_CURL_CONSTANT(CURLOPT_FTP_CREATE_MISSING_DIRS);
910	REGISTER_CURL_CONSTANT(CURLOPT_PROXYAUTH);
911#endif
912
913#if LIBCURL_VERSION_NUM >= 0x070a08 /* Available since 7.10.8 */
914	REGISTER_CURL_CONSTANT(CURLE_FILESIZE_EXCEEDED);
915	REGISTER_CURL_CONSTANT(CURLE_LDAP_INVALID_URL);
916	REGISTER_CURL_CONSTANT(CURLINFO_HTTPAUTH_AVAIL);
917	REGISTER_CURL_CONSTANT(CURLINFO_RESPONSE_CODE);
918	REGISTER_CURL_CONSTANT(CURLINFO_PROXYAUTH_AVAIL);
919	REGISTER_CURL_CONSTANT(CURLOPT_FTP_RESPONSE_TIMEOUT);
920	REGISTER_CURL_CONSTANT(CURLOPT_IPRESOLVE);
921	REGISTER_CURL_CONSTANT(CURLOPT_MAXFILESIZE);
922	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V4);
923	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V6);
924	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_WHATEVER);
925#endif
926
927#if LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
928	REGISTER_CURL_CONSTANT(CURLE_FTP_SSL_FAILED);
929	REGISTER_CURL_CONSTANT(CURLFTPSSL_ALL);
930	REGISTER_CURL_CONSTANT(CURLFTPSSL_CONTROL);
931	REGISTER_CURL_CONSTANT(CURLFTPSSL_NONE);
932	REGISTER_CURL_CONSTANT(CURLFTPSSL_TRY);
933	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL);
934	REGISTER_CURL_CONSTANT(CURLOPT_NETRC_FILE);
935#endif
936
937#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
938	REGISTER_CURL_CONSTANT(CURLFTPAUTH_DEFAULT);
939	REGISTER_CURL_CONSTANT(CURLFTPAUTH_SSL);
940	REGISTER_CURL_CONSTANT(CURLFTPAUTH_TLS);
941	REGISTER_CURL_CONSTANT(CURLOPT_FTPSSLAUTH);
942#endif
943
944#if LIBCURL_VERSION_NUM >= 0x070d00 /* Available since 7.13.0 */
945	REGISTER_CURL_CONSTANT(CURLOPT_FTP_ACCOUNT);
946#endif
947
948#if LIBCURL_VERSION_NUM >= 0x070b02 /* Available since 7.11.2 */
949	REGISTER_CURL_CONSTANT(CURLOPT_TCP_NODELAY);
950#endif
951
952#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
953	REGISTER_CURL_CONSTANT(CURLINFO_OS_ERRNO);
954#endif
955
956#if LIBCURL_VERSION_NUM >= 0x070c03 /* Available since 7.12.3 */
957	REGISTER_CURL_CONSTANT(CURLINFO_NUM_CONNECTS);
958	REGISTER_CURL_CONSTANT(CURLINFO_SSL_ENGINES);
959#endif
960
961#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
962	REGISTER_CURL_CONSTANT(CURLINFO_COOKIELIST);
963	REGISTER_CURL_CONSTANT(CURLOPT_COOKIELIST);
964	REGISTER_CURL_CONSTANT(CURLOPT_IGNORE_CONTENT_LENGTH);
965#endif
966
967#if LIBCURL_VERSION_NUM >= 0x070f00 /* Available since 7.15.0 */
968	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SKIP_PASV_IP);
969#endif
970
971#if LIBCURL_VERSION_NUM >= 0x070f01 /* Available since 7.15.1 */
972	REGISTER_CURL_CONSTANT(CURLOPT_FTP_FILEMETHOD);
973#endif
974
975#if LIBCURL_VERSION_NUM >= 0x070f02 /* Available since 7.15.2 */
976	REGISTER_CURL_CONSTANT(CURLOPT_CONNECT_ONLY);
977	REGISTER_CURL_CONSTANT(CURLOPT_LOCALPORT);
978	REGISTER_CURL_CONSTANT(CURLOPT_LOCALPORTRANGE);
979#endif
980
981#if LIBCURL_VERSION_NUM >= 0x070f03 /* Available since 7.15.3 */
982	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_MULTICWD);
983	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_NOCWD);
984	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_SINGLECWD);
985#endif
986
987#if LIBCURL_VERSION_NUM >= 0x070f04 /* Available since 7.15.4 */
988	REGISTER_CURL_CONSTANT(CURLINFO_FTP_ENTRY_PATH);
989#endif
990
991#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
992	REGISTER_CURL_CONSTANT(CURLOPT_FTP_ALTERNATIVE_TO_USER);
993	REGISTER_CURL_CONSTANT(CURLOPT_MAX_RECV_SPEED_LARGE);
994	REGISTER_CURL_CONSTANT(CURLOPT_MAX_SEND_SPEED_LARGE);
995#endif
996
997#if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
998	REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT_BADFILE);
999	REGISTER_CURL_CONSTANT(CURLOPT_SSL_SESSIONID_CACHE);
1000	REGISTER_CURL_CONSTANT(CURLMOPT_PIPELINING);
1001#endif
1002
1003#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
1004	REGISTER_CURL_CONSTANT(CURLE_SSH);
1005	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL_CCC);
1006	REGISTER_CURL_CONSTANT(CURLOPT_SSH_AUTH_TYPES);
1007	REGISTER_CURL_CONSTANT(CURLOPT_SSH_PRIVATE_KEYFILE);
1008	REGISTER_CURL_CONSTANT(CURLOPT_SSH_PUBLIC_KEYFILE);
1009	REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_ACTIVE);
1010	REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_NONE);
1011	REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_PASSIVE);
1012#endif
1013
1014#if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
1015	REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT_MS);
1016	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_CONTENT_DECODING);
1017	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_TRANSFER_DECODING);
1018	REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
1019#endif
1020
1021#if LIBCURL_VERSION_NUM >= 0x071003 /* Available since 7.16.3 */
1022	REGISTER_CURL_CONSTANT(CURLMOPT_MAXCONNECTS);
1023#endif
1024
1025#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
1026	REGISTER_CURL_CONSTANT(CURLOPT_KRBLEVEL);
1027	REGISTER_CURL_CONSTANT(CURLOPT_NEW_DIRECTORY_PERMS);
1028	REGISTER_CURL_CONSTANT(CURLOPT_NEW_FILE_PERMS);
1029#endif
1030
1031#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
1032	REGISTER_CURL_CONSTANT(CURLOPT_APPEND);
1033	REGISTER_CURL_CONSTANT(CURLOPT_DIRLISTONLY);
1034	REGISTER_CURL_CONSTANT(CURLOPT_USE_SSL);
1035	/* Curl SSL Constants */
1036	REGISTER_CURL_CONSTANT(CURLUSESSL_ALL);
1037	REGISTER_CURL_CONSTANT(CURLUSESSL_CONTROL);
1038	REGISTER_CURL_CONSTANT(CURLUSESSL_NONE);
1039	REGISTER_CURL_CONSTANT(CURLUSESSL_TRY);
1040#endif
1041
1042#if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
1043	REGISTER_CURL_CONSTANT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
1044#endif
1045
1046#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
1047	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TRANSFER_MODE);
1048	REGISTER_CURL_CONSTANT(CURLPAUSE_ALL);
1049	REGISTER_CURL_CONSTANT(CURLPAUSE_CONT);
1050	REGISTER_CURL_CONSTANT(CURLPAUSE_RECV);
1051	REGISTER_CURL_CONSTANT(CURLPAUSE_RECV_CONT);
1052	REGISTER_CURL_CONSTANT(CURLPAUSE_SEND);
1053	REGISTER_CURL_CONSTANT(CURLPAUSE_SEND_CONT);
1054	REGISTER_CURL_CONSTANT(CURL_READFUNC_PAUSE);
1055	REGISTER_CURL_CONSTANT(CURL_WRITEFUNC_PAUSE);
1056
1057	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4A);
1058	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5_HOSTNAME);
1059#endif
1060
1061#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
1062	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_URL);
1063#endif
1064
1065#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
1066	REGISTER_CURL_CONSTANT(CURLINFO_APPCONNECT_TIME);
1067	REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_IP);
1068
1069	REGISTER_CURL_CONSTANT(CURLOPT_ADDRESS_SCOPE);
1070	REGISTER_CURL_CONSTANT(CURLOPT_CRLFILE);
1071	REGISTER_CURL_CONSTANT(CURLOPT_ISSUERCERT);
1072	REGISTER_CURL_CONSTANT(CURLOPT_KEYPASSWD);
1073
1074	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_ANY);
1075	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_DEFAULT);
1076	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_HOST);
1077	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_KEYBOARD);
1078	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_NONE);
1079	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PASSWORD);
1080	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PUBLICKEY);
1081#endif
1082
1083#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1084	REGISTER_CURL_CONSTANT(CURLINFO_CERTINFO);
1085	REGISTER_CURL_CONSTANT(CURLOPT_CERTINFO);
1086	REGISTER_CURL_CONSTANT(CURLOPT_PASSWORD);
1087	REGISTER_CURL_CONSTANT(CURLOPT_POSTREDIR);
1088	REGISTER_CURL_CONSTANT(CURLOPT_PROXYPASSWORD);
1089	REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERNAME);
1090	REGISTER_CURL_CONSTANT(CURLOPT_USERNAME);
1091	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_301);
1092	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_302);
1093	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_ALL);
1094#endif
1095
1096#if LIBCURL_VERSION_NUM >= 0x071303 /* Available since 7.19.3 */
1097	REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST_IE);
1098#endif
1099
1100#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
1101	REGISTER_CURL_CONSTANT(CURLINFO_CONDITION_UNMET);
1102
1103	REGISTER_CURL_CONSTANT(CURLOPT_NOPROXY);
1104	REGISTER_CURL_CONSTANT(CURLOPT_PROTOCOLS);
1105	REGISTER_CURL_CONSTANT(CURLOPT_REDIR_PROTOCOLS);
1106	REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_GSSAPI_NEC);
1107	REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_GSSAPI_SERVICE);
1108	REGISTER_CURL_CONSTANT(CURLOPT_TFTP_BLKSIZE);
1109
1110	REGISTER_CURL_CONSTANT(CURLPROTO_ALL);
1111	REGISTER_CURL_CONSTANT(CURLPROTO_DICT);
1112	REGISTER_CURL_CONSTANT(CURLPROTO_FILE);
1113	REGISTER_CURL_CONSTANT(CURLPROTO_FTP);
1114	REGISTER_CURL_CONSTANT(CURLPROTO_FTPS);
1115	REGISTER_CURL_CONSTANT(CURLPROTO_HTTP);
1116	REGISTER_CURL_CONSTANT(CURLPROTO_HTTPS);
1117	REGISTER_CURL_CONSTANT(CURLPROTO_LDAP);
1118	REGISTER_CURL_CONSTANT(CURLPROTO_LDAPS);
1119	REGISTER_CURL_CONSTANT(CURLPROTO_SCP);
1120	REGISTER_CURL_CONSTANT(CURLPROTO_SFTP);
1121	REGISTER_CURL_CONSTANT(CURLPROTO_TELNET);
1122	REGISTER_CURL_CONSTANT(CURLPROTO_TFTP);
1123
1124	REGISTER_CURL_CONSTANT(CURLPROXY_HTTP_1_0);
1125
1126	REGISTER_CURL_CONSTANT(CURLFTP_CREATE_DIR);
1127	REGISTER_CURL_CONSTANT(CURLFTP_CREATE_DIR_NONE);
1128	REGISTER_CURL_CONSTANT(CURLFTP_CREATE_DIR_RETRY);
1129#endif
1130
1131#if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
1132	REGISTER_CURL_CONSTANT(CURLOPT_SSH_KNOWNHOSTS);
1133#endif
1134
1135#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
1136	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_CLIENT_CSEQ);
1137	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_CSEQ_RECV);
1138	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_SERVER_CSEQ);
1139	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_SESSION_ID);
1140	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_PRET);
1141	REGISTER_CURL_CONSTANT(CURLOPT_MAIL_FROM);
1142	REGISTER_CURL_CONSTANT(CURLOPT_MAIL_RCPT);
1143	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_CLIENT_CSEQ);
1144	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_REQUEST);
1145	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_SERVER_CSEQ);
1146	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_SESSION_ID);
1147	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_STREAM_URI);
1148	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_TRANSPORT);
1149	REGISTER_CURL_CONSTANT(CURLPROTO_IMAP);
1150	REGISTER_CURL_CONSTANT(CURLPROTO_IMAPS);
1151	REGISTER_CURL_CONSTANT(CURLPROTO_POP3);
1152	REGISTER_CURL_CONSTANT(CURLPROTO_POP3S);
1153	REGISTER_CURL_CONSTANT(CURLPROTO_RTSP);
1154	REGISTER_CURL_CONSTANT(CURLPROTO_SMTP);
1155	REGISTER_CURL_CONSTANT(CURLPROTO_SMTPS);
1156	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_ANNOUNCE);
1157	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_DESCRIBE);
1158	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_GET_PARAMETER);
1159	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_OPTIONS);
1160	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_PAUSE);
1161	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_PLAY);
1162	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_RECEIVE);
1163	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_RECORD);
1164	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_SET_PARAMETER);
1165	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_SETUP);
1166	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_TEARDOWN);
1167#endif
1168
1169#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1170	REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_IP);
1171	REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_PORT);
1172	REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_PORT);
1173	REGISTER_CURL_CONSTANT(CURLOPT_FNMATCH_FUNCTION);
1174	REGISTER_CURL_CONSTANT(CURLOPT_WILDCARDMATCH);
1175	REGISTER_CURL_CONSTANT(CURLPROTO_RTMP);
1176	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPE);
1177	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPS);
1178	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPT);
1179	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPTE);
1180	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPTS);
1181	REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_FAIL);
1182	REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_MATCH);
1183	REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_NOMATCH);
1184#endif
1185
1186#if LIBCURL_VERSION_NUM >= 0x071502 /* Available since 7.21.2 */
1187	REGISTER_CURL_CONSTANT(CURLPROTO_GOPHER);
1188#endif
1189
1190#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
1191	REGISTER_CURL_CONSTANT(CURLAUTH_ONLY);
1192	REGISTER_CURL_CONSTANT(CURLOPT_RESOLVE);
1193#endif
1194
1195#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
1196	REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_PASSWORD);
1197	REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_TYPE);
1198	REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_USERNAME);
1199	REGISTER_CURL_CONSTANT(CURL_TLSAUTH_SRP);
1200#endif
1201
1202#if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
1203	REGISTER_CURL_CONSTANT(CURLOPT_ACCEPT_ENCODING);
1204	REGISTER_CURL_CONSTANT(CURLOPT_TRANSFER_ENCODING);
1205#endif
1206
1207#if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
1208	REGISTER_CURL_CONSTANT(CURLAUTH_NTLM_WB);
1209	REGISTER_CURL_CONSTANT(CURLGSSAPI_DELEGATION_FLAG);
1210	REGISTER_CURL_CONSTANT(CURLGSSAPI_DELEGATION_POLICY_FLAG);
1211	REGISTER_CURL_CONSTANT(CURLOPT_GSSAPI_DELEGATION);
1212#endif
1213
1214#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
1215	REGISTER_CURL_CONSTANT(CURLOPT_ACCEPTTIMEOUT_MS);
1216	REGISTER_CURL_CONSTANT(CURLOPT_DNS_SERVERS);
1217#endif
1218
1219#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
1220	REGISTER_CURL_CONSTANT(CURLOPT_MAIL_AUTH);
1221	REGISTER_CURL_CONSTANT(CURLOPT_SSL_OPTIONS);
1222	REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPALIVE);
1223	REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPIDLE);
1224	REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPINTVL);
1225	REGISTER_CURL_CONSTANT(CURLSSLOPT_ALLOW_BEAST);
1226#endif
1227
1228#if LIBCURL_VERSION_NUM >= 0x071901 /* Available since 7.25.1 */
1229	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_303);
1230#endif
1231
1232#if LIBCURL_VERSION_NUM >= 0x071c00 /* Available since 7.28.0 */
1233	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_AGENT);
1234#endif
1235
1236#if LIBCURL_VERSION_NUM >= 0x071e00 /* Available since 7.30.0 */
1237	REGISTER_CURL_CONSTANT(CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE);
1238	REGISTER_CURL_CONSTANT(CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE);
1239	REGISTER_CURL_CONSTANT(CURLMOPT_MAX_HOST_CONNECTIONS);
1240	REGISTER_CURL_CONSTANT(CURLMOPT_MAX_PIPELINE_LENGTH);
1241	REGISTER_CURL_CONSTANT(CURLMOPT_MAX_TOTAL_CONNECTIONS);
1242#endif
1243
1244#if LIBCURL_VERSION_NUM >= 0x071f00 /* Available since 7.31.0 */
1245	REGISTER_CURL_CONSTANT(CURLOPT_SASL_IR);
1246#endif
1247
1248#if LIBCURL_VERSION_NUM >= 0x072100 /* Available since 7.33.0 */
1249	REGISTER_CURL_CONSTANT(CURLOPT_DNS_INTERFACE);
1250	REGISTER_CURL_CONSTANT(CURLOPT_DNS_LOCAL_IP4);
1251	REGISTER_CURL_CONSTANT(CURLOPT_DNS_LOCAL_IP6);
1252	REGISTER_CURL_CONSTANT(CURLOPT_XOAUTH2_BEARER);
1253
1254	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2_0);
1255	REGISTER_CURL_CONSTANT(CURL_VERSION_HTTP2);
1256#endif
1257
1258#if LIBCURL_VERSION_NUM >= 0x072200 /* Available since 7.34.0 */
1259	REGISTER_CURL_CONSTANT(CURLOPT_LOGIN_OPTIONS);
1260
1261	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_0);
1262	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_1);
1263	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_2);
1264#endif
1265
1266#if LIBCURL_VERSION_NUM >= 0x072400 /* Available since 7.36.0 */
1267	REGISTER_CURL_CONSTANT(CURLOPT_EXPECT_100_TIMEOUT_MS);
1268	REGISTER_CURL_CONSTANT(CURLOPT_SSL_ENABLE_ALPN);
1269	REGISTER_CURL_CONSTANT(CURLOPT_SSL_ENABLE_NPN);
1270#endif
1271
1272#if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
1273	REGISTER_CURL_CONSTANT(CURLHEADER_SEPARATE);
1274	REGISTER_CURL_CONSTANT(CURLHEADER_UNIFIED);
1275	REGISTER_CURL_CONSTANT(CURLOPT_HEADEROPT);
1276	REGISTER_CURL_CONSTANT(CURLOPT_PROXYHEADER);
1277#endif
1278
1279#if LIBCURL_VERSION_NUM >= 0x072600 /* Available since 7.38.0 */
1280	REGISTER_CURL_CONSTANT(CURLAUTH_NEGOTIATE);
1281#endif
1282
1283#if LIBCURL_VERSION_NUM >= 0x072700 /* Available since 7.39.0 */
1284	REGISTER_CURL_CONSTANT(CURLOPT_PINNEDPUBLICKEY);
1285#endif
1286
1287#if LIBCURL_VERSION_NUM >= 0x072800 /* Available since 7.40.0 */
1288	REGISTER_CURL_CONSTANT(CURLOPT_UNIX_SOCKET_PATH);
1289
1290	REGISTER_CURL_CONSTANT(CURLPROTO_SMB);
1291	REGISTER_CURL_CONSTANT(CURLPROTO_SMBS);
1292#endif
1293
1294#if LIBCURL_VERSION_NUM >= 0x072900 /* Available since 7.41.0 */
1295	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYSTATUS);
1296#endif
1297
1298#if LIBCURL_VERSION_NUM >= 0x072a00 /* Available since 7.42.0 */
1299	REGISTER_CURL_CONSTANT(CURLOPT_PATH_AS_IS);
1300	REGISTER_CURL_CONSTANT(CURLOPT_SSL_FALSESTART);
1301#endif
1302
1303#if LIBCURL_VERSION_NUM >= 0x072b00 /* Available since 7.43.0 */
1304	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2);
1305
1306	REGISTER_CURL_CONSTANT(CURLOPT_PIPEWAIT);
1307	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SERVICE_NAME);
1308	REGISTER_CURL_CONSTANT(CURLOPT_SERVICE_NAME);
1309
1310	REGISTER_CURL_CONSTANT(CURLPIPE_NOTHING);
1311	REGISTER_CURL_CONSTANT(CURLPIPE_HTTP1);
1312	REGISTER_CURL_CONSTANT(CURLPIPE_MULTIPLEX);
1313#endif
1314
1315#if LIBCURL_VERSION_NUM >= 0x072c00 /* Available since 7.44.0 */
1316	REGISTER_CURL_CONSTANT(CURLSSLOPT_NO_REVOKE);
1317#endif
1318
1319#if LIBCURL_VERSION_NUM >= 0x072d00 /* Available since 7.45.0 */
1320	REGISTER_CURL_CONSTANT(CURLOPT_DEFAULT_PROTOCOL);
1321#endif
1322
1323#if LIBCURL_VERSION_NUM >= 0x072e00 /* Available since 7.46.0 */
1324	REGISTER_CURL_CONSTANT(CURLOPT_STREAM_WEIGHT);
1325#endif
1326
1327#if LIBCURL_VERSION_NUM >= 0x072f00 /* Available since 7.47.0 */
1328	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2TLS);
1329#endif
1330
1331#if LIBCURL_VERSION_NUM >= 0x073000 /* Available since 7.48.0 */
1332	REGISTER_CURL_CONSTANT(CURLOPT_TFTP_NO_OPTIONS);
1333#endif
1334
1335#if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
1336	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
1337	REGISTER_CURL_CONSTANT(CURLOPT_CONNECT_TO);
1338	REGISTER_CURL_CONSTANT(CURLOPT_TCP_FASTOPEN);
1339#endif
1340
1341#if CURLOPT_FTPASCII != 0
1342	REGISTER_CURL_CONSTANT(CURLOPT_FTPASCII);
1343#endif
1344#if CURLOPT_MUTE != 0
1345	REGISTER_CURL_CONSTANT(CURLOPT_MUTE);
1346#endif
1347#if CURLOPT_PASSWDFUNCTION != 0
1348	REGISTER_CURL_CONSTANT(CURLOPT_PASSWDFUNCTION);
1349#endif
1350	REGISTER_CURL_CONSTANT(CURLOPT_SAFE_UPLOAD);
1351
1352#ifdef PHP_CURL_NEED_OPENSSL_TSL
1353	if (!CRYPTO_get_id_callback()) {
1354		int i, c = CRYPTO_num_locks();
1355
1356		php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
1357		if (!php_curl_openssl_tsl) {
1358			return FAILURE;
1359		}
1360
1361		for (i = 0; i < c; ++i) {
1362			php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
1363		}
1364
1365		CRYPTO_set_id_callback(php_curl_ssl_id);
1366		CRYPTO_set_locking_callback(php_curl_ssl_lock);
1367	}
1368#endif
1369#ifdef PHP_CURL_NEED_GNUTLS_TSL
1370	gcry_control(GCRYCTL_SET_THREAD_CBS, &php_curl_gnutls_tsl);
1371#endif
1372
1373	if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
1374		return FAILURE;
1375	}
1376
1377	curlfile_register_class();
1378
1379	return SUCCESS;
1380}
1381/* }}} */
1382
1383/* {{{ PHP_MSHUTDOWN_FUNCTION
1384 */
1385PHP_MSHUTDOWN_FUNCTION(curl)
1386{
1387	curl_global_cleanup();
1388#ifdef PHP_CURL_NEED_OPENSSL_TSL
1389	if (php_curl_openssl_tsl) {
1390		int i, c = CRYPTO_num_locks();
1391
1392		CRYPTO_set_id_callback(NULL);
1393		CRYPTO_set_locking_callback(NULL);
1394
1395		for (i = 0; i < c; ++i) {
1396			tsrm_mutex_free(php_curl_openssl_tsl[i]);
1397		}
1398
1399		free(php_curl_openssl_tsl);
1400		php_curl_openssl_tsl = NULL;
1401	}
1402#endif
1403	UNREGISTER_INI_ENTRIES();
1404	return SUCCESS;
1405}
1406/* }}} */
1407
1408/* {{{ curl_write_nothing
1409 * Used as a work around. See _php_curl_close_ex
1410 */
1411static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ctx)
1412{
1413	return size * nmemb;
1414}
1415/* }}} */
1416
1417/* {{{ curl_write
1418 */
1419static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
1420{
1421	php_curl *ch = (php_curl *) ctx;
1422	php_curl_write *t = ch->handlers->write;
1423	size_t length = size * nmemb;
1424
1425#if PHP_CURL_DEBUG
1426	fprintf(stderr, "curl_write() called\n");
1427	fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
1428#endif
1429
1430	switch (t->method) {
1431		case PHP_CURL_STDOUT:
1432			PHPWRITE(data, length);
1433			break;
1434		case PHP_CURL_FILE:
1435			return fwrite(data, size, nmemb, t->fp);
1436		case PHP_CURL_RETURN:
1437			if (length > 0) {
1438				smart_str_appendl(&t->buf, data, (int) length);
1439			}
1440			break;
1441		case PHP_CURL_USER: {
1442			zval argv[2];
1443			zval retval;
1444			int  error;
1445			zend_fcall_info fci;
1446
1447			ZVAL_RES(&argv[0], ch->res);
1448			Z_ADDREF(argv[0]);
1449			ZVAL_STRINGL(&argv[1], data, length);
1450
1451			fci.size = sizeof(fci);
1452			fci.object = NULL;
1453			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1454			fci.retval = &retval;
1455			fci.param_count = 2;
1456			fci.params = argv;
1457			fci.no_separation = 0;
1458
1459			ch->in_callback = 1;
1460			error = zend_call_function(&fci, &t->fci_cache);
1461			ch->in_callback = 0;
1462			if (error == FAILURE) {
1463				php_error_docref(NULL, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
1464				length = -1;
1465			} else if (!Z_ISUNDEF(retval)) {
1466				_php_curl_verify_handlers(ch, 1);
1467				length = zval_get_long(&retval);
1468			}
1469
1470			zval_ptr_dtor(&argv[0]);
1471			zval_ptr_dtor(&argv[1]);
1472			break;
1473		}
1474	}
1475
1476	return length;
1477}
1478/* }}} */
1479
1480#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1481/* {{{ curl_fnmatch
1482 */
1483static int curl_fnmatch(void *ctx, const char *pattern, const char *string)
1484{
1485	php_curl *ch = (php_curl *) ctx;
1486	php_curl_fnmatch *t = ch->handlers->fnmatch;
1487	int rval = CURL_FNMATCHFUNC_FAIL;
1488	switch (t->method) {
1489		case PHP_CURL_USER: {
1490			zval argv[3];
1491			zval retval;
1492			int  error;
1493			zend_fcall_info fci;
1494
1495			ZVAL_RES(&argv[0], ch->res);
1496			Z_ADDREF(argv[0]);
1497			ZVAL_STRING(&argv[1], pattern);
1498			ZVAL_STRING(&argv[2], string);
1499
1500			fci.size = sizeof(fci);
1501			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1502			fci.object = NULL;
1503			fci.retval = &retval;
1504			fci.param_count = 3;
1505			fci.params = argv;
1506			fci.no_separation = 0;
1507
1508			ch->in_callback = 1;
1509			error = zend_call_function(&fci, &t->fci_cache);
1510			ch->in_callback = 0;
1511			if (error == FAILURE) {
1512				php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_FNMATCH_FUNCTION");
1513			} else if (!Z_ISUNDEF(retval)) {
1514				_php_curl_verify_handlers(ch, 1);
1515				rval = zval_get_long(&retval);
1516			}
1517			zval_ptr_dtor(&argv[0]);
1518			zval_ptr_dtor(&argv[1]);
1519			zval_ptr_dtor(&argv[2]);
1520			break;
1521		}
1522	}
1523	return rval;
1524}
1525/* }}} */
1526#endif
1527
1528/* {{{ curl_progress
1529 */
1530static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
1531{
1532	php_curl *ch = (php_curl *)clientp;
1533	php_curl_progress *t = ch->handlers->progress;
1534	size_t	rval = 0;
1535
1536#if PHP_CURL_DEBUG
1537	fprintf(stderr, "curl_progress() called\n");
1538	fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
1539#endif
1540
1541	switch (t->method) {
1542		case PHP_CURL_USER: {
1543			zval argv[5];
1544			zval retval;
1545			int  error;
1546			zend_fcall_info fci;
1547
1548			ZVAL_RES(&argv[0], ch->res);
1549			Z_ADDREF(argv[0]);
1550			ZVAL_LONG(&argv[1], (zend_long)dltotal);
1551			ZVAL_LONG(&argv[2], (zend_long)dlnow);
1552			ZVAL_LONG(&argv[3], (zend_long)ultotal);
1553			ZVAL_LONG(&argv[4], (zend_long)ulnow);
1554
1555			fci.size = sizeof(fci);
1556			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1557			fci.object = NULL;
1558			fci.retval = &retval;
1559			fci.param_count = 5;
1560			fci.params = argv;
1561			fci.no_separation = 0;
1562
1563			ch->in_callback = 1;
1564			error = zend_call_function(&fci, &t->fci_cache);
1565			ch->in_callback = 0;
1566			if (error == FAILURE) {
1567				php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION");
1568			} else if (!Z_ISUNDEF(retval)) {
1569				_php_curl_verify_handlers(ch, 1);
1570				if (0 != zval_get_long(&retval)) {
1571					rval = 1;
1572				}
1573			}
1574			zval_ptr_dtor(&argv[0]);
1575			zval_ptr_dtor(&argv[1]);
1576			zval_ptr_dtor(&argv[2]);
1577			zval_ptr_dtor(&argv[3]);
1578			zval_ptr_dtor(&argv[4]);
1579			break;
1580		}
1581	}
1582	return rval;
1583}
1584/* }}} */
1585
1586/* {{{ curl_read
1587 */
1588static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
1589{
1590	php_curl *ch = (php_curl *)ctx;
1591	php_curl_read *t = ch->handlers->read;
1592	int length = 0;
1593
1594	switch (t->method) {
1595		case PHP_CURL_DIRECT:
1596			if (t->fp) {
1597				length = fread(data, size, nmemb, t->fp);
1598			}
1599			break;
1600		case PHP_CURL_USER: {
1601			zval argv[3];
1602			zval retval;
1603			int  error;
1604			zend_fcall_info fci;
1605
1606			ZVAL_RES(&argv[0], ch->res);
1607			Z_ADDREF(argv[0]);
1608			if (t->res) {
1609				ZVAL_RES(&argv[1], t->res);
1610				Z_ADDREF(argv[1]);
1611			} else {
1612				ZVAL_NULL(&argv[1]);
1613			}
1614			ZVAL_LONG(&argv[2], (int)size * nmemb);
1615
1616			fci.size = sizeof(fci);
1617			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1618			fci.object = NULL;
1619			fci.retval = &retval;
1620			fci.param_count = 3;
1621			fci.params = argv;
1622			fci.no_separation = 0;
1623
1624			ch->in_callback = 1;
1625			error = zend_call_function(&fci, &t->fci_cache);
1626			ch->in_callback = 0;
1627			if (error == FAILURE) {
1628				php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_READFUNCTION");
1629#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
1630				length = CURL_READFUNC_ABORT;
1631#endif
1632			} else if (!Z_ISUNDEF(retval)) {
1633				_php_curl_verify_handlers(ch, 1);
1634				if (Z_TYPE(retval) == IS_STRING) {
1635					length = MIN((int) (size * nmemb), Z_STRLEN(retval));
1636					memcpy(data, Z_STRVAL(retval), length);
1637				}
1638				zval_ptr_dtor(&retval);
1639			}
1640
1641			zval_ptr_dtor(&argv[0]);
1642			zval_ptr_dtor(&argv[1]);
1643			zval_ptr_dtor(&argv[2]);
1644			break;
1645		}
1646	}
1647
1648	return length;
1649}
1650/* }}} */
1651
1652/* {{{ curl_write_header
1653 */
1654static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
1655{
1656	php_curl *ch = (php_curl *) ctx;
1657	php_curl_write *t = ch->handlers->write_header;
1658	size_t length = size * nmemb;
1659
1660	switch (t->method) {
1661		case PHP_CURL_STDOUT:
1662			/* Handle special case write when we're returning the entire transfer
1663			 */
1664			if (ch->handlers->write->method == PHP_CURL_RETURN && length > 0) {
1665				smart_str_appendl(&ch->handlers->write->buf, data, (int) length);
1666			} else {
1667				PHPWRITE(data, length);
1668			}
1669			break;
1670		case PHP_CURL_FILE:
1671			return fwrite(data, size, nmemb, t->fp);
1672		case PHP_CURL_USER: {
1673			zval argv[2];
1674			zval retval;
1675			int  error;
1676			zend_fcall_info fci;
1677
1678			ZVAL_RES(&argv[0], ch->res);
1679			Z_ADDREF(argv[0]);
1680			ZVAL_STRINGL(&argv[1], data, length);
1681
1682			fci.size = sizeof(fci);
1683			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1684			fci.object = NULL;
1685			fci.retval = &retval;
1686			fci.param_count = 2;
1687			fci.params = argv;
1688			fci.no_separation = 0;
1689
1690			ch->in_callback = 1;
1691			error = zend_call_function(&fci, &t->fci_cache);
1692			ch->in_callback = 0;
1693			if (error == FAILURE) {
1694				php_error_docref(NULL, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION");
1695				length = -1;
1696			} else if (!Z_ISUNDEF(retval)) {
1697				_php_curl_verify_handlers(ch, 1);
1698				length = zval_get_long(&retval);
1699			}
1700			zval_ptr_dtor(&argv[0]);
1701			zval_ptr_dtor(&argv[1]);
1702			break;
1703		}
1704
1705		case PHP_CURL_IGNORE:
1706			return length;
1707
1708		default:
1709			return -1;
1710	}
1711
1712	return length;
1713}
1714/* }}} */
1715
1716static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */
1717{
1718	php_curl *ch = (php_curl *)ctx;
1719
1720	if (type == CURLINFO_HEADER_OUT) {
1721		if (ch->header.str) {
1722			zend_string_release(ch->header.str);
1723		}
1724		if (buf_len > 0) {
1725			ch->header.str = zend_string_init(buf, buf_len, 0);
1726		}
1727	}
1728
1729	return 0;
1730}
1731/* }}} */
1732
1733#if CURLOPT_PASSWDFUNCTION != 0
1734/* {{{ curl_passwd
1735 */
1736static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen)
1737{
1738	php_curl *ch = (php_curl *) ctx;
1739	zval *func = &ch->handlers->passwd;
1740	zval  argv[3];
1741	zval  retval;
1742	int   error;
1743	int   ret = -1;
1744
1745	ZVAL_RES(&argv[0], ch->res);
1746	Z_ADDREF(argv[0]);
1747	ZVAL_STRING(&argv[1], prompt);
1748	ZVAL_LONG(&argv[2], buflen);
1749
1750	error = call_user_function(EG(function_table), NULL, func, &retval, 2, argv);
1751	if (error == FAILURE) {
1752		php_error_docref(NULL, E_WARNING, "Could not call the CURLOPT_PASSWDFUNCTION");
1753	} else if (Z_TYPE(retval) == IS_STRING) {
1754		if (Z_STRLEN(retval) > buflen) {
1755			php_error_docref(NULL, E_WARNING, "Returned password is too long for libcurl to handle");
1756		} else {
1757			memcpy(buf, Z_STRVAL(retval), Z_STRLEN(retval) + 1);
1758		}
1759	} else {
1760		php_error_docref(NULL, E_WARNING, "User handler '%s' did not return a string", Z_STRVAL_P(func));
1761	}
1762
1763	zval_ptr_dtor(&argv[0]);
1764	zval_ptr_dtor(&argv[1]);
1765	zval_ptr_dtor(&argv[2]);
1766	zval_ptr_dtor(&retval);
1767
1768	return ret;
1769}
1770/* }}} */
1771#endif
1772
1773/* {{{ curl_free_string
1774 */
1775static void curl_free_string(void **string)
1776{
1777	efree((char *)*string);
1778}
1779/* }}} */
1780
1781/* {{{ curl_free_post
1782 */
1783static void curl_free_post(void **post)
1784{
1785	curl_formfree((struct HttpPost *)*post);
1786}
1787/* }}} */
1788
1789/* {{{ curl_free_slist
1790 */
1791static void curl_free_slist(zval *el)
1792{
1793	curl_slist_free_all(((struct curl_slist *)Z_PTR_P(el)));
1794}
1795/* }}} */
1796
1797/* {{{ proto array curl_version([int version])
1798   Return cURL version information. */
1799PHP_FUNCTION(curl_version)
1800{
1801	curl_version_info_data *d;
1802	zend_long uversion = CURLVERSION_NOW;
1803
1804	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &uversion) == FAILURE) {
1805		return;
1806	}
1807
1808	d = curl_version_info(uversion);
1809	if (d == NULL) {
1810		RETURN_FALSE;
1811	}
1812
1813	array_init(return_value);
1814
1815	CAAL("version_number", d->version_num);
1816	CAAL("age", d->age);
1817	CAAL("features", d->features);
1818	CAAL("ssl_version_number", d->ssl_version_num);
1819	CAAS("version", d->version);
1820	CAAS("host", d->host);
1821	CAAS("ssl_version", d->ssl_version);
1822	CAAS("libz_version", d->libz_version);
1823	/* Add an array of protocols */
1824	{
1825		char **p = (char **) d->protocols;
1826		zval protocol_list;
1827
1828		array_init(&protocol_list);
1829
1830		while (*p != NULL) {
1831			add_next_index_string(&protocol_list, *p);
1832			p++;
1833		}
1834		CAAZ("protocols", &protocol_list);
1835	}
1836}
1837/* }}} */
1838
1839/* {{{ alloc_curl_handle
1840 */
1841static php_curl *alloc_curl_handle()
1842{
1843	php_curl *ch               = ecalloc(1, sizeof(php_curl));
1844	ch->to_free                = ecalloc(1, sizeof(struct _php_curl_free));
1845	ch->handlers               = ecalloc(1, sizeof(php_curl_handlers));
1846	ch->handlers->write        = ecalloc(1, sizeof(php_curl_write));
1847	ch->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
1848	ch->handlers->read         = ecalloc(1, sizeof(php_curl_read));
1849	ch->handlers->progress     = NULL;
1850#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1851	ch->handlers->fnmatch      = NULL;
1852#endif
1853	ch->clone 				   = emalloc(sizeof(uint32_t));
1854	*ch->clone                 = 1;
1855
1856	memset(&ch->err, 0, sizeof(struct _php_curl_error));
1857
1858	zend_llist_init(&ch->to_free->str,   sizeof(char *),          (llist_dtor_func_t)curl_free_string, 0);
1859	zend_llist_init(&ch->to_free->post,  sizeof(struct HttpPost *), (llist_dtor_func_t)curl_free_post,   0);
1860
1861	ch->to_free->slist = emalloc(sizeof(HashTable));
1862	zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
1863
1864	return ch;
1865}
1866/* }}} */
1867
1868#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1869/* {{{ split_certinfo
1870 */
1871static void split_certinfo(char *string, zval *hash)
1872{
1873	char *org = estrdup(string);
1874	char *s = org;
1875	char *split;
1876
1877	if(org) {
1878		do {
1879			char *key;
1880			char *val;
1881			char *tmp;
1882
1883			split = strstr(s, "; ");
1884			if(split)
1885				*split = '\0';
1886
1887			key = s;
1888			tmp = memchr(key, '=', 64);
1889			if(tmp) {
1890				*tmp = '\0';
1891				val = tmp+1;
1892				add_assoc_string(hash, key, val);
1893			}
1894			s = split+2;
1895		} while(split);
1896		efree(org);
1897	}
1898}
1899/* }}} */
1900
1901/* {{{ create_certinfo
1902 */
1903static void create_certinfo(struct curl_certinfo *ci, zval *listcode)
1904{
1905	int i;
1906
1907	if (ci) {
1908		zval certhash;
1909
1910		for (i=0; i<ci->num_of_certs; i++) {
1911			struct curl_slist *slist;
1912
1913			array_init(&certhash);
1914			for (slist = ci->certinfo[i]; slist; slist = slist->next) {
1915				int len;
1916				char s[64];
1917				char *tmp;
1918				strncpy(s, slist->data, 64);
1919				tmp = memchr(s, ':', 64);
1920				if(tmp) {
1921					*tmp = '\0';
1922					len = strlen(s);
1923					if (!strcmp(s, "Subject") || !strcmp(s, "Issuer")) {
1924						zval hash;
1925
1926						array_init(&hash);
1927
1928						split_certinfo(&slist->data[len+1], &hash);
1929						add_assoc_zval(&certhash, s, &hash);
1930					} else {
1931						add_assoc_string(&certhash, s, &slist->data[len+1]);
1932					}
1933				} else {
1934					php_error_docref(NULL, E_WARNING, "Could not extract hash key from certificate info");
1935				}
1936			}
1937			add_next_index_zval(listcode, &certhash);
1938		}
1939	}
1940}
1941/* }}} */
1942#endif
1943
1944/* {{{ _php_curl_set_default_options()
1945   Set default options for a handle */
1946static void _php_curl_set_default_options(php_curl *ch)
1947{
1948	char *cainfo;
1949
1950	curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
1951	curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
1952	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1953	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
1954	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1955	curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
1956	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1957	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
1958	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1959#if !defined(ZTS)
1960	curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
1961#endif
1962	curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1963	curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1964
1965	cainfo = INI_STR("openssl.cafile");
1966	if (!(cainfo && cainfo[0] != '\0')) {
1967		cainfo = INI_STR("curl.cainfo");
1968	}
1969	if (cainfo && cainfo[0] != '\0') {
1970		curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1971	}
1972
1973#if defined(ZTS)
1974	curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1975#endif
1976}
1977/* }}} */
1978
1979/* {{{ proto resource curl_init([string url])
1980   Initialize a cURL session */
1981PHP_FUNCTION(curl_init)
1982{
1983	php_curl *ch;
1984	CURL 	 *cp;
1985	char 	 *url = NULL;
1986	size_t		  url_len = 0;
1987
1988	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &url, &url_len) == FAILURE) {
1989		return;
1990	}
1991
1992	cp = curl_easy_init();
1993	if (!cp) {
1994		php_error_docref(NULL, E_WARNING, "Could not initialize a new cURL handle");
1995		RETURN_FALSE;
1996	}
1997
1998	ch = alloc_curl_handle();
1999
2000	ch->cp = cp;
2001
2002	ch->handlers->write->method = PHP_CURL_STDOUT;
2003	ch->handlers->read->method  = PHP_CURL_DIRECT;
2004	ch->handlers->write_header->method = PHP_CURL_IGNORE;
2005
2006	_php_curl_set_default_options(ch);
2007
2008	if (url) {
2009		if (php_curl_option_url(ch, url, url_len) == FAILURE) {
2010			_php_curl_close_ex(ch);
2011			RETURN_FALSE;
2012		}
2013	}
2014
2015	ZVAL_RES(return_value, zend_register_resource(ch, le_curl));
2016	ch->res = Z_RES_P(return_value);
2017}
2018/* }}} */
2019
2020/* {{{ proto resource curl_copy_handle(resource ch)
2021   Copy a cURL handle along with all of it's preferences */
2022PHP_FUNCTION(curl_copy_handle)
2023{
2024	CURL		*cp;
2025	zval		*zid;
2026	php_curl	*ch, *dupch;
2027
2028	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
2029		return;
2030	}
2031
2032	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2033		RETURN_FALSE;
2034	}
2035
2036	cp = curl_easy_duphandle(ch->cp);
2037	if (!cp) {
2038		php_error_docref(NULL, E_WARNING, "Cannot duplicate cURL handle");
2039		RETURN_FALSE;
2040	}
2041
2042	dupch = alloc_curl_handle();
2043
2044	dupch->cp = cp;
2045	Z_ADDREF_P(zid);
2046	if (!Z_ISUNDEF(ch->handlers->write->stream)) {
2047		Z_ADDREF(ch->handlers->write->stream);
2048	}
2049	dupch->handlers->write->stream = ch->handlers->write->stream;
2050	dupch->handlers->write->method = ch->handlers->write->method;
2051	if (!Z_ISUNDEF(ch->handlers->read->stream)) {
2052		Z_ADDREF(ch->handlers->read->stream);
2053	}
2054	dupch->handlers->read->stream  = ch->handlers->read->stream;
2055	dupch->handlers->read->method  = ch->handlers->read->method;
2056	dupch->handlers->write_header->method = ch->handlers->write_header->method;
2057	if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
2058		Z_ADDREF(ch->handlers->write_header->stream);
2059	}
2060	dupch->handlers->write_header->stream = ch->handlers->write_header->stream;
2061
2062	dupch->handlers->write->fp = ch->handlers->write->fp;
2063	dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
2064	dupch->handlers->read->fp = ch->handlers->read->fp;
2065	dupch->handlers->read->res = ch->handlers->read->res;
2066#if CURLOPT_PASSWDDATA != 0
2067	if (!Z_ISUNDEF(ch->handlers->passwd)) {
2068		ZVAL_COPY(&dupch->handlers->passwd, &ch->handlers->passwd);
2069		curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
2070	}
2071#endif
2072	if (!Z_ISUNDEF(ch->handlers->write->func_name)) {
2073		ZVAL_COPY(&dupch->handlers->write->func_name, &ch->handlers->write->func_name);
2074	}
2075	if (!Z_ISUNDEF(ch->handlers->read->func_name)) {
2076		ZVAL_COPY(&dupch->handlers->read->func_name, &ch->handlers->read->func_name);
2077	}
2078	if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) {
2079		ZVAL_COPY(&dupch->handlers->write_header->func_name, &ch->handlers->write_header->func_name);
2080	}
2081
2082	curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER,       dupch->err.str);
2083	curl_easy_setopt(dupch->cp, CURLOPT_FILE,              (void *) dupch);
2084	curl_easy_setopt(dupch->cp, CURLOPT_INFILE,            (void *) dupch);
2085	curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER,       (void *) dupch);
2086
2087	if (ch->handlers->progress) {
2088		dupch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2089		if (!Z_ISUNDEF(ch->handlers->progress->func_name)) {
2090			ZVAL_COPY(&dupch->handlers->progress->func_name, &ch->handlers->progress->func_name);
2091		}
2092		dupch->handlers->progress->method = ch->handlers->progress->method;
2093		curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA, (void *) dupch);
2094	}
2095
2096/* Available since 7.21.0 */
2097#if LIBCURL_VERSION_NUM >= 0x071500
2098	if (ch->handlers->fnmatch) {
2099		dupch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2100		if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) {
2101			ZVAL_COPY(&dupch->handlers->fnmatch->func_name, &ch->handlers->fnmatch->func_name);
2102		}
2103		dupch->handlers->fnmatch->method = ch->handlers->fnmatch->method;
2104		curl_easy_setopt(dupch->cp, CURLOPT_FNMATCH_DATA, (void *) dupch);
2105	}
2106#endif
2107
2108	efree(dupch->to_free->slist);
2109	efree(dupch->to_free);
2110	dupch->to_free = ch->to_free;
2111	efree(dupch->clone);
2112	dupch->clone = ch->clone;
2113
2114	/* Keep track of cloned copies to avoid invoking curl destructors for every clone */
2115	(*ch->clone)++;
2116
2117	ZVAL_RES(return_value, zend_register_resource(dupch, le_curl));
2118	dupch->res = Z_RES_P(return_value);
2119}
2120/* }}} */
2121
2122static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */
2123{
2124	CURLcode error = CURLE_OK;
2125	zend_long lval;
2126
2127	ZVAL_DEREF(zvalue);
2128	switch (option) {
2129		/* Long options */
2130		case CURLOPT_SSL_VERIFYHOST:
2131			lval = zval_get_long(zvalue);
2132			if (lval == 1) {
2133#if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
2134				php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST with value 1 is deprecated and will be removed as of libcurl 7.28.1. It is recommended to use value 2 instead");
2135#else
2136				php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
2137				error = curl_easy_setopt(ch->cp, option, 2);
2138				break;
2139#endif
2140			}
2141		case CURLOPT_AUTOREFERER:
2142		case CURLOPT_BUFFERSIZE:
2143		case CURLOPT_CONNECTTIMEOUT:
2144		case CURLOPT_COOKIESESSION:
2145		case CURLOPT_CRLF:
2146		case CURLOPT_DNS_CACHE_TIMEOUT:
2147		case CURLOPT_DNS_USE_GLOBAL_CACHE:
2148		case CURLOPT_FAILONERROR:
2149		case CURLOPT_FILETIME:
2150		case CURLOPT_FORBID_REUSE:
2151		case CURLOPT_FRESH_CONNECT:
2152		case CURLOPT_FTP_USE_EPRT:
2153		case CURLOPT_FTP_USE_EPSV:
2154		case CURLOPT_HEADER:
2155		case CURLOPT_HTTPGET:
2156		case CURLOPT_HTTPPROXYTUNNEL:
2157		case CURLOPT_HTTP_VERSION:
2158		case CURLOPT_INFILESIZE:
2159		case CURLOPT_LOW_SPEED_LIMIT:
2160		case CURLOPT_LOW_SPEED_TIME:
2161		case CURLOPT_MAXCONNECTS:
2162		case CURLOPT_MAXREDIRS:
2163		case CURLOPT_NETRC:
2164		case CURLOPT_NOBODY:
2165		case CURLOPT_NOPROGRESS:
2166		case CURLOPT_NOSIGNAL:
2167		case CURLOPT_PORT:
2168		case CURLOPT_POST:
2169		case CURLOPT_PROXYPORT:
2170		case CURLOPT_PROXYTYPE:
2171		case CURLOPT_PUT:
2172		case CURLOPT_RESUME_FROM:
2173		case CURLOPT_SSLVERSION:
2174		case CURLOPT_SSL_VERIFYPEER:
2175		case CURLOPT_TIMECONDITION:
2176		case CURLOPT_TIMEOUT:
2177		case CURLOPT_TIMEVALUE:
2178		case CURLOPT_TRANSFERTEXT:
2179		case CURLOPT_UNRESTRICTED_AUTH:
2180		case CURLOPT_UPLOAD:
2181		case CURLOPT_VERBOSE:
2182#if LIBCURL_VERSION_NUM >= 0x070a06 /* Available since 7.10.6 */
2183		case CURLOPT_HTTPAUTH:
2184#endif
2185#if LIBCURL_VERSION_NUM >= 0x070a07 /* Available since 7.10.7 */
2186		case CURLOPT_FTP_CREATE_MISSING_DIRS:
2187		case CURLOPT_PROXYAUTH:
2188#endif
2189#if LIBCURL_VERSION_NUM >= 0x070a08 /* Available since 7.10.8 */
2190		case CURLOPT_FTP_RESPONSE_TIMEOUT:
2191		case CURLOPT_IPRESOLVE:
2192		case CURLOPT_MAXFILESIZE:
2193#endif
2194#if LIBCURL_VERSION_NUM >= 0x070b02 /* Available since 7.11.2 */
2195		case CURLOPT_TCP_NODELAY:
2196#endif
2197#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
2198		case CURLOPT_FTPSSLAUTH:
2199#endif
2200#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
2201		case CURLOPT_IGNORE_CONTENT_LENGTH:
2202#endif
2203#if LIBCURL_VERSION_NUM >= 0x070f00 /* Available since 7.15.0 */
2204		case CURLOPT_FTP_SKIP_PASV_IP:
2205#endif
2206#if LIBCURL_VERSION_NUM >= 0x070f01 /* Available since 7.15.1 */
2207		case CURLOPT_FTP_FILEMETHOD:
2208#endif
2209#if LIBCURL_VERSION_NUM >= 0x070f02 /* Available since 7.15.2 */
2210		case CURLOPT_CONNECT_ONLY:
2211		case CURLOPT_LOCALPORT:
2212		case CURLOPT_LOCALPORTRANGE:
2213#endif
2214#if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
2215		case CURLOPT_SSL_SESSIONID_CACHE:
2216#endif
2217#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2218		case CURLOPT_FTP_SSL_CCC:
2219		case CURLOPT_SSH_AUTH_TYPES:
2220#endif
2221#if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
2222		case CURLOPT_CONNECTTIMEOUT_MS:
2223		case CURLOPT_HTTP_CONTENT_DECODING:
2224		case CURLOPT_HTTP_TRANSFER_DECODING:
2225		case CURLOPT_TIMEOUT_MS:
2226#endif
2227#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2228		case CURLOPT_NEW_DIRECTORY_PERMS:
2229		case CURLOPT_NEW_FILE_PERMS:
2230#endif
2231#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2232		case CURLOPT_USE_SSL:
2233#elif LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2234		case CURLOPT_FTP_SSL:
2235#endif
2236#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2237		case CURLOPT_APPEND:
2238		case CURLOPT_DIRLISTONLY:
2239#else
2240		case CURLOPT_FTPAPPEND:
2241		case CURLOPT_FTPLISTONLY:
2242#endif
2243#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
2244		case CURLOPT_PROXY_TRANSFER_MODE:
2245#endif
2246#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2247		case CURLOPT_ADDRESS_SCOPE:
2248#endif
2249#if LIBCURL_VERSION_NUM >  0x071301 /* Available since 7.19.1 */
2250		case CURLOPT_CERTINFO:
2251#endif
2252#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2253		case CURLOPT_PROTOCOLS:
2254		case CURLOPT_REDIR_PROTOCOLS:
2255		case CURLOPT_SOCKS5_GSSAPI_NEC:
2256		case CURLOPT_TFTP_BLKSIZE:
2257#endif
2258#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2259		case CURLOPT_FTP_USE_PRET:
2260		case CURLOPT_RTSP_CLIENT_CSEQ:
2261		case CURLOPT_RTSP_REQUEST:
2262		case CURLOPT_RTSP_SERVER_CSEQ:
2263#endif
2264#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2265		case CURLOPT_WILDCARDMATCH:
2266#endif
2267#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2268		case CURLOPT_TLSAUTH_TYPE:
2269#endif
2270#if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
2271		case CURLOPT_GSSAPI_DELEGATION:
2272#endif
2273#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2274		case CURLOPT_ACCEPTTIMEOUT_MS:
2275#endif
2276#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2277		case CURLOPT_SSL_OPTIONS:
2278		case CURLOPT_TCP_KEEPALIVE:
2279		case CURLOPT_TCP_KEEPIDLE:
2280		case CURLOPT_TCP_KEEPINTVL:
2281#endif
2282#if LIBCURL_VERSION_NUM >= 0x071f00 /* Available since 7.31.0 */
2283		case CURLOPT_SASL_IR:
2284#endif
2285#if LIBCURL_VERSION_NUM >= 0x072400 /* Available since 7.36.0 */
2286		case CURLOPT_EXPECT_100_TIMEOUT_MS:
2287		case CURLOPT_SSL_ENABLE_ALPN:
2288		case CURLOPT_SSL_ENABLE_NPN:
2289#endif
2290#if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
2291		case CURLOPT_HEADEROPT:
2292#endif
2293#if LIBCURL_VERSION_NUM >= 0x072900 /* Available since 7.41.0 */
2294		case CURLOPT_SSL_VERIFYSTATUS:
2295#endif
2296#if LIBCURL_VERSION_NUM >= 0x072a00 /* Available since 7.42.0 */
2297		case CURLOPT_PATH_AS_IS:
2298		case CURLOPT_SSL_FALSESTART:
2299#endif
2300#if LIBCURL_VERSION_NUM >= 0x072b00 /* Available since 7.43.0 */
2301		case CURLOPT_PIPEWAIT:
2302#endif
2303#if LIBCURL_VERSION_NUM >= 0x072e00 /* Available since 7.46.0 */
2304		case CURLOPT_STREAM_WEIGHT:
2305#endif
2306#if LIBCURL_VERSION_NUM >= 0x073000 /* Available since 7.48.0 */
2307		case CURLOPT_TFTP_NO_OPTIONS:
2308#endif
2309#if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
2310		case CURLOPT_TCP_FASTOPEN:
2311#endif
2312#if CURLOPT_MUTE != 0
2313		case CURLOPT_MUTE:
2314#endif
2315			lval = zval_get_long(zvalue);
2316#if LIBCURL_VERSION_NUM >= 0x71304
2317			if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
2318				(PG(open_basedir) && *PG(open_basedir)) && (lval & CURLPROTO_FILE)) {
2319					php_error_docref(NULL, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
2320					return 1;
2321			}
2322#endif
2323# if defined(ZTS)
2324			if (option == CURLOPT_DNS_USE_GLOBAL_CACHE) {
2325				php_error_docref(NULL, E_WARNING, "CURLOPT_DNS_USE_GLOBAL_CACHE cannot be activated when thread safety is enabled");
2326				return 1;
2327			}
2328# endif
2329			error = curl_easy_setopt(ch->cp, option, lval);
2330			break;
2331		case CURLOPT_SAFE_UPLOAD:
2332			lval = zval_get_long(zvalue);
2333			if (lval == 0) {
2334				php_error_docref(NULL, E_WARNING, "Disabling safe uploads is no longer supported");
2335				return FAILURE;
2336			}
2337			break;
2338
2339		/* String options */
2340		case CURLOPT_CAINFO:
2341		case CURLOPT_CAPATH:
2342		case CURLOPT_COOKIE:
2343		case CURLOPT_EGDSOCKET:
2344		case CURLOPT_INTERFACE:
2345		case CURLOPT_PROXY:
2346		case CURLOPT_PROXYUSERPWD:
2347		case CURLOPT_REFERER:
2348		case CURLOPT_SSLCERTTYPE:
2349		case CURLOPT_SSLENGINE:
2350		case CURLOPT_SSLENGINE_DEFAULT:
2351		case CURLOPT_SSLKEY:
2352		case CURLOPT_SSLKEYPASSWD:
2353		case CURLOPT_SSLKEYTYPE:
2354		case CURLOPT_SSL_CIPHER_LIST:
2355		case CURLOPT_USERAGENT:
2356		case CURLOPT_USERPWD:
2357#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
2358		case CURLOPT_COOKIELIST:
2359#endif
2360#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2361		case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2362#endif
2363#if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
2364		case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2365#endif
2366#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2367		case CURLOPT_PASSWORD:
2368		case CURLOPT_PROXYPASSWORD:
2369		case CURLOPT_PROXYUSERNAME:
2370		case CURLOPT_USERNAME:
2371#endif
2372#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2373		case CURLOPT_NOPROXY:
2374		case CURLOPT_SOCKS5_GSSAPI_SERVICE:
2375#endif
2376#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2377		case CURLOPT_MAIL_FROM:
2378		case CURLOPT_RTSP_STREAM_URI:
2379		case CURLOPT_RTSP_TRANSPORT:
2380#endif
2381#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2382		case CURLOPT_TLSAUTH_PASSWORD:
2383		case CURLOPT_TLSAUTH_USERNAME:
2384#endif
2385#if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
2386		case CURLOPT_ACCEPT_ENCODING:
2387		case CURLOPT_TRANSFER_ENCODING:
2388#else
2389		case CURLOPT_ENCODING:
2390#endif
2391#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2392		case CURLOPT_DNS_SERVERS:
2393#endif
2394#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2395		case CURLOPT_MAIL_AUTH:
2396#endif
2397#if LIBCURL_VERSION_NUM >= 0x072200 /* Available since 7.34.0 */
2398		case CURLOPT_LOGIN_OPTIONS:
2399#endif
2400#if LIBCURL_VERSION_NUM >= 0x072700 /* Available since 7.39.0 */
2401		case CURLOPT_PINNEDPUBLICKEY:
2402#endif
2403#if LIBCURL_VERSION_NUM >= 0x072b00 /* Available since 7.43.0 */
2404		case CURLOPT_PROXY_SERVICE_NAME:
2405		case CURLOPT_SERVICE_NAME:
2406#endif
2407#if LIBCURL_VERSION_NUM >= 0x072d00 /* Available since 7.45.0 */
2408		case CURLOPT_DEFAULT_PROTOCOL:
2409#endif
2410		{
2411			zend_string *str = zval_get_string(zvalue);
2412			int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2413			zend_string_release(str);
2414			return ret;
2415		}
2416
2417		/* Curl nullable string options */
2418		case CURLOPT_CUSTOMREQUEST:
2419		case CURLOPT_FTPPORT:
2420		case CURLOPT_RANGE:
2421#if LIBCURL_VERSION_NUM >= 0x070d00 /* Available since 7.13.0 */
2422		case CURLOPT_FTP_ACCOUNT:
2423#endif
2424#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2425		case CURLOPT_RTSP_SESSION_ID:
2426#endif
2427#if LIBCURL_VERSION_NUM >= 0x072100 /* Available since 7.33.0 */
2428		case CURLOPT_DNS_INTERFACE:
2429		case CURLOPT_DNS_LOCAL_IP4:
2430		case CURLOPT_DNS_LOCAL_IP6:
2431		case CURLOPT_XOAUTH2_BEARER:
2432#endif
2433#if LIBCURL_VERSION_NUM >= 0x072800 /* Available since 7.40.0 */
2434		case CURLOPT_UNIX_SOCKET_PATH:
2435#endif
2436#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2437		case CURLOPT_KRBLEVEL:
2438#else
2439		case CURLOPT_KRB4LEVEL:
2440#endif
2441		{
2442			if (Z_ISNULL_P(zvalue)) {
2443				error = curl_easy_setopt(ch->cp, option, NULL);
2444			} else {
2445				zend_string *str = zval_get_string(zvalue);
2446				int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2447				zend_string_release(str);
2448				return ret;
2449			}
2450			break;
2451		}
2452
2453		/* Curl private option */
2454		case CURLOPT_PRIVATE:
2455		{
2456			zend_string *str = zval_get_string(zvalue);
2457			int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 1);
2458			zend_string_release(str);
2459			return ret;
2460		}
2461
2462		/* Curl url option */
2463		case CURLOPT_URL:
2464		{
2465			zend_string *str = zval_get_string(zvalue);
2466			int ret = php_curl_option_url(ch, ZSTR_VAL(str), ZSTR_LEN(str));
2467			zend_string_release(str);
2468			return ret;
2469		}
2470
2471		/* Curl file handle options */
2472		case CURLOPT_FILE:
2473		case CURLOPT_INFILE:
2474		case CURLOPT_STDERR:
2475		case CURLOPT_WRITEHEADER: {
2476			FILE *fp = NULL;
2477			php_stream *what = NULL;
2478
2479			if (Z_TYPE_P(zvalue) != IS_NULL) {
2480				what = (php_stream *)zend_fetch_resource2_ex(zvalue, "File-Handle", php_file_le_stream(), php_file_le_pstream());
2481				if (!what) {
2482					return FAILURE;
2483				}
2484
2485				if (FAILURE == php_stream_cast(what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2486					return FAILURE;
2487				}
2488
2489				if (!fp) {
2490					return FAILURE;
2491				}
2492			}
2493
2494			error = CURLE_OK;
2495			switch (option) {
2496				case CURLOPT_FILE:
2497					if (!what) {
2498						if (!Z_ISUNDEF(ch->handlers->write->stream)) {
2499							zval_ptr_dtor(&ch->handlers->write->stream);
2500							ZVAL_UNDEF(&ch->handlers->write->stream);
2501						}
2502						ch->handlers->write->fp = NULL;
2503						ch->handlers->write->method = PHP_CURL_STDOUT;
2504					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2505						zval_ptr_dtor(&ch->handlers->write->stream);
2506						ch->handlers->write->fp = fp;
2507						ch->handlers->write->method = PHP_CURL_FILE;
2508						ZVAL_COPY(&ch->handlers->write->stream, zvalue);
2509					} else {
2510						php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2511						return FAILURE;
2512					}
2513					break;
2514				case CURLOPT_WRITEHEADER:
2515					if (!what) {
2516						if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
2517							zval_ptr_dtor(&ch->handlers->write_header->stream);
2518							ZVAL_UNDEF(&ch->handlers->write_header->stream);
2519						}
2520						ch->handlers->write_header->fp = NULL;
2521						ch->handlers->write_header->method = PHP_CURL_IGNORE;
2522					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2523						zval_ptr_dtor(&ch->handlers->write_header->stream);
2524						ch->handlers->write_header->fp = fp;
2525						ch->handlers->write_header->method = PHP_CURL_FILE;
2526						ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);;
2527					} else {
2528						php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2529						return FAILURE;
2530					}
2531					break;
2532				case CURLOPT_INFILE:
2533					if (!what) {
2534						if (!Z_ISUNDEF(ch->handlers->read->stream)) {
2535							zval_ptr_dtor(&ch->handlers->read->stream);
2536							ZVAL_UNDEF(&ch->handlers->read->stream);
2537						}
2538						ch->handlers->read->fp = NULL;
2539						ch->handlers->read->res = NULL;
2540					} else {
2541						zval_ptr_dtor(&ch->handlers->read->stream);
2542						ch->handlers->read->fp = fp;
2543						ch->handlers->read->res = Z_RES_P(zvalue);
2544						ZVAL_COPY(&ch->handlers->read->stream, zvalue);
2545					}
2546					break;
2547				case CURLOPT_STDERR:
2548					if (!what) {
2549						if (!Z_ISUNDEF(ch->handlers->std_err)) {
2550							zval_ptr_dtor(&ch->handlers->std_err);
2551							ZVAL_UNDEF(&ch->handlers->std_err);
2552						}
2553					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2554						zval_ptr_dtor(&ch->handlers->std_err);
2555						ZVAL_COPY(&ch->handlers->std_err, zvalue);
2556					} else {
2557						php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2558						return FAILURE;
2559					}
2560					/* break omitted intentionally */
2561				default:
2562					error = curl_easy_setopt(ch->cp, option, fp);
2563					break;
2564			}
2565			break;
2566		}
2567
2568		/* Curl linked list options */
2569		case CURLOPT_HTTP200ALIASES:
2570		case CURLOPT_HTTPHEADER:
2571		case CURLOPT_POSTQUOTE:
2572		case CURLOPT_PREQUOTE:
2573		case CURLOPT_QUOTE:
2574		case CURLOPT_TELNETOPTIONS:
2575#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2576		case CURLOPT_MAIL_RCPT:
2577#endif
2578#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2579		case CURLOPT_RESOLVE:
2580#endif
2581#if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
2582		case CURLOPT_PROXYHEADER:
2583#endif
2584#if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
2585		case CURLOPT_CONNECT_TO:
2586#endif
2587		{
2588			zval *current;
2589			HashTable *ph;
2590			zend_string *val;
2591			struct curl_slist *slist = NULL;
2592
2593			ph = HASH_OF(zvalue);
2594			if (!ph) {
2595				char *name = NULL;
2596				switch (option) {
2597					case CURLOPT_HTTPHEADER:
2598						name = "CURLOPT_HTTPHEADER";
2599						break;
2600					case CURLOPT_QUOTE:
2601						name = "CURLOPT_QUOTE";
2602						break;
2603					case CURLOPT_HTTP200ALIASES:
2604						name = "CURLOPT_HTTP200ALIASES";
2605						break;
2606					case CURLOPT_POSTQUOTE:
2607						name = "CURLOPT_POSTQUOTE";
2608						break;
2609					case CURLOPT_PREQUOTE:
2610						name = "CURLOPT_PREQUOTE";
2611						break;
2612					case CURLOPT_TELNETOPTIONS:
2613						name = "CURLOPT_TELNETOPTIONS";
2614						break;
2615#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2616					case CURLOPT_MAIL_RCPT:
2617						name = "CURLOPT_MAIL_RCPT";
2618						break;
2619#endif
2620#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2621					case CURLOPT_RESOLVE:
2622						name = "CURLOPT_RESOLVE";
2623						break;
2624#endif
2625#if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
2626					case CURLOPT_PROXYHEADER:
2627						name = "CURLOPT_PROXYHEADER";
2628						break;
2629#endif
2630#if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
2631					case CURLOPT_CONNECT_TO:
2632						name = "CURLOPT_CONNECT_TO";
2633						break;
2634#endif
2635				}
2636				php_error_docref(NULL, E_WARNING, "You must pass either an object or an array with the %s argument", name);
2637				return FAILURE;
2638			}
2639
2640			ZEND_HASH_FOREACH_VAL(ph, current) {
2641				ZVAL_DEREF(current);
2642				val = zval_get_string(current);
2643				slist = curl_slist_append(slist, ZSTR_VAL(val));
2644				zend_string_release(val);
2645				if (!slist) {
2646					php_error_docref(NULL, E_WARNING, "Could not build curl_slist");
2647					return 1;
2648				}
2649			} ZEND_HASH_FOREACH_END();
2650
2651			if ((*ch->clone) == 1) {
2652				zend_hash_index_update_ptr(ch->to_free->slist, option, slist);
2653			} else {
2654				zend_hash_next_index_insert_ptr(ch->to_free->slist, slist);
2655			}
2656
2657			error = curl_easy_setopt(ch->cp, option, slist);
2658
2659			break;
2660		}
2661
2662		case CURLOPT_BINARYTRANSFER:
2663			/* Do nothing, just backward compatibility */
2664			break;
2665
2666		case CURLOPT_FOLLOWLOCATION:
2667			lval = zval_get_long(zvalue);
2668#if LIBCURL_VERSION_NUM < 0x071304
2669			if (PG(open_basedir) && *PG(open_basedir)) {
2670				if (lval != 0) {
2671					php_error_docref(NULL, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set");
2672					return FAILURE;
2673				}
2674			}
2675#endif
2676			error = curl_easy_setopt(ch->cp, option, lval);
2677			break;
2678
2679		case CURLOPT_HEADERFUNCTION:
2680			if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) {
2681				zval_ptr_dtor(&ch->handlers->write_header->func_name);
2682				ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
2683			}
2684			ZVAL_COPY(&ch->handlers->write_header->func_name, zvalue);
2685			ch->handlers->write_header->method = PHP_CURL_USER;
2686			break;
2687
2688		case CURLOPT_POSTFIELDS:
2689			if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
2690				zval *current;
2691				HashTable *postfields;
2692				zend_string *string_key;
2693				zend_ulong  num_key;
2694				struct HttpPost *first = NULL;
2695				struct HttpPost *last  = NULL;
2696				CURLFORMcode form_error;
2697
2698				postfields = HASH_OF(zvalue);
2699				if (!postfields) {
2700					php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2701					return FAILURE;
2702				}
2703
2704				ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
2705					zend_string *postval;
2706					/* Pretend we have a string_key here */
2707					if (!string_key) {
2708						string_key = zend_long_to_str(num_key);
2709					} else {
2710						zend_string_addref(string_key);
2711					}
2712
2713					ZVAL_DEREF(current);
2714					if (Z_TYPE_P(current) == IS_OBJECT &&
2715							instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
2716						/* new-style file upload */
2717						zval *prop, rv;
2718						char *type = NULL, *filename = NULL;
2719
2720						prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
2721						if (Z_TYPE_P(prop) != IS_STRING) {
2722							php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
2723						} else {
2724							postval = Z_STR_P(prop);
2725
2726							if (php_check_open_basedir(ZSTR_VAL(postval))) {
2727								return 1;
2728							}
2729
2730							prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv);
2731							if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2732								type = Z_STRVAL_P(prop);
2733							}
2734							prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv);
2735							if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2736								filename = Z_STRVAL_P(prop);
2737							}
2738							form_error = curl_formadd(&first, &last,
2739											CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2740											CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2741											CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
2742											CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2743											CURLFORM_FILE, ZSTR_VAL(postval),
2744											CURLFORM_END);
2745							if (form_error != CURL_FORMADD_OK) {
2746								/* Not nice to convert between enums but we only have place for one error type */
2747								error = (CURLcode)form_error;
2748							}
2749						}
2750
2751						zend_string_release(string_key);
2752						continue;
2753					}
2754
2755					postval = zval_get_string(current);
2756
2757					/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2758					 * must be explicitly cast to long in curl_formadd
2759					 * use since curl needs a long not an int. */
2760					form_error = curl_formadd(&first, &last,
2761										 CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2762										 CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2763										 CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
2764										 CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
2765										 CURLFORM_END);
2766
2767					if (form_error != CURL_FORMADD_OK) {
2768						/* Not nice to convert between enums but we only have place for one error type */
2769						error = (CURLcode)form_error;
2770					}
2771					zend_string_release(postval);
2772					zend_string_release(string_key);
2773				} ZEND_HASH_FOREACH_END();
2774
2775				SAVE_CURL_ERROR(ch, error);
2776				if (error != CURLE_OK) {
2777					return FAILURE;
2778				}
2779
2780				if ((*ch->clone) == 1) {
2781					zend_llist_clean(&ch->to_free->post);
2782				}
2783				zend_llist_add_element(&ch->to_free->post, &first);
2784				error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2785			} else {
2786#if LIBCURL_VERSION_NUM >= 0x071101
2787				zend_string *str = zval_get_string(zvalue);
2788				/* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2789				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2790				error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, ZSTR_VAL(str));
2791				zend_string_release(str);
2792#else
2793				char *post = NULL;
2794				zend_string *str = zval_get_string(zvalue);
2795
2796				post = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
2797				zend_llist_add_element(&ch->to_free->str, &post);
2798
2799				curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
2800				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2801				zend_string_release(str);
2802#endif
2803			}
2804			break;
2805
2806		case CURLOPT_PROGRESSFUNCTION:
2807			curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,	curl_progress);
2808			curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
2809			if (ch->handlers->progress == NULL) {
2810				ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2811			} else if (!Z_ISUNDEF(ch->handlers->progress->func_name)) {
2812				zval_ptr_dtor(&ch->handlers->progress->func_name);
2813				ch->handlers->progress->fci_cache = empty_fcall_info_cache;
2814			}
2815			ZVAL_COPY(&ch->handlers->progress->func_name, zvalue);
2816			ch->handlers->progress->method = PHP_CURL_USER;
2817			break;
2818
2819		case CURLOPT_READFUNCTION:
2820			if (!Z_ISUNDEF(ch->handlers->read->func_name)) {
2821				zval_ptr_dtor(&ch->handlers->read->func_name);
2822				ch->handlers->read->fci_cache = empty_fcall_info_cache;
2823			}
2824			ZVAL_COPY(&ch->handlers->read->func_name, zvalue);
2825			ch->handlers->read->method = PHP_CURL_USER;
2826			break;
2827
2828		case CURLOPT_RETURNTRANSFER:
2829			lval = zval_get_long(zvalue);
2830			if (lval) {
2831				ch->handlers->write->method = PHP_CURL_RETURN;
2832			} else {
2833				ch->handlers->write->method = PHP_CURL_STDOUT;
2834			}
2835			break;
2836
2837		case CURLOPT_WRITEFUNCTION:
2838			if (!Z_ISUNDEF(ch->handlers->write->func_name)) {
2839				zval_ptr_dtor(&ch->handlers->write->func_name);
2840				ch->handlers->write->fci_cache = empty_fcall_info_cache;
2841			}
2842			ZVAL_COPY(&ch->handlers->write->func_name, zvalue);
2843			ch->handlers->write->method = PHP_CURL_USER;
2844			break;
2845
2846#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2847		case CURLOPT_MAX_RECV_SPEED_LARGE:
2848		case CURLOPT_MAX_SEND_SPEED_LARGE:
2849			lval = zval_get_long(zvalue);
2850			error = curl_easy_setopt(ch->cp, option, (curl_off_t)lval);
2851			break;
2852#endif
2853
2854#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2855		case CURLOPT_POSTREDIR:
2856			lval = zval_get_long(zvalue);
2857			error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, lval & CURL_REDIR_POST_ALL);
2858			break;
2859#endif
2860
2861#if CURLOPT_PASSWDFUNCTION != 0
2862		case CURLOPT_PASSWDFUNCTION:
2863			zval_ptr_dtor(&ch->handlers->passwd);
2864			ZVAL_COPY(&ch->handlers->passwd, zvalue);
2865			error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
2866			error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
2867			break;
2868#endif
2869
2870		/* the following options deal with files, therefore the open_basedir check
2871		 * is required.
2872		 */
2873		case CURLOPT_COOKIEFILE:
2874		case CURLOPT_COOKIEJAR:
2875		case CURLOPT_RANDOM_FILE:
2876		case CURLOPT_SSLCERT:
2877#if LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2878		case CURLOPT_NETRC_FILE:
2879#endif
2880#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2881		case CURLOPT_SSH_PRIVATE_KEYFILE:
2882		case CURLOPT_SSH_PUBLIC_KEYFILE:
2883#endif
2884#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2885		case CURLOPT_CRLFILE:
2886		case CURLOPT_ISSUERCERT:
2887#endif
2888#if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
2889		case CURLOPT_SSH_KNOWNHOSTS:
2890#endif
2891		{
2892			zend_string *str = zval_get_string(zvalue);
2893			int ret;
2894
2895			if (ZSTR_LEN(str) && php_check_open_basedir(ZSTR_VAL(str))) {
2896				zend_string_release(str);
2897				return FAILURE;
2898			}
2899
2900			ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2901			zend_string_release(str);
2902			return ret;
2903		}
2904
2905		case CURLINFO_HEADER_OUT:
2906			lval = zval_get_long(zvalue);
2907			if (lval == 1) {
2908				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2909				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2910				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2911			} else {
2912				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2913				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2914				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2915			}
2916			break;
2917
2918		case CURLOPT_SHARE:
2919			{
2920				php_curlsh *sh;
2921				if ((sh = (php_curlsh *)zend_fetch_resource_ex(zvalue, le_curl_share_handle_name, le_curl_share_handle))) {
2922					curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2923				}
2924			}
2925			break;
2926
2927#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2928		case CURLOPT_FNMATCH_FUNCTION:
2929			curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_FUNCTION, curl_fnmatch);
2930			curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, ch);
2931			if (ch->handlers->fnmatch == NULL) {
2932				ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2933			} else if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) {
2934				zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
2935				ch->handlers->fnmatch->fci_cache = empty_fcall_info_cache;
2936			}
2937			ZVAL_COPY(&ch->handlers->fnmatch->func_name, zvalue);
2938			ch->handlers->fnmatch->method = PHP_CURL_USER;
2939			break;
2940#endif
2941
2942	}
2943
2944	SAVE_CURL_ERROR(ch, error);
2945	if (error != CURLE_OK) {
2946		return FAILURE;
2947	} else {
2948		return SUCCESS;
2949	}
2950}
2951/* }}} */
2952
2953/* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
2954   Set an option for a cURL transfer */
2955PHP_FUNCTION(curl_setopt)
2956{
2957	zval       *zid, *zvalue;
2958	zend_long        options;
2959	php_curl   *ch;
2960
2961	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &zid, &options, &zvalue) == FAILURE) {
2962		return;
2963	}
2964
2965	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2966		RETURN_FALSE;
2967	}
2968
2969	if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
2970		php_error_docref(NULL, E_WARNING, "Invalid curl configuration option");
2971		RETURN_FALSE;
2972	}
2973
2974	if (_php_curl_setopt(ch, options, zvalue) == SUCCESS) {
2975		RETURN_TRUE;
2976	} else {
2977		RETURN_FALSE;
2978	}
2979}
2980/* }}} */
2981
2982/* {{{ proto bool curl_setopt_array(resource ch, array options)
2983   Set an array of option for a cURL transfer */
2984PHP_FUNCTION(curl_setopt_array)
2985{
2986	zval		*zid, *arr, *entry;
2987	php_curl	*ch;
2988	zend_ulong	option;
2989	zend_string	*string_key;
2990
2991	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra", &zid, &arr) == FAILURE) {
2992		return;
2993	}
2994
2995	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2996		RETURN_FALSE;
2997	}
2998
2999	ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
3000		if (string_key) {
3001			php_error_docref(NULL, E_WARNING,
3002					"Array keys must be CURLOPT constants or equivalent integer values");
3003			RETURN_FALSE;
3004		}
3005		if (_php_curl_setopt(ch, (zend_long) option, entry) == FAILURE) {
3006			RETURN_FALSE;
3007		}
3008	} ZEND_HASH_FOREACH_END();
3009
3010	RETURN_TRUE;
3011}
3012/* }}} */
3013
3014/* {{{ _php_curl_cleanup_handle(ch)
3015   Cleanup an execution phase */
3016void _php_curl_cleanup_handle(php_curl *ch)
3017{
3018	smart_str_free(&ch->handlers->write->buf);
3019	if (ch->header.str) {
3020		zend_string_release(ch->header.str);
3021		ch->header.str = NULL;
3022	}
3023
3024	memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
3025	ch->err.no = 0;
3026}
3027/* }}} */
3028
3029/* {{{ proto bool curl_exec(resource ch)
3030   Perform a cURL session */
3031PHP_FUNCTION(curl_exec)
3032{
3033	CURLcode	error;
3034	zval		*zid;
3035	php_curl	*ch;
3036
3037	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3038		return;
3039	}
3040
3041	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3042		RETURN_FALSE;
3043	}
3044
3045	_php_curl_verify_handlers(ch, 1);
3046
3047	_php_curl_cleanup_handle(ch);
3048
3049	error = curl_easy_perform(ch->cp);
3050	SAVE_CURL_ERROR(ch, error);
3051	/* CURLE_PARTIAL_FILE is returned by HEAD requests */
3052	if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
3053		smart_str_free(&ch->handlers->write->buf);
3054		RETURN_FALSE;
3055	}
3056
3057	if (!Z_ISUNDEF(ch->handlers->std_err)) {
3058		php_stream  *stream;
3059		stream = (php_stream*)zend_fetch_resource2_ex(&ch->handlers->std_err, NULL, php_file_le_stream(), php_file_le_pstream());
3060		if (stream) {
3061			php_stream_flush(stream);
3062		}
3063	}
3064
3065	if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.s) {
3066		smart_str_0(&ch->handlers->write->buf);
3067		RETURN_STR_COPY(ch->handlers->write->buf.s);
3068	}
3069
3070	/* flush the file handle, so any remaining data is synched to disk */
3071	if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
3072		fflush(ch->handlers->write->fp);
3073	}
3074	if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
3075		fflush(ch->handlers->write_header->fp);
3076	}
3077
3078	if (ch->handlers->write->method == PHP_CURL_RETURN) {
3079		RETURN_EMPTY_STRING();
3080	} else {
3081		RETURN_TRUE;
3082	}
3083}
3084/* }}} */
3085
3086/* {{{ proto mixed curl_getinfo(resource ch [, int option])
3087   Get information regarding a specific transfer */
3088PHP_FUNCTION(curl_getinfo)
3089{
3090	zval		*zid;
3091	php_curl	*ch;
3092	zend_long	option = 0;
3093
3094	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zid, &option) == FAILURE) {
3095		return;
3096	}
3097
3098	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3099		RETURN_FALSE;
3100	}
3101
3102	if (ZEND_NUM_ARGS() < 2) {
3103		char *s_code;
3104		/* libcurl expects long datatype. So far no cases are known where
3105		   it would be an issue. Using zend_long would truncate a 64-bit
3106		   var on Win64, so the exact long datatype fits everywhere, as
3107		   long as there's no 32-bit int overflow. */
3108		long l_code;
3109		double d_code;
3110#if LIBCURL_VERSION_NUM >  0x071301
3111		struct curl_certinfo *ci = NULL;
3112		zval listcode;
3113#endif
3114
3115		array_init(return_value);
3116
3117		if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
3118			CAAS("url", s_code);
3119		}
3120		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
3121			if (s_code != NULL) {
3122				CAAS("content_type", s_code);
3123			} else {
3124				zval retnull;
3125				ZVAL_NULL(&retnull);
3126				CAAZ("content_type", &retnull);
3127			}
3128		}
3129		if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
3130			CAAL("http_code", l_code);
3131		}
3132		if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
3133			CAAL("header_size", l_code);
3134		}
3135		if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
3136			CAAL("request_size", l_code);
3137		}
3138		if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
3139			CAAL("filetime", l_code);
3140		}
3141		if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
3142			CAAL("ssl_verify_result", l_code);
3143		}
3144		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
3145			CAAL("redirect_count", l_code);
3146		}
3147		if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
3148			CAAD("total_time", d_code);
3149		}
3150		if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
3151			CAAD("namelookup_time", d_code);
3152		}
3153		if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
3154			CAAD("connect_time", d_code);
3155		}
3156		if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
3157			CAAD("pretransfer_time", d_code);
3158		}
3159		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
3160			CAAD("size_upload", d_code);
3161		}
3162		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
3163			CAAD("size_download", d_code);
3164		}
3165		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
3166			CAAD("speed_download", d_code);
3167		}
3168		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
3169			CAAD("speed_upload", d_code);
3170		}
3171		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
3172			CAAD("download_content_length", d_code);
3173		}
3174		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
3175			CAAD("upload_content_length", d_code);
3176		}
3177		if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
3178			CAAD("starttransfer_time", d_code);
3179		}
3180		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
3181			CAAD("redirect_time", d_code);
3182		}
3183#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
3184		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
3185			CAAS("redirect_url", s_code);
3186		}
3187#endif
3188#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
3189		if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
3190			CAAS("primary_ip", s_code);
3191		}
3192#endif
3193#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3194		if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3195			array_init(&listcode);
3196			create_certinfo(ci, &listcode);
3197			CAAZ("certinfo", &listcode);
3198		}
3199#endif
3200#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3201		if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
3202			CAAL("primary_port", l_code);
3203		}
3204		if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
3205			CAAS("local_ip", s_code);
3206		}
3207		if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
3208			CAAL("local_port", l_code);
3209		}
3210#endif
3211		if (ch->header.str) {
3212			CAASTR("request_header", ch->header.str);
3213		}
3214	} else {
3215		switch (option) {
3216			case CURLINFO_HEADER_OUT:
3217				if (ch->header.str) {
3218					RETURN_STR_COPY(ch->header.str);
3219				} else {
3220					RETURN_FALSE;
3221				}
3222#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3223			case CURLINFO_CERTINFO: {
3224				struct curl_certinfo *ci = NULL;
3225
3226				array_init(return_value);
3227
3228				if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3229					create_certinfo(ci, return_value);
3230				} else {
3231					RETURN_FALSE;
3232				}
3233				break;
3234			}
3235#endif
3236			default: {
3237				int type = CURLINFO_TYPEMASK & option;
3238				switch (type) {
3239					case CURLINFO_STRING:
3240					{
3241						char *s_code = NULL;
3242
3243						if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
3244							RETURN_STRING(s_code);
3245						} else {
3246							RETURN_FALSE;
3247						}
3248						break;
3249					}
3250					case CURLINFO_LONG:
3251					{
3252						zend_long code = 0;
3253
3254						if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3255							RETURN_LONG(code);
3256						} else {
3257							RETURN_FALSE;
3258						}
3259						break;
3260					}
3261					case CURLINFO_DOUBLE:
3262					{
3263						double code = 0.0;
3264
3265						if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3266							RETURN_DOUBLE(code);
3267						} else {
3268							RETURN_FALSE;
3269						}
3270						break;
3271					}
3272#if LIBCURL_VERSION_NUM >= 0x070c03 /* Available since 7.12.3 */
3273					case CURLINFO_SLIST:
3274					{
3275						struct curl_slist *slist;
3276						array_init(return_value);
3277						if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
3278							while (slist) {
3279								add_next_index_string(return_value, slist->data);
3280								slist = slist->next;
3281							}
3282							curl_slist_free_all(slist);
3283						} else {
3284							RETURN_FALSE;
3285						}
3286						break;
3287					}
3288#endif
3289					default:
3290						RETURN_FALSE;
3291				}
3292			}
3293		}
3294	}
3295}
3296/* }}} */
3297
3298/* {{{ proto string curl_error(resource ch)
3299   Return a string contain the last error for the current session */
3300PHP_FUNCTION(curl_error)
3301{
3302	zval		*zid;
3303	php_curl	*ch;
3304
3305	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3306		return;
3307	}
3308
3309	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3310		RETURN_FALSE;
3311	}
3312
3313	ch->err.str[CURL_ERROR_SIZE] = 0;
3314	RETURN_STRING(ch->err.str);
3315}
3316/* }}} */
3317
3318/* {{{ proto int curl_errno(resource ch)
3319   Return an integer containing the last error number */
3320PHP_FUNCTION(curl_errno)
3321{
3322	zval		*zid;
3323	php_curl	*ch;
3324
3325	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3326		return;
3327	}
3328
3329	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3330		RETURN_FALSE;
3331	}
3332
3333	RETURN_LONG(ch->err.no);
3334}
3335/* }}} */
3336
3337/* {{{ proto void curl_close(resource ch)
3338   Close a cURL session */
3339PHP_FUNCTION(curl_close)
3340{
3341	zval		*zid;
3342	php_curl	*ch;
3343
3344	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3345		return;
3346	}
3347
3348	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3349		RETURN_FALSE;
3350	}
3351
3352	if (ch->in_callback) {
3353		php_error_docref(NULL, E_WARNING, "Attempt to close cURL handle from a callback");
3354		return;
3355	}
3356
3357	if (Z_REFCOUNT_P(zid) <= 2) {
3358		zend_list_close(Z_RES_P(zid));
3359	}
3360}
3361/* }}} */
3362
3363/* {{{ _php_curl_close_ex()
3364   List destructor for curl handles */
3365static void _php_curl_close_ex(php_curl *ch)
3366{
3367#if PHP_CURL_DEBUG
3368	fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
3369#endif
3370
3371	_php_curl_verify_handlers(ch, 0);
3372
3373	/*
3374	 * Libcurl is doing connection caching. When easy handle is cleaned up,
3375	 * if the handle was previously used by the curl_multi_api, the connection
3376	 * remains open un the curl multi handle is cleaned up. Some protocols are
3377	 * sending content like the FTP one, and libcurl try to use the
3378	 * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
3379	 * callback are freed, we need to use an other callback to which avoid
3380	 * segfaults.
3381	 *
3382	 * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
3383	 */
3384	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
3385	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
3386
3387	curl_easy_cleanup(ch->cp);
3388
3389	/* cURL destructors should be invoked only by last curl handle */
3390	if (--(*ch->clone) == 0) {
3391		zend_llist_clean(&ch->to_free->str);
3392		zend_llist_clean(&ch->to_free->post);
3393		zend_hash_destroy(ch->to_free->slist);
3394		efree(ch->to_free->slist);
3395		efree(ch->to_free);
3396		efree(ch->clone);
3397	}
3398
3399	smart_str_free(&ch->handlers->write->buf);
3400	zval_ptr_dtor(&ch->handlers->write->func_name);
3401	zval_ptr_dtor(&ch->handlers->read->func_name);
3402	zval_ptr_dtor(&ch->handlers->write_header->func_name);
3403#if CURLOPT_PASSWDFUNCTION != 0
3404	zval_ptr_dtor(&ch->handlers->passwd);
3405#endif
3406	zval_ptr_dtor(&ch->handlers->std_err);
3407	if (ch->header.str) {
3408		zend_string_release(ch->header.str);
3409	}
3410
3411	zval_ptr_dtor(&ch->handlers->write_header->stream);
3412	zval_ptr_dtor(&ch->handlers->write->stream);
3413	zval_ptr_dtor(&ch->handlers->read->stream);
3414
3415	efree(ch->handlers->write);
3416	efree(ch->handlers->write_header);
3417	efree(ch->handlers->read);
3418
3419	if (ch->handlers->progress) {
3420		zval_ptr_dtor(&ch->handlers->progress->func_name);
3421		efree(ch->handlers->progress);
3422	}
3423
3424#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3425	if (ch->handlers->fnmatch) {
3426		zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3427		efree(ch->handlers->fnmatch);
3428	}
3429#endif
3430
3431	efree(ch->handlers);
3432	efree(ch);
3433}
3434/* }}} */
3435
3436/* {{{ _php_curl_close()
3437   List destructor for curl handles */
3438static void _php_curl_close(zend_resource *rsrc)
3439{
3440	php_curl *ch = (php_curl *) rsrc->ptr;
3441	_php_curl_close_ex(ch);
3442}
3443/* }}} */
3444
3445#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
3446/* {{{ proto bool curl_strerror(int code)
3447      return string describing error code */
3448PHP_FUNCTION(curl_strerror)
3449{
3450	zend_long code;
3451	const char *str;
3452
3453	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
3454		return;
3455	}
3456
3457	str = curl_easy_strerror(code);
3458	if (str) {
3459		RETURN_STRING(str);
3460	} else {
3461		RETURN_NULL();
3462	}
3463}
3464/* }}} */
3465#endif
3466
3467#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
3468/* {{{ _php_curl_reset_handlers()
3469   Reset all handlers of a given php_curl */
3470static void _php_curl_reset_handlers(php_curl *ch)
3471{
3472	if (!Z_ISUNDEF(ch->handlers->write->stream)) {
3473		zval_ptr_dtor(&ch->handlers->write->stream);
3474		ZVAL_UNDEF(&ch->handlers->write->stream);
3475	}
3476	ch->handlers->write->fp = NULL;
3477	ch->handlers->write->method = PHP_CURL_STDOUT;
3478
3479	if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
3480		zval_ptr_dtor(&ch->handlers->write_header->stream);
3481		ZVAL_UNDEF(&ch->handlers->write_header->stream);
3482	}
3483	ch->handlers->write_header->fp = NULL;
3484	ch->handlers->write_header->method = PHP_CURL_IGNORE;
3485
3486	if (!Z_ISUNDEF(ch->handlers->read->stream)) {
3487		zval_ptr_dtor(&ch->handlers->read->stream);
3488		ZVAL_UNDEF(&ch->handlers->read->stream);
3489	}
3490	ch->handlers->read->fp = NULL;
3491	ch->handlers->read->res = NULL;
3492	ch->handlers->read->method  = PHP_CURL_DIRECT;
3493
3494	if (!Z_ISUNDEF(ch->handlers->std_err)) {
3495		zval_ptr_dtor(&ch->handlers->std_err);
3496		ZVAL_UNDEF(&ch->handlers->std_err);
3497	}
3498
3499	if (ch->handlers->progress) {
3500		zval_ptr_dtor(&ch->handlers->progress->func_name);
3501		efree(ch->handlers->progress);
3502		ch->handlers->progress = NULL;
3503	}
3504
3505#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3506	if (ch->handlers->fnmatch) {
3507		zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3508		efree(ch->handlers->fnmatch);
3509		ch->handlers->fnmatch = NULL;
3510	}
3511#endif
3512
3513}
3514/* }}} */
3515
3516/* {{{ proto void curl_reset(resource ch)
3517   Reset all options of a libcurl session handle */
3518PHP_FUNCTION(curl_reset)
3519{
3520	zval       *zid;
3521	php_curl   *ch;
3522
3523	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3524		return;
3525	}
3526
3527	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3528		RETURN_FALSE;
3529	}
3530
3531	if (ch->in_callback) {
3532		php_error_docref(NULL, E_WARNING, "Attempt to reset cURL handle from a callback");
3533		return;
3534	}
3535
3536	curl_easy_reset(ch->cp);
3537	_php_curl_reset_handlers(ch);
3538	_php_curl_set_default_options(ch);
3539}
3540/* }}} */
3541#endif
3542
3543#if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
3544/* {{{ proto void curl_escape(resource ch, string str)
3545   URL encodes the given string */
3546PHP_FUNCTION(curl_escape)
3547{
3548	char       *str = NULL, *res = NULL;
3549	size_t        str_len = 0;
3550	zval       *zid;
3551	php_curl   *ch;
3552
3553	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zid, &str, &str_len) == FAILURE) {
3554		return;
3555	}
3556
3557	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3558		RETURN_FALSE;
3559	}
3560
3561	if ((res = curl_easy_escape(ch->cp, str, str_len))) {
3562		RETVAL_STRING(res);
3563		curl_free(res);
3564	} else {
3565		RETURN_FALSE;
3566	}
3567}
3568/* }}} */
3569
3570/* {{{ proto void curl_unescape(resource ch, string str)
3571   URL decodes the given string */
3572PHP_FUNCTION(curl_unescape)
3573{
3574	char       *str = NULL, *out = NULL;
3575	size_t     str_len = 0;
3576	int        out_len;
3577	zval       *zid;
3578	php_curl   *ch;
3579
3580	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zid, &str, &str_len) == FAILURE) {
3581		return;
3582	}
3583
3584	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3585		RETURN_FALSE;
3586	}
3587
3588	if ((out = curl_easy_unescape(ch->cp, str, str_len, &out_len))) {
3589		RETVAL_STRINGL(out, out_len);
3590		curl_free(out);
3591	} else {
3592		RETURN_FALSE;
3593	}
3594}
3595/* }}} */
3596#endif
3597
3598#if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
3599/* {{{ proto void curl_pause(resource ch, int bitmask)
3600       pause and unpause a connection */
3601PHP_FUNCTION(curl_pause)
3602{
3603	zend_long       bitmask;
3604	zval       *zid;
3605	php_curl   *ch;
3606
3607	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zid, &bitmask) == FAILURE) {
3608		return;
3609	}
3610
3611	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3612		RETURN_FALSE;
3613	}
3614
3615	RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3616}
3617/* }}} */
3618#endif
3619
3620#endif /* HAVE_CURL */
3621
3622/*
3623 * Local variables:
3624 * tab-width: 4
3625 * c-basic-offset: 4
3626 * End:
3627 * vim600: fdm=marker
3628 * vim: noet sw=4 ts=4
3629 */
3630