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                  = 1;
1743
1744    memset(&ch->err, 0, sizeof(struct _php_curl_error));
1745
1746    zend_llist_init(&ch->to_free->str,   sizeof(char *),          (llist_dtor_func_t)curl_free_string, 0);
1747    zend_llist_init(&ch->to_free->post,  sizeof(struct HttpPost), (llist_dtor_func_t)curl_free_post,   0);
1748
1749    ch->to_free->slist = emalloc(sizeof(HashTable));
1750    zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
1751
1752    return ch;
1753}
1754/* }}} */
1755
1756#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1757/* {{{ split_certinfo
1758 */
1759static void split_certinfo(char *string, zval *hash)
1760{
1761    char *org = estrdup(string);
1762    char *s = org;
1763    char *split;
1764
1765    if(org) {
1766        do {
1767            char *key;
1768            char *val;
1769            char *tmp;
1770
1771            split = strstr(s, "; ");
1772            if(split)
1773                *split = '\0';
1774
1775            key = s;
1776            tmp = memchr(key, '=', 64);
1777            if(tmp) {
1778                *tmp = '\0';
1779                val = tmp+1;
1780                add_assoc_string(hash, key, val);
1781            }
1782            s = split+2;
1783        } while(split);
1784        efree(org);
1785    }
1786}
1787/* }}} */
1788
1789/* {{{ create_certinfo
1790 */
1791static void create_certinfo(struct curl_certinfo *ci, zval *listcode)
1792{
1793    int i;
1794
1795    if (ci) {
1796        zval certhash;
1797
1798        for (i=0; i<ci->num_of_certs; i++) {
1799            struct curl_slist *slist;
1800
1801            array_init(&certhash);
1802            for (slist = ci->certinfo[i]; slist; slist = slist->next) {
1803                int len;
1804                char s[64];
1805                char *tmp;
1806                strncpy(s, slist->data, 64);
1807                tmp = memchr(s, ':', 64);
1808                if(tmp) {
1809                    *tmp = '\0';
1810                    len = strlen(s);
1811                    if (!strcmp(s, "Subject") || !strcmp(s, "Issuer")) {
1812                        zval hash;
1813
1814                        array_init(&hash);
1815
1816                        split_certinfo(&slist->data[len+1], &hash);
1817                        add_assoc_zval(&certhash, s, &hash);
1818                    } else {
1819                        add_assoc_string(&certhash, s, &slist->data[len+1]);
1820                    }
1821                } else {
1822                    php_error_docref(NULL, E_WARNING, "Could not extract hash key from certificate info");
1823                }
1824            }
1825            add_next_index_zval(listcode, &certhash);
1826        }
1827    }
1828}
1829/* }}} */
1830#endif
1831
1832/* {{{ _php_curl_set_default_options()
1833   Set default options for a handle */
1834static void _php_curl_set_default_options(php_curl *ch)
1835{
1836    char *cainfo;
1837
1838    curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
1839    curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
1840    curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1841    curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
1842    curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1843    curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
1844    curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1845    curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
1846    curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1847    curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
1848    curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1849    curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1850
1851    cainfo = INI_STR("openssl.cafile");
1852    if (!(cainfo && cainfo[0] != '\0')) {
1853        cainfo = INI_STR("curl.cainfo");
1854    }
1855    if (cainfo && cainfo[0] != '\0') {
1856        curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1857    }
1858
1859#if defined(ZTS)
1860    curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1861#endif
1862}
1863/* }}} */
1864
1865/* {{{ proto resource curl_init([string url])
1866   Initialize a cURL session */
1867PHP_FUNCTION(curl_init)
1868{
1869    php_curl *ch;
1870    CURL     *cp;
1871    char     *url = NULL;
1872    size_t        url_len = 0;
1873
1874    if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &url, &url_len) == FAILURE) {
1875        return;
1876    }
1877
1878    cp = curl_easy_init();
1879    if (!cp) {
1880        php_error_docref(NULL, E_WARNING, "Could not initialize a new cURL handle");
1881        RETURN_FALSE;
1882    }
1883
1884    ch = alloc_curl_handle();
1885
1886    ch->cp = cp;
1887
1888    ch->handlers->write->method = PHP_CURL_STDOUT;
1889    ch->handlers->read->method  = PHP_CURL_DIRECT;
1890    ch->handlers->write_header->method = PHP_CURL_IGNORE;
1891
1892    _php_curl_set_default_options(ch);
1893
1894    if (url) {
1895        if (php_curl_option_url(ch, url, url_len) == FAILURE) {
1896            _php_curl_close_ex(ch);
1897            RETURN_FALSE;
1898        }
1899    }
1900
1901    ZVAL_RES(return_value, zend_register_resource(ch, le_curl));
1902    ch->res = Z_RES_P(return_value);
1903}
1904/* }}} */
1905
1906/* {{{ proto resource curl_copy_handle(resource ch)
1907   Copy a cURL handle along with all of it's preferences */
1908PHP_FUNCTION(curl_copy_handle)
1909{
1910    CURL        *cp;
1911    zval        *zid;
1912    php_curl    *ch, *dupch;
1913
1914    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
1915        return;
1916    }
1917
1918    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
1919        RETURN_FALSE;
1920    }
1921
1922    cp = curl_easy_duphandle(ch->cp);
1923    if (!cp) {
1924        php_error_docref(NULL, E_WARNING, "Cannot duplicate cURL handle");
1925        RETURN_FALSE;
1926    }
1927
1928    dupch = alloc_curl_handle();
1929
1930    dupch->cp = cp;
1931    Z_ADDREF_P(zid);
1932    if (!Z_ISUNDEF(ch->handlers->write->stream)) {
1933        Z_ADDREF(ch->handlers->write->stream);
1934    }
1935    dupch->handlers->write->stream = ch->handlers->write->stream;
1936    dupch->handlers->write->method = ch->handlers->write->method;
1937    if (!Z_ISUNDEF(ch->handlers->read->stream)) {
1938        Z_ADDREF(ch->handlers->read->stream);
1939    }
1940    dupch->handlers->read->stream  = ch->handlers->read->stream;
1941    dupch->handlers->read->method  = ch->handlers->read->method;
1942    dupch->handlers->write_header->method = ch->handlers->write_header->method;
1943    if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
1944        Z_ADDREF(ch->handlers->write_header->stream);
1945    }
1946    dupch->handlers->write_header->stream = ch->handlers->write_header->stream;
1947
1948    dupch->handlers->write->fp = ch->handlers->write->fp;
1949    dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
1950    dupch->handlers->read->fp = ch->handlers->read->fp;
1951    dupch->handlers->read->res = ch->handlers->read->res;
1952#if CURLOPT_PASSWDDATA != 0
1953    if (!Z_ISUNDEF(ch->handlers->passwd)) {
1954        ZVAL_COPY(&dupch->handlers->passwd, &ch->handlers->passwd);
1955        curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
1956    }
1957#endif
1958    if (!Z_ISUNDEF(ch->handlers->write->func_name)) {
1959        ZVAL_COPY(&dupch->handlers->write->func_name, &ch->handlers->write->func_name);
1960    }
1961    if (!Z_ISUNDEF(ch->handlers->read->func_name)) {
1962        ZVAL_COPY(&dupch->handlers->read->func_name, &ch->handlers->read->func_name);
1963    }
1964    if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) {
1965        ZVAL_COPY(&dupch->handlers->write_header->func_name, &ch->handlers->write_header->func_name);
1966    }
1967
1968    curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER,       dupch->err.str);
1969    curl_easy_setopt(dupch->cp, CURLOPT_FILE,              (void *) dupch);
1970    curl_easy_setopt(dupch->cp, CURLOPT_INFILE,            (void *) dupch);
1971    curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER,       (void *) dupch);
1972
1973    if (ch->handlers->progress) {
1974        dupch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
1975        if (!Z_ISUNDEF(ch->handlers->progress->func_name)) {
1976            ZVAL_COPY(&dupch->handlers->progress->func_name, &ch->handlers->progress->func_name);
1977        }
1978        dupch->handlers->progress->method = ch->handlers->progress->method;
1979        curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA, (void *) dupch);
1980    }
1981
1982/* Available since 7.21.0 */
1983#if LIBCURL_VERSION_NUM >= 0x071500
1984    if (ch->handlers->fnmatch) {
1985        dupch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
1986        if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) {
1987            ZVAL_COPY(&dupch->handlers->fnmatch->func_name, &ch->handlers->fnmatch->func_name);
1988        }
1989        dupch->handlers->fnmatch->method = ch->handlers->fnmatch->method;
1990        curl_easy_setopt(dupch->cp, CURLOPT_FNMATCH_DATA, (void *) dupch);
1991    }
1992#endif
1993
1994    efree(dupch->to_free->slist);
1995    efree(dupch->to_free);
1996    dupch->to_free = ch->to_free;
1997
1998    /* Keep track of cloned copies to avoid invoking curl destructors for every clone */
1999    ch->clone++;
2000
2001    ZVAL_RES(return_value, zend_register_resource(dupch, le_curl));
2002    dupch->res = Z_RES_P(return_value);
2003}
2004/* }}} */
2005
2006static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */
2007{
2008    CURLcode error = CURLE_OK;
2009    zend_long lval;
2010
2011    switch (option) {
2012        /* Long options */
2013        case CURLOPT_SSL_VERIFYHOST:
2014            lval = zval_get_long(zvalue);
2015            if (lval == 1) {
2016#if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
2017                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");
2018#else
2019                php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
2020                error = curl_easy_setopt(ch->cp, option, 2);
2021                break;
2022#endif
2023            }
2024        case CURLOPT_AUTOREFERER:
2025        case CURLOPT_BUFFERSIZE:
2026        case CURLOPT_CONNECTTIMEOUT:
2027        case CURLOPT_COOKIESESSION:
2028        case CURLOPT_CRLF:
2029        case CURLOPT_DNS_CACHE_TIMEOUT:
2030        case CURLOPT_DNS_USE_GLOBAL_CACHE:
2031        case CURLOPT_FAILONERROR:
2032        case CURLOPT_FILETIME:
2033        case CURLOPT_FORBID_REUSE:
2034        case CURLOPT_FRESH_CONNECT:
2035        case CURLOPT_FTP_USE_EPRT:
2036        case CURLOPT_FTP_USE_EPSV:
2037        case CURLOPT_HEADER:
2038        case CURLOPT_HTTPGET:
2039        case CURLOPT_HTTPPROXYTUNNEL:
2040        case CURLOPT_HTTP_VERSION:
2041        case CURLOPT_INFILESIZE:
2042        case CURLOPT_LOW_SPEED_LIMIT:
2043        case CURLOPT_LOW_SPEED_TIME:
2044        case CURLOPT_MAXCONNECTS:
2045        case CURLOPT_MAXREDIRS:
2046        case CURLOPT_NETRC:
2047        case CURLOPT_NOBODY:
2048        case CURLOPT_NOPROGRESS:
2049        case CURLOPT_NOSIGNAL:
2050        case CURLOPT_PORT:
2051        case CURLOPT_POST:
2052        case CURLOPT_PROXYPORT:
2053        case CURLOPT_PROXYTYPE:
2054        case CURLOPT_PUT:
2055        case CURLOPT_RESUME_FROM:
2056        case CURLOPT_SSLVERSION:
2057        case CURLOPT_SSL_VERIFYPEER:
2058        case CURLOPT_TIMECONDITION:
2059        case CURLOPT_TIMEOUT:
2060        case CURLOPT_TIMEVALUE:
2061        case CURLOPT_TRANSFERTEXT:
2062        case CURLOPT_UNRESTRICTED_AUTH:
2063        case CURLOPT_UPLOAD:
2064        case CURLOPT_VERBOSE:
2065#if LIBCURL_VERSION_NUM >= 0x070a06 /* Available since 7.10.6 */
2066        case CURLOPT_HTTPAUTH:
2067#endif
2068#if LIBCURL_VERSION_NUM >= 0x070a07 /* Available since 7.10.7 */
2069        case CURLOPT_FTP_CREATE_MISSING_DIRS:
2070        case CURLOPT_PROXYAUTH:
2071#endif
2072#if LIBCURL_VERSION_NUM >= 0x070a08 /* Available since 7.10.8 */
2073        case CURLOPT_FTP_RESPONSE_TIMEOUT:
2074        case CURLOPT_IPRESOLVE:
2075        case CURLOPT_MAXFILESIZE:
2076#endif
2077#if LIBCURL_VERSION_NUM >= 0x070b02 /* Available since 7.11.2 */
2078        case CURLOPT_TCP_NODELAY:
2079#endif
2080#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
2081        case CURLOPT_FTPSSLAUTH:
2082#endif
2083#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
2084        case CURLOPT_IGNORE_CONTENT_LENGTH:
2085#endif
2086#if LIBCURL_VERSION_NUM >= 0x070f00 /* Available since 7.15.0 */
2087        case CURLOPT_FTP_SKIP_PASV_IP:
2088#endif
2089#if LIBCURL_VERSION_NUM >= 0x070f01 /* Available since 7.15.1 */
2090        case CURLOPT_FTP_FILEMETHOD:
2091#endif
2092#if LIBCURL_VERSION_NUM >= 0x070f02 /* Available since 7.15.2 */
2093        case CURLOPT_CONNECT_ONLY:
2094        case CURLOPT_LOCALPORT:
2095        case CURLOPT_LOCALPORTRANGE:
2096#endif
2097#if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
2098        case CURLOPT_SSL_SESSIONID_CACHE:
2099#endif
2100#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2101        case CURLOPT_FTP_SSL_CCC:
2102        case CURLOPT_SSH_AUTH_TYPES:
2103#endif
2104#if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
2105        case CURLOPT_CONNECTTIMEOUT_MS:
2106        case CURLOPT_HTTP_CONTENT_DECODING:
2107        case CURLOPT_HTTP_TRANSFER_DECODING:
2108        case CURLOPT_TIMEOUT_MS:
2109#endif
2110#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2111        case CURLOPT_NEW_DIRECTORY_PERMS:
2112        case CURLOPT_NEW_FILE_PERMS:
2113#endif
2114#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2115        case CURLOPT_USE_SSL:
2116#elif LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2117        case CURLOPT_FTP_SSL:
2118#endif
2119#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2120        case CURLOPT_APPEND:
2121        case CURLOPT_DIRLISTONLY:
2122#else
2123        case CURLOPT_FTPAPPEND:
2124        case CURLOPT_FTPLISTONLY:
2125#endif
2126#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
2127        case CURLOPT_PROXY_TRANSFER_MODE:
2128#endif
2129#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2130        case CURLOPT_ADDRESS_SCOPE:
2131#endif
2132#if LIBCURL_VERSION_NUM >  0x071301 /* Available since 7.19.1 */
2133        case CURLOPT_CERTINFO:
2134#endif
2135#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2136        case CURLOPT_NOPROXY:
2137        case CURLOPT_PROTOCOLS:
2138        case CURLOPT_REDIR_PROTOCOLS:
2139        case CURLOPT_SOCKS5_GSSAPI_NEC:
2140        case CURLOPT_TFTP_BLKSIZE:
2141#endif
2142#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2143        case CURLOPT_FTP_USE_PRET:
2144        case CURLOPT_RTSP_CLIENT_CSEQ:
2145        case CURLOPT_RTSP_REQUEST:
2146        case CURLOPT_RTSP_SERVER_CSEQ:
2147#endif
2148#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2149        case CURLOPT_WILDCARDMATCH:
2150#endif
2151#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2152        case CURLOPT_TLSAUTH_TYPE:
2153#endif
2154#if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
2155        case CURLOPT_GSSAPI_DELEGATION:
2156#endif
2157#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2158        case CURLOPT_ACCEPTTIMEOUT_MS:
2159#endif
2160#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2161        case CURLOPT_SSL_OPTIONS:
2162        case CURLOPT_TCP_KEEPALIVE:
2163        case CURLOPT_TCP_KEEPIDLE:
2164        case CURLOPT_TCP_KEEPINTVL:
2165#endif
2166#if CURLOPT_MUTE != 0
2167        case CURLOPT_MUTE:
2168#endif
2169            lval = zval_get_long(zvalue);
2170#if LIBCURL_VERSION_NUM >= 0x71304
2171            if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
2172                (PG(open_basedir) && *PG(open_basedir)) && (lval & CURLPROTO_FILE)) {
2173                    php_error_docref(NULL, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
2174                    return 1;
2175            }
2176#endif
2177            error = curl_easy_setopt(ch->cp, option, lval);
2178            break;
2179        case CURLOPT_SAFE_UPLOAD:
2180            lval = zval_get_long(zvalue);
2181            if (lval == 0) {
2182                php_error_docref(NULL, E_WARNING, "Disabling safe uploads is no longer supported");
2183                return FAILURE;
2184            }
2185            break;
2186
2187        /* String options */
2188        case CURLOPT_CAINFO:
2189        case CURLOPT_CAPATH:
2190        case CURLOPT_COOKIE:
2191        case CURLOPT_EGDSOCKET:
2192        case CURLOPT_INTERFACE:
2193        case CURLOPT_PROXY:
2194        case CURLOPT_PROXYUSERPWD:
2195        case CURLOPT_REFERER:
2196        case CURLOPT_SSLCERTTYPE:
2197        case CURLOPT_SSLENGINE:
2198        case CURLOPT_SSLENGINE_DEFAULT:
2199        case CURLOPT_SSLKEY:
2200        case CURLOPT_SSLKEYPASSWD:
2201        case CURLOPT_SSLKEYTYPE:
2202        case CURLOPT_SSL_CIPHER_LIST:
2203        case CURLOPT_USERAGENT:
2204        case CURLOPT_USERPWD:
2205#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
2206        case CURLOPT_COOKIELIST:
2207#endif
2208#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2209        case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2210#endif
2211#if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
2212        case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2213#endif
2214#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2215        case CURLOPT_PASSWORD:
2216        case CURLOPT_PROXYPASSWORD:
2217        case CURLOPT_PROXYUSERNAME:
2218        case CURLOPT_USERNAME:
2219#endif
2220#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2221        case CURLOPT_SOCKS5_GSSAPI_SERVICE:
2222#endif
2223#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2224        case CURLOPT_MAIL_FROM:
2225        case CURLOPT_RTSP_STREAM_URI:
2226        case CURLOPT_RTSP_TRANSPORT:
2227#endif
2228#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2229        case CURLOPT_TLSAUTH_PASSWORD:
2230        case CURLOPT_TLSAUTH_USERNAME:
2231#endif
2232#if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
2233        case CURLOPT_ACCEPT_ENCODING:
2234        case CURLOPT_TRANSFER_ENCODING:
2235#else
2236        case CURLOPT_ENCODING:
2237#endif
2238#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2239        case CURLOPT_DNS_SERVERS:
2240#endif
2241#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2242        case CURLOPT_MAIL_AUTH:
2243#endif
2244        {
2245            zend_string *str = zval_get_string(zvalue);
2246            int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2247            zend_string_release(str);
2248            return ret;
2249        }
2250
2251        /* Curl nullable string options */
2252        case CURLOPT_CUSTOMREQUEST:
2253        case CURLOPT_FTPPORT:
2254        case CURLOPT_RANGE:
2255#if LIBCURL_VERSION_NUM >= 0x070d00 /* Available since 7.13.0 */
2256        case CURLOPT_FTP_ACCOUNT:
2257#endif
2258#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2259        case CURLOPT_RTSP_SESSION_ID:
2260#endif
2261#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2262        case CURLOPT_KRBLEVEL:
2263#else
2264        case CURLOPT_KRB4LEVEL:
2265#endif
2266        {
2267            if (Z_ISNULL_P(zvalue)) {
2268                error = curl_easy_setopt(ch->cp, option, NULL);
2269            } else {
2270                zend_string *str = zval_get_string(zvalue);
2271                int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2272                zend_string_release(str);
2273                return ret;
2274            }
2275            break;
2276        }
2277
2278        /* Curl private option */
2279        case CURLOPT_PRIVATE:
2280        {
2281            zend_string *str = zval_get_string(zvalue);
2282            int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 1);
2283            zend_string_release(str);
2284            return ret;
2285        }
2286
2287        /* Curl url option */
2288        case CURLOPT_URL:
2289        {
2290            zend_string *str = zval_get_string(zvalue);
2291            int ret = php_curl_option_url(ch, ZSTR_VAL(str), ZSTR_LEN(str));
2292            zend_string_release(str);
2293            return ret;
2294        }
2295
2296        /* Curl file handle options */
2297        case CURLOPT_FILE:
2298        case CURLOPT_INFILE:
2299        case CURLOPT_STDERR:
2300        case CURLOPT_WRITEHEADER: {
2301            FILE *fp = NULL;
2302            php_stream *what = NULL;
2303
2304            if (Z_TYPE_P(zvalue) != IS_NULL) {
2305                what = (php_stream *)zend_fetch_resource2_ex(zvalue, "File-Handle", php_file_le_stream(), php_file_le_pstream());
2306                if (!what) {
2307                    return FAILURE;
2308                }
2309
2310                if (FAILURE == php_stream_cast(what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2311                    return FAILURE;
2312                }
2313
2314                if (!fp) {
2315                    return FAILURE;
2316                }
2317            }
2318
2319            error = CURLE_OK;
2320            switch (option) {
2321                case CURLOPT_FILE:
2322                    if (!what) {
2323                        if (!Z_ISUNDEF(ch->handlers->write->stream)) {
2324                            zval_ptr_dtor(&ch->handlers->write->stream);
2325                            ZVAL_UNDEF(&ch->handlers->write->stream);
2326                        }
2327                        ch->handlers->write->fp = NULL;
2328                        ch->handlers->write->method = PHP_CURL_STDOUT;
2329                    } else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2330                        zval_ptr_dtor(&ch->handlers->write->stream);
2331                        ch->handlers->write->fp = fp;
2332                        ch->handlers->write->method = PHP_CURL_FILE;
2333                        ZVAL_COPY(&ch->handlers->write->stream, zvalue);
2334                    } else {
2335                        php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2336                        return FAILURE;
2337                    }
2338                    break;
2339                case CURLOPT_WRITEHEADER:
2340                    if (!what) {
2341                        if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
2342                            zval_ptr_dtor(&ch->handlers->write_header->stream);
2343                            ZVAL_UNDEF(&ch->handlers->write_header->stream);
2344                        }
2345                        ch->handlers->write_header->fp = NULL;
2346                        ch->handlers->write_header->method = PHP_CURL_IGNORE;
2347                    } else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2348                        zval_ptr_dtor(&ch->handlers->write_header->stream);
2349                        ch->handlers->write_header->fp = fp;
2350                        ch->handlers->write_header->method = PHP_CURL_FILE;
2351                        ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);;
2352                    } else {
2353                        php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2354                        return FAILURE;
2355                    }
2356                    break;
2357                case CURLOPT_INFILE:
2358                    if (!what) {
2359                        if (!Z_ISUNDEF(ch->handlers->read->stream)) {
2360                            zval_ptr_dtor(&ch->handlers->read->stream);
2361                            ZVAL_UNDEF(&ch->handlers->read->stream);
2362                        }
2363                        ch->handlers->read->fp = NULL;
2364                        ch->handlers->read->res = NULL;
2365                    } else {
2366                        zval_ptr_dtor(&ch->handlers->read->stream);
2367                        ch->handlers->read->fp = fp;
2368                        ch->handlers->read->res = Z_RES_P(zvalue);
2369                        ZVAL_COPY(&ch->handlers->read->stream, zvalue);
2370                    }
2371                    break;
2372                case CURLOPT_STDERR:
2373                    if (!what) {
2374                        if (!Z_ISUNDEF(ch->handlers->std_err)) {
2375                            zval_ptr_dtor(&ch->handlers->std_err);
2376                            ZVAL_UNDEF(&ch->handlers->std_err);
2377                        }
2378                    } else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2379                        zval_ptr_dtor(&ch->handlers->std_err);
2380                        ZVAL_COPY(&ch->handlers->std_err, zvalue);
2381                    } else {
2382                        php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2383                        return FAILURE;
2384                    }
2385                    /* break omitted intentionally */
2386                default:
2387                    error = curl_easy_setopt(ch->cp, option, fp);
2388                    break;
2389            }
2390            break;
2391        }
2392
2393        /* Curl linked list options */
2394        case CURLOPT_HTTP200ALIASES:
2395        case CURLOPT_HTTPHEADER:
2396        case CURLOPT_POSTQUOTE:
2397        case CURLOPT_PREQUOTE:
2398        case CURLOPT_QUOTE:
2399        case CURLOPT_TELNETOPTIONS:
2400#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2401        case CURLOPT_MAIL_RCPT:
2402#endif
2403#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2404        case CURLOPT_RESOLVE:
2405#endif
2406        {
2407            zval *current;
2408            HashTable *ph;
2409            zend_string *val;
2410            struct curl_slist *slist = NULL;
2411
2412            ph = HASH_OF(zvalue);
2413            if (!ph) {
2414                char *name = NULL;
2415                switch (option) {
2416                    case CURLOPT_HTTPHEADER:
2417                        name = "CURLOPT_HTTPHEADER";
2418                        break;
2419                    case CURLOPT_QUOTE:
2420                        name = "CURLOPT_QUOTE";
2421                        break;
2422                    case CURLOPT_HTTP200ALIASES:
2423                        name = "CURLOPT_HTTP200ALIASES";
2424                        break;
2425                    case CURLOPT_POSTQUOTE:
2426                        name = "CURLOPT_POSTQUOTE";
2427                        break;
2428                    case CURLOPT_PREQUOTE:
2429                        name = "CURLOPT_PREQUOTE";
2430                        break;
2431                    case CURLOPT_TELNETOPTIONS:
2432                        name = "CURLOPT_TELNETOPTIONS";
2433                        break;
2434#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2435                    case CURLOPT_MAIL_RCPT:
2436                        name = "CURLOPT_MAIL_RCPT";
2437                        break;
2438#endif
2439#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2440                    case CURLOPT_RESOLVE:
2441                        name = "CURLOPT_RESOLVE";
2442                        break;
2443#endif
2444                }
2445                php_error_docref(NULL, E_WARNING, "You must pass either an object or an array with the %s argument", name);
2446                return FAILURE;
2447            }
2448
2449            ZEND_HASH_FOREACH_VAL(ph, current) {
2450                val = zval_get_string(current);
2451                slist = curl_slist_append(slist, ZSTR_VAL(val));
2452                zend_string_release(val);
2453                if (!slist) {
2454                    php_error_docref(NULL, E_WARNING, "Could not build curl_slist");
2455                    return 1;
2456                }
2457            } ZEND_HASH_FOREACH_END();
2458
2459            zend_hash_index_update_ptr(ch->to_free->slist, option, slist);
2460
2461            error = curl_easy_setopt(ch->cp, option, slist);
2462
2463            break;
2464        }
2465
2466        case CURLOPT_BINARYTRANSFER:
2467            /* Do nothing, just backward compatibility */
2468            break;
2469
2470        case CURLOPT_FOLLOWLOCATION:
2471            lval = zval_get_long(zvalue);
2472#if LIBCURL_VERSION_NUM < 0x071304
2473            if (PG(open_basedir) && *PG(open_basedir)) {
2474                if (lval != 0) {
2475                    php_error_docref(NULL, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set");
2476                    return FAILURE;
2477                }
2478            }
2479#endif
2480            error = curl_easy_setopt(ch->cp, option, lval);
2481            break;
2482
2483        case CURLOPT_HEADERFUNCTION:
2484            if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) {
2485                zval_ptr_dtor(&ch->handlers->write_header->func_name);
2486                ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
2487            }
2488            ZVAL_COPY(&ch->handlers->write_header->func_name, zvalue);
2489            ch->handlers->write_header->method = PHP_CURL_USER;
2490            break;
2491
2492        case CURLOPT_POSTFIELDS:
2493            if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
2494                zval *current;
2495                HashTable *postfields;
2496                zend_string *string_key;
2497                zend_ulong  num_key;
2498                struct HttpPost *first = NULL;
2499                struct HttpPost *last  = NULL;
2500                CURLFORMcode form_error;
2501
2502                postfields = HASH_OF(zvalue);
2503                if (!postfields) {
2504                    php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2505                    return FAILURE;
2506                }
2507
2508                ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
2509                    zend_string *postval;
2510                    /* Pretend we have a string_key here */
2511                    if (!string_key) {
2512                        string_key = zend_long_to_str(num_key);
2513                    } else {
2514                        zend_string_addref(string_key);
2515                    }
2516
2517                    if (Z_TYPE_P(current) == IS_OBJECT &&
2518                            instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
2519                        /* new-style file upload */
2520                        zval *prop, rv;
2521                        char *type = NULL, *filename = NULL;
2522
2523                        prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
2524                        if (Z_TYPE_P(prop) != IS_STRING) {
2525                            php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
2526                        } else {
2527                            postval = Z_STR_P(prop);
2528
2529                            if (php_check_open_basedir(ZSTR_VAL(postval))) {
2530                                return 1;
2531                            }
2532
2533                            prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv);
2534                            if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2535                                type = Z_STRVAL_P(prop);
2536                            }
2537                            prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv);
2538                            if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2539                                filename = Z_STRVAL_P(prop);
2540                            }
2541                            form_error = curl_formadd(&first, &last,
2542                                            CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2543                                            CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2544                                            CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
2545                                            CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2546                                            CURLFORM_FILE, ZSTR_VAL(postval),
2547                                            CURLFORM_END);
2548                            if (form_error != CURL_FORMADD_OK) {
2549                                /* Not nice to convert between enums but we only have place for one error type */
2550                                error = (CURLcode)form_error;
2551                            }
2552                        }
2553
2554                        zend_string_release(string_key);
2555                        continue;
2556                    }
2557
2558                    postval = zval_get_string(current);
2559
2560                    /* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2561                     * must be explicitly cast to long in curl_formadd
2562                     * use since curl needs a long not an int. */
2563                    form_error = curl_formadd(&first, &last,
2564                                         CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2565                                         CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2566                                         CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
2567                                         CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
2568                                         CURLFORM_END);
2569
2570                    if (form_error != CURL_FORMADD_OK) {
2571                        /* Not nice to convert between enums but we only have place for one error type */
2572                        error = (CURLcode)form_error;
2573                    }
2574                    zend_string_release(postval);
2575                    zend_string_release(string_key);
2576                } ZEND_HASH_FOREACH_END();
2577
2578                SAVE_CURL_ERROR(ch, error);
2579                if (error != CURLE_OK) {
2580                    return FAILURE;
2581                }
2582
2583                if (ch->clone == 0) {
2584                    zend_llist_clean(&ch->to_free->post);
2585                }
2586                zend_llist_add_element(&ch->to_free->post, &first);
2587                error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2588            } else {
2589#if LIBCURL_VERSION_NUM >= 0x071101
2590                zend_string *str = zval_get_string(zvalue);
2591                /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2592                error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2593                error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, ZSTR_VAL(str));
2594                zend_string_release(str);
2595#else
2596                char *post = NULL;
2597                zend_string *str = zval_get_string(zvalue);
2598
2599                post = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
2600                zend_llist_add_element(&ch->to_free->str, &post);
2601
2602                curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
2603                error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2604                zend_string_release(str);
2605#endif
2606            }
2607            break;
2608
2609        case CURLOPT_PROGRESSFUNCTION:
2610            curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,  curl_progress);
2611            curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
2612            if (ch->handlers->progress == NULL) {
2613                ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2614            } else if (!Z_ISUNDEF(ch->handlers->progress->func_name)) {
2615                zval_ptr_dtor(&ch->handlers->progress->func_name);
2616                ch->handlers->progress->fci_cache = empty_fcall_info_cache;
2617            }
2618            ZVAL_COPY(&ch->handlers->progress->func_name, zvalue);
2619            ch->handlers->progress->method = PHP_CURL_USER;
2620            break;
2621
2622        case CURLOPT_READFUNCTION:
2623            if (!Z_ISUNDEF(ch->handlers->read->func_name)) {
2624                zval_ptr_dtor(&ch->handlers->read->func_name);
2625                ch->handlers->read->fci_cache = empty_fcall_info_cache;
2626            }
2627            ZVAL_COPY(&ch->handlers->read->func_name, zvalue);
2628            ch->handlers->read->method = PHP_CURL_USER;
2629            break;
2630
2631        case CURLOPT_RETURNTRANSFER:
2632            lval = zval_get_long(zvalue);
2633            if (lval) {
2634                ch->handlers->write->method = PHP_CURL_RETURN;
2635            } else {
2636                ch->handlers->write->method = PHP_CURL_STDOUT;
2637            }
2638            break;
2639
2640        case CURLOPT_WRITEFUNCTION:
2641            if (!Z_ISUNDEF(ch->handlers->write->func_name)) {
2642                zval_ptr_dtor(&ch->handlers->write->func_name);
2643                ch->handlers->write->fci_cache = empty_fcall_info_cache;
2644            }
2645            ZVAL_COPY(&ch->handlers->write->func_name, zvalue);
2646            ch->handlers->write->method = PHP_CURL_USER;
2647            break;
2648
2649#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2650        case CURLOPT_MAX_RECV_SPEED_LARGE:
2651        case CURLOPT_MAX_SEND_SPEED_LARGE:
2652            lval = zval_get_long(zvalue);
2653            error = curl_easy_setopt(ch->cp, option, (curl_off_t)lval);
2654            break;
2655#endif
2656
2657#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2658        case CURLOPT_POSTREDIR:
2659            lval = zval_get_long(zvalue);
2660            error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, lval & CURL_REDIR_POST_ALL);
2661            break;
2662#endif
2663
2664#if CURLOPT_PASSWDFUNCTION != 0
2665        case CURLOPT_PASSWDFUNCTION:
2666            zval_ptr_dtor(&ch->handlers->passwd);
2667            ZVAL_COPY(&ch->handlers->passwd, zvalue);
2668            error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
2669            error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
2670            break;
2671#endif
2672
2673        /* the following options deal with files, therefore the open_basedir check
2674         * is required.
2675         */
2676        case CURLOPT_COOKIEFILE:
2677        case CURLOPT_COOKIEJAR:
2678        case CURLOPT_RANDOM_FILE:
2679        case CURLOPT_SSLCERT:
2680#if LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2681        case CURLOPT_NETRC_FILE:
2682#endif
2683#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2684        case CURLOPT_SSH_PRIVATE_KEYFILE:
2685        case CURLOPT_SSH_PUBLIC_KEYFILE:
2686#endif
2687#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2688        case CURLOPT_CRLFILE:
2689        case CURLOPT_ISSUERCERT:
2690#endif
2691#if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
2692        case CURLOPT_SSH_KNOWNHOSTS:
2693#endif
2694        {
2695            zend_string *str = zval_get_string(zvalue);
2696            int ret;
2697
2698            if (ZSTR_LEN(str) && php_check_open_basedir(ZSTR_VAL(str))) {
2699                zend_string_release(str);
2700                return FAILURE;
2701            }
2702
2703            ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2704            zend_string_release(str);
2705            return ret;
2706        }
2707
2708        case CURLINFO_HEADER_OUT:
2709            lval = zval_get_long(zvalue);
2710            if (lval == 1) {
2711                curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2712                curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2713                curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2714            } else {
2715                curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2716                curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2717                curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2718            }
2719            break;
2720
2721        case CURLOPT_SHARE:
2722            {
2723                php_curlsh *sh;
2724                if ((sh = (php_curlsh *)zend_fetch_resource_ex(zvalue, le_curl_share_handle_name, le_curl_share_handle))) {
2725                    curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2726                }
2727            }
2728            break;
2729
2730#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2731        case CURLOPT_FNMATCH_FUNCTION:
2732            curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_FUNCTION, curl_fnmatch);
2733            curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, ch);
2734            if (ch->handlers->fnmatch == NULL) {
2735                ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2736            } else if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) {
2737                zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
2738                ch->handlers->fnmatch->fci_cache = empty_fcall_info_cache;
2739            }
2740            ZVAL_COPY(&ch->handlers->fnmatch->func_name, zvalue);
2741            ch->handlers->fnmatch->method = PHP_CURL_USER;
2742            break;
2743#endif
2744
2745    }
2746
2747    SAVE_CURL_ERROR(ch, error);
2748    if (error != CURLE_OK) {
2749        return FAILURE;
2750    } else {
2751        return SUCCESS;
2752    }
2753}
2754/* }}} */
2755
2756/* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
2757   Set an option for a cURL transfer */
2758PHP_FUNCTION(curl_setopt)
2759{
2760    zval       *zid, *zvalue;
2761    zend_long        options;
2762    php_curl   *ch;
2763
2764    if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &zid, &options, &zvalue) == FAILURE) {
2765        return;
2766    }
2767
2768    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2769        RETURN_FALSE;
2770    }
2771
2772    if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
2773        php_error_docref(NULL, E_WARNING, "Invalid curl configuration option");
2774        RETURN_FALSE;
2775    }
2776
2777    if (_php_curl_setopt(ch, options, zvalue) == SUCCESS) {
2778        RETURN_TRUE;
2779    } else {
2780        RETURN_FALSE;
2781    }
2782}
2783/* }}} */
2784
2785/* {{{ proto bool curl_setopt_array(resource ch, array options)
2786   Set an array of option for a cURL transfer */
2787PHP_FUNCTION(curl_setopt_array)
2788{
2789    zval        *zid, *arr, *entry;
2790    php_curl    *ch;
2791    zend_ulong  option;
2792    zend_string *string_key;
2793
2794    if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra", &zid, &arr) == FAILURE) {
2795        return;
2796    }
2797
2798    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2799        RETURN_FALSE;
2800    }
2801
2802    ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
2803        if (string_key) {
2804            php_error_docref(NULL, E_WARNING,
2805                    "Array keys must be CURLOPT constants or equivalent integer values");
2806            RETURN_FALSE;
2807        }
2808        if (_php_curl_setopt(ch, (zend_long) option, entry) == FAILURE) {
2809            RETURN_FALSE;
2810        }
2811    } ZEND_HASH_FOREACH_END();
2812
2813    RETURN_TRUE;
2814}
2815/* }}} */
2816
2817/* {{{ _php_curl_cleanup_handle(ch)
2818   Cleanup an execution phase */
2819void _php_curl_cleanup_handle(php_curl *ch)
2820{
2821    smart_str_free(&ch->handlers->write->buf);
2822    if (ch->header.str) {
2823        zend_string_release(ch->header.str);
2824        ch->header.str = NULL;
2825    }
2826
2827    memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
2828    ch->err.no = 0;
2829}
2830/* }}} */
2831
2832/* {{{ proto bool curl_exec(resource ch)
2833   Perform a cURL session */
2834PHP_FUNCTION(curl_exec)
2835{
2836    CURLcode    error;
2837    zval        *zid;
2838    php_curl    *ch;
2839
2840    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
2841        return;
2842    }
2843
2844    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2845        RETURN_FALSE;
2846    }
2847
2848    _php_curl_verify_handlers(ch, 1);
2849
2850    _php_curl_cleanup_handle(ch);
2851
2852    error = curl_easy_perform(ch->cp);
2853    SAVE_CURL_ERROR(ch, error);
2854    /* CURLE_PARTIAL_FILE is returned by HEAD requests */
2855    if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
2856        smart_str_free(&ch->handlers->write->buf);
2857        RETURN_FALSE;
2858    }
2859
2860    if (!Z_ISUNDEF(ch->handlers->std_err)) {
2861        php_stream  *stream;
2862        stream = (php_stream*)zend_fetch_resource2_ex(&ch->handlers->std_err, NULL, php_file_le_stream(), php_file_le_pstream());
2863        if (stream) {
2864            php_stream_flush(stream);
2865        }
2866    }
2867
2868    if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.s) {
2869        smart_str_0(&ch->handlers->write->buf);
2870        RETURN_STR_COPY(ch->handlers->write->buf.s);
2871    }
2872
2873    /* flush the file handle, so any remaining data is synched to disk */
2874    if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
2875        fflush(ch->handlers->write->fp);
2876    }
2877    if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
2878        fflush(ch->handlers->write_header->fp);
2879    }
2880
2881    if (ch->handlers->write->method == PHP_CURL_RETURN) {
2882        RETURN_EMPTY_STRING();
2883    } else {
2884        RETURN_TRUE;
2885    }
2886}
2887/* }}} */
2888
2889/* {{{ proto mixed curl_getinfo(resource ch [, int option])
2890   Get information regarding a specific transfer */
2891PHP_FUNCTION(curl_getinfo)
2892{
2893    zval        *zid;
2894    php_curl    *ch;
2895    zend_long   option = 0;
2896
2897    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zid, &option) == FAILURE) {
2898        return;
2899    }
2900
2901    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2902        RETURN_FALSE;
2903    }
2904
2905    if (ZEND_NUM_ARGS() < 2) {
2906        char *s_code;
2907        /* libcurl expects long datatype. So far no cases are known where
2908           it would be an issue. Using zend_long would truncate a 64-bit
2909           var on Win64, so the exact long datatype fits everywhere, as
2910           long as there's no 32-bit int overflow. */
2911        long l_code;
2912        double d_code;
2913#if LIBCURL_VERSION_NUM >  0x071301
2914        struct curl_certinfo *ci = NULL;
2915        zval listcode;
2916#endif
2917
2918        array_init(return_value);
2919
2920        if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
2921            CAAS("url", s_code);
2922        }
2923        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
2924            if (s_code != NULL) {
2925                CAAS("content_type", s_code);
2926            } else {
2927                zval retnull;
2928                ZVAL_NULL(&retnull);
2929                CAAZ("content_type", &retnull);
2930            }
2931        }
2932        if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
2933            CAAL("http_code", l_code);
2934        }
2935        if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
2936            CAAL("header_size", l_code);
2937        }
2938        if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
2939            CAAL("request_size", l_code);
2940        }
2941        if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
2942            CAAL("filetime", l_code);
2943        }
2944        if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
2945            CAAL("ssl_verify_result", l_code);
2946        }
2947        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
2948            CAAL("redirect_count", l_code);
2949        }
2950        if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
2951            CAAD("total_time", d_code);
2952        }
2953        if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
2954            CAAD("namelookup_time", d_code);
2955        }
2956        if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
2957            CAAD("connect_time", d_code);
2958        }
2959        if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
2960            CAAD("pretransfer_time", d_code);
2961        }
2962        if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
2963            CAAD("size_upload", d_code);
2964        }
2965        if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
2966            CAAD("size_download", d_code);
2967        }
2968        if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
2969            CAAD("speed_download", d_code);
2970        }
2971        if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
2972            CAAD("speed_upload", d_code);
2973        }
2974        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
2975            CAAD("download_content_length", d_code);
2976        }
2977        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
2978            CAAD("upload_content_length", d_code);
2979        }
2980        if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
2981            CAAD("starttransfer_time", d_code);
2982        }
2983        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
2984            CAAD("redirect_time", d_code);
2985        }
2986#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
2987        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
2988            CAAS("redirect_url", s_code);
2989        }
2990#endif
2991#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2992        if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
2993            CAAS("primary_ip", s_code);
2994        }
2995#endif
2996#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2997        if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2998            array_init(&listcode);
2999            create_certinfo(ci, &listcode);
3000            CAAZ("certinfo", &listcode);
3001        }
3002#endif
3003#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3004        if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
3005            CAAL("primary_port", l_code);
3006        }
3007        if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
3008            CAAS("local_ip", s_code);
3009        }
3010        if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
3011            CAAL("local_port", l_code);
3012        }
3013#endif
3014        if (ch->header.str) {
3015            CAASTR("request_header", zend_string_copy(ch->header.str));
3016        }
3017    } else {
3018        switch (option) {
3019            case CURLINFO_HEADER_OUT:
3020                if (ch->header.str) {
3021                    RETURN_STR_COPY(ch->header.str);
3022                } else {
3023                    RETURN_FALSE;
3024                }
3025#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3026            case CURLINFO_CERTINFO: {
3027                struct curl_certinfo *ci = NULL;
3028
3029                array_init(return_value);
3030
3031                if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3032                    create_certinfo(ci, return_value);
3033                } else {
3034                    RETURN_FALSE;
3035                }
3036                break;
3037            }
3038#endif
3039            default: {
3040                int type = CURLINFO_TYPEMASK & option;
3041                switch (type) {
3042                    case CURLINFO_STRING:
3043                    {
3044                        char *s_code = NULL;
3045
3046                        if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
3047                            RETURN_STRING(s_code);
3048                        } else {
3049                            RETURN_FALSE;
3050                        }
3051                        break;
3052                    }
3053                    case CURLINFO_LONG:
3054                    {
3055                        zend_long code = 0;
3056
3057                        if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3058                            RETURN_LONG(code);
3059                        } else {
3060                            RETURN_FALSE;
3061                        }
3062                        break;
3063                    }
3064                    case CURLINFO_DOUBLE:
3065                    {
3066                        double code = 0.0;
3067
3068                        if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3069                            RETURN_DOUBLE(code);
3070                        } else {
3071                            RETURN_FALSE;
3072                        }
3073                        break;
3074                    }
3075#if LIBCURL_VERSION_NUM >= 0x070c03 /* Available since 7.12.3 */
3076                    case CURLINFO_SLIST:
3077                    {
3078                        struct curl_slist *slist;
3079                        array_init(return_value);
3080                        if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
3081                            while (slist) {
3082                                add_next_index_string(return_value, slist->data);
3083                                slist = slist->next;
3084                            }
3085                            curl_slist_free_all(slist);
3086                        } else {
3087                            RETURN_FALSE;
3088                        }
3089                        break;
3090                    }
3091#endif
3092                    default:
3093                        RETURN_FALSE;
3094                }
3095            }
3096        }
3097    }
3098}
3099/* }}} */
3100
3101/* {{{ proto string curl_error(resource ch)
3102   Return a string contain the last error for the current session */
3103PHP_FUNCTION(curl_error)
3104{
3105    zval        *zid;
3106    php_curl    *ch;
3107
3108    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3109        return;
3110    }
3111
3112    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3113        RETURN_FALSE;
3114    }
3115
3116    ch->err.str[CURL_ERROR_SIZE] = 0;
3117    RETURN_STRING(ch->err.str);
3118}
3119/* }}} */
3120
3121/* {{{ proto int curl_errno(resource ch)
3122   Return an integer containing the last error number */
3123PHP_FUNCTION(curl_errno)
3124{
3125    zval        *zid;
3126    php_curl    *ch;
3127
3128    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3129        return;
3130    }
3131
3132    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3133        RETURN_FALSE;
3134    }
3135
3136    RETURN_LONG(ch->err.no);
3137}
3138/* }}} */
3139
3140/* {{{ proto void curl_close(resource ch)
3141   Close a cURL session */
3142PHP_FUNCTION(curl_close)
3143{
3144    zval        *zid;
3145    php_curl    *ch;
3146
3147    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3148        return;
3149    }
3150
3151    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3152        RETURN_FALSE;
3153    }
3154
3155    if (ch->in_callback) {
3156        php_error_docref(NULL, E_WARNING, "Attempt to close cURL handle from a callback");
3157        return;
3158    }
3159
3160    if (Z_REFCOUNT_P(zid) <= 2) {
3161        zend_list_close(Z_RES_P(zid));
3162    }
3163}
3164/* }}} */
3165
3166/* {{{ _php_curl_close_ex()
3167   List destructor for curl handles */
3168static void _php_curl_close_ex(php_curl *ch)
3169{
3170#if PHP_CURL_DEBUG
3171    fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
3172#endif
3173
3174    _php_curl_verify_handlers(ch, 0);
3175
3176    /*
3177     * Libcurl is doing connection caching. When easy handle is cleaned up,
3178     * if the handle was previously used by the curl_multi_api, the connection
3179     * remains open un the curl multi handle is cleaned up. Some protocols are
3180     * sending content like the FTP one, and libcurl try to use the
3181     * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
3182     * callback are freed, we need to use an other callback to which avoid
3183     * segfaults.
3184     *
3185     * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
3186     */
3187    curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
3188    curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
3189
3190    curl_easy_cleanup(ch->cp);
3191
3192    /* cURL destructors should be invoked only by last curl handle */
3193    if (--ch->clone == 0) {
3194        zend_llist_clean(&ch->to_free->str);
3195        zend_llist_clean(&ch->to_free->post);
3196        zend_hash_destroy(ch->to_free->slist);
3197        efree(ch->to_free->slist);
3198        efree(ch->to_free);
3199    }
3200
3201    smart_str_free(&ch->handlers->write->buf);
3202    zval_ptr_dtor(&ch->handlers->write->func_name);
3203    zval_ptr_dtor(&ch->handlers->read->func_name);
3204    zval_ptr_dtor(&ch->handlers->write_header->func_name);
3205#if CURLOPT_PASSWDFUNCTION != 0
3206    zval_ptr_dtor(&ch->handlers->passwd);
3207#endif
3208    zval_ptr_dtor(&ch->handlers->std_err);
3209    if (ch->header.str) {
3210        zend_string_release(ch->header.str);
3211    }
3212
3213    zval_ptr_dtor(&ch->handlers->write_header->stream);
3214    zval_ptr_dtor(&ch->handlers->write->stream);
3215    zval_ptr_dtor(&ch->handlers->read->stream);
3216
3217    efree(ch->handlers->write);
3218    efree(ch->handlers->write_header);
3219    efree(ch->handlers->read);
3220
3221    if (ch->handlers->progress) {
3222        zval_ptr_dtor(&ch->handlers->progress->func_name);
3223        efree(ch->handlers->progress);
3224    }
3225
3226#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3227    if (ch->handlers->fnmatch) {
3228        zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3229        efree(ch->handlers->fnmatch);
3230    }
3231#endif
3232
3233    efree(ch->handlers);
3234    efree(ch);
3235}
3236/* }}} */
3237
3238/* {{{ _php_curl_close()
3239   List destructor for curl handles */
3240static void _php_curl_close(zend_resource *rsrc)
3241{
3242    php_curl *ch = (php_curl *) rsrc->ptr;
3243    _php_curl_close_ex(ch);
3244}
3245/* }}} */
3246
3247#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
3248/* {{{ proto bool curl_strerror(int code)
3249      return string describing error code */
3250PHP_FUNCTION(curl_strerror)
3251{
3252    zend_long code;
3253    const char *str;
3254
3255    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
3256        return;
3257    }
3258
3259    str = curl_easy_strerror(code);
3260    if (str) {
3261        RETURN_STRING(str);
3262    } else {
3263        RETURN_NULL();
3264    }
3265}
3266/* }}} */
3267#endif
3268
3269#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
3270/* {{{ _php_curl_reset_handlers()
3271   Reset all handlers of a given php_curl */
3272static void _php_curl_reset_handlers(php_curl *ch)
3273{
3274    if (!Z_ISUNDEF(ch->handlers->write->stream)) {
3275        zval_ptr_dtor(&ch->handlers->write->stream);
3276        ZVAL_UNDEF(&ch->handlers->write->stream);
3277    }
3278    ch->handlers->write->fp = NULL;
3279    ch->handlers->write->method = PHP_CURL_STDOUT;
3280
3281    if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
3282        zval_ptr_dtor(&ch->handlers->write_header->stream);
3283        ZVAL_UNDEF(&ch->handlers->write_header->stream);
3284    }
3285    ch->handlers->write_header->fp = NULL;
3286    ch->handlers->write_header->method = PHP_CURL_IGNORE;
3287
3288    if (!Z_ISUNDEF(ch->handlers->read->stream)) {
3289        zval_ptr_dtor(&ch->handlers->read->stream);
3290        ZVAL_UNDEF(&ch->handlers->read->stream);
3291    }
3292    ch->handlers->read->fp = NULL;
3293    ch->handlers->read->res = NULL;
3294    ch->handlers->read->method  = PHP_CURL_DIRECT;
3295
3296    if (!Z_ISUNDEF(ch->handlers->std_err)) {
3297        zval_ptr_dtor(&ch->handlers->std_err);
3298        ZVAL_UNDEF(&ch->handlers->std_err);
3299    }
3300
3301    if (ch->handlers->progress) {
3302        zval_ptr_dtor(&ch->handlers->progress->func_name);
3303        efree(ch->handlers->progress);
3304        ch->handlers->progress = NULL;
3305    }
3306
3307#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3308    if (ch->handlers->fnmatch) {
3309        zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3310        efree(ch->handlers->fnmatch);
3311        ch->handlers->fnmatch = NULL;
3312    }
3313#endif
3314
3315}
3316/* }}} */
3317
3318/* {{{ proto void curl_reset(resource ch)
3319   Reset all options of a libcurl session handle */
3320PHP_FUNCTION(curl_reset)
3321{
3322    zval       *zid;
3323    php_curl   *ch;
3324
3325    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
3326        return;
3327    }
3328
3329    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3330        RETURN_FALSE;
3331    }
3332
3333    if (ch->in_callback) {
3334        php_error_docref(NULL, E_WARNING, "Attempt to reset cURL handle from a callback");
3335        return;
3336    }
3337
3338    curl_easy_reset(ch->cp);
3339    _php_curl_reset_handlers(ch);
3340    _php_curl_set_default_options(ch);
3341}
3342/* }}} */
3343#endif
3344
3345#if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
3346/* {{{ proto void curl_escape(resource ch, string str)
3347   URL encodes the given string */
3348PHP_FUNCTION(curl_escape)
3349{
3350    char       *str = NULL, *res = NULL;
3351    size_t        str_len = 0;
3352    zval       *zid;
3353    php_curl   *ch;
3354
3355    if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zid, &str, &str_len) == FAILURE) {
3356        return;
3357    }
3358
3359    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3360        RETURN_FALSE;
3361    }
3362
3363    if ((res = curl_easy_escape(ch->cp, str, str_len))) {
3364        RETVAL_STRING(res);
3365        curl_free(res);
3366    } else {
3367        RETURN_FALSE;
3368    }
3369}
3370/* }}} */
3371
3372/* {{{ proto void curl_unescape(resource ch, string str)
3373   URL decodes the given string */
3374PHP_FUNCTION(curl_unescape)
3375{
3376    char       *str = NULL, *out = NULL;
3377    size_t     str_len = 0;
3378    int        out_len;
3379    zval       *zid;
3380    php_curl   *ch;
3381
3382    if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zid, &str, &str_len) == FAILURE) {
3383        return;
3384    }
3385
3386    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3387        RETURN_FALSE;
3388    }
3389
3390    if ((out = curl_easy_unescape(ch->cp, str, str_len, &out_len))) {
3391        RETVAL_STRINGL(out, out_len);
3392        curl_free(out);
3393    } else {
3394        RETURN_FALSE;
3395    }
3396}
3397/* }}} */
3398#endif
3399
3400#if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
3401/* {{{ proto void curl_pause(resource ch, int bitmask)
3402       pause and unpause a connection */
3403PHP_FUNCTION(curl_pause)
3404{
3405    zend_long       bitmask;
3406    zval       *zid;
3407    php_curl   *ch;
3408
3409    if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zid, &bitmask) == FAILURE) {
3410        return;
3411    }
3412
3413    if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3414        RETURN_FALSE;
3415    }
3416
3417    RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3418}
3419/* }}} */
3420#endif
3421
3422#endif /* HAVE_CURL */
3423
3424/*
3425 * Local variables:
3426 * tab-width: 4
3427 * c-basic-offset: 4
3428 * End:
3429 * vim600: fdm=marker
3430 * vim: noet sw=4 ts=4
3431 */
3432