|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class Config { |
|
7
|
|
|
/** |
|
8
|
|
|
* @var \Wordlift_Admin_Setup |
|
9
|
|
|
*/ |
|
10
|
|
|
private $admin_setup; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Wordlift_Key_Validation_Service |
|
13
|
|
|
*/ |
|
14
|
|
|
private $key_validation_service; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Config constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param $admin_setup \Wordlift_Admin_Setup |
|
20
|
|
|
* @param $key_validation_service \Wordlift_Key_Validation_Service |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct( $admin_setup, $key_validation_service ) { |
|
23
|
|
|
|
|
24
|
|
|
$this->admin_setup = $admin_setup; |
|
25
|
|
|
$this->key_validation_service = $key_validation_service; |
|
26
|
|
|
add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
27
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Raw byte array from image |
|
32
|
|
|
* |
|
33
|
|
|
* @param $image_string |
|
34
|
|
|
* |
|
35
|
|
|
* Returns png or jpeg based on header, or else returns false. |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool|int|string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function get_mime_type_from_string( $image_string ) { |
|
40
|
|
|
$mime_types = array( |
|
41
|
|
|
'jpeg' => "\xFF\xD8\xFF", |
|
42
|
|
|
'png' => "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
foreach ( $mime_types as $mime_type => $byte_value ) { |
|
46
|
|
|
if ( ! $byte_value === substr( $image_string, 0, strlen( $byte_value ) ) ) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $mime_type; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function config() { |
|
57
|
|
|
|
|
58
|
|
|
$account_info = $this->key_validation_service->get_account_info( (string) $_POST['license'] ); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* we need to check if the key is not associated with any account |
|
62
|
|
|
* before setting it, we should check if the url is null. |
|
63
|
|
|
*/ |
|
64
|
|
|
if ( is_wp_error( $account_info ) |
|
65
|
|
|
|| wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$account_info_json = $account_info['body']; |
|
70
|
|
|
|
|
71
|
|
|
$account_info_data = json_decode( $account_info_json, true ); |
|
72
|
|
|
|
|
73
|
|
|
if ( ! $account_info_data ) { |
|
74
|
|
|
// Invalid json returned by api. |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ( $account_info_data['url'] !== null ) { |
|
79
|
|
|
// key already associated with another account. |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$image_string = (string) $_POST['image']; |
|
84
|
|
|
|
|
85
|
|
|
$image_decoded_string = base64_decode( $image_string ); |
|
86
|
|
|
|
|
87
|
|
|
$upload_dir = wp_upload_dir(); |
|
88
|
|
|
|
|
89
|
|
|
$mime_type = $this->get_mime_type_from_string( $image_decoded_string ); |
|
90
|
|
|
|
|
91
|
|
|
if ( ! $mime_type ) { |
|
|
|
|
|
|
92
|
|
|
wp_send_json_error( "Image type not valid" ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $mime_type; |
|
96
|
|
|
|
|
97
|
|
|
file_put_contents( $file_path, $image_decoded_string ); |
|
98
|
|
|
|
|
99
|
|
|
$attachment_id = wp_insert_attachment( array(), $file_path ); |
|
100
|
|
|
|
|
101
|
|
|
$params = array( |
|
102
|
|
|
'send_diagnostic' => $_POST['diagnostic'], |
|
103
|
|
|
'key' => $_POST['license'], |
|
104
|
|
|
'vocabulary' => $_POST['vocabulary'], |
|
105
|
|
|
'language' => $_POST['language'], |
|
106
|
|
|
'name' => $_POST['publisherName'], |
|
107
|
|
|
'user_type' => $_POST['publisher'], |
|
108
|
|
|
'logo' => $attachment_id |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$this->admin_setup->save_configuration( $params ); |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: