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 = NULL;
2349
2350            if (Z_TYPE_PP(zvalue) != IS_NULL) {
2351                what = zend_fetch_resource(zvalue TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream(), php_file_le_pstream());
2352                if (!what) {
2353                    return FAILURE;
2354                }
2355
2356                if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2357                    return FAILURE;
2358                }
2359
2360                if (!fp) {
2361                    return FAILURE;
2362                }
2363            }
2364
2365            error = CURLE_OK;
2366            switch (option) {
2367                case CURLOPT_FILE:
2368                    if (!what) {
2369                        if (ch->handlers->write->stream) {
2370                            Z_DELREF_P(ch->handlers->write->stream);
2371                            ch->handlers->write->stream = NULL;
2372                        }
2373                        ch->handlers->write->fp = NULL;
2374                        ch->handlers->write->method = PHP_CURL_STDOUT;
2375                    } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
2376                        if (ch->handlers->write->stream) {
2377                            Z_DELREF_P(ch->handlers->write->stream);
2378                        }
2379                        Z_ADDREF_PP(zvalue);
2380                        ch->handlers->write->fp = fp;
2381                        ch->handlers->write->method = PHP_CURL_FILE;
2382                        ch->handlers->write->stream = *zvalue;
2383                    } else {
2384                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
2385                        return FAILURE;
2386                    }
2387                    break;
2388                case CURLOPT_WRITEHEADER:
2389                    if (!what) {
2390                        if (ch->handlers->write_header->stream) {
2391                            Z_DELREF_P(ch->handlers->write_header->stream);
2392                            ch->handlers->write_header->stream = NULL;
2393                        }
2394                        ch->handlers->write_header->fp = NULL;
2395                        ch->handlers->write_header->method = PHP_CURL_IGNORE;
2396                    } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
2397                        if (ch->handlers->write_header->stream) {
2398                            Z_DELREF_P(ch->handlers->write_header->stream);
2399                        }
2400                        Z_ADDREF_PP(zvalue);
2401                        ch->handlers->write_header->fp = fp;
2402                        ch->handlers->write_header->method = PHP_CURL_FILE;
2403                        ch->handlers->write_header->stream = *zvalue;
2404                    } else {
2405                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
2406                        return FAILURE;
2407                    }
2408                    break;
2409                case CURLOPT_INFILE:
2410                    if (!what) {
2411                        if (ch->handlers->read->stream) {
2412                            Z_DELREF_P(ch->handlers->read->stream);
2413                            ch->handlers->read->stream = NULL;
2414                        }
2415                        ch->handlers->read->fp = NULL;
2416                        ch->handlers->read->fd = 0;
2417                    } else {
2418                        if (ch->handlers->read->stream) {
2419                            Z_DELREF_P(ch->handlers->read->stream);
2420                        }
2421                        Z_ADDREF_PP(zvalue);
2422                        ch->handlers->read->fp = fp;
2423                        ch->handlers->read->fd = Z_LVAL_PP(zvalue);
2424                        ch->handlers->read->stream = *zvalue;
2425                    }
2426                    break;
2427                case CURLOPT_STDERR:
2428                    if (!what) {
2429                        if (ch->handlers->std_err) {
2430                            zval_ptr_dtor(&ch->handlers->std_err);
2431                            ch->handlers->std_err = NULL;
2432                        }
2433                    } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
2434                        if (ch->handlers->std_err) {
2435                            zval_ptr_dtor(&ch->handlers->std_err);
2436                        }
2437                        zval_add_ref(zvalue);
2438                        ch->handlers->std_err = *zvalue;
2439                    } else {
2440                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
2441                        return FAILURE;
2442                    }
2443                    /* break omitted intentionally */
2444                default:
2445                    error = curl_easy_setopt(ch->cp, option, fp);
2446                    break;
2447            }
2448            break;
2449        }
2450
2451        /* Curl linked list options */
2452        case CURLOPT_HTTP200ALIASES:
2453        case CURLOPT_HTTPHEADER:
2454        case CURLOPT_POSTQUOTE:
2455        case CURLOPT_PREQUOTE:
2456        case CURLOPT_QUOTE:
2457        case CURLOPT_TELNETOPTIONS:
2458#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2459        case CURLOPT_MAIL_RCPT:
2460#endif
2461#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2462        case CURLOPT_RESOLVE:
2463#endif
2464        {
2465            zval              **current;
2466            HashTable          *ph;
2467            struct curl_slist  *slist = NULL;
2468
2469            ph = HASH_OF(*zvalue);
2470            if (!ph) {
2471                char *name = NULL;
2472                switch (option) {
2473                    case CURLOPT_HTTPHEADER:
2474                        name = "CURLOPT_HTTPHEADER";
2475                        break;
2476                    case CURLOPT_QUOTE:
2477                        name = "CURLOPT_QUOTE";
2478                        break;
2479                    case CURLOPT_HTTP200ALIASES:
2480                        name = "CURLOPT_HTTP200ALIASES";
2481                        break;
2482                    case CURLOPT_POSTQUOTE:
2483                        name = "CURLOPT_POSTQUOTE";
2484                        break;
2485                    case CURLOPT_PREQUOTE:
2486                        name = "CURLOPT_PREQUOTE";
2487                        break;
2488                    case CURLOPT_TELNETOPTIONS:
2489                        name = "CURLOPT_TELNETOPTIONS";
2490                        break;
2491#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2492                    case CURLOPT_MAIL_RCPT:
2493                        name = "CURLOPT_MAIL_RCPT";
2494                        break;
2495#endif
2496#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2497                    case CURLOPT_RESOLVE:
2498                        name = "CURLOPT_RESOLVE";
2499                        break;
2500#endif
2501                }
2502                php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must pass either an object or an array with the %s argument", name);
2503                return FAILURE;
2504            }
2505
2506            for (zend_hash_internal_pointer_reset(ph);
2507                 zend_hash_get_current_data(ph, (void **) &current) == SUCCESS;
2508                 zend_hash_move_forward(ph)
2509            ) {
2510                SEPARATE_ZVAL(current);
2511                convert_to_string_ex(current);
2512
2513                slist = curl_slist_append(slist, Z_STRVAL_PP(current));
2514                if (!slist) {
2515                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist");
2516                    return 1;
2517                }
2518            }
2519            zend_hash_index_update(ch->to_free->slist, (ulong) option, &slist, sizeof(struct curl_slist *), NULL);
2520
2521            error = curl_easy_setopt(ch->cp, option, slist);
2522
2523            break;
2524        }
2525
2526        case CURLOPT_BINARYTRANSFER:
2527            /* Do nothing, just backward compatibility */
2528            break;
2529
2530        case CURLOPT_FOLLOWLOCATION:
2531            convert_to_long_ex(zvalue);
2532#if LIBCURL_VERSION_NUM < 0x071304
2533            if (PG(open_basedir) && *PG(open_basedir)) {
2534                if (Z_LVAL_PP(zvalue) != 0) {
2535                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set");
2536                    return FAILURE;
2537                }
2538            }
2539#endif
2540            error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
2541            break;
2542
2543        case CURLOPT_HEADERFUNCTION:
2544            if (ch->handlers->write_header->func_name) {
2545                zval_ptr_dtor(&ch->handlers->write_header->func_name);
2546                ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
2547            }
2548            zval_add_ref(zvalue);
2549            ch->handlers->write_header->func_name = *zvalue;
2550            ch->handlers->write_header->method = PHP_CURL_USER;
2551            break;
2552
2553        case CURLOPT_POSTFIELDS:
2554            if (Z_TYPE_PP(zvalue) == IS_ARRAY || Z_TYPE_PP(zvalue) == IS_OBJECT) {
2555                zval            **current;
2556                HashTable        *postfields;
2557                struct HttpPost  *first = NULL;
2558                struct HttpPost  *last  = NULL;
2559
2560                postfields = HASH_OF(*zvalue);
2561                if (!postfields) {
2562                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2563                    return FAILURE;
2564                }
2565
2566                for (zend_hash_internal_pointer_reset(postfields);
2567                     zend_hash_get_current_data(postfields, (void **) &current) == SUCCESS;
2568                     zend_hash_move_forward(postfields)
2569                ) {
2570                    char  *postval;
2571                    char  *string_key = NULL;
2572                    uint   string_key_len;
2573                    ulong  num_key;
2574                    int    numeric_key;
2575
2576                    zend_hash_get_current_key_ex(postfields, &string_key, &string_key_len, &num_key, 0, NULL);
2577
2578                    /* Pretend we have a string_key here */
2579                    if(!string_key) {
2580                        spprintf(&string_key, 0, "%ld", num_key);
2581                        string_key_len = strlen(string_key)+1;
2582                        numeric_key = 1;
2583                    } else {
2584                        numeric_key = 0;
2585                    }
2586
2587                    if(Z_TYPE_PP(current) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(current), curl_CURLFile_class TSRMLS_CC)) {
2588                        /* new-style file upload */
2589                        zval *prop;
2590                        char *type = NULL, *filename = NULL;
2591
2592                        prop = zend_read_property(curl_CURLFile_class, *current, "name", sizeof("name")-1, 0 TSRMLS_CC);
2593                        if(Z_TYPE_P(prop) != IS_STRING) {
2594                            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filename for key %s", string_key);
2595                        } else {
2596                            postval = Z_STRVAL_P(prop);
2597
2598                            if (php_check_open_basedir(postval TSRMLS_CC)) {
2599                                return 1;
2600                            }
2601
2602                            prop = zend_read_property(curl_CURLFile_class, *current, "mime", sizeof("mime")-1, 0 TSRMLS_CC);
2603                            if(Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2604                                type = Z_STRVAL_P(prop);
2605                            }
2606                            prop = zend_read_property(curl_CURLFile_class, *current, "postname", sizeof("postname")-1, 0 TSRMLS_CC);
2607                            if(Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2608                                filename = Z_STRVAL_P(prop);
2609                            }
2610                            error = curl_formadd(&first, &last,
2611                                            CURLFORM_COPYNAME, string_key,
2612                                            CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2613                                            CURLFORM_FILENAME, filename ? filename : postval,
2614                                            CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2615                                            CURLFORM_FILE, postval,
2616                                            CURLFORM_END);
2617                        }
2618
2619                        if (numeric_key) {
2620                            efree(string_key);
2621                        }
2622                        continue;
2623                    }
2624
2625                    SEPARATE_ZVAL(current);
2626                    convert_to_string_ex(current);
2627
2628                    postval = Z_STRVAL_PP(current);
2629
2630                    /* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2631                     * must be explicitly cast to long in curl_formadd
2632                     * use since curl needs a long not an int. */
2633                    if (!ch->safe_upload && *postval == '@') {
2634                        char *type, *filename;
2635                        ++postval;
2636
2637                        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");
2638
2639                        if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, postval + Z_STRLEN_PP(current)))) {
2640                            *type = '\0';
2641                        }
2642                        if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, postval + Z_STRLEN_PP(current)))) {
2643                            *filename = '\0';
2644                        }
2645                        /* open_basedir check */
2646                        if (php_check_open_basedir(postval TSRMLS_CC)) {
2647                            return FAILURE;
2648                        }
2649                        error = curl_formadd(&first, &last,
2650                                        CURLFORM_COPYNAME, string_key,
2651                                        CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2652                                        CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval,
2653                                        CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream",
2654                                        CURLFORM_FILE, postval,
2655                                        CURLFORM_END);
2656                        if (type) {
2657                            *type = ';';
2658                        }
2659                        if (filename) {
2660                            *filename = ';';
2661                        }
2662                    } else {
2663                        error = curl_formadd(&first, &last,
2664                                             CURLFORM_COPYNAME, string_key,
2665                                             CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2666                                             CURLFORM_COPYCONTENTS, postval,
2667                                             CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current),
2668                                             CURLFORM_END);
2669                    }
2670
2671                    if (numeric_key) {
2672                        efree(string_key);
2673                    }
2674                }
2675
2676                SAVE_CURL_ERROR(ch, error);
2677                if (error != CURLE_OK) {
2678                    return FAILURE;
2679                }
2680
2681                if (Z_REFCOUNT_P(ch->clone) <= 1) {
2682                    zend_llist_clean(&ch->to_free->post);
2683                }
2684                zend_llist_add_element(&ch->to_free->post, &first);
2685                error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2686
2687            } else {
2688#if LIBCURL_VERSION_NUM >= 0x071101
2689                convert_to_string_ex(zvalue);
2690                /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2691                error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
2692                error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue));
2693#else
2694                char *post = NULL;
2695
2696                convert_to_string_ex(zvalue);
2697                post = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
2698                zend_llist_add_element(&ch->to_free->str, &post);
2699
2700                curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
2701                error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
2702#endif
2703            }
2704            break;
2705
2706        case CURLOPT_PROGRESSFUNCTION:
2707            curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,  curl_progress);
2708            curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
2709            if (ch->handlers->progress == NULL) {
2710                ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2711            } else if (ch->handlers->progress->func_name) {
2712                zval_ptr_dtor(&ch->handlers->progress->func_name);
2713                ch->handlers->progress->fci_cache = empty_fcall_info_cache;
2714            }
2715            zval_add_ref(zvalue);
2716            ch->handlers->progress->func_name = *zvalue;
2717            ch->handlers->progress->method = PHP_CURL_USER;
2718            break;
2719
2720        case CURLOPT_READFUNCTION:
2721            if (ch->handlers->read->func_name) {
2722                zval_ptr_dtor(&ch->handlers->read->func_name);
2723                ch->handlers->read->fci_cache = empty_fcall_info_cache;
2724            }
2725            zval_add_ref(zvalue);
2726            ch->handlers->read->func_name = *zvalue;
2727            ch->handlers->read->method = PHP_CURL_USER;
2728            break;
2729
2730        case CURLOPT_RETURNTRANSFER:
2731            convert_to_long_ex(zvalue);
2732            if (Z_LVAL_PP(zvalue)) {
2733                ch->handlers->write->method = PHP_CURL_RETURN;
2734            } else {
2735                ch->handlers->write->method = PHP_CURL_STDOUT;
2736            }
2737            break;
2738
2739        case CURLOPT_WRITEFUNCTION:
2740            if (ch->handlers->write->func_name) {
2741                zval_ptr_dtor(&ch->handlers->write->func_name);
2742                ch->handlers->write->fci_cache = empty_fcall_info_cache;
2743            }
2744            zval_add_ref(zvalue);
2745            ch->handlers->write->func_name = *zvalue;
2746            ch->handlers->write->method = PHP_CURL_USER;
2747            break;
2748
2749#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2750        case CURLOPT_MAX_RECV_SPEED_LARGE:
2751        case CURLOPT_MAX_SEND_SPEED_LARGE:
2752            convert_to_long_ex(zvalue);
2753            error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_PP(zvalue));
2754            break;
2755#endif
2756
2757#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2758        case CURLOPT_POSTREDIR:
2759            convert_to_long_ex(zvalue);
2760            error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, Z_LVAL_PP(zvalue) & CURL_REDIR_POST_ALL);
2761            break;
2762#endif
2763
2764#if CURLOPT_PASSWDFUNCTION != 0
2765        case CURLOPT_PASSWDFUNCTION:
2766            if (ch->handlers->passwd) {
2767                zval_ptr_dtor(&ch->handlers->passwd);
2768            }
2769            zval_add_ref(zvalue);
2770            ch->handlers->passwd = *zvalue;
2771            error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
2772            error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
2773            break;
2774#endif
2775
2776        /* the following options deal with files, therefore the open_basedir check
2777         * is required.
2778         */
2779        case CURLOPT_COOKIEFILE:
2780        case CURLOPT_COOKIEJAR:
2781        case CURLOPT_RANDOM_FILE:
2782        case CURLOPT_SSLCERT:
2783#if LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2784        case CURLOPT_NETRC_FILE:
2785#endif
2786#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2787        case CURLOPT_SSH_PRIVATE_KEYFILE:
2788        case CURLOPT_SSH_PUBLIC_KEYFILE:
2789#endif
2790#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2791        case CURLOPT_CRLFILE:
2792        case CURLOPT_ISSUERCERT:
2793#endif
2794#if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
2795        case CURLOPT_SSH_KNOWNHOSTS:
2796#endif
2797        {
2798            convert_to_string_ex(zvalue);
2799
2800            if (Z_STRLEN_PP(zvalue) && php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) {
2801                return FAILURE;
2802            }
2803
2804            return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC);
2805        }
2806
2807        case CURLINFO_HEADER_OUT:
2808            convert_to_long_ex(zvalue);
2809            if (Z_LVAL_PP(zvalue) == 1) {
2810                curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2811                curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2812                curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2813            } else {
2814                curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2815                curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2816                curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2817            }
2818            break;
2819
2820        case CURLOPT_SHARE:
2821            {
2822                php_curlsh *sh = NULL;
2823                ZEND_FETCH_RESOURCE_NO_RETURN(sh, php_curlsh *, zvalue, -1, le_curl_share_handle_name, le_curl_share_handle);
2824                if (sh) {
2825                    curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2826                }
2827            }
2828
2829#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2830        case CURLOPT_FNMATCH_FUNCTION:
2831            curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_FUNCTION, curl_fnmatch);
2832            curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, ch);
2833            if (ch->handlers->fnmatch == NULL) {
2834                ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2835            } else if (ch->handlers->fnmatch->func_name) {
2836                zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
2837                ch->handlers->fnmatch->fci_cache = empty_fcall_info_cache;
2838            }
2839            zval_add_ref(zvalue);
2840            ch->handlers->fnmatch->func_name = *zvalue;
2841            ch->handlers->fnmatch->method = PHP_CURL_USER;
2842            break;
2843#endif
2844
2845    }
2846
2847    SAVE_CURL_ERROR(ch, error);
2848    if (error != CURLE_OK) {
2849        return FAILURE;
2850    } else {
2851        return SUCCESS;
2852    }
2853}
2854/* }}} */
2855
2856/* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
2857   Set an option for a cURL transfer */
2858PHP_FUNCTION(curl_setopt)
2859{
2860    zval       *zid, **zvalue;
2861    long        options;
2862    php_curl   *ch;
2863
2864    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) {
2865        return;
2866    }
2867
2868    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2869
2870    if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
2871        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl configuration option");
2872        RETURN_FALSE;
2873    }
2874
2875    if (_php_curl_setopt(ch, options, zvalue TSRMLS_CC) == SUCCESS) {
2876        RETURN_TRUE;
2877    } else {
2878        RETURN_FALSE;
2879    }
2880}
2881/* }}} */
2882
2883/* {{{ proto bool curl_setopt_array(resource ch, array options)
2884   Set an array of option for a cURL transfer */
2885PHP_FUNCTION(curl_setopt_array)
2886{
2887    zval        *zid, *arr, **entry;
2888    php_curl    *ch;
2889    ulong       option;
2890    HashPosition    pos;
2891    char        *string_key;
2892    uint        str_key_len;
2893
2894    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) {
2895        return;
2896    }
2897
2898    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2899
2900    zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
2901    while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
2902        if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) != HASH_KEY_IS_LONG) {
2903            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent integer values");
2904            RETURN_FALSE;
2905        }
2906        if (_php_curl_setopt(ch, (long) option, entry TSRMLS_CC) == FAILURE) {
2907            RETURN_FALSE;
2908        }
2909        zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
2910    }
2911    RETURN_TRUE;
2912}
2913/* }}} */
2914
2915/* {{{ _php_curl_cleanup_handle(ch)
2916   Cleanup an execution phase */
2917void _php_curl_cleanup_handle(php_curl *ch)
2918{
2919    if (ch->handlers->write->buf.len > 0) {
2920        smart_str_free(&ch->handlers->write->buf);
2921    }
2922    if (ch->header.str_len) {
2923        efree(ch->header.str);
2924        ch->header.str_len = 0;
2925    }
2926
2927    memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
2928    ch->err.no = 0;
2929}
2930/* }}} */
2931
2932/* {{{ proto bool curl_exec(resource ch)
2933   Perform a cURL session */
2934PHP_FUNCTION(curl_exec)
2935{
2936    CURLcode    error;
2937    zval        *zid;
2938    php_curl    *ch;
2939
2940    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
2941        return;
2942    }
2943
2944    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2945
2946    _php_curl_verify_handlers(ch, 1 TSRMLS_CC);
2947
2948    _php_curl_cleanup_handle(ch);
2949
2950    error = curl_easy_perform(ch->cp);
2951    SAVE_CURL_ERROR(ch, error);
2952    /* CURLE_PARTIAL_FILE is returned by HEAD requests */
2953    if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
2954        if (ch->handlers->write->buf.len > 0) {
2955            smart_str_free(&ch->handlers->write->buf);
2956        }
2957        RETURN_FALSE;
2958    }
2959
2960    if (ch->handlers->std_err) {
2961        php_stream  *stream;
2962        stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
2963        if (stream) {
2964            php_stream_flush(stream);
2965        }
2966    }
2967
2968    if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
2969        smart_str_0(&ch->handlers->write->buf);
2970        RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
2971    }
2972
2973    /* flush the file handle, so any remaining data is synched to disk */
2974    if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
2975        fflush(ch->handlers->write->fp);
2976    }
2977    if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
2978        fflush(ch->handlers->write_header->fp);
2979    }
2980
2981    if (ch->handlers->write->method == PHP_CURL_RETURN) {
2982        RETURN_EMPTY_STRING();
2983    } else {
2984        RETURN_TRUE;
2985    }
2986}
2987/* }}} */
2988
2989/* {{{ proto mixed curl_getinfo(resource ch [, int option])
2990   Get information regarding a specific transfer */
2991PHP_FUNCTION(curl_getinfo)
2992{
2993    zval        *zid;
2994    php_curl    *ch;
2995    long        option = 0;
2996
2997    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zid, &option) == FAILURE) {
2998        return;
2999    }
3000
3001    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3002
3003    if (ZEND_NUM_ARGS() < 2) {
3004        char   *s_code;
3005        long    l_code;
3006        double  d_code;
3007#if LIBCURL_VERSION_NUM >  0x071301
3008        struct curl_certinfo *ci = NULL;
3009        zval *listcode;
3010#endif
3011
3012        array_init(return_value);
3013
3014        if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
3015            CAAS("url", s_code);
3016        }
3017        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
3018            if (s_code != NULL) {
3019                CAAS("content_type", s_code);
3020            } else {
3021                zval *retnull;
3022                MAKE_STD_ZVAL(retnull);
3023                ZVAL_NULL(retnull);
3024                CAAZ("content_type", retnull);
3025            }
3026        }
3027        if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
3028            CAAL("http_code", l_code);
3029        }
3030        if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
3031            CAAL("header_size", l_code);
3032        }
3033        if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
3034            CAAL("request_size", l_code);
3035        }
3036        if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
3037            CAAL("filetime", l_code);
3038        }
3039        if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
3040            CAAL("ssl_verify_result", l_code);
3041        }
3042        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
3043            CAAL("redirect_count", l_code);
3044        }
3045        if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
3046            CAAD("total_time", d_code);
3047        }
3048        if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
3049            CAAD("namelookup_time", d_code);
3050        }
3051        if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
3052            CAAD("connect_time", d_code);
3053        }
3054        if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
3055            CAAD("pretransfer_time", d_code);
3056        }
3057        if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
3058            CAAD("size_upload", d_code);
3059        }
3060        if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
3061            CAAD("size_download", d_code);
3062        }
3063        if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
3064            CAAD("speed_download", d_code);
3065        }
3066        if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
3067            CAAD("speed_upload", d_code);
3068        }
3069        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
3070            CAAD("download_content_length", d_code);
3071        }
3072        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
3073            CAAD("upload_content_length", d_code);
3074        }
3075        if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
3076            CAAD("starttransfer_time", d_code);
3077        }
3078        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
3079            CAAD("redirect_time", d_code);
3080        }
3081#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
3082        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
3083            CAAS("redirect_url", s_code);
3084        }
3085#endif
3086#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
3087        if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
3088            CAAS("primary_ip", s_code);
3089        }
3090#endif
3091#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3092        if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3093            MAKE_STD_ZVAL(listcode);
3094            array_init(listcode);
3095            create_certinfo(ci, listcode TSRMLS_CC);
3096            CAAZ("certinfo", listcode);
3097        }
3098#endif
3099#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3100        if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
3101            CAAL("primary_port", l_code);
3102        }
3103        if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
3104            CAAS("local_ip", s_code);
3105        }
3106        if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
3107            CAAL("local_port", l_code);
3108        }
3109#endif
3110        if (ch->header.str_len > 0) {
3111            CAAS("request_header", ch->header.str);
3112        }
3113    } else {
3114        switch (option) {
3115            case CURLINFO_HEADER_OUT:
3116                if (ch->header.str_len > 0) {
3117                    RETURN_STRINGL(ch->header.str, ch->header.str_len, 1);
3118                } else {
3119                    RETURN_FALSE;
3120                }
3121#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3122            case CURLINFO_CERTINFO: {
3123                struct curl_certinfo *ci = NULL;
3124
3125                array_init(return_value);
3126
3127                if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3128                    create_certinfo(ci, return_value TSRMLS_CC);
3129                } else {
3130                    RETURN_FALSE;
3131                }
3132                break;
3133            }
3134#endif
3135            default: {
3136                int type = CURLINFO_TYPEMASK & option;
3137                switch (type) {
3138                    case CURLINFO_STRING:
3139                    {
3140                        char *s_code = NULL;
3141
3142                        if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
3143                            RETURN_STRING(s_code, 1);
3144                        } else {
3145                            RETURN_FALSE;
3146                        }
3147                        break;
3148                    }
3149                    case CURLINFO_LONG:
3150                    {
3151                        long code = 0;
3152
3153                        if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3154                            RETURN_LONG(code);
3155                        } else {
3156                            RETURN_FALSE;
3157                        }
3158                        break;
3159                    }
3160                    case CURLINFO_DOUBLE:
3161                    {
3162                        double code = 0.0;
3163
3164                        if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3165                            RETURN_DOUBLE(code);
3166                        } else {
3167                            RETURN_FALSE;
3168                        }
3169                        break;
3170                    }
3171#if LIBCURL_VERSION_NUM >= 0x070c03 /* Available since 7.12.3 */
3172                    case CURLINFO_SLIST:
3173                    {
3174                        struct curl_slist *slist;
3175                        array_init(return_value);
3176                        if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
3177                            while (slist) {
3178                                add_next_index_string(return_value, slist->data, 1);
3179                                slist = slist->next;
3180                            }
3181                            curl_slist_free_all(slist);
3182                        } else {
3183                            RETURN_FALSE;
3184                        }
3185                        break;
3186                    }
3187#endif
3188                    default:
3189                        RETURN_FALSE;
3190                }
3191            }
3192        }
3193    }
3194}
3195/* }}} */
3196
3197/* {{{ proto string curl_error(resource ch)
3198   Return a string contain the last error for the current session */
3199PHP_FUNCTION(curl_error)
3200{
3201    zval        *zid;
3202    php_curl    *ch;
3203
3204    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3205        return;
3206    }
3207
3208    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3209
3210    ch->err.str[CURL_ERROR_SIZE] = 0;
3211    RETURN_STRING(ch->err.str, 1);
3212}
3213/* }}} */
3214
3215/* {{{ proto int curl_errno(resource ch)
3216   Return an integer containing the last error number */
3217PHP_FUNCTION(curl_errno)
3218{
3219    zval        *zid;
3220    php_curl    *ch;
3221
3222    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3223        return;
3224    }
3225
3226    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3227
3228    RETURN_LONG(ch->err.no);
3229}
3230/* }}} */
3231
3232/* {{{ proto void curl_close(resource ch)
3233   Close a cURL session */
3234PHP_FUNCTION(curl_close)
3235{
3236    zval        *zid;
3237    php_curl    *ch;
3238
3239    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3240        return;
3241    }
3242
3243    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3244
3245    if (ch->in_callback) {
3246        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close cURL handle from a callback");
3247        return;
3248    }
3249
3250    zend_list_delete(Z_LVAL_P(zid));
3251}
3252/* }}} */
3253
3254/* {{{ _php_curl_close()
3255   List destructor for curl handles */
3256static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
3257{
3258#if PHP_CURL_DEBUG
3259    fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
3260#endif
3261
3262    _php_curl_verify_handlers(ch, 0 TSRMLS_CC);
3263
3264    /*
3265     * Libcurl is doing connection caching. When easy handle is cleaned up,
3266     * if the handle was previously used by the curl_multi_api, the connection
3267     * remains open un the curl multi handle is cleaned up. Some protocols are
3268     * sending content like the FTP one, and libcurl try to use the
3269     * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
3270     * callback are freed, we need to use an other callback to which avoid
3271     * segfaults.
3272     *
3273     * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
3274     */
3275    curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
3276    curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
3277
3278    curl_easy_cleanup(ch->cp);
3279
3280    /* cURL destructors should be invoked only by last curl handle */
3281    if (Z_REFCOUNT_P(ch->clone) <= 1) {
3282        zend_llist_clean(&ch->to_free->str);
3283        zend_llist_clean(&ch->to_free->post);
3284        zend_hash_destroy(ch->to_free->slist);
3285        efree(ch->to_free->slist);
3286        efree(ch->to_free);
3287        FREE_ZVAL(ch->clone);
3288    } else {
3289        Z_DELREF_P(ch->clone);
3290    }
3291
3292    if (ch->handlers->write->buf.len > 0) {
3293        smart_str_free(&ch->handlers->write->buf);
3294    }
3295    if (ch->handlers->write->func_name) {
3296        zval_ptr_dtor(&ch->handlers->write->func_name);
3297    }
3298    if (ch->handlers->read->func_name) {
3299        zval_ptr_dtor(&ch->handlers->read->func_name);
3300    }
3301    if (ch->handlers->write_header->func_name) {
3302        zval_ptr_dtor(&ch->handlers->write_header->func_name);
3303    }
3304#if CURLOPT_PASSWDFUNCTION != 0
3305    if (ch->handlers->passwd) {
3306        zval_ptr_dtor(&ch->handlers->passwd);
3307    }
3308#endif
3309    if (ch->handlers->std_err) {
3310        zval_ptr_dtor(&ch->handlers->std_err);
3311    }
3312    if (ch->header.str_len > 0) {
3313        efree(ch->header.str);
3314    }
3315
3316    if (ch->handlers->write_header->stream) {
3317        zval_ptr_dtor(&ch->handlers->write_header->stream);
3318    }
3319    if (ch->handlers->write->stream) {
3320        zval_ptr_dtor(&ch->handlers->write->stream);
3321    }
3322    if (ch->handlers->read->stream) {
3323        zval_ptr_dtor(&ch->handlers->read->stream);
3324    }
3325
3326    efree(ch->handlers->write);
3327    efree(ch->handlers->write_header);
3328    efree(ch->handlers->read);
3329
3330    if (ch->handlers->progress) {
3331        if (ch->handlers->progress->func_name) {
3332            zval_ptr_dtor(&ch->handlers->progress->func_name);
3333        }
3334        efree(ch->handlers->progress);
3335    }
3336
3337#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3338    if (ch->handlers->fnmatch) {
3339        if (ch->handlers->fnmatch->func_name) {
3340            zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3341        }
3342        efree(ch->handlers->fnmatch);
3343    }
3344#endif
3345
3346    efree(ch->handlers);
3347    efree(ch);
3348}
3349/* }}} */
3350
3351/* {{{ _php_curl_close()
3352   List destructor for curl handles */
3353static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
3354{
3355    php_curl *ch = (php_curl *) rsrc->ptr;
3356    _php_curl_close_ex(ch TSRMLS_CC);
3357}
3358/* }}} */
3359
3360#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
3361/* {{{ proto bool curl_strerror(int code)
3362      return string describing error code */
3363PHP_FUNCTION(curl_strerror)
3364{
3365    long code;
3366    const char *str;
3367
3368    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code) == FAILURE) {
3369        return;
3370    }
3371
3372    str = curl_easy_strerror(code);
3373    if (str) {
3374        RETURN_STRING(str, 1);
3375    } else {
3376        RETURN_NULL();
3377    }
3378}
3379/* }}} */
3380#endif
3381
3382#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
3383/* {{{ _php_curl_reset_handlers()
3384   Reset all handlers of a given php_curl */
3385static void _php_curl_reset_handlers(php_curl *ch)
3386{
3387    if (ch->handlers->write->stream) {
3388        Z_DELREF_P(ch->handlers->write->stream);
3389        ch->handlers->write->stream = NULL;
3390    }
3391    ch->handlers->write->fp = NULL;
3392    ch->handlers->write->method = PHP_CURL_STDOUT;
3393
3394    if (ch->handlers->write_header->stream) {
3395        Z_DELREF_P(ch->handlers->write_header->stream);
3396        ch->handlers->write_header->stream = NULL;
3397    }
3398    ch->handlers->write_header->fp = NULL;
3399    ch->handlers->write_header->method = PHP_CURL_IGNORE;
3400
3401    if (ch->handlers->read->stream) {
3402        Z_DELREF_P(ch->handlers->read->stream);
3403        ch->handlers->read->stream = NULL;
3404    }
3405    ch->handlers->read->fp = NULL;
3406    ch->handlers->read->fd = 0;
3407    ch->handlers->read->method  = PHP_CURL_DIRECT;
3408
3409    if (ch->handlers->std_err) {
3410        zval_ptr_dtor(&ch->handlers->std_err);
3411        ch->handlers->std_err = NULL;
3412    }
3413
3414    if (ch->handlers->progress) {
3415        if (ch->handlers->progress->func_name) {
3416            zval_ptr_dtor(&ch->handlers->progress->func_name);
3417        }
3418        efree(ch->handlers->progress);
3419        ch->handlers->progress = NULL;
3420    }
3421
3422#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3423    if (ch->handlers->fnmatch) {
3424        if (ch->handlers->fnmatch->func_name) {
3425            zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3426        }
3427        efree(ch->handlers->fnmatch);
3428        ch->handlers->fnmatch = NULL;
3429    }
3430#endif
3431
3432}
3433/* }}} */
3434
3435/* {{{ proto void curl_reset(resource ch)
3436   Reset all options of a libcurl session handle */
3437PHP_FUNCTION(curl_reset)
3438{
3439    zval       *zid;
3440    php_curl   *ch;
3441
3442    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3443        return;
3444    }
3445
3446    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3447
3448    if (ch->in_callback) {
3449        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to reset cURL handle from a callback");
3450        return;
3451    }
3452
3453    curl_easy_reset(ch->cp);
3454    _php_curl_reset_handlers(ch);
3455    _php_curl_set_default_options(ch);
3456}
3457/* }}} */
3458#endif
3459
3460#if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
3461/* {{{ proto void curl_escape(resource ch, string str)
3462   URL encodes the given string */
3463PHP_FUNCTION(curl_escape)
3464{
3465    char       *str = NULL, *res = NULL;
3466    int        str_len = 0;
3467    zval       *zid;
3468    php_curl   *ch;
3469
3470    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zid, &str, &str_len) == FAILURE) {
3471        return;
3472    }
3473
3474    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3475
3476    if ((res = curl_easy_escape(ch->cp, str, str_len))) {
3477        RETVAL_STRING(res, 1);
3478        curl_free(res);
3479    } else {
3480        RETURN_FALSE;
3481    }
3482}
3483/* }}} */
3484
3485/* {{{ proto void curl_unescape(resource ch, string str)
3486   URL decodes the given string */
3487PHP_FUNCTION(curl_unescape)
3488{
3489    char       *str = NULL, *out = NULL;
3490    int        str_len = 0, out_len;
3491    zval       *zid;
3492    php_curl   *ch;
3493
3494    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zid, &str, &str_len) == FAILURE) {
3495        return;
3496    }
3497
3498    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3499
3500    if ((out = curl_easy_unescape(ch->cp, str, str_len, &out_len))) {
3501        RETVAL_STRINGL(out, out_len, 1);
3502        curl_free(out);
3503    } else {
3504        RETURN_FALSE;
3505    }
3506}
3507/* }}} */
3508#endif
3509
3510#if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
3511/* {{{ proto void curl_pause(resource ch, int bitmask)
3512       pause and unpause a connection */
3513PHP_FUNCTION(curl_pause)
3514{
3515    long       bitmask;
3516    zval       *zid;
3517    php_curl   *ch;
3518
3519    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zid, &bitmask) == FAILURE) {
3520        return;
3521    }
3522
3523    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3524
3525    RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3526}
3527/* }}} */
3528#endif
3529
3530#endif /* HAVE_CURL */
3531
3532/*
3533 * Local variables:
3534 * tab-width: 4
3535 * c-basic-offset: 4
3536 * End:
3537 * vim600: fdm=marker
3538 * vim: noet sw=4 ts=4
3539 */
3540