AuthController.php
1 year ago
AutomationController.php
3 years ago
EventController.php
3 years ago
GlobalSearchController.php
1 year ago
IntegrationsController.php
2 years ago
OptionController.php
3 years ago
RestController.php
1 year ago
RoutesController.php
3 years ago
SettingsController.php
3 years ago
RestController.php
371 lines
| 1 | <?php |
| 2 | /** |
| 3 | * RestController. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category RestController |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Controllers; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\WordPress\WordPress; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | use SureTriggers\Models\SaasApiToken; |
| 20 | use WP_REST_Request; |
| 21 | use WP_REST_Response; |
| 22 | |
| 23 | /** |
| 24 | * RestController |
| 25 | * |
| 26 | * @category RestController |
| 27 | * @package SureTriggers |
| 28 | * @author BSF <username@example.com> |
| 29 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 30 | * @link https://www.brainstormforce.com/ |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | class RestController { |
| 34 | |
| 35 | /** |
| 36 | * Access token for authentication. |
| 37 | * |
| 38 | * @var string $acccess_token |
| 39 | */ |
| 40 | private $secret_key; |
| 41 | |
| 42 | use SingletonLoader; |
| 43 | |
| 44 | /** |
| 45 | * Initialise data. |
| 46 | */ |
| 47 | public function __construct() { |
| 48 | $this->secret_key = SaasApiToken::get(); |
| 49 | add_filter( 'determine_current_user', [ $this, 'basic_auth_handler' ], 20 ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Permission callback for rest api after deterination of current user. |
| 54 | * |
| 55 | * @param WP_REST_Request $request Request. |
| 56 | */ |
| 57 | public function autheticate_user( $request ) { |
| 58 | $secret_key = $request->get_header( 'st_authorization' ); |
| 59 | list($secret_key) = sscanf( $secret_key, 'Bearer %s' ); |
| 60 | |
| 61 | if ( $this->secret_key !== $secret_key ) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Authenticate User for API calls. |
| 70 | * |
| 71 | * @param array|object $user USer. |
| 72 | * |
| 73 | * @return int|null |
| 74 | */ |
| 75 | public function basic_auth_handler( $user ) { |
| 76 | // Don't authenticate twice. |
| 77 | if ( ! empty( $user ) ) { |
| 78 | return $user; |
| 79 | } |
| 80 | |
| 81 | // Check that we're trying to authenticate. |
| 82 | if ( ! isset( $_SERVER['PHP_AUTH_USER'] ) || ! isset( $_SERVER['PHP_AUTH_PW'] ) ) { //phpcs:ignore |
| 83 | return $user; |
| 84 | } |
| 85 | |
| 86 | $username = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) ); //phpcs:ignore |
| 87 | $password = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ); //phpcs:ignore |
| 88 | |
| 89 | /** |
| 90 | * In multi-site, wp_authenticate_spam_check filter is run on authentication. This filter calls. |
| 91 | * get_currentuserinfo which in turn calls the determine_current_user filter. This leads to infinite. |
| 92 | * recursion and a stack overflow unless the current function is removed from the determine_current_user. |
| 93 | * filter during authentication. |
| 94 | */ |
| 95 | remove_filter( 'determine_current_user', [ $this, 'basic_auth_handler' ], 20 ); |
| 96 | |
| 97 | $user = wp_authenticate( $username, $password ); |
| 98 | |
| 99 | add_filter( 'determine_current_user', [ $this, 'basic_auth_handler' ], 20 ); |
| 100 | |
| 101 | if ( is_wp_error( $user ) ) { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | return $user->ID; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Authenticate user for new connection create api. |
| 110 | * |
| 111 | * @return bool |
| 112 | */ |
| 113 | public function is_current_user() { |
| 114 | if ( current_user_can( 'manage_options' ) ) { |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Execute action events. |
| 122 | * |
| 123 | * @param WP_REST_Request $request Request data. |
| 124 | * @return WP_REST_Response |
| 125 | */ |
| 126 | public function run_action( $request ) { |
| 127 | $request->get_param( 'wp_user_id' ); |
| 128 | |
| 129 | $user_id = $request->get_param( 'wp_user_id' ); |
| 130 | $automation_id = $request->get_param( 'automation_id' ); |
| 131 | $integration = $request->get_param( 'integration' ); |
| 132 | $action_type = $request->get_param( 'type_event' ); |
| 133 | $selected_options = $request->get_param( 'selected_options' ); |
| 134 | $context = $request->get_param( 'context' ); |
| 135 | $fields = $request->get_param( 'fields' ); |
| 136 | |
| 137 | if ( empty( $user_id ) ) { |
| 138 | $user_id = isset( $context['pluggable_data']['wp_user_id'] ) ? sanitize_text_field( $context['pluggable_data']['wp_user_id'] ) : ''; |
| 139 | } |
| 140 | |
| 141 | if ( empty( $integration ) || empty( $action_type ) ) { |
| 142 | return self::error_message( 'Integration or action type is missing' ); |
| 143 | } |
| 144 | |
| 145 | if ( isset( $selected_options['wp_user_email'] ) ) { |
| 146 | $is_valid = WordPress::validate_email( $selected_options['wp_user_email'] ); |
| 147 | |
| 148 | if ( ! $is_valid->valid ) { |
| 149 | if ( $is_valid->multiple ) { |
| 150 | return self::error_message( 'One or more email address is not valid.' ); |
| 151 | } else { |
| 152 | return self::error_message( 'Email address is not valid.' ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if ( str_contains( $selected_options['wp_user_email'], ',' ) ) { |
| 157 | $email_list = explode( ',', $selected_options['wp_user_email'] ); |
| 158 | |
| 159 | foreach ( $email_list as $single_email ) { |
| 160 | if ( ! email_exists( trim( $single_email ) ) ) { |
| 161 | return self::error_message( 'User with email ' . $single_email . ' does not exists.' ); |
| 162 | } |
| 163 | } |
| 164 | } else { |
| 165 | if ( ! email_exists( $selected_options['wp_user_email'] ) ) { |
| 166 | return self::error_message( 'User with email ' . $selected_options['wp_user_email'] . ' does not exists.' ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | $registered_actions = EventController::get_instance()->actions; |
| 171 | $action_event = $registered_actions[ $integration ][ $action_type ]; |
| 172 | |
| 173 | $fun_params = [ |
| 174 | $user_id, |
| 175 | $automation_id, |
| 176 | $fields, |
| 177 | $selected_options, |
| 178 | $context, |
| 179 | ]; |
| 180 | |
| 181 | try { |
| 182 | $result = call_user_func_array( |
| 183 | $action_event['function'], |
| 184 | $fun_params |
| 185 | ); |
| 186 | return self::success_message( $result ); |
| 187 | } catch ( Exception $e ) { |
| 188 | return self::error_message( $e->getMessage(), 400 ); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Error message format. |
| 194 | * |
| 195 | * @param string $message Error message. |
| 196 | * @param string $status Error message. |
| 197 | * |
| 198 | * @return object |
| 199 | */ |
| 200 | public static function error_message( $message, $status = 401 ) { |
| 201 | return new WP_REST_Response( |
| 202 | [ |
| 203 | 'success' => false, |
| 204 | 'data' => [ |
| 205 | 'errors' => $message, |
| 206 | ], |
| 207 | ], |
| 208 | $status |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Success message format. |
| 214 | * |
| 215 | * @param array $data response data to be sent. |
| 216 | * |
| 217 | * @return object |
| 218 | */ |
| 219 | public static function success_message( $data = [] ) { |
| 220 | $result = []; |
| 221 | |
| 222 | if ( ! empty( $data ) ) { |
| 223 | $result['result'] = $data; |
| 224 | } |
| 225 | |
| 226 | return new WP_REST_Response( |
| 227 | [ |
| 228 | 'success' => true, |
| 229 | 'data' => $result, |
| 230 | ], |
| 231 | 200 |
| 232 | ); |
| 233 | |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Add/Remove/Update the triggers.. |
| 238 | * When new/update/remove automation on Sass then execute this endpoint to update the automation. |
| 239 | * |
| 240 | * @param WP_REST_Request $request Request data. |
| 241 | * @return WP_REST_Response |
| 242 | */ |
| 243 | public function manage_triggers( $request ) { |
| 244 | $events = $request->get_param( 'events' ) ? json_decode( stripslashes( $request->get_param( 'events' ) ), true ) : []; |
| 245 | // Selected field data from the trigger. |
| 246 | $data = $request->get_param( 'data' ) ? json_decode( stripslashes( $request->get_param( 'data' ) ), true ) : []; |
| 247 | |
| 248 | // Get the trigger data from the option and append data in trigger data option. |
| 249 | $trigger_data = OptionController::get_option( 'trigger_data' ); |
| 250 | if ( empty( $trigger_data ) ) { |
| 251 | $trigger_data = []; |
| 252 | } |
| 253 | if ( is_array( $data ) && is_array( $events ) ) { |
| 254 | $index = array_search( $data['trigger'], array_column( $events, 'trigger' ) ); |
| 255 | if ( is_array( $trigger_data ) && false !== $index && $data['integration'] === $events[ $index ]['integration'] ) { |
| 256 | $trigger_data[ $data['integration'] ][ $data['trigger'] ]['selected_options'] = $data['selected_data']; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | OptionController::set_option( 'triggers', $events ); |
| 261 | // Set the new option for the trigger data. |
| 262 | OptionController::set_option( 'trigger_data', $trigger_data ); |
| 263 | $events = array_column( $events, 'trigger' ); |
| 264 | return self::success_message( |
| 265 | [ |
| 266 | 'events' => $events, |
| 267 | 'data' => $trigger_data, |
| 268 | ] |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Send response to Sasss that trigger is executed. |
| 274 | * |
| 275 | * @param string $trigger_data Trigger data. |
| 276 | * |
| 277 | * @return bool |
| 278 | */ |
| 279 | public function trigger_listener( $trigger_data ) { |
| 280 | $args = [ |
| 281 | 'headers' => [ |
| 282 | 'Authorization' => 'Bearer ' . $this->secret_key, |
| 283 | 'Referer' => str_replace( '/wp-json/', '', get_site_url() ), |
| 284 | ], |
| 285 | 'body' => json_decode( wp_json_encode( $trigger_data ), 1 ), |
| 286 | 'sslverify' => false, |
| 287 | ]; |
| 288 | |
| 289 | /** |
| 290 | * |
| 291 | * Ignore line |
| 292 | * |
| 293 | * @phpstan-ignore-next-line |
| 294 | */ |
| 295 | $response = wp_remote_post( WEBHOOK_SERVER_URL . '/wordpress/webhook', $args ); |
| 296 | if ( wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Update the connection from SAAS. |
| 305 | * |
| 306 | * @param WP_REST_Request $request Request data. |
| 307 | * |
| 308 | * @return void |
| 309 | */ |
| 310 | public function connection_update( $request ) { |
| 311 | $secret = $request->get_param( 'secret_key' ); |
| 312 | if ( $secret && is_string( $secret ) ) { |
| 313 | SaasApiToken::save( $secret ); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Disconnect connection |
| 319 | * |
| 320 | * @param WP_REST_Request $request Request data. |
| 321 | * @return WP_REST_Response |
| 322 | */ |
| 323 | public function connection_disconnect( $request ) { |
| 324 | SaasApiToken::save( null ); |
| 325 | return self::success_message(); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Test Trigger |
| 330 | * When test trigger is initiated on Sass then execute this endpoint to create a transient for identifying trigger event. |
| 331 | * |
| 332 | * @param WP_REST_Request $request Request data. |
| 333 | * @return WP_REST_Response |
| 334 | */ |
| 335 | public function test_triggers( $request ) { |
| 336 | $test_triggers = (array) OptionController::get_option( 'test_triggers', [] ); |
| 337 | $event = [ |
| 338 | 'trigger' => $request->get_param( 'trigger' ), |
| 339 | 'integration' => $request->get_param( 'integration' ), |
| 340 | ]; |
| 341 | |
| 342 | // if request is to delete the transient, delete it and return. |
| 343 | if ( $request->get_param( 'clear_transient_data' ) === 'yes' ) { |
| 344 | $test_triggers = array_filter( |
| 345 | $test_triggers, |
| 346 | function ( $v ) use ( $event ) { |
| 347 | return $v !== $event; |
| 348 | } |
| 349 | ); |
| 350 | OptionController::set_option( 'test_triggers', $test_triggers ); |
| 351 | |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | $test_triggers[] = $event; |
| 356 | $test_triggers = array_unique( $test_triggers, SORT_REGULAR ); |
| 357 | $tmp_test_triggers = []; |
| 358 | |
| 359 | foreach ( $test_triggers as $test_trigger ) { |
| 360 | if ( ! empty( $test_trigger['trigger'] ) ) { |
| 361 | $tmp_test_triggers[] = $test_trigger; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | OptionController::set_option( 'test_triggers', $tmp_test_triggers ); |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | RestController::get_instance(); |
| 371 |