Ejemplo n.º 1
0
 /**
  * @ignore
  */
 public function setInternalOptions(&$success, $cookiesFp = null, $newCookieSession = null)
 {
     $success = true;
     if ($this->m_hasError) {
         $success = false;
         $this->finalize();
         return;
     }
     $options = CMap::make();
     // How to deal with the response.
     if (!$this->m_echoResponse) {
         $options[CURLOPT_RETURNTRANSFER] = true;
         $this->m_isReturnTransferSet = true;
     } else {
         $options[CURLOPT_RETURNTRANSFER] = false;
         $this->m_isReturnTransferSet = false;
     }
     if (isset($this->m_verboseOutput) && $this->m_verboseOutput) {
         $options[CURLOPT_VERBOSE] = true;
     }
     // The destination URL and port.
     $options[CURLOPT_URL] = $this->m_url;
     if (isset($this->m_port)) {
         $options[CURLOPT_PORT] = $this->m_port;
     }
     // Avoid response caching and reuse, which might happen because of any unconventional caching strategies used
     // by cURL.
     $options[CURLOPT_FORBID_REUSE] = true;
     $options[CURLOPT_FRESH_CONNECT] = true;
     if ($this->m_type != self::HTTP_HEAD) {
         $options[CURLOPT_HEADER] = false;
     }
     if ($this->isHttp()) {
         if ($this->m_type == self::HTTP_GET) {
             $options[CURLOPT_HTTPGET] = true;
         } else {
             if ($this->m_type == self::HTTP_DOWNLOAD || $this->m_type == self::ANY_DOWNLOAD) {
                 $options[CURLOPT_HTTPGET] = true;
                 assert('isset($this->m_downloadDestinationFp)', vs(isset($this), get_defined_vars()));
                 $this->m_downloadFile = new CFile($this->m_downloadDestinationFp, CFile::WRITE_NEW);
                 $options[CURLOPT_FILE] = $this->m_downloadFile->systemResource();
             } else {
                 if ($this->m_type == self::HTTP_POST) {
                     // POST.
                     $options[CURLOPT_POST] = true;
                     // At least one POST variable needs to be set in order to make a POST request.
                     assert('isset($this->m_postQuery)', vs(isset($this), get_defined_vars()));
                     // POST variables use the same format as the query string (application/x-www-form-urlencoded).
                     $options[CURLOPT_POSTFIELDS] = $this->m_postQuery->queryString();
                 } else {
                     if ($this->m_type == self::HTTP_UPLOAD) {
                         // File upload via POST and using the CURLFile class.
                         $options[CURLOPT_POST] = true;
                         assert('isset($this->m_postFileUploadRecord)', vs(isset($this), get_defined_vars()));
                         $options[CURLOPT_POSTFIELDS] = $this->m_postFileUploadRecord;
                     } else {
                         if ($this->m_type == self::HTTP_PUT) {
                             // PUT.
                             assert('isset($this->m_nonPostFileUploadFp)', vs(isset($this), get_defined_vars()));
                             $options[CURLOPT_PUT] = true;
                             $this->m_uploadFile = new CFile($this->m_nonPostFileUploadFp, CFile::READ);
                             $options[CURLOPT_INFILE] = $this->m_uploadFile->systemResource();
                             $options[CURLOPT_INFILESIZE] = CFile::size($this->m_nonPostFileUploadFp);
                         } else {
                             if ($this->m_type == self::HTTP_DELETE) {
                                 // DELETE.
                                 $options[CURLOPT_CUSTOMREQUEST] = "DELETE";
                             } else {
                                 if ($this->m_type == self::HTTP_HEAD) {
                                     // HEAD.
                                     $options[CURLOPT_HEADER] = true;
                                     $options[CURLOPT_NOBODY] = true;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // HTTP redirections.
         $options[CURLOPT_FOLLOWLOCATION] = $this->m_redirection;
         if ($this->m_redirection) {
             if (isset($this->m_maxNumRedirections)) {
                 $options[CURLOPT_MAXREDIRS] = $this->m_maxNumRedirections;
             }
             if (isset($this->m_redirectionAutoReferer)) {
                 $options[CURLOPT_AUTOREFERER] = $this->m_redirectionAutoReferer;
             }
             if (isset($this->m_redirectionKeepAuth)) {
                 $options[CURLOPT_UNRESTRICTED_AUTH] = $this->m_redirectionKeepAuth;
             }
         }
         // HTTP response code treatment.
         $options[CURLOPT_FAILONERROR] = $this->m_failOn400ResponseCodeOrGreater;
         // HTTP headers.
         if ($this->m_sendDefaultAcceptEncodingHeader && !(isset($this->m_requestHeaders) && $this->hasHeader(CHttpRequest::ACCEPT_ENCODING))) {
             $options[CURLOPT_ENCODING] = "";
         }
         if ($this->m_sendDefaultUserAgentHeader && !(isset($this->m_requestHeaders) && $this->hasHeader(CHttpRequest::USER_AGENT))) {
             $userAgent = self::$ms_defaultUserAgent;
             $curlVersion = self::curlVersion();
             $sslVersion = self::openSslVersion();
             $sslVersion = CRegex::remove($sslVersion, "/OpenSSL(\\/|\\h+)/i");
             $userAgent = CString::replaceCi($userAgent, "curl/x.x.x", "curl/{$curlVersion}");
             $userAgent = CString::replaceCi($userAgent, "libcurl x.x.x", "libcurl {$curlVersion}");
             $userAgent = CString::replaceCi($userAgent, "OpenSSL x.x.x", "OpenSSL {$sslVersion}");
             $this->addHeader(CHttpRequest::USER_AGENT, $userAgent);
         }
         if (isset($this->m_requestHeaders) && !CArray::isEmpty($this->m_requestHeaders)) {
             $options[CURLOPT_HTTPHEADER] = CArray::toPArray($this->m_requestHeaders);
         }
         if (isset($this->m_requestCookies) && !CArray::isEmpty($this->m_requestCookies)) {
             // Custom HTTP cookies.
             $cookieHeaderValue = CArray::join($this->m_requestCookies, "; ");
             $options[CURLOPT_COOKIE] = $cookieHeaderValue;
         }
         if (isset($cookiesFp)) {
             $options[CURLOPT_COOKIEFILE] = $cookiesFp;
             $options[CURLOPT_COOKIEJAR] = $cookiesFp;
         }
         if (isset($newCookieSession) && $newCookieSession) {
             $options[CURLOPT_COOKIESESSION] = true;
         }
         // Needed for the retrieval of information regarding the data transfer after it is complete.
         $options[CURLINFO_HEADER_OUT] = true;
         // Needed for the retrieval of response headers.
         $options[CURLOPT_HEADERFUNCTION] = [$this, "headerFunction"];
         if (isset($this->m_userAndPassword)) {
             // HTTP authentication. Let cURL pick any authentication method it finds suitable (it will
             // automatically select the one it finds most secure).
             $options[CURLOPT_HTTPAUTH] = CURLAUTH_ANY;
         }
     } else {
         if ($this->m_type == self::FTP_LIST) {
             $options[CURLOPT_FTPLISTONLY] = true;
         } else {
             if ($this->m_type == self::FTP_DOWNLOAD || $this->m_type == self::ANY_DOWNLOAD) {
                 assert('isset($this->m_downloadDestinationFp)', vs(isset($this), get_defined_vars()));
                 $this->m_downloadFile = new CFile($this->m_downloadDestinationFp, CFile::WRITE_NEW);
                 $options[CURLOPT_FILE] = $this->m_downloadFile->systemResource();
             } else {
                 if ($this->m_type == self::FTP_UPLOAD) {
                     // File upload via FTP.
                     assert('isset($this->m_nonPostFileUploadFp)', vs(isset($this), get_defined_vars()));
                     $this->m_uploadFile = new CFile($this->m_nonPostFileUploadFp, CFile::READ);
                     $options[CURLOPT_UPLOAD] = true;
                     $options[CURLOPT_INFILE] = $this->m_uploadFile->systemResource();
                     $options[CURLOPT_INFILESIZE] = CFile::size($this->m_nonPostFileUploadFp);
                     if ($this->m_ftpCreateMissingDirectoriesForUpload) {
                         $options[CURLOPT_FTP_CREATE_MISSING_DIRS] = true;
                     }
                     if (isset($this->m_ftpAppendUpload) && $this->m_ftpAppendUpload) {
                         $options[CURLOPT_FTPAPPEND] = true;
                     }
                 }
             }
         }
         if (isset($this->m_ftpQuoteCommands) && !CArray::isEmpty($this->m_ftpQuoteCommands)) {
             $options[CURLOPT_QUOTE] = CArray::toPArray($this->m_ftpQuoteCommands);
         }
         if (isset($this->m_ftpPostQuoteCommands) && !CArray::isEmpty($this->m_ftpPostQuoteCommands)) {
             $options[CURLOPT_POSTQUOTE] = CArray::toPArray($this->m_ftpPostQuoteCommands);
         }
         if (isset($this->m_ftpUseEpsv) && !$this->m_ftpUseEpsv) {
             $options[CURLOPT_FTP_USE_EPSV] = false;
         }
         if (isset($this->m_ftpActiveModeBackAddress)) {
             $options[CURLOPT_FTPPORT] = $this->m_ftpActiveModeBackAddress;
         }
         if (isset($this->m_ftpUseEprt) && !$this->m_ftpUseEprt) {
             $options[CURLOPT_FTP_USE_EPRT] = false;
         }
     }
     // Timeouts.
     if (isset($this->m_requestTimeoutSeconds)) {
         $options[CURLOPT_TIMEOUT] = $this->m_requestTimeoutSeconds;
     }
     if (isset($this->m_connectionTimeoutSeconds)) {
         $options[CURLOPT_CONNECTTIMEOUT] = $this->m_connectionTimeoutSeconds;
     }
     if (isset($this->m_dnsCacheTimeoutSeconds)) {
         $options[CURLOPT_DNS_CACHE_TIMEOUT] = $this->m_dnsCacheTimeoutSeconds;
     }
     // The byte range(s) of interest.
     if (isset($this->m_requestedByteRange)) {
         $options[CURLOPT_RANGE] = $this->m_requestedByteRange;
     }
     // SSL certificate verification options.
     $options[CURLOPT_SSL_VERIFYPEER] = $this->m_certificateVerification;
     if (isset($this->m_alternateCertificateFpOrDp)) {
         if (CFile::isFile($this->m_alternateCertificateFpOrDp)) {
             $options[CURLOPT_CAINFO] = $this->m_alternateCertificateFpOrDp;
         } else {
             if (CFile::isDirectory($this->m_alternateCertificateFpOrDp)) {
                 $options[CURLOPT_CAPATH] = $this->m_alternateCertificateFpOrDp;
             } else {
                 assert('false', vs(isset($this), get_defined_vars()));
             }
         }
     }
     if (!$this->m_hostVerification) {
         // The default should be the highest setting, so only set this option to `0` if host verification is
         // disabled.
         $options[CURLOPT_SSL_VERIFYHOST] = 0;
     }
     if (isset($this->m_userAndPassword)) {
         $options[CURLOPT_USERPWD] = $this->m_userAndPassword;
     }
     // SSL options.
     if (isset($this->m_sslCertificateFp)) {
         $options[CURLOPT_SSLCERT] = $this->m_sslCertificateFp;
     }
     if (isset($this->m_sslPrivateKeyFp)) {
         $options[CURLOPT_SSLKEY] = $this->m_sslPrivateKeyFp;
     }
     if (isset($this->m_sslCertificateFormat)) {
         $options[CURLOPT_SSLCERTTYPE] = $this->m_sslCertificateFormat;
     }
     if (isset($this->m_sslPrivateKeyFormat)) {
         $options[CURLOPT_SSLKEYTYPE] = $this->m_sslPrivateKeyFormat;
     }
     if (isset($this->m_sslCertificatePassphrase)) {
         $options[CURLOPT_SSLCERTPASSWD] = $this->m_sslCertificatePassphrase;
     }
     if (isset($this->m_sslPrivateKeyPassphrase)) {
         $options[CURLOPT_SSLKEYPASSWD] = $this->m_sslPrivateKeyPassphrase;
     }
     if (isset($this->m_sslVersion)) {
         $options[CURLOPT_SSLVERSION] = $this->m_sslVersion;
     }
     if (isset($this->m_sslCipherList)) {
         $options[CURLOPT_SSL_CIPHER_LIST] = $this->m_sslCipherList;
     }
     if (isset($this->m_sslEngine)) {
         $options[CURLOPT_SSLENGINE] = $this->m_sslEngine;
     }
     if (isset($this->m_sslDefaultEngine)) {
         $options[CURLOPT_SSLENGINE_DEFAULT] = $this->m_sslDefaultEngine;
     }
     if (isset($this->m_useKerberos) && $this->m_useKerberos) {
         $options[CURLOPT_KRBLEVEL] = $this->m_kerberosLevel;
     }
     if (isset($this->m_proxyAddress)) {
         // Use a proxy.
         $options[CURLOPT_PROXY] = $this->m_proxyAddress;
         if (isset($this->m_proxyUserAndPassword)) {
             $options[CURLOPT_PROXYUSERPWD] = $this->m_proxyUserAndPassword;
         }
         if (isset($this->m_proxyType)) {
             $proxyType;
             switch ($this->m_proxyType) {
                 case self::PROXY_HTTP:
                     $proxyType = CURLPROXY_HTTP;
                     break;
                 case self::PROXY_SOCKS_5:
                     $proxyType = CURLPROXY_SOCKS5;
                     break;
                 default:
                     assert('false', vs(isset($this), get_defined_vars()));
                     break;
             }
             $options[CURLOPT_PROXYTYPE] = $proxyType;
         }
         if (isset($this->m_proxyPort)) {
             $options[CURLOPT_PROXYPORT] = $this->m_proxyPort;
         }
         if (isset($this->m_proxyTunneling) && $this->m_proxyTunneling) {
             $options[CURLOPT_HTTPPROXYTUNNEL] = true;
         }
         if (isset($this->m_proxyConnectOnly) && $this->m_proxyConnectOnly) {
             $options[CURLOPT_CONNECT_ONLY] = true;
         }
     }
     if (isset($this->m_outgoingInterface)) {
         $options[CURLOPT_INTERFACE] = $this->m_outgoingInterface;
     }
     // Speed limits.
     if (isset($this->m_maxDownloadSpeed)) {
         $options[CURLOPT_MAX_RECV_SPEED_LARGE] = $this->m_maxDownloadSpeed;
     }
     if (isset($this->m_maxUploadSpeed)) {
         $options[CURLOPT_MAX_SEND_SPEED_LARGE] = $this->m_maxUploadSpeed;
     }
     // Set cURL options.
     $res = curl_setopt_array($this->m_curl, $options);
     if (!$res || !CString::isEmpty(curl_error($this->m_curl))) {
         // Should never get in here as long as cURL options are being set correctly, hence the assertion.
         assert('false', vs(isset($this), get_defined_vars()));
         $this->m_hasError = true;
         $curlError = curl_error($this->m_curl);
         $this->m_errorMessage = !CString::isEmpty($curlError) ? $curlError : "The 'curl_setopt_array' function failed.";
         $success = false;
         $this->finalize();
         return;
     }
 }