| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.'); |
| 4 |
|
| 5 |
abstract class UpdraftPlus_Login { |
| 6 |
|
| 7 |
/** |
| 8 |
* Pulls the appropriate message for the given code and translate it before |
| 9 |
* returning it to the caller |
| 10 |
* |
| 11 |
* @internal |
| 12 |
* @param string $code The code of the message to pull |
| 13 |
* @return string - The translated message |
| 14 |
*/ |
| 15 |
abstract protected function translate_message($code); |
| 16 |
|
| 17 |
/** |
| 18 |
* Executes login or registration process. Connects and sends request to the UpdraftCentral Cloud |
| 19 |
* and returns the response coming from the server |
| 20 |
* |
| 21 |
* @internal |
| 22 |
* @param array $data The submitted form data |
| 23 |
* @param boolean $register Indicates whether the current call is for a registration process or not. Defaults to false. |
| 24 |
* @return array - The response from the request |
| 25 |
*/ |
| 26 |
abstract protected function login_or_register($data, $register = false); |
| 27 |
|
| 28 |
/** |
| 29 |
* Handles the actual sending of the request to the remote server (UpdraftCentral Cloud) and pre-checks the response |
| 30 |
* and wraps it up for the caller before sending it back |
| 31 |
* |
| 32 |
* @internal |
| 33 |
* @param array $data The submitted form data |
| 34 |
* @param string $action The name of the action or service that will be triggered in UpdraftCentral Cloud |
| 35 |
* @return array - The response from the server |
| 36 |
*/ |
| 37 |
protected function send_remote_request($data, $action) { |
| 38 |
global $updraftplus; |
| 39 |
|
| 40 |
$result = wp_remote_post($updraftplus->get_url('mothership').'/?udm_action='.$action, |
| 41 |
array( |
| 42 |
'timeout' => 20, |
| 43 |
'headers' => apply_filters('updraftplus_auth_headers', ''), |
| 44 |
'body' => $data |
| 45 |
) |
| 46 |
); |
| 47 |
|
| 48 |
// If we got an error then we return the WP_Error object itself |
| 49 |
// and let the caller handle it. |
| 50 |
if (is_wp_error($result)) return $result; |
| 51 |
|
| 52 |
$response = json_decode(wp_remote_retrieve_body($result), true); |
| 53 |
if (!is_array($response) || !isset($response['mothership']) || !isset($response['status'])) { |
| 54 |
|
| 55 |
if (preg_match('/has banned your IP address \(([\.:0-9a-f]+)\)/', $result['body'], $matches)) { |
| 56 |
return new WP_Error('banned_ip', sprintf(__("UpdraftPlus.com has responded with 'Access Denied'.", 'updraftplus').'<br>'.__("It appears that your web server's IP Address (%s) is blocked.", 'updraftplus').' '.__('This most likely means that you share a webserver with a hacked website that has been used in previous attacks.', 'updraftplus').'<br> <a href="'.apply_filters("updraftplus_com_link", "https://updraftplus.com/unblock-ip-address/").'" target="_blank">'.__('To remove the block, please go here.', 'updraftplus').'</a> ', $matches[1])); |
| 57 |
} else { |
| 58 |
return new WP_Error('unknown_response', sprintf(__('UpdraftPlus.Com returned a response which we could not understand (data: %s)', 'updraftplus'), wp_remote_retrieve_body($result))); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $response; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* The ajax based request point of entry for the login process |
| 67 |
* |
| 68 |
* @param array $data - The submitted form data |
| 69 |
* @param boolean $echo_results - Whether to echo/display the results directly to the user or assign it to a variable |
| 70 |
* @return array - Response of the process |
| 71 |
*/ |
| 72 |
public function ajax_process_login($data = array(), $echo_results = true) { |
| 73 |
try { |
| 74 |
if (isset($data['form_data'])) { |
| 75 |
if (is_string($data['form_data'])) { |
| 76 |
parse_str($data['form_data'], $form_data); |
| 77 |
} elseif (is_array($data['form_data'])) { |
| 78 |
$form_data = $data['form_data']; |
| 79 |
} |
| 80 |
} |
| 81 |
$response = $this->login_or_register($form_data); |
| 82 |
} catch (Exception $e) { |
| 83 |
$response = array('error' => true, 'message' => $e->getMessage()); |
| 84 |
} |
| 85 |
|
| 86 |
if ($echo_results) { |
| 87 |
echo json_encode($response); |
| 88 |
} else { |
| 89 |
return $response; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* The ajax based request point of entry for the registration process |
| 95 |
* |
| 96 |
* @param array $data - The submitted form data |
| 97 |
* @param boolean $echo_results - Whether to echo/display the results directly to the user or assign it to a variable |
| 98 |
* @return array - Response of the process |
| 99 |
*/ |
| 100 |
public function ajax_process_registration($data = array(), $echo_results = true) { |
| 101 |
try { |
| 102 |
if (isset($data['form_data'])) parse_str($data['form_data'], $form_data); |
| 103 |
|
| 104 |
$response = $this->login_or_register($form_data, true); |
| 105 |
} catch (Exception $e) { |
| 106 |
$response = array('error' => true, 'message' => $e->getMessage()); |
| 107 |
} |
| 108 |
|
| 109 |
if ($echo_results) { |
| 110 |
echo json_encode($response); |
| 111 |
} else { |
| 112 |
return $response; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|