| 1520 | grunt.registerTask( 'build:protect_folders', 'Ensures every directory has an index.php file.', function() { |
| 1521 | var defaultIndexFile = BUILD_DIR + 'wp-content/index.php', |
| 1522 | folders = [ |
| 1523 | 'wp-includes', |
| 1524 | 'wp-admin', |
| 1525 | 'wp-content', |
| 1526 | ], |
| 1527 | excluded = [ |
| 1528 | 'wp-includes/ID3', |
| 1529 | 'wp-includes/PHPMailer', |
| 1530 | 'wp-includes/Requests', |
| 1531 | 'wp-includes/SimplePie', |
| 1532 | 'wp-includes/sodium_compat', |
| 1533 | 'wp-includes/Text', |
| 1534 | ]; |
| 1535 | |
| 1536 | /** |
| 1537 | * Gets all folders from a given path. |
| 1538 | * |
| 1539 | * @param {string} dirPath Directory path to search against. |
| 1540 | * @param {array} arrayOfFiles Array of files in the directory. |
| 1541 | * @param {array} arrayOfFolders Array of directories in the directory. |
| 1542 | * @returns {array} Array of folders |
| 1543 | */ |
| 1544 | var getAllFolders = function( dirPath, arrayOfFiles, arrayOfFolders ) { |
| 1545 | // Let's get all of the folders on this path. |
| 1546 | var files = fs.readdirSync( dirPath ); |
| 1547 | |
| 1548 | arrayOfFiles = arrayOfFiles || []; |
| 1549 | arrayOfFolders = arrayOfFolders || []; |
| 1550 | |
| 1551 | // Let's loop through the folders, looking for more directories. |
| 1552 | files.forEach( function( file ) { |
| 1553 | // Let's look for a directory. |
| 1554 | if ( fs.statSync( dirPath + "/" + file ).isDirectory() ) { |
| 1555 | // Let's see if the path contains any of the `excluded` paths. |
| 1556 | var dir = dirPath + "/" + file, |
| 1557 | block = false; |
| 1558 | |
| 1559 | excluded.map( function( path ) { |
| 1560 | if ( dir.includes( BUILD_DIR + path ) ) { |
| 1561 | block = true; |
| 1562 | } |
| 1563 | }); |
| 1564 | |
| 1565 | // This is a legit path, let's add to our array of valid paths. |
| 1566 | if ( ! block ) { |
| 1567 | arrayOfFolders.push( dir ); |
| 1568 | arrayOfFolders = getAllFolders( dirPath + "/" + file, arrayOfFiles, arrayOfFolders ); |
| 1569 | } |
| 1570 | } else { |
| 1571 | // Keep digging for paths. |
| 1572 | arrayOfFiles.push( path.join( __dirname, dirPath, "/", file ) ); |
| 1573 | } |
| 1574 | }); |
| 1575 | |
| 1576 | return arrayOfFolders; |
| 1577 | } |
| 1578 | |
| 1579 | /** |
| 1580 | * Checks if index.php exists in the current directory. |
| 1581 | * |
| 1582 | * @param {string} dirPath Path to start a search for index.php files. |
| 1583 | * @returns {bool} |
| 1584 | */ |
| 1585 | var doesDirHaveIndex = function ( dirPath ) { |
| 1586 | var index = '/index.php'; |
| 1587 | |
| 1588 | return fs.existsSync( dirPath + index ); |
| 1589 | } |
| 1590 | |
| 1591 | // Loop through folders, looking for blank index.php files. |
| 1592 | folders.forEach( function( path ) { |
| 1593 | var folder = getAllFolders( BUILD_DIR + path ); |
| 1594 | |
| 1595 | folder.forEach( function( path ) { |
| 1596 | if( ! doesDirHaveIndex( path ) ) { |
| 1597 | var newFile = path + '/index.php', |
| 1598 | index = fs.readFileSync( defaultIndexFile ); |
| 1599 | |
| 1600 | // The `wx` flag ensures that it doesn't overwrite any existing files. |
| 1601 | fs.writeFileSync( newFile, index, {flag: "wx"} ); |
| 1602 | } |
| 1603 | }) |
| 1604 | }); |
| 1605 | }); |
| 1606 | |