class-gravity-api.php
7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
if ( ! defined( 'GRAVITY_API_URL' ) ) {
define( 'GRAVITY_API_URL', 'https://o4zq2dvjn6.execute-api.us-east-1.amazonaws.com/prod/' );
}
if ( ! class_exists( 'Gravity_Api' ) ) {
/**
* Client-side API wrapper for interacting with the Gravity APIs.
*
* @package Gravity Forms
* @subpackage Gravity_Api
* @since 1.9
* @access public
*/
class Gravity_Api {
private static $instance = null;
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Retrieves site key and site secret key from remote API and stores them as WP options. Returns false if license key is invalid; otherwise, returns true.
*
* @since 1.9.?
* @access public
*
* @param string $license_key
*
* @return bool Success
*/
public function register_current_site( $license_key, $is_md5 = false ) {
$body = GFCommon::get_remote_post_params();
$body['site_name'] = get_bloginfo( 'name' );
$body['site_url'] = get_bloginfo( 'url' );
if ( $is_md5 ) {
$body['license_key_md5'] = $license_key;
$license_key_md5 = $license_key;
} else {
$body['license_key'] = $license_key;
$license_key_md5 = md5( $license_key );
}
GFCommon::log_debug( __METHOD__ . '() - registering site' );
$result = $this->request( 'sites', $body, 'POST', array( 'headers' => $this->get_license_auth_header( $license_key_md5 ) ) );
$result = $this->prepare_response_body( $result );
if ( is_wp_error( $result ) ) {
GFCommon::log_debug( __METHOD__ . '() - error registering site. ' . print_r( $result, true ) );
return $result;
}
//Updating site key and secret
update_option( 'gf_site_key', $result->key );
update_option( 'gf_site_secret', $result->secret );
GFCommon::log_debug( __METHOD__ . '() - site registration successful. Site Key: ' . $result->key );
return true;
}
public function update_current_site( $new_license_key_md5 ) {
$site_key = $this->get_site_key();
if ( empty( $site_key ) ) {
return false;
}
$body = GFCommon::get_remote_post_params();
$body['site_name'] = get_bloginfo( 'name' );
$body['site_url'] = get_bloginfo( 'url' );
$body['license_key_md5'] = $new_license_key_md5;
$body['site_key'] = $site_key;
$site_secret = $this->get_site_secret();
if ( $site_secret ) {
$site_secret_md5 = md5( $site_secret );
$body['site_secret'] = $site_secret;
$body['site_secret_md5'] = $site_secret_md5;
}
GFCommon::log_debug( __METHOD__ . '() - refreshing license info' );
$result = $this->request( 'sites/' . $site_key, $body, 'PUT', array( 'headers' => $this->get_site_auth_header( $site_key, $site_secret ) ) );
$result = $this->prepare_response_body( $result );
if ( is_wp_error( $result ) ) {
GFCommon::log_debug( __METHOD__ . '() - error updating site registration. ' . print_r( $result, true ) );
return $result;
}
return true;
}
public function deregister_current_site(){
$site_key = $this->get_site_key();
$site_secret = $this->get_site_secret();
if ( empty( $site_key ) ) {
return false;
}
GFCommon::log_debug( __METHOD__ . '() - deregistering' );
$body = array(
'license_key_md5' => '',
);
$result = $this->request( 'sites/' . $site_key, $body, 'PUT', array( 'headers' => $this->get_site_auth_header( $site_key, $site_secret ) ) );
$result = $this->prepare_response_body( $result );
if ( is_wp_error( $result ) ) {
GFCommon::log_debug( __METHOD__ . '() - error updating site registration. ' . print_r( $result, true ) );
return $result;
}
return true;
}
/***
* Retrieves API Keys for third party services. Requires a valid license
*
* @param $third_party_name string - Name or the third party service. "Dropbox" is currently the only supported service.
* @return WP_Error|object - If successful, returns the api key.
*/
public function get_api_key( $third_party_name ) {
$site_keys = $this->ensure_site_registered();
if ( empty( $site_keys ) ) {
return false;
}
switch ( $third_party_name ) {
case 'Dropbox' :
GFCommon::log_debug( __METHOD__ . '() - retrieving dropbox api key' );
$auth = base64_encode( $site_keys['site_key'] . ':' . $site_keys['site_secret'] );
$headers = array( 'Authorization' => 'GravityAPI ' . $auth );
$response = $this->request( 'credentials/dropbox', array(), 'GET', array( 'headers' => $headers ) );
return $this->prepare_response_body( $response );
default :
return new WP_Error( 'unsupported_service_name', 'The provided third party service name: ' . $third_party_name . ' is not supported. ' );
}
}
// # HELPERS
private function get_site_auth_header( $site_key, $site_secret ){
$auth = base64_encode( "{$site_key}:{$site_secret}" );
return array( 'Authorization' => 'GravityAPI ' . $auth );
}
private function get_license_auth_header( $license_key_md5 ){
$auth = base64_encode( "license:{$license_key_md5}" );
return array( 'Authorization' => 'GravityAPI ' . $auth );
}
public function prepare_response_body( $raw_response ) {
if ( is_wp_error( $raw_response ) ) {
return $raw_response;
}
else if ( $raw_response['response']['code'] != 200 ) {
return new WP_Error( 'server_error', 'Error from server: ' . $raw_response['response']['message'] );
}
$response_body = json_decode( $raw_response['body'] );
if ( $response_body === null ){
return new WP_Error( 'invalid_response', 'Invalid response from server: ' . $raw_response['body'] );
}
return $response_body;
}
public function purge_site_credentials() {
delete_option( 'gf_site_key' );
delete_option( 'gf_site_secret' );
}
public function request( $resource, $body, $method = 'POST', $options = array() ) {
$body['timestamp'] = time();
// set default options
$options = wp_parse_args( $options, array(
'method' => $method,
'timeout' => 10,
'body' => in_array( $method, array( 'GET', 'DELETE' ) ) ? null : json_encode( $body ),
'headers' => array(),
'sslverify' => false,
) );
// set default header options
$options['headers'] = wp_parse_args( $options['headers'], array(
'Content-Type' => 'application/json; charset=' . get_option( 'blog_charset' ),
'User-Agent' => 'WordPress/' . get_bloginfo( 'version' ),
'Referer' => get_bloginfo( 'url' ),
) );
// WP docs say method should be uppercase
$options['method'] = strtoupper( $options['method'] );
$request_url = $this->get_gravity_api_url() . $resource;
$raw_response = wp_remote_request( $request_url, $options );
return $raw_response;
}
public function get_site_key() {
if ( defined( 'GRAVITY_API_SITE_KEY' ) ) {
return GRAVITY_API_SITE_KEY;
}
$site_key = get_option( 'gf_site_key' );
if ( empty( $site_key ) ) {
return false;
}
return $site_key;
}
public function get_site_secret() {
if ( defined( 'GRAVITY_API_SITE_SECRET' ) ) {
return GRAVITY_API_SITE_SECRET;
}
$site_secret = get_option( 'gf_site_secret' );
if ( empty( $site_secret ) ) {
return false;
}
return $site_secret;
}
public function get_gravity_api_url() {
return trailingslashit( GRAVITY_API_URL );
}
public function ensure_site_registered() {
if ( ! $this->is_site_registered() ) {
$license_key_md5 = GFCommon::get_key();
if ( empty( $license_key_md5 ) ) {
return false;
}
$result = $this->register_current_site( $license_key_md5, true );
if ( ! $result || is_wp_error( $result ) ) {
return false;
}
}
return array(
'site_key' => $this->get_site_key(),
'site_secret' => $this->get_site_secret(),
);
}
public function is_site_registered() {
return $this->get_site_key() && $this->get_site_secret();
}
}
function gapi() {
return Gravity_Api::get_instance();
}
gapi();
}