-
Notifications
You must be signed in to change notification settings - Fork 36
/
fileupload.php
192 lines (156 loc) · 6.53 KB
/
fileupload.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
<?php
/**
* Handle file uploads via XMLHttpRequest
*/
include ("include.common.php");
include_once ('server.includes.inc.php');
/**
* 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['file']['tmp_name'], $path)){
return false;
}
return true;
}
function getName() {
return $_FILES['file']['name'];
}
function getSize() {
return $_FILES['file']['size'];
}
}
class qqFileUploader {
var $log = null;
private $allowedExtensions = array();
private $sizeLimit = 10485760;
//private $sizeLimit = 2485760;
private $file;
function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
$allowedExtensions = array_map("strtolower", $allowedExtensions);
$this->allowedExtensions = $allowedExtensions;
$this->sizeLimit = $sizeLimit;
$this->checkServerSettings();
$this->file = new qqUploadedFileForm();
}
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("{'error':'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;
}
/**
* Returns array('success'=>1) or array('error'=>'error message')
*/
function handleUpload($uploadDirectory,$saveFileName, $replaceOldFile = FALSE){
if (!is_writable($uploadDirectory)){
return array('success'=>0,'error' => "Server error. Upload directory isn't writable.");
}
if (!$this->file){
return array('success'=>0,'error' => 'No files were uploaded.');
}
$size = $this->file->getSize();
error_log('file size ='.$size);
error_log('file size limit ='.$this->sizeLimit);
if ($size == 0) {
return array('success'=>0,'error' => 'File is empty');
}
if ($size > $this->sizeLimit) {
return array('success'=>0,'error' => 'File is too large');
}
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = $pathinfo['extension'];
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return array('success'=>0,'error' => 'File has an invalid extension, it should be one of '. $these . '.');
}
//$filename .= microtime(true);
$filename = $saveFileName; // file with only name
$saveFileName = $saveFileName.'.'.strtolower($ext); // file with extention
$final_img_location = $uploadDirectory . $saveFileName;
if ($this->file->save($final_img_location)){
$arr = explode("/", $final_img_location);
return array('success'=>1,'filename'=>$arr[count($arr)-1],'error'=>'');
} else {
return array('success'=>0,'error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
}
}
//Generate File Name
$saveFileName = $_POST['file_name'];
$saveFileName = str_replace("..","",$saveFileName);
$saveFileName = str_replace("/","",$saveFileName);
if(stristr($saveFileName,".php")){
$saveFileName = str_replace(".php","",$saveFileName);
}
if(empty($saveFileName) || $saveFileName == "_NEW_"){
$saveFileName = microtime();
$saveFileName = str_replace(".", "-", $saveFileName);
}
$file = new File();
$file->Load("name = ?",array($saveFileName));
// list of valid extensions, ex. array("jpeg", "xml", "bmp")
$allowedExtensions = explode(',', "csv,doc,xls,docx,xlsx,txt,ppt,pptx,rtf,pdf,xml,jpg,bmp,gif,png,jpeg");
// max file size in bytes
$sizeLimit =MAX_FILE_SIZE_KB * 1024;
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload(CLIENT_BASE_PATH.'data/',$saveFileName);
// to pass data through iframe you will need to encode all html tags
$uploadFilesToS3 = $settingsManager->getSetting("Files: Upload Files to S3");
$uploadFilesToS3Key = $settingsManager->getSetting("Files: Amazon S3 Key for File Upload");
$uploadFilesToS3Secret = $settingsManager->getSetting("Files: Amazone S3 Secret for File Upload");
$s3Bucket = $settingsManager->getSetting("Files: S3 Bucket");
$s3WebUrl = $settingsManager->getSetting("Files: S3 Web Url");
$uploadedToS3 = false;
error_log($uploadFilesToS3."|".$uploadFilesToS3Key."|".$uploadFilesToS3Secret."|".$s3Bucket."|".$s3WebUrl."|".CLIENT_NAME);
if($uploadFilesToS3.'' == '1' && !empty($uploadFilesToS3Key) && !empty($uploadFilesToS3Secret) &&
!empty($s3Bucket) && !empty($s3WebUrl)){
$localFile = CLIENT_BASE_PATH.'data/'.$result['filename'];
$f_size = filesize($localFile);
$uploadname = CLIENT_NAME."/".$result['filename'];
error_log("Upload file to s3:".$uploadname);
error_log("Local file:".$localFile);
error_log("Local file size:".$f_size);
$s3FileSys = new S3FileSystem($uploadFilesToS3Key, $uploadFilesToS3Secret);
$res = $s3FileSys->putObject($s3Bucket, $uploadname, $localFile, 'authenticated-read');
$file_url = $s3WebUrl.$uploadname;
$file_url = $s3FileSys->generateExpiringURL($file_url);
error_log("Response from s3 file sys:".print_r($res,true));
unlink($localFile);
$uploadedToS3 = true;
}
if($result['success'] == 1){
$file->name = $saveFileName;
$file->filename = $result['filename'];
$file->employee = $_POST['user']=="_NONE_"?null:$_POST['user'];
$file->file_group = $_POST['file_group'];
$file->Save();
if($uploadedToS3){
$result['data'] = $file_url;
}else{
$result['data'] = CLIENT_BASE_URL.'data/'.$result['filename'];
}
$result['data'] .= "|".$saveFileName;
$result['data'] .= "|".$file->id;
}
echo "<script>parent.closeUploadDialog(".$result['success'].",'".$result['error']."','".$result['data']."');</script>";