PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-e-commerce/wpsc-admin/ajax-and-init.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1154 lines | 903 code | 168 blank | 83 comment | 219 complexity | dd62433d6a87ec60dcd3e504cec8ab6f MD5 | raw file
  1. <?php
  2. /**
  3. * WP eCommerce Admin AJAX functions
  4. *
  5. * These are the WPSC Admin AJAX functions
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. //exit('<pre>'.print_r($_POST, true).'</pre>');
  11. function wpsc_ajax_add_tracking() {
  12. global $wpdb;
  13. foreach($_POST as $key=>$value){
  14. if($value != ''){
  15. $parts = preg_split('/^wpsc_trackingid/', $key);
  16. if(count($parts) > '1'){
  17. $id = $parts[1];
  18. $trackingid = $value;
  19. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `track_id`='".$trackingid."' WHERE `id`=".$id;
  20. $wpdb->query($sql);
  21. }
  22. }
  23. }
  24. }
  25. if($_REQUEST['submit'] == 'Add Tracking ID') {
  26. add_action('admin_init', 'wpsc_ajax_add_tracking');
  27. }
  28. function wpsc_delete_currency_layer() {
  29. global $wpdb;
  30. $meta_key = 'currency['.$_POST['currSymbol'].']';
  31. $sql= "DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key`='".$meta_key."' LIMIT 1";
  32. $wpdb->query($sql);
  33. // exit('<pre>'.print_r($_POST, true).'</pre>');
  34. }
  35. if($_REQUEST['wpsc_admin_action'] == 'delete_currency_layer') {
  36. add_action('admin_init', 'wpsc_delete_currency_layer');
  37. }
  38. function wpsc_purchlog_email_trackid() {
  39. global $wpdb;
  40. $id = absint($_POST['purchlog_id']);
  41. $trackingid = $wpdb->get_var("SELECT `track_id` FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE `id`={$id} LIMIT 1");
  42. $message = get_option('wpsc_trackingid_message');
  43. $message = str_replace('%trackid%',$trackingid,$message);
  44. $message = str_replace('%shop_name%',get_option('blogname'),$message);
  45. $email_form_field = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type` IN ('email') AND `active` = '1' ORDER BY `order` ASC LIMIT 1");
  46. $email = $wpdb->get_var("SELECT `value` FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id`=".$id." AND `form_id` = '$email_form_field' LIMIT 1");
  47. $subject = get_option('wpsc_trackingid_subject');
  48. $subject = str_replace('%shop_name%',get_option('blogname'),$subject);
  49. wp_mail($email, $subject, $message,"From: ".get_option('return_email')." <".get_option('return_email').">");
  50. //exit($email.'<br /> '.$subject.'<br /> '. $message.'<br /> '."From: ".get_option('return_email')." <".get_option('return_email').">");
  51. exit(true);
  52. }
  53. if($_REQUEST['wpsc_admin_action'] == 'purchlog_email_trackid') {
  54. add_action('admin_init', 'wpsc_purchlog_email_trackid');
  55. }
  56. function wpsc_ajax_sales_quarterly() {
  57. global $wpdb;
  58. $lastdate = $_POST['add_start'];
  59. $date = preg_split('/-/', $lastdate);
  60. $lastquart = mktime(0,0,0,$date[1], $date[2], $date[0]);
  61. //$lastdate = date('M d y', $lastquart);
  62. if($lastquart != get_option('wpsc_last_quarter')){
  63. update_option('wpsc_last_date', $lastdate);
  64. update_option('wpsc_fourth_quart', $lastquart);
  65. $thirdquart = mktime(0,0,0,$date[1]-3, $date[2], $date[0]);
  66. update_option('wpsc_third_quart', $thirdquart);
  67. $secondquart = mktime(0,0,0,$date[1]-6, $date[2], $date[0]);
  68. update_option('wpsc_second_quart', $secondquart);
  69. $firstquart = mktime(0,0,0,$date[1]-9, $date[2], $date[0]);
  70. update_option('wpsc_first_quart', $firstquart);
  71. $finalquart = mktime(0,0,0,$date[1], $date[2], $date[0]-1);
  72. update_option('wpsc_final_quart', $finalquart);
  73. }
  74. // exit($lastquart.' '.$firstquart.' '.$secondquart.' '.$thirdquart);
  75. }
  76. if($_REQUEST['wpsc_admin_action'] == 'wpsc_quarterly') {
  77. add_action('admin_init', 'wpsc_ajax_sales_quarterly');
  78. }
  79. function wpsc_ajax_load_product() {
  80. global $wpdb;
  81. $product_id = absint($_REQUEST['product_id']);
  82. check_admin_referer('edit_product_' . $product_id);
  83. wpsc_display_product_form($product_id);
  84. exit();
  85. }
  86. if($_REQUEST['wpsc_admin_action'] == 'load_product') {
  87. add_action('admin_init', 'wpsc_ajax_load_product');
  88. }
  89. function wpsc_crop_thumb() {
  90. global $wpdb;
  91. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  92. $targ_w = $targ_h = $_POST['thumbsize'];
  93. $jpeg_quality = $_POST['jpegquality'];
  94. $product_id = $_POST['product_id'];
  95. $image['x'] = absint($_POST['x']);
  96. $image['y'] = absint($_POST['y']);
  97. $image['w'] = absint($_POST['w']);
  98. $image['h'] = absint($_POST['h']);
  99. $imagename = basename($_POST['imagename']);
  100. $source = WPSC_IMAGE_DIR.$imagename;
  101. $destination = WPSC_THUMBNAIL_DIR.$imagename;
  102. if(is_file($source)) {
  103. $imagetype = getimagesize($source);
  104. switch($imagetype[2]) {
  105. case IMAGETYPE_JPEG:
  106. $img_r = imagecreatefromjpeg($source);
  107. break;
  108. case IMAGETYPE_GIF:
  109. $img_r = imagecreatefromgif($source);
  110. break;
  111. case IMAGETYPE_PNG:
  112. $img_r = imagecreatefrompng($source);
  113. break;
  114. }
  115. $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
  116. imagecopyresampled($dst_r,$img_r,0,0,$image['x'],$image['y'],$targ_w,$targ_h,$image['w'],$image['h']);
  117. imagejpeg($dst_r,$destination,$jpeg_quality);
  118. $cropped = true;
  119. }
  120. $sendback = wp_get_referer();
  121. if($cropped){
  122. $sendback = add_query_arg('product_id', $product_id, $sendback);
  123. }
  124. wp_redirect($sendback);
  125. //exit();
  126. }
  127. }
  128. if($_REQUEST['wpsc_admin_action'] == 'crop_thumb') {
  129. add_action('admin_init', 'wpsc_crop_thumb');
  130. }
  131. function wpsc_delete_file() {
  132. global $wpdb;
  133. $file_id = $_GET['file_id'];
  134. $file_hash = $wpdb->get_var("SELECT `idhash` FROM `".WPSC_TABLE_PRODUCT_FILES."` WHERE `id` LIKE '".$file_id."' LIMIT 1");
  135. check_admin_referer('delete_file_'.$file_id);
  136. if(file_exists(WPSC_FILE_DIR.basename($file_hash)) && is_file(WPSC_FILE_DIR.basename($file_hash))) {
  137. if($wpdb->query($wpdb->prepare("DELETE FROM ".WPSC_TABLE_PRODUCT_FILES." WHERE idhash=%s", $file_hash)) == 1) {
  138. // Only delete the file if the delete query above affected a single row. Prevents deletion of an arbitrary file
  139. unlink(WPSC_FILE_DIR.basename($file_hash));
  140. }
  141. }
  142. if($_POST['ajax'] !== 'true') {
  143. $sendback = wp_get_referer();
  144. wp_redirect($sendback);
  145. }
  146. exit();
  147. }
  148. if($_REQUEST['wpsc_admin_action'] == 'delete_file') {
  149. add_action('admin_init', 'wpsc_delete_file');
  150. }
  151. function wpsc_bulk_modify_products() {
  152. global $wpdb;
  153. $doaction = $_GET['bulkAction'];
  154. $sendback = wp_get_referer();
  155. switch ( $doaction ) {
  156. case 'delete':
  157. if ( isset($_GET['product']) && ! isset($_GET['bulk_edit']) && (isset($doaction) || isset($_GET['doaction2'])) ) {
  158. check_admin_referer('bulk-products', 'wpsc-bulk-products');
  159. $deleted = 0;
  160. foreach( (array) $_GET['product'] as $product_id ) {
  161. $product_id = absint($product_id);
  162. if($wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `active` = '0' WHERE `id`='{$product_id}' LIMIT 1")) {
  163. $wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id` = '{$product_id}' AND `meta_key` IN ('url_name')");
  164. product_tag_init();
  165. $term = wp_get_object_terms($product_id, 'product_tag');
  166. if ($term->errors == '') {
  167. wp_delete_object_term_relationships($product_id, 'product_tag');
  168. }
  169. $deleted++;
  170. }
  171. }
  172. }
  173. if ( isset($deleted) ) {
  174. $sendback = add_query_arg('deleted', $deleted, $sendback);
  175. }
  176. break;
  177. case 'show':
  178. case 'hide':
  179. if ( isset($_GET['product']) && ! isset($_GET['bulk_edit']) && (isset($doaction) || isset($_GET['doaction2'])) ) {
  180. check_admin_referer('bulk-products', 'wpsc-bulk-products');
  181. $flipped = 0;
  182. $status = array('show' => 1, 'hide' => 0);
  183. if( !key_exists($_REQUEST['bulkAction'], $status) ) break; // Action not valid
  184. $status_key = $_REQUEST['bulkAction'];
  185. $status_value = $status[$status_key];
  186. foreach( (array) $_GET['product'] as $product_id ) {
  187. $product_id = absint($product_id);
  188. $new_status = wpsc_set_publish_status($product_id, $status_value);
  189. $flipped++;
  190. }
  191. }
  192. $sendback = add_query_arg('flipped', $flipped, $sendback);
  193. break;
  194. default:
  195. if(isset($_GET['search']) && !empty($_GET['search'])) {
  196. $sendback = add_query_arg('search',$_GET['search'], $sendback);
  197. }
  198. break;
  199. }
  200. wp_redirect($sendback);
  201. exit();
  202. }
  203. if($_REQUEST['wpsc_admin_action'] == 'bulk_modify') {
  204. add_action('admin_init', 'wpsc_bulk_modify_products');
  205. }
  206. function wpsc_modify_product_price() {
  207. global $wpdb;
  208. $product_data = array_pop($_POST['product_price']);
  209. $product_id = absint($product_data['id']);
  210. $product_price = (float)$product_data['price'];
  211. $product_nonce = $product_data['nonce'];
  212. if(wp_verify_nonce($product_nonce, 'edit-product_price-'.$product_id) ) {
  213. if($wpdb->query("UPDATE ".WPSC_TABLE_PRODUCT_LIST." SET price='{$product_price}' WHERE id='{$product_id}'")) {
  214. echo "success = 1;\n\r";
  215. echo "new_price = '".nzshpcrt_currency_display($product_price, 1, true)."';\n\r";
  216. } else {
  217. echo "success = 0;\n\r";
  218. }
  219. } else {
  220. echo "success = -1;\n\r";
  221. }
  222. exit();
  223. }
  224. if($_REQUEST['wpsc_admin_action'] == 'modify_price') {
  225. add_action('admin_init', 'wpsc_modify_product_price');
  226. }
  227. /**
  228. Function and action for deleting single products
  229. */
  230. function wpsc_delete_product() {
  231. global $wpdb;
  232. $deleted = 0;
  233. $product_id = absint($_GET['product']);
  234. check_admin_referer('delete_product_' . $product_id);
  235. if($wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `active` = '0' WHERE `id`='{$product_id}' LIMIT 1")) {
  236. $wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id` = '{$product_id}' AND `meta_key` IN ('url_name')");
  237. product_tag_init();
  238. $term = wp_get_object_terms($product_id, 'product_tag');
  239. if ($term->errors == '') {
  240. wp_delete_object_term_relationships($product_id, 'product_tag');
  241. }
  242. $deleted = 1;
  243. do_action('wpsc_delete_product', $product_id);
  244. }
  245. $sendback = wp_get_referer();
  246. if ( isset($deleted) ) {
  247. $sendback = add_query_arg('deleted', $deleted, $sendback);
  248. }
  249. wp_redirect($sendback);
  250. exit();
  251. }
  252. if($_REQUEST['wpsc_admin_action'] == 'delete_product') {
  253. add_action('admin_init', 'wpsc_delete_product');
  254. }
  255. /**
  256. Function and action for publishing or unpublishing single products
  257. */
  258. function wpsc_ajax_toggle_published() {
  259. global $wpdb;
  260. $product_id = absint($_GET['product']);
  261. check_admin_referer('toggle_publish_' . $product_id);
  262. $status = (wpsc_toggle_publish_status($product_id)) ? ('true') : ('false');
  263. $sendback = add_query_arg('flipped', "1", wp_get_referer());
  264. wp_redirect($sendback);
  265. exit();
  266. }
  267. if($_REQUEST['wpsc_admin_action'] == 'toggle_publish') {
  268. add_action('admin_init', 'wpsc_ajax_toggle_published');
  269. }
  270. /**
  271. Function and action for duplicating products,
  272. */
  273. function wpsc_duplicate_product() {
  274. global $wpdb;
  275. $product_id = absint($_GET['product']);
  276. check_admin_referer('duplicate_product_' . $product_id);
  277. if ($product_id > 0) {
  278. $sql = " INSERT INTO ".WPSC_TABLE_PRODUCT_LIST."( `name` , `description` , `additional_description` , `price` , `weight` , `weight_unit` , `pnp` , `international_pnp` , `file` , `image` , `quantity_limited` , `quantity` , `special` , `special_price` , `display_frontpage` , `notax` , `active` , `publish`, `donation` , `no_shipping` , `thumbnail_image` , `thumbnail_state` ) SELECT `name` , `description` , `additional_description` , `price` , `weight` , `weight_unit` , `pnp` , `international_pnp` , `file` , `image` , `quantity_limited` , `quantity` , `special` , `special_price` , `display_frontpage` , `notax` , `active` , `publish`, `donation` , `no_shipping` , `thumbnail_image` , `thumbnail_state` FROM ".WPSC_TABLE_PRODUCT_LIST." WHERE id = '".$product_id."' ";
  279. // exit($sql);
  280. $wpdb->query($sql);
  281. $new_id= $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_PRODUCT_LIST."` LIMIT 1");
  282. //Inserting duplicated variations record.
  283. $nzshpcrt_variations = new nzshpcrt_variations();
  284. $nzshpcrt_variations->duplicate_variation_values($new_id, $product_id);
  285. /*
  286. $variation_assocs = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_ASSOC." WHERE associated_id = ".$product_id, ARRAY_A);
  287. if(count($variation_assocs))foreach($variation_assocs as $variation_assoc){
  288. $wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_ASSOC." VALUES ('', '".$variation_assoc['type']."', '".$variation_assoc['name']."', '".$new_id."', '".$variation_assoc['variation_id']."');");
  289. }
  290. $variation_combinations = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_COMBINATIONS." WHERE product_id = ".$product_id, ARRAY_A);
  291. if(count($variation_combinations))foreach($variation_combinations as $variation_combination){
  292. $variation_properties = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_PROPERTIES." WHERE id = ".$variation_combination['priceandstock_id'], ARRAY_A);
  293. exit('<pre>'.print_r($variation_properties, true).'</pre>');
  294. $wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_PROPERTIES." VALUES ('', '".$new_id."', '".$variation_properties[0]['stock']."', '".$variation_properties[0]['price']."', '".$variation_properties[0]['weight']."', '".$variation_properties[0]['weight_unit']."', '".$variation_properties[0]['visibility']."', '".$variation_properties[0]['file']."');");
  295. $new_prop_id= $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` LIMIT 1");
  296. $wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_COMBINATIONS." VALUES ('".$new_id."', '".$new_prop_id."', '".$variation_combination['value_id']."', '".$variation_combination['variation_id']."', '".$variation_combination['all_variation_ids']."');");
  297. }
  298. $variation_values_assocs = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_VALUES_ASSOC." WHERE product_id = ".$product_id, ARRAY_A);
  299. if(count($variation_values_assocs))foreach($variation_values_assocs as $variation_values_assoc){
  300. $wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_VALUES_ASSOC." VALUES ('', '".$new_id."', '".$variation_values_assoc['value_id']."', '".$variation_values_assoc['visible']."', '".$variation_values_assoc['variation_id']."');");
  301. }
  302. //end of variations
  303. */
  304. //Inserting duplicated category record.
  305. $category_assoc = $wpdb->get_col("SELECT `category_id` FROM ".WPSC_TABLE_ITEM_CATEGORY_ASSOC." WHERE product_id = '".$product_id."'");
  306. $new_product_category = array();
  307. if (count($category_assoc) > 0) {
  308. foreach($category_assoc as $key => $category) {
  309. $new_product_category[] = "('".$new_id."','".$category."')";
  310. $check_existing = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_PRODUCT_ORDER."` WHERE `category_id` IN('$category') AND `order` IN('0') LIMIT 1;",ARRAY_A);
  311. if($wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PRODUCT_ORDER."` WHERE `category_id` IN('$category') AND `product_id` IN('$product_id') LIMIT 1")) {
  312. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_ORDER."` SET `order` = '0' WHERE `category_id` IN('$category') AND `product_id` IN('$product_id') LIMIT 1;");
  313. } else {
  314. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_ORDER."` (`category_id`, `product_id`, `order`) VALUES ('$category', '$product_id', 0)");
  315. }
  316. if($check_existing != null) {
  317. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_ORDER."` SET `order` = (`order` + 1) WHERE `category_id` IN('$category') AND `product_id` NOT IN('$product_id') AND `order` < '0'");
  318. }
  319. }
  320. $wpdb->query("INSERT INTO ".WPSC_TABLE_ITEM_CATEGORY_ASSOC." (product_id, category_id) VALUES ".implode(",",$new_product_category));
  321. }
  322. //Inserting duplicated meta info
  323. $meta_values = $wpdb->get_results("SELECT `meta_key`, `meta_value`, `custom` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id='".$product_id."' AND `meta_key` NOT IN ('url_name') ", ARRAY_A);
  324. $new_meta_value = array();
  325. if (count($meta_values)>0) {
  326. foreach($meta_values as $key => $meta) {
  327. $new_meta_value[] = "('".$new_id."','".$meta['meta_key']."','".$meta['meta_value']."','".$meta['custom']."')";
  328. }
  329. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCTMETA."` (`product_id`, `meta_key`, `meta_value`, `custom`) VALUES ".implode(",",$new_meta_value));
  330. }
  331. $product_name = $wpdb->get_var("SELECT `name` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '$new_id' LIMIT 1");
  332. if($product_name != '') {
  333. $tidied_name = strtolower(trim($product_name));
  334. $url_name = sanitize_title($tidied_name);
  335. $similar_names = $wpdb->get_row("SELECT COUNT(*) AS `count`, MAX(REPLACE(`meta_value`, '$url_name', '')) AS `max_number` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ('url_name') AND `meta_value` REGEXP '^($url_name){1}[[:digit:]]*$' ",ARRAY_A);
  336. $extension_number = '';
  337. if($similar_names['count'] > 0) {
  338. $extension_number = (int)$similar_names['max_number']+1;
  339. }
  340. $url_name .= $extension_number;
  341. add_product_meta($new_id, 'url_name', $url_name,true);
  342. }
  343. $tags = wp_get_object_terms($product_id, 'product_tag', array('fields' => 'names'));
  344. wp_set_object_terms($new_id, $tags, 'product_tag');
  345. //Inserting duplicated image info
  346. $image_values = $wpdb->get_results("SELECT `image`, `width`, `height`, `image_order`, `meta` FROM ".WPSC_TABLE_PRODUCT_IMAGES." WHERE product_id='".$product_id."'", ARRAY_A);
  347. $new_image_value = array();
  348. if (count($image_values)>0){
  349. foreach($image_values as $key => $image) {
  350. if($image['image'] != '') {
  351. if(is_numeric($image['width']) && is_numeric($image['height'])) {
  352. $image['width'] = absint($image['width']);
  353. $image['height'] = absint($image['height']);
  354. } else {
  355. $image['width'] = 'null';
  356. $image['height'] = 'null';
  357. }
  358. $new_image_value[] = "('".$new_id."','".$image['image']."',".$image['width'].",".$image['height'].",'".$image['image_order']."','".$image['meta']."')";
  359. }
  360. }
  361. if(count($new_image_value) > 0) {
  362. $new_image_value = implode(",", $new_image_value);
  363. $sql = "INSERT INTO ".WPSC_TABLE_PRODUCT_IMAGES." (`product_id`, `image`, `width`, `height`, `image_order`, `meta`) VALUES ".$new_image_value;
  364. $wpdb->query($sql);
  365. }
  366. }
  367. $duplicated = true;
  368. }
  369. $sendback = wp_get_referer();
  370. if ( isset($duplicated) ) {
  371. $sendback = add_query_arg('duplicated', (int)$duplicated, $sendback);
  372. }
  373. wp_redirect($sendback);
  374. exit();
  375. }
  376. if ($_GET['wpsc_admin_action'] == 'duplicate_product') {
  377. add_action('admin_init', 'wpsc_duplicate_product');
  378. }
  379. function wpsc_purchase_log_csv() {
  380. global $wpdb,$user_level,$wp_rewrite;
  381. get_currentuserinfo();
  382. if(($_GET['rss_key'] == 'key') && is_numeric($_GET['start_timestamp']) && is_numeric($_GET['end_timestamp']) && ($user_level >= 7)) {
  383. //exit('in use');
  384. $form_sql = "SELECT * FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `active` = '1' AND `display_log` = '1';";
  385. $form_data = $wpdb->get_results($form_sql,ARRAY_A);
  386. $start_timestamp = $_GET['start_timestamp'];
  387. $end_timestamp = $_GET['end_timestamp'];
  388. $data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `date` BETWEEN '$start_timestamp' AND '$end_timestamp' ORDER BY `date` DESC",ARRAY_A);
  389. // exit('<pre>'.print_r($data, true).'</pre>');
  390. header('Content-Type: text/csv');
  391. header('Content-Disposition: inline; filename="Purchase Log '.date("M-d-Y", $start_timestamp).' to '.date("M-d-Y", $end_timestamp).'.csv"');
  392. foreach((array)$data as $purchase) {
  393. $country_sql = "SELECT * FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id` = '".$purchase['id']."' AND `form_id` = '".get_option('country_form_field')."' LIMIT 1";
  394. $country_data = $wpdb->get_results($country_sql,ARRAY_A);
  395. $country = $country_data[0]['value'];
  396. $output .= "\"".$purchase['totalprice'] ."\",";
  397. foreach((array)$form_data as $form_field) {
  398. $collected_data_sql = "SELECT * FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id` = '".$purchase['id']."' AND `form_id` = '".$form_field['id']."' LIMIT 1";
  399. $collected_data = $wpdb->get_results($collected_data_sql,ARRAY_A);
  400. $collected_data = $collected_data[0];
  401. $output .= "\"".$collected_data['value']."\",";
  402. }
  403. if(get_option('payment_method') == 2) {
  404. $gateway_name = '';
  405. foreach($GLOBALS['nzshpcrt_gateways'] as $gateway) {
  406. if($purchase['gateway'] != 'testmode') {
  407. if($gateway['internalname'] == $purchase['gateway'] ) {
  408. $gateway_name = $gateway['name'];
  409. }
  410. } else {
  411. $gateway_name = "Manual Payment";
  412. }
  413. }
  414. $output .= "\"". $gateway_name ."\",";
  415. }
  416. if($purchase['processed'] < 1) {
  417. $purchase['processed'] = 1;
  418. }
  419. $stage_sql = "SELECT * FROM `".WPSC_TABLE_PURCHASE_STATUSES."` WHERE `id`='".$purchase['processed']."' AND `active`='1' LIMIT 1";
  420. $stage_data = $wpdb->get_results($stage_sql,ARRAY_A);
  421. $output .= "\"". $stage_data[0]['name'] ."\",";
  422. $output .= "\"". date("jS M Y",$purchase['date']) ."\"";
  423. $cartsql = "SELECT * FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE `purchaseid`=".$purchase['id']."";
  424. $cart = $wpdb->get_results($cartsql,ARRAY_A) ;
  425. //exit(nl2br(print_r($cart,true)));
  426. foreach((array)$cart as $item) {
  427. $output .= ",";
  428. $product = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`=".$item['prodid']." LIMIT 1",ARRAY_A);
  429. $skusql = "SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key`= 'sku' AND `product_id` = ".$item['prodid'];
  430. // exit($skusql);
  431. $skuvalue = $wpdb->get_var($skusql);
  432. $variation_sql = "SELECT * FROM `".WPSC_TABLE_CART_ITEM_VARIATIONS."` WHERE `cart_id`='".$item['id']."'";
  433. $variation_data = $wpdb->get_results($variation_sql,ARRAY_A);
  434. $variation_count = count($variation_data);
  435. if($variation_count >= 1) {
  436. $variation_list = " (";
  437. $i = 0;
  438. foreach($variation_data as $variation) {
  439. if($i > 0) {
  440. $variation_list .= ", ";
  441. }
  442. $value_id = $variation['value_id'];
  443. $value_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id`='".$value_id."' LIMIT 1",ARRAY_A);
  444. $variation_list .= $value_data[0]['name'];
  445. $i++;
  446. }
  447. $variation_list .= ")";
  448. }
  449. // exit('<pre>'.print_r($item,true).'</pre>');
  450. $output .= "\"".$item['quantity']." ".str_replace('"', '\"',$product['name']).$variation_list."\"";
  451. $output .= ",".$skuvalue;
  452. }
  453. $output .= "\n"; // terminates the row/line in the CSV file
  454. }
  455. echo $output;
  456. exit();
  457. }
  458. }
  459. if($_REQUEST['wpsc_admin_action'] == 'wpsc_downloadcsv') {
  460. add_action('admin_init', 'wpsc_purchase_log_csv');
  461. }
  462. function wpsc_admin_ajax() {
  463. global $wpdb,$user_level,$wp_rewrite;
  464. get_currentuserinfo();
  465. if(is_numeric($_POST['catid'])) {
  466. /* fill category form */
  467. echo nzshpcrt_getcategoryform($_POST['catid']);
  468. exit();
  469. } else if(is_numeric($_POST['brandid'])) {
  470. /* fill brand form */
  471. echo nzshpcrt_getbrandsform($_POST['brandid']);
  472. exit();
  473. } else if(is_numeric($_POST['variation_id'])) {
  474. echo nzshpcrt_getvariationform($_POST['variation_id']);
  475. exit();
  476. }
  477. if ($_POST['action'] == 'product-page-order'){
  478. $order = $_POST['order'];
  479. if(!isset($order[0])) {
  480. $order = $order['normal'];
  481. } else {
  482. $order = $order[0];
  483. }
  484. $order = array_unique(explode(',', $order));
  485. update_option('wpsc_product_page_order', $order);
  486. exit(print_r($order,1));
  487. }
  488. if(($_POST['save_image_upload_state'] == "true") && is_numeric($_POST['image_upload_state'])) {
  489. //get_option('wpsc_image_upload_state');
  490. $upload_state = (int)(bool)$_POST['image_upload_state'];
  491. update_option('wpsc_use_flash_uploader', $upload_state);
  492. exit("done");
  493. }
  494. if(($_POST['remove_variation_value'] == "true") && is_numeric($_POST['variation_value_id'])) {
  495. $wpdb->query("DELETE FROM `".WPSC_TABLE_VARIATION_VALUES_ASSOC."` WHERE `value_id` = '".(int)$_POST['variation_value_id']."'");
  496. $wpdb->query("DELETE FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id` = '".(int)$_POST['variation_value_id']."' LIMIT 1");
  497. exit();
  498. }
  499. if(($_POST['edit_variation_value_list'] == 'true') && is_numeric($_POST['variation_id']) && is_numeric($_POST['product_id'])) {
  500. $variation_id = (int)$_POST['variation_id'];
  501. $product_id = (int)$_POST['product_id'];
  502. $variations_processor = new nzshpcrt_variations();
  503. $variation_values = $variations_processor->falsepost_variation_values($variation_id);
  504. if(is_array($variation_values)) {
  505. //echo(print_r($variation_values,true));
  506. $check_variation_added = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_VARIATION_ASSOC."` WHERE `type` IN ('product') AND `associated_id` IN ('{$product_id}') AND `variation_id` IN ('{$variation_id}') LIMIT 1");
  507. //exit("<pre>".print_r($variation_values,true)."<pre>");
  508. if($check_variation_added == null) {
  509. $variations_processor->add_to_existing_product($product_id,$variation_values);
  510. }
  511. echo $variations_processor->display_attached_variations($product_id);
  512. echo $variations_processor->variations_grid_view($product_id);
  513. } else {
  514. echo "false";
  515. }
  516. exit();
  517. }
  518. if(($_POST['remove_form_field'] == "true") && is_numeric($_POST['form_id'])) {
  519. //exit(print_r($user,true));
  520. if(current_user_can('level_7')) {
  521. $wpdb->query($wpdb->prepare("UPDATE `".WPSC_TABLE_CHECKOUT_FORMS."` SET `active` = '0' WHERE `id` = %d LIMIT 1 ;", $_POST['form_id']));
  522. exit(' ');
  523. }
  524. }
  525. if($_POST['hide_ecom_dashboard'] == 'true') {
  526. require_once (ABSPATH . WPINC . '/rss.php');
  527. $rss = fetch_rss('http://www.instinct.co.nz/feed/');
  528. $rss->items = array_slice($rss->items, 0, 5);
  529. $rss_hash = sha1(serialize($rss->items));
  530. update_option('wpsc_ecom_news_hash', $rss_hash);
  531. exit(1);
  532. }
  533. if(($_POST['remove_meta'] == 'true') && is_numeric($_POST['meta_id'])) {
  534. $meta_id = (int)$_POST['meta_id'];
  535. $selected_meta = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `id` IN('{$meta_id}') ",ARRAY_A);
  536. if($selected_meta != null) {
  537. if($wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `id` IN('{$meta_id}') LIMIT 1")) {
  538. echo $meta_id;
  539. exit();
  540. }
  541. }
  542. echo 0;
  543. exit();
  544. }
  545. if(($_REQUEST['log_state'] == "true") && is_numeric($_POST['id']) && is_numeric($_POST['value'])) {
  546. $newvalue = $_POST['value'];
  547. if ($_REQUEST['suspend']=='true'){
  548. if ($_REQUEST['value']==1){
  549. wpsc_member_dedeactivate_subscriptions($_POST['id']);
  550. } else {
  551. wpsc_member_deactivate_subscriptions($_POST['id']);
  552. }
  553. exit();
  554. } else {
  555. $log_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `id` = '".$_POST['id']."' LIMIT 1",ARRAY_A);
  556. if (($newvalue==2) && function_exists('wpsc_member_activate_subscriptions')){
  557. wpsc_member_activate_subscriptions($_POST['id']);
  558. }
  559. $update_sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `processed` = '".$newvalue."' WHERE `id` = '".$_POST['id']."' LIMIT 1";
  560. $wpdb->query($update_sql);
  561. //echo("/*");
  562. if(($newvalue > $log_data['processed']) && ($log_data['processed'] < 2)) {
  563. transaction_results($log_data['sessionid'],false);
  564. }
  565. //echo("*/");
  566. $stage_sql = "SELECT * FROM `".WPSC_TABLE_PURCHASE_STATUSES."` WHERE `id`='".$newvalue."' AND `active`='1' LIMIT 1";
  567. $stage_data = $wpdb->get_row($stage_sql,ARRAY_A);
  568. echo "document.getElementById(\"form_group_".$_POST['id']."_text\").innerHTML = '".$stage_data['name']."';\n";
  569. echo "document.getElementById(\"form_group_".$_POST['id']."_text\").style.color = '#".$stage_data['colour']."';\n";
  570. $year = date("Y");
  571. $month = date("m");
  572. $start_timestamp = mktime(0, 0, 0, $month, 1, $year);
  573. $end_timestamp = mktime(0, 0, 0, ($month+1), 0, $year);
  574. echo "document.getElementById(\"log_total_month\").innerHTML = '".addslashes(nzshpcrt_currency_display(admin_display_total_price($start_timestamp, $end_timestamp),1))."';\n";
  575. echo "document.getElementById(\"log_total_absolute\").innerHTML = '".addslashes(nzshpcrt_currency_display(admin_display_total_price(),1))."';\n";
  576. exit();
  577. }
  578. }
  579. if(($_POST['list_variation_values'] == "true")) {
  580. // retrieve the forms for associating variations and their values with products
  581. $variation_processor = new nzshpcrt_variations();
  582. $variations_selected = array();
  583. foreach((array)$_POST['variations'] as $variation_id => $checked) {
  584. $variations_selected[] = (int)$variation_id;
  585. }
  586. if(is_numeric($_POST['product_id']) && ($_POST['product_id'] > 0)) {
  587. $product_id = absint($_POST['product_id']);
  588. $selected_price = (float)$_POST['selected_price'];
  589. // variation values housekeeping
  590. $completed_variation_values = $variation_processor->edit_product_values($product_id,$_POST['edit_var_val'], $selected_price);
  591. // get all the currently associated variations from the database
  592. $associated_variations = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_VARIATION_ASSOC."` WHERE `type` IN ('product') AND `associated_id` IN ('{$product_id}')", ARRAY_A);
  593. $variations_still_associated = array();
  594. foreach((array)$associated_variations as $associated_variation) {
  595. // remove variations not checked that are in the database
  596. if(array_search($associated_variation['variation_id'], $variations_selected) === false) {
  597. $wpdb->query("DELETE FROM `".WPSC_TABLE_VARIATION_ASSOC."` WHERE `id` = '{$associated_variation['id']}' LIMIT 1");
  598. $wpdb->query("DELETE FROM `".WPSC_TABLE_VARIATION_VALUES_ASSOC."` WHERE `product_id` = '{$product_id}' AND `variation_id` = '{$associated_variation['variation_id']}' ");
  599. } else {
  600. // make an array for adding in the variations next step, for efficiency
  601. $variations_still_associated[] = $associated_variation['variation_id'];
  602. }
  603. }
  604. foreach((array)$variations_selected as $variation_id) {
  605. // add variations not already in the database that have been checked.
  606. $variation_values = $variation_processor->falsepost_variation_values($variation_id);
  607. if(array_search($variation_id, $variations_still_associated) === false) {
  608. $variation_processor->add_to_existing_product($product_id,$variation_values);
  609. }
  610. }
  611. //echo "/* ".print_r($variation_values,true)." */\n\r";
  612. echo "edit_variation_combinations_html = \"".str_replace(array("\n","\r"), array('\n','\r'), addslashes($variation_processor->variations_grid_view($product_id, (array)$completed_variation_values)))."\";\n";
  613. } else {
  614. if(count($variations_selected) > 0) {
  615. // takes an array of variations, returns a form for adding data to those variations.
  616. if((float)$_POST['selected_price'] > 0) {
  617. $selected_price = (float)$_POST['selected_price'];
  618. }
  619. $limited_stock = false;
  620. if($_POST['limited_stock'] == 'true') {
  621. $limited_stock = true;
  622. }
  623. $selected_variation_values = array();
  624. foreach($_POST['edit_var_val'] as $variation_value_array) {
  625. //echo "/* ".print_r($variation_value_array,true)." */\n\r";
  626. $selected_variation_values = array_merge(array_keys($variation_value_array), $selected_variation_values);
  627. }
  628. ////echo "/* ".print_r($selected_variation_values,true)." */\n\r";
  629. echo "edit_variation_combinations_html = \"".__('Edit Variation Set', 'wpsc')."<br />".str_replace(array("\n","\r"), array('\n','\r'), addslashes($variation_processor->variations_grid_view(0, (array)$variations_selected, (array)$selected_variation_values, $selected_price, $limited_stock)))."\";\n";
  630. } else {
  631. echo "edit_variation_combinations_html = \"\";\n";
  632. }
  633. }
  634. exit();
  635. }
  636. if(isset($_POST['language_setting']) && ($_GET['page'] = WPSC_DIR_NAME.'/wpsc-admin/display-options.page.php')) {
  637. if($user_level >= 7) {
  638. update_option('language_setting', $_POST['language_setting']);
  639. }
  640. }
  641. }
  642. function wpsc_admin_sale_rss() {
  643. global $wpdb;
  644. if(($_GET['rss'] == "true") && ($_GET['rss_key'] == 'key') && ($_GET['action'] == "purchase_log")) {
  645. $sql = "SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `date`!='' ORDER BY `date` DESC";
  646. $purchase_log = $wpdb->get_results($sql,ARRAY_A);
  647. header("Content-Type: application/xml; charset=UTF-8");
  648. header('Content-Disposition: inline; filename="WP_E-Commerce_Purchase_Log.rss"');
  649. $output = '';
  650. $output .= "<?xml version='1.0'?>\n\r";
  651. $output .= "<rss version='2.0'>\n\r";
  652. $output .= " <channel>\n\r";
  653. $output .= " <title>WP E-Commerce Product Log</title>\n\r";
  654. $output .= " <link>".get_option('siteurl')."/wp-admin/admin.php?page=".WPSC_DIR_NAME."/display-log.php</link>\n\r";
  655. $output .= " <description>This is the WP E-Commerce Product Log RSS feed</description>\n\r";
  656. $output .= " <generator>WP E-Commerce Plugin</generator>\n\r";
  657. foreach((array)$purchase_log as $purchase) {
  658. $purchase_link = get_option('siteurl')."/wp-admin/admin.php?page=".WPSC_DIR_NAME."/display-log.php&amp;purchaseid=".$purchase['id'];
  659. $output .= " <item>\n\r";
  660. $output .= " <title>Purchase No. ".$purchase['id']."</title>\n\r";
  661. $output .= " <link>$purchase_link</link>\n\r";
  662. $output .= " <description>This is an entry in the purchase log.</description>\n\r";
  663. $output .= " <pubDate>".date("r",$purchase['date'])."</pubDate>\n\r";
  664. $output .= " <guid>$purchase_link</guid>\n\r";
  665. $output .= " </item>\n\r";
  666. }
  667. $output .= " </channel>\n\r";
  668. $output .= "</rss>";
  669. echo $output;
  670. exit();
  671. }
  672. }
  673. function wpsc_swfupload_images() {
  674. global $wpdb, $current_user;
  675. // xdebug_start_trace();
  676. $file = $_FILES['async-upload'];
  677. $product_id = absint($_POST['product_id']);
  678. $nonce = $_POST['_wpnonce'];
  679. $output = '';
  680. // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead, code is from wp-admin/async-upload.php
  681. if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) ) {
  682. $_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
  683. } else if ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) ) {
  684. $_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
  685. }
  686. unset($current_user);
  687. require_once(ABSPATH . 'wp-admin/admin.php');
  688. if(!current_user_can('upload_files') ) {
  689. exit("status=-1;\n");
  690. }
  691. if(!wp_verify_nonce($nonce, 'product-swfupload') ) {
  692. exit("status=-1;\n");
  693. }
  694. if(function_exists('gold_shpcrt_display_gallery')) {
  695. // if more than one image is permitted
  696. $existing_image_data = $wpdb->get_row("SELECT COUNT(*) AS `count`, MAX(image_order) AS `order` FROM ".WPSC_TABLE_PRODUCT_IMAGES." WHERE `product_id`='".absint($product_id)."' AND `image` NOT IN ('')", ARRAY_A);
  697. $order = (int)$existing_image_data['order'];
  698. $count = $existing_image_data['count'];
  699. $previous_image = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='".absint($product_id)."' LIMIT 1");
  700. //echo "/* $count $previous_image */ ";
  701. if(($count > 0) && ( (strlen($previous_image) > 0) || (absint($product_id) < 1) ) ) {
  702. // if there is more than one image
  703. $success = move_uploaded_file($file['tmp_name'], WPSC_IMAGE_DIR.basename($file['name']));
  704. if ($product_id == '') {
  705. copy(WPSC_IMAGE_DIR.basename($file['name']),WPSC_THUMBNAIL_DIR.basename($file['name']));
  706. }
  707. $order++;
  708. if ($success) {
  709. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_IMAGES."` ( `product_id` , `image` , `width` , `height` , `image_order` ) VALUES( '$product_id','".basename($file['name'])."', '0', '0', '$order')");
  710. $id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` LIMIT 1");
  711. $src = $file['name'];
  712. $output .= "upload_status=1;\n";
  713. $output .= "image_src='".$src."';\n";
  714. $output .= "image_id='$id';\n";
  715. $output .= "product_id='$product_id';\n";
  716. $output .= "replace_existing=0;";
  717. } else {
  718. $output .= "status=0;\n";
  719. }
  720. } else {
  721. // if thereare no images
  722. if($product_id > 0) {
  723. $image_data = wpsc_item_process_image($product_id, $file['tmp_name'], $file['name'], 0, 0, 1, true);
  724. $src = $image_data['filename'];
  725. $image_id = $image_data['image_id'];
  726. } else {
  727. $success = move_uploaded_file($file['tmp_name'], WPSC_IMAGE_DIR.basename($file['name']));
  728. copy(WPSC_IMAGE_DIR.basename($file['name']),WPSC_THUMBNAIL_DIR.basename($file['name']));
  729. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_IMAGES."` ( `product_id` , `image` , `width` , `height` , `image_order` ) VALUES( '$product_id','".basename($file['name'])."', '0', '0', '0')");
  730. $src = basename($file['name']);
  731. }
  732. if($src != null) {
  733. if($image_id < 1) {
  734. $image_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` LIMIT 1");
  735. if($product_id > 0) {
  736. $previous_image = $wpdb->get_var("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `image` = '{$image_id}' WHERE `id`='{$product_id}' LIMIT 1");
  737. }
  738. }
  739. $output .= "upload_status=1;\n";
  740. $output .= "image_src='".$src."';\n";
  741. $output .= "image_id='$image_ids';\n";
  742. $output .= "product_id='$product_id';\n";
  743. $output .= "replace_existing=1;";
  744. } else {
  745. $output .= "status=0;\n";
  746. }
  747. }
  748. } else {
  749. // Otherwise...
  750. $previous_image = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  751. $image_replaced = false;
  752. if($product_id > 0) {
  753. $image_data = wpsc_item_process_image($product_id, $file['tmp_name'], $file['name'], 0, 0, 1, true);
  754. $src = $image_data['filename'];
  755. $image_id = $image_data['image_id'];
  756. } else {
  757. $success = move_uploaded_file($file['tmp_name'], WPSC_IMAGE_DIR.basename($file['name']));
  758. copy(WPSC_IMAGE_DIR.basename($file['name']),WPSC_THUMBNAIL_DIR.basename($file['name']));
  759. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_IMAGES."` ( `product_id` , `image` , `width` , `height` , `image_order` ) VALUES( '$product_id','".basename($file['name'])."', '0', '0', '0')");
  760. $src = basename($file['name']);
  761. }
  762. //$src = wpsc_item_process_image($product_id, $file['tmp_name'], $file['name']);
  763. if($src != null) {
  764. if($image_id < 1) {
  765. $image_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` LIMIT 1");
  766. if($product_id > 0) {
  767. $previous_image = $wpdb->get_var("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `image` = '{$image_id}' WHERE `id`='{$product_id}' LIMIT 1");
  768. }
  769. }
  770. $image_replaced = true;
  771. $output .= "upload_status=1;\n";
  772. $output .= "image_src='".$src."';\n";
  773. $output .= "image_id='$image_id';\n";
  774. $output .= "product_id='$product_id';\n";
  775. if($image_replaced == true) {
  776. $output .= "replace_existing=1;\n";
  777. } else {
  778. $output .= "replace_existing=0;\n";
  779. }
  780. } else {
  781. $output .= "status=0;\n";
  782. }
  783. }
  784. exit($output);
  785. }
  786. function wpsc_display_invoice() {
  787. $purchase_id = (int)$_GET['purchaselog_id'];
  788. include_once(WPSC_FILE_PATH."/admin-form-functions.php");
  789. // echo "testing";
  790. require_once(ABSPATH.'wp-admin/includes/media.php');
  791. wp_iframe('wpsc_packing_slip', $purchase_id);
  792. //wpsc_packing_slip($purchase_id);
  793. exit();
  794. }
  795. if($_REQUEST['wpsc_admin_action'] == 'wpsc_display_invoice') {
  796. add_action('admin_init', 'wpsc_display_invoice');
  797. }
  798. function wpsc_save_inline_price() {
  799. global $wpdb;
  800. $pid = $_POST['id'];
  801. $new_price = $_POST['value'];
  802. $new_price1 = str_replace('$','',$new_price);
  803. $wpdb->query("UPDATE ".WPSC_TABLE_PRODUCT_LIST." SET price='$new_price1' WHERE id='$pid'");
  804. exit($new_price);
  805. }
  806. if($_GET['inline_price']=='true') {
  807. add_action('admin_init', 'wpsc_save_inline_price', 0);
  808. }
  809. /**
  810. * Purchase log ajax code starts here
  811. */
  812. function wpsc_purchlog_resend_email(){
  813. global $wpdb;
  814. $siteurl = get_option('siteurl');
  815. $log_id = $_GET['email_buyer_id'];
  816. if(is_numeric($log_id)) {
  817. $selectsql = "SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `id`= ".$log_id." LIMIT 1";
  818. $purchase_log = $wpdb->get_row($selectsql,ARRAY_A) ;
  819. if(($purchase_log['gateway'] == "testmode") && ($purchase_log['processed'] < 2)) {
  820. $message = get_option("wpsc_email_receipt");
  821. $message_html = "<h2 style='font-size:16px;font-weight:bold;color:#000;border:0px;padding-top: 0px;' >".__('Your Order', 'wpsc')."</h2>";
  822. } else {
  823. $message = get_option("wpsc_email_receipt");
  824. $message_html = $message;
  825. }
  826. $order_url = $siteurl."/wp-admin/admin.php?page=".WPSC_DIR_NAME."/display-log.php&amp;purchcaseid=".$purchase_log['id'];
  827. $cartsql = "SELECT * FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE `purchaseid`=".$purchase_log['id']."";
  828. $cart = $wpdb->get_results($cartsql,ARRAY_A);
  829. if($purchase_log['shipping_country'] != '') {
  830. $billing_country = $purchase_log['billing_country'];
  831. $shipping_country = $purchase_log['shipping_country'];
  832. } else {
  833. $country = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id`=".$purchase_log['id']." AND `form_id` = '".get_option('country_form_field')."' LIMIT 1",ARRAY_A);
  834. $billing_country = $country[0]['value'];
  835. $shipping_country = $country[0]['value'];
  836. }
  837. $email_form_field = $wpdb->get_results("SELECT `id`,`type` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type` IN ('email') AND `active` = '1' ORDER BY `order` ASC LIMIT 1",ARRAY_A);
  838. $email_address = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id`=".$purchase_log['id']." AND `form_id` = '".$email_form_field[0]['id']."' LIMIT 1",ARRAY_A);
  839. $email = $email_address[0]['value'];
  840. $previous_download_ids = array(0);
  841. if(($cart != null)) {
  842. foreach($cart as $row) {
  843. $link = "";
  844. $productsql= "SELECT * FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`=".$row['prodid']."";
  845. $product_data = $wpdb->get_results($productsql,ARRAY_A) ;
  846. if($product_data[0]['file'] > 0) {
  847. if($purchase_log['email_sent'] != 1) {
  848. $wpdb->query("UPDATE `".WPSC_TABLE_DOWNLOAD_STATUS."` SET `active`='1' WHERE `fileid`='".$product_data[0]['file']."' AND `purchid` = '".$purchase_log['id']."' LIMIT 1");
  849. }
  850. if (($purchase_log['processed'] >= 2)) {
  851. $download_data = $wpdb->get_results("SELECT *
  852. FROM `".WPSC_TABLE_DOWNLOAD_STATUS."` INNER JOIN `".WPSC_TABLE_PRODUCT_FILES."`
  853. ON `".WPSC_TABLE_DOWNLOAD_STATUS."`.`fileid` = `".WPSC_TABLE_PRODUCT_FILES."`.`id`
  854. WHERE `".WPSC_TABLE_DOWNLOAD_STATUS."`.`active`='1'
  855. AND `".WPSC_TABLE_DOWNLOAD_STATUS."`.`purchid`='".$purchase_log['id']."'
  856. AND (
  857. `".WPSC_TABLE_DOWNLOAD_STATUS."`.`cartid` = '".$row['id']."'
  858. OR (
  859. `".WPSC_TABLE_DOWNLOAD_STATUS."`.`cartid` IS NULL
  860. AND `".WPSC_TABLE_DOWNLOAD_STATUS."`.`fileid` = '{$product_data['file']}'
  861. )
  862. )
  863. AND `".WPSC_TABLE_DOWNLOAD_STATUS."`.`id` NOT IN ('".implode("','",$previous_download_ids)."')",ARRAY_A);
  864. $link=array();
  865. //exit('IM HERE'.$errorcode.'<pre>'.print_r($download_data).'</pre>');
  866. if(sizeof($download_data) != 0) {
  867. foreach($download_data as $single_download){
  868. if($single_download['uniqueid'] == null){// if the uniqueid is not equal to null, its "valid", regardless of what it is
  869. $link[] = array("url"=>$siteurl."?downloadid=".$single_download['id'], "name" =>$single_download["filename"]);
  870. } else {
  871. $link[] = array("url"=>$siteurl."?downloadid=".$single_download['uniqueid'], "name" =>$single_download["filename"]);
  872. }
  873. }
  874. }
  875. $previous_download_ids[] = $download_data['id'];
  876. $order_status= 4;
  877. }
  878. }
  879. do_action('wpsc_confirm_checkout', $purchase_log['id']);
  880. $shipping = nzshpcrt_determine_item_shipping($row['prodid'], $row['quantity'], $shipping_country);
  881. if (isset($_SESSION['quote_shipping'])){
  882. $shipping = $_SESSION['quote_shipping'];
  883. }
  884. $total_shipping += $shipping;
  885. if($product_data[0]['special']==1) {
  886. $price_modifier = $product_data[0]['special_price'];
  887. } else {
  888. $price_modifier = 0;
  889. }
  890. $total+=($row['price']*$row['quantity']);
  891. $message_price = nzshpcrt_currency_display(($row['price']*$row['quantity']), $product_data[0]['notax'], true);
  892. $shipping_price = nzshpcrt_currency_display($shipping, 1, true);
  893. $variation_sql = "SELECT * FROM `".WPSC_TABLE_CART_ITEM_VARIATIONS."` WHERE `cart_id`='".$row['id']."'";
  894. $variation_data = $wpdb->get_results($variation_sql,ARRAY_A);
  895. $variation_count = count($variation_data);
  896. if($variation_count > 1) {
  897. $variation_list = " (";
  898. if($purchase['gateway'] != 'testmode') {
  899. if($gateway['internalname'] == $purch_data[0]['gateway'] ) {
  900. $gateway_name = $gateway['name'];
  901. }
  902. } else {
  903. $gateway_name = "Manual Payment";
  904. }
  905. $i = 0;
  906. foreach($variation_data as $variation) {
  907. if($i > 0) {
  908. $variation_list.= ", ";
  909. }
  910. $value_id = $variation['value_id'];
  911. $value_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id`='".$value_id."' LIMIT 1",ARRAY_A);
  912. $variation_list.= $value_data[0]['name'];
  913. $i++;
  914. }
  915. $variation_list .= ")";
  916. } else {
  917. if($variation_count == 1) {
  918. $value_id = $variation_data[0]['value_id'];
  919. $value_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id`='".$value_id."' LIMIT 1",ARRAY_A);
  920. $variation_list = " (".$value_data[0]['name'].")";
  921. } else {
  922. $variation_list = '';
  923. }
  924. }
  925. if($link != '') {
  926. $product_list .= " - ". $product_data['name'] . stripslashes($variation_list) ." ".$message_price ." ".__('Click to download', 'wpsc').":";
  927. $product_list_html .= " - ". $product_data['name'] . stripslashes($variation_list) ." ".$message_price ."&nbsp;&nbsp;".__('Click to download', 'wpsc').":\n\r";
  928. foreach($link as $single_link) {
  929. $product_list .= "\n\r ".$single_link["name"].": ".$single_link["url"]."\n\r";
  930. $product_list_html .= "<a href='".$single_link["url"]."'>".$single_link["name"]."</a>\n";
  931. }
  932. } else {
  933. $plural = '';
  934. if($row['quantity'] > 1) {
  935. $plural = "s";
  936. }
  937. $product_list.= " - ".$row['quantity']." ". $product_data[0]['name'].$variation_list ." ". $message_price ."\n";
  938. if ($shipping > 0) $product_list .= " - ". __('Shipping', 'wpsc').":".$shipping_price ."\n\r";
  939. $product_list_html.= " - ".$row['quantity']." ". $product_data[0]['name'].$variation_list ." ". $message_price ."\n";
  940. if ($shipping > 0) $product_list_html .= " - ". __('Shipping', 'wpsc').":".$shipping_price ."\n\r";
  941. }
  942. $report.= " - ". $product_data[0]['name'] .$variation_list." ".$message_price ."\n";
  943. }
  944. if($purchase_log['discount_data'] != '') {
  945. $coupon_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_COUPON_CODES."` WHERE coupon_code='".$wpdb->escape($purchase_log['discount_data'])."' LIMIT 1",ARRAY_A);
  946. if($coupon_data['use-once'] == 1) {
  947. $wpdb->query("UPDATE `".WPSC_TABLE_COUPON_CODES."` SET `active`='0', `is-used`='1' WHERE `id`='".$coupon_data['id']."' LIMIT 1");
  948. }
  949. }
  950. //$wpdb->query("UPDATE `".WPSC_TABLE_DOWNLOAD_STATUS."` SET `active`='1' WHERE `fileid`='".$product_data[0]['file']."' AND `purchid` = '".$purchase_log['id']."' LIMIT 1");
  951. $total_shipping += $purchase_log['base_shipping'];
  952. $total = (($total+$total_shipping) - $purchase_log['discount_value']);
  953. // $message.= "\n\r";
  954. $product_list.= "Your Purchase No.: ".$purchase_log['id']."\n\r";
  955. if($purchase_log['discount_value'] > 0) {
  956. $discount_email.= __('Discount', 'wpsc').": ".nzshpcrt_currency_display($purchase_log['discount_value'], 1, true)."\n\r";
  957. }
  958. $total_shipping_email.= __('Total Shipping', 'wpsc').": ".nzshpcrt_currency_display($total_shipping,1,true)."\n\r";
  959. $total_price_email.= __('Total', 'wpsc').": ".nzshpcrt_currency_display($total,1,true)."\n\r";
  960. $product_list_html.= "Your Purchase No.: ".$purchase_log['id']."\n\n\r";
  961. if($purchase_log['discount_value'] > 0) {
  962. $discount_html.= __('Discount', 'wpsc').": ".nzshpcrt_currency_display($purchase_log['discount_value'], 1, true)."\n\r";
  963. }
  964. $total_shipping_html.= __('Total Shipping', 'wpsc').": ".nzshpcrt_currency_display($total_shipping,1,true)."\n\r";
  965. $total_price_html.= __('Total', 'wpsc').": ".nzshpcrt_currency_display($total, 1,true)."\n\r";
  966. if(isset($_GET['ti'])) {
  967. $message.= "\n\r".__('Your Transaction ID', 'wpsc').": " . $_GET['ti'];
  968. $message_html.= "\n\r".__('Your Transaction ID', 'wpsc').": " . $_GET['ti'];
  969. $report.= "\n\r".__('Transaction ID', 'wpsc').": "