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