PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 1.2.4
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v1.2.4
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / lib / Google / IO / Curl.php

Curl.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) 1.2.4, at lib/Google/IO/Curl.php

141 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2014 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /**
19 * Curl based implementation of Analytify_Google_IO.
20 *
21 * @author Stuart Langley <slangley@google.com>
22 */
23
24 require_once ANALYTIFY_LIB_PATH . 'Google/IO/Abstract.php';
25
26 class Analytify_Google_IO_Curl extends Analytify_Google_IO_Abstract
27 {
28 // cURL hex representation of version 7.30.0
29 const NO_QUIRK_VERSION = 0x071E00;
30
31 private $options = array();
32 /**
33 * Execute an HTTP Request
34 *
35 * @param Analytify_Google_HttpRequest $request the http request to be executed
36 * @return Analytify_Google_HttpRequest http request with the response http code,
37 * response headers and response body filled in
38 * @throws Analytify_Google_IO_Exception on curl or IO error
39 */
40 public function executeRequest(Analytify_Google_Http_Request $request)
41 {
42 $curl = curl_init();
43
44 if ($request->getPostBody()) {
45 curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
46 }
47
48 $requestHeaders = $request->getRequestHeaders();
49 if ($requestHeaders && is_array($requestHeaders)) {
50 $curlHeaders = array();
51 foreach ($requestHeaders as $k => $v) {
52 $curlHeaders[] = "$k: $v";
53 }
54 curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
55 }
56
57 curl_setopt($curl, CURLOPT_URL, $request->getUrl());
58
59 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
60 curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
61
62 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
63 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
64 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
65 curl_setopt($curl, CURLOPT_HEADER, true);
66
67 if ($request->canGzip()) {
68 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
69 }
70
71 foreach ($this->options as $key => $var) {
72 curl_setopt($curl, $key, $var);
73 }
74
75 if (!isset($this->options[CURLOPT_CAINFO])) {
76 curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
77 }
78
79 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
80
81 $response = curl_exec($curl);
82 if ($response === false) {
83
84 throw new Analytify_Google_IO_Exception( curl_error( $curl) );
85 }
86 $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
87
88 list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
89
90 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
91
92 return array( $responseBody, $responseHeaders, $responseCode );
93 }
94
95 /**
96 * Set options that update the transport implementation's behavior.
97 * @param $options
98 */
99 public function setOptions( $options )
100 {
101 $this->options = $options + $this->options;
102 }
103
104 /**
105 * Set the maximum request time in seconds.
106 * @param $timeout in seconds
107 */
108 public function setTimeout($timeout)
109 {
110 // Since this timeout is really for putting a bound on the time
111 // we'll set them both to the same. If you need to specify a longer
112 // CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
113 // do is use the setOptions method for the values individually.
114 $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
115 $this->options[CURLOPT_TIMEOUT] = $timeout;
116 }
117
118 /**
119 * Get the maximum request time in seconds.
120 * @return timeout in seconds
121 */
122 public function getTimeout()
123 {
124 return $this->options[CURLOPT_TIMEOUT];
125 }
126
127 /**
128 * Test for the presence of a cURL header processing bug
129 *
130 * {@inheritDoc}
131 *
132 * @return boolean
133 */
134 protected function needsQuirk()
135 {
136 $ver = curl_version();
137 $versionNum = $ver['version_number'];
138 return $versionNum < Analytify_Google_IO_Curl::NO_QUIRK_VERSION;
139 }
140 }
141