-
Notifications
You must be signed in to change notification settings - Fork 8
/
sso.php
executable file
·242 lines (203 loc) · 10.2 KB
/
sso.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
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
<?php
//Exit if not called in proper context
if (!defined('ABSPATH')) exit();
//When Saving Profile, These Actions Sync Data to FoxyCart
add_action('profile_update', 'foxyshop_profile_update', 5);
add_action('user_register', 'foxyshop_profile_add', 5);
add_action('password_reset', 'foxyshop_password_reset_at_foxycart', 5, 2);
//Reset the Password at FoxyCart on a WordPress Password Reset
function foxyshop_password_reset_at_foxycart($wp_user, $new_password) {
//Get User Info
$foxycart_customer_id = get_user_meta($wp_user->ID, 'foxycart_customer_id', true);
//Send Updated Info to FoxyCart
$foxy_data = array("api_action" => "customer_save");
if ($foxycart_customer_id) $foxy_data["customer_id"] = $foxycart_customer_id;
$foxy_data["customer_email"] = $wp_user->user_email;
$foxy_data["customer_password"] = $new_password;
if ($wp_user->user_firstname) $foxy_data["customer_first_name"] = $wp_user->user_firstname;
if ($wp_user->user_lastname) $foxy_data["customer_last_name"] = $wp_user->user_lastname;
$foxy_response = foxyshop_get_foxycart_data($foxy_data);
}
//Runs When WP Profile is Updated
function foxyshop_profile_update($user_id) {
global $foxyshop_new_password_hash;
//Get User Info
$foxycart_customer_id = get_user_meta($user_id, 'foxycart_customer_id', true);
//Get User Data
$wp_user = get_userdata($user_id);
//Set The New Password
$new_password = $wp_user->user_pass;
if (isset($foxyshop_new_password_hash)) $new_password = $foxyshop_new_password_hash;
//Send Updated Info to FoxyCart
$foxy_data = array("api_action" => "customer_save");
if ($foxycart_customer_id) $foxy_data["customer_id"] = $foxycart_customer_id;
$foxy_data["customer_email"] = $wp_user->user_email;
$foxy_data["customer_password_hash"] = $new_password;
if ($wp_user->user_firstname) $foxy_data["customer_first_name"] = $wp_user->user_firstname;
if ($wp_user->user_lastname) $foxy_data["customer_last_name"] = $wp_user->user_lastname;
//Hook To Add Your Own Function to Update the $foxy_data array with your own data
if (has_filter('foxyshop_save_sso_to_foxycart')) $foxy_data = apply_filters('foxyshop_save_sso_to_foxycart', $foxy_data, $user_id, "update");
$foxy_response = foxyshop_get_foxycart_data($foxy_data);
$xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
$foxycart_customer_id = (string)$xml->result != "ERROR" ? (string)$xml->customer_id : "";
//If FoxyCart Customer ID Returned, Add FoxyCart Customer ID To User Meta
if ($foxycart_customer_id) {
add_user_meta($user_id, 'foxycart_customer_id', $foxycart_customer_id, true);
}
}
//Runs When WP User is Added
function foxyshop_profile_add($user_id) {
//Get User Data
$wp_user = get_userdata($user_id);
//Set Foxy Data
$foxy_data = array("api_action" => "customer_save");
$foxy_data["customer_email"] = $wp_user->user_email;
$foxy_data["customer_password_hash"] = $wp_user->user_pass;
if ($wp_user->user_firstname) $foxy_data["customer_first_name"] = $wp_user->user_firstname;
if ($wp_user->user_lastname) $foxy_data["customer_last_name"] = $wp_user->user_lastname;
//Hook To Add Your Own Function to Update the $foxy_data array with your own data
if (has_filter('foxyshop_save_sso_to_foxycart')) $foxy_data = apply_filters('foxyshop_save_sso_to_foxycart', $foxy_data, $user_id, "add");
//Send To FoxyCart
$foxy_response = foxyshop_get_foxycart_data($foxy_data);
$xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
$foxycart_customer_id = (string)$xml->result != "ERROR" ? (string)$xml->customer_id : "";
//If FoxyCart Customer ID Returned, Add FoxyCart Customer ID To User Meta
if ($foxycart_customer_id) {
add_user_meta($user_id, 'foxycart_customer_id', $foxycart_customer_id, true);
}
//Auto-login if user wasn't logged in before
//Note that if you don't have the querystring "redirect_to" set on the registration page the page will not redirect anywhere and won't appear logged in at first
$auto_login = apply_filters("foxyshop_new_user_auto_login", true);
if (!is_user_logged_in() && $auto_login) wp_set_auth_cookie($user_id, false, is_ssl());
}
//Adds a Login Message When Prompting Users to Login Before Checking Out
if (isset($_GET['foxycart_checkout'])) {
add_filter('login_message', 'foxyshop_login_message', 2);
add_action('login_head','foxyshop_login_head', 2);
}
function foxyshop_login_head() { ?>
<style type="text/css">
#login_error, .message { display:none; }
.custom-message {
-moz-border-radius:3px 3px 3px 3px;
border-style:solid;
border-width:1px;
margin:0 0 16px 8px;
padding:12px;
}
.login .custom-message {
background-color:#FFFFE0;
border-color:#E6DB55;
}
</style><?php
}
function foxyshop_login_message() {
$message = '<p class="custom-message">' . __('Please login before checking out.', 'foxyshop') . ' <a href="' . get_bloginfo("wpurl") . '/wp-login.php?action=register">' . __('Click here to register.', 'foxyshop') . '</a></p><br />';
return $message;
}
//Setup Actions
add_action('admin_init', 'foxyshop_user_init');
function foxyshop_user_init() {
add_action('show_user_profile', 'foxyshop_action_show_user_profile');
add_action('edit_user_profile', 'foxyshop_action_show_user_profile');
add_action('personal_options_update', 'foxyshop_action_process_option_update');
add_action('edit_user_profile_update', 'foxyshop_action_process_option_update');
}
function foxyshop_action_show_user_profile($user) {
global $foxyshop_settings;
if (!current_user_can('administrator')) return;
?>
<h3><?php _e('FoxyCart User Data') ?></h3>
<table class="form-table">
<tr>
<th><label for="foxycart_customer_id"><?php _e('FoxyCart Customer ID', 'foxyshop'); ?></label></th>
<td><input type="text" name="foxycart_customer_id" id="foxycart_customer_id" value="<?php echo esc_attr(get_user_meta($user->ID, 'foxycart_customer_id', 1) ); ?>" /> <span class="description"><?php _e('Editing is not recommended', 'foxyshop'); ?></span></td>
</tr>
<?php
//Custom Hook To Allow Customization of the Content that Goes Here (add your own fields). Passes in one argument: the current user ID
//Also note that is before the </table> so anything you add should be wrapped in <tr>
do_action("foxyshop_show_user_profile_data", $user->ID);
?>
</table>
<?php
//Get User's Subscription Array
$foxyshop_subscription = get_user_meta($user->ID, 'foxyshop_subscription', true);
if (!is_array($foxyshop_subscription)) $foxyshop_subscription = array();
if (count($foxyshop_subscription) > 0) {
?>
<h3><?php _e('FoxyCart Subscriptions', 'foxyshop') ?></h3>
<table class="widefat" cellspacing="0">
<thead>
<tr>
<tr>
<th class="manage-column column-columnname" scope="col"><?php echo esc_html(FOXYSHOP_PRODUCT_NAME_SINGULAR) . ' ' . __('Code', 'foxyshop'); ?></th>
<th class="manage-column column-columnname" scope="col"><?php _e('Active', 'foxyshop'); ?></th>
<th class="manage-column column-columnname" scope="col"><?php _e('Actions', 'foxyshop'); ?></th>
</tr>
</tr>
</thead>
<tbody>
<?php
foreach ($foxyshop_subscription as $key => $val) {
$sub_token = str_replace('https://'.$foxyshop_settings['domain'].'/cart?sub_token=', "", $val['sub_token_url']);
?>
<tr class="alternate">
<td class="column-columnname"><?php echo esc_html($key); ?></td>
<td class="column-columnname"><?php echo ($val['is_active'] == 1 ? __('Yes', 'foxyshop') : __('No', 'foxyshop')); ?></td>
<td class="column-columnname"><a href="<?php echo esc_url($val['sub_token_url']); ?>&cart=checkout" target="_blank"><?php _e('Update Info', 'foxyshop');?></a> | <a href="<?php echo esc_url($val['sub_token_url']); ?>&sub_cancel=true&cart=checkout" target="_blank"><?php _e('Cancel', 'foxyshop');?></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} //End Subscription View
}
function foxyshop_action_process_option_update($user_id) {
if (!current_user_can('administrator')) return;
if (isset($_POST['foxycart_customer_id'])) update_user_meta($user_id, 'foxycart_customer_id', sanitize_text_field($_POST['foxycart_customer_id']));
}
//Keep redirect_to in URL
add_filter('site_url', 'foxyshop_add_registration_redirect', 5);
function foxyshop_add_registration_redirect($path) {
if ((strpos($path, "action=register") !== false || strpos($path, "action=lostpassword") !== false) && isset($_REQUEST['redirect_to'])) return $path . '&redirect_to='.urlencode(sanitize_text_field($_REQUEST['redirect_to']));
if (substr($path, strlen($path)-12) == "wp-login.php" && isset($_REQUEST['redirect_to'])) return $path . '?redirect_to='.urlencode(sanitize_text_field($_REQUEST['redirect_to']));
return $path;
}
//Process Reverse SSO Login
function foxyshop_reverse_sso_login() {
$redirect_url = apply_filters("foxyshop_reverse_sso_login_failed_destination", get_home_url());
$result = "failed";
if (!isset($_REQUEST['foxycart_customer_id']) || !isset($_GET['timestamp']) || !isset($_GET['fc_auth_token'])) {
wp_redirect($redirect_url);
die;
}
//Build Token
global $foxyshop_settings;
$timestamp = sanitize_text_field($_GET['timestamp']);
$current_timestamp = date("U");
$calculated_auth_token = sha1(sanitize_text_field($_GET['foxycart_customer_id']) . '|' . sanitize_text_field($_GET['timestamp']) . '|' . sanitize_text_field($foxyshop_settings['api_key']));
//Token Matches, Do Login
if ($calculated_auth_token === $_GET['fc_auth_token'] && $timestamp >= $current_timestamp) {
//Lookup ID By FoxyCart Customer ID
$wp_user_id = 0;
$user_data = get_users(array('meta_key' => 'foxycart_customer_id', 'meta_value' => sanitize_text_field($_GET['foxycart_customer_id'])));
foreach ($user_data as $user) {
$wp_user_id = $user->ID;
}
//Login
if ($wp_user_id) {
$redirect_url = apply_filters("foxyshop_reverse_sso_login_destination", get_home_url());
wp_set_auth_cookie($wp_user_id);
$result = "ok";
}
}
//Is This a JSONP Request?
if (isset($_GET['callback'])) {
echo htmlspecialchars(esc_attr($_GET['callback'])) . '({ "result": "' . esc_attr($result) . '"})';
die;
}
wp_redirect($redirect_url);
die;
}