-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_upload.php
217 lines (205 loc) · 5.74 KB
/
bt_upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
// Function to handle attachments
function atta_upload( $time, $orig_file, $action = 'add' )
{
wp_get_current_user();
global $current_user, $wpdb;
// Change $file (string) to $file_arr (array), so it'll be easier to handle
$atta_post = $_FILES['atta'];
$file_arr = (empty($orig_file)) ? array() : unserialize($orig_file);
var_dump($_FILES);
var_dump($file_arr);
if ( $action == 'edit_save' )
{
foreach ( $file_arr as $key => $path )
{
if ( empty($path) )
{
continue;
} elseif ( isset($_POST['delete_atta'][$key]) && $_POST['delete_atta'][$key] == 'yes' ) {
// The user want to remove this attachment, so don't add it to $new_file_arr
continue;
} else {
$new_file_arr[] = $path;
}
}
}
foreach ( $atta_post['size'] as $key => $value )
{
if ( $value > 0 )
{
// Validate the uploaded file
if ( !is_uploaded_file($atta_post['tmp_name'][$key]) )
{
echo '<div class="error"><p><strong>錯誤:</strong>Bad monkey! no babana! ' . $key . '</div>';
$atta_ok = 0;
break;
}
if ( $atta_post['error'][$key] > 0 )
{
switch ( $atta_post['error'][$key] )
{
case 1:
echo '<div class="error"><p><strong>錯誤:</strong>檔案太大了!(upload_max_filesize)</div>';
$atta_ok = 11;
break;
case 2:
echo '<div class="error"><p><strong>錯誤:</strong>檔案太大了!(max_file_size)</div>';
$atta_ok = 12;
break;
case 3:
echo '<div class="error"><p><strong>錯誤:</strong>傳輸中斷,只上傳了一部分</div>';
$atta_ok = 13;
break;
case 4:
echo '<div class="error"><p><strong>錯誤:</strong>沒有上傳任何檔案</div>';
$atta_ok = 14;
break;
case 6:
echo '<div class="error"><p><strong>錯誤:</strong>未指定伺服器的暫存目錄</div>';
$atta_ok = 16;
break;
case 7:
echo '<div class="error"><p><strong>錯誤:</strong>無法寫入磁碟</div>';
$atta_ok = 17;
break;
}
}
// Check file type
$filepart = pathinfo($atta_post['name'][$key]);
$ext = array('csv', 'doc', 'xls', 'odt', 'ods', 'txt', 'pdf', 'jpg', 'png', 'gif');
if ( in_array($filepart['extension'], $ext) )
{
$upload_dir = WP_CONTENT_DIR . '/bt_uploads/';
if ( is_writable($upload_dir) )
{
// Get the time
$date = explode(" ", $time);
$date = $date[0];
list($year, $month, $day) = explode("-", $date);
// Check if the destination folder exists, if not, create it
// The structure is bt_uploads/<username>/<year>/<month>/<filename>
while ( !isset($u_exists) || $u_exists == 0 )
{
$ls_u = scandir($upload_dir);
foreach ( $ls_u as $u )
{
if ( $u == $current_user->user_login )
{
$u_exists = 1;
$upload_dir .= $u . '/';
break;
} else {
$u_exists = 0;
}
}
if ( $u_exists == 0 )
{
$dir = $upload_dir . $current_user->user_login;
mkdir($dir);
}
}
unset($u_exists);
while ( !isset($y_exists) || $y_exists == 0 )
{
$ls_y = scandir($upload_dir);
foreach ( $ls_y as $y )
{
if ( $y == $year )
{
$y_exists = 1;
$upload_dir .= $y . '/';
break;
} else {
$y_exists = 0;
}
}
if ( $y_exists == 0 )
{
$dir = $upload_dir . $year;
mkdir($dir);
}
}
unset($y_exists);
while ( !isset($m_exists) || $m_exists == 0 )
{
$ls_m = scandir($upload_dir);
foreach ( $ls_m as $m )
{
if ( $m == $month )
{
$m_exists = 1;
$upload_dir .= $m . '/';
break;
} else {
$m_exists = 0;
}
}
if ( $m_exists == 0 )
{
$dir = $upload_dir . $month;
mkdir($dir);
}
}
unset($m_exists);
// Check if the filename already exists in the folder
$i = 0;
$filename = $filepart['basename'];
$full_path = $filepart['dirname'] . $filepart['basename'];
if ( file_exists($full_path) )
{
// Rename if already exists
$filename = $filepart['filename'] . '_' . $i++ . '.' .$filepart['extension'];
}
$dest_file = $upload_dir . $filename;
$temp_file = $atta_post['tmp_name'][$key];
// Check for folder permission
if ( !is_writable($upload_dir) )
{
echo '<div class="error"><p><strong>錯誤:</strong>無法寫入子目錄</p></div>';
$atta_ok = 0;
break;
} else {
// Move the file
if ( !@move_uploaded_file($temp_file, $dest_file) )
{
echo '<div class="error"><p><strong>錯誤:</strong>無法移動檔案到子目錄</p></div>';
$atta_ok = 0;
break;
} else {
$atta_ok = 1;
$new_file_arr[] = "bt_uploads/$u/$y/$m/$filename";
}
}
} else {
echo '<div class="error"><p><strong>錯誤:</strong>無法寫入上傳目錄</p></div>';
$atta_ok = 0;
break;
}
} else {
echo '<div class="error"><p><strong>錯誤:</strong>不允許此檔案類型的附件</p></div>';
$atta_ok = 0;
break;
}
} elseif ( $value == 0 ) {
$atta_ok = 1;
} else {
$atta_ok = 0;
break;
}
}
if ( !isset($new_file_arr) ) $new_file_arr = '';
// Check if $file_arr only contains empty elements, if so, set it to empty to prevent overhead
if ( is_array($new_file_arr) && count(array_filter($new_file_arr)) != 0 )
{
// Serialize $file_arr so we can save it to the database later
$new_file_serialized = $wpdb->escape(serialize($new_file_arr));
} else {
$new_file_serialized = '';
}
var_dump($new_file_serialized);
//echo "<div class=\"error\"><p>$file</p></div>";
$reply = array('atta_ok' => $atta_ok, 'file' => $new_file_serialized);
return $reply;
}
?>