function old_wpsc_item_process_file($mode = 'add') { global $wpdb; $files = $wpdb->get_results("SELECT * FROM " . WPSC_TABLE_PRODUCT_FILES . " ORDER BY id ASC", ARRAY_A); if (is_array($files)) { foreach ($files as $file) { $file_names[] = $file['filename']; $file_hashes[] = $file['idhash']; } } if (apply_filters('wpsc_filter_file', $_FILES['file']['tmp_name'])) { // initialise $idhash to null to prevent issues with undefined variables and error logs $idhash = null; switch ($mode) { case 'edit': /* if we are editing, grab the current file and ID hash */ $product_id = $_POST['prodid']; $fileid_data = $wpdb->get_results("SELECT `file` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id` = '{$product_id}' LIMIT 1", ARRAY_A); case 'add': default: /* if we are adding, make a new file row and get the ID of it */ $timestamp = time(); $query_results = $wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCT_FILES . "` ( `filename` , `mimetype` , `idhash` , `date` ) VALUES ( '', '', '', '{$timestamp}');"); $fileid = $wpdb->get_var("SELECT LAST_INSERT_ID() FROM `" . WPSC_TABLE_PRODUCT_FILES . "`"); break; } /* if there is no idhash, generate it */ if ($idhash == null) { $idhash = sha1($fileid); if ($idhash == '') { // if sha1 doesnt spit an error, but doesnt return anything either (it has done so on some servers) $idhash = md5($fileid); } } // if needed, we can add code here to stop hash doubleups in the unlikely event that they shoud occur $mimetype = wpsc_get_mimetype($_FILES['file']['tmp_name']); $filename = basename($_FILES['file']['name']); if (in_array($_FILES['file']['name'], (array) $file_names)) { $i = 0; $new_name = $_FILES['file']['name'] . ".old"; while (file_exists(WPSC_FILE_DIR . $new_name)) { $new_name = $_FILES['file']['name'] . ".old_" . $i; $i++; } $old_idhash_id = array_search($_FILES['file']['name'], (array) $file_names); $old_idhash = $file_hashes[$old_idhash_id]; while (!file_exists(WPSC_FILE_DIR . $old_idhash)) { unset($file_hashes[$old_idhash_id]); unset($file_names[$old_idhash_id]); $old_idhash_id = array_search($_FILES['file']['name'], (array) $file_names); $old_idhash = $file_hashes[$old_idhash_id]; } copy(WPSC_FILE_DIR . $old_idhash, WPSC_FILE_DIR . $new_name); unlink(WPSC_FILE_DIR . $old_idhash); } if (move_uploaded_file($_FILES['file']['tmp_name'], WPSC_FILE_DIR . $idhash)) { $stat = stat(dirname(WPSC_FILE_DIR . $idhash)); $perms = $stat['mode'] & 0666; @chmod(WPSC_FILE_DIR . $idhash, $perms); if (function_exists("make_mp3_preview")) { if (!isset($_FILES['preview_file']['tmp_name'])) { // 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 make_mp3_preview(WPSC_FILE_DIR . $idhash, WPSC_PREVIEW_DIR . $idhash . ".mp3"); $preview_filepath = WPSC_PREVIEW_DIR . $idhash . ".mp3"; } else { if (file_exists($_FILES['preview_file']['tmp_name'])) { $preview_filename = basename($_FILES['preview_file']['name']); $preview_mimetype = wpsc_get_mimetype($_FILES['preview_file']['tmp_name']); copy($_FILES['preview_file']['tmp_name'], WPSC_PREVIEW_DIR . $preview_filename); $preview_filepath = WPSC_PREVIEW_DIR . $preview_filename; $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_FILES . "` SET `preview` = '" . $wpdb->escape($preview_filename) . "', `preview_mimetype` = '" . $preview_mimetype . "' WHERE `id` = '{$fileid}' LIMIT 1"); } } $stat = stat(dirname($preview_filepath)); $perms = $stat['mode'] & 0666; @chmod($preview_filepath, $perms); } $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_FILES . "` SET `filename` = '" . $wpdb->escape($filename) . "', `mimetype` = '{$mimetype}', `idhash` = '{$idhash}' WHERE `id` = '{$fileid}' LIMIT 1"); } if ($mode == 'edit') { //if we are editing, update the file ID in the product row, this cannot be done for add because the row does not exist yet. $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `file` = '{$fileid}' WHERE `id` = '{$product_id}' LIMIT 1"); } return $fileid; } else { return false; } }
/** * wpsc_item_add_preview_file function * * @param integer product ID * @param array the preview file array from $_FILES */ function wpsc_item_add_preview_file($product_id, $preview_file) { global $wpdb; $current_file_id = $wpdb->get_var("SELECT `file` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id` = '{$product_id}' LIMIT 1"); $file_data = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PRODUCT_FILES . "` WHERE `id`='{$current_file_id}' LIMIT 1", ARRAY_A); if (apply_filters('wpsc_filter_file', $preview_file['tmp_name'])) { //echo "test?"; if (function_exists("make_mp3_preview")) { if ($mimetype == "audio/mpeg" && !isset($preview_file['tmp_name'])) { // 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 make_mp3_preview(WPSC_FILE_DIR . $idhash, WPSC_PREVIEW_DIR . $idhash . ".mp3"); $preview_filepath = WPSC_PREVIEW_DIR . $idhash . ".mp3"; } else { if (file_exists($preview_file['tmp_name'])) { $preview_filename = basename($preview_file['name']); $preview_mimetype = wpsc_get_mimetype($preview_file['tmp_name']); copy($preview_file['tmp_name'], WPSC_PREVIEW_DIR . $preview_filename); $preview_filepath = WPSC_PREVIEW_DIR . $preview_filename; $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_FILES . "` SET `preview` = '" . $wpdb->escape($preview_filename) . "', `preview_mimetype` = '" . $preview_mimetype . "' WHERE `id` = '{$file_data['id']}' LIMIT 1"); //exit("UPDATE `".WPSC_TABLE_PRODUCT_FILES."` SET `preview` = '".$wpdb->escape($preview_filename)."', `preview_mimetype` = '".$preview_mimetype."' WHERE `id` = '{$file_data['id']}' LIMIT 1"); } } $stat = stat(dirname($preview_filepath)); $perms = $stat['mode'] & 0666; @chmod($preview_filepath, $perms); } //exit("<pre>".print_r($preview_file,true)."</pre>"); return $fileid; } else { return $selected_files; } }