PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Lidbary/foxyshop
PHP | 336 lines | 213 code | 45 blank | 78 comment | 26 complexity | 2e1b5d4c73f6e27d1b54bb2d5e0eae66 MD5 | raw file
  1. <?php
  2. //Exit if not called in proper context
  3. if (!defined('ABSPATH')) exit();
  4. //Decrypt Data From Source
  5. function foxyshop_decrypt($src) {
  6. global $foxyshop_settings;
  7. return rc4crypt::decrypt($foxyshop_settings['api_key'],urldecode($src));
  8. }
  9. //Push Feed to External Datafeeds
  10. function foxyshop_run_external_datafeeds($external_datafeeds) {
  11. global $foxyshop_settings;
  12. if ($foxyshop_settings["orderdesk_url"]) {
  13. //Check Referer to make sure we aren't coming from Order Desk and thus creating a loop
  14. if ($_SERVER['REMOTE_ADDR'] == "216.70.96.51") {
  15. die("It looks like you have a potential datafeed loop with FoxyShop and Order Desk. You can't send the datafeed to Order Desk and then have Order Desk send it back to FoxyShop or you'll have a never-ending loop. It's recommended that you uncheck the 'Send to Order Desk' feature in your FoxyShop Settings.");
  16. }
  17. $external_datafeeds[] = $foxyshop_settings["orderdesk_url"];
  18. }
  19. if (!defined('FOXYSHOP_CURL_CONNECTTIMEOUT')) define('FOXYSHOP_CURL_CONNECTTIMEOUT', 10); //10
  20. if (!defined('FOXYSHOP_CURL_TIMEOUT')) define('FOXYSHOP_CURL_TIMEOUT', 15); //15
  21. if (!isset($_POST["FoxyData"]) && !isset($_POST["FoxySubscriptionData"])) return;
  22. foreach($external_datafeeds as $feedurl) {
  23. if ($feedurl) {
  24. $ch = curl_init();
  25. curl_setopt($ch, CURLOPT_URL, $feedurl);
  26. if (isset($_POST["FoxyData"])) {
  27. curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $_POST["FoxyData"]));
  28. } elseif (isset($_POST["FoxySubscriptionData"])) {
  29. curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxySubscriptionData" => $_POST["FoxySubscriptionData"]));
  30. }
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  32. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, FOXYSHOP_CURL_CONNECTTIMEOUT);
  33. curl_setopt($ch, CURLOPT_TIMEOUT, FOXYSHOP_CURL_TIMEOUT);
  34. if (defined('FOXYSHOP_CURL_SSL_VERIFYPEER')) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FOXYSHOP_CURL_SSL_VERIFYPEER);
  35. $response = trim(curl_exec($ch));
  36. //If Error, Send Email and Kill Process
  37. if ($response != 'foxy' && $response != 'foxysub') {
  38. $error_msg = (!$response ? "Datafeed Processing Error: " . curl_error($ch) : $response);
  39. $to_email = get_bloginfo('admin_email');
  40. $message = "A FoxyCart datafeed error was encountered at " . date("F j, Y, g:i a") . ".\n\n";
  41. $message .= "The feed that failed was $feedurl\n\n";
  42. $message .= "The error is listed below:\n\n";
  43. $message .= $error_msg;
  44. //$message .= "\n\n" . print_r($_POST, 1);
  45. //$message .= "\n\n" . print_r($_SERVER, 1);
  46. $message .= "\n\n" . foxyshop_decrypt($_POST["FoxyData"]);
  47. $headers = 'From: ' . get_bloginfo('name') . ' Server Admin <' . $to_email . '>' . "\r\n";
  48. mail($to_email, 'Data Feed Error on ' . get_bloginfo('name'), $message, $headers);
  49. curl_close($ch);
  50. die($error_msg);
  51. } else {
  52. curl_close($ch);
  53. }
  54. }
  55. }
  56. }
  57. //Update the FoxyShop Inventory
  58. function foxyshop_datafeed_inventory_update($xml) {
  59. global $wpdb, $foxyshop_settings;
  60. //For Each Transaction
  61. foreach($xml->transactions->transaction as $transaction) {
  62. //For Each Transaction Detail
  63. foreach($transaction->transaction_details->transaction_detail as $transactiondetails) {
  64. if ((int)$transactiondetails->is_future_line_item == 1) continue;
  65. $product_name = (string)$transactiondetails->product_name;
  66. $product_code = (string)$transactiondetails->product_code;
  67. $product_quantity = (int)$transactiondetails->product_quantity;
  68. //Skip if there's no product code
  69. if (!$product_code) continue;
  70. //Get List of Target ID's for Inventory Update
  71. $meta_list = $wpdb->get_results("SELECT post_id, meta_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_inventory_levels' AND meta_value LIKE '%" . esc_sql($product_code) . "%'");
  72. foreach ($meta_list as $meta) {
  73. $productID = $meta->post_id;
  74. $val = unserialize($meta->meta_value);
  75. if (!is_array($val)) $val = array();
  76. foreach ($val as $ivcode => $iv) {
  77. if ($ivcode == $product_code) {
  78. $original_count = $iv['count'];
  79. $new_count = $original_count - $product_quantity;
  80. $alert_level = ($iv['alert'] == '' ? $foxyshop_settings['inventory_alert_level'] : $iv['alert']);
  81. $val[$ivcode]['count'] = $new_count;
  82. //Send Email Alert Email
  83. if ($foxyshop_settings['inventory_alert_email'] && $new_count <= $alert_level) {
  84. $subject_line = "Inventory Alert: " . $product_name;
  85. $to_email = apply_filters('foxyshop_inventory_alert_email', get_bloginfo('admin_email'));
  86. $message = "The inventory for one of your products is getting low:\n\n";
  87. $message .= "Product Name: $product_name\n";
  88. $message .= "Product Code: $product_code\n";
  89. $message .= "Current Inventory Level: $new_count\n";
  90. $message .= "Inventory Alert Level: $alert_level\n";
  91. $message .= "\n". get_bloginfo('wpurl') . "/wp-admin/edit.php?post_type=foxyshop_product\n";
  92. $headers = 'From: ' . get_bloginfo('name') . ' <' . $to_email . '>' . "\r\n";
  93. wp_mail($to_email, $subject_line, $message, $headers);
  94. }
  95. }
  96. }
  97. //Run the Update
  98. foxyshop_inventory_count_update($product_code, $new_count, $productID, 0);
  99. }
  100. }
  101. }
  102. }
  103. //Update the WordPress Customer's Subscription List
  104. function foxyshop_datafeed_sso_update($xml) {
  105. global $wpdb;
  106. //For Each Transaction
  107. foreach($xml->transactions->transaction as $transaction) {
  108. //Get FoxyCart Transaction Information
  109. $transaction_id = (string)$transaction->id;
  110. $customer_id = (string)$transaction->customer_id;
  111. //For Each Transaction Detail
  112. foreach($transaction->transaction_details->transaction_detail as $transactiondetails) {
  113. $product_code = (string)$transactiondetails->product_code;
  114. $sub_token_url = (string)$transactiondetails->sub_token_url;
  115. //Set Subscription Features if using SSO
  116. if ($sub_token_url != "") {
  117. //Get WordPress User ID
  118. $select_user = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'foxycart_customer_id' AND meta_value = '$customer_id'";
  119. $user_id = $wpdb->get_var($select_user);
  120. if ($user_id) {
  121. //Get User's Subscription Array
  122. $foxyshop_subscription = get_user_meta($user_id, 'foxyshop_subscription', true);
  123. if (!is_array($foxyshop_subscription)) $foxyshop_subscription = array();
  124. //Add On To Array
  125. $foxyshop_subscription[$product_code] = array(
  126. "is_active" => 1,
  127. "sub_token_url" => $sub_token_url
  128. );
  129. //Write Array Back to DB
  130. update_user_meta($user_id, 'foxyshop_subscription', $foxyshop_subscription);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. //Update or Create a WordPress User After Checkout
  137. function foxyshop_datafeed_user_update($xml) {
  138. global $wpdb, $foxyshop_new_password_hash;
  139. //For Each Transaction
  140. foreach($xml->transactions->transaction as $transaction) {
  141. //Get FoxyCart Transaction Information
  142. $customer_id = (string)$transaction->customer_id;
  143. $customer_first_name = (string)$transaction->customer_first_name;
  144. $customer_last_name = (string)$transaction->customer_last_name;
  145. $customer_email = (string)$transaction->customer_email;
  146. $customer_password = (string)$transaction->customer_password;
  147. $is_anonymous = (int)$transaction->is_anonymous;
  148. //Add or Update WordPress User If Not Guest Checkout
  149. if ($customer_id != '0' && $is_anonymous == 0) {
  150. //Check To See if WordPress User Already Exists
  151. $current_user = get_user_by("email", $customer_email);
  152. $foxyshop_new_password_hash = $customer_password;
  153. //No Return, Add New User, Username will be email address
  154. if (!$current_user) {
  155. remove_action('user_register', 'foxyshop_profile_add', 5);
  156. $new_user_id = wp_insert_user(array(
  157. 'user_login' => $customer_email,
  158. 'user_email' => $customer_email,
  159. 'first_name' => $customer_first_name,
  160. 'last_name' => $customer_last_name,
  161. 'user_email' => $customer_email,
  162. 'user_pass' => wp_generate_password(),
  163. 'user_nicename' => $customer_first_name . ' ' . $customer_last_name,
  164. 'display_name' => $customer_first_name . ' ' . $customer_last_name,
  165. 'nickname' => $customer_first_name . ' ' . $customer_last_name,
  166. 'role' => apply_filters('foxyshop_default_user_role', 'subscriber'),
  167. ));
  168. add_user_meta($new_user_id, 'foxycart_customer_id', $customer_id, true);
  169. //Set Password In WordPress Database
  170. $wpdb->query("UPDATE $wpdb->users SET user_pass = '" . esc_sql($customer_password) . "' WHERE ID = $new_user_id");
  171. //Set Original Password at FoxyCart
  172. //foxyshop_get_foxycart_data(array("api_action" => "customer_save", "customer_id" => $customer_id, "customer_password_hash" => $customer_password));
  173. //Run Your Custom Actions Here with add_action()
  174. do_action("foxyshop_datafeed_add_wp_user", $xml, $new_user_id);
  175. //Update User
  176. } else {
  177. //Set Password
  178. $wpdb->query("UPDATE $wpdb->users SET user_pass = '" . esc_sql($customer_password) . "' WHERE ID = " . $current_user->ID);
  179. //Update First Name and Last Name
  180. $updated_user_id = wp_update_user(array(
  181. 'ID' => $current_user->ID,
  182. 'first_name' => $customer_first_name,
  183. 'last_name' => $customer_last_name
  184. ));
  185. //Reset Password Again
  186. $wpdb->query("UPDATE $wpdb->users SET user_pass = '" . esc_sql($customer_password) . "' WHERE ID = " . $current_user->ID);
  187. //Add FoxyCart User ID if not added before
  188. add_user_meta($current_user->ID, 'foxycart_customer_id', $customer_id, true);
  189. //Run Your Custom Actions Here with add_action()
  190. do_action("foxyshop_datafeed_update_wp_user", $xml, $current_user->ID);
  191. }
  192. }
  193. }
  194. }
  195. //ConsoliBYTE Inventory Processor
  196. function foxyshop_consolibyte_inventory_process() {
  197. //DECRYPT (required)
  198. //-----------------------------------------------------
  199. $FoxyData_decrypted = foxyshop_decrypt($_POST["FoxyInventory"]);
  200. $xml = simplexml_load_string($FoxyData_decrypted, NULL, LIBXML_NOCDATA);
  201. //For Each Item
  202. foreach($xml->foxyinventory->item as $item) {
  203. //Set Variables
  204. $product_code = (string)$item->product_code;
  205. $quantity_on_hand = (int)$item->quantity_on_hand;
  206. //Update Inventory
  207. foxyshop_inventory_count_update($product_code, $quantity_on_hand, 0);
  208. }
  209. //All Done!
  210. die("foxyinventory");
  211. }
  212. // ======================================================================================
  213. // RC4 ENCRYPTION CLASS
  214. // Do not modify.
  215. // ======================================================================================
  216. /**
  217. * RC4Crypt 3.2
  218. *
  219. * RC4Crypt is a petite library that allows you to use RC4
  220. * encryption easily in PHP. It's OO and can produce outputs
  221. * in binary and hex.
  222. *
  223. * (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]
  224. * All Rights Reserved
  225. *
  226. * @link http://rc4crypt.devhome.org
  227. * @author Mukul Sabharwal <mjsabby@gmail.com>
  228. * @version $Id: class.rc4crypt.php,v 3.2 2006/03/10 05:47:24 mukul Exp $
  229. * @copyright Copyright &copy; 2006 Mukul Sabharwal
  230. * @license http://www.gnu.org/copyleft/gpl.html
  231. * @package RC4Crypt
  232. */
  233. class rc4crypt {
  234. /**
  235. * The symmetric encryption function
  236. *
  237. * @param string $pwd Key to encrypt with (can be binary of hex)
  238. * @param string $data Content to be encrypted
  239. * @param bool $ispwdHex Key passed is in hexadecimal or not
  240. * @access public
  241. * @return string
  242. */
  243. public static function encrypt ($pwd, $data, $ispwdHex = 0) {
  244. if ($ispwdHex) $pwd = @pack('H*', $pwd); // valid input, please!
  245. $key[] = '';
  246. $box[] = '';
  247. $cipher = '';
  248. $pwd_length = strlen($pwd);
  249. $data_length = strlen($data);
  250. for ($i = 0; $i < 256; $i++) {
  251. $key[$i] = ord($pwd[$i % $pwd_length]);
  252. $box[$i] = $i;
  253. }
  254. for ($j = $i = 0; $i < 256; $i++) {
  255. $j = ($j + $box[$i] + $key[$i]) % 256;
  256. $tmp = $box[$i];
  257. $box[$i] = $box[$j];
  258. $box[$j] = $tmp;
  259. }
  260. for ($a = $j = $i = 0; $i < $data_length; $i++) {
  261. $a = ($a + 1) % 256;
  262. $j = ($j + $box[$a]) % 256;
  263. $tmp = $box[$a];
  264. $box[$a] = $box[$j];
  265. $box[$j] = $tmp;
  266. $k = $box[(($box[$a] + $box[$j]) % 256)];
  267. $cipher .= chr(ord($data[$i]) ^ $k);
  268. }
  269. return $cipher;
  270. }
  271. /**
  272. * Decryption, recall encryption
  273. *
  274. * @param string $pwd Key to decrypt with (can be binary of hex)
  275. * @param string $data Content to be decrypted
  276. * @param bool $ispwdHex Key passed is in hexadecimal or not
  277. * @access public
  278. * @return string
  279. */
  280. public static function decrypt ($pwd, $data, $ispwdHex = 0) {
  281. return rc4crypt::encrypt($pwd, $data, $ispwdHex);
  282. }
  283. }