| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Class allowing to filter DirectoryIterator, to return only files that Imagify can optimize and folders. |
| 6 |
* It also allows to remove forbidden folders. |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
* @author Grégory Viguier |
| 10 |
*/ |
| 11 |
class Imagify_Files_Iterator extends FilterIterator { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class version. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
* @since 1.7 |
| 18 |
* @author Grégory Viguier |
| 19 |
*/ |
| 20 |
const VERSION = '1.0.2'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Tell if the iterator will return both folders and images, or only images. |
| 24 |
* |
| 25 |
* @var bool |
| 26 |
* @since 1.7 |
| 27 |
*/ |
| 28 |
protected $include_folders; |
| 29 |
|
| 30 |
/** |
| 31 |
* Filesystem object. |
| 32 |
* |
| 33 |
* @var object Imagify_Filesystem |
| 34 |
* @since 1.7.1 |
| 35 |
* @access protected |
| 36 |
* @author Grégory Viguier |
| 37 |
*/ |
| 38 |
protected $filesystem; |
| 39 |
|
| 40 |
/** |
| 41 |
* Check whether the current element of the iterator is acceptable. |
| 42 |
* |
| 43 |
* @since 1.7 |
| 44 |
* @access public |
| 45 |
* @author Grégory Viguier |
| 46 |
* |
| 47 |
* @param object $iterator The iterator that is being filtered. |
| 48 |
* @param bool $include_folders True to return both folders and images. False to return only images. |
| 49 |
*/ |
| 50 |
public function __construct( $iterator, $include_folders = true ) { |
| 51 |
parent::__construct( $iterator ); |
| 52 |
$this->include_folders = (bool) $include_folders; |
| 53 |
$this->filesystem = Imagify_Filesystem::get_instance(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check whether the current element of the iterator is acceptable. |
| 58 |
* |
| 59 |
* @since 1.7 |
| 60 |
* @access public |
| 61 |
* @author Grégory Viguier |
| 62 |
* |
| 63 |
* @return bool Returns whether the current element of the iterator is acceptable through this filter. |
| 64 |
*/ |
| 65 |
public function accept() { |
| 66 |
static $extensions, $has_extension_method; |
| 67 |
|
| 68 |
$file_path = $this->current()->getPathname(); |
| 69 |
|
| 70 |
// Prevent triggering an open_basedir restriction error. |
| 71 |
$file_name = $this->filesystem->file_name( $file_path ); |
| 72 |
|
| 73 |
if ( '.' === $file_name || '..' === $file_name ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
// Forbidden file/folder paths and names. |
| 78 |
$is_dir = $this->isDir(); |
| 79 |
|
| 80 |
if ( $is_dir ) { |
| 81 |
$file_path = trailingslashit( $file_path ); |
| 82 |
} |
| 83 |
|
| 84 |
if ( Imagify_Files_Scan::is_path_forbidden( $file_path ) ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
// OK for folders. |
| 89 |
if ( $this->include_folders && $is_dir ) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
// Only files. |
| 94 |
if ( ! $this->current()->isFile() ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
// Only files with the required extension. |
| 99 |
if ( ! isset( $extensions ) ) { |
| 100 |
$extensions = array_keys( imagify_get_mime_types() ); |
| 101 |
$extensions = implode( '|', $extensions ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! isset( $has_extension_method ) ) { |
| 105 |
// This method was introduced in php 5.3.6. |
| 106 |
$has_extension_method = method_exists( $this->current(), 'getExtension' ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( $has_extension_method ) { |
| 110 |
$file_extension = strtolower( $this->current()->getExtension() ); |
| 111 |
} else { |
| 112 |
$file_extension = strtolower( $this->filesystem->path_info( $file_path, 'extension' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
return preg_match( '@^' . $extensions . '$@', $file_extension ); |
| 116 |
} |
| 117 |
} |
| 118 |
|