PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/ajax.functions.php

https://github.com/AaronFernandes/aquestionof
PHP | 1070 lines | 820 code | 160 blank | 90 comment | 304 complexity | 786d4d27443fbd3a80911a99d2f28c1d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * WP eCommerce AJAX and Init functions
  4. *
  5. * These are the WPSC AJAX and Init functions
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. function wpsc_special_widget() {
  11. global $wpdb;
  12. wpsc_add_to_cart();
  13. }
  14. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && ($_REQUEST['wpsc_ajax_action'] == 'special_widget' || $_REQUEST['wpsc_ajax_action'] == 'donations_widget') ) {
  15. add_action( 'init', 'wpsc_special_widget' );
  16. }
  17. /**
  18. * add_to_cart function, used through ajax and in normal page loading.
  19. * No parameters, returns nothing
  20. */
  21. function wpsc_add_to_cart() {
  22. global $wpdb, $wpsc_cart;
  23. /// default values
  24. $default_parameters['variation_values'] = null;
  25. $default_parameters['quantity'] = 1;
  26. $default_parameters['provided_price'] = null;
  27. $default_parameters['comment'] = null;
  28. $default_parameters['time_requested'] = null;
  29. $default_parameters['custom_message'] = null;
  30. $default_parameters['file_data'] = null;
  31. $default_parameters['is_customisable'] = false;
  32. $default_parameters['meta'] = null;
  33. $provided_parameters = array();
  34. /// sanitise submitted values
  35. $product_id = (int)$_POST['product_id'];
  36. // compatibility with older themes
  37. if ( isset( $_POST['wpsc_quantity_update'] ) && is_array( $_POST['wpsc_quantity_update'] ) ) {
  38. $_POST['wpsc_quantity_update'] = $_POST['wpsc_quantity_update'][$product_id];
  39. }
  40. if(isset($_POST['variation'])){
  41. foreach ( (array)$_POST['variation'] as $key => $variation )
  42. $provided_parameters['variation_values'][(int)$key] = (int)$variation;
  43. if ( count( $provided_parameters['variation_values'] ) > 0 ) {
  44. $variation_product_id = wpsc_get_child_object_in_terms( $product_id, $provided_parameters['variation_values'], 'wpsc-variation' );
  45. if ( $variation_product_id > 0 )
  46. $product_id = $variation_product_id;
  47. }
  48. }
  49. if ((isset($_POST['quantity']) && $_POST['quantity'] > 0) && (!isset( $_POST['wpsc_quantity_update'] )) ) {
  50. $provided_parameters['quantity'] = (int)$_POST['quantity'];
  51. } else if ( isset( $_POST['wpsc_quantity_update'] ) ) {
  52. $wpsc_cart->remove_item( $_POST['key'] );
  53. $provided_parameters['quantity'] = (int)$_POST['wpsc_quantity_update'];
  54. }
  55. if (isset( $_POST['is_customisable']) && $_POST['is_customisable'] == 'true' ) {
  56. $provided_parameters['is_customisable'] = true;
  57. if ( isset( $_POST['custom_text'] ) ) {
  58. $provided_parameters['custom_message'] = $_POST['custom_text'];
  59. }
  60. if ( isset( $_FILES['custom_file'] ) ) {
  61. $provided_parameters['file_data'] = $_FILES['custom_file'];
  62. }
  63. }
  64. if ( isset($_POST['donation_price']) && ((float)$_POST['donation_price'] > 0 ) ) {
  65. $provided_parameters['provided_price'] = (float)$_POST['donation_price'];
  66. }
  67. $parameters = array_merge( $default_parameters, (array)$provided_parameters );
  68. $state = $wpsc_cart->set_item( $product_id, $parameters );
  69. $product = get_post( $product_id );
  70. if ( $state == true ) {
  71. $cart_messages[] = str_replace( "[product_name]", stripslashes( $product->post_title ), __( 'You just added "[product_name]" to your cart.', 'wpsc' ) );
  72. } else {
  73. if ( $parameters['quantity'] <= 0 ) {
  74. $cart_messages[] = __( 'Sorry, but you cannot add zero items to your cart', 'wpsc' );
  75. } else if ( $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'], $parameters['quantity'] ) > 0 ) {
  76. $quantity = $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'], $parameters['quantity'] );
  77. $cart_messages[] = sprintf( _n( 'Sorry, but there is only %s of this item in stock.', 'Sorry, but there are only %s of this item in stock.', $quantity, 'wpsc' ), $quantity );
  78. } else {
  79. $cart_messages[] = sprintf( __( 'Sorry, but the item "%s" is out of stock.', 'wpsc' ), $product->post_title );
  80. }
  81. }
  82. if ( isset($_GET['ajax']) && $_GET['ajax'] == 'true' ) {
  83. if ( ($product_id != null) && (get_option( 'fancy_notifications' ) == 1) ) {
  84. echo "if(jQuery('#fancy_notification_content')) {\n\r";
  85. echo " jQuery('#fancy_notification_content').html(\"" . str_replace( array( "\n", "\r" ), array( '\n', '\r' ), addslashes( fancy_notification_content( $cart_messages ) ) ) . "\");\n\r";
  86. echo " jQuery('#loading_animation').css('display', 'none');\n\r";
  87. echo " jQuery('#fancy_notification_content').css('display', 'block');\n\r";
  88. echo "}\n\r";
  89. $error_messages = array( );
  90. }
  91. ob_start();
  92. include_once( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
  93. $output = ob_get_contents();
  94. ob_end_clean();
  95. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  96. echo "jQuery('div.shopping-cart-wrapper').html('$output');\n";
  97. if ( get_option( 'show_sliding_cart' ) == 1 ) {
  98. if ( (wpsc_cart_item_count() > 0) || (count( $cart_messages ) > 0) ) {
  99. $_SESSION['slider_state'] = 1;
  100. echo "
  101. jQuery('#sliding_cart').slideDown('fast',function(){
  102. jQuery('#fancy_collapser').attr('src', ('".WPSC_CORE_IMAGES_URL."/minus.png'));
  103. });
  104. ";
  105. } else {
  106. $_SESSION['slider_state'] = 0;
  107. echo "
  108. jQuery('#sliding_cart').slideUp('fast',function(){
  109. jQuery('#fancy_collapser').attr('src', ('".WPSC_CORE_IMAGES_URL."/plus.png'));
  110. });
  111. ";
  112. }
  113. }
  114. echo "jQuery('.cart_message').delay(3000).slideUp(500);";
  115. do_action( 'wpsc_alternate_cart_html', $cart_messages );
  116. exit();
  117. }
  118. }
  119. // execute on POST and GET
  120. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && ($_REQUEST['wpsc_ajax_action'] == 'add_to_cart') ) {
  121. add_action( 'init', 'wpsc_add_to_cart' );
  122. }
  123. function wpsc_get_cart() {
  124. global $wpdb, $wpsc_cart;
  125. ob_start();
  126. include_once( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
  127. $output = ob_get_contents();
  128. ob_end_clean();
  129. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  130. echo "jQuery('div.shopping-cart-wrapper').html('$output');\n";
  131. if ( get_option( 'show_sliding_cart' ) == 1 ) {
  132. if ( (wpsc_cart_item_count() > 0) || (count( $cart_messages ) > 0) ) {
  133. $_SESSION['slider_state'] = 1;
  134. echo "
  135. jQuery('#sliding_cart').slideDown('fast',function(){
  136. jQuery('#fancy_collapser').attr('src', (WPSC_CORE_IMAGES_URL+'/minus.png'));
  137. });
  138. ";
  139. } else {
  140. $_SESSION['slider_state'] = 0;
  141. echo "
  142. jQuery('#sliding_cart').slideUp('fast',function(){
  143. jQuery('#fancy_collapser').attr('src', (WPSC_CORE_IMAGES_URL+'/plus.png'));
  144. });
  145. ";
  146. }
  147. }
  148. do_action( 'wpsc_alternate_cart_html', '' );
  149. exit();
  150. }
  151. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && ($_REQUEST['wpsc_ajax_action'] == 'get_cart') ) {
  152. add_action( 'init', 'wpsc_get_cart' );
  153. }
  154. /**
  155. * empty cart function, used through ajax and in normal page loading.
  156. * No parameters, returns nothing
  157. */
  158. function wpsc_empty_cart() {
  159. global $wpdb, $wpsc_cart;
  160. $wpsc_cart->empty_cart( false );
  161. if ( $_REQUEST['ajax'] == 'true' ) {
  162. ob_start();
  163. include_once( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
  164. $output = ob_get_contents();
  165. ob_end_clean();
  166. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  167. echo "jQuery('div.shopping-cart-wrapper').html('$output');";
  168. do_action( 'wpsc_alternate_cart_html' );
  169. if ( get_option( 'show_sliding_cart' ) == 1 ) {
  170. $_SESSION['slider_state'] = 0;
  171. echo "
  172. jQuery('#sliding_cart').slideUp('fast',function(){
  173. jQuery('#fancy_collapser').attr('src', (WPSC_CORE_IMAGES_URL+'/plus.png'));
  174. });
  175. ";
  176. }
  177. exit();
  178. }
  179. // this if statement is needed, as this function also runs on returning from the gateway
  180. if ( $_REQUEST['wpsc_ajax_action'] == 'empty_cart' ) {
  181. wp_redirect( remove_query_arg( array( 'wpsc_ajax_action', 'ajax' ) ) );
  182. exit();
  183. }
  184. }
  185. // execute on POST and GET
  186. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && (($_REQUEST['wpsc_ajax_action'] == 'empty_cart') || (isset($_GET['sessionid']) && ($_GET['sessionid'] > 0))) ) {
  187. add_action( 'init', 'wpsc_empty_cart' );
  188. }
  189. /**
  190. * coupons price, used through ajax and in normal page loading.
  191. * No parameters, returns nothing
  192. */
  193. function wpsc_coupon_price( $currCoupon = '' ) {
  194. global $wpdb, $wpsc_cart, $wpsc_coupons;
  195. if ( isset( $_POST['coupon_num'] ) && $_POST['coupon_num'] != '' ) {
  196. $coupon = $wpdb->escape( $_POST['coupon_num'] );
  197. $_SESSION['coupon_numbers'] = $coupon;
  198. $wpsc_coupons = new wpsc_coupons( $coupon );
  199. if ( $wpsc_coupons->validate_coupon() ) {
  200. $discountAmount = $wpsc_coupons->calculate_discount();
  201. $wpsc_cart->apply_coupons( $discountAmount, $coupon );
  202. $wpsc_coupons->errormsg = false;
  203. } else {
  204. $wpsc_coupons->errormsg = true;
  205. $wpsc_cart->coupons_amount = 0;
  206. $wpsc_cart->coupons_name = '';
  207. }
  208. } else if ( (!isset( $_POST['coupon_num'] ) || $_POST['coupon_num'] == '') && $currCoupon == '' ) {
  209. $wpsc_cart->coupons_amount = 0;
  210. $wpsc_cart->coupons_name = '';
  211. } else if ( $currCoupon != '' ) {
  212. $coupon = $wpdb->escape( $currCoupon );
  213. $_SESSION['coupon_numbers'] = $coupon;
  214. $wpsc_coupons = new wpsc_coupons( $coupon );
  215. if ( $wpsc_coupons->validate_coupon() ) {
  216. $discountAmount = $wpsc_coupons->calculate_discount();
  217. $wpsc_cart->apply_coupons( $discountAmount, $coupon );
  218. $wpsc_coupons->errormsg = false;
  219. }
  220. }
  221. }
  222. // execute on POST and GET
  223. if ( isset( $_POST['coupon_num'] ) ) {
  224. add_action( 'init', 'wpsc_coupon_price' );
  225. }
  226. /**
  227. * update quantity function, used through ajax and in normal page loading.
  228. * No parameters, returns nothing
  229. */
  230. function wpsc_update_item_quantity() {
  231. global $wpdb, $wpsc_cart;
  232. if ( is_numeric( $_POST['key'] ) ) {
  233. $key = (int)$_POST['key'];
  234. if ( $_POST['quantity'] > 0 ) {
  235. // if the quantity is greater than 0, update the item;
  236. $parameters['quantity'] = (int)$_POST['quantity'];
  237. $wpsc_cart->edit_item( $key, $parameters );
  238. } else {
  239. // if the quantity is 0, remove the item.
  240. $wpsc_cart->remove_item( $key );
  241. }
  242. if ( isset( $_SESSION['coupon_numbers'] ) ) {
  243. wpsc_coupon_price( $_SESSION['coupon_numbers'] );
  244. }
  245. }
  246. if ( isset( $_REQUEST['ajax'] ) && $_REQUEST['ajax'] == 'true' ) {
  247. ob_start();
  248. include_once( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
  249. $output = ob_get_contents();
  250. ob_end_clean();
  251. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  252. echo "jQuery('div.shopping-cart-wrapper').html('$output');\n";
  253. do_action( 'wpsc_alternate_cart_html' );
  254. exit();
  255. }
  256. }
  257. // execute on POST and GET
  258. if ( isset( $_REQUEST['wpsc_update_quantity'] ) && ($_REQUEST['wpsc_update_quantity'] == 'true') ) {
  259. add_action( 'init', 'wpsc_update_item_quantity' );
  260. }
  261. function wpsc_update_product_rating() {
  262. global $wpdb;
  263. $nowtime = time();
  264. $product_id = absint( $_POST['product_id'] );
  265. $ip_number = $wpdb->escape( $_SERVER['REMOTE_ADDR'] );
  266. $rating = absint( $_POST['product_rating'] );
  267. $cookie_data = explode( ",", $_COOKIE['voting_cookie'][$product_id] );
  268. if ( is_numeric( $cookie_data[0] ) && ($cookie_data[0] > 0) ) {
  269. $vote_id = absint( $cookie_data[0] );
  270. $wpdb->update( WPSC_TABLE_PRODUCT_RATING, array(
  271. 'rated' => $rating
  272. ), array( 'id' => $vote_id ) );
  273. } else {
  274. $wpdb->insert( WPSC_TABLE_PRODUCT_RATING, array(
  275. 'ipnum' => $ip_number,
  276. 'productid' => $product_id,
  277. 'rated' => $rating,
  278. 'time' => $nowtime
  279. ) );
  280. $data = $wpdb->get_results( "SELECT `id`,`rated` FROM `" . WPSC_TABLE_PRODUCT_RATING . "` WHERE `ipnum`='" . $ip_number . "' AND `productid` = '" . $product_id . "' AND `rated` = '" . $rating . "' AND `time` = '" . $nowtime . "' ORDER BY `id` DESC LIMIT 1", ARRAY_A );
  281. $vote_id = $data[0]['id'];
  282. setcookie( "voting_cookie[$prodid]", ($vote_id . "," . $rating ), time() + (60 * 60 * 24 * 360) );
  283. }
  284. if ( $_POST['ajax'] == 'true' ) {
  285. exit();
  286. }
  287. }
  288. // execute on POST and GET
  289. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && ($_REQUEST['wpsc_ajax_action'] == 'rate_product') ) {
  290. add_action( 'init', 'wpsc_update_product_rating' );
  291. }
  292. /**
  293. * update_shipping_price function, used through ajax and in normal page loading.
  294. * No parameters, returns nothing
  295. */
  296. function wpsc_update_shipping_price() {
  297. global $wpdb, $wpsc_cart;
  298. $quote_shipping_method = $_POST['key1'];
  299. $quote_shipping_option = $_POST['key'];
  300. $wpsc_cart->update_shipping( $quote_shipping_method, $quote_shipping_option );
  301. echo "
  302. if(jQuery('.pricedisplay.checkout-shipping .pricedisplay')){
  303. jQuery('.pricedisplay.checkout-shipping > .pricedisplay:first').html(\"" . wpsc_cart_shipping() . "\");
  304. jQuery('.shoppingcart .pricedisplay.checkout-shipping > .pricedisplay:first').html(\"" . wpsc_cart_shipping() . "\");
  305. } else
  306. jQuery('.pricedisplay.checkout-shipping').html(\"" . wpsc_cart_shipping() . "\");
  307. ";
  308. echo "jQuery('.pricedisplay.checkout-total').html(\"" . wpsc_cart_total() . "\");\n\r";
  309. exit();
  310. }
  311. // execute on POST and GET
  312. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && ($_REQUEST['wpsc_ajax_action'] == 'update_shipping_price') ) {
  313. add_action( 'init', 'wpsc_update_shipping_price' );
  314. }
  315. /**
  316. * update_shipping_price function, used through ajax and in normal page loading.
  317. * No parameters, returns nothing
  318. */
  319. function wpsc_get_rating_count() {
  320. global $wpdb, $wpsc_cart;
  321. $prodid = $_POST['product_id'];
  322. $data = $wpdb->get_results( "SELECT COUNT(*) AS `count` FROM `" . WPSC_TABLE_PRODUCT_RATING . "` WHERE `productid` = '" . $prodid . "'", ARRAY_A );
  323. echo $data[0]['count'] . "," . $prodid;
  324. exit();
  325. }
  326. // execute on POST and GET
  327. if ( isset( $_REQUEST['get_rating_count'] ) && ($_REQUEST['get_rating_count'] == 'true') && is_numeric( $_POST['product_id'] ) ) {
  328. add_action( 'init', 'wpsc_get_rating_count' );
  329. }
  330. /**
  331. * update_product_page_price function, used through ajax with variations
  332. * No parameters, returns nothing
  333. */
  334. function wpsc_update_product_price() {
  335. global $wpdb, $wpsc_cart;
  336. $from = '';
  337. $change_price = true;
  338. foreach ( (array)$_POST['variation'] as $variation ) {
  339. if ( is_numeric( $variation ) ) {
  340. $variations[] = (int)$variation;
  341. }
  342. if($variation == 0){
  343. $from = ' from ';
  344. $from = apply_filters('wpsc_product_variation_text',$from);
  345. $change_price = false;
  346. }
  347. }
  348. do_action( 'wpsc_update_variation_product', (int)$_POST['product_id'], $variations );
  349. $stock = wpsc_check_variation_stock_availability( (int)$_POST['product_id'], $variations );
  350. if ( is_numeric( $stock ) && $stock == 0 ) {
  351. echo "product_msg=\"" . __( 'Sorry, but this variation is out of stock.', 'wpsc' ) . "\";\n";
  352. echo "variation_msg=\"" . __( 'Variation not in stock', 'wpsc' ) . "\";\n";
  353. echo "variation_status= false \n";
  354. }else{
  355. echo "variation_msg=\"" . __( 'Product in stock', 'wpsc' ) . "\";\n";
  356. echo "variation_status= true \n";
  357. }
  358. echo "product_id=" . (int)$_POST['product_id'] . ";\n";
  359. if($change_price){
  360. echo "old_price=\"" . wpsc_currency_display( wpsc_calculate_price( (int)$_POST['product_id'], $variations, false ), array( 'display_as_html' => false ) ) . "\";\n";
  361. echo "numeric_old_price=\"" . number_format( wpsc_calculate_price( (int)$_POST['product_id'], $variations, false ) ) . "\";\n";
  362. echo "you_save=\"" . wpsc_currency_display( wpsc_you_save( array( 'product_id' => (int)$_POST['product_id'], 'type' => 'amount', 'variations' => $variations ) ), array( 'display_as_html' => false ) ) . "! (".wpsc_you_save( array( 'product_id' => (int)$_POST['product_id'], 'variations' => $variations ) ) . "%)\";\n";
  363. echo "price=\"" . $from.wpsc_currency_display( wpsc_calculate_price( (int)$_POST['product_id'], $variations, true ),array( 'display_as_html' => false ) ) . "\";\n";
  364. echo "numeric_price=\"" . number_format( wpsc_calculate_price( (int)$_POST['product_id'], $variations, true ) ) . "\";\n";
  365. }
  366. exit();
  367. }
  368. // execute on POST and GET
  369. if ( isset( $_REQUEST['update_product_price'] ) && ($_REQUEST['update_product_price'] == 'true') && is_numeric( $_POST['product_id'] ) ) {
  370. add_action( 'init', 'wpsc_update_product_price' );
  371. }
  372. /**
  373. * update location function, used through ajax and in normal page loading.
  374. * No parameters, returns nothing
  375. */
  376. function wpsc_update_location() {
  377. global $wpdb, $wpsc_cart;
  378. if ( $_POST['country'] != null ) {
  379. $_SESSION['wpsc_delivery_country'] = $_POST['country'];
  380. if ( $_SESSION['wpsc_selected_country'] == null ) {
  381. $_SESSION['wpsc_selected_country'] = $_POST['country'];
  382. }
  383. if ( ! empty( $_POST['region'] ) ) {
  384. $_SESSION['wpsc_delivery_region'] = $_POST['region'];
  385. if ( $_SESSION['wpsc_selected_region'] == null ) {
  386. $_SESSION['wpsc_selected_region'] = $_POST['region'];
  387. }
  388. } else if ( $_SESSION['wpsc_selected_region'] == '' ) {
  389. $_SESSION['wpsc_delivery_region'] = get_option( 'base_region' );
  390. $_SESSION['wpsc_selected_region'] = get_option( 'base_region' );
  391. }
  392. if ( $_SESSION['wpsc_delivery_region'] == '' ) {
  393. $_SESSION['wpsc_delivery_region'] = $_SESSION['wpsc_selected_region'];
  394. }
  395. }
  396. if ( ! empty( $_POST['zipcode'] ) ) {
  397. $_SESSION['wpsc_zipcode'] = $_POST['zipcode'];
  398. }
  399. $delivery_region_count = $wpdb->get_var( "SELECT COUNT(`regions`.`id`) FROM `" . WPSC_TABLE_REGION_TAX . "` AS `regions` INNER JOIN `" . WPSC_TABLE_CURRENCY_LIST . "` AS `country` ON `country`.`id` = `regions`.`country_id` WHERE `country`.`isocode` IN('" . $wpdb->escape( $_SESSION['wpsc_delivery_country'] ) . "')" );
  400. if ( $delivery_region_count < 1 ) {
  401. $_SESSION['wpsc_delivery_region'] = null;
  402. }
  403. $selected_region_count = $wpdb->get_var( "SELECT COUNT(`regions`.`id`) FROM `" . WPSC_TABLE_REGION_TAX . "` AS `regions` INNER JOIN `" . WPSC_TABLE_CURRENCY_LIST . "` AS `country` ON `country`.`id` = `regions`.`country_id` WHERE `country`.`isocode` IN('" . $wpdb->escape( $_SESSION['wpsc_selected_country'] ) . "')" );
  404. if ( $selected_region_count < 1 ) {
  405. $_SESSION['wpsc_selected_region'] = null;
  406. }
  407. $wpsc_cart->update_location();
  408. $wpsc_cart->get_shipping_method();
  409. $wpsc_cart->get_shipping_option();
  410. if ( $wpsc_cart->selected_shipping_method != '' ) {
  411. $wpsc_cart->update_shipping( $wpsc_cart->selected_shipping_method, $wpsc_cart->selected_shipping_option );
  412. }
  413. if ( isset( $_GET['ajax'] ) && $_GET['ajax'] == 'true' ) {
  414. exit();
  415. }
  416. }
  417. // execute on POST and GET
  418. if ( isset( $_REQUEST['wpsc_ajax_actions'] ) && ($_REQUEST['wpsc_ajax_actions'] == 'update_location') ) {
  419. add_action( 'init', 'wpsc_update_location' );
  420. }
  421. function wpsc_cart_html_page() {
  422. require_once(WPSC_FILE_PATH . "/wpsc-includes/shopping_cart_container.php");
  423. exit();
  424. }
  425. // execute on POST and GET
  426. if ( isset( $_REQUEST['wpsc_action'] ) && ($_REQUEST['wpsc_action'] == 'cart_html_page') ) {
  427. add_action( 'init', 'wpsc_cart_html_page', 110 );
  428. }
  429. /**
  430. * submit checkout function, used through ajax and in normal page loading.
  431. * No parameters, returns nothing
  432. */
  433. function wpsc_submit_checkout() {
  434. global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules, $wpsc_gateways;
  435. $num_items = 0;
  436. $use_shipping = 0;
  437. $disregard_shipping = 0;
  438. $_SESSION['wpsc_checkout_misc_error_messages'] = array( );
  439. $wpsc_checkout = new wpsc_checkout();
  440. $selected_gateways = get_option( 'custom_gateway_options' );
  441. $submitted_gateway = $_POST['custom_gateway'];
  442. $options = get_option( 'custom_shipping_options' );
  443. $form_validity = $wpsc_checkout->validate_forms();
  444. extract( $form_validity ); // extracts $is_valid and $error_messages
  445. if ( $_POST['agree'] != 'yes' ) {
  446. $_SESSION['wpsc_checkout_misc_error_messages'][] = __( 'Please agree to the terms and conditions, otherwise we cannot process your order.', 'wpsc' );
  447. $is_valid = false;
  448. }
  449. $selectedCountry = $wpdb->get_results( "SELECT id, country FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE isocode='" . $wpdb->escape( $_SESSION['wpsc_delivery_country'] ) . "'", ARRAY_A );
  450. foreach ( $wpsc_cart->cart_items as $cartitem ) {
  451. if($cartitem->meta[0]['no_shipping'] == 1) continue;
  452. $categoriesIDs = $cartitem->category_id_list;
  453. foreach ( (array)$categoriesIDs as $catid ) {
  454. if ( is_array( $catid ) )
  455. $countries = wpsc_get_meta( $catid[0], 'target_market', 'wpsc_category' );
  456. else
  457. $countries = wpsc_get_meta( $catid, 'target_market', 'wpsc_category' );
  458. if ( !empty($countries) && !in_array( $selectedCountry[0]['id'], (array)$countries ) ) {
  459. $errormessage = sprintf( __( '%s cannot be shipped to %s. To continue with your transaction please remove this product from the list below.', 'wpsc' ), $cartitem->product_name, $selectedCountry[0]['country'] );
  460. $_SESSION['categoryAndShippingCountryConflict'] = $errormessage;
  461. $is_valid = false;
  462. }
  463. }
  464. //count number of items, and number of items using shipping
  465. $num_items++;
  466. if ( $cartitem->uses_shipping != 1 )
  467. $disregard_shipping++;
  468. else
  469. $use_shipping++;
  470. }
  471. if ( array_search( $submitted_gateway, $selected_gateways ) !== false )
  472. $_SESSION['wpsc_previous_selected_gateway'] = $submitted_gateway;
  473. else
  474. $is_valid = false;
  475. if ( get_option( 'do_not_use_shipping' ) == 0 && ($wpsc_cart->selected_shipping_method == null || $wpsc_cart->selected_shipping_option == null) && ( $num_items != $disregard_shipping ) ) {
  476. $_SESSION['wpsc_checkout_misc_error_messages'][] = __( 'You must select a shipping method, otherwise we cannot process your order.', 'wpsc' );
  477. $is_valid = false;
  478. }
  479. if ( (get_option( 'do_not_use_shipping' ) != 1) && (in_array( 'ups', (array)$options )) && $_SESSION['wpsc_zipcode'] == '' && ( $num_items != $disregard_shipping ) ) {
  480. $_SESSION['categoryAndShippingCountryConflict'] = __( 'Please enter a Zipcode and click calculate to proceed', 'wpsc' );
  481. $is_valid = false;
  482. }
  483. if ( $is_valid == true ) {
  484. $_SESSION['categoryAndShippingCountryConflict'] = '';
  485. // check that the submitted gateway is in the list of selected ones
  486. $sessionid = (mt_rand( 100, 999 ) . time());
  487. $_SESSION['wpsc_sessionid'] = $sessionid;
  488. $subtotal = $wpsc_cart->calculate_subtotal();
  489. if ( $wpsc_cart->has_total_shipping_discount() == false )
  490. $base_shipping = $wpsc_cart->calculate_base_shipping();
  491. else
  492. $base_shipping = 0;
  493. $delivery_country = $wpsc_cart->delivery_country;
  494. $delivery_region = $wpsc_cart->delivery_region;
  495. if ( wpsc_uses_shipping ( ) ) {
  496. $shipping_method = $wpsc_cart->selected_shipping_method;
  497. $shipping_option = $wpsc_cart->selected_shipping_option;
  498. } else {
  499. $shipping_method = '';
  500. $shipping_option = '';
  501. }
  502. if ( isset( $_POST['how_find_us'] ) )
  503. $find_us = $_POST['how_find_us'];
  504. else
  505. $find_us = '';
  506. //keep track of tax if taxes are exclusive
  507. $wpec_taxes_controller = new wpec_taxes_controller();
  508. if ( !$wpec_taxes_controller->wpec_taxes_isincluded() ) {
  509. $tax = $wpsc_cart->calculate_total_tax();
  510. $tax_percentage = $wpsc_cart->tax_percentage;
  511. } else {
  512. $tax = 0.00;
  513. $tax_percentage = 0.00;
  514. }
  515. $total = $wpsc_cart->calculate_total_price();
  516. $wpdb->insert( WPSC_TABLE_PURCHASE_LOGS, array(
  517. 'totalprice' => $total,
  518. 'statusno' => '0',
  519. 'sessionid' => $sessionid,
  520. 'user_ID' => (int)$user_ID,
  521. 'date' => strtotime( current_time( 'mysql' ) ),
  522. 'gateway' => $submitted_gateway,
  523. 'billing_country' => $wpsc_cart->selected_country,
  524. 'shipping_country' => $delivery_country,
  525. 'billing_region' => $wpsc_cart->selected_region,
  526. 'shipping_region' => $delivery_region,
  527. 'base_shipping' => $base_shipping,
  528. 'shipping_method' => $shipping_method,
  529. 'shipping_option' => $shipping_option,
  530. 'plugin_version' => WPSC_VERSION,
  531. 'discount_value' => $wpsc_cart->coupons_amount,
  532. 'discount_data' => $wpsc_cart->coupons_name,
  533. 'find_us' => $find_us,
  534. 'wpec_taxes_total' => $tax,
  535. 'wpec_taxes_rate' => $tax_percentage
  536. ) );
  537. $purchase_log_id = $wpdb->insert_id;
  538. $wpsc_checkout->save_forms_to_db( $purchase_log_id );
  539. $wpsc_cart->save_to_db( $purchase_log_id );
  540. $wpsc_cart->submit_stock_claims( $purchase_log_id );
  541. if ( get_option( 'wpsc_also_bought' ) == 1 )
  542. wpsc_populate_also_bought_list();
  543. if( !isset( $our_user_id ) && isset( $user_ID ))
  544. $our_user_id = $user_ID;
  545. $wpsc_cart->log_id = $purchase_log_id;
  546. do_action( 'wpsc_submit_checkout', array( "purchase_log_id" => $purchase_log_id, "our_user_id" => $our_user_id ) );
  547. if ( get_option( 'permalink_structure' ) != '' )
  548. $separator = "?";
  549. else
  550. $separator = "&";
  551. // submit to gateway
  552. $current_gateway_data = &$wpsc_gateways[$submitted_gateway];
  553. if ( $current_gateway_data['api_version'] >= 2.0 ) {
  554. $merchant_instance = new $current_gateway_data['class_name']( $purchase_log_id );
  555. $merchant_instance->construct_value_array();
  556. $merchant_instance->submit();
  557. } elseif ( ($current_gateway_data['internalname'] == $submitted_gateway) && ($current_gateway_data['internalname'] != 'google') ) {
  558. $gateway_used = $current_gateway_data['internalname'];
  559. $wpdb->update( WPSC_TABLE_PURCHASE_LOGS, array(
  560. 'gateway' => $gateway_used
  561. ), array( 'id' => $log_id ) );
  562. $current_gateway_data['function']( $separator, $sessionid );
  563. } elseif ( ($current_gateway_data['internalname'] == 'google') && ($current_gateway_data['internalname'] == $submitted_gateway) ) {
  564. $gateway_used = $current_gateway_data['internalname'];
  565. $wpdb->update( WPSC_TABLE_PURCHASE_LOGS, array(
  566. 'gateway' => $gateway_used
  567. ), array( 'id' => $log_id ) );
  568. $_SESSION['gateway'] = 'google';
  569. wp_redirect(get_option( 'shopping_cart_url' ));
  570. }
  571. }
  572. }
  573. // execute on POST and GET
  574. if ( isset( $_REQUEST['wpsc_action'] ) && ($_REQUEST['wpsc_action'] == 'submit_checkout') ) {
  575. add_action( 'init', 'wpsc_submit_checkout' );
  576. }
  577. function wpsc_product_rss() {
  578. global $wp_query,$wpsc_query, $wpdb;
  579. list($wp_query, $wpsc_query) = array( $wpsc_query, $wp_query ); // swap the wpsc_query object
  580. header( "Content-Type: application/xml; charset=UTF-8" );
  581. header( 'Content-Disposition: inline; filename="E-Commerce_Product_List.rss"' );
  582. require_once(WPSC_FILE_PATH . '/wpsc-includes/rss_template.php');
  583. list($wp_query, $wpsc_query) = array( $wpsc_query, $wp_query ); // swap the wpsc_query object
  584. exit();
  585. }
  586. if ( isset( $_REQUEST['wpsc_action'] ) && ($_REQUEST['wpsc_action'] == "rss") ) {
  587. add_action( 'template_redirect', 'wpsc_product_rss', 80 );
  588. }
  589. function wpsc_gateway_notification() {
  590. global $wpdb, $wpsc_gateways;
  591. $gateway_name = $_GET['gateway'];
  592. // work out what gateway we are getting the request from, run the appropriate code.
  593. if ( ($gateway_name != null) && isset( $wpsc_gateways[$gateway_name]['class_name'] ) ) {
  594. $merchant_class = $wpsc_gateways[$gateway_name]['class_name'];
  595. $merchant_instance = new $merchant_class( null, true );
  596. $merchant_instance->process_gateway_notification();
  597. }
  598. exit();
  599. }
  600. // execute on POST and GET
  601. if ( isset( $_REQUEST['wpsc_action'] ) && ($_REQUEST['wpsc_action'] == 'gateway_notification') ) {
  602. add_action( 'init', 'wpsc_gateway_notification' );
  603. }
  604. if ( isset( $_GET['termsandconds'] ) && ($_GET['termsandconds'] === 'true') ) {
  605. echo wpautop( stripslashes( get_option( 'terms_and_conditions' ) ) );
  606. exit();
  607. }
  608. /**
  609. * wpsc_change_tax function, used through ajax and in normal page loading.
  610. * No parameters, returns nothing
  611. */
  612. function wpsc_change_tax() {
  613. global $wpdb, $wpsc_cart;
  614. $form_id = absint( $_POST['form_id'] );
  615. $wpsc_selected_country = $wpsc_cart->selected_country;
  616. $wpsc_selected_region = $wpsc_cart->selected_region;
  617. $wpsc_delivery_country = $wpsc_cart->delivery_country;
  618. $wpsc_delivery_region = $wpsc_cart->delivery_region;
  619. $previous_country = $_SESSION['wpsc_selected_country'];
  620. if ( isset( $_POST['billing_country'] ) ) {
  621. $wpsc_selected_country = $wpdb->escape( $_POST['billing_country'] );
  622. $_SESSION['wpsc_selected_country'] = $wpsc_selected_country;
  623. }
  624. if ( isset( $_POST['billing_region'] ) ) {
  625. $wpsc_selected_region = absint( $_POST['billing_region'] );
  626. $_SESSION['wpsc_selected_region'] = $wpsc_selected_region;
  627. }
  628. $check_country_code = $wpdb->get_var( " SELECT `country`.`isocode` FROM `" . WPSC_TABLE_REGION_TAX . "` AS `region` INNER JOIN `" . WPSC_TABLE_CURRENCY_LIST . "` AS `country` ON `region`.`country_id` = `country`.`id` WHERE `region`.`id` = '" . $_SESSION['wpsc_selected_region'] . "' LIMIT 1" );
  629. if ( $_SESSION['wpsc_selected_country'] != $check_country_code ) {
  630. $wpsc_selected_region = null;
  631. }
  632. if ( isset( $_POST['shipping_country'] ) ) {
  633. $wpsc_delivery_country = $wpdb->escape( $_POST['shipping_country'] );
  634. $_SESSION['wpsc_delivery_country'] = $wpsc_delivery_country;
  635. }
  636. if ( isset( $_POST['shipping_region'] ) ) {
  637. $wpsc_delivery_region = absint( $_POST['shipping_region'] );
  638. $_SESSION['wpsc_delivery_region'] = $wpsc_delivery_region;
  639. }
  640. $check_country_code = $wpdb->get_var( " SELECT `country`.`isocode` FROM `" . WPSC_TABLE_REGION_TAX . "` AS `region` INNER JOIN `" . WPSC_TABLE_CURRENCY_LIST . "` AS `country` ON `region`.`country_id` = `country`.`id` WHERE `region`.`id` = '" . $wpsc_delivery_region . "' LIMIT 1" );
  641. if ( $wpsc_delivery_country != $check_country_code ) {
  642. $wpsc_delivery_region = null;
  643. }
  644. $wpsc_cart->update_location();
  645. $wpsc_cart->get_shipping_method();
  646. $wpsc_cart->get_shipping_option();
  647. if ( $wpsc_cart->selected_shipping_method != '' ) {
  648. $wpsc_cart->update_shipping( $wpsc_cart->selected_shipping_method, $wpsc_cart->selected_shipping_option );
  649. }
  650. $tax = $wpsc_cart->calculate_total_tax();
  651. $total = wpsc_cart_total();
  652. $total_input = wpsc_cart_total(false);
  653. if($wpsc_cart->coupons_amount >= wpsc_cart_total() && !empty($wpsc_cart->coupons_amount)){
  654. $total = 0;
  655. }
  656. if ( $wpsc_cart->total_price < 0 ) {
  657. $wpsc_cart->coupons_amount += $wpsc_cart->total_price;
  658. $wpsc_cart->total_price = null;
  659. $wpsc_cart->calculate_total_price();
  660. }
  661. ob_start();
  662. include_once( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
  663. $output = ob_get_contents();
  664. ob_end_clean();
  665. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  666. if ( get_option( 'lock_tax' ) == 1 ) {
  667. echo "jQuery('#current_country').val('" . $_SESSION['wpsc_delivery_country'] . "'); \n";
  668. if ( $_SESSION['wpsc_delivery_country'] == 'US' && get_option( 'lock_tax' ) == 1 ) {
  669. $output = wpsc_shipping_region_list( $_SESSION['wpsc_delivery_country'], $_SESSION['wpsc_delivery_region'] );
  670. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  671. echo "jQuery('#region').remove();\n\r";
  672. echo "jQuery('#change_country').append(\"" . $output . "\");\n\r";
  673. }
  674. }
  675. foreach ( $wpsc_cart->cart_items as $key => $cart_item ) {
  676. echo "jQuery('#shipping_$key').html(\"" . wpsc_currency_display( $cart_item->shipping ) . "\");\n\r";
  677. }
  678. echo "jQuery('#checkout_shipping').html(\"" . wpsc_cart_shipping() . "\");\n\r";
  679. echo "jQuery('div.shopping-cart-wrapper').html('$output');\n";
  680. if ( get_option( 'lock_tax' ) == 1 ) {
  681. echo "jQuery('.shipping_country').val('" . $_SESSION['wpsc_delivery_country'] . "') \n";
  682. $sql = "SELECT `country` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `isocode`='" . $_SESSION['wpsc_selected_country'] . "'";
  683. $country_name = $wpdb->get_var( $sql );
  684. echo "jQuery('.shipping_country_name').html('" . $country_name . "') \n";
  685. }
  686. $form_selected_country = null;
  687. $form_selected_region = null;
  688. $onchange_function = null;
  689. if ( ($_POST['billing_country'] != 'undefined') && !isset( $_POST['shipping_country'] ) ) {
  690. $form_selected_country = $wpsc_selected_country;
  691. $form_selected_region = $wpsc_selected_region;
  692. $onchange_function = 'set_billing_country';
  693. } else if ( ($_POST['shipping_country'] != 'undefined') && !isset( $_POST['billing_country'] ) ) {
  694. $form_selected_country = $wpsc_delivery_country;
  695. $form_selected_region = $wpsc_delivery_region;
  696. $onchange_function = 'set_shipping_country';
  697. }
  698. if ( ($form_selected_country != null) && ($onchange_function != null) ) {
  699. $region_list = $wpdb->get_results( "SELECT `" . WPSC_TABLE_REGION_TAX . "`.* FROM `" . WPSC_TABLE_REGION_TAX . "`, `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `" . WPSC_TABLE_CURRENCY_LIST . "`.`isocode` IN('" . $form_selected_country . "') AND `" . WPSC_TABLE_CURRENCY_LIST . "`.`id` = `" . WPSC_TABLE_REGION_TAX . "`.`country_id`", ARRAY_A );
  700. if ( $region_list != null ) {
  701. $title = (empty($_POST['billing_country']))?'shippingstate':'billingstate';
  702. $output = "<select name='collected_data[" . $form_id . "][1]' class='current_region' onchange='$onchange_function(\"region_country_form_$form_id\", \"$form_id\");' title='" . $title . "'>\n\r";
  703. foreach ( $region_list as $region ) {
  704. if ( $form_selected_region == $region['id'] ) {
  705. $selected = "selected='selected'";
  706. } else {
  707. $selected = "";
  708. }
  709. $output .= " <option value='" . $region['id'] . "' $selected>" . htmlspecialchars( $region['name'] ) . "</option>\n\r";
  710. }
  711. $output .= "</select>\n\r";
  712. $output = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $output ) );
  713. echo "jQuery('#region_select_$form_id').html(\"" . $output . "\");\n\r";
  714. echo "
  715. var wpsc_checkout_table_selector = jQuery('#region_select_$form_id').parents('.wpsc_checkout_table').attr('class');
  716. wpsc_checkout_table_selector = wpsc_checkout_table_selector.replace(' ','.');
  717. wpsc_checkout_table_selector = '.'+wpsc_checkout_table_selector;
  718. jQuery(wpsc_checkout_table_selector + ' input.billing_region').attr('disabled', 'disabled');
  719. jQuery(wpsc_checkout_table_selector + ' input.shipping_region').attr('disabled', 'disabled');
  720. jQuery(wpsc_checkout_table_selector + ' .billing_region').parent().parent().hide();
  721. jQuery(wpsc_checkout_table_selector + ' .shipping_region').parent().parent().hide();
  722. ";
  723. } else {
  724. if ( get_option( 'lock_tax' ) == 1 ) {
  725. echo "jQuery('#region').hide();";
  726. }
  727. echo "jQuery('#region_select_$form_id').html('');\n\r";
  728. echo "
  729. var wpsc_checkout_table_selector = jQuery('#region_select_$form_id').parents('.wpsc_checkout_table').attr('class');
  730. wpsc_checkout_table_selector = wpsc_checkout_table_selector.replace(' ','.');
  731. wpsc_checkout_table_selector = '.'+wpsc_checkout_table_selector;
  732. jQuery(wpsc_checkout_table_selector + ' input.billing_region').removeAttr('disabled');
  733. jQuery(wpsc_checkout_table_selector + ' input.shipping_region').removeAttr('disabled');
  734. jQuery(wpsc_checkout_table_selector + ' .billing_region').parent().parent().show();
  735. jQuery(wpsc_checkout_table_selector + ' .shipping_region').parent().parent().show();
  736. ";
  737. }
  738. }
  739. if ( $tax > 0 ) {
  740. echo "jQuery(\"tr.total_tax\").show();\n\r";
  741. } else {
  742. echo "jQuery(\"tr.total_tax\").hide();\n\r";
  743. }
  744. echo "jQuery('#checkout_tax').html(\"<span class='pricedisplay'>" . wpsc_cart_tax() . "</span>\");\n\r";
  745. echo "jQuery('#checkout_total').html(\"{$total}<input id='shopping_cart_total_price' type='hidden' value='{$total_input}' />\");\n\r";
  746. echo "if(jQuery(\"#shippingSameBilling\").is(\":checked\")) wpsc_shipping_same_as_billing();";
  747. exit();
  748. }
  749. // execute on POST and GET
  750. if ( isset( $_REQUEST['wpsc_ajax_action'] ) && ($_REQUEST['wpsc_ajax_action'] == 'change_tax') ) {
  751. add_action( 'init', 'wpsc_change_tax' );
  752. }
  753. /**
  754. * wpsc scale image function, dynamically resizes an image oif no image already exists of that size.
  755. */
  756. function wpsc_scale_image() {
  757. global $wpdb;
  758. if ( !isset( $_REQUEST['wpsc_action'] ) || !isset( $_REQUEST['attachment_id'] ) || ( 'scale_image' != $_REQUEST['wpsc_action'] ) || !is_numeric( $_REQUEST['attachment_id'] ) )
  759. return false;
  760. require_once(ABSPATH . 'wp-admin/includes/image.php');
  761. $attachment_id = absint( $_REQUEST['attachment_id'] );
  762. $width = absint( $_REQUEST['width'] );
  763. $height = absint( $_REQUEST['height'] );
  764. $intermediate_size = '';
  765. if ( (($width >= 10) && ($height >= 10)) && (($width <= 1024) && ($height <= 1024)) ) {
  766. $intermediate_size = "wpsc-{$width}x{$height}";
  767. $generate_thumbnail = true;
  768. } else {
  769. if ( isset( $_REQUEST['intermediate_size'] ) )
  770. $intermediate_size = $wpdb->escape( $_REQUEST['intermediate_size'] );
  771. $generate_thumbnail = false;
  772. }
  773. // If the attachment ID is greater than 0, and the width and height is greater than or equal to 10, and less than or equal to 1024
  774. if ( ($attachment_id > 0) && ($intermediate_size != '') ) {
  775. // Get all the required information about the attachment
  776. $uploads = wp_upload_dir();
  777. $image_meta = get_post_meta( $attachment_id, '' );
  778. $file_path = get_attached_file( $attachment_id );
  779. foreach ( $image_meta as $meta_name => $meta_value ) { // clean up the meta array
  780. $image_meta[$meta_name] = maybe_unserialize( array_pop( $meta_value ) );
  781. }
  782. if ( !isset( $image_meta['_wp_attachment_metadata'] ) )
  783. $image_meta['_wp_attachment_metadata'] = '';
  784. $attachment_metadata = $image_meta['_wp_attachment_metadata'];
  785. if ( !isset( $attachment_metadata['sizes'] ) )
  786. $attachment_metadata['sizes'] = '';
  787. if ( !isset( $attachment_metadata['sizes'][$intermediate_size] ) )
  788. $attachment_metadata['sizes'][$intermediate_size] = '';
  789. // determine if we already have an image of this size
  790. if ( (count( $attachment_metadata['sizes'] ) > 0) && ($attachment_metadata['sizes'][$intermediate_size]) ) {
  791. $intermediate_image_data = image_get_intermediate_size( $attachment_id, $intermediate_size );
  792. if ( file_exists( $file_path ) ) {
  793. $original_modification_time = filemtime( $file_path );
  794. $cache_modification_time = filemtime( $uploads['basedir'] . "/" . $intermediate_image_data['path'] );
  795. if ( $original_modification_time < $cache_modification_time ) {
  796. $generate_thumbnail = false;
  797. }
  798. }
  799. }
  800. if ( $generate_thumbnail == true ) {
  801. //JS - 7.1.2010 - Added true parameter to function to not crop - causing issues on WPShop
  802. $intermediate_size_data = image_make_intermediate_size( $file_path, $width, $height, true );
  803. $attachment_metadata['sizes'][$intermediate_size] = $intermediate_size_data;
  804. wp_update_attachment_metadata( $attachment_id, $attachment_metadata );
  805. $intermediate_image_data = image_get_intermediate_size( $attachment_id, $intermediate_size );
  806. }
  807. /// if we are serving the page using SSL, we have to use for the image too.
  808. if ( is_ssl ( ) ) {
  809. $output_url = str_replace( "http://", "https://", $intermediate_image_data['url'] );
  810. } else {
  811. $output_url = $intermediate_image_data['url'];
  812. }
  813. wp_redirect( $output_url );
  814. } else {
  815. _e( "Invalid Image parameters", 'wpsc' );
  816. }
  817. exit();
  818. }
  819. add_action( 'init', 'wpsc_scale_image' );
  820. function wpsc_download_file() {
  821. global $wpdb;
  822. if ( isset( $_GET['downloadid'] ) ) {
  823. // strip out anything that isnt 'a' to 'z' or '0' to '9'
  824. ini_set('max_execution_time',10800);
  825. $downloadid = preg_replace( "/[^a-z0-9]+/i", '', strtolower( $_GET['downloadid'] ) );
  826. $download_data = $wpdb->get_row( "SELECT * FROM `" . WPSC_TABLE_DOWNLOAD_STATUS . "` WHERE `uniqueid` = '" . $downloadid . "' AND `downloads` > '0' AND `active`='1' LIMIT 1", ARRAY_A );
  827. if ( ($download_data == null) && is_numeric( $downloadid ) ) {
  828. $download_data = $wpdb->get_row( "SELECT * FROM `" . WPSC_TABLE_DOWNLOAD_STATUS . "` WHERE `id` = '" . $downloadid . "' AND `downloads` > '0' AND `active`='1' AND `uniqueid` IS NULL LIMIT 1", ARRAY_A );
  829. }
  830. if ( (get_option( 'wpsc_ip_lock_downloads' ) == 1) && ($_SERVER['REMOTE_ADDR'] != null) ) {
  831. $ip_number = $_SERVER['REMOTE_ADDR'];
  832. if ( $download_data['ip_number'] == '' ) {
  833. // if the IP number is not set, set it
  834. $wpdb->update( WPSC_TABLE_DOWNLOAD_STATUS, array(
  835. 'ip_number' => $ip_number
  836. ), array( 'id' => $download_data['id'] ) );
  837. } else if ( $ip_number != $download_data['ip_number'] ) {
  838. // if the IP number is set but does not match, fail here.
  839. exit( _e( 'This download is no longer valid, Please contact the site administrator for more information.', 'wpsc' ) );
  840. }
  841. }
  842. $file_id = $download_data['fileid'];
  843. $file_data = wpsc_get_downloadable_file($file_id);
  844. if ( $file_data == null ) {
  845. exit( _e( 'This download is no longer valid, Please contact the site administrator for more information.', 'wpsc' ) );
  846. }
  847. if ( $download_data != null ) {
  848. if ( (int)$download_data['downloads'] >= 1 ) {
  849. $download_count = (int)$download_data['downloads'] - 1;
  850. } else {
  851. $download_count = 0;
  852. }
  853. $wpdb->update( WPSC_TABLE_DOWNLOAD_STATUS, array(
  854. 'downloads' => $download_count
  855. ), array( 'id' => $download_data['id'] ) );
  856. $cart_contents = $wpdb->get_results( "SELECT `" . WPSC_TABLE_CART_CONTENTS . "`.*, $wpdb->posts.`guid` FROM `" . WPSC_TABLE_CART_CONTENTS . "` LEFT JOIN $wpdb->posts ON `" . WPSC_TABLE_CART_CONTENTS . "`.`prodid`= $wpdb->posts.`post_parent` WHERE $wpdb->posts.`post_type` = 'wpsc-product-file' AND `purchaseid` =" . $download_data['purchid'], ARRAY_A );
  857. $dl = 0;
  858. foreach ( $cart_contents as $cart_content ) {
  859. if ( $cart_content['guid'] == 1 ) {
  860. $dl++;
  861. }
  862. }
  863. if ( count( $cart_contents ) == $dl ) {
  864. $wpdb->update( WPSC_TABLE_PURCHASE_LOGS, array(
  865. 'processed' => '4'
  866. ), array( 'id' => $download_data['purchid'] ) );
  867. }
  868. do_action( 'wpsc_alter_download_action', $file_id );
  869. $file_path = WPSC_FILE_DIR . basename( $file_data->post_title );
  870. $file_name = basename( $file_data->post_title );
  871. if ( is_file( $file_path ) ) {
  872. if( !ini_get('safe_mode') ) set_time_limit(0);
  873. header( 'Content-Type: ' . $file_data->post_mime_type );
  874. header( 'Content-Length: ' . filesize( $file_path ) );
  875. header( 'Content-Transfer-Encoding: binary' );
  876. header( 'Content-Disposition: attachment; filename="' . stripslashes( $file_name ) . '"' );
  877. if ( isset( $_SERVER["HTTPS"] ) && ($_SERVER["HTTPS"] != '') ) {
  878. /*
  879. There is a bug in how IE handles downloads from servers using HTTPS, this is part of the fix, you may also need:
  880. session_cache_limiter('public');
  881. session_cache_expire(30);
  882. At the start of your index.php file or before the session is started
  883. */
  884. header( "Pragma: public" );
  885. header( "Expires: 0" );
  886. header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  887. header( "Cache-Control: public" );
  888. } else {
  889. header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
  890. }
  891. header( "Pragma: public" );
  892. header( "Expires: 0" );
  893. // destroy the session to allow the file to be downloaded on some buggy browsers and webservers
  894. session_destroy();
  895. wpsc_readfile_chunked( $file_path );
  896. exit();
  897. }else{
  898. wp_die(__('Sorry something has gone wrong with your download!', 'wpsc'));
  899. }
  900. } else {
  901. exit( _e( 'This download is no longer valid, Please contact the site administrator for more information.', 'wpsc' ) );
  902. }
  903. }
  904. }
  905. add_action( 'init', 'wpsc_download_file' );
  906. function wpsc_shipping_same_as_billing(){
  907. $_SESSION['shippingSameBilling'] = $_POST['wpsc_shipping_same_as_billing'];
  908. }
  909. add_action('wp_ajax_wpsc_shipping_same_as_billing', 'wpsc_shipping_same_as_billing');
  910. ?>