Passed
Branch master (f2d2e3)
by Michael
18:45
created

ImageManager::_dirs()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 27
rs 9.2222
1
<?php
2
/**
3
 * ImageManager, list images, directories, and thumbnails.
4
 * @author Wei Zhuo
5
 * @version $Id: ImageManager.php,v 1.4 2006/12/21 21:28:00 thierrybo Exp $
6
 * @package ImageManager
7
 */
8
9
require_once('Files.php');
10
require_once('Transform.php');
11
12
/**
13
 * ImageManager Class.
14
 * @author Wei Zhuo
15
 * @version $Id: ImageManager.php,v 1.4 2006/12/21 21:28:00 thierrybo Exp $
16
 */
17
class ImageManager 
18
{
19
	/**
20
	 * Configuration array.
21
	 */
22
	var $config;
23
24
	/**
25
	 * Array of directory information.
26
	 */
27
	var $dirs;
28
29
	/**
30
	 * Constructor. Create a new Image Manager instance.
31
	 * @param array $config configuration array, see config.inc.php
32
	 */
33
	function ImageManager($config) 
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...
34
	{
35
		$this->config = $config;
36
	}
37
38
	/**
39
	 * Get the base directory.
40
	 * @return string base dir, see config.inc.php
41
	 */
42
	function getBaseDir() 
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...
43
	{
44
		Return $this->config['base_dir'];
45
	}
46
47
	/**
48
	 * Get the base URL.
49
	 * @return string base url, see config.inc.php
50
	 */
51
	function getBaseURL() 
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...
52
	{
53
		Return $this->config['base_url'];
54
	}
55
56
	function isValidBase()
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...
57
	{
58
		return is_dir($this->getBaseDir());
0 ignored issues
show
Bug introduced by
The method getBaseDir() does not exist on ImageManager. ( Ignorable by Annotation )

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

58
		return is_dir($this->/** @scrutinizer ignore-call */ getBaseDir());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
	}
60
61
	/**
62
	 * Get the tmp file prefix.
63
     * @return string tmp file prefix.
64
	 */
65
	function getTmpPrefix() 
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...
66
	{
67
		Return $this->config['tmp_prefix'];
68
	}
69
70
	/**
71
	 * Get the sub directories in the base dir.
72
	 * Each array element contain
73
	 * the relative path (relative to the base dir) as key and the 
74
	 * full path as value.
75
	 * @return array of sub directries
76
	 * <code>array('path name' => 'full directory path', ...)</code>
77
	 */
78
	function getDirs() 
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...
79
	{
80
		if(is_null($this->dirs))
81
		{
82
			$dirs = $this->_dirs($this->getBaseDir(),'/');
83
			ksort($dirs);
84
			$this->dirs = $dirs;
85
		}
86
		return $this->dirs;
87
	}
88
89
	/**
90
	 * Recursively travese the directories to get a list
91
	 * of accessable directories.
92
	 * @param string $base the full path to the current directory
93
	 * @param string $path the relative path name
94
	 * @return array of accessiable sub-directories
95
	 * <code>array('path name' => 'full directory path', ...)</code>
96
	 */
97
	function _dirs($base, $path) 
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...
98
	{
99
		$base = Files::fixPath($base);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::fixPath() is not static, but was called statically. ( Ignorable by Annotation )

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

99
		/** @scrutinizer ignore-call */ 
100
  $base = Files::fixPath($base);
Loading history...
100
		$dirs = array();
101
102
		if($this->isValidBase() == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
103
			return $dirs;
104
105
		$d = @dir($base);
106
		
107
		while (false !== ($entry = $d->read())) 
0 ignored issues
show
Bug introduced by
The method read() does not exist on null. ( Ignorable by Annotation )

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

107
		while (false !== ($entry = $d->/** @scrutinizer ignore-call */ read())) 

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
		{
109
			//If it is a directory, and it doesn't start with
110
			// a dot, and if is it not the thumbnail directory
111
			if(is_dir($base.$entry) 
112
				&& substr($entry,0,1) != '.'
113
				&& $this->isThumbDir($entry) == false) 
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
114
			{
115
				$relative = Files::fixPath($path.$entry);
116
				$fullpath = Files::fixPath($base.$entry);
117
				$dirs[$relative] = $fullpath;
118
				$dirs = array_merge($dirs, $this->_dirs($fullpath, $relative));
119
			}
120
		}
121
		$d->close();
122
123
		Return $dirs;
124
	}
125
126
	/**
127
	 * Get all the files and directories of a relative path.
128
	 * @param string $path relative path to be base path.
129
	 * @return array of file and path information.
130
	 * <code>array(0=>array('relative'=>'fullpath',...), 1=>array('filename'=>fileinfo array(),...)</code>
131
	 * fileinfo array: <code>array('url'=>'full url', 
132
	 *                       'relative'=>'relative to base', 
133
	 *                        'fullpath'=>'full file path', 
134
	 *                        'image'=>imageInfo array() false if not image,
135
	 *                        'stat' => filestat)</code>
136
	 */
137
	function getFiles($path) 
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...
138
	{
139
		$files = array();
140
		$dirs = array();
141
142
		if($this->isValidBase() == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
143
			return array($files,$dirs);
144
145
		$path = Files::fixPath($path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::fixPath() is not static, but was called statically. ( Ignorable by Annotation )

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

145
		/** @scrutinizer ignore-call */ 
146
  $path = Files::fixPath($path);
Loading history...
146
		$base = Files::fixPath($this->getBaseDir());
147
		$fullpath = Files::makePath($base,$path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

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

147
		/** @scrutinizer ignore-call */ 
148
  $fullpath = Files::makePath($base,$path);
Loading history...
148
149
150
		$d = @dir($fullpath);
151
		
152
		while (false !== ($entry = $d->read())) 
153
		{
154
			//not a dot file or directory
155
			if(substr($entry,0,1) != '.')
156
			{
157
				if(is_dir($fullpath.$entry)
158
					&& $this->isThumbDir($entry) == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
159
				{
160
					$relative = Files::fixPath($path.$entry);
161
					$full = Files::fixPath($fullpath.$entry);
162
					$count = $this->countFiles($full);
163
					$dirs[$relative] = array('fullpath'=>$full,'entry'=>$entry,'count'=>$count);
164
				}
165
				else if(is_file($fullpath.$entry) && $this->isThumb($entry)==false && $this->isTmpFile($entry) == false) 
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
166
				{
167
					$img = $this->getImageInfo($fullpath.$entry);
168
169
					if(!(!is_array($img)&&$this->config['validate_images']))
170
					{
171
						$file['url'] = Files::makePath($this->config['base_url'],$path).$entry;
172
						$file['relative'] = $path.$entry;
173
						$file['fullpath'] = $fullpath.$entry;
174
						$file['image'] = $img;
175
						$file['stat'] = stat($fullpath.$entry);
176
						$files[$entry] = $file;
177
					}
178
				}
179
			}
180
		}
181
		$d->close();
182
		ksort($dirs);
183
		ksort($files);
184
		
185
		Return array($dirs, $files);
186
	}	
187
188
	/**
189
	 * Count the number of files and directories in a given folder
190
	 * minus the thumbnail folders and thumbnails.
191
	 */
192
	function countFiles($path) 
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...
193
	{
194
		$total = 0;
195
196
		if(is_dir($path)) 
197
		{
198
			$d = @dir($path);
199
200
			while (false !== ($entry = $d->read())) 
201
			{
202
				//echo $entry."<br>";
203
				if(substr($entry,0,1) != '.'
204
					&& $this->isThumbDir($entry) == false
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
205
					&& $this->isTmpFile($entry) == false
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
206
					&& $this->isThumb($entry) == false) 
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
207
				{
208
					$total++;
209
				}
210
			}
211
			$d->close();
212
		}
213
		return $total;
214
	}
215
216
	/**
217
	 * Get image size information.
218
	 * @param string $file the image file
219
	 * @return array of getImageSize information, 
220
	 *  false if the file is not an image.
221
	 */
222
	function getImageInfo($file) 
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...
223
	{
224
		Return @getImageSize($file);
225
	}
226
227
	/**
228
	 * Check if the file contains the thumbnail prefix.
229
	 * @param string $file filename to be checked
230
	 * @return true if the file contains the thumbnail prefix, false otherwise.
231
	 */
232
	function isThumb($file) 
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...
233
	{
234
		$len = strlen($this->config['thumbnail_prefix']);
235
		if(substr($file,0,$len)==$this->config['thumbnail_prefix'])
236
			Return true;
237
		else
238
			Return false;
239
	}
240
241
	/**
242
	 * Check if the given directory is a thumbnail directory.
243
	 * @param string $entry directory name
244
	 * @return true if it is a thumbnail directory, false otherwise
245
	 */
246
	function isThumbDir($entry) 
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...
247
	{
248
		if($this->config['thumbnail_dir'] == false
249
			|| strlen(trim($this->config['thumbnail_dir'])) == 0)
250
			Return false;		
251
		else
252
			Return ($entry == $this->config['thumbnail_dir']);
253
	}
254
255
	/**
256
	 * Check if the given file is a tmp file.
257
	 * @param string $file file name
258
	 * @return boolean true if it is a tmp file, false otherwise
259
	 */
260
	function isTmpFile($file) 
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...
261
	{
262
		$len = strlen($this->config['tmp_prefix']);
263
		if(substr($file,0,$len)==$this->config['tmp_prefix'])
264
			Return true;
265
		else
266
			Return false;	 	
267
	}
268
269
	/**
270
	 * For a given image file, get the respective thumbnail filename
271
	 * no file existence check is done.
272
	 * @param string $fullpathfile the full path to the image file
273
	 * @return string of the thumbnail file
274
	 */
275
	function getThumbName($fullpathfile) 
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...
276
	{
277
		$path_parts = pathinfo($fullpathfile);
278
		
279
		$thumbnail = $this->config['thumbnail_prefix'].$path_parts['basename'];
280
281
		if($this->config['safe_mode'] == true
282
			|| strlen(trim($this->config['thumbnail_dir'])) == 0)
283
		{
284
			Return Files::makeFile($path_parts['dirname'],$thumbnail);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

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

284
			Return Files::/** @scrutinizer ignore-call */ makeFile($path_parts['dirname'],$thumbnail);
Loading history...
285
		}
286
		else
287
		{
288
			if(strlen(trim($this->config['thumbnail_dir'])) > 0)
289
			{
290
				$path = Files::makePath($path_parts['dirname'],$this->config['thumbnail_dir']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

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

290
				/** @scrutinizer ignore-call */ 
291
    $path = Files::makePath($path_parts['dirname'],$this->config['thumbnail_dir']);
Loading history...
291
				if(!is_dir($path))
292
					Files::createFolder($path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::createFolder() is not static, but was called statically. ( Ignorable by Annotation )

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

292
					Files::/** @scrutinizer ignore-call */ 
293
            createFolder($path);
Loading history...
293
				Return Files::makeFile($path,$thumbnail);
294
			}
295
			else //should this ever happen?
296
			{
297
				//error_log('ImageManager: Error in creating thumbnail name');
298
			}
299
		}
300
	}
301
	
302
	/**
303
	 * Similar to getThumbName, but returns the URL, base on the
304
	 * given base_url in config.inc.php
305
	 * @param string $relative the relative image file name, 
306
	 * relative to the base_dir path
307
	 * @return string the url of the thumbnail
308
	 */
309
	function getThumbURL($relative) 
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...
310
	{
311
		$path_parts = pathinfo($relative);
312
		$thumbnail = $this->config['thumbnail_prefix'].$path_parts['basename'];
313
		if($path_parts['dirname']=='\\') $path_parts['dirname']='/';
314
315
		if($this->config['safe_mode'] == true
316
			|| strlen(trim($this->config['thumbnail_dir'])) == 0)
317
		{
318
			$path = Files::fixPath($path_parts['dirname']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::fixPath() is not static, but was called statically. ( Ignorable by Annotation )

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

318
			/** @scrutinizer ignore-call */ 
319
   $path = Files::fixPath($path_parts['dirname']);
Loading history...
319
			$url_path = Files::makePath($this->getBaseURL(), $path);
0 ignored issues
show
Bug introduced by
The method getBaseURL() does not exist on ImageManager. ( Ignorable by Annotation )

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

319
			$url_path = Files::makePath($this->/** @scrutinizer ignore-call */ getBaseURL(), $path);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

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

319
			/** @scrutinizer ignore-call */ 
320
   $url_path = Files::makePath($this->getBaseURL(), $path);
Loading history...
320
			Return Files::makeFile($url_path,$thumbnail);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

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

320
			Return Files::/** @scrutinizer ignore-call */ makeFile($url_path,$thumbnail);
Loading history...
321
		}
322
		else
323
		{
324
			if(strlen(trim($this->config['thumbnail_dir'])) > 0)
325
			{
326
				$path = Files::makePath($path_parts['dirname'],$this->config['thumbnail_dir']);
327
				$url_path = Files::makePath($this->getBaseURL(), $path);
328
				Return Files::makeFile($url_path,$thumbnail);
329
			}
330
			else //should this ever happen?
331
			{
332
				//error_log('ImageManager: Error in creating thumbnail url');
333
			}
334
335
		}
336
	}
337
338
	/**
339
	 * Check if the given path is part of the subdirectories
340
	 * under the base_dir.
341
	 * @param string $path the relative path to be checked
342
	 * @return boolean true if the path exists, false otherwise
343
	 */
344
	function validRelativePath($path) 
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...
345
	{
346
		$dirs = $this->getDirs();
347
		if($path == '/' || $path == '')
348
			Return true;
349
		//check the path given in the url against the 
350
		//list of paths in the system.
351
		for($i = 0; $i < count($dirs); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
352
		{
353
			$key = key($dirs);
354
			//we found the path
355
			if($key == $path)
356
				Return true;
357
		
358
			next($dirs);
359
		}		
360
		Return false;
361
	}
362
363
	/**
364
	 * Process uploaded files, assumes the file is in 
365
	 * $_FILES['upload'] and $_POST['dir'] is set.
366
	 * The dir must be relative to the base_dir and exists.
367
	 * If 'validate_images' is set to true, only file with
368
	 * image dimensions will be accepted.
369
	 * @return null
370
	 */
371
	function processUploads() 
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...
372
	{
373
		if($this->isValidBase() == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
374
			return;
375
376
		$relative = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relative is dead and can be removed.
Loading history...
377
378
		if(isset($_POST['dir'])) 
379
			$relative = rawurldecode($_POST['dir']);
380
		else
381
			return;
382
383
		//check for the file, and must have valid relative path
384
		if(isset($_FILES['upload']) && $this->validRelativePath($relative))
385
		{
386
			return $this->_processFiles($relative, $_FILES['upload']);
387
		}
388
	}
389
390
	/**
391
	 * Process upload files. The file must be an 
392
	 * uploaded file. If 'validate_images' is set to
393
	 * true, only images will be processed. Any duplicate
394
	 * file will be renamed. See Files::copyFile for details
395
	 * on renaming.
396
	 * @param string $relative the relative path where the file
397
	 * should be copied to.
398
	 * @param array $file the uploaded file from $_FILES
399
	 * @return boolean true if the file was processed successfully, 
400
	 * false otherwise
401
	 */
402
	function _processFiles($relative, $file)
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...
403
	{
404
		
405
		if($file['error']!=0)
406
		{
407
			Return false;
408
		}
409
410
		if(!is_file($file['tmp_name']))
411
		{
412
			Return false;
413
		}
414
415
		if(!is_uploaded_file($file['tmp_name']))
416
		{
417
			Files::delFile($file['tmp_name']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::delFile() is not static, but was called statically. ( Ignorable by Annotation )

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

417
			Files::/** @scrutinizer ignore-call */ 
418
          delFile($file['tmp_name']);
Loading history...
418
			Return false;
419
		}
420
		
421
422
		if($this->config['validate_images'] == true)
423
		{
424
			$imgInfo = @getImageSize($file['tmp_name']);
425
			if(!is_array($imgInfo))
426
			{
427
				Files::delFile($file['tmp_name']);
428
				Return false;
429
			}
430
		}
431
432
		//now copy the file
433
		$path = Files::makePath($this->getBaseDir(),$relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

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

433
		/** @scrutinizer ignore-call */ 
434
  $path = Files::makePath($this->getBaseDir(),$relative);
Loading history...
434
		$result = Files::copyFile($file['tmp_name'], $path, $file['name']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::copyFile() is not static, but was called statically. ( Ignorable by Annotation )

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

434
		/** @scrutinizer ignore-call */ 
435
  $result = Files::copyFile($file['tmp_name'], $path, $file['name']);
Loading history...
435
436
		//no copy error
437
       if(!is_int($result))
0 ignored issues
show
introduced by
The condition is_int($result) is always false.
Loading history...
438
       {
439
		   $dimensionsIndex = isset($_REQUEST['uploadSize']) ? $_REQUEST['uploadSize'] : 0;
440
		   // If maximum size is specified, constrain image to it.
441
           if ($this->config['maxWidth'][$dimensionsIndex] > 0 && $this->config['maxHeight'][$dimensionsIndex] > 0)
442
           {
443
			   $img = Image_Transform::factory(IMAGE_CLASS);
0 ignored issues
show
Bug Best Practice introduced by
The method Image_Transform::factory() is not static, but was called statically. ( Ignorable by Annotation )

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

443
			   /** @scrutinizer ignore-call */ 
444
      $img = Image_Transform::factory(IMAGE_CLASS);
Loading history...
444
			   $img->load($path . $result);
445
446
			   // image larger than max dimensions?
447
			   if ($img->img_x > $this->config['maxWidth'][$dimensionsIndex] || $img->img_y > $this->config['maxHeight'][$dimensionsIndex])
448
			   {
449
				   $percentage = min($this->config['maxWidth'][$dimensionsIndex] / $img->img_x, $this->config['maxHeight'][$dimensionsIndex] / $img->img_y);
450
				   $img->scale($percentage);
451
			   }
452
453
			   $img->save($path . $result);
454
			   $img->free();
455
           }
456
	   }
457
458
		//delete tmp files.
459
		Files::delFile($file['tmp_name']);
460
		Return false;
461
	}
462
463
	/**
464
	 * Get the URL of the relative file.
465
	 * basically appends the relative file to the 
466
	 * base_url given in config.inc.php
467
	 * @param string $relative a file the relative to the base_dir
468
	 * @return string the URL of the relative file.
469
	 */
470
	function getFileURL($relative) 
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...
471
	{
472
		Return Files::makeFile($this->getBaseURL(),$relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

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

472
		Return Files::/** @scrutinizer ignore-call */ makeFile($this->getBaseURL(),$relative);
Loading history...
473
	}
474
475
	/**
476
	 * Get the fullpath to a relative file.
477
	 * @param string $relative the relative file.
478
	 * @return string the full path, .ie. the base_dir + relative.
479
	 */
480
	function getFullPath($relative) 
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...
481
	{
482
		Return Files::makeFile($this->getBaseDir(),$relative);;
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

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

482
		Return Files::/** @scrutinizer ignore-call */ makeFile($this->getBaseDir(),$relative);;
Loading history...
483
	}
484
485
	/**
486
	 * Get the default thumbnail.
487
	 * @return string default thumbnail, empty string if 
488
	 * the thumbnail doesn't exist.
489
	 */
490
	function getDefaultThumb() 
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...
491
	{
492
		if(is_file($this->config['default_thumbnail']))
493
			Return $this->config['default_thumbnail'];
494
		else 
495
			Return '';
496
	}
497
498
499
	/**
500
	 * Get the thumbnail url to be displayed. 
501
	 * If the thumbnail exists, and it is up-to-date
502
	 * the thumbnail url will be returns. If the 
503
	 * file is not an image, a default image will be returned.
504
	 * If it is an image file, and no thumbnail exists or 
505
	 * the thumbnail is out-of-date (i.e. the thumbnail 
506
	 * modified time is less than the original file)
507
	 * then a thumbs.php?img=filename.jpg is returned.
508
	 * The thumbs.php url will generate a new thumbnail
509
	 * on the fly. If the image is less than the dimensions
510
	 * of the thumbnails, the image will be display instead.
511
	 * @param string $relative the relative image file.
512
	 * @return string the url of the thumbnail, be it
513
	 * actually thumbnail or a script to generate the
514
	 * thumbnail on the fly.
515
	 */
516
	function getThumbnail($relative) 
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...
517
	{
518
		$fullpath = Files::makeFile($this->getBaseDir(),$relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

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

518
		/** @scrutinizer ignore-call */ 
519
  $fullpath = Files::makeFile($this->getBaseDir(),$relative);
Loading history...
519
520
		//not a file???
521
		if(!is_file($fullpath))
522
			Return $this->getDefaultThumb();
523
524
		$imgInfo = @getImageSize($fullpath);
525
		
526
		//not an image
527
		if(!is_array($imgInfo))
528
			Return $this->getDefaultThumb();
529
530
		//the original image is smaller than thumbnails,
531
		//so just return the url to the original image.
532
		if ($imgInfo[0] <= $this->config['thumbnail_width']
533
		 && $imgInfo[1] <= $this->config['thumbnail_height'])
534
			Return $this->getFileURL($relative);
535
536
		$thumbnail = $this->getThumbName($fullpath);
537
		
538
		//check for thumbnails, if exists and
539
		// it is up-to-date, return the thumbnail url
540
		if(is_file($thumbnail))
541
		{
542
			if(filemtime($thumbnail) >= filemtime($fullpath))
543
				Return $this->getThumbURL($relative);
544
		}
545
546
		//well, no thumbnail was found, so ask the thumbs.php
547
		//to generate the thumbnail on the fly.
548
		Return 'thumbs.php?img='.rawurlencode($relative);
549
	}
550
551
	/**
552
	 * Delete and specified files.
553
	 * @return boolean true if delete, false otherwise
554
	 */
555
	function deleteFiles() 
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...
556
	{
557
		if(isset($_GET['delf']))
558
			$this->_delFile(rawurldecode($_GET['delf']));
559
	}
560
561
	/**
562
	 * Delete and specified directories.
563
	 * @return boolean true if delete, false otherwise
564
	 */
565
	function deleteDirs() 
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...
566
	{
567
		 if(isset($_GET['deld']))
568
			return $this->_delDir(rawurldecode($_GET['deld']));		
569
		 else
570
			 Return false;
571
	}
572
573
	/**
574
	 * Delete the relative file, and any thumbnails.
575
	 * @param string $relative the relative file.
576
	 * @return boolean true if deleted, false otherwise.
577
	 */
578
	function _delFile($relative) 
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...
579
	{
580
		$fullpath = Files::makeFile($this->getBaseDir(),$relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

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

580
		/** @scrutinizer ignore-call */ 
581
  $fullpath = Files::makeFile($this->getBaseDir(),$relative);
Loading history...
581
		
582
		//check that the file is an image
583
		if($this->config['validate_images'] == true)
584
		{
585
			if(!is_array($this->getImageInfo($fullpath)))
0 ignored issues
show
introduced by
The condition is_array($this->getImageInfo($fullpath)) is always true.
Loading history...
586
				return false; //hmmm not an Image!!???
587
		}
588
589
		$thumbnail = $this->getThumbName($fullpath);
590
591
		if(Files::delFile($fullpath))
0 ignored issues
show
Bug Best Practice introduced by
The method Files::delFile() is not static, but was called statically. ( Ignorable by Annotation )

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

591
		if(Files::/** @scrutinizer ignore-call */ delFile($fullpath))
Loading history...
592
			Return Files::delFile($thumbnail);
593
		else
594
			Return false;
595
	}
596
597
	/**
598
	 * Delete directories recursively.
599
	 * @param string $relative the relative path to be deleted.
600
	 * @return boolean true if deleted, false otherwise.
601
	 */
602
	function _delDir($relative) 
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...
603
	{
604
		$fullpath = Files::makePath($this->getBaseDir(),$relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

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

604
		/** @scrutinizer ignore-call */ 
605
  $fullpath = Files::makePath($this->getBaseDir(),$relative);
Loading history...
605
		if($this->countFiles($fullpath) <= 0)
606
			return Files::delFolder($fullpath,true); //delete recursively.
0 ignored issues
show
Bug Best Practice introduced by
The method Files::delFolder() is not static, but was called statically. ( Ignorable by Annotation )

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

606
			return Files::/** @scrutinizer ignore-call */ delFolder($fullpath,true); //delete recursively.
Loading history...
607
		else
608
			Return false;
609
	}
610
611
	/**
612
	 * Create new directories.
613
	 * If in safe_mode, nothing happens.
614
	 * @return boolean true if created, false otherwise.
615
	 */
616
	function processNewDir() 
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...
617
	{
618
		if($this->config['safe_mode'] == true)
619
			Return false;
620
621
		if(isset($_GET['newDir']) && isset($_GET['dir']))
622
		{
623
			$newDir = rawurldecode($_GET['newDir']);
624
			$dir = rawurldecode($_GET['dir']);
625
			$path = Files::makePath($this->getBaseDir(),$dir);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

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

625
			/** @scrutinizer ignore-call */ 
626
   $path = Files::makePath($this->getBaseDir(),$dir);
Loading history...
626
			$fullpath = Files::makePath($path, Files::escape($newDir));
0 ignored issues
show
Bug Best Practice introduced by
The method Files::escape() is not static, but was called statically. ( Ignorable by Annotation )

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

626
			$fullpath = Files::makePath($path, Files::/** @scrutinizer ignore-call */ escape($newDir));
Loading history...
627
			if(is_dir($fullpath))
628
				Return false;
629
630
			Return Files::createFolder($fullpath);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::createFolder() is not static, but was called statically. ( Ignorable by Annotation )

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

630
			Return Files::/** @scrutinizer ignore-call */ createFolder($fullpath);
Loading history...
631
		}
632
	}
633
634
	/**
635
	 * Do some graphic library method checkings
636
	 * @param string $library the graphics library, GD, NetPBM, or IM.
637
	 * @param string $method the method to check.
638
	 * @return boolean true if able, false otherwise.
639
	 */
640
	function validGraphicMethods($library,$method)
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...
641
	{
642
		switch ($library)
643
		{
644
			case 'GD':
645
				return $this->_checkGDLibrary($method);
0 ignored issues
show
Bug introduced by
The method _checkGDLibrary() does not exist on ImageManager. ( Ignorable by Annotation )

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

645
				return $this->/** @scrutinizer ignore-call */ _checkGDLibrary($method);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
646
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
647
			case 'NetPBM':
648
				return $this->_checkNetPBMLibrary($method);
0 ignored issues
show
Bug introduced by
The method _checkNetPBMLibrary() does not exist on ImageManager. ( Ignorable by Annotation )

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

648
				return $this->/** @scrutinizer ignore-call */ _checkNetPBMLibrary($method);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
649
				break;
650
			case 'IM':
651
				return $this->_checkIMLibrary($method);
0 ignored issues
show
Bug introduced by
The method _checkIMLibrary() does not exist on ImageManager. ( Ignorable by Annotation )

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

651
				return $this->/** @scrutinizer ignore-call */ _checkIMLibrary($method);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
652
		}
653
		return false;
654
	}
655
656
	function _checkIMLibrary($method)
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...
657
	{
658
		//ImageMagick goes throught 1 single executable
659
		if(is_file(Files::fixPath(IMAGE_TRANSFORM_LIB_PATH).'convert'))
0 ignored issues
show
Bug Best Practice introduced by
The method Files::fixPath() is not static, but was called statically. ( Ignorable by Annotation )

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

659
		if(is_file(Files::/** @scrutinizer ignore-call */ fixPath(IMAGE_TRANSFORM_LIB_PATH).'convert'))
Loading history...
660
			return true;
661
		else
662
			return false;
663
	}
664
665
	/**
666
	 * Check the GD library functionality.
667
	 * @param string $library the graphics library, GD, NetPBM, or IM.
668
	 * @return boolean true if able, false otherwise.
669
	 */
670
	function _checkGDLibrary($method)
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...
671
	{
672
		$errors = array();
673
		switch($method)
674
		{
675
			case 'create':
676
				$errors['createjpeg'] = function_exists('imagecreatefromjpeg');
677
				$errors['creategif'] = function_exists('imagecreatefromgif');
678
				$errors['createpng'] = function_exists('imagecreatefrompng');
679
				break;
680
			case 'modify':
681
				$errors['create'] = function_exists('ImageCreateTrueColor') || function_exists('ImageCreate');
682
				$errors['copy'] = function_exists('ImageCopyResampled') || function_exists('ImageCopyResized');
683
				break;
684
			case 'save':
685
				$errors['savejpeg'] = function_exists('imagejpeg');
686
				$errors['savegif'] = function_exists('imagegif');
687
				$errors['savepng'] = function_exists('imagepng');
688
				break;
689
		}
690
691
		return $errors;
692
	}
693
}
694
695
?>
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...
696