Completed
Push — master ( fa85af...17ec4b )
by Adeniyi
8s
created

src/Helper/Upload.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /**
17
     *@var string contains the name of the file to be uploaded.
18
     */
19
    protected $FileName;
20
    /**
21
     *@var string contains the temporary name of the file to be uploaded.
22
     */
23
    protected $TempFileName;
24
    /**
25
     *@var string contains directory where the files should be uploaded.
26
     */
27
    protected $UploadDirectory;
28
    /**
29
     *@var string contains an array of valid extensions which are allowed to be uploaded.
30
     */
31
    protected $ValidExtensions;
32
    /**
33
     *@var string contains a message which can be used for debugging.
34
     */
35
    protected $Message;
36
    /**
37
     *@var integer contains maximum size of fiels to be uploaded in bytes.
38
     */
39
    protected $MaximumFileSize;
40
    /**
41
     *@var bool contains whether or not the files being uploaded are images.
42
     */
43
    protected $IsImage;
44
    /**
45
     *@var string contains the email address of the recipient of upload logs.
46
     */
47
    protected $Email;
48
    /**
49
     *@var integer contains maximum width of images to be uploaded.
50
     */
51
    protected $MaximumWidth;
52
    /**
53
     *@var integer contains maximum height of images to be uploaded.
54
     */
55
    protected $MaximumHeight;
56
57
    public function upload()
58
    {
59
        return 123445;
60
    }
61
62
    /**
63
     *@method bool ValidateExtension() returns whether the extension of file to be uploaded
64
     *    is allowable or not.
65
     *@return true the extension is valid.
66
     *@return false the extension is invalid.
67
     */
68
    public function ValidateExtension()
69
    {
70
71
        $FileName = trim($this->FileName);
72
        $FileParts = pathinfo($FileName);
73
        $Extension = strtolower($FileParts['extension']);
74
        $ValidExtensions = $this->ValidExtensions;
75
76
        if (!$FileName) {
77
            $this->SetMessage("ERROR: File name is empty.");
78
            return false;
79
        }
80
81
        if (!$ValidExtensions) {
82
            $this->SetMessage("WARNING: All extensions are valid.");
83
            return true;
84
        }
85
86
        if (in_array($Extension, $ValidExtensions)) {
87
            $this->SetMessage("MESSAGE: The extension '$Extension' appears to be valid.");
88
            return true;
89
        } else {
90
            $this->SetMessage("Error: The extension '$Extension' is invalid.");
91
            return false;  
92
        }
93
94
    }
95
96
    /**
97
     *@method bool ValidateSize() returns whether the file size is acceptable.
98
     *@return true the size is smaller than the alloted value.
99
     *@return false the size is larger than the alloted value.
100
     */
101
    public function ValidateSize()
102
    {
103
        $MaximumFileSize = $this->MaximumFileSize;
104
        $TempFileName = $this->GetTempName();
105
        $TempFileSize = filesize($TempFileName);
106
107
        if($MaximumFileSize == "") {
108
            $this->SetMessage("WARNING: There is no size restriction.");
109
            return true;
110
        }
111
112
        if ($MaximumFileSize >= $TempFileSize) {
113
            $this->SetMessage("ERROR: The file is too big. It must be less than $MaximumFileSize and it is $TempFileSize.");
114
            return false;
115
        }
116
117
        $this->SetMessage("Message: The file size is less than the MaximumFileSize.");
118
        return true;
119
    }
120
121
    /**
122
     *@method bool ValidateExistance() determins whether the file already exists. If so, rename $FileName.
123
     *@return true can never be returned as all file names must be unique.
124
     *@return false the file name does not exist.
125
     */
126
    public function ValidateExistance()
127
    {
128
        $FileName = $this->FileName;
129
        $UploadDirectory = $this->UploadDirectory;
130
        $File = $UploadDirectory . $FileName;
131
132
        if (file_exists($File)) {
133
            $this->SetMessage("Message: The file '$FileName' already exist.");
134
            $UniqueName = rand() . $FileName;
135
            $this->SetFileName($UniqueName);
136
            $this->ValidateExistance();
137
        } else {
138
            $this->SetMessage("Message: The file name '$FileName' does not exist.");
139
            return false;
140
        }
141
    }
142
143
    /**
144
     *@method bool ValidateDirectory()
145
     *@return true the UploadDirectory exists, writable, and has a traling slash.
146
     *@return false the directory was never set, does not exist, or is not writable.
147
     */
148
    public function ValidateDirectory()
149
    {
150
        $UploadDirectory = $this->UploadDirectory;
151
152
        if (!$UploadDirectory) {
153
            $this->SetMessage("ERROR: The directory variable is empty.");
154
            return false;
155
        }
156
157
        if (!is_dir($UploadDirectory)) {
158
            $this->SetMessage("ERROR: The directory '$UploadDirectory' does not exist.");
159
            return false;
160
        }
161
162
        if (!is_writable($UploadDirectory)) {
163
            $this->SetMessage("ERROR: The directory '$UploadDirectory' does not writable.");
164
            return false;
165
        }
166
167
        if (substr($UploadDirectory, -1) != "/") {
168
            $this->SetMessage("ERROR: The traling slash does not exist.");
169
            $NewDirectory = $UploadDirectory . "/";
170
            $this->SetUploadDirectory($NewDirectory);
171
            $this->ValidateDirectory();
172
        } else {
173
            $this->SetMessage("MESSAGE: The traling slash exist.");
174
            return true;
175
        }
176
    }
177
178
    /**
179
     *@method bool ValidateImage()
180
     *@return true the image is smaller than the alloted dimensions.
181
     *@return false the width and/or height is larger then the alloted dimensions.
182
     */
183
    public function ValidateImage() {
184
        $MaximumWidth = $this->MaximumWidth;
185
        $MaximumHeight = $this->MaximumHeight;
186
        $TempFileName = $this->TempFileName;
187
188
    if($Size = @getimagesize($TempFileName)) {
189
        $Width = $Size[0];   //$Width is the width in pixels of the image uploaded to the server.
190
        $Height = $Size[1];  //$Height is the height in pixels of the image uploaded to the server.
191
    }
192
193
        if ($Width > $MaximumWidth) {
194
            $this->SetMessage("The width of the image [$Width] exceeds the maximum amount [$MaximumWidth].");
195
            return false;
196
        }
197
198
        if ($Height > $MaximumHeight) {
199
            $this->SetMessage("The height of the image [$Height] exceeds the maximum amount [$MaximumHeight].");
200
            return false;
201
        }
202
203
        $this->SetMessage("The image height [$Height] and width [$Width] are within their limitations.");     
204
        return true;
205
    }
206
207
    /**
208
     *@method bool UploadFile() uploads the file to the server after passing all the validations.
209
     *@return true the file was uploaded.
210
     *@return false the upload failed.
211
     */
212
    public function uploadFile()
213
    {
214
215
        if (!$this->ValidateExtension()) {
216
            die($this->GetMessage());
0 ignored issues
show
Coding Style Compatibility introduced by
The method uploadFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
217
        } 
218
219
        else if (!$this->ValidateSize()) {
220
            die($this->GetMessage());
0 ignored issues
show
Coding Style Compatibility introduced by
The method uploadFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
221
        }
222
223
        else if ($this->ValidateExistance()) {
224
            die($this->GetMessage());
0 ignored issues
show
Coding Style Compatibility introduced by
The method uploadFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
225
        }
226
227
        else if (!$this->ValidateDirectory()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->ValidateDirectory() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
228
            die($this->GetMessage());
0 ignored issues
show
Coding Style Compatibility introduced by
The method uploadFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
229
        }
230
231
        else if ($this->IsImage && !$this->ValidateImage()) {
232
            die($this->GetMessage());
0 ignored issues
show
Coding Style Compatibility introduced by
The method uploadFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
233
        }
234
235
        else {
236
237
            $FileName = $this->FileName;
238
            $TempFileName = $this->TempFileName;
239
            $UploadDirectory = $this->UploadDirectory;
240
241
            if (is_uploaded_file($TempFileName)) { 
242
                move_uploaded_file($TempFileName, $UploadDirectory . $FileName);
243
                return true;
244
            } else {
245
                return false;
246
            }
247
248
        }
249
250
    }
251
252
    #Accessors and Mutators beyond this point.
253
    #Siginificant documentation is not needed.
254
    public function SetFileName($argv)
255
    {
256
        $this->FileName = $argv;
257
    }
258
259
    public function SetUploadDirectory($argv)
260
    {
261
        $this->UploadDirectory = $argv;
262
    }
263
264
    public function SetTempName($argv)
265
    {
266
        $this->TempFileName = $argv;
267
    }
268
269
    public function SetValidExtensions($argv)
270
    {
271
        $this->ValidExtensions = $argv;
272
    }
273
274
    public function SetMessage($argv)
275
    {
276
        $this->Message = $argv;
277
    }
278
279
    public function SetMaximumFileSize($argv)
280
    {
281
        $this->MaximumFileSize = $argv;
282
    }
283
284
    public function SetEmail($argv)
285
    {
286
        $this->Email = $argv;
287
    }
288
   
289
    public function SetIsImage($argv)
290
    {
291
        $this->IsImage = $argv;
292
    }
293
294
    public function SetMaximumWidth($argv)
295
    {
296
        $this->MaximumWidth = $argv;
297
    }
298
299
    public function SetMaximumHeight($argv)
300
    {
301
        $this->MaximumHeight = $argv;
302
    }   
303
    public function GetFileName()
304
    {
305
        return $this->FileName;
306
    }
307
308
    public function GetUploadDirectory()
309
    {
310
        return $this->UploadDirectory;
311
    }
312
313
    public function GetTempName()
314
    {
315
        return $this->TempFileName;
316
    }
317
318
    public function GetValidExtensions()
319
    {
320
        return $this->ValidExtensions;
321
    }
322
323
    public function GetMessage()
324
    {
325
        if (!isset($this->Message)) {
326
            $this->SetMessage("No Message");
327
        }
328
329
        return $this->Message;
330
    }
331
332
    public function GetMaximumFileSize()
333
    {
334
        return $this->MaximumFileSize;
335
    }
336
337
    public function GetEmail()
338
    {
339
        return $this->Email;
340
    }
341
342
    public function GetIsImage()
343
    {
344
        return $this->IsImage;
345
    }
346
347
    public function GetMaximumWidth()
348
    {
349
        return $this->MaximumWidth;
350
    }
351
352
    public function GetMaximumHeight()
353
    {
354
        return $this->MaximumHeight;
355
    }
356
}