| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
if ( |
| 6 |
check_ajax_referer('upload_file', '_ajax_nonce', false) != 1 |
| 7 |
) wp_send_json_error('Unauthorised request!', 400); |
| 8 |
|
| 9 |
global $wpdb; |
| 10 |
$isError = false; |
| 11 |
$errorMessege = ''; |
| 12 |
$attachment_id = 0; |
| 13 |
|
| 14 |
if(!$_FILES){ |
| 15 |
$isError=true; |
| 16 |
} |
| 17 |
$wpsc_allow_attachment_type = get_option('wpsc_allow_attachment_type'); |
| 18 |
$wpsc_attachment_type = explode(',',$wpsc_allow_attachment_type); |
| 19 |
$wpsc_attachment_type = array_map('trim', $wpsc_attachment_type); |
| 20 |
$wpsc_attachment_type = array_map('strtolower', $wpsc_attachment_type); |
| 21 |
|
| 22 |
$tempExtension = explode('.', $_FILES['file']['name']); |
| 23 |
$extension = strtolower($tempExtension[count($tempExtension)-1]); |
| 24 |
|
| 25 |
if( !$isError ){ |
| 26 |
|
| 27 |
switch ($extension){ |
| 28 |
case 'exe': |
| 29 |
case 'php': |
| 30 |
case 'js': |
| 31 |
$isError = true; |
| 32 |
$errorMessege = __('Error: file format not supported!','supportcandy'); |
| 33 |
break; |
| 34 |
} |
| 35 |
if ( preg_match('/php/i', $extension) || preg_match('/phtml/i', $extension) ){ |
| 36 |
$isError=true; |
| 37 |
$errorMessege=__('Error: file format not supported!','supportcandy'); |
| 38 |
} |
| 39 |
if (!(in_array($extension,$wpsc_attachment_type))) { |
| 40 |
$isError = true; |
| 41 |
$errorMessege = __('Error: file format not supported!','supportcandy'); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if( !$isError && $_FILES['file']['tmp_name']==''){ |
| 46 |
$isError = true; |
| 47 |
$errorMessege = __('Error: file size exceeded allowed limit!','supportcandy'); |
| 48 |
} |
| 49 |
|
| 50 |
if( !$isError ){ |
| 51 |
|
| 52 |
//conflict with wp rocket and sg optimizer cache plugins |
| 53 |
$attachment_count = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}options WHERE option_name='wpsc_attachment_count' "); |
| 54 |
//$attachment_count = get_option('wpsc_attachment_count'); |
| 55 |
if(!$attachment_count) $attachment_count = 1; |
| 56 |
|
| 57 |
$flag = true; |
| 58 |
do{ |
| 59 |
$term = wp_insert_term( 'attachment_'.$attachment_count, 'wpsc_attachment' ); |
| 60 |
if(!$term || is_wp_error($term)){ |
| 61 |
$attachment_count++; |
| 62 |
}else{ |
| 63 |
$flag = false; |
| 64 |
} |
| 65 |
}while($flag); |
| 66 |
|
| 67 |
update_option('wpsc_attachment_count',++$attachment_count); |
| 68 |
|
| 69 |
$now = date("Y-m-d H:i:s"); |
| 70 |
$time = strtotime($now); |
| 71 |
$month = date("m",$time); |
| 72 |
$year = date("Y",$time); |
| 73 |
|
| 74 |
$upload_dir = wp_upload_dir(); |
| 75 |
if (!file_exists($upload_dir['basedir'] . '/wpsc/'.$year)) { |
| 76 |
mkdir($upload_dir['basedir'] . '/wpsc/'.$year, 0755, true); |
| 77 |
} |
| 78 |
if (!file_exists($upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month)) { |
| 79 |
mkdir($upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month, 0755, true); |
| 80 |
} |
| 81 |
|
| 82 |
add_term_meta ($term['term_id'], 'filename', $_FILES['file']['name']); |
| 83 |
|
| 84 |
$save_file_name = str_replace(' ','_',$_FILES['file']['name']); |
| 85 |
$save_file_name = str_replace(',','_',$_FILES['file']['name']); |
| 86 |
$save_file_name = explode('.', $save_file_name); |
| 87 |
|
| 88 |
$img_extensions = array('png','jpeg','jpg','bmp','pdf','gif','PNG','JPEG','JPG','BMP','PDF','GIF'); |
| 89 |
$extension = $save_file_name[count($save_file_name)-1]; |
| 90 |
if(!in_array($extension, $img_extensions)){ |
| 91 |
$extension = $extension.'.txt'; |
| 92 |
add_term_meta ($term['term_id'], 'is_image', '0'); |
| 93 |
} else { |
| 94 |
add_term_meta ($term['term_id'], 'is_image', '1'); |
| 95 |
} |
| 96 |
|
| 97 |
unset( $save_file_name[count($save_file_name)-1] ); |
| 98 |
|
| 99 |
$save_file_name = implode('-', $save_file_name); |
| 100 |
|
| 101 |
$save_file_name = time().'_'.preg_replace('/[^A-Za-z0-9\-]/', '', $save_file_name).'.'.$extension; |
| 102 |
|
| 103 |
$save_directory = $upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month.'/'.$save_file_name; |
| 104 |
|
| 105 |
move_uploaded_file( $_FILES['file']['tmp_name'], $save_directory ); |
| 106 |
|
| 107 |
add_term_meta ($term['term_id'], 'save_file_name', $save_file_name); |
| 108 |
add_term_meta ($term['term_id'], 'active', '0'); |
| 109 |
add_term_meta ($term['term_id'], 'time_uploaded', $now); |
| 110 |
add_term_meta ($term['term_id'], 'is_restructured', 1); |
| 111 |
|
| 112 |
$attachment_id = $term['term_id']; |
| 113 |
$errorMessege = __('done','supportcandy'); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
$isError=($isError)?'yes':'no'; |
| 118 |
|
| 119 |
$response = array( |
| 120 |
'error' => $isError, |
| 121 |
'errorMessege' => $errorMessege, |
| 122 |
'id' => $attachment_id |
| 123 |
); |
| 124 |
|
| 125 |
echo json_encode($response); |