Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / includes / file.inc
index 3fcb38a4e45f6433ef041e5f00367433aceb847f..77a743eab044ad4ec095b26c1fa159a4a56dba25 100644 (file)
@@ -6,7 +6,6 @@
  */
 
 use Drupal\Component\FileSystem\FileSystem as ComponentFileSystem;
-use Drupal\Component\Utility\Unicode;
 use Drupal\Component\Utility\UrlHelper;
 use Drupal\Component\PhpStorage\FileStorage;
 use Drupal\Component\Utility\Bytes;
@@ -100,7 +99,6 @@ function file_stream_wrapper_valid_scheme($scheme) {
   return \Drupal::service('file_system')->validScheme($scheme);
 }
 
-
 /**
  * Returns the part of a URI after the schema.
  *
@@ -202,7 +200,7 @@ function file_create_url($uri) {
     //   HTTP and to https://example.com/bar.jpg when viewing a HTTPS page)
     // Both types of relative URIs are characterized by a leading slash, hence
     // we can use a single check.
-    if (Unicode::substr($uri, 0, 1) == '/') {
+    if (mb_substr($uri, 0, 1) == '/') {
       return $uri;
     }
     else {
@@ -270,7 +268,7 @@ function file_url_transform_relative($file_url) {
     $http_host = $host . ':' . $port;
   }
 
-  return preg_replace('|^https?://' . $http_host . '|', '', $file_url);
+  return preg_replace('|^https?://' . preg_quote($http_host, '|') . '|', '', $file_url);
 }
 
 /**
@@ -462,8 +460,9 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
   }
   // Attempt to resolve the URIs. This is necessary in certain configurations
   // (see above).
-  $real_source = drupal_realpath($source) ?: $source;
-  $real_destination = drupal_realpath($destination) ?: $destination;
+  $file_system = \Drupal::service('file_system');
+  $real_source = $file_system->realpath($source) ?: $source;
+  $real_destination = $file_system->realpath($destination) ?: $destination;
   // Perform the copy operation.
   if (!@copy($real_source, $real_destination)) {
     \Drupal::logger('file')->error('The specified file %file could not be copied to %destination.', ['%file' => $source, '%destination' => $destination]);
@@ -507,12 +506,14 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
 function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_EXISTS_RENAME) {
   $original_source = $source;
   $logger = \Drupal::logger('file');
+  $file_system = \Drupal::service('file_system');
 
   // Assert that the source file actually exists.
   if (!file_exists($source)) {
-    // @todo Replace drupal_set_message() calls with exceptions instead.
-    drupal_set_message(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', ['%file' => $original_source]), 'error');
-    if (($realpath = drupal_realpath($original_source)) !== FALSE) {
+    // @todo Replace \Drupal::messenger()->addError() calls with exceptions
+    // instead.
+    \Drupal::messenger()->addError(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', ['%file' => $original_source]));
+    if (($realpath = $file_system->realpath($original_source)) !== FALSE) {
       $logger->notice('File %file (%realpath) could not be moved/copied because it does not exist.', ['%file' => $original_source, '%realpath' => $realpath]);
     }
     else {
@@ -526,7 +527,6 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
     $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.
@@ -538,7 +538,7 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
     if (!file_prepare_directory($dirname)) {
       // The destination is not valid.
       $logger->notice('File %file could not be moved/copied because the destination directory %destination is not configured correctly.', ['%file' => $original_source, '%destination' => $dirname]);
-      drupal_set_message(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', ['%file' => $original_source]), 'error');
+      \Drupal::messenger()->addError(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', ['%file' => $original_source]));
       return FALSE;
     }
   }
@@ -546,16 +546,16 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
   // Determine whether we can perform this operation based on overwrite rules.
   $destination = file_destination($destination, $replace);
   if ($destination === FALSE) {
-    drupal_set_message(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', ['%file' => $original_source]), 'error');
+    \Drupal::messenger()->addError(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', ['%file' => $original_source]));
     $logger->notice('File %file could not be moved/copied because a file by that name already exists in the destination directory (%destination)', ['%file' => $original_source, '%destination' => $destination]);
     return FALSE;
   }
 
   // Assert that the source and destination filenames are not the same.
-  $real_source = drupal_realpath($source);
-  $real_destination = drupal_realpath($destination);
+  $real_source = $file_system->realpath($source);
+  $real_destination = $file_system->realpath($destination);
   if ($source == $destination || ($real_source !== FALSE) && ($real_source == $real_destination)) {
-    drupal_set_message(t('The specified file %file was not moved/copied because it would overwrite itself.', ['%file' => $source]), 'error');
+    \Drupal::messenger()->addError(t('The specified file %file was not moved/copied because it would overwrite itself.', ['%file' => $source]));
     $logger->notice('File %file could not be moved/copied because it would overwrite itself.', ['%file' => $source]);
     return FALSE;
   }
@@ -653,8 +653,9 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST
   }
   // Attempt to resolve the URIs. This is necessary in certain configurations
   // (see above) and can also permit fast moves across local schemes.
-  $real_source = drupal_realpath($source) ?: $source;
-  $real_destination = drupal_realpath($destination) ?: $destination;
+  $file_system = \Drupal::service('file_system');
+  $real_source = $file_system->realpath($source) ?: $source;
+  $real_destination = $file_system->realpath($destination) ?: $destination;
   // Perform the move operation.
   if (!@rename($real_source, $real_destination)) {
     // Fall back to slow copy and unlink procedure. This is necessary for
@@ -696,8 +697,8 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST
  * @param $extensions
  *   A space-separated list of extensions that should not be altered.
  * @param $alerts
- *   If TRUE, drupal_set_message() will be called to display a message if the
- *   file name was changed.
+ *   If TRUE, \Drupal::messenger()->addStatus() will be called to display
+ *   a message if the file name was changed.
  *
  * @return string
  *   The potentially modified $filename.
@@ -716,8 +717,10 @@ function file_munge_filename($filename, $extensions, $alerts = TRUE) {
     // Split the filename up by periods. The first part becomes the basename
     // the last part the final extension.
     $filename_parts = explode('.', $filename);
-    $new_filename = array_shift($filename_parts); // Remove file basename.
-    $final_extension = array_pop($filename_parts); // Remove final extension.
+    // Remove file basename.
+    $new_filename = array_shift($filename_parts);
+    // Remove final extension.
+    $final_extension = array_pop($filename_parts);
 
     // Loop through the middle parts of the name and add an underscore to the
     // end of each section that could be a file extension but isn't in the list
@@ -731,7 +734,7 @@ function file_munge_filename($filename, $extensions, $alerts = TRUE) {
     $filename = $new_filename . '.' . $final_extension;
 
     if ($alerts && $original != $filename) {
-      drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $filename]));
+      \Drupal::messenger()->addStatus(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $filename]));
     }
   }
 
@@ -891,7 +894,7 @@ function file_unmanaged_delete($path) {
  *
  * @param $path
  *   A string containing either an URI or a file or directory path.
- * @param $callback
+ * @param callable $callback
  *   (optional) Callback function to run on each file prior to deleting it and
  *   on each directory prior to traversing it. For example, can be used to
  *   modify permissions.
@@ -922,8 +925,6 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
   return file_unmanaged_delete($path);
 }
 
-
-
 /**
  * Moves an uploaded file to a new location.
  *
@@ -966,7 +967,7 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX
   // Write the data to a temporary file.
   $temp_name = drupal_tempnam('temporary://', 'file');
   if (file_put_contents($temp_name, $data) === FALSE) {
-    drupal_set_message(t('The file could not be created.'), 'error');
+    \Drupal::messenger()->addError(t('The file could not be created.'));
     return FALSE;
   }
 
@@ -1031,7 +1032,7 @@ function file_scan_directory($dir, $mask, $options = [], $depth = 0) {
   // performance boost.
   if (!isset($options['nomask'])) {
     $ignore_directories = Settings::get('file_scan_ignore_directories', []);
-    array_walk($ignore_directories, function(&$value) {
+    array_walk($ignore_directories, function (&$value) {
       $value = preg_quote($value, '/');
     });
     $default_nomask = '/^' . implode('|', $ignore_directories) . '$/';