/
CF7EWProcessPost.php
142 lines (113 loc) · 5.47 KB
/
CF7EWProcessPost.php
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
<?php
if (!defined('ABSPATH'))
exit; // Exit if accessed directly
require_once("CF7EWFunctions.php");
global $wpdb;
/*
* Process updates in Contact form 7 Eway extension plugin
*/
if (isset($_POST[CF7EW_SUBMIT_FIELD]) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'login') && current_user_can('manage_options')) {
$table = $wpdb->prefix . "" . CF7EW_SETTINGS_TABLE;
$url = addslashes($_POST[CF7EW_URL_FIELD]);
$user = addslashes($_POST[CF7EW_USER_FIELD]);
$clientid = addslashes($_POST[CF7EW_CLIENTID_FIELD]);
$clientsecret = addslashes($_POST[CF7EW_CLIENTSECRET_FIELD]);
try {
$connector = new eWayConnector($url, $user, null, false, false, true, CF7EW_VERSION, $clientid, $clientsecret);
$codeVerifier = $connector->generateCodeVerifier();
$codeChallenge = $connector->getCodeChallenge($codeVerifier);
// Delete any previous settings
deleteSettings($wpdb);
$sql = "SELECT * FROM " . $table;
$r = $wpdb->get_row($sql, ARRAY_A);
if ($wpdb->num_rows == 0) {
$data = array(CF7EW_URL_FIELD => $url, CF7EW_USER_FIELD => $user, CF7EW_CLIENTID_FIELD => $clientid, CF7EW_CLIENTSECRET_FIELD => $clientsecret, CF7EW_CODEVERIFIER_FIELD => $codeVerifier);
$query = $wpdb->insert($table, $data);
if (!$query) {
CF7EWEchoError('Unable to save credentials: ' . $wpdb->last_error);
return;
}
}
$redirectUrl = $connector->getAuthorizationUrl(getRedirectUrl(), $codeChallenge, $user, true);
if (empty($redirectUrl)) {
CF7EWEchoError('Unable to get authorization URL.');
return;
}
CF7EWEchoInfo("Redirecting...<script>window.location.href = \"" . $redirectUrl . "\"</script>");
} catch (Exception $e) {
CF7EWEchoError($e->getMessage());
}
}
if (isset($_GET[CF7EW_CODE_PARAM]) && isset($_GET[CF7EW_WSURL_PARAM])) {
try {
$table = $wpdb->prefix . "" . CF7EW_SETTINGS_TABLE;
$sql = "SELECT * FROM " . $table;
$row = $wpdb->get_row($sql, ARRAY_A);
$connector = new eWayConnector($row[CF7EW_URL_FIELD], $row[CF7EW_USER_FIELD], null, false, false, true, CF7EW_VERSION, $row[CF7EW_CLIENTID_FIELD], $row[CF7EW_CLIENTSECRET_FIELD]);
$response = $connector->finishAuthorization(getRedirectUrl(), $row[CF7EW_CODEVERIFIER_FIELD], $_GET[CF7EW_CODE_PARAM]);
if (empty($response->refresh_token)) {
CF7EWEchoError('There is not refresh token in the response.');
return;
}
if (!$connector->getUserGuid()) {
CF7EWEchoError('Unable to check connection.');
return;
}
CF7EWLogMsg("Connection successful.\n");
$wpdb->query("UPDATE " . $table . " SET " . CF7EW_REFRESHTOKEN_FIELD . " = '" . $response->refresh_token . "', " . CF7EW_CODEVERIFIER_FIELD . " = NULL");
} catch (Exception $e) {
CF7EWEchoError($e->getMessage());
CF7EWLogMsg($e);
}
}
if (isset($_POST[CF7EW_LOGOUT_FIELD]) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'logout') && current_user_can('manage_options')) {
deleteSettings($wpdb);
}
if (isset($_POST[CF7EW_ADD_FIELD]) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'fields') && current_user_can('manage_options')) {
$table = $wpdb->prefix . "" . CF7EW_FIELDS_TABLE;
$query = $wpdb->insert($table, array(CF7EW_FIELD_KEY => sanitize_text_field($_POST["wordpress"]), CF7EW_FIELD_VALUE => sanitize_text_field($_POST["eway"])));
if (!$query) {
CF7EWEchoError('Unable to add new field: ' . $wpdb->last_error);
} else {
CF7EWEchoInfo('Contact form 7 eWay-CRM extension custom field was succesfully created.');
}
}
if (isset($_POST[CF7EW_RESTORE_DEFAULT]) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'fields') && current_user_can('manage_options')) {
$wpdb->query("TRUNCATE TABLE " . $wpdb->prefix . "" . CF7EW_FIELDS_TABLE);
$wpdb->insert($wpdb->prefix . "" . CF7EW_FIELDS_TABLE, array(CF7EW_FIELD_KEY => "your-email", CF7EW_FIELD_VALUE => "Email"));
$wpdb->insert($wpdb->prefix . "" . CF7EW_FIELDS_TABLE, array(CF7EW_FIELD_KEY => "your-subject", CF7EW_FIELD_VALUE => "FileAs"));
$wpdb->insert($wpdb->prefix . "" . CF7EW_FIELDS_TABLE, array(CF7EW_FIELD_KEY => "your-message", CF7EW_FIELD_VALUE => "Note"));
CF7EWLogMsg("Custom fields were restored to default state.\n");
}
if (isset($_POST[CF7EW_DELETE_FIELD]) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'delete_field') && current_user_can('manage_options')) {
$table = $wpdb->prefix . "" . CF7EW_FIELDS_TABLE;
$query = $wpdb->delete($table, array(CF7EW_ID_FIELD => $_POST[CF7EW_ID_FIELD]));
if (!$query) {
CF7EWEchoError('Unable to delete field: ' . $wpdb->last_error);
} else {
CF7EWEchoInfo('Contact form 7 eWay-CRM extension custom field was succesfully deleted.');
}
}
function CF7EWEchoError($msg)
{
echo '<div style="margin: 10px; border: 2px solid red; padding: 10px;">';
echo $msg;
echo '</div>';
CF7EWLogMsg($msg . "\n");
}
function CF7EWEchoInfo($msg)
{
echo '<div style="margin: 10px; border: 2px solid green; padding: 10px;">';
echo $msg;
echo '</div>';
CF7EWLogMsg($msg . "\n");
}
function getRedirectUrl()
{
return admin_url() . "options-general.php?page=" . CF7EW_ADMIN_PAGE;
}
function deleteSettings($wpdb)
{
$wpdb->query("TRUNCATE TABLE " . $wpdb->prefix . "" . CF7EW_SETTINGS_TABLE);
}
?>