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