-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.qqUploadedFileXhr.php
261 lines (208 loc) · 7.98 KB
/
class.qqUploadedFileXhr.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Handle file uploads via XMLHttpRequest
*/
class qqUploadedFileXhr {
/**
* Save the file to the specified path
* @return boolean TRUE on success
*/
function save($path) {
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
if ($realSize != $this->getSize()){
return false;
}
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
return true;
}
function getName() {
return $_GET['qqfile'];
}
function getSize() {
if (isset($_SERVER["CONTENT_LENGTH"])){
return (int)$_SERVER["CONTENT_LENGTH"];
} else {
throw new Exception('Getting content length is not supported.');
}
}
}
/**
* Handle file uploads via regular form post (uses the $_FILES array)
*/
class qqUploadedFileForm {
/**
* Save the file to the specified path
* @return boolean TRUE on success
*/
function save($path) {
if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
return false;
}
return true;
}
function getName() {
return $_FILES['qqfile']['name'];
}
function getSize() {
return $_FILES['qqfile']['size'];
}
}
class qqFileUploader {
private $allowedExtensions = array();
private $sizeLimit = 10485760;
private $file;
function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
$allowedExtensions = array_map("strtolower", $allowedExtensions);
$this->allowedExtensions = $allowedExtensions;
$this->sizeLimit = $sizeLimit;
$this->checkServerSettings();
if (isset($_GET['qqfile'])) {
$this->file = new qqUploadedFileXhr();
} elseif (isset($_FILES['qqfile'])) {
$this->file = new qqUploadedFileForm();
} else {
$this->file = false;
}
}
private function checkServerSettings(){
$postSize = $this->toBytes(ini_get('post_max_size'));
$uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
$size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
die("{'status'=>'error','result'=>'increase post_max_size and upload_max_filesize to $size'}");
}
}
private function toBytes($str){
$val = trim($str);
$last = strtolower($str[strlen($str)-1]);
switch($last) {
case 'g': $val *= 1024;
case 'm': $val *= 1024;
case 'k': $val *= 1024;
}
return $val;
}
function handleUpload($uploadDirectory='', $replaceOldFile = FALSE){
$uploadFolders = wp_upload_dir();
$this->targetPath = $uploadFolders['basedir'].trailingslashit($uploadDirectory); // FOLDER IN FILESYSTEM
$this->targetURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$this->targetPath); // BROWSER URL
if(!is_dir($this->targetPath)){
@mkdir($this->targetPath,0755);
}
if (!is_writable($this->targetPath)){
return array(
'status' => 'error',
'result' => __("Server error. Upload directory isn't writable.",THEME)
);
}
if (!$this->file){
return array(
'status' => 'error',
'result' => "No files were uploaded."
);
}
$size = $this->file->getSize();
if ($size == 0) {
return array(
'status' => 'error',
'result' => "File is empty"
);
}
if ($size > $this->sizeLimit) {
return array(
'status' => 'error',
'result' => "File is too large"
);
}
$pathinfo = pathinfo($this->file->getName());
//$filename = $pathinfo['filename'];
$filename = md5(uniqid());
$ext = $pathinfo['extension'];
$this->dataFile = $filename.'.'.$ext;
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return array(
'status' => 'error',
'result' => 'File has an invalid extension, it should be one of '. $these . '.'
);
}
if(!$replaceOldFile){
/// don't overwrite previous files that were uploaded
while (file_exists($this->targetPath . $this->dataFile)) {
$filename .= rand(10, 99);
}
}
if ($this->file->save($this->targetPath . $this->dataFile)){
return $this->showTableView();
} else {
return array(
'status' => 'error',
'result' => 'Could not save uploaded file. The upload was cancelled, or a server error was encountered.'
);
}
}
function normalize($s) {
define('CR', "\r"); // carriage return; Mac
define('LF', "\n"); // line feed; Unix
define('CRLF', "\r\n"); // carriage return and line feed; Windows
define('BR', '<br />' . LF); // HTML Break
// Normalize line endings using Global
// Convert all line-endings to UNIX format
$s = str_replace(CRLF, LF, $s);
$s = str_replace(CR, LF, $s);
// Don't allow out-of-control blank lines
$s = preg_replace("/\n{2,}/", LF . LF, $s);
return $s;
}
function showTableView(){
$error=false;
if(!file_exists($this->targetPath.$this->dataFile)){$error=true;}
if(!$this->file->getSize()){$error=true;}
if($error){
return array(
'status' => 'error',
'result' => 'Could not read uploaded file.'
);
}
$addressData = file_get_contents($this->targetPath.$this->dataFile);
// $addressData = utf8_encode($this->normalize($addressData)); // convert to utf8 and set unix line breaks
$addressData = $this->normalize($addressData); // convert to utf8 and set unix line breaks
$entries=explode(chr(10),$addressData);
foreach($entries as &$row){
$row=explode(';',$row);
}
unset($row);
$GLOBALS['CMS']->saveAsJson($entries,$this->targetPath.str_replace('.csv','.json',$this->dataFile));
unlink($this->targetPath.$this->dataFile);
$html=array();
$i=0;
foreach($entries as &$row){
if($i==0){
foreach($row as &$cell){
$cell = __('column.'.$cell,THEME);// replace with translation from .mo file
}
unset($cell);
$html[]='<tr class="head"><th>'.implode('</th><th>',$row).'</th></tr>';
}else{
$html[]='<tr class="'.($i%2?'even':'odd').'"><td>'.implode('</td><td>',$row).'</td></tr>';
}
$i++;
}
unset($row);
$html = '<table class="data">'.implode('',$html).'</table>';
$html.= '<div id="showTableView">
<p>'.__('The information in this table will be imported to the database at the next stage. If there is no email address specified for a recipient, this individual recipient will not be added.',THEME).'</p>
<p>'.__('If the information in the table displayed here is incorrect, please click "cancel", amend your list, and begin the upload procedure again.',THEME).'</p>
<p>'.sprintf(__('Would you like to %1s or %2s?',THEME),
'<a href="#" onclick="FRP_MAILING.saveAddresses(\''.str_replace('.csv','',$this->dataFile).'\')">'.__('continue',THEME).'</a>',
'<a href="'.site_url().'">'.__('cancel',THEME).'</a>').'</p>
</div>';
return array('success'=>true, 'content' => $html);
}
}