Passed
Pull Request — master (#2)
by Michael
07:08 queued 02:34
created

SonglistMediaUploader::setTargetFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
class SonglistMediaUploader
4
{
5
	var $mediaName;
6
	var $mediaType;
7
	var $mediaSize;
8
	var $mediaTmpName;
9
	var $mediaError;
10
11
	var $uploadDir = '';
12
13
	var $allowedMimeTypes = array();
14
	var $allowedExtensions = array();
15
16
	var $maxFileSize = 3299999999;
17
	var $maxWidth;
18
	var $maxHeight;
19
20
	var $targetFileName;
21
22
	var $prefix;
23
24
	var $errors = array();
25
26
	var $savedDestination;
27
28
	var $savedFileName;
29
30
	/**
31
	 * Constructor
32
	 * 
33
	 * @param   string  $uploadDir
34
	 * @param   array   $allowedMimeTypes
35
	 * @param   int	 $maxFileSize
36
	 * @param   int	 $maxWidth
37
	 * @param   int	 $maxHeight
38
	 * @param   int	 $cmodvalue
39
	 * @param   array   $allowedExtensions
40
	 **/
41
	function SonglistMediaUploader($uploadDir, $allowedMimeTypes, $maxFileSize, $maxWidth=null, $maxHeight=null, $allowedExtensions=null )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
	{
43
		if (is_array($allowedMimeTypes)) {
0 ignored issues
show
introduced by
The condition is_array($allowedMimeTypes) is always true.
Loading history...
44
			$this->allowedMimeTypes =& $allowedMimeTypes;
45
		}
46
		$this->uploadDir = $uploadDir.(substr($uploadDir, strlen($uploadDir)-1,1)!=DS?DS:'');
47
		if (is_dir($uploadDir)) {
48
			foreach(explode(DS, $uploadDir) as $folder) {
49
				$path .= DS . $folder;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $path seems to be never defined.
Loading history...
50
				mkdir($path, 0777);
51
			}
52
		}
53
		$this->maxFileSize = intval($maxFileSize);
54
		if(isset($maxWidth)) {
55
			$this->maxWidth = intval($maxWidth);
56
		}
57
		if(isset($maxHeight)) {
58
			$this->maxHeight = intval($maxHeight);
59
		}
60
		if( isset( $allowedExtensions ) && is_array( $allowedExtensions ) ) {
61
			$this->allowedExtensions =& $allowedExtensions ;
62
		}
63
	}
64
65
	/**
66
	 * Fetch the uploaded file
67
	 * 
68
	 * @param   string  $media_name Name of the file field
69
	 * @param   int	 $index	  Index of the file (if more than one uploaded under that name)
70
	 * @return  bool
71
	 **/
72
	function fetchMedia($index_name, $index = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
	{
74
	if (!isset($_FILES[$index_name])) {
75
		$this->setErrors('File not found');
76
		return false;
77
	} elseif (is_array($_FILES[$index_name]['name'][$index]) && isset($index)) {
78
			$this->mediaName = $_FILES[$index_name]['name'][$index];
79
			$this->mediaType = $_FILES[$index_name]['type'][$index];
80
			$this->mediaSize = $_FILES[$index_name]['size'][$index];
81
			$this->mediaTmpName = $_FILES[$index_name]['tmp_name'][$index];
82
			$this->mediaError = !empty($_FILES[$index_name]['error'][$index]) ? $_FILES[$index_name]['errir'][$index] : 0;
83
	} else {
84
			$this->mediaName = $_FILES[$index_name]['name'];
85
			$this->mediaType = $_FILES[$index_name]['type'];
86
			$this->mediaSize = $_FILES[$index_name]['size'];
87
			$this->mediaTmpName = $_FILES[$index_name]['tmp_name'];
88
			$this->mediaError = !empty($_FILES[$index_name]['error']) ? $_FILES[$index_name]['error'] : 0;
89
		}
90
		$this->errors = array();
91
		if (intval($this->mediaSize) < 0) {
92
			$this->setErrors('Invalid File Size');
93
			return false;
94
		}
95
		if ($this->mediaName == '') {
96
			$this->setErrors('Filename Is Empty');
97
			return false;
98
		}
99
		if ($this->mediaTmpName == 'none' || !is_uploaded_file($this->mediaTmpName) || $this->mediaSize == 0 ) {
100
			$this->setErrors('No file uploaded');
101
			return false;
102
		}
103
		if ($this->mediaError > 0) {
104
			$this->setErrors('Error occurred: Error #'.$this->mediaError);
105
			return false;
106
		}
107
		return true;
108
	}
109
110
	/**
111
	 * Set the target filename
112
	 * 
113
	 * @param   string  $value
114
	 **/
115
	function setTargetFileName($value){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
		$this->targetFileName = strval(trim($value));
117
	}
118
119
	/**
120
	 * Set the prefix
121
	 * 
122
	 * @param   string  $value
123
	 **/
124
	function setPrefix($value){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
		$this->prefix = strval(trim($value));
126
	}
127
128
	/**
129
	 * Get the uploaded filename
130
	 * 
131
	 * @return  string 
132
	 **/
133
	function getMediaName()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
	{
135
		return $this->mediaName;
136
	}
137
138
	/**
139
	 * Get the type of the uploaded file
140
	 * 
141
	 * @return  string 
142
	 **/
143
	function getMediaType()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
144
	{
145
		return $this->mediaType;
146
	}
147
148
	/**
149
	 * Get the size of the uploaded file
150
	 * 
151
	 * @return  int 
152
	 **/
153
	function getMediaSize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
154
	{
155
		return $this->mediaSize;
156
	}
157
158
	/**
159
	 * Get the temporary name that the uploaded file was stored under
160
	 * 
161
	 * @return  string 
162
	 **/
163
	function getMediaTmpName()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
164
	{
165
		return $this->mediaTmpName;
166
	}
167
168
	/**
169
	 * Get the saved filename
170
	 * 
171
	 * @return  string 
172
	 **/
173
	function getSavedFileName(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
174
		return $this->savedFileName;
175
	}
176
177
	/**
178
	 * Get the destination the file is saved to
179
	 * 
180
	 * @return  string
181
	 **/
182
	function getSavedDestination(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
		return $this->savedDestination;
184
	}
185
186
	/**
187
	 * Check the file and copy it to the destination
188
	 * 
189
	 * @return  bool
190
	 **/
191
	function upload($chmod = 0644)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
192
	{
193
		if ($this->uploadDir == '') {
194
			$this->setErrors('Upload directory not set');
195
			return false;
196
		}
197
		if (!is_dir($this->uploadDir)) {
198
			$this->setErrors('Failed opening directory: '.$this->uploadDir);
199
			return false;
200
		}
201
		if (!is_writeable($this->uploadDir)) {
202
			$this->setErrors('Failed opening directory with write permission: '.$this->uploadDir);
203
			return false;
204
		}
205
		if (!$this->checkMimeType()) {
206
			$this->setErrors('MIME type not allowed: '.$this->mediaType);
207
			return false;
208
		}
209
		if (!$this->checkExtension()) {
210
			$this->setErrors('Extension not allowed');
211
			return false;
212
		}
213
		if (!$this->checkMaxFileSize()) {
214
			$this->setErrors('File size too large: '.$this->mediaSize);
215
		}
216
		if (!$this->checkMaxWidth()) {
217
			$this->setErrors(sprintf('File width must be smaller than %u', $this->maxWidth));
218
		}
219
		if (!$this->checkMaxHeight()) {
220
			$this->setErrors(sprintf('File height must be smaller than %u', $this->maxHeight));
221
		}
222
		if (count($this->errors) > 0) {
223
			return false;
224
		}
225
		if (!$this->_copyFile($chmod)) {
226
			$this->setErrors('Failed uploading file: '.$this->mediaName);
227
			return false;
228
		}
229
		return true;
230
	}
231
232
	/**
233
	 * Copy the file to its destination
234
	 * 
235
	 * @return  bool 
236
	 **/
237
	function _copyFile($chmod)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
238
	{
239
		$matched = array();
240
		if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) {
241
			return false;
242
		}
243
		if (isset($this->targetFileName)) {
244
			$this->savedFileName = $this->targetFileName;
245
		} elseif (isset($this->prefix)) {
246
			$this->savedFileName = uniqid($this->prefix).'.'.strtolower($matched[1]);
247
		} else {
248
			$this->savedFileName = strtolower($this->mediaName);
249
		}
250
		$this->savedDestination = $this->uploadDir.'/'.$this->savedFileName;
251
		if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
252
			return false;
253
		}
254
		@chmod($this->savedDestination, $chmod);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

254
		/** @scrutinizer ignore-unhandled */ @chmod($this->savedDestination, $chmod);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
255
		return true;
256
	}
257
258
	/**
259
	 * Is the file the right size?
260
	 * 
261
	 * @return  bool 
262
	 **/
263
	function checkMaxFileSize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
264
	{
265
		if ($this->mediaSize > $this->maxFileSize) {
266
			return false;
267
		}
268
		return true;
269
	}
270
271
	/**
272
	 * Is the picture the right width?
273
	 * 
274
	 * @return  bool 
275
	 **/
276
	function checkMaxWidth()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
277
	{
278
		if (!isset($this->maxWidth)||$this->maxWidth<1) {
279
			return true;
280
		}
281
		if (false !== $dimension = getimagesize($this->mediaTmpName)) {
282
			if ($dimension[0] > $this->maxWidth) {
283
				return false;
284
			}
285
		} else {
286
			trigger_error(sprintf('Failed fetching image size of %s, skipping max width check..', $this->mediaTmpName), E_USER_WARNING);
287
		}
288
		return true;
289
	}
290
291
	/**
292
	 * Is the picture the right height?
293
	 * 
294
	 * @return  bool 
295
	 **/
296
	function checkMaxHeight()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
297
	{
298
		if (!isset($this->maxHeight)||$this->maxHeight<1) {
299
			return true;
300
		}
301
		if (false !== $dimension = getimagesize($this->mediaTmpName)) {
302
			if ($dimension[1] > $this->maxHeight) {
303
				return false;
304
			}
305
		} else {
306
			trigger_error(sprintf('Failed fetching image size of %s, skipping max height check..', $this->mediaTmpName), E_USER_WARNING);
307
		}
308
		return true;
309
	}
310
311
	/**
312
	 * Is the file the right Mime type 
313
	 * 
314
	 * (is there a right type of mime? ;-)
315
	 * 
316
	 * @return  bool
317
	 **/
318
	function checkMimeType()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
319
	{
320
		if (count($this->allowedMimeTypes) > 0 && !in_array($this->mediaType, $this->allowedMimeTypes)) {
321
			return false;
322
		} else {
323
			return true;
324
		}
325
	}
326
327
	/**
328
	 * Is the file the right extension
329
	 * 
330
	 * @return  bool
331
	 **/
332
	function checkExtension()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
333
	{
334
		$ext = substr( strrchr( $this->mediaName , '.' ) , 1 ) ;
335
		if( ! empty( $this->allowedExtensions ) && ! in_array( strtolower( $ext ) , $this->allowedExtensions ) ) {
336
			return false;
337
		} else {
338
			return true;
339
		}
340
	}
341
342
	/**
343
	 * Add an error
344
	 * 
345
	 * @param   string  $error
346
	 **/
347
	function setErrors($error)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
348
	{
349
		$this->errors[] = trim($error);
350
	}
351
352
	/**
353
	 * Get generated errors
354
	 * 
355
	 * @param	bool	$ashtml Format using HTML?
356
	 * 
357
	 * @return	array|string	Array of array messages OR HTML string
358
	 */
359
	function &getErrors($ashtml = true)
360
	{
361
		if (!$ashtml) {
362
			return $this->errors;
363
		} else {
364
			$ret = '';
365
			if (count($this->errors) > 0) {
366
				$ret = '<h4>Errors Returned While Uploading</h4>';
367
				foreach ($this->errors as $error) {
368
					$ret .= $error.'<br />';
369
				}
370
			}
371
			return $ret;
372
		}
373
	}
374
}
375
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...