1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ibonly\PotatoORM; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class allows a user to upload and |
7
|
|
|
* validate their files. |
8
|
|
|
* |
9
|
|
|
* @author John Ciacia <[email protected]> |
10
|
|
|
* @version 1.0 |
11
|
|
|
* @copyright Copyright (c) 2007, John Ciacia |
12
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
13
|
|
|
*/ |
14
|
|
|
trait Upload { |
15
|
|
|
|
16
|
|
|
protected $file; |
17
|
|
|
/** |
18
|
|
|
*@var string contains an array of valid extensions which are allowed to be uploaded. |
19
|
|
|
*/ |
20
|
|
|
protected $ValidExtensions; |
21
|
|
|
/** |
22
|
|
|
*@var string contains a message which can be used for debugging. |
23
|
|
|
*/ |
24
|
|
|
protected $Message; |
25
|
|
|
/** |
26
|
|
|
*@var integer contains maximum size of fiels to be uploaded in bytes. |
27
|
|
|
*/ |
28
|
|
|
protected $MaximumFileSize; |
29
|
|
|
/** |
30
|
|
|
*@var bool contains whether or not the files being uploaded are images. |
31
|
|
|
*/ |
32
|
|
|
protected $IsImage; |
33
|
|
|
/** |
34
|
|
|
*@var string contains the email address of the recipient of upload logs. |
35
|
|
|
*/ |
36
|
|
|
protected $Email; |
37
|
|
|
/** |
38
|
|
|
*@var integer contains maximum width of images to be uploaded. |
39
|
|
|
*/ |
40
|
|
|
protected $MaximumWidth; |
41
|
|
|
/** |
42
|
|
|
*@var integer contains maximum height of images to be uploaded. |
43
|
|
|
*/ |
44
|
|
|
protected $MaximumHeight; |
45
|
|
|
|
46
|
|
|
// public function upload() |
|
|
|
|
47
|
|
|
// { |
48
|
|
|
|
49
|
|
|
// } |
50
|
|
|
|
51
|
|
|
public function file($file) |
52
|
|
|
{ |
53
|
|
|
$this->output = $file; |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// public function fileSize() |
|
|
|
|
59
|
|
|
// { |
60
|
|
|
// return $this->output['size']; |
|
|
|
|
61
|
|
|
// } |
62
|
|
|
|
63
|
|
|
// public function fileTmp() |
|
|
|
|
64
|
|
|
// { |
65
|
|
|
// return $this->output['tmp_name']; |
|
|
|
|
66
|
|
|
// } |
67
|
|
|
|
68
|
|
|
// public function fileName() |
|
|
|
|
69
|
|
|
// { |
70
|
|
|
// return $this->output; |
|
|
|
|
71
|
|
|
// } |
72
|
|
|
|
73
|
|
|
// public function fileMove($destination, $fileName = NULL) |
|
|
|
|
74
|
|
|
// { |
75
|
|
|
// $file_name = ($fileName == null) ? $this->fileName() : $fileName; |
|
|
|
|
76
|
|
|
// if (move_uploaded_file($this->fileTmp(), $destination.'/'.$file_name)) { |
|
|
|
|
77
|
|
|
// return 333; |
78
|
|
|
// } else { |
|
|
|
|
79
|
|
|
// return 000; |
80
|
|
|
// } |
81
|
|
|
// } |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
*@method bool ValidateExtension() returns whether the extension of file to be uploaded |
85
|
|
|
* is allowable or not. |
86
|
|
|
*@return true the extension is valid. |
87
|
|
|
*@return false the extension is invalid. |
88
|
|
|
*/ |
89
|
|
|
public function ValidateExtension() |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
$FileName = trim($this->GetFileName()); |
93
|
|
|
$FileParts = pathinfo($FileName); |
94
|
|
|
$Extension = strtolower($FileParts['extension']); |
95
|
|
|
$ValidExtensions = $this->ValidExtensions; |
96
|
|
|
|
97
|
|
|
if (!$FileName) { |
98
|
|
|
$this->SetMessage("ERROR: File name is empty."); |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$ValidExtensions) { |
103
|
|
|
$this->SetMessage("WARNING: All extensions are valid."); |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (in_array($Extension, $ValidExtensions)) { |
108
|
|
|
$this->SetMessage("MESSAGE: The extension '$Extension' appears to be valid."); |
109
|
|
|
return true; |
110
|
|
|
} else { |
111
|
|
|
$this->SetMessage("Error: The extension '$Extension' is invalid."); |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
*@method bool ValidateSize() returns whether the file size is acceptable. |
118
|
|
|
*@return true the size is smaller than the alloted value. |
119
|
|
|
*@return false the size is larger than the alloted value. |
120
|
|
|
*/ |
121
|
|
|
public function ValidateSize() |
122
|
|
|
{ |
123
|
|
|
$MaximumFileSize = $this->MaximumFileSize; |
124
|
|
|
$TempFileName = $this->GetTempName(); |
125
|
|
|
$TempFileSize = filesize($TempFileName); |
126
|
|
|
|
127
|
|
|
if($MaximumFileSize == "") { |
128
|
|
|
$this->SetMessage("WARNING: There is no size restriction."); |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($MaximumFileSize >= $TempFileSize) { |
133
|
|
|
$this->SetMessage("ERROR: The file is too big. It must be less than $MaximumFileSize and it is $TempFileSize."); |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->SetMessage("Message: The file size is less than the MaximumFileSize."); |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
*@method bool ValidateExistance() determins whether the file already exists. If so, rename $FileName. |
143
|
|
|
*@return true can never be returned as all file names must be unique. |
144
|
|
|
*@return false the file name does not exist. |
145
|
|
|
*/ |
146
|
|
|
public function ValidateExistance($uploadDirectory) |
147
|
|
|
{ |
148
|
|
|
$FileName = $this->GetFileName(); |
149
|
|
|
$File = $uploadDirectory . $FileName; |
150
|
|
|
|
151
|
|
|
if (file_exists($File)) { |
152
|
|
|
$this->SetMessage("Message: The file '$FileName' already exist."); |
153
|
|
|
$UniqueName = rand() . $FileName; |
154
|
|
|
$this->SetFileName($UniqueName); |
|
|
|
|
155
|
|
|
$this->ValidateExistance($uploadDirectory); |
156
|
|
|
} else { |
157
|
|
|
$this->SetMessage("Message: The file name '$FileName' does not exist."); |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
*@method bool ValidateDirectory() |
164
|
|
|
*@return true the UploadDirectory exists, writable, and has a traling slash. |
165
|
|
|
*@return false the directory was never set, does not exist, or is not writable. |
166
|
|
|
*/ |
167
|
|
|
public function ValidateDirectory($uploadDirectory) |
168
|
|
|
{ |
169
|
|
|
if (! $uploadDirectory) { |
170
|
|
|
$this->SetMessage("ERROR: The directory variable is empty."); |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!is_dir($uploadDirectory)) { |
175
|
|
|
$this->SetMessage("ERROR: The directory '$uploadDirectory' does not exist."); |
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (!is_writable($uploadDirectory)) { |
180
|
|
|
$this->SetMessage("ERROR: The directory '$uploadDirectory' does not writable."); |
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if (substr($uploadDirectory, -1) != "/") { |
185
|
|
|
$this->SetMessage("ERROR: The traling slash does not exist."); |
186
|
|
|
$NewDirectory = $uploadDirectory . "/"; |
187
|
|
|
$this->SetUploadDirectory($NewDirectory); |
|
|
|
|
188
|
|
|
$this->ValidateDirectory($uploadDirectory); |
189
|
|
|
} else { |
190
|
|
|
$this->SetMessage("MESSAGE: The traling slash exist."); |
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
*@method bool ValidateImage() |
197
|
|
|
*@return true the image is smaller than the alloted dimensions. |
198
|
|
|
*@return false the width and/or height is larger then the alloted dimensions. |
199
|
|
|
*/ |
200
|
|
|
public function ValidateImage() { |
201
|
|
|
$MaximumWidth = $this->MaximumWidth; |
202
|
|
|
$MaximumHeight = $this->MaximumHeight; |
203
|
|
|
$TempFileName = $this->GetTempName(); |
204
|
|
|
|
205
|
|
|
if($Size = @getimagesize($TempFileName)) { |
206
|
|
|
$Width = $Size[0]; //$Width is the width in pixels of the image uploaded to the server. |
207
|
|
|
$Height = $Size[1]; //$Height is the height in pixels of the image uploaded to the server. |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($Width > $MaximumWidth) { |
211
|
|
|
$this->SetMessage("The width of the image [$Width] exceeds the maximum amount [$MaximumWidth]."); |
|
|
|
|
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if ($Height > $MaximumHeight) { |
216
|
|
|
$this->SetMessage("The height of the image [$Height] exceeds the maximum amount [$MaximumHeight]."); |
|
|
|
|
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->SetMessage("The image height [$Height] and width [$Width] are within their limitations."); |
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
*@method bool UploadFile() uploads the file to the server after passing all the validations. |
226
|
|
|
*@return true the file was uploaded. |
227
|
|
|
*@return false the upload failed. |
228
|
|
|
*/ |
229
|
|
|
public function uploadFile($uploadDirectory) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
|
232
|
|
|
// if (!$this->ValidateExtension()) { |
|
|
|
|
233
|
|
|
// die($this->GetMessage()); |
|
|
|
|
234
|
|
|
// } |
235
|
|
|
|
236
|
|
|
// else if (!$this->ValidateSize()) { |
|
|
|
|
237
|
|
|
// die($this->GetMessage()); |
|
|
|
|
238
|
|
|
// } |
239
|
|
|
|
240
|
|
|
// else if ($this->ValidateExistance($uploadDirectory)) { |
|
|
|
|
241
|
|
|
// die($this->GetMessage()); |
|
|
|
|
242
|
|
|
// } |
243
|
|
|
|
244
|
|
|
// elseif (!$this->ValidateDirectory($uploadDirectory)) { |
|
|
|
|
245
|
|
|
// die($this->GetMessage()); |
|
|
|
|
246
|
|
|
// } |
247
|
|
|
|
248
|
|
|
// else if ($this->IsImage && !$this->ValidateImage()) { |
|
|
|
|
249
|
|
|
// die($this->GetMessage()); |
|
|
|
|
250
|
|
|
// } |
251
|
|
|
|
252
|
|
|
// else { |
253
|
|
|
|
254
|
|
|
$FileName = time()."_".str_replace(' ', '_', $this->GetFileName()); |
255
|
|
|
$TempFileName = $this->GetTempName(); |
256
|
|
|
|
257
|
|
|
if (is_uploaded_file($TempFileName)) { |
258
|
|
|
move_uploaded_file($TempFileName, $_SERVER['DOCUMENT_ROOT'].'/'.$uploadDirectory.'/'.$FileName); |
259
|
|
|
return $FileName; |
260
|
|
|
} else { |
261
|
|
|
return 4444; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// } |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
#Accessors and Mutators beyond this point. |
269
|
|
|
#Siginificant documentation is not needed. |
270
|
|
|
public function SetValidExtensions($argv) |
271
|
|
|
{ |
272
|
|
|
$this->ValidExtensions = $argv; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function SetMessage($argv) |
276
|
|
|
{ |
277
|
|
|
$this->Message = $argv; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function SetMaximumFileSize($argv) |
281
|
|
|
{ |
282
|
|
|
$this->MaximumFileSize = $argv; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function SetEmail($argv) |
286
|
|
|
{ |
287
|
|
|
$this->Email = $argv; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function SetIsImage($argv) |
291
|
|
|
{ |
292
|
|
|
$this->IsImage = $argv; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function SetMaximumWidth($argv) |
296
|
|
|
{ |
297
|
|
|
$this->MaximumWidth = $argv; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function SetMaximumHeight($argv) |
301
|
|
|
{ |
302
|
|
|
$this->MaximumHeight = $argv; |
303
|
|
|
} |
304
|
|
|
public function GetFileName() |
305
|
|
|
{ |
306
|
|
|
return $this->output['name']; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function GetUploadDirectory() |
310
|
|
|
{ |
311
|
|
|
return $this->UploadDirectory; |
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function GetTempName() |
315
|
|
|
{ |
316
|
|
|
return $this->output['tmp_name']; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function GetValidExtensions() |
320
|
|
|
{ |
321
|
|
|
return $this->ValidExtensions; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function GetMessage() |
325
|
|
|
{ |
326
|
|
|
if (!isset($this->Message)) { |
327
|
|
|
$this->SetMessage("No Message"); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return $this->Message; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function GetMaximumFileSize() |
334
|
|
|
{ |
335
|
|
|
return $this->MaximumFileSize; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function GetIsImage() |
339
|
|
|
{ |
340
|
|
|
return $this->IsImage; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function GetMaximumWidth() |
344
|
|
|
{ |
345
|
|
|
return $this->MaximumWidth; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function GetMaximumHeight() |
349
|
|
|
{ |
350
|
|
|
return $this->MaximumHeight; |
351
|
|
|
} |
352
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.