|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Utils; |
| 15 | + |
| 16 | +require __DIR__ . '/../system/Test/bootstrap.php'; |
| 17 | + |
| 18 | +use CodeIgniter\CLI\CLI; |
| 19 | +use RecursiveDirectoryIterator; |
| 20 | +use RecursiveIteratorIterator; |
| 21 | +use RuntimeException; |
| 22 | + |
| 23 | +function findExecutableFiles($dir) |
| 24 | +{ |
| 25 | + $execFileList = [ |
| 26 | + 'admin/release-userguide', |
| 27 | + 'admin/release-deploy', |
| 28 | + 'admin/apibot', |
| 29 | + 'admin/alldocs', |
| 30 | + 'admin/release', |
| 31 | + 'admin/docbot', |
| 32 | + 'admin/release-notes.bb', |
| 33 | + 'admin/release-revert', |
| 34 | + 'admin/starter/builds', |
| 35 | + 'user_guide_src/add-edit-this-page', |
| 36 | + ]; |
| 37 | + |
| 38 | + $executableFiles = []; |
| 39 | + |
| 40 | + // Check if the directory exists |
| 41 | + if (! is_dir($dir)) { |
| 42 | + throw new RuntimeException('No such directory: ' . $dir); |
| 43 | + } |
| 44 | + |
| 45 | + // Create a Recursive Directory Iterator |
| 46 | + $iterator = new RecursiveIteratorIterator( |
| 47 | + new RecursiveDirectoryIterator($dir) |
| 48 | + ); |
| 49 | + |
| 50 | + // Iterate over each item in the directory |
| 51 | + foreach ($iterator as $fileinfo) { |
| 52 | + // Check if the item is a file and is executable |
| 53 | + if ($fileinfo->isFile() && is_executable($fileinfo->getPathname())) { |
| 54 | + $filePath = $fileinfo->getPathname(); |
| 55 | + |
| 56 | + // Check allow list |
| 57 | + if (in_array($filePath, $execFileList, true)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if (str_ends_with($filePath, '.sh')) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $executableFiles[] = $filePath; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return $executableFiles; |
| 70 | +} |
| 71 | + |
| 72 | +// Main |
| 73 | +chdir(__DIR__ . '/../'); |
| 74 | + |
| 75 | +$dirs = ['admin', 'app', 'system', 'tests', 'user_guide_src', 'utils', 'writable']; |
| 76 | + |
| 77 | +$executableFiles = []; |
| 78 | + |
| 79 | +foreach ($dirs as $dir) { |
| 80 | + $executableFiles = array_merge($executableFiles, findExecutableFiles($dir)); |
| 81 | +} |
| 82 | + |
| 83 | +if ($executableFiles !== []) { |
| 84 | + CLI::write('Files with unnecessary execution permissions were detected:', 'light_gray', 'red'); |
| 85 | + |
| 86 | + foreach ($executableFiles as $file) { |
| 87 | + CLI::write('- ' . $file); |
| 88 | + } |
| 89 | + |
| 90 | + exit(1); |
| 91 | +} |
| 92 | + |
| 93 | +CLI::write('No files with unnecessary execution permissions were detected.', 'black', 'green'); |
| 94 | + |
| 95 | +exit(0); |
0 commit comments