-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvltp-shortcode.php
More file actions
409 lines (323 loc) · 11.4 KB
/
Copy pathvltp-shortcode.php
File metadata and controls
409 lines (323 loc) · 11.4 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/**
* Process the [vltp] shortcode.
*
* @param array $attrs {
* Shortcode attributes.
*
* @type int $id The VPN test ID
* }
*
* @return string Content with shortcode parsed
*/
function vltp_shortcode($attrs) {
global $wpdb;
extract( shortcode_atts( array(
'id' => 1,
), $attrs ) );
if ( !$id ) {
return '';
}
$id = intval( $id );
// Retrives the Test configuration
$row = $wpdb->get_row( 'SELECT * FROM '.$wpdb->prefix.'vltp WHERE vltp_id='.$id, ARRAY_A );
if ( !$row ) {
return '';
}
if ( !in_array( $row['vltp_type'], array( 'dns', 'email', 'webrtc', 'torrent' ) ) ) {
return '';
}
$content = '';
//
$options = @unserialize( stripslashes( $row['vltp_options'] ) );
// Extract the Test configuration from the database table `vltp`
// For fields description see file vltp_admin_page.php, function vltp_admin_page()
$row['vltp_progress_image'] = isset( $options['vltp_progress_image'] ) ? stripslashes( $options['vltp_progress_image'] ) : '';
$row['vltp_start'] = isset( $options['vltp_start'] ) ? stripslashes( $options['vltp_start'] ) : '';
$row['vltp_result'] = isset( $options['vltp_result'] ) ? stripslashes( $options['vltp_result'] ) : '';
$row['vltp_conclusion'] = isset( $options['vltp_conclusion'] ) ? stripslashes( $options['vltp_conclusion'] ) : '';
wp_enqueue_style( 'vltp-css' );
wp_enqueue_script( 'vltp-js' );
$image = '';
if ( $row['vltp_progress_image'] ) {
$arr = wp_get_attachment_image_src( intval( $row['vltp_progress_image'] ), 'medium', false );
if ( isset( $arr[0] ) ) {
$image = $arr[0];
}
}
if ( !$image ) {
$image = plugin_dir_url( __FILE__ ).'include/ajax-loader.gif';
}
global $wp;
$script_array = array( );
$script_array['vltp_progress_image'] = $image;
$script_array['vltp_test_id'] = mt_rand( 100000000, 999999999 );
$script_array['vltp_url'] = home_url( add_query_arg( array( 'vltp_test_id'=>$script_array['vltp_test_id'], 'vltp_id'=>$id ), $wp->request ) );
$script_array['vltp_ajax_url'] = admin_url( 'admin-ajax.php' );
if ( $row['vltp_type'] == 'email' ) {
$m = $script_array['vltp_test_id'].'@bash.ws';
$script_array['vltp_email_message'] = sprintf( __('Please send an email to <a href="mailto:%s">%s</a>. The email subject and body doesn\'t matter. Do not refresh the page.', 'vpn-leaks-test') ,$m, $m );
}
if ( $row['vltp_type'] == 'torrent' ) {
$m = 'https://bash.ws/torrent/'.$script_array['vltp_test_id'];
$script_array['vltp_torrent_message'] = sprintf( __('Please download the torrent <a href="%s">%s.torrent</a>. Do not refresh the page.', 'vpn-leaks-test') ,$m, $script_array['vltp_test_id'] );
}
wp_localize_script( 'vltp-js', 'vltp_settings_'.$id, $script_array );
$content = '<div class="vltp-test">';
$content.= '<div class="vltp-start" data-type="'.esc_attr( $row['vltp_type'] ).'" data-id="'.esc_attr( $id ).'">'.$row['vltp_start'].'</div>';
$test_id = isset( $_REQUEST['vltp_test_id'] ) ? intval( $_REQUEST['vltp_test_id'] ) : 0;
$vltp_id = isset( $_REQUEST['vltp_id'] ) ? intval( $_REQUEST['vltp_id'] ) : 0;
if ( $test_id && $vltp_id == $id ) {
$content .= vltp_test_result( $row );
}
$content .= '</div>';
return $content;
}
/**
* Receives and displays the results of testing from 3rd party service bash.ws
*
* @param array $row {
* The VPN test configuration
*
* @type string $vltp_type VPN test type
* @type string $vltp_result Line by line HTML to format the VPN test result
* @type string $vltp_conclusion Line by line HTML to format the VPN test conclusion
* }
*
* @return string The result of testing
*/
function vltp_test_result( $row ) {
$content = '';
$test_id = isset( $_REQUEST['vltp_test_id'] ) ? intval( $_REQUEST['vltp_test_id'] ) : 0;
if ( !$test_id ) {
return '';
}
if ($row['vltp_type'] == 'dns') {
$url = 'https://bash.ws/dnsleak/test/'.$test_id;
}
else if ($row['vltp_type'] == 'email') {
$url = 'https://bash.ws/email-leak-test/test/'.$test_id;
}
else if ($row['vltp_type'] == 'webrtc') {
$url = 'https://bash.ws/webrtc-leak-test/test/'.$test_id;
}
else if ($row['vltp_type'] == 'torrent') {
$url = 'https://bash.ws/torrent-leak-test/test/'.$test_id;
}
else {
return '';
}
$ip = isset( $_REQUEST['ip'] ) ? sanitize_text_field( $_REQUEST['ip'] ) : '';
if (!$ip) {
$ip = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : '';
}
if ( !$ip ) {
$ip = isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
}
if ( !$ip ) {
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
}
if ( !$ip ) {
return '';
}
$data = array('ip' => $ip);
/**
* Receives array of JSON objects from the 3rd party service bash.ws
*
* @param string $ip The user IP address
*
* @return array $results {
*
* @type string $type The result type: "dns" - DNS leak test, "webrtc" - WebRtc test, "email" - Email test, "conclusion" - test conclusion
* @type string $ip IP address
* @type int $country Country code of IP address
* @type string $country_name Country name of IP address
* @type string $asn ASN name of IP address
*
* }
*
*/
$response = wp_remote_post( esc_url_raw( $url.'?json' ), array( 'body' => $data ) );
$results = array();
if ( !is_wp_error( $response ) ) {
$results = @json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $results['error'] ) ) {
$results = array();
}
}
if ( $results ) {
$total = 0;
foreach ($results as $k=>$v) {
if ( !isset( $v['type'] ) ) {
continue;
}
if ( $row['vltp_type'] == 'dns' ) {
if ( $v['type'] != 'dns' ) {
continue;
}
}
else if ($row['vltp_type'] == 'email') {
if ($v['type'] != 'mail') {
continue;
}
}
else if ($row['vltp_type'] == 'webrtc') {
if ($v['type'] != 'webrtc') {
continue;
}
}
else if ($row['vltp_type'] == 'torrent') {
if ($v['type'] != 'torrent') {
continue;
}
}
$total ++;
}
$content .= '<div class="vltp-title">';
if ( $row['vltp_type'] == 'dns' ) {
$content .= sprintf( _n('You use %d DNS server', 'You use %d DNS servers', $total, 'vpn-leaks-test'), $total);
}
else if ( $row['vltp_type'] == 'email' ) {
$content .= sprintf( _n('Your email contains %d IP', 'Your email contains %d IPs', $total, 'vpn-leaks-test'), $total );
}
else if ( $row['vltp_type'] == 'webrtc' ) {
$content .= sprintf( _n( 'WebRTC is able to see %d IP', 'WebRTC is able to see %d IPs', $total, 'vpn-leaks-test' ), $total );
}
else if ( $row['vltp_type'] == 'torrent' ) {
$content .= sprintf( _n('Your torrent application shares %d IP', 'Your torrent application shares %d IPs', $total, 'vpn-leaks-test'), $total );
}
$content .= '</div>';
$content .= '<div class="vltp-results">';
foreach ( $results as $k=>$v ) {
$check_fields = array( 'country', 'country_name', 'ip', 'type');
foreach ( $check_fields as $field ) {
if ( !isset( $v[$field] ) || !$v[$field] ) {
continue;
}
}
if ( $row['vltp_type'] == 'dns' ) {
if ( $v['type'] != 'dns' ) {
continue;
}
}
else if ( $row['vltp_type'] == 'email' ) {
if ( $v['type'] != 'mail' ) {
continue;
}
}
else if ( $row['vltp_type'] == 'webrtc' ) {
if ( $v['type'] != 'webrtc' ) {
continue;
}
}
else if ( $row['vltp_type'] == 'torrent' ) {
if ( $v['type'] != 'torrent' ) {
continue;
}
}
$r = array();
$r['%country_code'] = esc_attr( $v['country'] );
$r['%country_name'] = esc_attr( $v['country_name'] );
$r['%asn'] = esc_attr( $v['asn'] );
$r['%ip'] = esc_attr( $v['ip'] );
$r['%flag'] = plugin_dir_url( __FILE__ ).'include/flags/'.esc_attr( $v['country'] ).'.png';
$content .= str_replace( array_keys( $r ), array_values( $r ), $row['vltp_result'] );
}
$content .= '</div>';
$content .= '<div class="vltp-conclusion">';
foreach ( $results as $k=>$v ) {
if ( !isset( $v['type'] ) ) {
continue;
}
if ( $v['type'] != 'conclusion' ) {
continue;
}
$r = array();
$r['%text'] = esc_attr( $v['ip'] );
$content .= str_replace( array_keys($r), array_values($r), $row['vltp_conclusion'] );
}
$content .= '</div>';
}
else {
$content.= '<div class="vltp-results">'.__( 'The Test failed, please try again...', 'vpn-leaks-test' ).'</div>';
}
return $content;
}
/**
* Checks to see if the IP for testing was received and analyzed by the bash.ws
* The bash.ws sends JSON object as an answer:
* [{
* 'type': 'done',
* 'done': '1'
* }]
* if everything is ok the field 'done' is equal to '1', otherwise it is equal to '0'
*
* @return null
*/
function vltp_test_webrtc() {
$ips = isset( $_REQUEST['ips'] ) ? (array) $_REQUEST['ips'] : array();
$ips = array_map( 'sanitize_text_field', $ips );
$ip = isset( $_REQUEST['ip'] ) ? sanitize_text_field( $_REQUEST['ip'] ) : '';
$test_id = isset( $_REQUEST['vltp_test_id'] ) ? intval( $_REQUEST['vltp_test_id'] ) : 0;
if (!$ip) {
$ip = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : '';
}
if (!$ip) {
$ip = isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
}
if (!$ip) {
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
}
$url = 'https://bash.ws/webrtc-leak-test/test/'.$test_id;
$data = array( 'ajax' => '1', 'ips'=>$ips, 'ip'=>$ip );
$response = wp_remote_post( esc_url_raw( $url ), array( 'body' => $data ) );
if ( is_wp_error( $response ) ) {
wp_send_json( array( 'done' => 0, 'error' => __( 'The internal communication error occurred...', 'vpn-leak-test') ) );
}
$json_data = json_decode( wp_remote_retrieve_body( $response ), true );
wp_send_json( $json_data );
}
/**
* Checks to see if the Email for testing was received and analyzed by the bash.ws
* The bash.ws sends JSON object as an answer:
* [{
* 'type': 'done',
* 'done': '1'
* }]
* if everything is ok the field 'done' is equal to '1', otherwise it is equal to '0'
*
* @return null
*/
function vltp_test_email_check() {
$test_id = isset( $_REQUEST['vltp_test_id'] ) ? intval( $_REQUEST['vltp_test_id'] ) : 0;
$url = 'https://bash.ws/email-leak-test/test/'.$test_id;
$data = array( 'ajax' => '1' );
$response = wp_remote_post( esc_url_raw( $url ), array( 'body' => $data ) );
if ( is_wp_error( $response ) ) {
wp_send_json( array( 'done' => 0, 'error' => __( 'The internal communication error occurred...', 'vpn-leak-test') ) );
}
$json_data = json_decode( wp_remote_retrieve_body( $response ), true );
wp_send_json( $json_data );
}
/**
* Checks to see if the Torrent for testing downloaded and analyzed by the bash.ws
* The bash.ws sends JSON object as an answer:
* [{
* 'type': 'done',
* 'done': '1'
* }]
* if everything is ok the field 'done' is equal to '1', otherwise it is equal to '0'
*
* @return null
*/
function vltp_test_torrent_check() {
$test_id = isset( $_REQUEST['vltp_test_id'] ) ? intval( $_REQUEST['vltp_test_id'] ) : 0;
$url = 'https://bash.ws/torrent-leak-test/test/'.$test_id;
$data = array( 'ajax' => '1' );
$response = wp_remote_post( esc_url_raw( $url ), array( 'body' => $data ) );
if ( is_wp_error( $response ) ) {
wp_send_json( array( 'done' => 0, 'error' => __( 'The internal communication error occurred...', 'vpn-leak-test') ) );
}
$json_data = json_decode( wp_remote_retrieve_body( $response ), true );
wp_send_json( $json_data );
}