PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / WebHooks / WebHooksHandler.php

WebHooksHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.10.2, at includes/Core/Integration/WebHooks/WebHooksHandler.php

203 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WebHooks Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\WebHooks;
9
10 use BitCode\BitForm\Core\Integration\IntegrationHandler;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for webhooks
16 */
17 class WebHooksHandler
18 {
19 private $formID;
20 private $webhookID;
21
22 private $_logResponse;
23
24 public function __construct($webhookID, $formID)
25 {
26 $this->formID = $formID;
27 $this->webhookID = $webhookID;
28 $this->_logResponse = new UtilApiResponse();
29 }
30
31 /**
32 * Helps to register ajax function's with wp
33 *
34 * @return null
35 */
36 public static function registerAjax()
37 {
38 add_action('wp_ajax_bitforms_test_webhook', [__CLASS__, 'testWebhook']);
39 }
40
41 public static function testWebhook()
42 {
43 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) {
44 $inputJSON = file_get_contents('php://input');
45 $webhookDetails = json_decode($inputJSON);
46 $details = is_string($webhookDetails) ? json_decode($webhookDetails)->hookDetails : $webhookDetails->hookDetails;
47 $method = isset($details->method) ? $details->method : 'get';
48 $data = isset($details->url) ? WebHooksHandler::urlParserWrapper($details->url) : false;
49 $response = null;
50 if ($data) {
51 $url = $data['url'];
52 $params = $data['params'];
53 $params['entry_id'] = 'test';
54 switch (strtoupper($method)) {
55 case 'GET':
56 $response = HttpHelper::get($url, $params);
57 break;
58
59 case 'POST':
60 $response = HttpHelper::post($url, $params);
61 break;
62
63 default:
64 $response = HttpHelper::request($url, $method, $params);
65 break;
66 }
67 }
68 if (is_wp_error($response)) {
69 wp_send_json_error(
70 empty($response) ? 'Unknown Error Occured' : $response->get_error_message(),
71 400
72 );
73 }
74 if (empty($data['url'])) {
75 wp_send_json_error(__('webhook url is empty', 'bit-form'), 400);
76 }
77 wp_send_json_success(['msg' => 'webhook executed succcessfully', 'response' => $response], 200);
78 } else {
79 wp_send_json_error(
80 __(
81 'Token expired',
82 'bit-form'
83 ),
84 401
85 );
86 }
87 }
88
89 public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID)
90 {
91 $details = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details;
92 $method = isset($details->method) ? $details->method : 'get';
93 $data = isset($details->url) ? $this->urlParserWrapper($details->url) : false;
94 if ($data) {
95 $url = $data['url'];
96 $params = $data['params'];
97 $params = IntegrationHandler::replaceFieldWithValue($params, self::iterate($fieldValues));
98 $params['entry_id'] = $entryID;
99 switch (strtoupper($method)) {
100 case 'GET':
101 $response = HttpHelper::get($url, $params);
102 break;
103
104 case 'POST':
105 $response = HttpHelper::post($url, $params);
106 break;
107
108 default:
109 $response = HttpHelper::request($url, $method, $params);
110 break;
111 }
112
113 // if bitform pro is not active then return the response without response log
114 if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
115 return $response;
116 }
117
118 // if bitform pro is active then return the response with response log
119 if (is_wp_error($response)) {
120 $this->_logResponse->apiResponse(
121 $logID,
122 $this->webhookID,
123 ['type' => 'record', 'type_name' => 'web hooks'],
124 'errors',
125 $response
126 );
127 } else {
128 $this->_logResponse->apiResponse(
129 $logID,
130 $this->webhookID,
131 ['type' => 'record', 'type_name' => 'web hooks'],
132 'success',
133 $response
134 );
135 }
136 return $response;
137 } else {
138 if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
139 return false;
140 }
141
142 $this->_logResponse->apiResponse(
143 $logID,
144 $this->webhookID,
145 ['type' => 'record', 'type_name' => 'web hooks'],
146 'errors',
147 'There is something wrong in the webhook url'
148 );
149 }
150 }
151
152 private static function urlParserWrapper($url)
153 {
154 if (empty($url)) {
155 return false;
156 }
157 $parsedURL = wp_parse_url($url);
158
159 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
160 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
161 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
162 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
163 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
164 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
165 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
166 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
167
168 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
169 $params = [];
170 foreach (explode('&', $Query) as $keyValue) {
171 if (empty($keyValue)) {
172 continue;
173 }
174 list($field, $value) = explode('=', $keyValue);
175 if ('' === trim($value)) {
176 continue;
177 }
178 if (isset($params[$field])) {
179 if (\is_array($params[$field])) {
180 $params[$field][] = sanitize_text_field(urldecode($value));
181 } else {
182 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
183 }
184 } else {
185 $params[$field] = sanitize_text_field(urldecode($value));
186 }
187 }
188
189 return ['url' => $cleanURL, 'params' => $params];
190 }
191
192 private function iterate($array)
193 {
194 $ar = [];
195 if (is_array($array)) {
196 foreach ($array as $k => $v) {
197 $ar[$k] = str_replace("\'", "'", $v);
198 }
199 }
200 return $ar;
201 }
202 }
203