~canonical-sysadmins/wordpress/5.0

« back to all changes in this revision

Viewing changes to wp-includes/functions.php

  • Committer: Barry Price
  • Date: 2018-07-06 10:05:47 UTC
  • mto: This revision was merged to the branch mainline in revision 40.
  • Revision ID: barry.price@canonical.com-20180706100547-hhm1a1chqcb32bpm
new upstream release 4.9.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1704
1704
 * @since 3.9.0
1705
1705
 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
1706
1706
 * @since 4.5.0 Allows for Windows network shares.
 
1707
 * @since 4.9.7 Allows for PHP file wrappers.
1707
1708
 *
1708
1709
 * @param string $path Path to normalize.
1709
1710
 * @return string Normalized path.
1710
1711
 */
1711
1712
function wp_normalize_path( $path ) {
 
1713
        $wrapper = '';
 
1714
        if ( wp_is_stream( $path ) ) {
 
1715
                list( $wrapper, $path ) = explode( '://', $path, 2 );
 
1716
                $wrapper .= '://';
 
1717
        }
 
1718
 
 
1719
        // Standardise all paths to use /
1712
1720
        $path = str_replace( '\\', '/', $path );
 
1721
 
 
1722
        // Replace multiple slashes down to a singular, allowing for network shares having two slashes.
1713
1723
        $path = preg_replace( '|(?<=.)/+|', '/', $path );
 
1724
 
 
1725
        // Windows paths should uppercase the drive letter
1714
1726
        if ( ':' === substr( $path, 1, 1 ) ) {
1715
1727
                $path = ucfirst( $path );
1716
1728
        }
1717
 
        return $path;
 
1729
 
 
1730
        return $wrapper . $path;
1718
1731
}
1719
1732
 
1720
1733
/**
5504
5517
}
5505
5518
 
5506
5519
/**
 
5520
 * Deletes a file if its path is within the given directory.
 
5521
 *
 
5522
 * @since 4.9.7
 
5523
 *
 
5524
 * @param string $file      Absolute path to the file to delete.
 
5525
 * @param string $directory Absolute path to a directory.
 
5526
 * @return bool True on success, false on failure.
 
5527
 */
 
5528
function wp_delete_file_from_directory( $file, $directory ) {
 
5529
        $real_file = realpath( wp_normalize_path( $file ) );
 
5530
        $real_directory = realpath( wp_normalize_path( $directory ) );
 
5531
 
 
5532
        if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
 
5533
                return false;
 
5534
        }
 
5535
 
 
5536
        wp_delete_file( $file );
 
5537
 
 
5538
        return true;
 
5539
}
 
5540
 
 
5541
/**
5507
5542
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
5508
5543
 *
5509
5544
 * This prevents reusing the same tab for a preview when the user has navigated away.