enable-media-replace
Last commit date
emr-list.png
16 years ago
emr-preview.png
16 years ago
enable-media-replace-ru_RU.mo
16 years ago
enable-media-replace-ru_RU.po
16 years ago
enable-media-replace-sv_SE.mo
16 years ago
enable-media-replace-sv_SE.po
16 years ago
enable-media-replace.mo
16 years ago
enable-media-replace.php
16 years ago
enable-media-replace.po
16 years ago
popup.php
16 years ago
readme.txt
16 years ago
screenshot-1.png
17 years ago
screenshot-2.png
17 years ago
upload.php
16 years ago
upload.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | $wppath = str_replace("wp-content/plugins/enable-media-replace/upload.php", "", __FILE__); |
| 4 | |
| 5 | require_once($wppath . "wp-load.php"); |
| 6 | require_once($wppath . "wp-admin/admin.php"); |
| 7 | |
| 8 | if (!current_user_can('upload_files')) |
| 9 | wp_die(__('You do not have permission to upload files.')); |
| 10 | |
| 11 | global $wpdb; |
| 12 | |
| 13 | // Define DB table names |
| 14 | $table_name = $wpdb->prefix . "posts"; |
| 15 | $postmeta_table_name = $wpdb->prefix . "postmeta"; |
| 16 | |
| 17 | // Get old guid and filetype from DB |
| 18 | $sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = {$_POST["ID"]}"; |
| 19 | list($current_filename, $current_filetype) = mysql_fetch_array(mysql_query($sql)); |
| 20 | |
| 21 | // Massage a bunch of vars |
| 22 | $current_guid = $current_filename; |
| 23 | $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1)); |
| 24 | |
| 25 | $current_file = get_attached_file($_POST["ID"], true); |
| 26 | $current_path = substr($current_file, 0, (strrpos($current_file, "/"))); |
| 27 | $current_file = str_replace("//", "/", $current_file); |
| 28 | $current_filename = basename($current_file); |
| 29 | |
| 30 | |
| 31 | $replace_type = $_POST["replace_type"]; |
| 32 | // We have two types: replace / replace_and_search |
| 33 | |
| 34 | if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) { |
| 35 | $new_filename = $_FILES["userfile"]["name"]; |
| 36 | $new_filetype = $_FILES["userfile"]["type"]; |
| 37 | $new_filesize = $_FILES["userfile"]["size"]; |
| 38 | |
| 39 | if ($replace_type == "replace") { |
| 40 | // Drop-in replace and we don't even care if you uploaded something that is the wrong file-type. |
| 41 | // That's your own fault, because we warned you! |
| 42 | |
| 43 | // Delete old file |
| 44 | unlink($current_file); |
| 45 | |
| 46 | // Move new file to old location/name |
| 47 | move_uploaded_file($_FILES["userfile"]["tmp_name"], $current_file); |
| 48 | |
| 49 | // Chmod new file to 644 |
| 50 | chmod($current_file, 0644); |
| 51 | |
| 52 | // Make thumb and/or update metadata |
| 53 | wp_update_attachment_metadata( $_POST["ID"], wp_generate_attachment_metadata( $_POST["ID"], $current_file ) ); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | else { |
| 58 | // Replace file, replace file name, update meta data, replace links pointing to old file name |
| 59 | |
| 60 | // Delete old file |
| 61 | unlink($current_file); |
| 62 | |
| 63 | // Massage new filename to adhere to WordPress standards |
| 64 | $new_filename= wp_unique_filename( $current_path, $new_filename ); |
| 65 | |
| 66 | // Move new file to old location, new name |
| 67 | $new_file = $current_path . "/" . $new_filename; |
| 68 | move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file); |
| 69 | |
| 70 | // Chmod new file to 644 |
| 71 | chmod($new_file, 0644); |
| 72 | |
| 73 | $new_filetitle = preg_replace('/\.[^.]+$/', '', basename($new_file)); |
| 74 | $new_guid = str_replace($current_filename, $new_filename, $current_guid); |
| 75 | |
| 76 | // Update database file name |
| 77 | mysql_query("UPDATE $table_name SET post_title = '$new_filetitle', post_name = '$new_filetitle', guid = '$new_guid', post_mime_type = '$new_filetype' WHERE ID = {$_POST["ID"]}"); |
| 78 | |
| 79 | // Update the postmeta file name |
| 80 | |
| 81 | // Get old postmeta _wp_attached_file |
| 82 | $sql = "SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = '{$_POST["ID"]}'"; |
| 83 | $old_meta_name = mysql_result(mysql_query($sql),0); |
| 84 | |
| 85 | // Make new postmeta _wp_attached_file |
| 86 | $new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name); |
| 87 | mysql_query("UPDATE $postmeta_table_name SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = '{$_POST["ID"]}'"); |
| 88 | |
| 89 | // Make thumb and/or update metadata |
| 90 | wp_update_attachment_metadata( $_POST["ID"], wp_generate_attachment_metadata( $_POST["ID"], $new_file) ); |
| 91 | |
| 92 | // Search-and-replace filename in post database |
| 93 | $sql = "SELECT ID, post_content FROM $table_name WHERE post_content LIKE '%$current_guid%'"; |
| 94 | $rs = mysql_query($sql); |
| 95 | |
| 96 | while($rows = mysql_fetch_assoc($rs)) { |
| 97 | |
| 98 | // replace old guid with new guid |
| 99 | $post_content = $rows["post_content"]; |
| 100 | $post_content = addslashes(str_replace($current_guid, $new_guid, $post_content)); |
| 101 | |
| 102 | mysql_query("UPDATE $table_name SET post_content = '$post_content' WHERE ID = {$rows["ID"]}"); |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | $returnurl = get_bloginfo("wpurl") . "/wp-admin/upload.php?posted=3"; |
| 110 | if (FORCE_SSL_ADMIN) { |
| 111 | $returnurl = str_replace("http:", "https:", $returnurl); |
| 112 | } |
| 113 | |
| 114 | header("Location: " . $returnurl); |
| 115 | |
| 116 | ?> |