PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/foxyshop/sso.php

https://gitlab.com/Lidbary/foxyshop
PHP | 242 lines | 170 code | 39 blank | 33 comment | 38 complexity | 6fcdaa6933331740c45bdf0cf5216e73 MD5 | raw file
  1. <?php
  2. //Exit if not called in proper context
  3. if (!defined('ABSPATH')) exit();
  4. //When Saving Profile, These Actions Sync Data to FoxyCart
  5. add_action('profile_update', 'foxyshop_profile_update', 5);
  6. add_action('user_register', 'foxyshop_profile_add', 5);
  7. add_action('password_reset', 'foxyshop_password_reset_at_foxycart', 5, 2);
  8. //Reset the Password at FoxyCart on a WordPress Password Reset
  9. function foxyshop_password_reset_at_foxycart($wp_user, $new_password) {
  10. //Get User Info
  11. $foxycart_customer_id = get_user_meta($wp_user->ID, 'foxycart_customer_id', true);
  12. //Send Updated Info to FoxyCart
  13. $foxy_data = array("api_action" => "customer_save");
  14. if ($foxycart_customer_id) $foxy_data["customer_id"] = $foxycart_customer_id;
  15. $foxy_data["customer_email"] = $wp_user->user_email;
  16. $foxy_data["customer_password"] = $new_password;
  17. if ($wp_user->user_firstname) $foxy_data["customer_first_name"] = $wp_user->user_firstname;
  18. if ($wp_user->user_lastname) $foxy_data["customer_last_name"] = $wp_user->user_lastname;
  19. $foxy_response = foxyshop_get_foxycart_data($foxy_data);
  20. }
  21. //Runs When WP Profile is Updated
  22. function foxyshop_profile_update($user_id) {
  23. global $foxyshop_new_password_hash;
  24. //Get User Info
  25. $foxycart_customer_id = get_user_meta($user_id, 'foxycart_customer_id', true);
  26. //Get User Data
  27. $wp_user = get_userdata($user_id);
  28. //Set The New Password
  29. $new_password = $wp_user->user_pass;
  30. if (isset($foxyshop_new_password_hash)) $new_password = $foxyshop_new_password_hash;
  31. //Send Updated Info to FoxyCart
  32. $foxy_data = array("api_action" => "customer_save");
  33. if ($foxycart_customer_id) $foxy_data["customer_id"] = $foxycart_customer_id;
  34. $foxy_data["customer_email"] = $wp_user->user_email;
  35. $foxy_data["customer_password_hash"] = $new_password;
  36. if ($wp_user->user_firstname) $foxy_data["customer_first_name"] = $wp_user->user_firstname;
  37. if ($wp_user->user_lastname) $foxy_data["customer_last_name"] = $wp_user->user_lastname;
  38. //Hook To Add Your Own Function to Update the $foxy_data array with your own data
  39. if (has_filter('foxyshop_save_sso_to_foxycart')) $foxy_data = apply_filters('foxyshop_save_sso_to_foxycart', $foxy_data, $user_id, "update");
  40. $foxy_response = foxyshop_get_foxycart_data($foxy_data);
  41. $xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
  42. $foxycart_customer_id = (string)$xml->result != "ERROR" ? (string)$xml->customer_id : "";
  43. //If FoxyCart Customer ID Returned, Add FoxyCart Customer ID To User Meta
  44. if ($foxycart_customer_id) {
  45. add_user_meta($user_id, 'foxycart_customer_id', $foxycart_customer_id, true);
  46. }
  47. }
  48. //Runs When WP User is Added
  49. function foxyshop_profile_add($user_id) {
  50. //Get User Data
  51. $wp_user = get_userdata($user_id);
  52. //Set Foxy Data
  53. $foxy_data = array("api_action" => "customer_save");
  54. $foxy_data["customer_email"] = $wp_user->user_email;
  55. $foxy_data["customer_password_hash"] = $wp_user->user_pass;
  56. if ($wp_user->user_firstname) $foxy_data["customer_first_name"] = $wp_user->user_firstname;
  57. if ($wp_user->user_lastname) $foxy_data["customer_last_name"] = $wp_user->user_lastname;
  58. //Hook To Add Your Own Function to Update the $foxy_data array with your own data
  59. if (has_filter('foxyshop_save_sso_to_foxycart')) $foxy_data = apply_filters('foxyshop_save_sso_to_foxycart', $foxy_data, $user_id, "add");
  60. //Send To FoxyCart
  61. $foxy_response = foxyshop_get_foxycart_data($foxy_data);
  62. $xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
  63. $foxycart_customer_id = (string)$xml->result != "ERROR" ? (string)$xml->customer_id : "";
  64. //If FoxyCart Customer ID Returned, Add FoxyCart Customer ID To User Meta
  65. if ($foxycart_customer_id) {
  66. add_user_meta($user_id, 'foxycart_customer_id', $foxycart_customer_id, true);
  67. }
  68. //Auto-login if user wasn't logged in before
  69. //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
  70. $auto_login = apply_filters("foxyshop_new_user_auto_login", true);
  71. if (!is_user_logged_in() && $auto_login) wp_set_auth_cookie($user_id, false, is_ssl());
  72. }
  73. //Adds a Login Message When Prompting Users to Login Before Checking Out
  74. if (isset($_GET['foxycart_checkout'])) {
  75. add_filter('login_message', 'foxyshop_login_message', 2);
  76. add_action('login_head','foxyshop_login_head', 2);
  77. }
  78. function foxyshop_login_head() { ?>
  79. <style type="text/css">
  80. #login_error, .message { display:none; }
  81. .custom-message {
  82. -moz-border-radius:3px 3px 3px 3px;
  83. border-style:solid;
  84. border-width:1px;
  85. margin:0 0 16px 8px;
  86. padding:12px;
  87. }
  88. .login .custom-message {
  89. background-color:#FFFFE0;
  90. border-color:#E6DB55;
  91. }
  92. </style><?php
  93. }
  94. function foxyshop_login_message() {
  95. $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 />';
  96. return $message;
  97. }
  98. //Setup Actions
  99. add_action('admin_init', 'foxyshop_user_init');
  100. function foxyshop_user_init() {
  101. add_action('show_user_profile', 'action_show_user_profile');
  102. add_action('edit_user_profile', 'action_show_user_profile');
  103. add_action('personal_options_update', 'action_process_option_update');
  104. add_action('edit_user_profile_update', 'action_process_option_update');
  105. }
  106. function action_show_user_profile($user) {
  107. global $foxyshop_settings;
  108. if (!current_user_can('administrator')) return;
  109. ?>
  110. <h3><?php _e('FoxyCart User Data') ?></h3>
  111. <table class="form-table">
  112. <tr>
  113. <th><label for="foxycart_customer_id"><?php _e('FoxyCart Customer ID', 'foxyshop'); ?></label></th>
  114. <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>
  115. </tr>
  116. <?php
  117. //Custom Hook To Allow Customization of the Content that Goes Here (add your own fields). Passes in one argument: the current user ID
  118. //Also note that is before the </table> so anything you add should be wrapped in <tr>
  119. do_action("foxyshop_show_user_profile_data", $user->ID);
  120. ?>
  121. </table>
  122. <?php
  123. //Get User's Subscription Array
  124. $foxyshop_subscription = get_user_meta($user->ID, 'foxyshop_subscription', true);
  125. if (!is_array($foxyshop_subscription)) $foxyshop_subscription = array();
  126. if (count($foxyshop_subscription) > 0) {
  127. ?>
  128. <h3><?php _e('FoxyCart Subscriptions', 'foxyshop') ?></h3>
  129. <table class="widefat" cellspacing="0">
  130. <thead>
  131. <tr>
  132. <tr>
  133. <th class="manage-column column-columnname" scope="col"><?php echo FOXYSHOP_PRODUCT_NAME_SINGULAR . ' ' . __('Code', 'foxyshop'); ?></th>
  134. <th class="manage-column column-columnname" scope="col"><?php _e('Active', 'foxyshop'); ?></th>
  135. <th class="manage-column column-columnname" scope="col"><?php _e('Actions', 'foxyshop'); ?></th>
  136. </tr>
  137. </tr>
  138. </thead>
  139. <tbody>
  140. <?php
  141. foreach ($foxyshop_subscription as $key => $val) {
  142. $sub_token = str_replace('https://'.$foxyshop_settings['domain'].'/cart?sub_token=', "", $val['sub_token_url']);
  143. ?>
  144. <tr class="alternate">
  145. <td class="column-columnname"><?php echo $key; ?></td>
  146. <td class="column-columnname"><?php echo ($val['is_active'] == 1 ? __('Yes', 'foxyshop') : __('No', 'foxyshop')); ?></td>
  147. <td class="column-columnname"><a href="<?php echo $val['sub_token_url']; ?>&amp;cart=checkout" target="_blank"><?php _e('Update Info', 'foxyshop');?></a> | <a href="<?php echo $val['sub_token_url']; ?>&amp;sub_cancel=true&amp;cart=checkout" target="_blank"><?php _e('Cancel', 'foxyshop');?></a></td>
  148. </tr>
  149. <?php
  150. }
  151. ?>
  152. </tbody>
  153. </table>
  154. <?php
  155. } //End Subscription View
  156. }
  157. function action_process_option_update($user_id) {
  158. if (!current_user_can('administrator')) return;
  159. if (isset($_POST['foxycart_customer_id'])) update_user_meta($user_id, 'foxycart_customer_id', $_POST['foxycart_customer_id']);
  160. }
  161. //Keep redirect_to in URL
  162. add_filter('site_url', 'foxyshop_add_registration_redirect', 5);
  163. function foxyshop_add_registration_redirect($path) {
  164. if ((strpos($path, "action=register") !== false || strpos($path, "action=lostpassword") !== false) && isset($_REQUEST['redirect_to'])) return $path . '&amp;redirect_to='.urlencode($_REQUEST['redirect_to']);
  165. if (substr($path, strlen($path)-12) == "wp-login.php" && isset($_REQUEST['redirect_to'])) return $path . '?redirect_to='.urlencode($_REQUEST['redirect_to']);
  166. return $path;
  167. }
  168. //Process Reverse SSO Login
  169. function foxyshop_reverse_sso_login() {
  170. $redirect_url = apply_filters("foxyshop_reverse_sso_login_failed_destination", get_home_url());
  171. $result = "failed";
  172. if (!isset($_REQUEST['foxycart_customer_id']) || !isset($_GET['timestamp']) || !isset($_GET['fc_auth_token'])) {
  173. wp_redirect($redirect_url);
  174. die;
  175. }
  176. //Build Token
  177. global $foxyshop_settings;
  178. $timestamp = $_GET['timestamp'];
  179. $current_timestamp = date("U");
  180. $calculated_auth_token = sha1($_GET['foxycart_customer_id'] . '|' . $_GET['timestamp'] . '|' . $foxyshop_settings['api_key']);
  181. //Token Matches, Do Login
  182. if ($calculated_auth_token === $_GET['fc_auth_token'] && $timestamp >= $current_timestamp) {
  183. //Lookup ID By FoxyCart Customer ID
  184. $wp_user_id = 0;
  185. $user_data = get_users(array('meta_key' => 'foxycart_customer_id', 'meta_value' => $_GET['foxycart_customer_id']));
  186. foreach ($user_data as $user) {
  187. $wp_user_id = $user->ID;
  188. }
  189. //Login
  190. if ($wp_user_id) {
  191. $redirect_url = apply_filters("foxyshop_reverse_sso_login_destination", get_home_url());
  192. wp_set_auth_cookie($wp_user_id);
  193. $result = "ok";
  194. }
  195. }
  196. //Is This a JSONP Request?
  197. if (isset($_GET['callback'])) {
  198. echo htmlspecialchars($_GET['callback']) . '({ "result": "' . $result . '"})';
  199. die;
  200. }
  201. wp_redirect($redirect_url);
  202. die;
  203. }