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