PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/curl - interface.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1008 1181 85.4 %
Date: 2015-03-22 Functions: 28 31 90.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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 a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Sterling Hughes <sterling@php.net>                           |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
      22             : 
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include "php.h"
      28             : 
      29             : #if HAVE_CURL
      30             : 
      31             : #include <stdio.h>
      32             : #include <string.h>
      33             : 
      34             : #ifdef PHP_WIN32
      35             : #include <winsock2.h>
      36             : #include <sys/types.h>
      37             : #endif
      38             : 
      39             : #include <curl/curl.h>
      40             : #include <curl/easy.h>
      41             : 
      42             : /* As of curl 7.11.1 this is no longer defined inside curl.h */
      43             : #ifndef HttpPost
      44             : #define HttpPost curl_httppost
      45             : #endif
      46             : 
      47             : /* {{{ cruft for thread safe SSL crypto locks */
      48             : #if defined(ZTS) && defined(HAVE_CURL_SSL)
      49             : # ifdef PHP_WIN32
      50             : #  define PHP_CURL_NEED_OPENSSL_TSL
      51             : #  include <openssl/crypto.h>
      52             : # else /* !PHP_WIN32 */
      53             : #  if defined(HAVE_CURL_OPENSSL)
      54             : #   if defined(HAVE_OPENSSL_CRYPTO_H)
      55             : #    define PHP_CURL_NEED_OPENSSL_TSL
      56             : #    include <openssl/crypto.h>
      57             : #   else
      58             : #    warning \
      59             :         "libcurl was compiled with OpenSSL support, but configure could not find " \
      60             :         "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
      61             :         "cause random crashes on SSL requests"
      62             : #   endif
      63             : #  elif defined(HAVE_CURL_GNUTLS)
      64             : #   if defined(HAVE_GCRYPT_H)
      65             : #    define PHP_CURL_NEED_GNUTLS_TSL
      66             : #    include <gcrypt.h>
      67             : #   else
      68             : #    warning \
      69             :         "libcurl was compiled with GnuTLS support, but configure could not find " \
      70             :         "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
      71             :         "cause random crashes on SSL requests"
      72             : #   endif
      73             : #  else
      74             : #   warning \
      75             :         "libcurl was compiled with SSL support, but configure could not determine which" \
      76             :         "library was used; thus no SSL crypto locking callbacks will be set, which may " \
      77             :         "cause random crashes on SSL requests"
      78             : #  endif /* HAVE_CURL_OPENSSL || HAVE_CURL_GNUTLS */
      79             : # endif /* PHP_WIN32 */
      80             : #endif /* ZTS && HAVE_CURL_SSL */
      81             : /* }}} */
      82             : 
      83             : #define SMART_STR_PREALLOC 4096
      84             : 
      85             : #include "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             : 
      91             : int  le_curl;
      92             : int  le_curl_multi_handle;
      93             : 
      94             : #ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
      95             : static MUTEX_T *php_curl_openssl_tsl = NULL;
      96             : 
      97             : static void php_curl_ssl_lock(int mode, int n, const char * file, int line)
      98             : {
      99             :         if (mode & CRYPTO_LOCK) {
     100             :                 tsrm_mutex_lock(php_curl_openssl_tsl[n]);
     101             :         } else {
     102             :                 tsrm_mutex_unlock(php_curl_openssl_tsl[n]);
     103             :         }
     104             : }
     105             : 
     106             : static unsigned long php_curl_ssl_id(void)
     107             : {
     108             :         return (unsigned long) tsrm_thread_id();
     109             : }
     110             : #endif
     111             : /* }}} */
     112             : 
     113             : #ifdef PHP_CURL_NEED_GNUTLS_TSL /* {{{ */
     114             : static int php_curl_ssl_mutex_create(void **m)
     115             : {
     116             :         if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
     117             :                 return SUCCESS;
     118             :         } else {
     119             :                 return FAILURE;
     120             :         }
     121             : }
     122             : 
     123             : static int php_curl_ssl_mutex_destroy(void **m)
     124             : {
     125             :         tsrm_mutex_free(*((MUTEX_T *) m));
     126             :         return SUCCESS;
     127             : }
     128             : 
     129             : static int php_curl_ssl_mutex_lock(void **m)
     130             : {
     131             :         return tsrm_mutex_lock(*((MUTEX_T *) m));
     132             : }
     133             : 
     134             : static int php_curl_ssl_mutex_unlock(void **m)
     135             : {
     136             :         return tsrm_mutex_unlock(*((MUTEX_T *) m));
     137             : }
     138             : 
     139             : static struct gcry_thread_cbs php_curl_gnutls_tsl = {
     140             :         GCRY_THREAD_OPTION_USER,
     141             :         NULL,
     142             :         php_curl_ssl_mutex_create,
     143             :         php_curl_ssl_mutex_destroy,
     144             :         php_curl_ssl_mutex_lock,
     145             :         php_curl_ssl_mutex_unlock
     146             : };
     147             : #endif
     148             : /* }}} */
     149             : 
     150             : static void _php_curl_close_ex(php_curl *ch TSRMLS_DC);
     151             : static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC);
     152             : 
     153             : 
     154             : #define SAVE_CURL_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
     155             : 
     156             : #define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s), (long) v);
     157             : #define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s), (double) v);
     158             : #define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s), (char *) (v ? v : ""), 1);
     159             : #define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s), (zval *) v);
     160             : 
     161             : #if defined(PHP_WIN32) || defined(__GNUC__)
     162             : # define php_curl_ret(__ret) RETVAL_FALSE; return __ret;
     163             : #else
     164             : # define php_curl_ret(__ret) RETVAL_FALSE; return;
     165             : #endif
     166             : 
     167        1104 : static int php_curl_option_url(php_curl *ch, const char *url, const int len TSRMLS_DC) /* {{{ */
     168             : {
     169        1104 :         CURLcode error = CURLE_OK;
     170             : #if LIBCURL_VERSION_NUM < 0x071100
     171             :         char *copystr = NULL;
     172             : #endif
     173             : 
     174        1104 :         if (strlen(url) != len) {
     175           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Curl option contains invalid characters (\\0)");
     176           1 :                 return 0;
     177             :         }
     178             : 
     179             :         /* Disable file:// if open_basedir are used */
     180        1103 :         if (PG(open_basedir) && *PG(open_basedir)) {
     181             : #if LIBCURL_VERSION_NUM >= 0x071304
     182           0 :                 error = curl_easy_setopt(ch->cp, CURLOPT_PROTOCOLS, CURLPROTO_ALL & ~CURLPROTO_FILE);
     183             : #else
     184             :                 php_url *uri;
     185             : 
     186             :                 if (!(uri = php_url_parse_ex(url, len))) {
     187             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid URL '%s'", url);
     188             :                         return 0;
     189             :                 }
     190             : 
     191             :                 if (uri->scheme && !strncasecmp("file", uri->scheme, sizeof("file"))) {
     192             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Protocol 'file' disabled in cURL");
     193             :                         php_url_free(uri);
     194             :                         return 0;
     195             :                 }
     196             :                 php_url_free(uri);
     197             : #endif
     198             :         }
     199             :         /* Strings passed to libcurl as 'char *' arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
     200             : #if LIBCURL_VERSION_NUM >= 0x071100
     201        1103 :         error = curl_easy_setopt(ch->cp, CURLOPT_URL, url);
     202             : #else
     203             :         copystr = estrndup(url, len);
     204             :         error = curl_easy_setopt(ch->cp, CURLOPT_URL, copystr);
     205             :         zend_llist_add_element(&ch->to_free->str, &copystr);
     206             : #endif
     207             : 
     208        1103 :         return (error == CURLE_OK ? 1 : 0);
     209             : }
     210             : /* }}} */
     211             : 
     212        1343 : int _php_curl_verify_handlers(php_curl *ch, int reporterror TSRMLS_DC) /* {{{ */
     213             : {
     214             :         php_stream *stream;
     215        1343 :         if (!ch || !ch->handlers) {
     216           0 :                 return 0;
     217             :         }
     218             : 
     219        1343 :         if (ch->handlers->std_err) {
     220          10 :                 stream = (php_stream *) zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
     221          10 :                 if (stream == NULL) {
     222           6 :                         if (reporterror) {
     223           4 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr");
     224             :                         }
     225           6 :                         zval_ptr_dtor(&ch->handlers->std_err);
     226           6 :                         ch->handlers->std_err = NULL;
     227             : 
     228           6 :                         curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
     229             :                 }
     230             :         }
     231        1343 :         if (ch->handlers->read && ch->handlers->read->stream) {
     232           7 :                 stream = (php_stream *) zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
     233           7 :                 if (stream == NULL) {
     234           3 :                         if (reporterror) {
     235           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default");
     236             :                         }
     237           3 :                         zval_ptr_dtor(&ch->handlers->read->stream);
     238           3 :                         ch->handlers->read->fd = 0;
     239           3 :                         ch->handlers->read->fp = 0;
     240           3 :                         ch->handlers->read->stream = NULL;
     241             : 
     242           3 :                         curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
     243             :                 }
     244             :         }
     245        1343 :         if (ch->handlers->write_header && ch->handlers->write_header->stream) {
     246           3 :                 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());
     247           3 :                 if (stream == NULL) {
     248           3 :                         if (reporterror) {
     249           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default");
     250             :                         }
     251           3 :                         zval_ptr_dtor(&ch->handlers->write_header->stream);
     252           3 :                         ch->handlers->write_header->fp = 0;
     253           3 :                         ch->handlers->write_header->stream = NULL;
     254             : 
     255           3 :                         ch->handlers->write_header->method = PHP_CURL_IGNORE;
     256           3 :                         curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
     257             :                 }
     258             :         }
     259        1343 :         if (ch->handlers->write && ch->handlers->write->stream) {
     260        1035 :                 stream = (php_stream *) zend_fetch_resource(&ch->handlers->write->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
     261        1035 :                 if (stream == NULL) {
     262           4 :                         if (reporterror) {
     263           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default");
     264             :                         }
     265           4 :                         zval_ptr_dtor(&ch->handlers->write->stream);
     266           4 :                         ch->handlers->write->fp = 0;
     267           4 :                         ch->handlers->write->stream = NULL;
     268             : 
     269           4 :                         ch->handlers->write->method = PHP_CURL_STDOUT;
     270           4 :                         ch->handlers->write->type   = PHP_CURL_ASCII;
     271           4 :                         curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
     272             :                 }
     273             :         }
     274        1343 :         return 1;
     275             : }
     276             : /* }}} */
     277             : 
     278             : /* {{{ arginfo */
     279             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_version, 0, 0, 0)
     280             :         ZEND_ARG_INFO(0, version)
     281             : ZEND_END_ARG_INFO()
     282             : 
     283             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_init, 0, 0, 0)
     284             :         ZEND_ARG_INFO(0, url)
     285             : ZEND_END_ARG_INFO()
     286             : 
     287             : ZEND_BEGIN_ARG_INFO(arginfo_curl_copy_handle, 0)
     288             :         ZEND_ARG_INFO(0, ch)
     289             : ZEND_END_ARG_INFO()
     290             : 
     291             : ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt, 0)
     292             :         ZEND_ARG_INFO(0, ch)
     293             :         ZEND_ARG_INFO(0, option)
     294             :         ZEND_ARG_INFO(0, value)
     295             : ZEND_END_ARG_INFO()
     296             : 
     297             : ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt_array, 0)
     298             :         ZEND_ARG_INFO(0, ch)
     299             :         ZEND_ARG_ARRAY_INFO(0, options, 0)
     300             : ZEND_END_ARG_INFO()
     301             : 
     302             : ZEND_BEGIN_ARG_INFO(arginfo_curl_exec, 0)
     303             :         ZEND_ARG_INFO(0, ch)
     304             : ZEND_END_ARG_INFO()
     305             : 
     306             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_getinfo, 0, 0, 1)
     307             :         ZEND_ARG_INFO(0, ch)
     308             :         ZEND_ARG_INFO(0, option)
     309             : ZEND_END_ARG_INFO()
     310             : 
     311             : ZEND_BEGIN_ARG_INFO(arginfo_curl_error, 0)
     312             :         ZEND_ARG_INFO(0, ch)
     313             : ZEND_END_ARG_INFO()
     314             : 
     315             : ZEND_BEGIN_ARG_INFO(arginfo_curl_errno, 0)
     316             :         ZEND_ARG_INFO(0, ch)
     317             : ZEND_END_ARG_INFO()
     318             : 
     319             : ZEND_BEGIN_ARG_INFO(arginfo_curl_close, 0)
     320             :         ZEND_ARG_INFO(0, ch)
     321             : ZEND_END_ARG_INFO()
     322             : 
     323             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
     324             : ZEND_END_ARG_INFO()
     325             : 
     326             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_add_handle, 0)
     327             :         ZEND_ARG_INFO(0, mh)
     328             :         ZEND_ARG_INFO(0, ch)
     329             : ZEND_END_ARG_INFO()
     330             : 
     331             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_remove_handle, 0)
     332             :         ZEND_ARG_INFO(0, mh)
     333             :         ZEND_ARG_INFO(0, ch)
     334             : ZEND_END_ARG_INFO()
     335             : 
     336             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_select, 0, 0, 1)
     337             :         ZEND_ARG_INFO(0, mh)
     338             :         ZEND_ARG_INFO(0, timeout)
     339             : ZEND_END_ARG_INFO()
     340             : 
     341             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_exec, 0, 0, 1)
     342             :         ZEND_ARG_INFO(0, mh)
     343             :         ZEND_ARG_INFO(1, still_running)
     344             : ZEND_END_ARG_INFO()
     345             : 
     346             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_getcontent, 0)
     347             :         ZEND_ARG_INFO(0, ch)
     348             : ZEND_END_ARG_INFO()
     349             : 
     350             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_info_read, 0, 0, 1)
     351             :         ZEND_ARG_INFO(0, mh)
     352             :         ZEND_ARG_INFO(1, msgs_in_queue)
     353             : ZEND_END_ARG_INFO()
     354             : 
     355             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_close, 0)
     356             :         ZEND_ARG_INFO(0, mh)
     357             : ZEND_END_ARG_INFO()
     358             : /* }}} */
     359             : 
     360             : /* {{{ curl_functions[]
     361             :  */
     362             : const zend_function_entry curl_functions[] = {
     363             :         PHP_FE(curl_init,                arginfo_curl_init)
     364             :         PHP_FE(curl_copy_handle,         arginfo_curl_copy_handle)
     365             :         PHP_FE(curl_version,             arginfo_curl_version)
     366             :         PHP_FE(curl_setopt,              arginfo_curl_setopt)
     367             :         PHP_FE(curl_setopt_array,        arginfo_curl_setopt_array)
     368             :         PHP_FE(curl_exec,                arginfo_curl_exec)
     369             :         PHP_FE(curl_getinfo,             arginfo_curl_getinfo)
     370             :         PHP_FE(curl_error,               arginfo_curl_error)
     371             :         PHP_FE(curl_errno,               arginfo_curl_errno)
     372             :         PHP_FE(curl_close,               arginfo_curl_close)
     373             :         PHP_FE(curl_multi_init,          arginfo_curl_multi_init)
     374             :         PHP_FE(curl_multi_add_handle,    arginfo_curl_multi_add_handle)
     375             :         PHP_FE(curl_multi_remove_handle, arginfo_curl_multi_remove_handle)
     376             :         PHP_FE(curl_multi_select,        arginfo_curl_multi_select)
     377             :         PHP_FE(curl_multi_exec,          arginfo_curl_multi_exec)
     378             :         PHP_FE(curl_multi_getcontent,    arginfo_curl_multi_getcontent)
     379             :         PHP_FE(curl_multi_info_read,     arginfo_curl_multi_info_read)
     380             :         PHP_FE(curl_multi_close,         arginfo_curl_multi_close)
     381             :         PHP_FE_END
     382             : };
     383             : /* }}} */
     384             : 
     385             : /* {{{ curl_module_entry
     386             :  */
     387             : zend_module_entry curl_module_entry = {
     388             :         STANDARD_MODULE_HEADER,
     389             :         "curl",
     390             :         curl_functions,
     391             :         PHP_MINIT(curl),
     392             :         PHP_MSHUTDOWN(curl),
     393             :         NULL,
     394             :         NULL,
     395             :         PHP_MINFO(curl),
     396             :         NO_VERSION_YET,
     397             :         STANDARD_MODULE_PROPERTIES
     398             : };
     399             : /* }}} */
     400             : 
     401             : #ifdef COMPILE_DL_CURL
     402             : ZEND_GET_MODULE (curl)
     403             : #endif
     404             : 
     405             : /* {{{ PHP_INI_BEGIN */
     406             : PHP_INI_BEGIN()
     407             :         PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
     408             : PHP_INI_END()
     409             : /* }}} */
     410             : 
     411             : /* {{{ PHP_MINFO_FUNCTION
     412             :  */
     413         148 : PHP_MINFO_FUNCTION(curl)
     414             : {
     415             :         curl_version_info_data *d;
     416             :         char **p;
     417             :         char str[1024];
     418         148 :         size_t n = 0;
     419             : 
     420         148 :         d = curl_version_info(CURLVERSION_NOW);
     421         148 :         php_info_print_table_start();
     422         148 :         php_info_print_table_row(2, "cURL support",    "enabled");
     423         148 :         php_info_print_table_row(2, "cURL Information", d->version);
     424         148 :         sprintf(str, "%d", d->age);
     425         148 :         php_info_print_table_row(2, "Age", str);
     426             : 
     427             :         /* To update on each new cURL release using src/main.c in cURL sources */
     428         148 :         if (d->features) {
     429             :                 struct feat {
     430             :                         const char *name;
     431             :                         int bitmask;
     432             :                 };
     433             : 
     434             :                 unsigned int i;
     435             : 
     436             :                 static const struct feat feats[] = {
     437             : #if LIBCURL_VERSION_NUM > 0x070a06 /* 7.10.7 */
     438             :                         {"AsynchDNS", CURL_VERSION_ASYNCHDNS},
     439             : #endif
     440             : #if LIBCURL_VERSION_NUM > 0x070a05 /* 7.10.6 */
     441             :                         {"Debug", CURL_VERSION_DEBUG},
     442             :                         {"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
     443             : #endif
     444             : #if LIBCURL_VERSION_NUM > 0x070b02 /* 7.12.0 */
     445             :                         {"IDN", CURL_VERSION_IDN},
     446             : #endif
     447             : #ifdef CURL_VERSION_IPV6
     448             :                         {"IPv6", CURL_VERSION_IPV6},
     449             : #endif
     450             : #if LIBCURL_VERSION_NUM > 0x070b00 /* 7.11.1 */
     451             :                         {"Largefile", CURL_VERSION_LARGEFILE},
     452             : #endif
     453             : #if LIBCURL_VERSION_NUM > 0x070a05 /* 7.10.6 */
     454             :                         {"NTLM", CURL_VERSION_NTLM},
     455             : #endif
     456             : #if LIBCURL_VERSION_NUM > 0x070a07 /* 7.10.8 */
     457             :                         {"SPNEGO", CURL_VERSION_SPNEGO},
     458             : #endif
     459             : #ifdef CURL_VERSION_SSL
     460             :                         {"SSL",  CURL_VERSION_SSL},
     461             : #endif
     462             : #if LIBCURL_VERSION_NUM > 0x070d01 /* 7.13.2 */
     463             :                         {"SSPI",  CURL_VERSION_SSPI},
     464             : #endif
     465             : #ifdef CURL_VERSION_KERBEROS4
     466             :                         {"krb4", CURL_VERSION_KERBEROS4},
     467             : #endif
     468             : #ifdef CURL_VERSION_LIBZ
     469             :                         {"libz", CURL_VERSION_LIBZ},
     470             : #endif
     471             : #if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
     472             :                         {"CharConv", CURL_VERSION_CONV},
     473             : #endif
     474             :                         {NULL, 0}
     475             :                 };
     476             : 
     477         148 :                 php_info_print_table_row(1, "Features");
     478        2220 :                 for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
     479        2072 :                         if (feats[i].name) {
     480        1924 :                                 php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
     481             :                         }
     482             :                 }
     483             :         }
     484             : 
     485         148 :         n = 0;
     486         148 :         p = (char **) d->protocols;
     487        2072 :         while (*p != NULL) {
     488        1776 :                         n += sprintf(str + n, "%s%s", *p, *(p + 1) != NULL ? ", " : "");
     489        1776 :                         p++;
     490             :         }
     491         148 :         php_info_print_table_row(2, "Protocols", str);
     492             : 
     493         148 :         php_info_print_table_row(2, "Host", d->host);
     494             : 
     495         148 :         if (d->ssl_version) {
     496         148 :                 php_info_print_table_row(2, "SSL Version", d->ssl_version);
     497             :         }
     498             : 
     499         148 :         if (d->libz_version) {
     500         148 :                 php_info_print_table_row(2, "ZLib Version", d->libz_version);
     501             :         }
     502             : 
     503             : #if defined(CURLVERSION_SECOND) && CURLVERSION_NOW >= CURLVERSION_SECOND
     504             :         if (d->ares) {
     505             :                 php_info_print_table_row(2, "ZLib Version", d->ares);
     506             :         }
     507             : #endif
     508             : 
     509             : #if defined(CURLVERSION_THIRD) && CURLVERSION_NOW >= CURLVERSION_THIRD
     510             :         if (d->libidn) {
     511             :                 php_info_print_table_row(2, "libIDN Version", d->libidn);
     512             :         }
     513             : #endif
     514             : 
     515             : #if LIBCURL_VERSION_NUM >= 0x071300
     516             : 
     517         148 :         if (d->iconv_ver_num) {
     518           0 :                 php_info_print_table_row(2, "IconV Version", d->iconv_ver_num);
     519             :         }
     520             : 
     521         148 :         if (d->libssh_version) {
     522         148 :                 php_info_print_table_row(2, "libSSH Version", d->libssh_version);
     523             :         }
     524             : #endif
     525         148 :         php_info_print_table_end();
     526         148 : }
     527             : /* }}} */
     528             : 
     529             : #define REGISTER_CURL_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)
     530             : 
     531             : /* {{{ PHP_MINIT_FUNCTION
     532             :  */
     533       20247 : PHP_MINIT_FUNCTION(curl)
     534             : {
     535       20247 :         le_curl = zend_register_list_destructors_ex(_php_curl_close, NULL, "curl", module_number);
     536       20247 :         le_curl_multi_handle = zend_register_list_destructors_ex(_php_curl_multi_close, NULL, "curl_multi", module_number);
     537             : 
     538       20247 :         REGISTER_INI_ENTRIES();
     539             : 
     540             :         /* See http://curl.haxx.se/lxr/source/docs/libcurl/symbols-in-versions
     541             :            or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
     542             :            of options and which version they were introduced */
     543             : 
     544             :         /* Constants for curl_setopt() */
     545             : #if LIBCURL_VERSION_NUM > 0x070a07 /* CURLOPT_IPRESOLVE is available since curl 7.10.8 */
     546       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_IPRESOLVE);
     547       20247 :         REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_WHATEVER);
     548       20247 :         REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V4);
     549       20247 :         REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V6);
     550             : #endif
     551       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_DNS_USE_GLOBAL_CACHE);
     552       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_DNS_CACHE_TIMEOUT);
     553       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PORT);
     554       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FILE);
     555       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_READDATA);
     556       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_INFILE);
     557       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_INFILESIZE);
     558       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_URL);
     559       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXY);
     560       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_VERBOSE);
     561       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HEADER);
     562       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPHEADER);
     563       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_NOPROGRESS);
     564       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROGRESSFUNCTION);
     565       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_NOBODY);
     566       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FAILONERROR);
     567       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_UPLOAD);
     568       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_POST);
     569       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPLISTONLY);
     570       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPAPPEND);
     571       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_NETRC);
     572       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FOLLOWLOCATION);
     573             : #if CURLOPT_FTPASCII != 0
     574             :         REGISTER_CURL_CONSTANT(CURLOPT_FTPASCII);
     575             : #endif
     576       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PUT);
     577             : #if CURLOPT_MUTE != 0
     578             :         REGISTER_CURL_CONSTANT(CURLOPT_MUTE);
     579             : #endif
     580       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_USERPWD);
     581       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERPWD);
     582       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_RANGE);
     583       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT);
     584             : #if LIBCURL_VERSION_NUM > 0x071002
     585       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
     586             : #endif
     587       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_POSTFIELDS);
     588       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_REFERER);
     589       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_USERAGENT);
     590       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPPORT);
     591       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPSV);
     592       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_LIMIT);
     593       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_TIME);
     594       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_RESUME_FROM);
     595       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIE);
     596       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIESESSION);
     597       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_AUTOREFERER);
     598       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLCERT);
     599       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTPASSWD);
     600       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_WRITEHEADER);
     601       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYHOST);
     602       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIEFILE);
     603       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLVERSION);
     604       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMECONDITION);
     605       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMEVALUE);
     606       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CUSTOMREQUEST);
     607       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_STDERR);
     608       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_TRANSFERTEXT);
     609       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_RETURNTRANSFER);
     610       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_QUOTE);
     611       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_POSTQUOTE);
     612       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_INTERFACE);
     613       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_KRB4LEVEL);
     614       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPPROXYTUNNEL);
     615       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FILETIME);
     616       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_WRITEFUNCTION);
     617       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_READFUNCTION);
     618             : #if CURLOPT_PASSWDFUNCTION != 0
     619             :         REGISTER_CURL_CONSTANT(CURLOPT_PASSWDFUNCTION);
     620             : #endif
     621       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HEADERFUNCTION);
     622       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_MAXREDIRS);
     623       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_MAXCONNECTS);
     624       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CLOSEPOLICY);
     625       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FRESH_CONNECT);
     626       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FORBID_REUSE);
     627       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_RANDOM_FILE);
     628       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_EGDSOCKET);
     629       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT);
     630             : #if LIBCURL_VERSION_NUM > 0x071002
     631       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT_MS);
     632             : #endif
     633       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYPEER);
     634       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CAINFO);
     635       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CAPATH);
     636       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIEJAR);
     637       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSL_CIPHER_LIST);
     638       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_BINARYTRANSFER);
     639       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_NOSIGNAL);
     640       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYTYPE);
     641       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_BUFFERSIZE);
     642       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPGET);
     643       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTP_VERSION);
     644       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLKEY);
     645       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYTYPE);
     646       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYPASSWD);
     647       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE);
     648       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE_DEFAULT);
     649       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTTYPE);
     650       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CRLF);
     651       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_ENCODING);
     652       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYPORT);
     653       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_UNRESTRICTED_AUTH);
     654       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPRT);
     655             : #if LIBCURL_VERSION_NUM > 0x070b01 /* CURLOPT_TCP_NODELAY is available since curl 7.11.2 */
     656       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_TCP_NODELAY);
     657             : #endif
     658       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTP200ALIASES);
     659       20247 :         REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFMODSINCE);
     660       20247 :         REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFUNMODSINCE);
     661       20247 :         REGISTER_CURL_CONSTANT(CURL_TIMECOND_LASTMOD);
     662             : 
     663             : #if LIBCURL_VERSION_NUM > 0x070f04 /* CURLOPT_MAX_RECV_SPEED_LARGE & CURLOPT_MAX_SEND_SPEED_LARGE  are available since curl 7.15.5 */
     664       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_MAX_RECV_SPEED_LARGE);
     665       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_MAX_SEND_SPEED_LARGE);
     666             : #endif 
     667             : 
     668             : #if LIBCURL_VERSION_NUM > 0x070a05 /* CURLOPT_HTTPAUTH is available since curl 7.10.6 */
     669       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPAUTH);
     670             :         /* http authentication options */
     671       20247 :         REGISTER_CURL_CONSTANT(CURLAUTH_BASIC);
     672       20247 :         REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST);
     673       20247 :         REGISTER_CURL_CONSTANT(CURLAUTH_GSSNEGOTIATE);
     674       20247 :         REGISTER_CURL_CONSTANT(CURLAUTH_NTLM);
     675       20247 :         REGISTER_CURL_CONSTANT(CURLAUTH_ANY);
     676       20247 :         REGISTER_CURL_CONSTANT(CURLAUTH_ANYSAFE);
     677             : #endif
     678             : 
     679             : #if LIBCURL_VERSION_NUM > 0x070a06 /* CURLOPT_PROXYAUTH & CURLOPT_FTP_CREATE_MISSING_DIRS are available since curl 7.10.7 */
     680       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYAUTH);
     681       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_CREATE_MISSING_DIRS);
     682             : #endif
     683             : 
     684       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PRIVATE);
     685             : 
     686             :         /* Constants effecting the way CURLOPT_CLOSEPOLICY works */
     687       20247 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
     688       20247 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_LEAST_TRAFFIC);
     689       20247 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_SLOWEST);
     690       20247 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_CALLBACK);
     691       20247 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_OLDEST);
     692             : 
     693             :         /* Info constants */
     694       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_EFFECTIVE_URL);
     695       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CODE);
     696       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_HEADER_SIZE);
     697       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_REQUEST_SIZE);
     698       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_TOTAL_TIME);
     699       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_NAMELOOKUP_TIME);
     700       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_CONNECT_TIME);
     701       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_PRETRANSFER_TIME);
     702       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_SIZE_UPLOAD);
     703       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_SIZE_DOWNLOAD);
     704       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_SPEED_DOWNLOAD);
     705       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_SPEED_UPLOAD);
     706       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_FILETIME);
     707       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_SSL_VERIFYRESULT);
     708       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
     709       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_UPLOAD);
     710       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_STARTTRANSFER_TIME);
     711       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_TYPE);
     712       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME);
     713       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_COUNT);
     714       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_HEADER_OUT);
     715       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_PRIVATE);
     716             : #if LIBCURL_VERSION_NUM >  0x071301
     717       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_CERTINFO);
     718             : #endif
     719             : #if LIBCURL_VERSION_NUM >= 0x071202
     720       20247 :     REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_URL);
     721             : #endif
     722             : #if LIBCURL_VERSION_NUM >= 0x071300 /* 7.19.0 */
     723       20247 :         REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_IP);
     724             : #endif
     725             : #if LIBCURL_VERSION_NUM >= 0x071500 /* 7.21.0 */
     726             :         REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_PORT);
     727             :         REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_IP);
     728             :         REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_PORT);
     729             : #endif
     730             : 
     731             : 
     732             :         /* cURL protocol constants (curl_version) */
     733       20247 :         REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
     734       20247 :         REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS4);
     735       20247 :         REGISTER_CURL_CONSTANT(CURL_VERSION_SSL);
     736       20247 :         REGISTER_CURL_CONSTANT(CURL_VERSION_LIBZ);
     737             : 
     738             :         /* version constants */
     739       20247 :         REGISTER_CURL_CONSTANT(CURLVERSION_NOW);
     740             : 
     741             :         /* Error Constants */
     742       20247 :         REGISTER_CURL_CONSTANT(CURLE_OK);
     743       20247 :         REGISTER_CURL_CONSTANT(CURLE_UNSUPPORTED_PROTOCOL);
     744       20247 :         REGISTER_CURL_CONSTANT(CURLE_FAILED_INIT);
     745       20247 :         REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT);
     746       20247 :         REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT_USER);
     747       20247 :         REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_PROXY);
     748       20247 :         REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_HOST);
     749       20247 :         REGISTER_CURL_CONSTANT(CURLE_COULDNT_CONNECT);
     750       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_SERVER_REPLY);
     751       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_ACCESS_DENIED);
     752       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_USER_PASSWORD_INCORRECT);
     753       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASS_REPLY);
     754       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_USER_REPLY);
     755       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASV_REPLY);
     756       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_227_FORMAT);
     757       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_GET_HOST);
     758       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_RECONNECT);
     759       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_BINARY);
     760       20247 :         REGISTER_CURL_CONSTANT(CURLE_PARTIAL_FILE);
     761       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_RETR_FILE);
     762       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WRITE_ERROR);
     763       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_QUOTE_ERROR);
     764       20247 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_NOT_FOUND);
     765       20247 :         REGISTER_CURL_CONSTANT(CURLE_WRITE_ERROR);
     766       20247 :         REGISTER_CURL_CONSTANT(CURLE_MALFORMAT_USER);
     767       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_STOR_FILE);
     768       20247 :         REGISTER_CURL_CONSTANT(CURLE_READ_ERROR);
     769       20247 :         REGISTER_CURL_CONSTANT(CURLE_OUT_OF_MEMORY);
     770       20247 :         REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEOUTED);
     771       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_ASCII);
     772       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_PORT_FAILED);
     773       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_USE_REST);
     774       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_GET_SIZE);
     775       20247 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_RANGE_ERROR);
     776       20247 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_POST_ERROR);
     777       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CONNECT_ERROR);
     778       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_BAD_DOWNLOAD_RESUME);
     779       20247 :         REGISTER_CURL_CONSTANT(CURLE_FILE_COULDNT_READ_FILE);
     780       20247 :         REGISTER_CURL_CONSTANT(CURLE_LDAP_CANNOT_BIND);
     781       20247 :         REGISTER_CURL_CONSTANT(CURLE_LDAP_SEARCH_FAILED);
     782       20247 :         REGISTER_CURL_CONSTANT(CURLE_LIBRARY_NOT_FOUND);
     783       20247 :         REGISTER_CURL_CONSTANT(CURLE_FUNCTION_NOT_FOUND);
     784       20247 :         REGISTER_CURL_CONSTANT(CURLE_ABORTED_BY_CALLBACK);
     785       20247 :         REGISTER_CURL_CONSTANT(CURLE_BAD_FUNCTION_ARGUMENT);
     786       20247 :         REGISTER_CURL_CONSTANT(CURLE_BAD_CALLING_ORDER);
     787       20247 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_PORT_FAILED);
     788       20247 :         REGISTER_CURL_CONSTANT(CURLE_BAD_PASSWORD_ENTERED);
     789       20247 :         REGISTER_CURL_CONSTANT(CURLE_TOO_MANY_REDIRECTS);
     790       20247 :         REGISTER_CURL_CONSTANT(CURLE_UNKNOWN_TELNET_OPTION);
     791       20247 :         REGISTER_CURL_CONSTANT(CURLE_TELNET_OPTION_SYNTAX);
     792       20247 :         REGISTER_CURL_CONSTANT(CURLE_OBSOLETE);
     793       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_PEER_CERTIFICATE);
     794       20247 :         REGISTER_CURL_CONSTANT(CURLE_GOT_NOTHING);
     795       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_NOTFOUND);
     796       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_SETFAILED);
     797       20247 :         REGISTER_CURL_CONSTANT(CURLE_SEND_ERROR);
     798       20247 :         REGISTER_CURL_CONSTANT(CURLE_RECV_ERROR);
     799       20247 :         REGISTER_CURL_CONSTANT(CURLE_SHARE_IN_USE);
     800       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CERTPROBLEM);
     801       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CIPHER);
     802       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT);
     803       20247 :         REGISTER_CURL_CONSTANT(CURLE_BAD_CONTENT_ENCODING);
     804             : #if LIBCURL_VERSION_NUM >= 0x070a08
     805       20247 :         REGISTER_CURL_CONSTANT(CURLE_LDAP_INVALID_URL);
     806       20247 :         REGISTER_CURL_CONSTANT(CURLE_FILESIZE_EXCEEDED);
     807             : #endif
     808             : #if LIBCURL_VERSION_NUM >= 0x070b00
     809       20247 :         REGISTER_CURL_CONSTANT(CURLE_FTP_SSL_FAILED);
     810             : #endif
     811       20247 :         REGISTER_CURL_CONSTANT(CURLPROXY_HTTP);
     812       20247 :         REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4);
     813       20247 :         REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5);
     814             : 
     815       20247 :         REGISTER_CURL_CONSTANT(CURL_NETRC_OPTIONAL);
     816       20247 :         REGISTER_CURL_CONSTANT(CURL_NETRC_IGNORED);
     817       20247 :         REGISTER_CURL_CONSTANT(CURL_NETRC_REQUIRED);
     818             : 
     819       20247 :         REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_NONE);
     820       20247 :         REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_0);
     821       20247 :         REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_1);
     822             : 
     823       20247 :         REGISTER_CURL_CONSTANT(CURLM_CALL_MULTI_PERFORM);
     824       20247 :         REGISTER_CURL_CONSTANT(CURLM_OK);
     825       20247 :         REGISTER_CURL_CONSTANT(CURLM_BAD_HANDLE);
     826       20247 :         REGISTER_CURL_CONSTANT(CURLM_BAD_EASY_HANDLE);
     827       20247 :         REGISTER_CURL_CONSTANT(CURLM_OUT_OF_MEMORY);
     828       20247 :         REGISTER_CURL_CONSTANT(CURLM_INTERNAL_ERROR);
     829             : 
     830       20247 :         REGISTER_CURL_CONSTANT(CURLMSG_DONE);
     831             : 
     832             : #if LIBCURL_VERSION_NUM >= 0x070c02
     833       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPSSLAUTH);
     834       20247 :         REGISTER_CURL_CONSTANT(CURLFTPAUTH_DEFAULT);
     835       20247 :         REGISTER_CURL_CONSTANT(CURLFTPAUTH_SSL);
     836       20247 :         REGISTER_CURL_CONSTANT(CURLFTPAUTH_TLS);
     837             : #endif
     838             : 
     839             : #if LIBCURL_VERSION_NUM >  0x070b00
     840       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL);
     841       20247 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_NONE);
     842       20247 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_TRY);
     843       20247 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_CONTROL);
     844       20247 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_ALL);
     845             : #endif
     846             : 
     847             : #if LIBCURL_VERSION_NUM > 0x071301
     848       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_CERTINFO);
     849       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_POSTREDIR);
     850             : #endif
     851             : 
     852             : /* SSH support works in 7.19.0+ using libssh2 */
     853             : #if LIBCURL_VERSION_NUM >= 0x071300
     854       20247 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_NONE);
     855       20247 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PUBLICKEY);
     856       20247 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PASSWORD);
     857       20247 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_HOST);
     858       20247 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_KEYBOARD);
     859       20247 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_DEFAULT);
     860       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_AUTH_TYPES);
     861       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_KEYPASSWD);
     862       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_PUBLIC_KEYFILE);
     863       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_PRIVATE_KEYFILE);
     864       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
     865       20247 :         REGISTER_CURL_CONSTANT(CURLE_SSH);
     866             : #endif
     867             : 
     868             : #if LIBCURL_VERSION_NUM >= 0x071304
     869       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_REDIR_PROTOCOLS);
     870       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_PROTOCOLS);
     871       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_HTTP);
     872       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_HTTPS);
     873       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_FTP);
     874       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_FTPS);
     875       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_SCP);
     876       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_SFTP);
     877       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_TELNET);
     878       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_LDAP);
     879       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_LDAPS);
     880       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_DICT);
     881       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_FILE);
     882       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_TFTP);
     883       20247 :         REGISTER_CURL_CONSTANT(CURLPROTO_ALL);
     884             : #endif
     885             : 
     886             : #if LIBCURL_VERSION_NUM >= 0x070f01
     887       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_FILEMETHOD);
     888       20247 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_SKIP_PASV_IP);
     889             : #endif
     890             : 
     891             : #if LIBCURL_VERSION_NUM >= 0x071001
     892       20247 :         REGISTER_CURL_CONSTANT(CURLFTPMETHOD_MULTICWD);
     893       20247 :         REGISTER_CURL_CONSTANT(CURLFTPMETHOD_NOCWD);
     894       20247 :         REGISTER_CURL_CONSTANT(CURLFTPMETHOD_SINGLECWD);
     895             : #endif
     896             : 
     897             : #ifdef PHP_CURL_NEED_OPENSSL_TSL
     898             :         if (!CRYPTO_get_id_callback()) {
     899             :                 int i, c = CRYPTO_num_locks();
     900             : 
     901             :                 php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
     902             :                 if (!php_curl_openssl_tsl) {
     903             :                         return FAILURE;
     904             :                 }
     905             : 
     906             :                 for (i = 0; i < c; ++i) {
     907             :                         php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
     908             :                 }
     909             : 
     910             :                 CRYPTO_set_id_callback(php_curl_ssl_id);
     911             :                 CRYPTO_set_locking_callback(php_curl_ssl_lock);
     912             :         }
     913             : #endif
     914             : #ifdef PHP_CURL_NEED_GNUTLS_TSL
     915             :         gcry_control(GCRYCTL_SET_THREAD_CBS, &php_curl_gnutls_tsl);
     916             : #endif
     917             : 
     918       20247 :         if (curl_global_init(CURL_GLOBAL_SSL) != CURLE_OK) {
     919           0 :                 return FAILURE;
     920             :         }
     921             : 
     922             : #ifdef PHP_CURL_URL_WRAPPERS
     923             : # if HAVE_CURL_VERSION_INFO
     924             :         {
     925       20247 :                 curl_version_info_data *info = curl_version_info(CURLVERSION_NOW);
     926       20247 :                 char **p = (char **)info->protocols;
     927             : 
     928      283458 :                 while (*p != NULL) {
     929             :                         /* Do not enable cURL "file" protocol and make sure cURL is always used when --with-curlwrappers is enabled */
     930      242964 :                         if (strncasecmp(*p, "file", sizeof("file")-1) != 0) {
     931      222717 :                                 php_unregister_url_stream_wrapper(*p TSRMLS_CC);
     932      222717 :                                 php_register_url_stream_wrapper(*p, &php_curl_wrapper TSRMLS_CC);
     933             :                         }
     934      242964 :                         (void) *p++;
     935             :                 }
     936             :         }
     937             : # else
     938             :         php_unregister_url_stream_wrapper("http");
     939             :         php_register_url_stream_wrapper("http", &php_curl_wrapper TSRMLS_CC);
     940             :         php_unregister_url_stream_wrapper("https");
     941             :         php_register_url_stream_wrapper("https", &php_curl_wrapper TSRMLS_CC);
     942             :         php_unregister_url_stream_wrapper("ftp");
     943             :         php_register_url_stream_wrapper("ftp", &php_curl_wrapper TSRMLS_CC);
     944             :         php_unregister_url_stream_wrapper("ftps");
     945             :         php_register_url_stream_wrapper("ftps", &php_curl_wrapper TSRMLS_CC);
     946             :         php_unregister_url_stream_wrapper("ldap");
     947             :         php_register_url_stream_wrapper("ldap", &php_curl_wrapper TSRMLS_CC);
     948             : # endif
     949             : #endif
     950             : 
     951       20247 :         return SUCCESS;
     952             : }
     953             : /* }}} */
     954             : 
     955             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     956             :  */
     957       20283 : PHP_MSHUTDOWN_FUNCTION(curl)
     958             : {
     959             : #ifdef PHP_CURL_URL_WRAPPERS
     960       20283 :         php_unregister_url_stream_wrapper("http" TSRMLS_CC);
     961       20283 :         php_unregister_url_stream_wrapper("https" TSRMLS_CC);
     962       20283 :         php_unregister_url_stream_wrapper("ftp" TSRMLS_CC);
     963       20283 :         php_unregister_url_stream_wrapper("ldap" TSRMLS_CC);
     964             : #endif
     965       20283 :         curl_global_cleanup();
     966             : #ifdef PHP_CURL_NEED_OPENSSL_TSL
     967             :         if (php_curl_openssl_tsl) {
     968             :                 int i, c = CRYPTO_num_locks();
     969             : 
     970             :                 CRYPTO_set_id_callback(NULL);
     971             :                 CRYPTO_set_locking_callback(NULL);
     972             : 
     973             :                 for (i = 0; i < c; ++i) {
     974             :                         tsrm_mutex_free(php_curl_openssl_tsl[i]);
     975             :                 }
     976             : 
     977             :                 free(php_curl_openssl_tsl);
     978             :                 php_curl_openssl_tsl = NULL;
     979             :         }
     980             : #endif
     981       20283 :         UNREGISTER_INI_ENTRIES();
     982       20283 :         return SUCCESS;
     983             : }
     984             : /* }}} */
     985             : 
     986             : /* {{{ curl_write_nothing
     987             :  * Used as a work around. See _php_curl_close_ex
     988             :  */
     989           0 : static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ctx) 
     990             : {
     991           0 :         return size * nmemb;
     992             : }
     993             : /* }}} */
     994             : 
     995             : /* {{{ curl_write
     996             :  */
     997          74 : static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
     998             : {
     999          74 :         php_curl       *ch     = (php_curl *) ctx;
    1000          74 :         php_curl_write *t      = ch->handlers->write;
    1001          74 :         size_t          length = size * nmemb;
    1002             :         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1003             : 
    1004             : #if PHP_CURL_DEBUG
    1005             :         fprintf(stderr, "curl_write() called\n");
    1006             :         fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
    1007             : #endif
    1008             : 
    1009          74 :         switch (t->method) {
    1010             :                 case PHP_CURL_STDOUT:
    1011          14 :                         PHPWRITE(data, length);
    1012          14 :                         break;
    1013             :                 case PHP_CURL_FILE:
    1014           2 :                         return fwrite(data, size, nmemb, t->fp);
    1015             :                 case PHP_CURL_RETURN:
    1016          56 :                         if (length > 0) {
    1017          56 :                                 smart_str_appendl(&t->buf, data, (int) length);
    1018             :                         }
    1019          56 :                         break;
    1020             :                 case PHP_CURL_USER: {
    1021             :                         zval **argv[2];
    1022           2 :                         zval *retval_ptr = NULL;
    1023           2 :                         zval *handle = NULL;
    1024           2 :                         zval *zdata = NULL;
    1025             :                         int   error;
    1026             :                         zend_fcall_info fci;
    1027             : 
    1028           2 :                         MAKE_STD_ZVAL(handle);
    1029           2 :                         ZVAL_RESOURCE(handle, ch->id);
    1030           2 :                         zend_list_addref(ch->id);
    1031           2 :                         argv[0] = &handle;
    1032             : 
    1033           2 :                         MAKE_STD_ZVAL(zdata);
    1034           2 :                         ZVAL_STRINGL(zdata, data, length, 1);
    1035           2 :                         argv[1] = &zdata;
    1036             : 
    1037           2 :                         fci.size = sizeof(fci);
    1038           2 :                         fci.function_table = EG(function_table);
    1039           2 :                         fci.object_ptr = NULL;
    1040           2 :                         fci.function_name = t->func_name;
    1041           2 :                         fci.retval_ptr_ptr = &retval_ptr;
    1042           2 :                         fci.param_count = 2;
    1043           2 :                         fci.params = argv;
    1044           2 :                         fci.no_separation = 0;
    1045           2 :                         fci.symbol_table = NULL;
    1046             : 
    1047           2 :                         ch->in_callback = 1;
    1048           2 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1049           2 :                         ch->in_callback = 0;
    1050           2 :                         if (error == FAILURE) {
    1051           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
    1052           0 :                                 length = -1;
    1053           2 :                         } else if (retval_ptr) {
    1054           2 :                                 if (Z_TYPE_P(retval_ptr) != IS_LONG) {
    1055           0 :                                         convert_to_long_ex(&retval_ptr);
    1056             :                                 }
    1057           2 :                                 length = Z_LVAL_P(retval_ptr);
    1058           2 :                                 zval_ptr_dtor(&retval_ptr);
    1059             :                         }
    1060             : 
    1061           2 :                         zval_ptr_dtor(argv[0]);
    1062           2 :                         zval_ptr_dtor(argv[1]);
    1063             :                         break;
    1064             :                 }
    1065             :         }
    1066             : 
    1067          72 :         return length;
    1068             : }
    1069             : /* }}} */
    1070             : 
    1071             : /* {{{ curl_progress
    1072             :  */
    1073           7 : static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
    1074             : {
    1075           7 :         php_curl       *ch = (php_curl *) clientp;
    1076           7 :         php_curl_progress  *t  = ch->handlers->progress;
    1077           7 :         size_t  rval = 0;
    1078             : 
    1079             : #if PHP_CURL_DEBUG
    1080             :         fprintf(stderr, "curl_progress() called\n");
    1081             :         fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
    1082             : #endif
    1083             : 
    1084           7 :         switch (t->method) {
    1085             :                 case PHP_CURL_USER: {
    1086             :                         zval **argv[4];
    1087           7 :                         zval  *zdltotal = NULL;
    1088           7 :                         zval  *zdlnow = NULL;
    1089           7 :                         zval  *zultotal = NULL;
    1090           7 :                         zval  *zulnow = NULL;
    1091             :                         zval  *retval_ptr;
    1092             :                         int   error;
    1093             :                         zend_fcall_info fci;
    1094             :                         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1095             : 
    1096           7 :                         MAKE_STD_ZVAL(zdltotal);
    1097           7 :                         MAKE_STD_ZVAL(zdlnow);
    1098           7 :                         MAKE_STD_ZVAL(zultotal);
    1099           7 :                         MAKE_STD_ZVAL(zulnow);
    1100             : 
    1101           7 :                         ZVAL_LONG(zdltotal, (long) dltotal);
    1102           7 :                         ZVAL_LONG(zdlnow, (long) dlnow);
    1103           7 :                         ZVAL_LONG(zultotal, (long) ultotal);
    1104           7 :                         ZVAL_LONG(zulnow, (long) ulnow);
    1105             : 
    1106           7 :                         argv[0] = &zdltotal;
    1107           7 :                         argv[1] = &zdlnow;
    1108           7 :                         argv[2] = &zultotal;
    1109           7 :                         argv[3] = &zulnow;
    1110             : 
    1111           7 :                         fci.size = sizeof(fci);
    1112           7 :                         fci.function_table = EG(function_table);
    1113           7 :                         fci.function_name = t->func_name;
    1114           7 :                         fci.object_ptr = NULL;
    1115           7 :                         fci.retval_ptr_ptr = &retval_ptr;
    1116           7 :                         fci.param_count = 4;
    1117           7 :                         fci.params = argv;
    1118           7 :                         fci.no_separation = 0;
    1119           7 :                         fci.symbol_table = NULL;
    1120             : 
    1121           7 :                         ch->in_callback = 1;
    1122           7 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1123           7 :                         ch->in_callback = 0;
    1124           7 :                         if (error == FAILURE) {
    1125           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION");
    1126           7 :                         } else if (retval_ptr) {
    1127           7 :                                 if (Z_TYPE_P(retval_ptr) != IS_LONG) {
    1128          28 :                                         convert_to_long_ex(&retval_ptr);
    1129             :                                 }
    1130           7 :                                 if (0 != Z_LVAL_P(retval_ptr)) {
    1131           0 :                                         rval = 1;
    1132             :                                 }
    1133           7 :                                 zval_ptr_dtor(&retval_ptr);
    1134             :                         }
    1135           7 :                         zval_ptr_dtor(argv[0]);
    1136           7 :                         zval_ptr_dtor(argv[1]);
    1137           7 :                         zval_ptr_dtor(argv[2]);
    1138           7 :                         zval_ptr_dtor(argv[3]);
    1139             :                         break;
    1140             :                 }
    1141             :         }
    1142           7 :         return rval;
    1143             : }
    1144             : /* }}} */
    1145             : 
    1146             : /* {{{ curl_read
    1147             :  */
    1148           3 : static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
    1149             : {
    1150           3 :         php_curl       *ch = (php_curl *) ctx;
    1151           3 :         php_curl_read  *t  = ch->handlers->read;
    1152           3 :         int             length = 0;
    1153             : 
    1154           3 :         switch (t->method) {
    1155             :                 case PHP_CURL_DIRECT:
    1156           1 :                         if (t->fp) {
    1157           1 :                                 length = fread(data, size, nmemb, t->fp);
    1158             :                         }
    1159           1 :                         break;
    1160             :                 case PHP_CURL_USER: {
    1161             :                         zval **argv[3];
    1162           2 :                         zval  *handle = NULL;
    1163           2 :                         zval  *zfd = NULL;
    1164           2 :                         zval  *zlength = NULL;
    1165             :                         zval  *retval_ptr;
    1166             :                         int   error;
    1167             :                         zend_fcall_info fci;
    1168             :                         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1169             : 
    1170           2 :                         MAKE_STD_ZVAL(handle);
    1171           2 :                         MAKE_STD_ZVAL(zfd);
    1172           2 :                         MAKE_STD_ZVAL(zlength);
    1173             : 
    1174           2 :                         ZVAL_RESOURCE(handle, ch->id);
    1175           2 :                         zend_list_addref(ch->id);
    1176           2 :                         ZVAL_RESOURCE(zfd, t->fd);
    1177           2 :                         zend_list_addref(t->fd);
    1178           2 :                         ZVAL_LONG(zlength, (int) size * nmemb);
    1179             : 
    1180           2 :                         argv[0] = &handle;
    1181           2 :                         argv[1] = &zfd;
    1182           2 :                         argv[2] = &zlength;
    1183             : 
    1184           2 :                         fci.size = sizeof(fci);
    1185           2 :                         fci.function_table = EG(function_table);
    1186           2 :                         fci.function_name = t->func_name;
    1187           2 :                         fci.object_ptr = NULL;
    1188           2 :                         fci.retval_ptr_ptr = &retval_ptr;
    1189           2 :                         fci.param_count = 3;
    1190           2 :                         fci.params = argv;
    1191           2 :                         fci.no_separation = 0;
    1192           2 :                         fci.symbol_table = NULL;
    1193             : 
    1194           2 :                         ch->in_callback = 1;
    1195           2 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1196           2 :                         ch->in_callback = 0;
    1197           2 :                         if (error == FAILURE) {
    1198           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_READFUNCTION");
    1199             : #if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
    1200           0 :                                 length = CURL_READFUNC_ABORT;
    1201             : #endif
    1202           2 :                         } else if (retval_ptr) {
    1203           2 :                                 if (Z_TYPE_P(retval_ptr) == IS_STRING) {
    1204           2 :                                         length = MIN((int) (size * nmemb), Z_STRLEN_P(retval_ptr));
    1205           2 :                                         memcpy(data, Z_STRVAL_P(retval_ptr), length);
    1206             :                                 }
    1207           2 :                                 zval_ptr_dtor(&retval_ptr);
    1208             :                         }
    1209             : 
    1210           2 :                         zval_ptr_dtor(argv[0]);
    1211           2 :                         zval_ptr_dtor(argv[1]);
    1212           2 :                         zval_ptr_dtor(argv[2]);
    1213             :                         break;
    1214             :                 }
    1215             :         }
    1216             : 
    1217           3 :         return length;
    1218             : }
    1219             : /* }}} */
    1220             : 
    1221             : /* {{{ curl_write_header
    1222             :  */
    1223         443 : static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
    1224             : {
    1225         443 :         php_curl       *ch  = (php_curl *) ctx;
    1226         443 :         php_curl_write *t   = ch->handlers->write_header;
    1227         443 :         size_t          length = size * nmemb;
    1228             :         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1229             : 
    1230         443 :         switch (t->method) {
    1231             :                 case PHP_CURL_STDOUT:
    1232             :                         /* Handle special case write when we're returning the entire transfer
    1233             :                          */
    1234           0 :                         if (ch->handlers->write->method == PHP_CURL_RETURN && length > 0) {
    1235           0 :                                 smart_str_appendl(&ch->handlers->write->buf, data, (int) length);
    1236             :                         } else {
    1237           0 :                                 PHPWRITE(data, length);
    1238             :                         }
    1239           0 :                         break;
    1240             :                 case PHP_CURL_FILE:
    1241           0 :                         return fwrite(data, size, nmemb, t->fp);
    1242             :                 case PHP_CURL_USER: {
    1243             :                         zval **argv[2];
    1244           1 :                         zval  *handle = NULL;
    1245           1 :                         zval  *zdata = NULL;
    1246             :                         zval  *retval_ptr;
    1247             :                         int   error;
    1248             :                         zend_fcall_info fci;
    1249             : 
    1250           1 :                         MAKE_STD_ZVAL(handle);
    1251           1 :                         MAKE_STD_ZVAL(zdata);
    1252             : 
    1253           1 :                         ZVAL_RESOURCE(handle, ch->id);
    1254           1 :                         zend_list_addref(ch->id);
    1255           1 :                         ZVAL_STRINGL(zdata, data, length, 1);
    1256             : 
    1257           1 :                         argv[0] = &handle;
    1258           1 :                         argv[1] = &zdata;
    1259             : 
    1260           1 :                         fci.size = sizeof(fci);
    1261           1 :                         fci.function_table = EG(function_table);
    1262           1 :                         fci.function_name = t->func_name;
    1263           1 :                         fci.symbol_table = NULL;
    1264           1 :                         fci.object_ptr = NULL;
    1265           1 :                         fci.retval_ptr_ptr = &retval_ptr;
    1266           1 :                         fci.param_count = 2;
    1267           1 :                         fci.params = argv;
    1268           1 :                         fci.no_separation = 0;
    1269             : 
    1270           1 :                         ch->in_callback = 1;
    1271           1 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1272           1 :                         ch->in_callback = 0;
    1273           1 :                         if (error == FAILURE) {
    1274           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION");
    1275           0 :                                 length = -1;
    1276           1 :                         } else if (retval_ptr) {
    1277           1 :                                 if (Z_TYPE_P(retval_ptr) != IS_LONG) {
    1278           4 :                                         convert_to_long_ex(&retval_ptr);
    1279             :                                 }
    1280           1 :                                 length = Z_LVAL_P(retval_ptr);
    1281           1 :                                 zval_ptr_dtor(&retval_ptr);
    1282             :                         }
    1283           1 :                         zval_ptr_dtor(argv[0]);
    1284           1 :                         zval_ptr_dtor(argv[1]);
    1285           1 :                         break;
    1286             :                 }
    1287             : 
    1288             :                 case PHP_CURL_IGNORE:
    1289         442 :                         return length;
    1290             : 
    1291             :                 default:
    1292           0 :                         return -1;
    1293             :         }
    1294             : 
    1295           1 :         return length;
    1296             : }
    1297             : /* }}} */
    1298             : 
    1299           0 : static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */
    1300             : {
    1301           0 :         php_curl    *ch   = (php_curl *) ctx;
    1302             : 
    1303           0 :         if (type == CURLINFO_HEADER_OUT) {
    1304           0 :                 if (ch->header.str_len) {
    1305           0 :                         efree(ch->header.str);
    1306             :                 }
    1307           0 :                 if (buf_len > 0) {
    1308           0 :                         ch->header.str = estrndup(buf, buf_len);
    1309           0 :                         ch->header.str_len = buf_len;
    1310             :                 }
    1311             :         }
    1312             : 
    1313           0 :         return 0;
    1314             : }
    1315             : /* }}} */
    1316             : 
    1317             : #if CURLOPT_PASSWDFUNCTION != 0
    1318             : /* {{{ curl_passwd
    1319             :  */
    1320             : static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen)
    1321             : {
    1322             :         php_curl    *ch   = (php_curl *) ctx;
    1323             :         zval        *func = ch->handlers->passwd;
    1324             :         zval        *argv[3];
    1325             :         zval        *retval = NULL;
    1326             :         int          error;
    1327             :         int          ret = -1;
    1328             :         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1329             : 
    1330             :         MAKE_STD_ZVAL(argv[0]);
    1331             :         MAKE_STD_ZVAL(argv[1]);
    1332             :         MAKE_STD_ZVAL(argv[2]);
    1333             : 
    1334             :         ZVAL_RESOURCE(argv[0], ch->id);
    1335             :         zend_list_addref(ch->id);
    1336             :         ZVAL_STRING(argv[1], prompt, 1);
    1337             :         ZVAL_LONG(argv[2], buflen);
    1338             : 
    1339             :         error = call_user_function(EG(function_table), NULL, func, retval, 2, argv TSRMLS_CC);
    1340             :         if (error == FAILURE) {
    1341             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_PASSWDFUNCTION");
    1342             :         } else if (Z_TYPE_P(retval) == IS_STRING) {
    1343             :                 if (Z_STRLEN_P(retval) > buflen) {
    1344             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Returned password is too long for libcurl to handle");
    1345             :                 } else {
    1346             :                         strlcpy(buf, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
    1347             :                 }
    1348             :         } else {
    1349             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "User handler '%s' did not return a string", Z_STRVAL_P(func));
    1350             :         }
    1351             : 
    1352             :         zval_ptr_dtor(&argv[0]);
    1353             :         zval_ptr_dtor(&argv[1]);
    1354             :         zval_ptr_dtor(&argv[2]);
    1355             :         zval_ptr_dtor(&retval);
    1356             : 
    1357             :         return ret;
    1358             : }
    1359             : /* }}} */
    1360             : #endif
    1361             : 
    1362             : /* {{{ curl_free_string
    1363             :  */
    1364           1 : static void curl_free_string(void **string)
    1365             : {
    1366           1 :         efree(*string);
    1367           1 : }
    1368             : /* }}} */
    1369             : 
    1370             : /* {{{ curl_free_post
    1371             :  */
    1372           7 : static void curl_free_post(void **post)
    1373             : {
    1374           7 :         curl_formfree((struct HttpPost *) *post);
    1375           7 : }
    1376             : /* }}} */
    1377             : 
    1378             : /* {{{ curl_free_slist
    1379             :  */
    1380       10003 : static void curl_free_slist(void *slist)
    1381             : {
    1382       10003 :         curl_slist_free_all(*((struct curl_slist **) slist));
    1383       10003 : }
    1384             : /* }}} */
    1385             : 
    1386             : /* {{{ proto array curl_version([int version])
    1387             :    Return cURL version information. */
    1388          30 : PHP_FUNCTION(curl_version)
    1389             : {
    1390             :         curl_version_info_data *d;
    1391          30 :         long uversion = CURLVERSION_NOW;
    1392             : 
    1393          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &uversion) == FAILURE) {
    1394           8 :                 return;
    1395             :         }
    1396             : 
    1397          22 :         d = curl_version_info(uversion);
    1398          22 :         if (d == NULL) {
    1399           0 :                 RETURN_FALSE;
    1400             :         }
    1401             : 
    1402          22 :         array_init(return_value);
    1403             : 
    1404          22 :         CAAL("version_number", d->version_num);
    1405          22 :         CAAL("age", d->age);
    1406          22 :         CAAL("features", d->features);
    1407          22 :         CAAL("ssl_version_number", d->ssl_version_num);
    1408          22 :         CAAS("version", d->version);
    1409          22 :         CAAS("host", d->host);
    1410          22 :         CAAS("ssl_version", d->ssl_version);
    1411          22 :         CAAS("libz_version", d->libz_version);
    1412             :         /* Add an array of protocols */
    1413             :         {
    1414          22 :                 char **p = (char **) d->protocols;
    1415          22 :                 zval  *protocol_list = NULL;
    1416             : 
    1417          22 :                 MAKE_STD_ZVAL(protocol_list);
    1418          22 :                 array_init(protocol_list);
    1419             : 
    1420         308 :                 while (*p != NULL) {
    1421         264 :                         add_next_index_string(protocol_list, *p, 1);
    1422         264 :                         p++;
    1423             :                 }
    1424          22 :                 CAAZ("protocols", protocol_list);
    1425             :         }
    1426             : }
    1427             : /* }}} */
    1428             : 
    1429             : /* {{{ alloc_curl_handle
    1430             :  */
    1431         100 : static void alloc_curl_handle(php_curl **ch)
    1432             : {
    1433         100 :         *ch                           = emalloc(sizeof(php_curl));
    1434         100 :         (*ch)->to_free                = ecalloc(1, sizeof(struct _php_curl_free));
    1435         100 :         (*ch)->handlers               = ecalloc(1, sizeof(php_curl_handlers));
    1436         100 :         (*ch)->handlers->write        = ecalloc(1, sizeof(php_curl_write));
    1437         100 :         (*ch)->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
    1438         100 :         (*ch)->handlers->read         = ecalloc(1, sizeof(php_curl_read));
    1439         100 :         (*ch)->handlers->progress     = ecalloc(1, sizeof(php_curl_progress));
    1440             : 
    1441         100 :         (*ch)->in_callback = 0;
    1442         100 :         (*ch)->header.str_len = 0;
    1443             : 
    1444         100 :         memset(&(*ch)->err, 0, sizeof((*ch)->err));
    1445         100 :         (*ch)->handlers->write->stream = NULL;
    1446         100 :         (*ch)->handlers->write_header->stream = NULL;
    1447         100 :         (*ch)->handlers->read->stream = NULL;
    1448             : 
    1449         100 :         zend_llist_init(&(*ch)->to_free->str,   sizeof(char *),            (llist_dtor_func_t) curl_free_string, 0);
    1450         100 :         zend_llist_init(&(*ch)->to_free->post,  sizeof(struct HttpPost),   (llist_dtor_func_t) curl_free_post,   0);
    1451             : 
    1452         100 :         (*ch)->to_free->slist = emalloc(sizeof(HashTable));
    1453         100 :         zend_hash_init((*ch)->to_free->slist, 4, NULL, curl_free_slist, 0);
    1454         100 : }
    1455             : /* }}} */
    1456             : 
    1457             : #if LIBCURL_VERSION_NUM > 0x071301
    1458             : /* {{{ split_certinfo
    1459             :  */
    1460           0 : static void split_certinfo(char *string, zval *hash)
    1461             : {
    1462           0 :         char *org = estrdup(string);
    1463           0 :         char *s = org;
    1464             :         char *split;
    1465             : 
    1466           0 :         if(org) {
    1467             :         do {
    1468             :                         char *key;
    1469             :                         char *val;
    1470             :                         char *tmp;
    1471             : 
    1472           0 :             split = strstr(s, "; ");
    1473           0 :             if(split)
    1474           0 :                 *split = '\0';
    1475             :                         
    1476           0 :                         key = s;
    1477           0 :                         tmp = memchr(key, '=', 64);
    1478           0 :                         if(tmp) {
    1479           0 :                                 *tmp = '\0';
    1480           0 :                                 val = tmp+1;
    1481           0 :                                 add_assoc_string(hash, key, val, 1);
    1482             :                         }
    1483           0 :                         s = split+2;
    1484           0 :                 } while(split);
    1485           0 :                 efree(org);
    1486             :         }
    1487           0 : }
    1488             : /* }}} */
    1489             : 
    1490             : /* {{{ create_certinfo
    1491             :  */
    1492          13 : static void create_certinfo(struct curl_certinfo *ci, zval *listcode TSRMLS_DC)
    1493             : {
    1494             :         int i;
    1495             :                         
    1496          13 :         if(ci) {
    1497          13 :                 zval *certhash = NULL;
    1498             :                 
    1499          13 :                 for(i=0; i<ci->num_of_certs; i++) {
    1500             :                         struct curl_slist *slist;
    1501             :                         
    1502           0 :                         MAKE_STD_ZVAL(certhash);
    1503           0 :                         array_init(certhash);
    1504           0 :                         for(slist = ci->certinfo[i]; slist; slist = slist->next) {
    1505             :                                 int len;
    1506             :                                 char s[64];
    1507             :                                 char *tmp;
    1508           0 :                                 strncpy(s, slist->data, 64);
    1509           0 :                                 tmp = memchr(s, ':', 64);
    1510           0 :                                 if(tmp) {
    1511           0 :                                         *tmp = '\0';
    1512           0 :                                         len = strlen(s);
    1513           0 :                                         if(!strcmp(s, "Subject") || !strcmp(s, "Issuer")) {
    1514             :                                                 zval *hash;
    1515             : 
    1516           0 :                                                 MAKE_STD_ZVAL(hash);
    1517           0 :                                                 array_init(hash);
    1518             :                                                 
    1519           0 :                                                 split_certinfo(&slist->data[len+1], hash);
    1520           0 :                                                 add_assoc_zval(certhash, s, hash);
    1521             :                                         } else {
    1522           0 :                                                 add_assoc_string(certhash, s, &slist->data[len+1], 1);
    1523             :                                         }
    1524             :                                 } else {
    1525           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not extract hash key from certificate info"); 
    1526             :                                 }
    1527             :                         }
    1528           0 :                         add_next_index_zval(listcode, certhash);
    1529             :                 }
    1530             :         }
    1531          13 : }
    1532             : /* }}} */
    1533             : #endif
    1534             : 
    1535             : /* {{{ proto resource curl_init([string url])
    1536             :    Initialize a cURL session */
    1537          89 : PHP_FUNCTION(curl_init)
    1538             : {
    1539             :         php_curl        *ch;
    1540             :         CURL            *cp;
    1541             :         zval            *clone;
    1542          89 :         char            *url = NULL;
    1543          89 :         int             url_len = 0;
    1544             :         char *cainfo;
    1545             : 
    1546          89 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &url, &url_len) == FAILURE) {
    1547           0 :                 return;
    1548             :         }
    1549             : 
    1550          89 :         cp = curl_easy_init();
    1551          89 :         if (!cp) {
    1552           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize a new cURL handle");
    1553           0 :                 RETURN_FALSE;
    1554             :         }
    1555             : 
    1556          89 :         alloc_curl_handle(&ch);
    1557             :         TSRMLS_SET_CTX(ch->thread_ctx);
    1558             : 
    1559          89 :         ch->cp = cp;
    1560             : 
    1561          89 :         ch->handlers->write->method = PHP_CURL_STDOUT;
    1562          89 :         ch->handlers->write->type   = PHP_CURL_ASCII;
    1563          89 :         ch->handlers->read->method  = PHP_CURL_DIRECT;
    1564          89 :         ch->handlers->write_header->method = PHP_CURL_IGNORE;
    1565             : 
    1566          89 :         ch->uses = 0;
    1567             : 
    1568          89 :         MAKE_STD_ZVAL(clone);
    1569          89 :         ch->clone = clone;
    1570             : 
    1571          89 :         curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
    1572          89 :         curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
    1573          89 :         curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
    1574          89 :         curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
    1575          89 :         curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
    1576          89 :         curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
    1577          89 :         curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
    1578          89 :         curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
    1579          89 :         curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
    1580          89 :         curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
    1581          89 :         curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
    1582          89 :         curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
    1583             : 
    1584          89 :         cainfo = INI_STR("curl.cainfo");
    1585          89 :         if (cainfo && strlen(cainfo) > 0) {
    1586           0 :                 curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
    1587             :         }
    1588             : 
    1589             : #if defined(ZTS)
    1590             :         curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
    1591             : #endif
    1592             : 
    1593          89 :         if (url) {
    1594          10 :                 if (!php_curl_option_url(ch, url, url_len TSRMLS_CC)) {
    1595           0 :                         _php_curl_close_ex(ch TSRMLS_CC);
    1596           0 :                         RETURN_FALSE;
    1597             :                 }
    1598             :         }
    1599             : 
    1600          89 :         ZEND_REGISTER_RESOURCE(return_value, ch, le_curl);
    1601          89 :         ch->id = Z_LVAL_P(return_value);
    1602             : }
    1603             : /* }}} */
    1604             : 
    1605             : /* {{{ proto resource curl_copy_handle(resource ch)
    1606             :    Copy a cURL handle along with all of it's preferences */
    1607          11 : PHP_FUNCTION(curl_copy_handle)
    1608             : {
    1609             :         CURL            *cp;
    1610             :         zval            *zid;
    1611             :         php_curl        *ch, *dupch;
    1612             : 
    1613          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    1614           0 :                 return;
    1615             :         }
    1616             : 
    1617          11 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    1618             : 
    1619          11 :         cp = curl_easy_duphandle(ch->cp);
    1620          11 :         if (!cp) {
    1621           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot duplicate cURL handle");
    1622           0 :                 RETURN_FALSE;
    1623             :         }
    1624             : 
    1625          11 :         alloc_curl_handle(&dupch);
    1626             :         TSRMLS_SET_CTX(dupch->thread_ctx);
    1627             : 
    1628          11 :         dupch->cp = cp;
    1629          11 :         dupch->uses = 0;
    1630          11 :         ch->uses++;
    1631          11 :         if (ch->handlers->write->stream) {
    1632           1 :                 Z_ADDREF_P(ch->handlers->write->stream);
    1633             :         }
    1634          11 :         dupch->handlers->write->stream = ch->handlers->write->stream;
    1635          11 :         dupch->handlers->write->method = ch->handlers->write->method;
    1636          11 :         dupch->handlers->write->type   = ch->handlers->write->type;
    1637          11 :         if (ch->handlers->read->stream) {
    1638           0 :                 Z_ADDREF_P(ch->handlers->read->stream);
    1639             :         }
    1640          11 :         dupch->handlers->read->stream  = ch->handlers->read->stream;
    1641          11 :         dupch->handlers->read->method  = ch->handlers->read->method;
    1642          11 :         dupch->handlers->write_header->method = ch->handlers->write_header->method;
    1643          11 :         if (ch->handlers->write_header->stream) {
    1644           0 :                 Z_ADDREF_P(ch->handlers->write_header->stream);
    1645             :         }
    1646          11 :         dupch->handlers->write_header->stream = ch->handlers->write_header->stream;
    1647             : 
    1648          11 :         dupch->handlers->write->fp = ch->handlers->write->fp;
    1649          11 :         dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
    1650          11 :         dupch->handlers->read->fp = ch->handlers->read->fp;
    1651          11 :         dupch->handlers->read->fd = ch->handlers->read->fd;
    1652             : #if CURLOPT_PASSWDDATA != 0
    1653             :         if (ch->handlers->passwd) {
    1654             :                 zval_add_ref(&ch->handlers->passwd);
    1655             :                 dupch->handlers->passwd = ch->handlers->passwd;
    1656             :                 curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
    1657             :         }
    1658             : #endif
    1659          11 :         if (ch->handlers->write->func_name) {
    1660           0 :                 zval_add_ref(&ch->handlers->write->func_name);
    1661           0 :                 dupch->handlers->write->func_name = ch->handlers->write->func_name;
    1662             :         }
    1663          11 :         if (ch->handlers->read->func_name) {
    1664           0 :                 zval_add_ref(&ch->handlers->read->func_name);
    1665           0 :                 dupch->handlers->read->func_name = ch->handlers->read->func_name;
    1666             :         }
    1667          11 :         if (ch->handlers->write_header->func_name) {
    1668           0 :                 zval_add_ref(&ch->handlers->write_header->func_name);
    1669           0 :                 dupch->handlers->write_header->func_name = ch->handlers->write_header->func_name;
    1670             :         }
    1671             :         
    1672          11 :         if (ch->handlers->progress->func_name) {
    1673           1 :                 zval_add_ref(&ch->handlers->progress->func_name);
    1674           1 :                 dupch->handlers->progress->func_name = ch->handlers->progress->func_name;
    1675             :         }
    1676          11 :         dupch->handlers->progress->method = ch->handlers->progress->method;
    1677             : 
    1678          11 :         curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER,       dupch->err.str);
    1679          11 :         curl_easy_setopt(dupch->cp, CURLOPT_FILE,              (void *) dupch);
    1680          11 :         curl_easy_setopt(dupch->cp, CURLOPT_INFILE,            (void *) dupch);
    1681          11 :         curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER,       (void *) dupch);
    1682          11 :         curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA,      (void *) dupch); 
    1683             : 
    1684          11 :         efree(dupch->to_free->slist);
    1685          11 :         efree(dupch->to_free);
    1686          11 :         dupch->to_free = ch->to_free;
    1687             : 
    1688             :         /* Keep track of cloned copies to avoid invoking curl destructors for every clone */
    1689          11 :         Z_ADDREF_P(ch->clone);
    1690          11 :         dupch->clone = ch->clone;
    1691             : 
    1692          11 :         ZEND_REGISTER_RESOURCE(return_value, dupch, le_curl);
    1693          11 :         dupch->id = Z_LVAL_P(return_value);
    1694             : }
    1695             : /* }}} */
    1696             : 
    1697       12250 : static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */
    1698             : {
    1699       12250 :         CURLcode     error=CURLE_OK;
    1700             : 
    1701       12250 :         switch (option) {
    1702             :                 /* Long options */
    1703             :                 case CURLOPT_SSL_VERIFYHOST:
    1704           5 :                         if(Z_BVAL_PP(zvalue) == 1) {
    1705             : #if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
    1706           2 :                                 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");
    1707             : #else
    1708             :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
    1709             :                                 error = curl_easy_setopt(ch->cp, option, 2);
    1710             :                                 break;
    1711             : #endif
    1712             :                         }
    1713             :                 case CURLOPT_INFILESIZE:
    1714             :                 case CURLOPT_VERBOSE:
    1715             :                 case CURLOPT_HEADER:
    1716             :                 case CURLOPT_NOPROGRESS:
    1717             :                 case CURLOPT_NOBODY:
    1718             :                 case CURLOPT_FAILONERROR:
    1719             :                 case CURLOPT_UPLOAD:
    1720             :                 case CURLOPT_POST:
    1721             :                 case CURLOPT_FTPLISTONLY:
    1722             :                 case CURLOPT_FTPAPPEND:
    1723             :                 case CURLOPT_NETRC:
    1724             :                 case CURLOPT_PUT:
    1725             : #if CURLOPT_MUTE != 0
    1726             :                  case CURLOPT_MUTE:
    1727             : #endif
    1728             :                 case CURLOPT_TIMEOUT:
    1729             : #if LIBCURL_VERSION_NUM > 0x071002
    1730             :                 case CURLOPT_TIMEOUT_MS:
    1731             : #endif
    1732             :                 case CURLOPT_FTP_USE_EPSV:
    1733             :                 case CURLOPT_LOW_SPEED_LIMIT:
    1734             :                 case CURLOPT_SSLVERSION:
    1735             :                 case CURLOPT_LOW_SPEED_TIME:
    1736             :                 case CURLOPT_RESUME_FROM:
    1737             :                 case CURLOPT_TIMEVALUE:
    1738             :                 case CURLOPT_TIMECONDITION:
    1739             :                 case CURLOPT_TRANSFERTEXT:
    1740             :                 case CURLOPT_HTTPPROXYTUNNEL:
    1741             :                 case CURLOPT_FILETIME:
    1742             :                 case CURLOPT_MAXREDIRS:
    1743             :                 case CURLOPT_MAXCONNECTS:
    1744             :                 case CURLOPT_CLOSEPOLICY:
    1745             :                 case CURLOPT_FRESH_CONNECT:
    1746             :                 case CURLOPT_FORBID_REUSE:
    1747             :                 case CURLOPT_CONNECTTIMEOUT:
    1748             : #if LIBCURL_VERSION_NUM > 0x071002
    1749             :                 case CURLOPT_CONNECTTIMEOUT_MS:
    1750             : #endif
    1751             :                 case CURLOPT_SSL_VERIFYPEER:
    1752             :                 case CURLOPT_DNS_USE_GLOBAL_CACHE:
    1753             :                 case CURLOPT_NOSIGNAL:
    1754             :                 case CURLOPT_PROXYTYPE:
    1755             :                 case CURLOPT_BUFFERSIZE:
    1756             :                 case CURLOPT_HTTPGET:
    1757             :                 case CURLOPT_HTTP_VERSION:
    1758             :                 case CURLOPT_CRLF:
    1759             :                 case CURLOPT_DNS_CACHE_TIMEOUT:
    1760             :                 case CURLOPT_PROXYPORT:
    1761             :                 case CURLOPT_FTP_USE_EPRT:
    1762             : #if LIBCURL_VERSION_NUM > 0x070a05 /* CURLOPT_HTTPAUTH is available since curl 7.10.6 */
    1763             :                 case CURLOPT_HTTPAUTH:
    1764             : #endif
    1765             : #if LIBCURL_VERSION_NUM > 0x070a06 /* CURLOPT_PROXYAUTH & CURLOPT_FTP_CREATE_MISSING_DIRS are available since curl 7.10.7 */
    1766             :                 case CURLOPT_PROXYAUTH:
    1767             :                 case CURLOPT_FTP_CREATE_MISSING_DIRS:
    1768             : #endif
    1769             : 
    1770             : #if LIBCURL_VERSION_NUM >= 0x070c02
    1771             :                 case CURLOPT_FTPSSLAUTH:
    1772             : #endif
    1773             : #if LIBCURL_VERSION_NUM >  0x070b00
    1774             :                 case CURLOPT_FTP_SSL:
    1775             : #endif
    1776             :                 case CURLOPT_UNRESTRICTED_AUTH:
    1777             :                 case CURLOPT_PORT:
    1778             :                 case CURLOPT_AUTOREFERER:
    1779             :                 case CURLOPT_COOKIESESSION:
    1780             : #if LIBCURL_VERSION_NUM > 0x070b01 /* CURLOPT_TCP_NODELAY is available since curl 7.11.2 */
    1781             :                 case CURLOPT_TCP_NODELAY:
    1782             : #endif
    1783             : #if LIBCURL_VERSION_NUM >= 0x71304
    1784             :                 case CURLOPT_REDIR_PROTOCOLS:
    1785             :                 case CURLOPT_PROTOCOLS:
    1786             : #endif
    1787             : #if LIBCURL_VERSION_NUM > 0x070a07 /* CURLOPT_IPRESOLVE is available since curl 7.10.8 */
    1788             :                 case CURLOPT_IPRESOLVE:
    1789             : #endif
    1790             : #if LIBCURL_VERSION_NUM >= 0x070f01
    1791             :                 case CURLOPT_FTP_FILEMETHOD:
    1792             :                 case CURLOPT_FTP_SKIP_PASV_IP:
    1793             : #endif
    1794             : #if LIBCURL_VERSION_NUM >  0x071301
    1795             :                 case CURLOPT_CERTINFO:
    1796             : #endif
    1797          44 :                         convert_to_long_ex(zvalue);
    1798             : #if LIBCURL_VERSION_NUM >= 0x71304
    1799          27 :                         if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
    1800           0 :                                 (PG(open_basedir) && *PG(open_basedir)) && (Z_LVAL_PP(zvalue) & CURLPROTO_FILE)) {
    1801           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
    1802           0 :                                         RETVAL_FALSE;
    1803           0 :                                         return 1;
    1804             :                         }
    1805             : #endif
    1806          27 :                         error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
    1807          27 :                         break;
    1808             : #if LIBCURL_VERSION_NUM > 0x070f04
    1809             :                 case CURLOPT_MAX_RECV_SPEED_LARGE:
    1810             :                 case CURLOPT_MAX_SEND_SPEED_LARGE:
    1811           0 :                         convert_to_long_ex(zvalue);
    1812           0 :                         error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_PP(zvalue));
    1813           0 :                         break;
    1814             : #endif
    1815             :                 case CURLOPT_FOLLOWLOCATION:
    1816           1 :                         convert_to_long_ex(zvalue);
    1817           1 :                         if (PG(open_basedir) && *PG(open_basedir)) {
    1818           1 :                                 if (Z_LVAL_PP(zvalue) != 0) {
    1819           1 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set");
    1820           1 :                                         RETVAL_FALSE;
    1821           1 :                                         return 1;
    1822             :                                 }
    1823             :                         }
    1824           0 :                         error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
    1825           0 :                         break;
    1826             : #if LIBCURL_VERSION_NUM > 0x071301
    1827             :                 case CURLOPT_POSTREDIR:
    1828           0 :                         convert_to_long_ex(zvalue);
    1829           0 :                         error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, Z_LVAL_PP(zvalue) & CURL_REDIR_POST_ALL);
    1830           0 :                         break;
    1831             : #endif
    1832             :                 case CURLOPT_PRIVATE:
    1833             :                 case CURLOPT_URL:
    1834             :                 case CURLOPT_PROXY:
    1835             :                 case CURLOPT_USERPWD:
    1836             :                 case CURLOPT_PROXYUSERPWD:
    1837             :                 case CURLOPT_RANGE:
    1838             :                 case CURLOPT_CUSTOMREQUEST:
    1839             :                 case CURLOPT_USERAGENT:
    1840             :                 case CURLOPT_FTPPORT:
    1841             :                 case CURLOPT_COOKIE:
    1842             :                 case CURLOPT_REFERER:
    1843             :                 case CURLOPT_INTERFACE:
    1844             :                 case CURLOPT_KRB4LEVEL:
    1845             :                 case CURLOPT_EGDSOCKET:
    1846             :                 case CURLOPT_CAINFO:
    1847             :                 case CURLOPT_CAPATH:
    1848             :                 case CURLOPT_SSL_CIPHER_LIST:
    1849             :                 case CURLOPT_SSLKEY:
    1850             :                 case CURLOPT_SSLKEYTYPE:
    1851             :                 case CURLOPT_SSLKEYPASSWD:
    1852             :                 case CURLOPT_SSLENGINE:
    1853             :                 case CURLOPT_SSLENGINE_DEFAULT:
    1854             :                 case CURLOPT_SSLCERTTYPE:
    1855             :                 case CURLOPT_ENCODING:
    1856             : #if LIBCURL_VERSION_NUM >= 0x071300
    1857             :                 case CURLOPT_SSH_PUBLIC_KEYFILE:
    1858             :                 case CURLOPT_SSH_PRIVATE_KEYFILE:
    1859             : #endif
    1860             :                 {
    1861        1100 :                         convert_to_string_ex(zvalue);
    1862             : #if LIBCURL_VERSION_NUM >= 0x071300
    1863        1100 :                         if (
    1864             :                                 option == CURLOPT_SSH_PUBLIC_KEYFILE || option == CURLOPT_SSH_PRIVATE_KEYFILE
    1865             : 
    1866             :                         ) {
    1867           0 :                                 if (php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) {
    1868           0 :                                         RETVAL_FALSE;
    1869           0 :                                         return 1;
    1870             :                                 }
    1871             :                         }
    1872             : #endif
    1873        1100 :                         if (option == CURLOPT_URL) {
    1874        1094 :                                 if (!php_curl_option_url(ch, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue) TSRMLS_CC)) {
    1875           1 :                                         RETVAL_FALSE;
    1876           1 :                                         return 1;
    1877             :                                 }
    1878             :                         } else {
    1879           6 :                                 if (option == CURLOPT_PRIVATE) {
    1880             :                                         char *copystr;
    1881             : #if LIBCURL_VERSION_NUM < 0x071100
    1882             : string_copy:
    1883             : #endif
    1884           1 :                                         copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
    1885           1 :                                         error = curl_easy_setopt(ch->cp, option, copystr);
    1886           1 :                                         zend_llist_add_element(&ch->to_free->str, &copystr);
    1887             :                                 } else {
    1888             : #if LIBCURL_VERSION_NUM >= 0x071100
    1889             :                                         /* Strings passed to libcurl as ’char *’ arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
    1890           5 :                                         error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
    1891             : #else                           
    1892             :                                         goto string_copy;                       
    1893             : #endif
    1894             :                                 }
    1895             :                         }
    1896        1099 :                         break;
    1897             :                 }
    1898             :                 case CURLOPT_FILE:
    1899             :                 case CURLOPT_INFILE:
    1900             :                 case CURLOPT_WRITEHEADER:
    1901             :                 case CURLOPT_STDERR: {
    1902        1048 :                         FILE *fp = NULL;
    1903             :                         int type;
    1904             :                         void * what;
    1905             : 
    1906        1048 :                         what = zend_fetch_resource(zvalue TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream(), php_file_le_pstream());
    1907        1048 :                         if (!what) {
    1908           0 :                                 RETVAL_FALSE;
    1909           0 :                                 return 1;
    1910             :                         }
    1911             : 
    1912        1048 :                         if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
    1913           0 :                                 RETVAL_FALSE;
    1914           0 :                                 return 1;
    1915             :                         }
    1916             : 
    1917        1048 :                         if (!fp) {
    1918           0 :                                 RETVAL_FALSE;
    1919           0 :                                 return 1;
    1920             :                         }
    1921             : 
    1922        1048 :                         error = CURLE_OK;
    1923        1048 :                         switch (option) {
    1924             :                                 case CURLOPT_FILE:
    1925        2063 :                                         if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
    1926        1031 :                                                 if (ch->handlers->write->stream) {
    1927        1023 :                                                         Z_DELREF_P(ch->handlers->write->stream);
    1928             :                                                 }
    1929        1031 :                                                 Z_ADDREF_PP(zvalue);
    1930        1031 :                                                 ch->handlers->write->fp = fp;
    1931        1031 :                                                 ch->handlers->write->method = PHP_CURL_FILE;
    1932        1031 :                                                 ch->handlers->write->stream = *zvalue;
    1933             :                                         } else {
    1934           1 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
    1935           1 :                                                 RETVAL_FALSE;
    1936           1 :                                                 return 1;
    1937             :                                         }
    1938        1031 :                                         break;
    1939             :                                 case CURLOPT_WRITEHEADER:
    1940           6 :                                         if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
    1941           3 :                                                 if (ch->handlers->write_header->stream) {
    1942           0 :                                                         Z_DELREF_P(ch->handlers->write_header->stream);
    1943             :                                                 }
    1944           3 :                                                 Z_ADDREF_PP(zvalue);
    1945           3 :                                                 ch->handlers->write_header->fp = fp;
    1946           3 :                                                 ch->handlers->write_header->method = PHP_CURL_FILE;
    1947           3 :                                                 ch->handlers->write_header->stream = *zvalue;
    1948             :                                         } else {
    1949           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
    1950           0 :                                                 RETVAL_FALSE;
    1951           0 :                                                 return 1;
    1952             :                                         }
    1953           3 :                                         break;
    1954             :                                 case CURLOPT_INFILE:
    1955           5 :                                         if (ch->handlers->read->stream) {
    1956           0 :                                                 Z_DELREF_P(ch->handlers->read->stream);
    1957             :                                         }
    1958           5 :                                         Z_ADDREF_PP(zvalue);
    1959           5 :                                         ch->handlers->read->fp = fp;
    1960           5 :                                         ch->handlers->read->fd = Z_LVAL_PP(zvalue);
    1961           5 :                                         ch->handlers->read->stream = *zvalue;
    1962           5 :                                         break;
    1963             :                                 case CURLOPT_STDERR:
    1964          16 :                                         if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
    1965           8 :                                                 if (ch->handlers->std_err) {
    1966           1 :                                                         zval_ptr_dtor(&ch->handlers->std_err);
    1967             :                                                 }
    1968           8 :                                                 zval_add_ref(zvalue);
    1969           8 :                                                 ch->handlers->std_err = *zvalue;
    1970             :                                         } else {
    1971           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
    1972           0 :                                                 RETVAL_FALSE;
    1973           0 :                                                 return 1;
    1974             :                                         }
    1975             :                                         /* break omitted intentionally */
    1976             :                                 default:
    1977           8 :                                         error = curl_easy_setopt(ch->cp, option, fp);
    1978             :                                         break;
    1979             :                         }
    1980             : 
    1981        1047 :                         break;
    1982             :                 }
    1983             :                 case CURLOPT_RETURNTRANSFER:
    1984          82 :                         convert_to_long_ex(zvalue);
    1985             : 
    1986          49 :                         if (Z_LVAL_PP(zvalue)) {
    1987          48 :                                 ch->handlers->write->method = PHP_CURL_RETURN;
    1988             :                         } else {
    1989           1 :                                 ch->handlers->write->method = PHP_CURL_STDOUT;
    1990             :                         }
    1991          49 :                         break;
    1992             :                 case CURLOPT_BINARYTRANSFER:
    1993           6 :                         convert_to_long_ex(zvalue);
    1994             : 
    1995           1 :                         if (Z_LVAL_PP(zvalue)) {
    1996           1 :                                 ch->handlers->write->type = PHP_CURL_BINARY;
    1997             :                         } else {
    1998           0 :                                 ch->handlers->write->type = PHP_CURL_ASCII;
    1999             :                         }
    2000           1 :                         break;
    2001             :                 case CURLOPT_WRITEFUNCTION:
    2002           2 :                         if (ch->handlers->write->func_name) {
    2003           0 :                                 zval_ptr_dtor(&ch->handlers->write->func_name);
    2004           0 :                                 ch->handlers->write->fci_cache = empty_fcall_info_cache;
    2005             :                         }
    2006           2 :                         zval_add_ref(zvalue);
    2007           2 :                         ch->handlers->write->func_name = *zvalue;
    2008           2 :                         ch->handlers->write->method = PHP_CURL_USER;
    2009           2 :                         break;
    2010             :                 case CURLOPT_READFUNCTION:
    2011           1 :                         if (ch->handlers->read->func_name) {
    2012           0 :                                 zval_ptr_dtor(&ch->handlers->read->func_name);
    2013           0 :                                 ch->handlers->read->fci_cache = empty_fcall_info_cache;
    2014             :                         }
    2015           1 :                         zval_add_ref(zvalue);
    2016           1 :                         ch->handlers->read->func_name = *zvalue;
    2017           1 :                         ch->handlers->read->method = PHP_CURL_USER;
    2018           1 :                         break;
    2019             :                 case CURLOPT_PROGRESSFUNCTION:
    2020           1 :                         curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,   curl_progress);
    2021           1 :                         curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
    2022           1 :                         if (ch->handlers->progress->func_name) {
    2023           0 :                                 zval_ptr_dtor(&ch->handlers->progress->func_name);
    2024           0 :                                 ch->handlers->progress->fci_cache = empty_fcall_info_cache;
    2025             :                         }
    2026           1 :                         zval_add_ref(zvalue);
    2027           1 :                         ch->handlers->progress->func_name = *zvalue;
    2028           1 :                         ch->handlers->progress->method = PHP_CURL_USER;
    2029           1 :                         break;
    2030             :                 case CURLOPT_HEADERFUNCTION:
    2031           1 :                         if (ch->handlers->write_header->func_name) {
    2032           0 :                                 zval_ptr_dtor(&ch->handlers->write_header->func_name);
    2033           0 :                                 ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
    2034             :                         }
    2035           1 :                         zval_add_ref(zvalue);
    2036           1 :                         ch->handlers->write_header->func_name = *zvalue;
    2037           1 :                         ch->handlers->write_header->method = PHP_CURL_USER;
    2038           1 :                         break;
    2039             : #if CURLOPT_PASSWDFUNCTION != 0
    2040             :                 case CURLOPT_PASSWDFUNCTION:
    2041             :                         if (ch->handlers->passwd) {
    2042             :                                 zval_ptr_dtor(&ch->handlers->passwd);
    2043             :                         }
    2044             :                         zval_add_ref(zvalue);
    2045             :                         ch->handlers->passwd = *zvalue;
    2046             :                         error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
    2047             :                         error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
    2048             :                         break;
    2049             : #endif
    2050             :                 case CURLOPT_POSTFIELDS:
    2051          17 :                         if (Z_TYPE_PP(zvalue) == IS_ARRAY || Z_TYPE_PP(zvalue) == IS_OBJECT) {
    2052             :                                 zval            **current;
    2053             :                                 HashTable        *postfields;
    2054           7 :                                 struct HttpPost  *first = NULL;
    2055           7 :                                 struct HttpPost  *last  = NULL;
    2056             : 
    2057           7 :                                 postfields = HASH_OF(*zvalue);
    2058           7 :                                 if (!postfields) {
    2059           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
    2060           0 :                                         RETVAL_FALSE;
    2061           0 :                                         return 1;
    2062             :                                 }
    2063             : 
    2064          25 :                                 for (zend_hash_internal_pointer_reset(postfields);
    2065          18 :                                          zend_hash_get_current_data(postfields, (void **) &current) == SUCCESS;
    2066             :                                          zend_hash_move_forward(postfields)
    2067          11 :                                 ) {
    2068             :                                         char *postval;
    2069          11 :                                         char *string_key = NULL;
    2070             :                                         uint  string_key_len;
    2071             :                                         ulong num_key;
    2072             :                                         int numeric_key;
    2073             : 
    2074          22 :                                         SEPARATE_ZVAL(current);
    2075          11 :                                         convert_to_string_ex(current);
    2076             : 
    2077          11 :                                         zend_hash_get_current_key_ex(postfields, &string_key, &string_key_len, &num_key, 0, NULL);
    2078             : 
    2079             :                                         /* Pretend we have a string_key here */
    2080          11 :                                         if(!string_key) {
    2081           1 :                                                 spprintf(&string_key, 0, "%ld", num_key);
    2082           1 :                                                 string_key_len = strlen(string_key)+1;
    2083           1 :                                                 numeric_key = 1;
    2084             :                                         } else {
    2085          10 :                                                 numeric_key = 0;
    2086             :                                         }
    2087             : 
    2088          11 :                                         postval = Z_STRVAL_PP(current);
    2089             : 
    2090             :                                         /* The arguments after _NAMELENGTH and _CONTENTSLENGTH
    2091             :                                          * must be explicitly cast to long in curl_formadd
    2092             :                                          * use since curl needs a long not an int. */
    2093          11 :                                         if (*postval == '@') {
    2094             :                                                 char *type, *filename;
    2095           5 :                                                 ++postval;
    2096             : 
    2097           5 :                                                 if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, postval + Z_STRLEN_PP(current)))) {
    2098           3 :                                                         *type = '\0';
    2099             :                                                 }
    2100           5 :                                                 if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, postval + Z_STRLEN_PP(current)))) {
    2101           3 :                                                         *filename = '\0';
    2102             :                                                 }
    2103             :                                                 /* open_basedir check */
    2104           5 :                                                 if (php_check_open_basedir(postval TSRMLS_CC)) {
    2105           0 :                                                         RETVAL_FALSE;
    2106           0 :                                                         return 1;
    2107             :                                                 }
    2108           5 :                                                 error = curl_formadd(&first, &last,
    2109             :                                                                                 CURLFORM_COPYNAME, string_key,
    2110             :                                                                                 CURLFORM_NAMELENGTH, (long)string_key_len - 1,
    2111             :                                                                                 CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval,
    2112             :                                                                                 CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream",
    2113             :                                                                                 CURLFORM_FILE, postval,
    2114             :                                                                                 CURLFORM_END);
    2115           5 :                                                 if (type) {
    2116           3 :                                                         *type = ';';
    2117             :                                                 }
    2118           5 :                                                 if (filename) {
    2119           3 :                                                         *filename = ';';
    2120             :                                                 }
    2121             :                                         } else {
    2122           6 :                                                 error = curl_formadd(&first, &last,
    2123             :                                                                                          CURLFORM_COPYNAME, string_key,
    2124             :                                                                                          CURLFORM_NAMELENGTH, (long)string_key_len - 1,
    2125             :                                                                                          CURLFORM_COPYCONTENTS, postval,
    2126           6 :                                                                                          CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current),
    2127             :                                                                                          CURLFORM_END);
    2128             :                                         }
    2129             : 
    2130          11 :                                         if (numeric_key) {
    2131           1 :                                                 efree(string_key);
    2132             :                                         }
    2133             :                                 }
    2134             : 
    2135           7 :                                 SAVE_CURL_ERROR(ch, error);
    2136           7 :                                 if (error != CURLE_OK) {
    2137           0 :                                         RETVAL_FALSE;
    2138           0 :                                         return 1;
    2139             :                                 }
    2140             : 
    2141          14 :                                 if (Z_REFCOUNT_P(ch->clone) <= 1) {
    2142           7 :                                         zend_llist_clean(&ch->to_free->post);
    2143             :                                 }
    2144           7 :                                 zend_llist_add_element(&ch->to_free->post, &first);
    2145           7 :                                 error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
    2146             : 
    2147             :                         } else {
    2148             : #if LIBCURL_VERSION_NUM >= 0x071101
    2149           3 :                                 convert_to_string_ex(zvalue);
    2150             :                                 /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
    2151           3 :                                 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
    2152           3 :                                 error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue));
    2153             : #else
    2154             :                                 char *post = NULL;
    2155             : 
    2156             :                                 convert_to_string_ex(zvalue);
    2157             :                                 post = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
    2158             :                                 zend_llist_add_element(&ch->to_free->str, &post);
    2159             : 
    2160             :                                 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
    2161             :                                 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
    2162             : #endif
    2163             :                         }
    2164          10 :                         break;
    2165             :                 case CURLOPT_HTTPHEADER:
    2166             :                 case CURLOPT_QUOTE:
    2167             :                 case CURLOPT_HTTP200ALIASES:
    2168             :                 case CURLOPT_POSTQUOTE: {
    2169             :                         zval              **current;
    2170             :                         HashTable          *ph;
    2171       10004 :                         struct curl_slist  *slist = NULL;
    2172             : 
    2173       10004 :                         ph = HASH_OF(*zvalue);
    2174       10004 :                         if (!ph) {
    2175           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must pass either an object or an array with the CURLOPT_HTTPHEADER, CURLOPT_QUOTE, CURLOPT_HTTP200ALIASES and CURLOPT_POSTQUOTE arguments");
    2176           1 :                                 RETVAL_FALSE;
    2177           1 :                                 return 1;
    2178             :                         }
    2179             : 
    2180       30009 :                         for (zend_hash_internal_pointer_reset(ph);
    2181       20006 :                                  zend_hash_get_current_data(ph, (void **) &current) == SUCCESS;
    2182             :                                  zend_hash_move_forward(ph)
    2183       10003 :                         ) {
    2184       20006 :                                 SEPARATE_ZVAL(current);
    2185       10003 :                                 convert_to_string_ex(current);
    2186             : 
    2187       10003 :                                 slist = curl_slist_append(slist, Z_STRVAL_PP(current));
    2188       10003 :                                 if (!slist) {
    2189           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist");
    2190           0 :                                         RETVAL_FALSE;
    2191           0 :                                         return 1;
    2192             :                                 }
    2193             :                         }
    2194       10003 :                         zend_hash_index_update(ch->to_free->slist, (ulong) option, &slist, sizeof(struct curl_slist *), NULL);
    2195             : 
    2196       10003 :                         error = curl_easy_setopt(ch->cp, option, slist);
    2197             : 
    2198       10003 :                         break;
    2199             :                 }
    2200             :                 /* the following options deal with files, therefore the open_basedir check
    2201             :                  * is required.
    2202             :                  */
    2203             :                 case CURLOPT_COOKIEJAR:
    2204             :                 case CURLOPT_SSLCERT:
    2205             :                 case CURLOPT_RANDOM_FILE:
    2206             :                 case CURLOPT_COOKIEFILE: {
    2207             : #if LIBCURL_VERSION_NUM < 0x071100
    2208             :                         char *copystr = NULL;
    2209             : #endif
    2210             : 
    2211           3 :                         convert_to_string_ex(zvalue);
    2212             : 
    2213           3 :                         if (Z_STRLEN_PP(zvalue) && php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) {
    2214           1 :                                 RETVAL_FALSE;
    2215           1 :                                 return 1;
    2216             :                         }
    2217             : 
    2218             : #if LIBCURL_VERSION_NUM >= 0x071100
    2219           2 :                         error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
    2220             : #else
    2221             :                         copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
    2222             : 
    2223             :                         error = curl_easy_setopt(ch->cp, option, copystr);
    2224             :                         zend_llist_add_element(&ch->to_free->str, &copystr);
    2225             : #endif
    2226           2 :                         break;
    2227             :                 }
    2228             :                 case CURLINFO_HEADER_OUT:
    2229           0 :                         convert_to_long_ex(zvalue);
    2230           0 :                         if (Z_LVAL_PP(zvalue) == 1) {
    2231           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
    2232           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
    2233           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
    2234             :                         } else {
    2235           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
    2236           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
    2237           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
    2238             :                         }
    2239             :                         break;
    2240             :         }
    2241             : 
    2242       12245 :         SAVE_CURL_ERROR(ch, error);
    2243       12245 :         if (error != CURLE_OK) {
    2244           0 :                 return 1;
    2245             :         } else {
    2246       12245 :                 return 0;
    2247             :         }
    2248             : }
    2249             : /* }}} */
    2250             : 
    2251             : /* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
    2252             :    Set an option for a cURL transfer */
    2253       12225 : PHP_FUNCTION(curl_setopt)
    2254             : {
    2255             :         zval       *zid, **zvalue;
    2256             :         long        options;
    2257             :         php_curl   *ch;
    2258             : 
    2259       12225 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) {
    2260          10 :                 return;
    2261             :         }
    2262             : 
    2263       12215 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2264             : 
    2265       12215 :         if (options <= 0) {
    2266           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl configuration option");
    2267           1 :                 RETURN_FALSE;
    2268             :         }
    2269             : 
    2270       12214 :         if (!_php_curl_setopt(ch, options, zvalue, return_value TSRMLS_CC)) {
    2271       12209 :                 RETURN_TRUE;
    2272             :         } else {
    2273           5 :                 RETURN_FALSE;
    2274             :         }
    2275             : }
    2276             : /* }}} */
    2277             : 
    2278             : /* {{{ proto bool curl_setopt_array(resource ch, array options)
    2279             :    Set an array of option for a cURL transfer */
    2280          12 : PHP_FUNCTION(curl_setopt_array)
    2281             : {
    2282             :         zval            *zid, *arr, **entry;
    2283             :         php_curl        *ch;
    2284             :         ulong           option;
    2285             :         HashPosition    pos;
    2286             :         char            *string_key;
    2287             :         uint            str_key_len;
    2288             : 
    2289          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) {
    2290           0 :                 return;
    2291             :         }
    2292             : 
    2293          12 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2294             : 
    2295          12 :         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
    2296          60 :         while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
    2297          36 :                 if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) != HASH_KEY_IS_LONG) {
    2298           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent integer values");
    2299           0 :                         RETURN_FALSE;
    2300             :                 }
    2301          36 :                 if (_php_curl_setopt(ch, (long) option, entry, return_value TSRMLS_CC)) {
    2302           0 :                         RETURN_FALSE;
    2303             :                 }
    2304          36 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
    2305             :         }
    2306          12 :         RETURN_TRUE;
    2307             : }
    2308             : /* }}} */
    2309             : 
    2310             : /* {{{ _php_curl_cleanup_handle(ch)
    2311             :    Cleanup an execution phase */
    2312        1104 : void _php_curl_cleanup_handle(php_curl *ch)
    2313             : {
    2314        1104 :         if (ch->handlers->write->buf.len > 0) {
    2315           4 :                 smart_str_free(&ch->handlers->write->buf);
    2316             :         }
    2317        1104 :         if (ch->header.str_len) {
    2318           0 :                 efree(ch->header.str);
    2319           0 :                 ch->header.str_len = 0;
    2320             :         }
    2321             : 
    2322        1104 :         memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
    2323        1104 :         ch->err.no = 0;
    2324        1104 : }
    2325             : /* }}} */
    2326             : 
    2327             : /* {{{ proto bool curl_exec(resource ch)
    2328             :    Perform a cURL session */
    2329        1081 : PHP_FUNCTION(curl_exec)
    2330             : {
    2331             :         CURLcode        error;
    2332             :         zval            *zid;
    2333             :         php_curl        *ch;
    2334             : 
    2335        1081 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2336           0 :                 return;
    2337             :         }
    2338             : 
    2339        1081 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2340             : 
    2341        1081 :         _php_curl_verify_handlers(ch, 1 TSRMLS_CC);
    2342             : 
    2343        1081 :         _php_curl_cleanup_handle(ch);
    2344             : 
    2345        1081 :         error = curl_easy_perform(ch->cp);
    2346        1081 :         SAVE_CURL_ERROR(ch, error);
    2347             :         /* CURLE_PARTIAL_FILE is returned by HEAD requests */
    2348        1081 :         if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
    2349        1033 :                 if (ch->handlers->write->buf.len > 0) {
    2350           0 :                         smart_str_free(&ch->handlers->write->buf);
    2351             :                 }
    2352        1033 :                 RETURN_FALSE;
    2353             :         }
    2354             : 
    2355          48 :         if (ch->handlers->std_err) {
    2356             :                 php_stream  *stream;
    2357           2 :                 stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
    2358           2 :                 if (stream) {
    2359           2 :                         php_stream_flush(stream);
    2360             :                 }
    2361             :         }
    2362             : 
    2363          48 :         if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
    2364          33 :                 smart_str_0(&ch->handlers->write->buf);
    2365          33 :                 RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
    2366             :         }
    2367             : 
    2368             :         /* flush the file handle, so any remaining data is synched to disk */
    2369          15 :         if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
    2370           2 :                 fflush(ch->handlers->write->fp);
    2371             :         }
    2372          15 :         if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
    2373           0 :                 fflush(ch->handlers->write_header->fp);
    2374             :         }
    2375             : 
    2376          15 :         if (ch->handlers->write->method == PHP_CURL_RETURN) {
    2377           0 :                 RETURN_EMPTY_STRING();
    2378             :         } else {
    2379          15 :                 RETURN_TRUE;
    2380             :         }
    2381             : }
    2382             : /* }}} */
    2383             : 
    2384             : /* {{{ proto mixed curl_getinfo(resource ch [, int option])
    2385             :    Get information regarding a specific transfer */
    2386          18 : PHP_FUNCTION(curl_getinfo)
    2387             : {
    2388             :         zval            *zid;
    2389             :         php_curl        *ch;
    2390          18 :         long            option = 0;
    2391             : 
    2392          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zid, &option) == FAILURE) {
    2393           0 :                 return;
    2394             :         }
    2395             : 
    2396          18 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2397             : 
    2398          18 :         if (ZEND_NUM_ARGS() < 2) {
    2399             :                 char   *s_code;
    2400             :                 long    l_code;
    2401             :                 double  d_code;
    2402             : #if LIBCURL_VERSION_NUM >  0x071301
    2403          13 :                 struct curl_certinfo *ci = NULL;
    2404             :                 zval *listcode;
    2405             : #endif
    2406             : 
    2407          13 :                 array_init(return_value);
    2408             : 
    2409          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
    2410          13 :                         CAAS("url", s_code);
    2411             :                 }
    2412          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
    2413          13 :                         if (s_code != NULL) {
    2414           0 :                                 CAAS("content_type", s_code);
    2415             :                         } else {
    2416             :                                 zval *retnull;
    2417          13 :                                 MAKE_STD_ZVAL(retnull);
    2418          13 :                                 ZVAL_NULL(retnull);
    2419          13 :                                 CAAZ("content_type", retnull);
    2420             :                         }
    2421             :                 }
    2422          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
    2423          13 :                         CAAL("http_code", l_code);
    2424             :                 }
    2425          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
    2426          13 :                         CAAL("header_size", l_code);
    2427             :                 }
    2428          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
    2429          13 :                         CAAL("request_size", l_code);
    2430             :                 }
    2431          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
    2432          13 :                         CAAL("filetime", l_code);
    2433             :                 }
    2434          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
    2435          13 :                         CAAL("ssl_verify_result", l_code);
    2436             :                 }
    2437          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
    2438          13 :                         CAAL("redirect_count", l_code);
    2439             :                 }
    2440          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
    2441          13 :                         CAAD("total_time", d_code);
    2442             :                 }
    2443          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
    2444          13 :                         CAAD("namelookup_time", d_code);
    2445             :                 }
    2446          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
    2447          13 :                         CAAD("connect_time", d_code);
    2448             :                 }
    2449          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
    2450          13 :                         CAAD("pretransfer_time", d_code);
    2451             :                 }
    2452          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
    2453          13 :                         CAAD("size_upload", d_code);
    2454             :                 }
    2455          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
    2456          13 :                         CAAD("size_download", d_code);
    2457             :                 }
    2458          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
    2459          13 :                         CAAD("speed_download", d_code);
    2460             :                 }
    2461          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
    2462          13 :                         CAAD("speed_upload", d_code);
    2463             :                 }
    2464          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
    2465          13 :                         CAAD("download_content_length", d_code);
    2466             :                 }
    2467          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
    2468          13 :                         CAAD("upload_content_length", d_code);
    2469             :                 }
    2470          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
    2471          13 :                         CAAD("starttransfer_time", d_code);
    2472             :                 }
    2473          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
    2474          13 :                         CAAD("redirect_time", d_code);
    2475             :                 }
    2476             : #if LIBCURL_VERSION_NUM > 0x071301
    2477          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
    2478          13 :                         MAKE_STD_ZVAL(listcode);
    2479          13 :                         array_init(listcode);
    2480          13 :                         create_certinfo(ci, listcode TSRMLS_CC);
    2481          13 :                         CAAZ("certinfo", listcode);
    2482             :                 }
    2483             : #endif
    2484             : #if LIBCURL_VERSION_NUM >= 0x071300 /* 7.19.0 */
    2485          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
    2486          13 :                         CAAS("primary_ip", s_code);
    2487             :                 }
    2488             : #endif
    2489             : #if LIBCURL_VERSION_NUM > 0x071500
    2490             :                 if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
    2491             :                         CAAL("primary_port", l_code);
    2492             :                 }
    2493             :                 if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
    2494             :                         CAAS("local_ip", s_code);
    2495             :                 }
    2496             :                 if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
    2497             :                         CAAL("local_port", l_code);
    2498             :                 }
    2499             : #endif
    2500             : #if LIBCURL_VERSION_NUM >= 0x071202
    2501          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
    2502          13 :                         CAAS("redirect_url", s_code);
    2503             :                 }
    2504             : #endif
    2505          13 :                 if (ch->header.str_len > 0) {
    2506           0 :                         CAAS("request_header", ch->header.str);
    2507             :                 }
    2508             :         } else {
    2509           5 :                 switch (option) {
    2510             :                         /* string variable types */
    2511             : #if LIBCURL_VERSION_NUM >= 0x071300 /* 7.19.0 */
    2512             :                         case CURLINFO_PRIMARY_IP:
    2513             : #endif
    2514             : #if LIBCURL_VERSION_NUM >= 0x071500 /* 7.21.0 */
    2515             :                         case CURLINFO_LOCAL_IP:
    2516             : #endif
    2517             :                         case CURLINFO_PRIVATE:
    2518             :                         case CURLINFO_EFFECTIVE_URL:
    2519             :                         case CURLINFO_CONTENT_TYPE:
    2520             : #if LIBCURL_VERSION_NUM >= 0x071202
    2521             :                         case CURLINFO_REDIRECT_URL:
    2522             : #endif
    2523             :                         {
    2524           4 :                                 char *s_code = NULL;
    2525             : 
    2526           4 :                                 if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
    2527           4 :                                         RETURN_STRING(s_code, 1);
    2528             :                                 } else {
    2529           0 :                                         RETURN_FALSE;
    2530             :                                 }
    2531             :                                 break;
    2532             :                         }
    2533             :                         /* Long variable types */
    2534             : #if LIBCURL_VERSION_NUM >= 0x071500 /* 7.21.0 */
    2535             :                         case CURLINFO_PRIMARY_PORT:
    2536             :                         case CURLINFO_LOCAL_PORT:
    2537             : #endif
    2538             :                         case CURLINFO_HTTP_CODE:
    2539             :                         case CURLINFO_HEADER_SIZE:
    2540             :                         case CURLINFO_REQUEST_SIZE:
    2541             :                         case CURLINFO_FILETIME:
    2542             :                         case CURLINFO_SSL_VERIFYRESULT:
    2543             :                         case CURLINFO_REDIRECT_COUNT: {
    2544           1 :                                 long code = 0;
    2545             : 
    2546           1 :                                 if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
    2547           1 :                                         RETURN_LONG(code);
    2548             :                                 } else {
    2549           0 :                                         RETURN_FALSE;
    2550             :                                 }
    2551             :                                 break;
    2552             :                         }
    2553             :                         /* Double variable types */
    2554             :                         case CURLINFO_TOTAL_TIME:
    2555             :                         case CURLINFO_NAMELOOKUP_TIME:
    2556             :                         case CURLINFO_CONNECT_TIME:
    2557             :                         case CURLINFO_PRETRANSFER_TIME:
    2558             :                         case CURLINFO_SIZE_UPLOAD:
    2559             :                         case CURLINFO_SIZE_DOWNLOAD:
    2560             :                         case CURLINFO_SPEED_DOWNLOAD:
    2561             :                         case CURLINFO_SPEED_UPLOAD:
    2562             :                         case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
    2563             :                         case CURLINFO_CONTENT_LENGTH_UPLOAD:
    2564             :                         case CURLINFO_STARTTRANSFER_TIME:
    2565             :                         case CURLINFO_REDIRECT_TIME: {
    2566           0 :                                 double code = 0.0;
    2567             : 
    2568           0 :                                 if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
    2569           0 :                                         RETURN_DOUBLE(code);
    2570             :                                 } else {
    2571           0 :                                         RETURN_FALSE;
    2572             :                                 }
    2573             :                                 break;
    2574             :                         }
    2575             :                         case CURLINFO_HEADER_OUT:
    2576           0 :                                 if (ch->header.str_len > 0) {
    2577           0 :                                         RETURN_STRINGL(ch->header.str, ch->header.str_len, 1);
    2578             :                                 } else {
    2579           0 :                                         RETURN_FALSE;
    2580             :                                 }
    2581             : #if LIBCURL_VERSION_NUM > 0x071301
    2582             :                         case CURLINFO_CERTINFO: {
    2583           0 :                                 struct curl_certinfo *ci = NULL;
    2584             : 
    2585           0 :                                 array_init(return_value);
    2586             :                                 
    2587           0 :                                 if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
    2588           0 :                                         create_certinfo(ci, return_value TSRMLS_CC);
    2589             :                                 } else {
    2590           0 :                                         RETURN_FALSE;
    2591             :                                 }
    2592             :                                 break;
    2593             :                         }
    2594             : #endif
    2595             :                 }
    2596             :         }
    2597             : }
    2598             : /* }}} */
    2599             : 
    2600             : /* {{{ proto string curl_error(resource ch)
    2601             :    Return a string contain the last error for the current session */
    2602           5 : PHP_FUNCTION(curl_error)
    2603             : {
    2604             :         zval            *zid;
    2605             :         php_curl        *ch;
    2606             : 
    2607           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2608           0 :                 return;
    2609             :         }
    2610             : 
    2611           5 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2612             : 
    2613           5 :         ch->err.str[CURL_ERROR_SIZE] = 0;
    2614           5 :         RETURN_STRING(ch->err.str, 1);
    2615             : }
    2616             : /* }}} */
    2617             : 
    2618             : /* {{{ proto int curl_errno(resource ch)
    2619             :    Return an integer containing the last error number */
    2620           4 : PHP_FUNCTION(curl_errno)
    2621             : {
    2622             :         zval            *zid;
    2623             :         php_curl        *ch;
    2624             : 
    2625           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2626           0 :                 return;
    2627             :         }
    2628             : 
    2629           4 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2630             : 
    2631           4 :         RETURN_LONG(ch->err.no);
    2632             : }
    2633             : /* }}} */
    2634             : 
    2635             : /* {{{ proto void curl_close(resource ch)
    2636             :    Close a cURL session */
    2637          56 : PHP_FUNCTION(curl_close)
    2638             : {
    2639             :         zval            *zid;
    2640             :         php_curl        *ch;
    2641             : 
    2642          56 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2643           0 :                 return;
    2644             :         }
    2645             : 
    2646          56 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2647             : 
    2648          56 :         if (ch->in_callback) {
    2649           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close cURL handle from a callback");
    2650           0 :                 return;
    2651             :         }
    2652             : 
    2653          56 :         if (ch->uses) {
    2654           6 :                 ch->uses--;
    2655             :         } else {
    2656          50 :                 zend_list_delete(Z_LVAL_P(zid));
    2657             :         }
    2658             : }
    2659             : /* }}} */
    2660             : 
    2661             : /* {{{ _php_curl_close()
    2662             :    List destructor for curl handles */
    2663         100 : static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
    2664             : {
    2665             : #if PHP_CURL_DEBUG
    2666             :         fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
    2667             : #endif
    2668             : 
    2669         100 :         _php_curl_verify_handlers(ch, 0 TSRMLS_CC);
    2670             : 
    2671             :         /* 
    2672             :          * Libcurl is doing connection caching. When easy handle is cleaned up,
    2673             :          * if the handle was previously used by the curl_multi_api, the connection 
    2674             :          * remains open un the curl multi handle is cleaned up. Some protocols are
    2675             :          * sending content like the FTP one, and libcurl try to use the 
    2676             :          * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
    2677             :          * callback are freed, we need to use an other callback to which avoid
    2678             :          * segfaults.
    2679             :          *
    2680             :          * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2 
    2681             :          */
    2682         100 :         curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
    2683         100 :         curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
    2684             : 
    2685         100 :         curl_easy_cleanup(ch->cp);
    2686             : 
    2687             :         /* cURL destructors should be invoked only by last curl handle */
    2688         200 :         if (Z_REFCOUNT_P(ch->clone) <= 1) {
    2689          89 :                 zend_llist_clean(&ch->to_free->str);
    2690          89 :                 zend_llist_clean(&ch->to_free->post);
    2691          89 :                 zend_hash_destroy(ch->to_free->slist);
    2692          89 :                 efree(ch->to_free->slist);
    2693          89 :                 efree(ch->to_free);
    2694          89 :                 FREE_ZVAL(ch->clone);
    2695             :         } else {
    2696          11 :                 Z_DELREF_P(ch->clone);
    2697             :         }
    2698             : 
    2699         100 :         if (ch->handlers->write->buf.len > 0) {
    2700          47 :                 smart_str_free(&ch->handlers->write->buf);
    2701             :         }
    2702         100 :         if (ch->handlers->write->func_name) {
    2703           2 :                 zval_ptr_dtor(&ch->handlers->write->func_name);
    2704             :         }
    2705         100 :         if (ch->handlers->read->func_name) {
    2706           1 :                 zval_ptr_dtor(&ch->handlers->read->func_name);
    2707             :         }
    2708         100 :         if (ch->handlers->write_header->func_name) {
    2709           1 :                 zval_ptr_dtor(&ch->handlers->write_header->func_name);
    2710             :         }
    2711         100 :         if (ch->handlers->progress->func_name) {
    2712           2 :                 zval_ptr_dtor(&ch->handlers->progress->func_name);
    2713             :         }
    2714         100 :         if (ch->handlers->passwd) {
    2715           0 :                 zval_ptr_dtor(&ch->handlers->passwd);
    2716             :         }
    2717         100 :         if (ch->handlers->std_err) {
    2718           1 :                 zval_ptr_dtor(&ch->handlers->std_err);
    2719             :         }
    2720         100 :         if (ch->header.str_len > 0) {
    2721           0 :                 efree(ch->header.str);
    2722             :         }
    2723             : 
    2724         100 :         if (ch->handlers->write_header->stream) {
    2725           0 :                 zval_ptr_dtor(&ch->handlers->write_header->stream);
    2726             :         }
    2727         100 :         if (ch->handlers->write->stream) {
    2728           5 :                 zval_ptr_dtor(&ch->handlers->write->stream);
    2729             :         }
    2730         100 :         if (ch->handlers->read->stream) {
    2731           2 :                 zval_ptr_dtor(&ch->handlers->read->stream);
    2732             :         }
    2733             : 
    2734         100 :         efree(ch->handlers->write);
    2735         100 :         efree(ch->handlers->write_header);
    2736         100 :         efree(ch->handlers->read);
    2737         100 :         efree(ch->handlers->progress);
    2738         100 :         efree(ch->handlers);
    2739         100 :         efree(ch);
    2740         100 : }
    2741             : /* }}} */
    2742             : 
    2743             : /* {{{ _php_curl_close()
    2744             :    List destructor for curl handles */
    2745         100 : static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
    2746             : {
    2747         100 :         php_curl *ch = (php_curl *) rsrc->ptr;
    2748         100 :         _php_curl_close_ex(ch TSRMLS_CC);
    2749         100 : }
    2750             : /* }}} */
    2751             : 
    2752             : #endif /* HAVE_CURL */
    2753             : 
    2754             : /*
    2755             :  * Local variables:
    2756             :  * tab-width: 4
    2757             :  * c-basic-offset: 4
    2758             :  * End:
    2759             :  * vim600: fdm=marker
    2760             :  * vim: noet sw=4 ts=4
    2761             :  */

Generated by: LCOV version 1.10

Generated at Sun, 22 Mar 2015 22:19:49 +0000 (7 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.