| 1 |
<?php |
| 2 |
/** |
| 3 |
* Keeps plugin upload folders from being listed. |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace EasyInvoice\Helpers; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class UploadGuard { |
| 15 |
/** |
| 16 |
* Drop an index.php (and an .htaccess turning indexes off) into a folder |
| 17 |
* the plugin writes client files to, so a directory listing never shows |
| 18 |
* every payment proof or signature. Idempotent; silent on failure. |
| 19 |
* |
| 20 |
* @param string $dir Absolute path. |
| 21 |
*/ |
| 22 |
public static function protectDirectory( string $dir ): void { |
| 23 |
if ( '' === $dir ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
if ( ! is_dir( $dir ) ) { |
| 27 |
wp_mkdir_p( $dir ); |
| 28 |
} |
| 29 |
if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
$index = trailingslashit( $dir ) . 'index.php'; |
| 33 |
if ( ! file_exists( $index ) ) { |
| 34 |
@file_put_contents( $index, "<?php\n// Silence is golden.\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions,WordPress.PHP.NoSilencedErrors |
| 35 |
} |
| 36 |
$htaccess = trailingslashit( $dir ) . '.htaccess'; |
| 37 |
if ( ! file_exists( $htaccess ) ) { |
| 38 |
@file_put_contents( $htaccess, "Options -Indexes\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions,WordPress.PHP.NoSilencedErrors |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|