PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.1
Elementor Website Builder – more than just a page builder v4.3.1
4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 All 454 releases
elementor / vendor_prefixed / mixpanel / lib / ConsumerStrategies / CurlConsumer.php

CurlConsumer.php in Elementor Website Builder – more than just a page builder 4.3.1, at vendor_prefixed/mixpanel/lib/ConsumerStrategies/CurlConsumer.php

221 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorDeps;
4
5 require_once \dirname(__FILE__) . "/AbstractConsumer.php";
6 /**
7 * Consumes messages and sends them to a host/endpoint using cURL
8 */
9 class ConsumerStrategies_CurlConsumer extends ConsumerStrategies_AbstractConsumer
10 {
11 /**
12 * @var string the host to connect to (e.g. api.mixpanel.com)
13 */
14 protected $_host;
15 /**
16 * @var string the host-relative endpoint to write to (e.g. /engage)
17 */
18 protected $_endpoint;
19 /**
20 * @var int connect_timeout The number of seconds to wait while trying to connect. Default is 5 seconds.
21 */
22 protected $_connect_timeout;
23 /**
24 * @var int timeout The maximum number of seconds to allow cURL call to execute. Default is 30 seconds.
25 */
26 protected $_timeout;
27 /**
28 * @var string the protocol to use for the cURL connection
29 */
30 protected $_protocol;
31 /**
32 * @var bool|null true to fork the cURL process (using exec) or false to use PHP's cURL extension. false by default
33 */
34 protected $_fork = null;
35 /**
36 * @var int number of cURL requests to run in parallel. 1 by default
37 */
38 protected $_num_threads;
39 /**
40 * Creates a new CurlConsumer and assigns properties from the $options array
41 * @param array $options
42 * @throws Exception
43 */
44 function __construct($options)
45 {
46 parent::__construct($options);
47 $this->_host = $options['host'];
48 $this->_endpoint = $options['endpoint'];
49 $this->_connect_timeout = isset($options['connect_timeout']) ? $options['connect_timeout'] : 5;
50 $this->_timeout = isset($options['timeout']) ? $options['timeout'] : 30;
51 $this->_protocol = isset($options['use_ssl']) && $options['use_ssl'] == \true ? "https" : "http";
52 $this->_fork = isset($options['fork']) ? $options['fork'] == \true : \false;
53 $this->_num_threads = isset($options['num_threads']) ? \max(1, \intval($options['num_threads'])) : 1;
54 // ensure the environment is workable for the given settings
55 if ($this->_fork == \true) {
56 $exists = \function_exists('exec');
57 if (!$exists) {
58 throw new \Exception('The "exec" function must exist to use the cURL consumer in "fork" mode. Try setting fork = false or use another consumer.');
59 }
60 $disabled = \explode(', ', \ini_get('disable_functions'));
61 $enabled = !\in_array('exec', $disabled);
62 if (!$enabled) {
63 throw new \Exception('The "exec" function must be enabled to use the cURL consumer in "fork" mode. Try setting fork = false or use another consumer.');
64 }
65 } else {
66 if (!\function_exists('curl_init')) {
67 throw new \Exception('The cURL PHP extension is required to use the cURL consumer with fork = false. Try setting fork = true or use another consumer.');
68 }
69 }
70 }
71 /**
72 * Write to the given host/endpoint using either a forked cURL process or using PHP's cURL extension
73 * @param array $batch
74 * @return bool
75 */
76 public function persist($batch)
77 {
78 if (\count($batch) > 0) {
79 $url = $this->_protocol . "://" . $this->_host . $this->_endpoint;
80 if ($this->_fork) {
81 $data = "data=" . $this->_encode($batch);
82 return $this->_execute_forked($url, $data);
83 } else {
84 return $this->_execute($url, $batch);
85 }
86 } else {
87 return \true;
88 }
89 }
90 /**
91 * Write using the cURL php extension
92 * @param $url
93 * @param $batch
94 * @return bool
95 */
96 protected function _execute($url, $batch)
97 {
98 if ($this->_debug()) {
99 $this->_log("Making blocking cURL call to {$url}");
100 }
101 $mh = \curl_multi_init();
102 $chs = array();
103 $batch_size = \ceil(\count($batch) / $this->_num_threads);
104 for ($i = 0; $i < $this->_num_threads && !empty($batch); $i++) {
105 $ch = \curl_init();
106 $chs[] = $ch;
107 $data = "data=" . $this->_encode(\array_splice($batch, 0, $batch_size));
108 \curl_setopt($ch, \CURLOPT_URL, $url);
109 \curl_setopt($ch, \CURLOPT_HEADER, 0);
110 \curl_setopt($ch, \CURLOPT_CONNECTTIMEOUT, $this->_connect_timeout);
111 \curl_setopt($ch, \CURLOPT_TIMEOUT, $this->_timeout);
112 \curl_setopt($ch, \CURLOPT_POST, 1);
113 \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
114 \curl_setopt($ch, \CURLOPT_POSTFIELDS, $data);
115 \curl_multi_add_handle($mh, $ch);
116 }
117 $running = 0;
118 do {
119 \curl_multi_exec($mh, $running);
120 \curl_multi_select($mh);
121 } while ($running > 0);
122 $info = \curl_multi_info_read($mh);
123 $error = \false;
124 foreach ($chs as $ch) {
125 $response = \curl_multi_getcontent($ch);
126 if (\false === $response) {
127 $this->_handleError(\curl_errno($ch), \curl_error($ch));
128 $error = \true;
129 } elseif ("1" != \trim($response)) {
130 $this->_handleError(0, $response);
131 $error = \true;
132 }
133 \curl_multi_remove_handle($mh, $ch);
134 }
135 if (\CURLE_OK != $info['result']) {
136 $this->_handleError($info['result'], "cURL error with code=" . $info['result']);
137 $error = \true;
138 }
139 \curl_multi_close($mh);
140 return !$error;
141 }
142 /**
143 * Write using a forked cURL process
144 * @param $url
145 * @param $data
146 * @return bool
147 */
148 protected function _execute_forked($url, $data)
149 {
150 if ($this->_debug()) {
151 $this->_log("Making forked cURL call to {$url}");
152 }
153 $exec = 'curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d ' . $data . ' "' . $url . '"';
154 if (!$this->_debug()) {
155 $exec .= " >/dev/null 2>&1 &";
156 }
157 \exec($exec, $output, $return_var);
158 if ($return_var != 0) {
159 $this->_handleError($return_var, $output);
160 }
161 return $return_var == 0;
162 }
163 /**
164 * @return int
165 */
166 public function getConnectTimeout()
167 {
168 return $this->_connect_timeout;
169 }
170 /**
171 * @return string
172 */
173 public function getEndpoint()
174 {
175 return $this->_endpoint;
176 }
177 /**
178 * @return bool|null
179 */
180 public function getFork()
181 {
182 return $this->_fork;
183 }
184 /**
185 * @return string
186 */
187 public function getHost()
188 {
189 return $this->_host;
190 }
191 /**
192 * @return array
193 */
194 public function getOptions()
195 {
196 return $this->_options;
197 }
198 /**
199 * @return string
200 */
201 public function getProtocol()
202 {
203 return $this->_protocol;
204 }
205 /**
206 * @return int
207 */
208 public function getTimeout()
209 {
210 return $this->_timeout;
211 }
212 /**
213 * Number of requests/batches that will be processed in parallel using curl_multi_exec.
214 * @return int
215 */
216 public function getNumThreads()
217 {
218 return $this->_num_threads;
219 }
220 }
221