diff options
author | webchick <webchick@24967.no-reply.drupal.org> | 2012-01-21 10:52:40 -0800 |
---|---|---|
committer | webchick <webchick@24967.no-reply.drupal.org> | 2012-01-21 10:52:40 -0800 |
commit | 8164434505e4292e5d584eebbf7b05b403d9c07b (patch) | |
tree | 723b2507f6382b71902eb4bb7aa2a6c37a1e3c95 /includes | |
parent | 283523192421f0708c01f7fa08d5814442c757a5 (diff) | |
download | brdo-8164434505e4292e5d584eebbf7b05b403d9c07b.tar.gz brdo-8164434505e4292e5d584eebbf7b05b403d9c07b.tar.bz2 |
Issue #278425 by andypost, droplet, OnkelTem, chx, c960657, drewish, kotnik, realityloop: Change notice for: Using basename() is not locale safe.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 2 | ||||
-rw-r--r-- | includes/file.inc | 49 | ||||
-rw-r--r-- | includes/filetransfer/filetransfer.inc | 6 | ||||
-rw-r--r-- | includes/locale.inc | 2 | ||||
-rw-r--r-- | includes/stream_wrappers.inc | 4 | ||||
-rw-r--r-- | includes/updater.inc | 4 |
6 files changed, 48 insertions, 19 deletions
diff --git a/includes/common.inc b/includes/common.inc index 7dff3caec..f8e40a961 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -2974,7 +2974,7 @@ function drupal_get_css($css = NULL, $skip_alter = FALSE) { foreach ($css as $key => $item) { if ($item['type'] == 'file') { // If defined, force a unique basename for this file. - $basename = isset($item['basename']) ? $item['basename'] : basename($item['data']); + $basename = isset($item['basename']) ? $item['basename'] : drupal_basename($item['data']); if (isset($previous_item[$basename])) { // Remove the previous item that shared the same base name. unset($css[$previous_item[$basename]]); diff --git a/includes/file.inc b/includes/file.inc index 4188b417f..7fd6c71c9 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -770,7 +770,7 @@ function file_copy(stdClass $source, $destination = NULL, $replace = FILE_EXISTS $file = clone $source; $file->fid = NULL; $file->uri = $uri; - $file->filename = basename($uri); + $file->filename = drupal_basename($uri); // If we are replacing an existing file re-use its database record. if ($replace == FILE_EXISTS_REPLACE) { $existing_files = file_load_multiple(array(), array('uri' => $uri)); @@ -783,7 +783,7 @@ function file_copy(stdClass $source, $destination = NULL, $replace = FILE_EXISTS // If we are renaming around an existing file (rather than a directory), // use its basename for the filename. elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) { - $file->filename = basename($destination); + $file->filename = drupal_basename($destination); } $file = file_save($file); @@ -871,14 +871,14 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST // Build a destination URI if necessary. if (!isset($destination)) { - $destination = file_build_uri(basename($source)); + $destination = file_build_uri(drupal_basename($source)); } // Prepare the destination directory. if (file_prepare_directory($destination)) { // The destination is already a directory, so append the source basename. - $destination = file_stream_wrapper_uri_normalize($destination . '/' . basename($source)); + $destination = file_stream_wrapper_uri_normalize($destination . '/' . drupal_basename($source)); } else { // Perhaps $destination is a dir/file? @@ -958,7 +958,7 @@ function file_destination($destination, $replace) { break; case FILE_EXISTS_RENAME: - $basename = basename($destination); + $basename = drupal_basename($destination); $directory = drupal_dirname($destination); $destination = file_create_filename($basename, $directory); break; @@ -1033,7 +1033,7 @@ function file_move(stdClass $source, $destination = NULL, $replace = FILE_EXISTS // If we are renaming around an existing file (rather than a directory), // use its basename for the filename. elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) { - $file->filename = basename($destination); + $file->filename = drupal_basename($destination); } $file = file_save($file); @@ -1451,7 +1451,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE, $file = new stdClass(); $file->uid = $user->uid; $file->status = 0; - $file->filename = trim(basename($_FILES['files']['name'][$source]), '.'); + $file->filename = trim(drupal_basename($_FILES['files']['name'][$source]), '.'); $file->uri = $_FILES['files']['tmp_name'][$source]; $file->filemime = file_get_mimetype($file->filename); $file->filesize = $_FILES['files']['size'][$source]; @@ -1849,7 +1849,7 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM $file = new stdClass(); $file->fid = NULL; $file->uri = $uri; - $file->filename = basename($uri); + $file->filename = drupal_basename($uri); $file->filemime = file_get_mimetype($file->uri); $file->uid = $user->uid; $file->status = FILE_STATUS_PERMANENT; @@ -1865,7 +1865,7 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM // If we are renaming around an existing file (rather than a directory), // use its basename for the filename. elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) { - $file->filename = basename($destination); + $file->filename = drupal_basename($destination); } return file_save($file); @@ -2275,6 +2275,35 @@ function drupal_dirname($uri) { } /** + * Gets the filename from a given path. + * + * PHP's basename() does not properly support streams or filenames beginning + * with a non-US-ASCII character. + * + * @see http://bugs.php.net/bug.php?id=37738 + * @see basename() + * + * @ingroup php_wrappers + */ +function drupal_basename($uri, $suffix = NULL) { + $separators = '/'; + if (DIRECTORY_SEPARATOR != '/') { + // For Windows OS add special separator. + $separators .= DIRECTORY_SEPARATOR; + } + // Remove right-most slashes when $uri points to directory. + $uri = rtrim($uri, $separators); + // Returns the trailing part of the $uri starting after one of the directory + // separators. + $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : ''; + // Cuts off a suffix from the filename. + if ($suffix) { + $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename); + } + return $filename; +} + +/** * Creates a directory using Drupal's default mode. * * PHP's mkdir() does not respect Drupal's default permissions mode. If a mode @@ -2370,7 +2399,7 @@ function drupal_tempnam($directory, $prefix) { $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) { - return $scheme . '://' . basename($filename); + return $scheme . '://' . drupal_basename($filename); } else { return FALSE; diff --git a/includes/filetransfer/filetransfer.inc b/includes/filetransfer/filetransfer.inc index 2083da9d3..bd2057cdd 100644 --- a/includes/filetransfer/filetransfer.inc +++ b/includes/filetransfer/filetransfer.inc @@ -211,7 +211,7 @@ abstract class FileTransfer { */ protected function copyDirectoryJailed($source, $destination) { if ($this->isDirectory($destination)) { - $destination = $destination . '/' . basename($source); + $destination = $destination . '/' . drupal_basename($source); } $this->createDirectory($destination); foreach (new RecursiveIteratorIterator(new SkipDotsRecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) { @@ -302,9 +302,9 @@ abstract class FileTransfer { $chroot = ''; while (count($parts)) { $check = implode($parts, '/'); - if ($this->isFile($check . '/' . basename(__FILE__))) { + if ($this->isFile($check . '/' . drupal_basename(__FILE__))) { // Remove the trailing slash. - return substr($chroot,0,-1); + return substr($chroot, 0, -1); } $chroot .= array_shift($parts) . '/'; } diff --git a/includes/locale.inc b/includes/locale.inc index a00887d23..0db4d4a07 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -2293,7 +2293,7 @@ function _locale_batch_import($filepath, &$context) { // The filename is either {langcode}.po or {prefix}.{langcode}.po, so // we can extract the language code to use for the import from the end. if (preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $langcode)) { - $file = (object) array('filename' => basename($filepath), 'uri' => $filepath); + $file = (object) array('filename' => drupal_basename($filepath), 'uri' => $filepath); _locale_import_read_po('db-store', $file, LOCALE_IMPORT_KEEP, $langcode[2]); $context['results'][] = $filepath; } diff --git a/includes/stream_wrappers.inc b/includes/stream_wrappers.inc index e47668e3a..2af8c9e91 100644 --- a/includes/stream_wrappers.inc +++ b/includes/stream_wrappers.inc @@ -317,7 +317,7 @@ abstract class DrupalLocalStreamWrapper implements DrupalStreamWrapperInterface } $extension = ''; - $file_parts = explode('.', basename($uri)); + $file_parts = explode('.', drupal_basename($uri)); // Remove the first part: a full filename should not match an extension. array_shift($file_parts); @@ -377,7 +377,7 @@ abstract class DrupalLocalStreamWrapper implements DrupalStreamWrapperInterface $realpath = realpath($path); if (!$realpath) { // This file does not yet exist. - $realpath = realpath(dirname($path)) . '/' . basename($path); + $realpath = realpath(dirname($path)) . '/' . drupal_basename($path); } $directory = realpath($this->getDirectoryPath()); if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) { diff --git a/includes/updater.inc b/includes/updater.inc index 363c6ebff..9801629b1 100644 --- a/includes/updater.inc +++ b/includes/updater.inc @@ -141,7 +141,7 @@ class Updater { return FALSE; } foreach ($info_files as $info_file) { - if (drupal_substr($info_file->filename, 0, -5) == basename($directory)) { + if (drupal_substr($info_file->filename, 0, -5) == drupal_basename($directory)) { // Info file Has the same name as the directory, return it. return $info_file->uri; } @@ -163,7 +163,7 @@ class Updater { * The name of the project. */ public static function getProjectName($directory) { - return basename($directory); + return drupal_basename($directory); } /** |