PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
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.15.3, 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
95 if ($data) {
96 $url = $data['url'];
97 $params = $data['params'];
98 $params = IntegrationHandler::replaceFieldWithValue($params, $fieldValues);
99 $params['entry_id'] = $entryID;
100 switch (strtoupper($method)) {
101 case 'GET':
102 $response = HttpHelper::get($url, $params);
103 break;
104
105 case 'POST':
106 $response = HttpHelper::post($url, $params);
107 break;
108
109 default:
110 $response = HttpHelper::request($url, $method, $params);
111 break;
112 }
113
114 // if bitform pro is not active then return the response without response log
115 // if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
116 // return $response;
117 // }
118
119 // if bitform pro is active then return the response with response log
120 if (is_wp_error($response)) {
121 $this->_logResponse->apiResponse(
122 $logID,
123 $this->webhookID,
124 ['type' => 'record', 'type_name' => 'web hooks'],
125 'errors',
126 $response
127 );
128 } else {
129 $this->_logResponse->apiResponse(
130 $logID,
131 $this->webhookID,
132 ['type' => 'record', 'type_name' => 'web hooks'],
133 'success',
134 $response
135 );
136 }
137 return $response;
138 } else {
139 // if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
140 // return false;
141 // }
142
143 $this->_logResponse->apiResponse(
144 $logID,
145 $this->webhookID,
146 ['type' => 'record', 'type_name' => 'web hooks'],
147 'errors',
148 'There is something wrong in the webhook url'
149 );
150 }
151 }
152
153 private static function urlParserWrapper($url)
154 {
155 if (empty($url)) {
156 return false;
157 }
158 $parsedURL = wp_parse_url($url);
159
160 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
161 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
162 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
163 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
164 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
165 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
166 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
167 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
168
169 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
170 $params = [];
171 foreach (explode('&', $Query) as $keyValue) {
172 if (empty($keyValue)) {
173 continue;
174 }
175 list($field, $value) = explode('=', $keyValue);
176 if ('' === trim($value)) {
177 continue;
178 }
179 if (isset($params[$field])) {
180 if (\is_array($params[$field])) {
181 $params[$field][] = sanitize_text_field(urldecode($value));
182 } else {
183 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
184 }
185 } else {
186 $params[$field] = sanitize_text_field(urldecode($value));
187 }
188 }
189
190 return ['url' => $cleanURL, 'params' => $params];
191 }
192
193 private function iterate($array)
194 {
195 $ar = [];
196 if (is_array($array)) {
197 foreach ($array as $k => $v) {
198 $ar[$k] = str_replace("\'", "'", $v);
199 }
200 }
201 return $ar;
202 }
203 }