|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Wordlift_Key_Validation_Service class |
|
4
|
|
|
* |
|
5
|
|
|
* The {@link Wordlift_Key_Validation_Service} class provides WordLift's key validation services. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://wordlift.io |
|
8
|
|
|
* |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
* @since 3.9.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Define the {@link Wordlift_Key_Validation_Service} class. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 3.9.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Wordlift_Key_Validation_Service { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 3.14.0 |
|
24
|
|
|
* @access private |
|
25
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $log; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The {@link Wordlift_Configuration_Service} instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 3.14.0 |
|
33
|
|
|
* @access private |
|
34
|
|
|
* @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
35
|
|
|
*/ |
|
36
|
|
|
private $configuration_service; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create a {@link Wordlift_Key_Validation_Service} instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 3.14.0 |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( $configuration_service ) { |
|
46
|
|
|
|
|
47
|
|
|
$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
48
|
|
|
|
|
49
|
|
|
$this->configuration_service = $configuration_service; |
|
50
|
|
|
add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
51
|
|
|
add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Validate the provided key. |
|
57
|
|
|
* |
|
58
|
|
|
* @since 3.9.0 |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $key WordLift's key to validate. |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool True if the key is valid, otherwise false. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function is_valid( $key ) { |
|
65
|
|
|
|
|
66
|
|
|
$this->log->debug( 'Validating key...' ); |
|
67
|
|
|
|
|
68
|
|
|
// Request the account info as a way to validate the key |
|
69
|
|
|
|
|
70
|
|
|
$args = array_merge_recursive( |
|
71
|
|
|
unserialize( WL_REDLINK_API_HTTP_OPTIONS ), |
|
72
|
|
|
array( |
|
73
|
|
|
'headers' => array( |
|
74
|
|
|
'Content-Type' => 'application/json; charset=utf-8', |
|
75
|
|
|
'X-Authorization' => $key ) |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$response = wp_remote_get( $this->configuration_service->get_accounts_info_by_key( $key ), $args ); |
|
80
|
|
|
|
|
81
|
|
|
return $response; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* This function is hooked to the `wl_validate_key` AJAX call. |
|
86
|
|
|
* |
|
87
|
|
|
* @since 3.9.0 |
|
88
|
|
|
*/ |
|
89
|
|
|
public function validate_key() { |
|
90
|
|
|
|
|
91
|
|
|
// Ensure we don't have garbage before us. |
|
92
|
|
|
ob_clean(); |
|
93
|
|
|
|
|
94
|
|
|
// Check if we have a key. |
|
95
|
|
|
if ( ! isset( $_POST['key'] ) ) { |
|
96
|
|
|
wp_send_json_error( 'The key parameter is required.' ); |
|
97
|
|
|
} |
|
98
|
|
|
$response = $this->is_valid( $_POST['key'] ); |
|
99
|
|
|
$res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
100
|
|
|
$url = $res_body['url']; |
|
101
|
|
|
|
|
102
|
|
|
//Set a response with valid set to true and messgae according to the key validity with url match |
|
103
|
|
|
if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] && $url == site_url() ) { |
|
104
|
|
|
$is_valid = true; |
|
105
|
|
|
$message = " "; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] && $url != site_url() ) { |
|
109
|
|
|
$is_valid = false; |
|
110
|
|
|
$message = __( "The key is already used on another site, please contact us at [email protected] to move the key to another site.", 'wordlift' ); |
|
111
|
|
|
Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ( is_wp_error( $response ) || 500 === (int) $response['response']['code'] ) { |
|
115
|
|
|
$is_valid = false; |
|
116
|
|
|
$message = ""; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Set a response with valid set to true or false according to the key validity with message. |
|
120
|
|
|
wp_send_json_success( array( 'valid' => $is_valid, 'message' => $message ) ); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* This function is hooked `admin_init` to check _wl_blog_url. |
|
126
|
|
|
* |
|
127
|
|
|
*/ |
|
128
|
|
|
public function wl_load_plugin() |
|
129
|
|
|
{ |
|
130
|
|
|
$wl_blog_url = get_option( '_wl_blog_url' ); |
|
131
|
|
|
if ( !$wl_blog_url ) { |
|
132
|
|
|
update_option( '_wl_blog_url', site_url(), true ); |
|
133
|
|
|
} |
|
134
|
|
|
if ( $wl_blog_url != site_url() ) { |
|
135
|
|
|
Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
136
|
|
|
set_transient( 'wl-key-error-msg' , __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* This function is hooked to the `admin_notices` to show admin notification. |
|
142
|
|
|
* |
|
143
|
|
|
*/ |
|
144
|
|
|
public function wl_key_update_notice() { |
|
145
|
|
|
if ( get_transient( 'wl-key-error-msg' ) ) { |
|
146
|
|
|
?> |
|
147
|
|
|
<div class="updated notice is-dismissible error"> |
|
148
|
|
|
<p><?php _e( get_transient('wl-key-error-msg'), 'wordlift' ); ?></p> |
|
149
|
|
|
</div> |
|
150
|
|
|
<?php |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: