PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/reservationtelco
PHP | 850 lines | 547 code | 110 blank | 193 comment | 155 complexity | 1d21ba54b9821bd251757d4fb898b3e3 MD5 | raw file
  1. <?php
  2. /**
  3. * WPSC Product modifying functions
  4. *
  5. * @package wp-e-commerce
  6. * @since 3.7
  7. */
  8. /**
  9. * Check the memory_limit and calculate a recommended memory size
  10. * inspired by nextGenGallery Code
  11. *
  12. * @return string message about recommended image size
  13. */
  14. function wpsc_check_memory_limit() {
  15. if ( (function_exists('memory_get_usage')) && (ini_get('memory_limit')) ) {
  16. // get memory limit
  17. $memory_limit = ini_get('memory_limit');
  18. if ($memory_limit != '')
  19. $memory_limit = substr($memory_limit, 0, -1) * 1024 * 1024;
  20. // calculate the free memory
  21. $freeMemory = $memory_limit - memory_get_usage();
  22. // build the test sizes
  23. $sizes = array();
  24. $sizes[] = array ( 'width' => 800, 'height' => 600 );
  25. $sizes[] = array ( 'width' => 1024, 'height' => 768 );
  26. $sizes[] = array ( 'width' => 1280, 'height' => 960 ); // 1MP
  27. $sizes[] = array ( 'width' => 1600, 'height' => 1200 ); // 2MP
  28. $sizes[] = array ( 'width' => 2016, 'height' => 1512 ); // 3MP
  29. $sizes[] = array ( 'width' => 2272, 'height' => 1704 ); // 4MP
  30. $sizes[] = array ( 'width' => 2560, 'height' => 1920 ); // 5MP
  31. // test the classic sizes
  32. foreach ($sizes as $size){
  33. // very, very rough estimation
  34. if ($freeMemory < round( $size['width'] * $size['height'] * 5.09 )) {
  35. $result = sprintf( __( 'Please refrain from uploading images larger than <strong>%d x %d</strong> pixels' ), $size['width'], $size['height']);
  36. return $result;
  37. }
  38. }
  39. }
  40. return;
  41. }
  42. function wpsc_get_max_upload_size(){
  43. // Get PHP Max Upload Size
  44. if(ini_get('upload_max_filesize')) $upload_max = ini_get('upload_max_filesize');
  45. else $upload_max = __('N/A', 'nggallery');
  46. return $upload_max;
  47. }
  48. /**
  49. * wpsc_admin_submit_product function
  50. *
  51. * @return nothing
  52. */
  53. function wpsc_admin_submit_product() {
  54. check_admin_referer('edit-product', 'wpsc-edit-product');
  55. $post_data = wpsc_sanitise_product_forms();
  56. if(isset($post_data['title']) && $post_data['title'] != '' && isset($post_data['category'])){
  57. $product_id = wpsc_insert_product($post_data, true);
  58. if($product_id > 0) {
  59. $sendback = add_query_arg('product_id', $product_id);
  60. }
  61. $sendback = add_query_arg('message', 1, $sendback);
  62. //exit('<pre>'.print_r($sendback,true).'</pre>');
  63. wp_redirect($sendback);
  64. } else {
  65. $_SESSION['product_error_messages'] = array();
  66. if($post_data['title'] == ''){
  67. $_SESSION['product_error_messages'][] = __('<strong>ERROR</strong>: Please enter a Product name.<br />');
  68. }
  69. if(!isset($post_data['category'])){
  70. $_SESSION['product_error_messages'][] = __('<strong>ERROR</strong>: Please enter a Product Category.<br />');
  71. }
  72. $_SESSION['wpsc_failed_product_post_data'] = $post_data;
  73. // exit('<pre>'.print_r($_SESSION['product_error_messages'], true).'</pre>');
  74. $sendback = add_query_arg('ErrMessage', 1);
  75. wp_redirect($sendback);
  76. }
  77. exit();
  78. }
  79. /**
  80. * wpsc_sanitise_product_forms function
  81. *
  82. * @return array - Sanitised product details
  83. */
  84. function wpsc_sanitise_product_forms($post_data = null) {
  85. if ( empty($post_data) ) {
  86. $post_data = &$_POST;
  87. }
  88. // $post_data['product_id'] = isset($post_data['product_id']) ? $post_data['product_id'] : '';
  89. $post_data['name'] = isset($post_data['title']) ? $post_data['title'] : '';
  90. $post_data['description'] = isset($post_data['content']) ? $post_data['content'] : '';
  91. $post_data['meta'] = isset($post_data['productmeta_values']) ? $post_data['productmeta_values'] : '';
  92. $post_data['edit_variation_values'] = $post_data['edit_var_val'];
  93. // cast to boolean to convert to true or false, then cast to integer to convert to 1 or 0
  94. $post_data['quantity_limited'] = (int)(bool)$post_data['quantity_limited'];
  95. $post_data['special'] = (int)(bool)$post_data['special'];
  96. $post_data['notax'] = (int)(bool)$post_data['notax'];
  97. $post_data['donation'] = (int)(bool)$post_data['donation'];
  98. $post_data['no_shipping'] = (int)(bool)$post_data['no_shipping'];
  99. $post_data['publish'] = (int)(bool)$post_data['publish'];
  100. $post_data['meta']['unpublish_oos'] = (int)(bool)$post_data['inform_when_oos'];
  101. $post_data['price'] = (float)$post_data['price'];
  102. if(is_numeric($post_data['special_price'])) {
  103. $post_data['special_price'] = (float)($post_data['price'] - $post_data['special_price']);
  104. } else {
  105. $post_data['special_price'] = 0;
  106. }
  107. // if special is unticked, wipe the special_price value
  108. // if($post_data['special'] !== 1) {
  109. // $post_data['special_price'] = 0;
  110. // }
  111. // if table_rate_price is unticked, wipe the table rate prices
  112. if($post_data['table_rate_price'] != 1) {
  113. $post_data['meta']['table_rate_price'] = null;
  114. }
  115. $post_data['files'] = $_FILES;
  116. //exit('<pre>'.print_r($post_data, true).'</pre><pre>'.print_r($_POST, true).'</pre>');
  117. //exit('<pre>'.print_r($post_data, true).'</pre>');
  118. return $post_data;
  119. }
  120. /**
  121. * wpsc_insert_product function
  122. *
  123. * @param unknown
  124. * @return unknown
  125. */
  126. // exit('Image height'.get_option('product_image_height'));
  127. function wpsc_insert_product($post_data, $wpsc_error = false) {
  128. global $wpdb;
  129. $adding = false;
  130. $update = false;
  131. if((int)$post_data['product_id'] > 0) {
  132. $product_id = absint($post_data['product_id']);
  133. $update = true;
  134. }
  135. $product_columns = array(
  136. 'name' => '',
  137. 'description' => '',
  138. 'additional_description' => '',
  139. 'price' => null,
  140. 'weight' => null,
  141. 'weight_unit' => '',
  142. 'pnp' => null,
  143. 'international_pnp' => null,
  144. 'file' => null,
  145. 'image' => '0',
  146. 'quantity_limited' => '',
  147. 'quantity' => null,
  148. 'special' => null,
  149. 'special_price' => null,
  150. 'display_frontpage' => null,
  151. 'notax' => null,
  152. 'publish' => null,
  153. 'active' => null,
  154. 'donation' => null,
  155. 'no_shipping' => null,
  156. 'thumbnail_image' => null,
  157. 'thumbnail_state' => null
  158. );
  159. foreach($product_columns as $column => $default) {
  160. if(isset($post_data[$column]) || ($post_data[$column] !== null) ) {
  161. $update_values[$column] = stripslashes($post_data[$column]);
  162. } else if(($update != true) && ($default !== null)) {
  163. $update_values[$column] = stripslashes($default);
  164. }
  165. }
  166. if($update === true) {
  167. $where = array( 'id' => $product_id );
  168. if ( false === $wpdb->update( WPSC_TABLE_PRODUCT_LIST, $update_values, $where ) ) {
  169. if ( $wpsc_error ) {
  170. return new WP_Error('db_update_error', __('Could not update product in the database'), $wpdb->last_error);
  171. } else {
  172. return false;
  173. }
  174. }
  175. } else {
  176. if ( false === $wpdb->insert( WPSC_TABLE_PRODUCT_LIST, $update_values ) ) {
  177. if ( $wp_error ) {
  178. return new WP_Error('db_insert_error', __('Could not insert product into the database'), $wpdb->last_error);
  179. } else {
  180. return 0;
  181. }
  182. }
  183. $adding = true;
  184. $product_id = (int) $wpdb->insert_id;
  185. }
  186. /* Add tidy url name */
  187. if($post_data['name'] != '') {
  188. $existing_name = get_product_meta($product_id, 'url_name');
  189. // strip slashes, trim whitespace, convert to lowercase
  190. $tidied_name = strtolower(trim(stripslashes($post_data['name'])));
  191. // convert " - " to "-", all other spaces to dashes, and remove all foward slashes.
  192. //$url_name = preg_replace(array("/(\s-\s)+/","/(\s)+/", "/(\/)+/"), array("-","-", ""), $tidied_name);
  193. $url_name = sanitize_title($tidied_name);
  194. // Select all similar names, using an escaped version of the URL name
  195. $similar_names = (array)$wpdb->get_col("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id` NOT IN('{$product_id}}') AND `meta_key` IN ('url_name') AND `meta_value` REGEXP '^(".$wpdb->escape(preg_quote($url_name))."){1}[[:digit:]]*$' ");
  196. // Check desired name is not taken
  197. if(array_search($url_name, $similar_names) !== false) {
  198. // If it is, try to add a number to the end, if that is taken, try the next highest number...
  199. $i = 0;
  200. do {
  201. $i++;
  202. } while(array_search(($url_name.$i), $similar_names) !== false);
  203. // Concatenate the first number found that wasn't taken
  204. $url_name .= $i;
  205. }
  206. // If our URL name is the same as the existing name, do othing more.
  207. if($existing_name != $url_name) {
  208. update_product_meta($product_id, 'url_name', $url_name);
  209. }
  210. }
  211. // if we succeed, we can do further editing
  212. // update the categories
  213. wpsc_update_category_associations($product_id, $post_data['category']);
  214. // and the tags
  215. wpsc_update_product_tags($product_id, $post_data['product_tags'], $post_data['wpsc_existing_tags']);
  216. // and the meta
  217. wpsc_update_product_meta($product_id, $post_data['meta']);
  218. // and the custom meta
  219. wpsc_update_custom_meta($product_id, $post_data);
  220. // and the images
  221. wpsc_update_product_images($product_id, $post_data);
  222. //and the alt currency
  223. foreach((array)$post_data['newCurrency'] as $key =>$value){
  224. wpsc_update_alt_product_currency($product_id, $value, $post_data['newCurrPrice'][$key]);
  225. }
  226. if($post_data['files']['file']['tmp_name'] != '') {
  227. wpsc_item_process_file($product_id, $post_data['files']['file']);
  228. } else {
  229. wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
  230. }
  231. //exit('<pre>'.print_r($post_data, true).'</pre>');
  232. if($post_data['files']['preview_file']['tmp_name'] != '') {
  233. wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
  234. }
  235. $variations_processor = new nzshpcrt_variations;
  236. if(($adding === true) && ($_POST['variations'] != null)) {
  237. foreach((array)$_POST['variations'] as $variation_id => $state) {
  238. $variation_id = (int)$variation_id;
  239. if($state == 1) {
  240. $variation_values = $variations_processor->falsepost_variation_values($variation_id);
  241. $variations_processor->add_to_existing_product($product_id,$variation_values);
  242. }
  243. }
  244. }
  245. if($post_data['edit_variation_values'] != null) {
  246. $variations_processor->edit_product_values($product_id,$post_data['edit_variation_values']);
  247. }
  248. if($post_data['edit_add_variation_values'] != null) {
  249. $variations_processor->edit_add_product_values($product_id,$post_data['edit_add_variation_values']);
  250. }
  251. if($post_data['variation_priceandstock'] != null) {
  252. $variations_processor->update_variation_values($product_id, $post_data['variation_priceandstock']);
  253. }
  254. do_action('wpsc_edit_product', $product_id);
  255. wpsc_ping();
  256. return $product_id;
  257. }
  258. function wpsc_update_alt_product_currency($product_id, $newCurrency, $newPrice){
  259. global $wpdb;
  260. $sql = "SELECT `isocode` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id`=".$newCurrency;
  261. $isocode = $wpdb->get_var($sql);
  262. //exit($sql);
  263. $newCurrency = 'currency['.$isocode.']';
  264. if(($newPrice != '') && ($newPrice > 0)){
  265. update_product_meta($product_id, $newCurrency, $newPrice, $prev_value = '');
  266. } else {
  267. delete_product_meta($product_id, $newCurrency);
  268. }
  269. //exit('<pre>'.print_r($newCurrency, true).'</pre>'.$newPrice);
  270. }
  271. /**
  272. * wpsc_update_categories function
  273. *
  274. * @param integer product ID
  275. * @param array submitted categories
  276. */
  277. function wpsc_update_category_associations($product_id, $categories = array()) {
  278. global $wpdb;
  279. $associated_categories = $wpdb->get_col($wpdb->prepare("SELECT `category_id` FROM `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` WHERE `product_id` IN('%s')", $product_id));
  280. $categories_to_add = array_diff((array)$categories, (array)$associated_categories);
  281. $categories_to_delete = array_diff((array)$associated_categories, (array)$categories);
  282. $insert_sections = array();
  283. foreach($categories_to_delete as $key => $category_to_delete) {
  284. $categories_to_delete[$key] = absint($category_to_delete);
  285. }
  286. //exit('<pre>'.print_r($categories_to_delete, true).'</pre>');
  287. foreach($categories_to_add as $category_id) {
  288. $insert_sections[] = $wpdb->prepare("( %d, %d)", $product_id, $category_id);
  289. }
  290. if(count($insert_sections)) {
  291. $wpdb->query("INSERT INTO `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` (`product_id`, `category_id`) VALUES ".implode(", ",$insert_sections)."");
  292. }
  293. foreach($categories_to_add as $category_id) {
  294. $check_existing = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_PRODUCT_ORDER."` WHERE `category_id` IN('$category_id') AND `order` IN('0') LIMIT 1;",ARRAY_A);
  295. if($wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PRODUCT_ORDER."` WHERE `category_id` IN('$category_id') AND `product_id` IN('$product_id') LIMIT 1")) {
  296. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_ORDER."` SET `order` = '0' WHERE `category_id` IN('$category_id') AND `product_id` IN('$product_id') LIMIT 1;");
  297. } else {
  298. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_ORDER."` (`category_id`, `product_id`, `order`) VALUES ('$category_id', '$product_id', 0)");
  299. }
  300. if($check_existing != null) {
  301. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_ORDER."` SET `order` = (`order` + 1) WHERE `category_id` IN('$category_id') AND `product_id` NOT IN('$product_id') AND `order` < '0'");
  302. }
  303. }
  304. if(count($categories_to_delete) > 0) {
  305. $wpdb->query("DELETE FROM`".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` WHERE `product_id` = {$product_id} AND `category_id` IN(".implode(",",$categories_to_delete).") LIMIT ".count($categories_to_delete)."");
  306. }
  307. }
  308. /**
  309. * wpsc_update_product_tags function
  310. *
  311. * @param integer product ID
  312. * @param string comma separated tags
  313. */
  314. function wpsc_update_product_tags($product_id, $product_tags, $existing_tags) {
  315. if(isset($existing_tags)){
  316. $tags = explode(',',$existing_tags);
  317. if(is_array($tags)){
  318. foreach((array)$tags as $tag){
  319. $tt = wp_insert_term((string)$tag, 'product_tag');
  320. }
  321. }
  322. }
  323. wp_set_object_terms($product_id, $tags, 'product_tag');
  324. if(isset($product_tags) && $product_tags != 'Add new tag') {
  325. $tags = explode(',',$product_tags);
  326. product_tag_init();
  327. if(is_array($tags)) {
  328. foreach((array)$tags as $tag){
  329. $tt = wp_insert_term((string)$tag, 'product_tag');
  330. }
  331. }
  332. wp_set_object_terms($product_id, $tags, 'product_tag');
  333. }
  334. }
  335. /**
  336. * wpsc_update_product_meta function
  337. *
  338. * @param integer product ID
  339. * @param string comma separated tags
  340. */
  341. function wpsc_update_product_meta($product_id, $product_meta) {
  342. if($product_meta != null) {
  343. foreach((array)$product_meta as $key => $value) {
  344. if(get_product_meta($product_id, $key) != false) {
  345. update_product_meta($product_id, $key, $value);
  346. } else {
  347. add_product_meta($product_id, $key, $value);
  348. }
  349. }
  350. }
  351. }
  352. /*
  353. /* Code to support Publish/No Publish (1bigidea)
  354. */
  355. /**
  356. * set status of publish conditions
  357. * @return
  358. * @param string $product_id
  359. * @param bool $status Publish State
  360. */
  361. function wpsc_set_publish_status($product_id, $state) {
  362. global $wpdb;
  363. $status = (int) ( $state ) ? 1 : 0; // Cast the Publish flag
  364. $result = $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `publish` = '{$status}' WHERE `id` = '{$product_id}'");
  365. }
  366. /**
  367. * Toggle publish status and update product record
  368. * @return bool Publish status
  369. * @param string $product_id
  370. */
  371. function wpsc_toggle_publish_status($product_id) {
  372. global $wpdb;
  373. $status = (int) ( wpsc_publish_status($product_id) ) ? 0 : 1; // Flip the Publish flag True <=> False
  374. $sql = "UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `publish` = '{$status}' WHERE `id` = '{$product_id}'";
  375. $result = $wpdb->query($sql);
  376. return $status;
  377. }
  378. /**
  379. * Returns publish status from product database
  380. * @return bool publish status
  381. * @param string $product_id
  382. */
  383. function wpsc_publish_status($product_id) {
  384. global $wpdb;
  385. $status = (bool)$wpdb->get_var("SELECT `publish` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '{$product_id}'");
  386. return $status;
  387. }
  388. /**
  389. * Called from javascript within product page to toggle publish status - AJAX
  390. * @return bool publish status
  391. */
  392. function wpsc_ajax_toggle_publish() {
  393. /**
  394. * @todo - Check Admin Referer
  395. * @todo - Check Permissions
  396. */
  397. $status = (wpsc_toggle_publish_status($_REQUEST['productid'])) ? ('true') : ('false');
  398. exit( $status );
  399. }
  400. //add_action('wp_ajax_wpsc_toggle_publish','wpsc_ajax_toggle_publish');
  401. /*
  402. /* END - Publish /No Publish functions
  403. */
  404. function wpsc_update_custom_meta($product_id, $post_data) {
  405. global $wpdb;
  406. if($post_data['new_custom_meta'] != null) {
  407. foreach((array)$post_data['new_custom_meta']['name'] as $key => $name) {
  408. $value = $post_data['new_custom_meta']['value'][(int)$key];
  409. if(($name != '') && ($value != '')) {
  410. add_product_meta($product_id, $name, $value, false, true);
  411. }
  412. }
  413. }
  414. if($post_data['custom_meta'] != null) {
  415. foreach((array)$post_data['custom_meta'] as $key => $values) {
  416. if(($values['name'] != '') && ($values['value'] != '')) {
  417. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET `meta_key` = '".$wpdb->escape($values['name'])."', `meta_value` = '".$wpdb->escape($values['value'])."' WHERE `id` IN ('".(int)$key."')LIMIT 1 ;");
  418. // echo "UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET `meta_key` = '".$wpdb->escape($values['name'])."', `meta_value` = '".$wpdb->escape($values['value'])."' WHERE `id` IN ('".(int)$key."') LIMIT 1 ;";
  419. //add_product_meta($_POST['prodid'], $values['name'], $values['value'], false, true);
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * wpsc_update_product_tags function
  426. *
  427. * @param integer product ID
  428. * @param array the post data
  429. */
  430. function wpsc_update_product_images($product_id, $post_data) {
  431. global $wpdb;
  432. $uploaded_images = array();
  433. // This segment is for associating the images uploaded using swfuploader when adding a product
  434. foreach((array)$post_data['gallery_image_id'] as $added_image) {
  435. if($added_image > 0) {
  436. $uploaded_images[] = absint($added_image);
  437. }
  438. }
  439. if(count($uploaded_images) > 0) {
  440. $uploaded_image_data = $wpdb->get_col("SELECT `id` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` WHERE `id` IN (".implode(', ', $uploaded_images).") AND `product_id` = '0'");
  441. if(count($uploaded_image_data) > 0) {
  442. $first_image = null;
  443. foreach($uploaded_image_data as $uploaded_image_id) {
  444. if($first_image === null) {
  445. $first_image = absint($uploaded_image_id);
  446. }
  447. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_IMAGES."` SET `product_id` = '$product_id' WHERE `id` = '{$uploaded_image_id}' LIMIT 1;");
  448. }
  449. $previous_image = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  450. if($previous_image == 0) {
  451. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `image` = '{$first_image}' WHERE `id`='{$product_id}' LIMIT 1");
  452. }
  453. wpsc_resize_image_thumbnail($product_id, 1);
  454. }
  455. }
  456. /* Handle new image uploads here */
  457. if($post_data['files']['image']['tmp_name'] != '') {
  458. $image = wpsc_item_process_image($product_id, $post_data['files']['image']['tmp_name'], str_replace(" ", "_", $post_data['files']['image']['name']), $post_data['width'], $post_data['height'], $post_data['image_resize']);
  459. $image_action = absint($post_data['image_resize']);
  460. $image_width = $post_data['width'];
  461. $image_height = $post_data['height'];
  462. } else {
  463. $image_action = absint($post_data['gallery_resize']);
  464. $image_width = $post_data['gallery_width'];
  465. $image_height = $post_data['gallery_height'];
  466. }
  467. // exit( "<pre>".print_r($image_action, true)."</pre>");
  468. wpsc_resize_image_thumbnail($product_id, $image_action, $image_width, $image_height);
  469. //exit( " <pre>".print_r($post_data, true)."</pre>");
  470. }
  471. /**
  472. * wpsc_resize_image_thumbnail function
  473. *
  474. * @param integer product ID
  475. * @param integer the action to perform on the image
  476. * @param integer the width of the thumbnail image
  477. * @param integer the height of the thumbnail image
  478. * @param array the custom image array from $_FILES
  479. */
  480. function wpsc_resize_image_thumbnail($product_id, $image_action= 0, $width = 0, $height = 0, $custom_image = null) {
  481. global $wpdb;
  482. $image_id = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '{$product_id}' LIMIT 1");
  483. $image = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` WHERE `id` = '{$image_id}' LIMIT 1");
  484. // check if there is an image that is supposed to be there.
  485. if($image != '') {
  486. if(is_numeric($image)){
  487. }
  488. // check that is really there
  489. if(file_exists(WPSC_IMAGE_DIR.$image)) {
  490. // if the width or height is less than 1, set the size to the default
  491. if((($width < 1) || ($height < 1)) && ($image_action == 2)) {
  492. $image_action = 1;
  493. }
  494. switch($image_action) {
  495. case 0:
  496. if(!file_exists(WPSC_THUMBNAIL_DIR.$image)) {
  497. copy(WPSC_IMAGE_DIR.$image, WPSC_THUMBNAIL_DIR.$image);
  498. }
  499. break;
  500. case 1:
  501. // if case 1, replace the provided size with the default size
  502. $height = get_option('product_image_height');
  503. $width = get_option('product_image_width');
  504. case 2:
  505. // if case 2, use the provided size
  506. $image_input = WPSC_IMAGE_DIR . $image;
  507. $image_output = WPSC_THUMBNAIL_DIR . $image;
  508. if($width < 1) {
  509. $width = 96;
  510. }
  511. if($height < 1) {
  512. $height = 96;
  513. }
  514. image_processing($image_input, $image_output, $width, $height);
  515. update_product_meta($product_id, 'thumbnail_width', $width);
  516. update_product_meta($product_id, 'thumbnail_height', $height);
  517. break;
  518. case 3:
  519. // replacing the thumbnail with a custom image is done here
  520. $uploaded_image = null;
  521. //exit($uploaded_image);
  522. if(file_exists($_FILES['gallery_thumbnailImage']['tmp_name'])) {
  523. $uploaded_image = $_FILES['gallery_thumbnailImage']['tmp_name'];
  524. } else if(file_exists($_FILES['thumbnailImage']['tmp_name'])) {
  525. $uploaded_image = $_FILES['thumbnailImage']['tmp_name'];
  526. }
  527. if($uploaded_image !== null) {
  528. $image = uniqid().$image;
  529. move_uploaded_file($uploaded_image, WPSC_THUMBNAIL_DIR.$image);
  530. //exit($uploaded_image);
  531. }
  532. break;
  533. }
  534. if(!file_exists(WPSC_IMAGE_DIR.$image)) {
  535. //$wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_IMAGES."` SET `thumbnail_state` = '$image_action' WHERE `id`='{$product_id}' LIMIT 1");
  536. if($image_action != 3){
  537. $sql = "INSERT INTO `".WPSC_TABLE_PRODUCT_IMAGES."` (`product_id`, `image`, `width`, `height`) VALUES ('{$product_id}', '{$image}', '{$width}', '{$height}' )";
  538. $wpdb->query($sql);
  539. $image_id = (int) $wpdb->insert_id;
  540. }
  541. }
  542. if($image_action != 3){
  543. $sql="UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `thumbnail_state` = '$image_action', `image` ='{$image_id}' WHERE `id`='{$product_id}' LIMIT 1";
  544. }else{
  545. $sql="UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `thumbnail_state` = '$image_action', `image` ='{$image_id}',`thumbnail_image`='{$image}' WHERE `id`='{$product_id}' LIMIT 1";
  546. }
  547. $wpdb->query($sql);
  548. } else {
  549. //if it is not, we need to unset the associated image
  550. //$wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `image` = '' WHERE `id`='{$product_id}' LIMIT 1");
  551. //$wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_IMAGES."` (`product_id`, `image`, `width`, `height`) VALUES ('{$product_id}', '{$image}', '{$width}', '{$height}' )");
  552. }
  553. }
  554. }
  555. /**
  556. * wpsc_upload_image_thumbnail function
  557. *
  558. * @param integer product ID
  559. * @param string comma separated tags
  560. */
  561. function wpsc_upload_image_thumbnail($product_id, $product_meta) {
  562. if(($_POST['image_resize'] == 3) && ($_FILES['thumbnailImage'] != null) && file_exists($_FILES['thumbnailImage']['tmp_name'])) {
  563. $imagefield='thumbnailImage';
  564. $image=image_processing($_FILES['thumbnailImage']['tmp_name'], WPSC_THUMBNAIL_DIR.$_FILES['thumbnailImage']['name'],null,null,$imagefield);
  565. $thumbnail_image = $image;
  566. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `thumbnail_image` = '".$thumbnail_image."' WHERE `id` = '".$image_data['id']."'");
  567. $stat = stat( dirname( (WPSC_THUMBNAIL_DIR.$image_data['image']) ));
  568. $perms = $stat['mode'] & 0000775;
  569. @ chmod( (WPSC_THUMBNAIL_DIR.$image_data['image']), $perms );
  570. }
  571. }
  572. /**
  573. * wpsc_item_process_file function
  574. *
  575. * @param integer product ID
  576. * @param array the file array from $_FILES
  577. * @param array the preview file array from $_FILES
  578. */
  579. function wpsc_item_process_file($product_id, $submitted_file, $preview_file = null) {
  580. global $wpdb;
  581. $preview_file = null; //break this, is done in a different function, now
  582. $files = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_PRODUCT_FILES." ORDER BY id ASC", ARRAY_A);
  583. if (is_array($files)){
  584. foreach($files as $file){
  585. $file_names[] = $file['filename'];
  586. $file_hashes[] = $file['idhash'];
  587. }
  588. }
  589. if(apply_filters( 'wpsc_filter_file', $submitted_file['tmp_name'] )) {
  590. // initialise $idhash to null to prevent issues with undefined variables and error logs
  591. $idhash = null;
  592. // $fileid_data = $wpdb->get_results("SELECT `file` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '$product_id' LIMIT 1",ARRAY_A);
  593. /* if we are adding, make a new file row and get the ID of it */
  594. $timestamp = time();
  595. $query_results = $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_FILES."` ( `filename` , `mimetype` , `idhash` , `date` ) VALUES ( '', '', '', '$timestamp');");
  596. $fileid = $wpdb->get_var("SELECT LAST_INSERT_ID() FROM `".WPSC_TABLE_PRODUCT_FILES."`");
  597. /* if there is no idhash, generate it */
  598. if($idhash == null) {
  599. $idhash = sha1($fileid);
  600. if($idhash == '') {
  601. // if sha1 doesnt spit an error, but doesnt return anything either (it has done so on some servers)
  602. $idhash = md5($fileid);
  603. }
  604. }
  605. // if needed, we can add code here to stop hash doubleups in the unlikely event that they shoud occur
  606. $mimetype = wpsc_get_mimetype($submitted_file['tmp_name']);
  607. $filename = basename($submitted_file['name']);
  608. if (in_array($submitted_file['name'],(array)$file_names)){
  609. $i=0;
  610. $new_name = $submitted_file['name'].".old";
  611. while(file_exists(WPSC_FILE_DIR.$new_name)){
  612. $new_name = $submitted_file['name'].".old_".$i;
  613. $i++;
  614. }
  615. $old_idhash_id = array_search($submitted_file['name'],(array)$file_names);
  616. $old_idhash = $file_hashes[$old_idhash_id];
  617. while(!file_exists(WPSC_FILE_DIR.$old_idhash)){
  618. unset($file_hashes[$old_idhash_id]);
  619. unset($file_names[$old_idhash_id]);
  620. $old_idhash_id = array_search($submitted_file['name'],(array)$file_names);
  621. $old_idhash = $file_hashes[$old_idhash_id];
  622. }
  623. if(is_file(WPSC_FILE_DIR.$old_idhash)) {
  624. copy(WPSC_FILE_DIR.$old_idhash, WPSC_FILE_DIR.$new_name);
  625. unlink(WPSC_FILE_DIR.$old_idhash);
  626. }
  627. }
  628. if(move_uploaded_file($submitted_file['tmp_name'],(WPSC_FILE_DIR.$idhash))) {
  629. $stat = stat( dirname( (WPSC_FILE_DIR.$idhash) ));
  630. $perms = $stat['mode'] & 0000666;
  631. @ chmod( (WPSC_FILE_DIR.$idhash), $perms );
  632. if(function_exists("make_mp3_preview")) {
  633. if($mimetype == "audio/mpeg" && (!isset($preview_file['tmp_name']))) {
  634. // if we can generate a preview file, generate it (most can't due to sox being rare on servers and sox with MP3 support being even rarer), thus this needs to be enabled by editing code
  635. make_mp3_preview((WPSC_FILE_DIR.$idhash), (WPSC_PREVIEW_DIR.$idhash.".mp3"));
  636. $preview_filepath = (WPSC_PREVIEW_DIR.$idhash.".mp3");
  637. } else if(file_exists($preview_file['tmp_name'])) {
  638. $preview_filename = basename($preview_file['name']);
  639. $preview_mimetype = wpsc_get_mimetype($preview_file['tmp_name']);
  640. copy($preview_file['tmp_name'], (WPSC_PREVIEW_DIR.$preview_filename));
  641. $preview_filepath = (WPSC_PREVIEW_DIR.$preview_filename);
  642. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_FILES."` SET `preview` = '".$wpdb->escape($preview_filename)."', `preview_mimetype` = '".$preview_mimetype."' WHERE `id` = '$fileid' LIMIT 1");
  643. }
  644. $stat = stat( dirname($preview_filepath));
  645. $perms = $stat['mode'] & 0000666;
  646. @ chmod( $preview_filepath, $perms );
  647. }
  648. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_FILES."` SET `product_id` = '{$product_id}', `filename` = '".$wpdb->escape($filename)."', `mimetype` = '$mimetype', `idhash` = '$idhash' WHERE `id` = '$fileid' LIMIT 1");
  649. }
  650. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `file` = '$fileid' WHERE `id` = '$product_id' LIMIT 1");
  651. return $fileid;
  652. } else {
  653. return false;
  654. }
  655. }
  656. /**
  657. * wpsc_item_reassign_file function
  658. *
  659. * @param integer product ID
  660. * @param string the selected file name;
  661. */
  662. function wpsc_item_reassign_file($product_id, $selected_files) {
  663. global $wpdb;
  664. $product_file_list=array();
  665. // initialise $idhash to null to prevent issues with undefined variables and error logs
  666. $idhash = null;
  667. /* if we are editing, grab the current file and ID hash */
  668. if(!$selected_files) {
  669. // unlikely that anyone will ever upload a file called .none., so its the value used to signify clearing the product association
  670. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `file` = '0' WHERE `id` = '$product_id' LIMIT 1");
  671. return null;
  672. }
  673. foreach($selected_files as $selected_file) {
  674. // if we already use this file, there is no point doing anything more.
  675. $current_fileid = $wpdb->get_var("SELECT `file` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '$product_id' LIMIT 1");
  676. if($current_fileid > 0) {
  677. $current_file_data = $wpdb->get_row("SELECT `id`,`idhash` FROM `".WPSC_TABLE_PRODUCT_FILES."` WHERE `id` = '$current_fileid' LIMIT 1",ARRAY_A);
  678. if(basename($selected_file) == $file_data['idhash']) {
  679. //$product_file_list[] = $current_fileid;
  680. //return $current_fileid;
  681. }
  682. }
  683. $selected_file = basename($selected_file);
  684. if(file_exists(WPSC_FILE_DIR.$selected_file)) {
  685. $timestamp = time();
  686. $file_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PRODUCT_FILES."` WHERE `idhash` IN('".$wpdb->escape($selected_file)."') LIMIT 1", ARRAY_A);
  687. $fileid = (int)$file_data['id'];
  688. // if the file does not have a database row, add one.
  689. if($fileid < 1) {
  690. $mimetype = wpsc_get_mimetype(WPSC_FILE_DIR.$selected_file);
  691. $filename = $idhash = $selected_file;
  692. $timestamp = time();
  693. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCT_FILES."` (`product_id`, `filename` , `mimetype` , `idhash` , `date` ) VALUES ('{$product_id}', '{$filename}', '{$mimetype}', '{$idhash}', '{$timestamp}');");
  694. $fileid = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PRODUCT_FILES."` WHERE `date` = '{$timestamp}' AND `filename` IN ('{$filename}')");
  695. }
  696. // update the entry in the product table
  697. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_LIST."` SET `file` = '$fileid' WHERE `id` = '$product_id' LIMIT 1");
  698. $product_file_list[] = $fileid;
  699. }
  700. }
  701. //exit('<pre>'.print_r($product_file_list, true).'</pre>');
  702. update_product_meta($product_id, 'product_files', $product_file_list);
  703. return $fileid;
  704. }
  705. /**
  706. * wpsc_item_add_preview_file function
  707. *
  708. * @param integer product ID
  709. * @param array the preview file array from $_FILES
  710. */
  711. function wpsc_item_add_preview_file($product_id, $preview_file) {
  712. global $wpdb;
  713. $current_file_id = $wpdb->get_var("SELECT `file` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '$product_id' LIMIT 1");
  714. $file_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PRODUCT_FILES."` WHERE `id`='{$current_file_id}' LIMIT 1",ARRAY_A);
  715. if(apply_filters( 'wpsc_filter_file', $preview_file['tmp_name'] )) {
  716. //echo "test?";
  717. if(function_exists("make_mp3_preview")) {
  718. if($mimetype == "audio/mpeg" && (!isset($preview_file['tmp_name']))) {
  719. // if we can generate a preview file, generate it (most can't due to sox being rare on servers and sox with MP3 support being even rarer), thus this needs to be enabled by editing code
  720. make_mp3_preview((WPSC_FILE_DIR.$idhash), (WPSC_PREVIEW_DIR.$idhash.".mp3"));
  721. $preview_filepath = (WPSC_PREVIEW_DIR.$idhash.".mp3");
  722. } else if(file_exists($preview_file['tmp_name'])) {
  723. $preview_filename = basename($preview_file['name']);
  724. $preview_mimetype = wpsc_get_mimetype($preview_file['tmp_name']);
  725. copy($preview_file['tmp_name'], (WPSC_PREVIEW_DIR.$preview_filename));
  726. $preview_filepath = (WPSC_PREVIEW_DIR.$preview_filename);
  727. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCT_FILES."` SET `preview` = '".$wpdb->escape($preview_filename)."', `preview_mimetype` = '".$preview_mimetype."' WHERE `id` = '{$file_data['id']}' LIMIT 1");
  728. //exit("UPDATE `".WPSC_TABLE_PRODUCT_FILES."` SET `preview` = '".$wpdb->escape($preview_filename)."', `preview_mimetype` = '".$preview_mimetype."' WHERE `id` = '{$file_data['id']}' LIMIT 1");
  729. }
  730. $stat = stat( dirname($preview_filepath));
  731. $perms = $stat['mode'] & 0000666;
  732. @ chmod( $preview_filepath, $perms );
  733. }
  734. //exit("<pre>".print_r($preview_file,true)."</pre>");
  735. return $fileid;
  736. } else {
  737. return $selected_files;
  738. }
  739. }
  740. ?>