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