PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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