-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_lib.php
executable file
·349 lines (285 loc) · 10.1 KB
/
upload_lib.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* This class allows a user to upload and
* validate their files.
*
* @author John Ciacia <[email protected]>
* @version 1.0
* @copyright Copyright (c) 2007, John Ciacia
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class Upload {
/**
*@var string contains the name of the file to be uploaded.
*/
private $FileName;
/**
*@var string contains the temporary name of the file to be uploaded.
*/
private $TempFileName;
/**
*@var string contains directory where the files should be uploaded.
*/
private $UploadDirectory;
/**
*@var string contains an array of valid extensions which are allowed to be uploaded.
*/
private $ValidExtensions;
/**
*@var string contains a message which can be used for debugging.
*/
private $Message;
/**
*@var integer contains maximum size of fiels to be uploaded in bytes.
*/
private $MaximumFileSize;
/**
*@var bool contains whether or not the files being uploaded are images.
*/
private $IsImage;
/**
*@var integer contains maximum width of images to be uploaded.
*/
private $MaximumWidth;
/**
*@var integer contains maximum height of images to be uploaded.
*/
private $MaximumHeight;
private $FitSize;
public function __construct($file,
$directory,
$filename,
$validExt=null,
$maxSize=null,
$maxW=null,
$maxH=null,
$fit=null){
$this->FileName=$filename;
$this->TempFileName=$file;
$this->UploadDirectory=$directory;
$this->MaximumWidth=0;
$this->MaximumHeight=0;
if(isset($validExt)) $this->ValidExtensions=$validExt;
if(isset($maxSize)) $this->MaximumFileSize=$maxSize;
if(isset($maxW)) $this->MaximumWidth=$maxW;
if(isset($maxH)) $this->MaximumHeight=$maxH;
$this->FitSize=false;
if(isset($fit)) $this->FitSize=true;
}
/**
*@method bool ValidateExtension() returns whether the extension of file to be uploaded
* is allowable or not.
*@return true the extension is valid.
*@return false the extension is invalid.
*/
private function ValidateExtension(){
$FileName = trim($this->FileName);
$FileParts = pathinfo($FileName);
$Extension = strtolower($FileParts['extension']);
$ValidExtensions = $this->ValidExtensions;
if (!$FileName) {
$this->SetMessage("ERROR: File name is empty.");
return false;
}
if (!$ValidExtensions) {
$this->SetMessage("WARNING: All extensions are valid.");
return true;
}
if (in_array($Extension, $ValidExtensions)) {
$this->SetMessage("MESSAGE: The extension '$Extension' appears to be valid.");
return true;
} else {
$this->SetMessage("Error: The extension '$Extension' is invalid.");
return false;
}
}
/**
*@method bool ValidateSize() returns whether the file size is acceptable.
*@return true the size is smaller than the alloted value.
*@return false the size is larger than the alloted value.
*/
private function ValidateSize(){
$MaximumFileSize = $this->MaximumFileSize;
$TempFileName = $this->GetTempName();
$TempFileSize = filesize($TempFileName);
if($MaximumFileSize == "") {
$this->SetMessage("WARNING: There is no size restriction.");
return true;
}
if ($MaximumFileSize <= $TempFileSize) {
$this->SetMessage("ERROR: The file is too big. It must be less than $MaximumFileSize and it is $TempFileSize.");
return false;
}
$this->SetMessage("Message: The file size is less than the MaximumFileSize.");
return true;
}
/**
*@method bool ValidateExistance() determins whether the file already exists. If so, rename $FileName.
*@return true can never be returned as all file names must be unique.
*@return false the file name does not exist.
*/
private function ValidateExistance(){
$FileName = $this->FileName;
$UploadDirectory = $this->UploadDirectory;
$File = $UploadDirectory . $FileName;
if (file_exists($File)) {
$this->SetMessage("Message: The file '$FileName' already exist.");
$UniqueName = rand() . $FileName;
$this->SetFileName($UniqueName);
$this->ValidateExistance();
} else {
$this->SetMessage("Message: The file name '$FileName' does not exist.");
return false;
}
}
/**
*@method bool ValidateDirectory()
*@return true the UploadDirectory exists, writable, and has a traling slash.
*@return false the directory was never set, does not exist, or is not writable.
*/
private function ValidateDirectory(){
$UploadDirectory = $this->UploadDirectory;
if (!$UploadDirectory) {
$this->SetMessage("ERROR: The directory variable is empty.");
return false;
}
if (!is_dir($UploadDirectory)) {
$this->SetMessage("ERROR: The directory '$UploadDirectory' does not exist.");
return false;
}
if (!is_writable($UploadDirectory)) {
$this->SetMessage("ERROR: The directory '$UploadDirectory' does not writable.");
return false;
}
if (substr($UploadDirectory, -1) != "/") {
$this->SetMessage("ERROR: The traling slash does not exist.");
$NewDirectory = $UploadDirectory . "/";
$this->SetUploadDirectory($NewDirectory);
$this->ValidateDirectory();
} else {
$this->SetMessage("MESSAGE: The traling slash exist.");
return true;
}
}
/**
*@method bool ValidateImage()
*@return true the image is smaller than the alloted dimensions.
*@return false the width and/or height is larger then the alloted dimensions.
*/
private function ValidateImage(){
$MaximumWidth = $this->MaximumWidth;
$MaximumHeight = $this->MaximumHeight;
$TempFileName = $this->TempFileName;
if($Size = @getimagesize($TempFileName)) {
$Width = $Size[0]; //$Width is the width in pixels of the image uploaded to the server.
$Height = $Size[1]; //$Height is the height in pixels of the image uploaded to the server.
if ($this->FitSize){
$xscale=$Width/$this->MaximumWidth;
$yscale=$Height/$this->MaximumHeight;
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($TempFileName);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $Width, $Height);
$this->TempFileName=$imageResized;
$this->SetMessage("Image Resized.");
return false;
}
if ($Width > $MaximumWidth && $MaximumWidth>0 ) {
$this->SetMessage("The width of the image [$Width] exceeds the maximum amount [$MaximumWidth].");
return false;
}
if ($Height > $MaximumHeight && $MaximumHeight>0) {
$this->SetMessage("The height of the image [$Height] exceeds the maximum amount [$MaximumHeight].");
return false;
}
$this->SetMessage("The image height [$Height] and width [$Width] are within their limitations.");
return true;
}
$this->SetMessage("Unable to get file Width and Height");
return true;
}
/**
*@method bool UploadFile() uploads the file to the server after passing all the validations.
*@return true the file was uploaded.
*@return false the upload failed.
*/
public function UploadFile(){
if (!$this->ValidateExtension()) {
die($this->GetMessage());
}
else if (!$this->ValidateSize()) {
die($this->GetMessage());
}
else if ($this->ValidateExistance()) {
die($this->GetMessage());
}
else if (!$this->ValidateDirectory()) {
die($this->GetMessage());
}
else if (!$this->ValidateImage()) {
die($this->GetMessage());
}
else {
$FileName = $this->FileName;
$TempFileName = $this->TempFileName;
$UploadDirectory = $this->UploadDirectory;
if (is_uploaded_file($TempFileName)) {
move_uploaded_file($TempFileName, $UploadDirectory . $FileName);
return true;
} else {
return false;
}
}
}
#Accessors and Mutators beyond this point.
#Siginificant documentation is not needed.
private function SetMessage($argv){
$this->Message=$argv;
}
private function SetFileName($argv){
$this->FileName=$argv;
}
function GetFileName()
{
return $this->FileName;
}
function GetUploadDirectory()
{
return $this->UploadDirectory;
}
function GetTempName()
{
return $this->TempFileName;
}
function GetValidExtensions()
{
return $this->ValidExtensions;
}
function GetMessage()
{
if (!isset($this->Message)) {
$this->SetMessage("No Message");
}
return $this->Message;
}
function GetMaximumFileSize()
{
return $this->MaximumFileSize;
}
function GetMaximumWidth()
{
return $this->MaximumWidth;
}
function GetMaximumHeight()
{
return $this->MaximumHeight;
}
}
?>