| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
$response = array(); |
| 7 |
$file = $_FILES['file']; |
| 8 |
$tempExt = explode('.', $file['name']); |
| 9 |
$extension = strtolower($tempExt[count($tempExt)-1]); |
| 10 |
$isError = false; |
| 11 |
|
| 12 |
$wpsc_allow_attachment_type = get_option('wpsc_allow_attachment_type'); |
| 13 |
$wpsc_attachment_type = explode(',',$wpsc_allow_attachment_type); |
| 14 |
$wpsc_attachment_type = array_map('trim', $wpsc_attachment_type); |
| 15 |
$wpsc_attachment_type = array_map('strtolower', $wpsc_attachment_type); |
| 16 |
|
| 17 |
// Check if extension is allowed or not |
| 18 |
switch ($extension){ |
| 19 |
case 'exe': |
| 20 |
case 'php': |
| 21 |
case 'js': |
| 22 |
$isError = true; |
| 23 |
break; |
| 24 |
} |
| 25 |
if ( preg_match('/php/i', $extension) || preg_match('/phtml/i', $extension) ){ |
| 26 |
$isError = true; |
| 27 |
} |
| 28 |
if (!(in_array($extension,$wpsc_attachment_type))) { |
| 29 |
$isError = true; |
| 30 |
} |
| 31 |
if ($isError) { |
| 32 |
$response = new WP_Error( |
| 33 |
'file_format_not_allowed', |
| 34 |
'Given file format is not allowed!', |
| 35 |
array( |
| 36 |
'status' => 403, |
| 37 |
) |
| 38 |
); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
// get next attachment term |
| 43 |
$attachment_count = get_option('wpsc_attachment_count'); |
| 44 |
if(!$attachment_count) $attachment_count = 1; |
| 45 |
$term = wp_insert_term( 'attachment_'.$attachment_count, 'wpsc_attachment' ); |
| 46 |
if(!$term || is_wp_error($term)) { |
| 47 |
$response = new WP_Error( |
| 48 |
'internal_server_error', |
| 49 |
'Taxonomy not inserted!', |
| 50 |
array( |
| 51 |
'status' => 500, |
| 52 |
) |
| 53 |
); |
| 54 |
return; |
| 55 |
} |
| 56 |
update_option('wpsc_attachment_count',++$attachment_count); |
| 57 |
|
| 58 |
$now = date("Y-m-d H:i:s"); |
| 59 |
$time = strtotime($now); |
| 60 |
$month = date("m",$time); |
| 61 |
$year = date("Y",$time); |
| 62 |
|
| 63 |
$upload_dir = wp_upload_dir(); |
| 64 |
if (!file_exists($upload_dir['basedir'] . '/wpsc/'.$year)) { |
| 65 |
mkdir($upload_dir['basedir'] . '/wpsc/'.$year, 0755, true); |
| 66 |
} |
| 67 |
if (!file_exists($upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month)) { |
| 68 |
mkdir($upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month, 0755, true); |
| 69 |
} |
| 70 |
|
| 71 |
add_term_meta ($term['term_id'], 'filename', $file['name']); |
| 72 |
|
| 73 |
$save_file_name = str_replace(' ','_',$file['name']); |
| 74 |
$save_file_name = str_replace(',','_',$file['name']); |
| 75 |
$save_file_name = explode('.', $save_file_name); |
| 76 |
|
| 77 |
$img_extensions = array('png','jpeg','jpg','bmp','pdf','PNG','JPEG','JPG','BMP','PDF'); |
| 78 |
if(!in_array($extension, $img_extensions)){ |
| 79 |
$extension = $extension.'.txt'; |
| 80 |
add_term_meta ($term['term_id'], 'is_image', '0'); |
| 81 |
} else { |
| 82 |
add_term_meta ($term['term_id'], 'is_image', '1'); |
| 83 |
} |
| 84 |
|
| 85 |
unset( $save_file_name[count($save_file_name)-1] ); |
| 86 |
|
| 87 |
$save_file_name = implode('-', $save_file_name); |
| 88 |
|
| 89 |
$save_file_name = time().'_'.preg_replace('/[^A-Za-z0-9\-]/', '', $save_file_name).'.'.$extension; |
| 90 |
|
| 91 |
$save_directory = $upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month.'/'.$save_file_name; |
| 92 |
|
| 93 |
move_uploaded_file( $file['tmp_name'], $save_directory ); |
| 94 |
|
| 95 |
add_term_meta ($term['term_id'], 'save_file_name', $save_file_name); |
| 96 |
add_term_meta ($term['term_id'], 'active', '0'); |
| 97 |
add_term_meta ($term['term_id'], 'time_uploaded', date("Y-m-d H:i:s")); |
| 98 |
add_term_meta ($term['term_id'], 'is_restructured', 1); |
| 99 |
|
| 100 |
$attachment_id = $term['term_id']; |
| 101 |
|
| 102 |
$response = array( |
| 103 |
'status' => 200, |
| 104 |
'attachment_id' => $attachment_id, |
| 105 |
); |
| 106 |
|