-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadVendorImage.php
248 lines (206 loc) · 7.38 KB
/
uploadVendorImage.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
<?php
/**
* upload.php
*
* Copyright 2013, Moxiecode Systems AB
* Released under GPL License.
*
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
*/
#!! IMPORTANT:
#!! this file is just an example, it doesn't incorporate any security checks and
#!! is not recommended to be used in production environment as it is. Be sure to
#!! revise it and customize to your needs.
// Make sure file is not cached (as it happens for example on iOS devices)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
/*
// Support CORS
header("Access-Control-Allow-Origin: *");
// other CORS headers if any...
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
exit; // finish preflight CORS requests here
}
*/
// 5 minutes execution time
@set_time_limit(5 * 60);
// Uncomment this one to fake upload time
// usleep(5000);
// Settings
$targetDir = urldecode($_REQUEST["folder"]);
$targetDir = trim($targetDir,"/");
$srcDir = $targetDir . DIRECTORY_SEPARATOR . "src";
$cleanupTargetDir = true; // Remove old files
$maxFileAge = 5 * 3600; // Temp file age in seconds
$permissions = 0777;
// Create target dir & SRC dir
if (!file_exists($targetDir)) {
@mkdir($targetDir,0777,true);
}
if (!file_exists($srcDir)) {
@mkdir($srcDir,0777,true);
}
// Get a file name
if (isset($_REQUEST["name"])) {
$fileName = $_REQUEST["name"];
} elseif (!empty($_FILES)) {
$fileName = $_FILES["file"]["name"];
} else {
$fileName = uniqid("file_");
}
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
// Chunking might be enabled
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
// Remove old temp files
if ($cleanupTargetDir) {
if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
$uploadResult = ['uploaded'=>false,'code'=>100,'message'=>'Failed to open temp directory.'];
die(json_encode($uploadResult));
//note that the suggested code below does not trigger the Error event in the code
//die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
}
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
// If temp file is current file proceed to the next
if ($tmpfilePath == "{$filePath}.part") {
continue;
}
// Remove temp file if it is older than the max age and is not the current file
if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
@unlink($tmpfilePath);
}
}
closedir($dir);
}
// Open temp file
if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
$uploadResult = ['uploaded'=>false,'code'=>102,'message'=>'Failed to open output stream.'];
die(json_encode($uploadResult));
//die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
}
if (!empty($_FILES)) {
if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
$uploadResult = ['uploaded'=>false,'code'=>103,'message'=>'Failed to move uploaded file.'];
die(json_encode($uploadResult));
//die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
}
// Read binary input stream and append it to temp file
if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
$uploadResult = ['uploaded'=>false,'code'=>101,'message'=>'Failed to open input stream.'];
die(json_encode($uploadResult));
//die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
}
} else {
if (!$in = @fopen("php://input", "rb")) {
$uploadResult = ['uploaded'=>false,'code'=>101,'message'=>'Failed to open input stream.'];
die(json_encode($uploadResult));
//die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
}
}
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
@fclose($out);
@fclose($in);
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
// Strip the temp .part suffix off
rename("{$filePath}.part", $filePath);
$extension = pathinfo($filePath);
$extension = $extension['extension'];
rename($filePath, $targetDir . DIRECTORY_SEPARATOR . $_REQUEST['fileName'].".".$extension);
$filePath = $targetDir . DIRECTORY_SEPARATOR . $_REQUEST['fileName'].".".$extension;
$storeOriginalFilePath = $srcDir . DIRECTORY_SEPARATOR. $_REQUEST['fileName'].".".$extension;
//copy the original to a SRC folder, check to see if exists first
$unlinkedNewFile = true;
if ((file_exists($storeOriginalFilePath)) && (!unlink($storeOriginalFilePath))){
$unlinkedNewFile = false;
}
if ($unlinkedNewFile)
copy($filePath,$storeOriginalFilePath);
//resize image
$maxSize = $_REQUEST['size'];
$resize = true;
$image_info = getimagesize($filePath);
$image_type = $image_info[2];
$srcImage = NULL;
$validFileType = true;
if( $image_type == IMAGETYPE_JPEG ) {
$srcImage = imagecreatefromjpeg($filePath);
} elseif( $image_type == IMAGETYPE_GIF ) {
$srcImage = imagecreatefromgif($filePath);
} elseif( $image_type == IMAGETYPE_PNG ) {
$srcImage = imagecreatefrompng($filePath);
} else
$validFileType = false;
if ($validFileType){
$orgWidth = imagesx($srcImage);
$orgHeight = imagesy($srcImage);
$newWidth = 0;
$newHeight = 0;
if ($orgWidth > $orgHeight)
{
$resize = ($orgWidth > $maxSize)?true:false;
$newWidth = $maxSize;
$newHeight = $newWidth*($orgHeight/$orgWidth);
}
else
{
$resize = ($orgHeight > $maxSize)?true:false;
$newHeight = $maxSize;
$newWidth = $newHeight*($orgWidth/$orgHeight);
}
$newImage = NULL;
if ($resize)
{
$newImage = imagecreatetruecolor($newWidth,$newHeight);
if($image_type == IMAGETYPE_GIF or $image_type == IMAGETYPE_PNG){
imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 0, 0, 0, 127));
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
}
imagecopyresampled($newImage, $srcImage, 0, 0, 0, 0, $newWidth,$newHeight,$orgWidth,$orgHeight);
// }
// else{
// $newImage = imagecreatetruecolor($orgWidth,$orgHeight);
// imagecopyresampled($newImage, $srcImage, 0, 0, 0, 0, $orgWidth,$orgHeight,$orgWidth,$orgHeight);
// $newWidth = $orgWidth;
// $newHeight = $orgHeight;
// }
imagedestroy($srcImage);
$unlinked = true;
if (!unlink($filePath))
$unlinked = false;
if ($unlinked)
{
if( $image_type == IMAGETYPE_JPEG ) {
//echo "jpeg";
imagejpeg($newImage,$filePath,75);
} elseif( $image_type == IMAGETYPE_GIF ) {
//echo "gif";
imagegif($newImage,$filePath);
} elseif( $image_type == IMAGETYPE_PNG ) {
//echo "png";
imagepng($newImage,$filePath);
}
if( $permissions != null) {
chmod($filePath,$permissions);
chmod($storeOriginalFilePath,$permissions);
}
}
imagedestroy($newImage);
}//end if resize
} //end if valid file type
}
include_once('deleteOldVendorImages.php');
deleteOldVendorImages($filePath);
// Return Success JSON-RPC response
$uploadResult = ['uploaded'=>true,'filePath'=>$filePath];
die(json_encode($uploadResult));
//die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
?>