file.module 71 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Defines a "managed_file" Form API field and a "file" field for Field module.
 */

8
use Drupal\Component\Utility\Environment;
9
use Drupal\Core\Datetime\Entity\DateFormat;
10
use Drupal\Core\Field\FieldDefinitionInterface;
11 12
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\Messenger\MessengerInterface;
15
use Drupal\Core\Render\BubbleableMetadata;
16
use Drupal\Core\Render\Element;
17
use Drupal\Core\Routing\RouteMatchInterface;
18
use Drupal\Core\Link;
19
use Drupal\Core\Url;
20
use Drupal\file\Entity\File;
21
use Drupal\file\FileInterface;
22
use Drupal\Component\Utility\NestedArray;
23
use Drupal\Component\Utility\Unicode;
24
use Drupal\Core\Entity\EntityStorageInterface;
25
use Drupal\Core\Template\Attribute;
26

27 28 29
/**
 * The regex pattern used when checking for insecure file types.
 */
30
define('FILE_INSECURE_EXTENSION_REGEX', '/\.(phar|php|pl|py|cgi|asp|js)(\.|$)/i');
31

32
// Load all Field module hooks for File.
33
require_once __DIR__ . '/file.field.inc';
34

35 36 37
/**
 * Implements hook_help().
 */
38
function file_help($route_name, RouteMatchInterface $route_match) {
39 40
  switch ($route_name) {
    case 'help.page.file':
41 42
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
43
      $output .= '<p>' . t('The File module allows you to create fields that contain files. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":file_documentation">online documentation for the File module</a>.', [':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#', ':file_documentation' => 'https://www.drupal.org/documentation/modules/file']) . '</p>';
44 45
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
46
      $output .= '<dt>' . t('Managing and displaying file fields') . '</dt>';
47
      $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the file field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
48 49
      $output .= '<dt>' . t('Allowing file extensions') . '</dt>';
      $output .= '<dd>' . t('In the field settings, you can define the allowed file extensions (for example <em>pdf docx psd</em>) for the files that will be uploaded with the file field.') . '</dd>';
50
      $output .= '<dt>' . t('Storing files') . '</dt>';
51
      $output .= '<dd>' . t('Uploaded files can either be stored as <em>public</em> or <em>private</em>, depending on the <a href=":file-system">File system settings</a>. For more information, see the <a href=":system-help">System module help page</a>.', [':file-system' => Url::fromRoute('system.file_system_settings')->toString(), ':system-help' => Url::fromRoute('help.page', ['name' => 'system'])->toString()]) . '</dd>';
52 53 54 55
      $output .= '<dt>' . t('Restricting the maximum file size') . '</dt>';
      $output .= '<dd>' . t('The maximum file size that users can upload is limited by PHP settings of the server, but you can restrict by entering the desired value as the <em>Maximum upload size</em> setting. The maximum file size is automatically displayed to users in the help text of the file field.') . '</dd>';
      $output .= '<dt>' . t('Displaying files and descriptions') . '<dt>';
      $output .= '<dd>' . t('In the field settings, you can allow users to toggle whether individual files are displayed. In the display settings, you can then choose one of the following formats: <ul><li><em>Generic file</em> displays links to the files and adds icons that symbolize the file extensions. If <em>descriptions</em> are enabled and have been submitted, then the description is displayed instead of the file name.</li><li><em>URL to file</em> displays the full path to the file as plain text.</li><li><em>Table of files</em> lists links to the files and the file sizes in a table.</li><li><em>RSS enclosure</em> only displays the first file, and only in a RSS feed, formatted according to the RSS 2.0 syntax for enclosures.</li></ul> A file can still be linked to directly by its URI even if it is not displayed.') . '</dd>';
56 57 58 59 60
      $output .= '</dl>';
      return $output;
  }
}

61 62 63 64 65 66 67 68 69 70
/**
 * Implements hook_field_widget_info_alter().
 */
function file_field_widget_info_alter(array &$info) {
  // Allows using the 'uri' widget for the 'file_uri' field type, which uses it
  // as the default widget.
  // @see \Drupal\file\Plugin\Field\FieldType\FileUriItem
  $info['uri']['field_types'][] = 'file_uri';
}

71 72 73 74 75 76 77 78 79
/**
 * Copies a file to a new location and adds a file record to the database.
 *
 * This function should be used when manipulating files that have records
 * stored in the database. This is a powerful function that in many ways
 * performs like an advanced version of copy().
 * - Checks if $source and $destination are valid and readable/writable.
 * - If file already exists in $destination either the call will error out,
 *   replace the file or rename the file based on the $replace parameter.
80
 * - If the $source and $destination are equal, the behavior depends on the
81 82 83
 *   $replace parameter. FileSystemInterface::EXISTS_REPLACE will error out.
 *   FileSystemInterface::EXISTS_RENAME will rename the file until the
 *   $destination is unique.
84 85 86 87
 * - Adds the new file to the files database. If the source file is a
 *   temporary file, the resulting file will also be a temporary file. See
 *   file_save_upload() for details on temporary files.
 *
88
 * @param \Drupal\file\FileInterface $source
89
 *   A file entity.
90
 * @param string $destination
91 92
 *   A string containing the destination that $source should be
 *   copied to. This must be a stream wrapper URI.
93
 * @param int $replace
94 95
 *   (optional) Replace behavior when the destination file already exists.
 *   Possible values include:
96 97 98 99 100 101 102
 *   - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a
 *     managed file with the destination name exists, then its database entry
 *     will be updated. If no database entry is found, then a new one will be
 *     created.
 *   - FileSystemInterface::EXISTS_RENAME: (default) Append
 *     _{incrementing number} until the filename is unique.
 *   - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE.
103
 *
104 105
 * @return \Drupal\file\FileInterface|false
 *   File entity if the copy is successful, or FALSE in the event of an error.
106
 *
107
 * @see \Drupal\Core\File\FileSystemInterface::copy()
108 109
 * @see hook_file_copy()
 */
110
function file_copy(FileInterface $source, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) {
111 112
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
113 114 115 116
  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');

  if (!$stream_wrapper_manager->isValidUri($destination)) {
117
    if (($realpath = $file_system->realpath($source->getFileUri())) !== FALSE) {
118
      \Drupal::logger('file')->notice('File %file (%realpath) could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
119 120
    }
    else {
121
      \Drupal::logger('file')->notice('File %file could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]);
122
    }
123
    \Drupal::messenger()->addError(t('The specified file %file could not be copied because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()]));
124 125 126
    return FALSE;
  }

127 128
  try {
    $uri = $file_system->copy($source->getFileUri(), $destination, $replace);
129 130
    $file = $source->createDuplicate();
    $file->setFileUri($uri);
131
    $file->setFilename($file_system->basename($uri));
132
    // If we are replacing an existing file re-use its database record.
133 134
    // @todo Do not create a new entity in order to update it. See
    //   https://www.drupal.org/node/2241865.
135
    if ($replace == FileSystemInterface::EXISTS_REPLACE) {
136
      $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
137 138
      if (count($existing_files)) {
        $existing = reset($existing_files);
139
        $file->fid = $existing->id();
140
        $file->setOriginalId($existing->id());
141
        $file->setFilename($existing->getFilename());
142 143 144 145
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
146
    elseif ($replace == FileSystemInterface::EXISTS_RENAME && is_file($destination)) {
147
      $file->setFilename($file_system->basename($destination));
148 149 150 151 152
    }

    $file->save();

    // Inform modules that the file has been copied.
153
    \Drupal::moduleHandler()->invokeAll('file_copy', [$file, $source]);
154 155 156

    return $file;
  }
157 158 159
  catch (FileException $e) {
    return FALSE;
  }
160 161 162 163 164 165 166 167 168 169 170
}

/**
 * Moves a file to a new location and update the file's database entry.
 *
 * - Checks if $source and $destination are valid and readable/writable.
 * - Performs a file move if $source is not equal to $destination.
 * - If file already exists in $destination either the call will error out,
 *   replace the file or rename the file based on the $replace parameter.
 * - Adds the new file to the files database.
 *
171
 * @param \Drupal\file\FileInterface $source
172
 *   A file entity.
173
 * @param string $destination
174 175
 *   A string containing the destination that $source should be moved
 *   to. This must be a stream wrapper URI.
176
 * @param int $replace
177 178
 *   (optional) The replace behavior when the destination file already exists.
 *   Possible values include:
179 180 181 182 183 184 185 186
 *   - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a
 *     managed file with the destination name exists then its database entry
 *     will be updated and $source->delete() called after invoking
 *     hook_file_move(). If no database entry is found, then the source files
 *     record will be updated.
 *   - FileSystemInterface::EXISTS_RENAME: (default) Append
 *     _{incrementing number} until the filename is unique.
 *   - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE.
187
 *
188
 * @return \Drupal\file\FileInterface|false
189 190
 *   Resulting file entity for success, or FALSE in the event of an error.
 *
191
 * @see \Drupal\Core\File\FileSystemInterface::move()
192 193
 * @see hook_file_move()
 */
194
function file_move(FileInterface $source, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) {
195 196
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
197 198 199 200
  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');

  if (!$stream_wrapper_manager->isValidUri($destination)) {
201
    if (($realpath = $file_system->realpath($source->getFileUri())) !== FALSE) {
202
      \Drupal::logger('file')->notice('File %file (%realpath) could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
203 204
    }
    else {
205
      \Drupal::logger('file')->notice('File %file could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]);
206
    }
207
    \Drupal::messenger()->addError(t('The specified file %file could not be moved because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()]));
208 209 210
    return FALSE;
  }

211 212
  try {
    $uri = $file_system->move($source->getFileUri(), $destination, $replace);
213 214 215
    $delete_source = FALSE;

    $file = clone $source;
216
    $file->setFileUri($uri);
217
    // If we are replacing an existing file re-use its database record.
218
    if ($replace == FileSystemInterface::EXISTS_REPLACE) {
219
      $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
220 221 222
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $delete_source = TRUE;
223
        $file->fid = $existing->id();
224
        $file->uuid = $existing->uuid();
225 226 227 228
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
229
    elseif ($replace == FileSystemInterface::EXISTS_RENAME && is_file($destination)) {
230
      $file->setFilename(\Drupal::service('file_system')->basename($destination));
231 232 233 234 235
    }

    $file->save();

    // Inform modules that the file has been moved.
236
    \Drupal::moduleHandler()->invokeAll('file_move', [$file, $source]);
237 238

    // Delete the original if it's not in use elsewhere.
239
    if ($delete_source && !\Drupal::service('file.usage')->listUsage($source)) {
240 241 242 243 244
      $source->delete();
    }

    return $file;
  }
245 246 247
  catch (FileException $e) {
    return FALSE;
  }
248 249 250 251 252 253 254 255
}

/**
 * Checks that a file meets the criteria specified by the validators.
 *
 * After executing the validator callbacks specified hook_file_validate() will
 * also be called to allow other modules to report errors about the file.
 *
256
 * @param \Drupal\file\FileInterface $file
257
 *   A file entity.
258
 * @param array $validators
259 260 261 262 263 264 265
 *   (optional) An associative array of callback functions used to validate
 *   the file. The keys are function names and the values arrays of callback
 *   parameters which will be passed in after the file entity. The functions
 *   should return an array of error messages; an empty array indicates that
 *   the file passed validation. The callback functions will be called in the
 *   order specified in the array, then the hook hook_file_validate()
 *   will be invoked so other modules can validate the new file.
266
 *
267
 * @return array
268 269 270 271
 *   An array containing validation error messages.
 *
 * @see hook_file_validate()
 */
272
function file_validate(FileInterface $file, $validators = []) {
273
  // Call the validation functions specified by this function's caller.
274
  $errors = [];
275 276 277 278 279 280 281 282
  foreach ($validators as $function => $args) {
    if (function_exists($function)) {
      array_unshift($args, $file);
      $errors = array_merge($errors, call_user_func_array($function, $args));
    }
  }

  // Let other modules perform validation on the new file.
283
  return array_merge($errors, \Drupal::moduleHandler()->invokeAll('file_validate', [$file]));
284 285 286 287 288
}

/**
 * Checks for files with names longer than can be stored in the database.
 *
289
 * @param \Drupal\file\FileInterface $file
290 291
 *   A file entity.
 *
292
 * @return array
293 294
 *   An empty array if the file name length is smaller than the limit or an
 *   array containing an error message if it's not or is empty.
295
 */
296
function file_validate_name_length(FileInterface $file) {
297
  $errors = [];
298

299
  if (!$file->getFilename()) {
300 301
    $errors[] = t("The file's name is empty. Please give a name to the file.");
  }
302
  if (strlen($file->getFilename()) > 240) {
303 304 305 306 307 308 309 310
    $errors[] = t("The file's name exceeds the 240 characters limit. Please rename the file and try again.");
  }
  return $errors;
}

/**
 * Checks that the filename ends with an allowed extension.
 *
311
 * @param \Drupal\file\FileInterface $file
312
 *   A file entity.
313
 * @param string $extensions
314 315
 *   A string with a space separated list of allowed extensions.
 *
316
 * @return array
317 318
 *   An empty array if the file extension is allowed or an array containing an
 *   error message if it's not.
319 320 321
 *
 * @see hook_file_validate()
 */
322
function file_validate_extensions(FileInterface $file, $extensions) {
323
  $errors = [];
324 325

  $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
326
  if (!preg_match($regex, $file->getFilename())) {
327
    $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => $extensions]);
328 329 330 331 332 333 334
  }
  return $errors;
}

/**
 * Checks that the file's size is below certain limits.
 *
335
 * @param \Drupal\file\FileInterface $file
336
 *   A file entity.
337
 * @param int $file_limit
338 339
 *   (optional) The maximum file size in bytes. Zero (the default) indicates
 *   that no limit should be enforced.
340
 * @param int $user_limit
341 342
 *   (optional) The maximum number of bytes the user is allowed. Zero (the
 *   default) indicates that no limit should be enforced.
343
 *
344
 * @return array
345 346
 *   An empty array if the file size is below limits or an array containing an
 *   error message if it's not.
347 348 349
 *
 * @see hook_file_validate()
 */
350
function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit = 0) {
351
  $user = \Drupal::currentUser();
352
  $errors = [];
353

354
  if ($file_limit && $file->getSize() > $file_limit) {
355
    $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => format_size($file->getSize()), '%maxsize' => format_size($file_limit)]);
356
  }
357

358
  // Save a query by only calling spaceUsed() when a limit is provided.
359
  if ($user_limit && (\Drupal::entityTypeManager()->getStorage('file')->spaceUsed($user->id()) + $file->getSize()) > $user_limit) {
360
    $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', ['%filesize' => format_size($file->getSize()), '%quota' => format_size($user_limit)]);
361
  }
362

363 364 365 366
  return $errors;
}

/**
367
 * Checks that the file is recognized as a valid image.
368
 *
369
 * @param \Drupal\file\FileInterface $file
370 371
 *   A file entity.
 *
372
 * @return array
373 374
 *   An empty array if the file is a valid image or an array containing an error
 *   message if it's not.
375 376 377
 *
 * @see hook_file_validate()
 */
378
function file_validate_is_image(FileInterface $file) {
379
  $errors = [];
380

381 382 383 384
  $image_factory = \Drupal::service('image.factory');
  $image = $image_factory->get($file->getFileUri());
  if (!$image->isValid()) {
    $supported_extensions = $image_factory->getSupportedExtensions();
385
    $errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %types', ['%types' => implode(', ', $supported_extensions)]);
386 387 388 389 390 391 392 393
  }

  return $errors;
}

/**
 * Verifies that image dimensions are within the specified maximum and minimum.
 *
394
 * Non-image files will be ignored. If an image toolkit is available the image
395 396
 * will be scaled to fit within the desired maximum dimensions.
 *
397
 * @param \Drupal\file\FileInterface $file
398
 *   A file entity. This function may resize the file affecting its size.
399 400 401 402 403 404 405 406 407
 * @param string|int $maximum_dimensions
 *   (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or
 *   '85x85'. If an image toolkit is installed, the image will be resized down
 *   to these dimensions. A value of zero (the default) indicates no restriction
 *   on size, so no resizing will be attempted.
 * @param string|int $minimum_dimensions
 *   (optional) A string in the form WIDTHxHEIGHT. This will check that the
 *   image meets a minimum size. A value of zero (the default) indicates that
 *   there is no restriction on size.
408
 *
409 410 411 412 413
 * @return array
 *   An empty array if the file meets the specified dimensions, was resized
 *   successfully to meet those requirements or is not an image. If the image
 *   does not meet the requirements or an attempt to resize it fails, an array
 *   containing the error message will be returned.
414 415 416
 *
 * @see hook_file_validate()
 */
417
function file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
418
  $errors = [];
419 420

  // Check first that the file is an image.
421
  $image_factory = \Drupal::service('image.factory');
422
  $image = $image_factory->get($file->getFileUri());
423

424
  if ($image->isValid()) {
425
    $scaling = FALSE;
426 427 428
    if ($maximum_dimensions) {
      // Check that it is smaller than the given dimensions.
      list($width, $height) = explode('x', $maximum_dimensions);
429
      if ($image->getWidth() > $width || $image->getHeight() > $height) {
430
        // Try to resize the image to fit the dimensions.
431
        if ($image->scale($width, $height)) {
432
          $scaling = TRUE;
433
          $image->save();
434
          if (!empty($width) && !empty($height)) {
435 436 437 438 439 440
            $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
              [
                '%dimensions' => $maximum_dimensions,
                '%new_width' => $image->getWidth(),
                '%new_height' => $image->getHeight(),
              ]);
441 442
          }
          elseif (empty($width)) {
443 444 445 446 447 448
            $message = t('The image was resized to fit within the maximum allowed height of %height pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
              [
                '%height' => $height,
                '%new_width' => $image->getWidth(),
                '%new_height' => $image->getHeight(),
              ]);
449 450
          }
          elseif (empty($height)) {
451 452 453 454 455 456
            $message = t('The image was resized to fit within the maximum allowed width of %width pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
              [
                '%width' => $width,
                '%new_width' => $image->getWidth(),
                '%new_height' => $image->getHeight(),
              ]);
457
          }
458
          \Drupal::messenger()->addStatus($message);
459 460
        }
        else {
461
          $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.');
462 463 464 465 466 467 468
        }
      }
    }

    if ($minimum_dimensions) {
      // Check that it is larger than the given dimensions.
      list($width, $height) = explode('x', $minimum_dimensions);
469
      if ($image->getWidth() < $width || $image->getHeight() < $height) {
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
        if ($scaling) {
          $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.',
            [
              '%dimensions' => $minimum_dimensions,
              '%width' => $image->getWidth(),
              '%height' => $image->getHeight(),
            ]);
        }
        else {
          $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.',
            [
              '%dimensions' => $minimum_dimensions,
              '%width' => $image->getWidth(),
              '%height' => $image->getHeight(),
            ]);
        }
486 487 488 489 490 491 492 493 494 495
      }
    }
  }

  return $errors;
}

/**
 * Saves a file to the specified destination and creates a database entry.
 *
496
 * @param string $data
497
 *   A string containing the contents of the file.
498 499 500 501 502
 * @param string|null $destination
 *   (optional) A string containing the destination URI. This must be a stream
 *   wrapper URI. If no value or NULL is provided, a randomized name will be
 *   generated and the file will be saved using Drupal's default files scheme,
 *   usually "public://".
503
 * @param int $replace
504 505
 *   (optional) The replace behavior when the destination file already exists.
 *   Possible values include:
506 507 508 509 510 511 512
 *   - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a
 *     managed file with the destination name exists, then its database entry
 *     will be updated. If no database entry is found, then a new one will be
 *     created.
 *   - FileSystemInterface::EXISTS_RENAME: (default) Append
 *     _{incrementing number} until the filename is unique.
 *   - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE.
513
 *
514
 * @return \Drupal\file\FileInterface|false
515 516
 *   A file entity, or FALSE on error.
 *
517
 * @see \Drupal\Core\File\FileSystemInterface::saveData()
518
 */
519
function file_save_data($data, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) {
520
  $user = \Drupal::currentUser();
521 522

  if (empty($destination)) {
523
    $destination = \Drupal::config('system.file')->get('default_scheme') . '://';
524
  }
525 526 527 528

  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  if (!$stream_wrapper_manager->isValidUri($destination)) {
529
    \Drupal::logger('file')->notice('The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', ['%destination' => $destination]);
530
    \Drupal::messenger()->addError(t('The data could not be saved because the destination is invalid. More information is available in the system log.'));
531 532 533
    return FALSE;
  }

534 535
  try {
    $uri = \Drupal::service('file_system')->saveData($data, $destination, $replace);
536
    // Create a file entity.
537
    $file = File::create([
538
      'uri' => $uri,
539
      'uid' => $user->id(),
540
      'status' => FILE_STATUS_PERMANENT,
541
    ]);
542
    // If we are replacing an existing file re-use its database record.
543 544
    // @todo Do not create a new entity in order to update it. See
    //   https://www.drupal.org/node/2241865.
545
    if ($replace == FileSystemInterface::EXISTS_REPLACE) {
546
      $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
547 548
      if (count($existing_files)) {
        $existing = reset($existing_files);
549
        $file->fid = $existing->id();
550
        $file->setOriginalId($existing->id());
551
        $file->setFilename($existing->getFilename());
552 553 554 555
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
556
    elseif ($replace == FileSystemInterface::EXISTS_RENAME && is_file($destination)) {
557
      $file->setFilename(\Drupal::service('file_system')->basename($destination));
558 559 560 561 562
    }

    $file->save();
    return $file;
  }
563 564 565 566
  catch (FileException $e) {
    return FALSE;
  }

567 568 569 570 571
}

/**
 * Examines a file entity and returns appropriate content headers for download.
 *
572
 * @param \Drupal\file\FileInterface $file
573 574
 *   A file entity.
 *
575
 * @return array
576 577 578
 *   An associative array of headers, as expected by
 *   \Symfony\Component\HttpFoundation\StreamedResponse.
 */
579
function file_get_content_headers(FileInterface $file) {
580
  $type = Unicode::mimeHeaderEncode($file->getMimeType());
581

582
  return [
583
    'Content-Type' => $type,
584
    'Content-Length' => $file->getSize(),
585
    'Cache-Control' => 'private',
586
  ];
587 588
}

589
/**
590
 * Implements hook_theme().
591 592
 */
function file_theme() {
593
  return [
594
    // From file.module.
595 596 597 598
    'file_link' => [
      'variables' => ['file' => NULL, 'description' => NULL, 'attributes' => []],
    ],
    'file_managed_file' => [
599
      'render element' => 'element',
600
    ],
601 602 603 604 605 606
    'file_audio' => [
      'variables' => ['files' => [], 'attributes' => NULL],
    ],
    'file_video' => [
      'variables' => ['files' => [], 'attributes' => NULL],
    ],
607

608
    // From file.field.inc.
609
    'file_widget_multiple' => [
610
      'render element' => 'element',
611
      'file' => 'file.field.inc',
612 613 614
    ],
    'file_upload_help' => [
      'variables' => ['description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL],
615
      'file' => 'file.field.inc',
616 617
    ],
  ];
618 619 620
}

/**
621
 * Implements hook_file_download().
622
 */
623
function file_file_download($uri) {
624
  // Get the file record based on the URI. If not in the database just return.
625
  /** @var \Drupal\file\FileInterface[] $files */
626
  $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
627
  if (count($files)) {
628 629 630
    foreach ($files as $item) {
      // Since some database servers sometimes use a case-insensitive comparison
      // by default, double check that the filename is an exact match.
631
      if ($item->getFileUri() === $uri) {
632 633 634 635
        $file = $item;
        break;
      }
    }
636
  }
637
  if (!isset($file)) {
638 639 640
    return;
  }

641 642 643 644 645 646 647 648 649 650 651 652 653 654
  // Find out if a temporary file is still used in the system.
  if ($file->isTemporary()) {
    $usage = \Drupal::service('file.usage')->listUsage($file);
    if (empty($usage) && $file->getOwnerId() != \Drupal::currentUser()->id()) {
      // Deny access to temporary files without usage that are not owned by the
      // same user. This prevents the security issue that a private file that
      // was protected by field permissions becomes available after its usage
      // was removed and before it is actually deleted from the file system.
      // Modules that depend on this behavior should make the file permanent
      // instead.
      return -1;
    }
  }

655
  // Find out which (if any) fields of this type contain the file.
656
  $references = file_get_file_references($file, NULL, EntityStorageInterface::FIELD_LOAD_CURRENT, NULL);
657

658 659 660 661 662
  // Stop processing if there are no references in order to avoid returning
  // headers for files controlled by other modules. Make an exception for
  // temporary files where the host entity has not yet been saved (for example,
  // an image preview on a node/add form) in which case, allow download by the
  // file's owner.
663
  if (empty($references) && ($file->isPermanent() || $file->getOwnerId() != \Drupal::currentUser()->id())) {
664
    return;
665 666
  }

667
  if (!$file->access('download')) {
668 669 670 671
    return -1;
  }

  // Access is granted.
672 673
  $headers = file_get_content_headers($file);
  return $headers;
674 675
}

676
/**
677
 * Implements hook_cron().
678 679
 */
function file_cron() {
680
  $age = \Drupal::config('system.file')->get('temporary_maximum_age');
681
  $file_storage = \Drupal::entityTypeManager()->getStorage('file');
682

683 684 685
  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');

686 687 688 689 690 691 692 693
  // Only delete temporary files if older than $age. Note that automatic cleanup
  // is disabled if $age set to 0.
  if ($age) {
    $fids = Drupal::entityQuery('file')
      ->condition('status', FILE_STATUS_PERMANENT, '<>')
      ->condition('changed', REQUEST_TIME - $age, '<')
      ->range(0, 100)
      ->execute();
694
    $files = $file_storage->loadMultiple($fids);
695
    foreach ($files as $file) {
696
      $references = \Drupal::service('file.usage')->listUsage($file);
697
      if (empty($references)) {
698
        if (!file_exists($file->getFileUri())) {
699
          if (!$stream_wrapper_manager->isValidUri($file->getFileUri())) {
700 701 702 703 704
            \Drupal::logger('file system')->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem. This could be caused by a missing stream wrapper.', ['%path' => $file->getFileUri()]);
          }
          else {
            \Drupal::logger('file system')->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem.', ['%path' => $file->getFileUri()]);
          }
705
        }
706 707 708
        // Delete the file entity. If the file does not exist, this will
        // generate a second notice in the watchdog.
        $file->delete();
709 710
      }
      else {
711
        \Drupal::logger('file system')->info('Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.', ['%path' => $file->getFileUri(), '%modules' => implode(', ', array_keys($references))]);
712 713 714 715 716
      }
    }
  }
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
/**
 * Saves form file uploads.
 *
 * The files will be added to the {file_managed} table as temporary files.
 * Temporary files are periodically cleaned. Use the 'file.usage' service to
 * register the usage of the file which will automatically mark it as permanent.
 *
 * @param array $element
 *   The FAPI element whose values are being saved.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 * @param null|int $delta
 *   (optional) The delta of the file to return the file entity.
 *   Defaults to NULL.
 * @param int $replace
 *   (optional) The replace behavior when the destination file already exists.
 *   Possible values include:
734 735 736 737
 *   - FileSystemInterface::EXISTS_REPLACE: Replace the existing file.
 *   - FileSystemInterface::EXISTS_RENAME: (default) Append
 *     _{incrementing number} until the filename is unique.
 *   - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE.
738 739 740 741 742 743 744
 *
 * @return array|\Drupal\file\FileInterface|null|false
 *   An array of file entities or a single file entity if $delta != NULL. Each
 *   array element contains the file entity if the upload succeeded or FALSE if
 *   there was an error. Function returns NULL if no file was uploaded.
 *
 * @internal
745 746 747 748
 *   This function is internal, and may be removed in a minor version release.
 *   It wraps file_save_upload() to allow correct error handling in forms.
 *   Contrib and custom code should not call this function, they should use the
 *   managed file upload widgets in core.
749
 *
750 751
 * @see https://www.drupal.org/project/drupal/issues/3069020
 * @see https://www.drupal.org/project/drupal/issues/2482783
752
 */
753
function _file_save_upload_from_form(array $element, FormStateInterface $form_state, $delta = NULL, $replace = FileSystemInterface::EXISTS_RENAME) {
754 755
  // Get all errors set before calling this method. This will also clear them
  // from $_SESSION.
756
  $errors_before = \Drupal::messenger()->deleteByType(MessengerInterface::TYPE_ERROR);
757 758 759 760 761 762 763 764 765

  $upload_location = isset($element['#upload_location']) ? $element['#upload_location'] : FALSE;
  $upload_name = implode('_', $element['#parents']);
  $upload_validators = isset($element['#upload_validators']) ? $element['#upload_validators'] : [];

  $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $replace);

  // Get new errors that are generated while trying to save the upload. This
  // will also clear them from $_SESSION.
766 767
  $errors_new = \Drupal::messenger()->deleteByType(MessengerInterface::TYPE_ERROR);
  if (!empty($errors_new)) {
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

    if (count($errors_new) > 1) {
      // Render multiple errors into a single message.
      // This is needed because only one error per element is supported.
      $render_array = [
        'error' => [
          '#markup' => t('One or more files could not be uploaded.'),
        ],
        'item_list' => [
          '#theme' => 'item_list',
          '#items' => $errors_new,
        ],
      ];
      $error_message = \Drupal::service('renderer')->renderPlain($render_array);
    }
    else {
      $error_message = reset($errors_new);
    }

    $form_state->setError($element, $error_message);
  }

  // Ensure that errors set prior to calling this method are still shown to the
  // user.
792 793 794
  if (!empty($errors_before)) {
    foreach ($errors_before as $error) {
      \Drupal::messenger()->addError($error);
795 796 797 798 799 800
    }
  }

  return $result;
}

801 802 803 804
/**
 * Saves file uploads to a new location.
 *
 * The files will be added to the {file_managed} table as temporary files.
805 806
 * Temporary files are periodically cleaned. Use the 'file.usage' service to
 * register the usage of the file which will automatically mark it as permanent.
807
 *
808 809 810 811
 * Note that this function does not support correct form error handling. The
 * file upload widgets in core do support this. It is advised to use these in
 * any custom form, instead of calling this function.
 *
812
 * @param string $form_field_name
813 814
 *   A string that is the associative array key of the upload form element in
 *   the form array.
815
 * @param array $validators
816
 *   (optional) An associative array of callback functions used to validate the
817
 *   file. See file_validate() for a full discussion of the array format.
818 819 820 821 822 823 824 825 826 827 828 829
 *   If the array is empty, it will be set up to call file_validate_extensions()
 *   with a safe list of extensions, as follows: "jpg jpeg gif png txt doc
 *   xls pdf ppt pps odt ods odp". To allow all extensions, you must explicitly
 *   set this array to ['file_validate_extensions' => '']. (Beware: this is not
 *   safe and should only be allowed for trusted users, if at all.)
 * @param string|false $destination
 *   (optional) A string containing the URI that the file should be copied to.
 *   This must be a stream wrapper URI. If this value is omitted or set to
 *   FALSE, Drupal's temporary files scheme will be used ("temporary://").
 * @param null|int $delta
 *   (optional) The delta of the file to return the file entity.
 *   Defaults to NULL.
830
 * @param int $replace
831 832
 *   (optional) The replace behavior when the destination file already exists.
 *   Possible values include:
833 834 835 836
 *   - FileSystemInterface::EXISTS_REPLACE: Replace the existing file.
 *   - FileSystemInterface::EXISTS_RENAME: (default) Append
 *     _{incrementing number} until the filename is unique.
 *   - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE.
837
 *
838 839 840 841
 * @return array|\Drupal\file\FileInterface|null|false
 *   An array of file entities or a single file entity if $delta != NULL. Each
 *   array element contains the file entity if the upload succeeded or FALSE if
 *   there was an error. Function returns NULL if no file was uploaded.
842 843 844 845
 *
 * @see _file_save_upload_from_form()
 *
 * @todo: move this logic to a service in https://www.drupal.org/node/2244513.