Skip to content

Commit

Permalink
Update autoload function to check for directory, rearrange some check…
Browse files Browse the repository at this point in the history
…s, throw more exceptions on invalid, troublesome types of files for better clarity
  • Loading branch information
AaronHolbrook committed Mar 28, 2019
1 parent c1ff4ae commit b00dc7a
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
*/
function autoload( $directory ) {

// Get a listing of the current directory
$scanned_dir = scandir( $directory );

if ( empty( $scanned_dir ) ) {
// Ensure this path exists
if ( ! is_dir( $directory ) ) {
return;
}

if ( count( $scanned_dir ) > 200 ) {
throw new \Exception( 'Too many files attempted to load via autoload' );
}
// Get a listing of the current directory
$scanned_dir = scandir( $directory );

// Ignore these items from scandir
$ignore = [
Expand All @@ -31,6 +28,14 @@ function autoload( $directory ) {
// Remove the ignored items
$scanned_dir = array_diff( $scanned_dir, $ignore );

if ( empty( $scanned_dir ) ) {
return;
}

if ( count( $scanned_dir ) > 250 ) {
throw new \Exception( 'Too many files attempted to load via autoload' );
}

foreach ( $scanned_dir as $item ) {

$filename = $directory . '/' . $item;
Expand All @@ -50,24 +55,20 @@ function autoload( $directory ) {
if ( 'dir' === $filetype ) {

autoload( $real_path );
}

// If it's a file, let's try to load it
} // If it's a file, let's try to load it
else if ( 'file' === $filetype ) {

// Don't allow files that have been uploaded
if ( is_uploaded_file( $real_path ) ) {
if ( true !== is_readable( $real_path ) ) {
continue;
}

$filesize = filesize( $real_path );
// Don't include empty or negative sized files
if ( $filesize <= 0 ) {
// Don't allow files that have been uploaded
if ( is_uploaded_file( $real_path ) ) {
continue;
}

// Don't include files that are greater than 300kb
if ( $filesize > 300000 ) {
// Only for files that really exist
if ( true !== file_exists( $real_path ) ) {
continue;
}

Expand All @@ -88,13 +89,16 @@ function autoload( $directory ) {
continue;
}

// Only for files that really exist
if ( true !== file_exists( $real_path ) ) {
continue;
$filesize = filesize( $real_path );

// Don't include negative sized files
if ( $filesize < 0 ) {
throw new \Exception( 'File size is negative, not autoloading' );
}

if ( true !== is_readable( $real_path ) ) {
continue;
// Don't include files that are greater than 300kb
if ( $filesize > 300000 ) {
throw new \Exception( 'File size is greater than 300kb, not autoloading' );
}

require_once( $real_path );
Expand Down

0 comments on commit b00dc7a

Please sign in to comment.