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