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