Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / file / file.module
1 <?php
2
3 /**
4  * @file
5  * Defines a "managed_file" Form API field and a "file" field for Field module.
6  */
7
8 use Drupal\Core\Datetime\Entity\DateFormat;
9 use Drupal\Core\Field\FieldDefinitionInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Render\BubbleableMetadata;
12 use Drupal\Core\Render\Element;
13 use Drupal\Core\Routing\RouteMatchInterface;
14 use Drupal\Core\Url;
15 use Drupal\file\Entity\File;
16 use Drupal\file\FileInterface;
17 use Drupal\Component\Utility\NestedArray;
18 use Drupal\Component\Utility\Unicode;
19 use Drupal\Core\Entity\EntityStorageInterface;
20 use Drupal\Core\Template\Attribute;
21
22 // Load all Field module hooks for File.
23 require_once __DIR__ . '/file.field.inc';
24
25 /**
26  * Implements hook_help().
27  */
28 function file_help($route_name, RouteMatchInterface $route_match) {
29   switch ($route_name) {
30     case 'help.page.file':
31       $output = '';
32       $output .= '<h3>' . t('About') . '</h3>';
33       $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' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#', ':file_documentation' => 'https://www.drupal.org/documentation/modules/file']) . '</p>';
34       $output .= '<h3>' . t('Uses') . '</h3>';
35       $output .= '<dl>';
36       $output .= '<dt>' . t('Managing and displaying file fields') . '</dt>';
37       $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')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#']) . '</dd>';
38       $output .= '<dt>' . t('Allowing file extensions') . '</dt>';
39       $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>';
40       $output .= '<dt>' . t('Storing files') . '</dt>';
41       $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' => \Drupal::url('system.file_system_settings'), ':system-help' => \Drupal::url('help.page', ['name' => 'system'])]) . '</dd>';
42       $output .= '<dt>' . t('Restricting the maximum file size') . '</dt>';
43       $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>';
44       $output .= '<dt>' . t('Displaying files and descriptions') . '<dt>';
45       $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>';
46       $output .= '</dl>';
47       return $output;
48   }
49 }
50
51 /**
52  * Loads file entities from the database.
53  *
54  * @param array|null $fids
55  *   (optional) An array of entity IDs. If omitted or NULL, all entities are
56  *   loaded.
57  * @param bool $reset
58  *   (optional) Whether to reset the internal file_load_multiple() cache.
59  *   Defaults to FALSE.
60  *
61  * @return array
62  *   An array of file entities, indexed by fid.
63  *
64  * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
65  *   Use \Drupal\file\Entity\File::loadMultiple().
66  *
67  * @see hook_ENTITY_TYPE_load()
68  * @see file_load()
69  * @see entity_load()
70  * @see \Drupal\Core\Entity\Query\EntityQueryInterface
71  */
72 function file_load_multiple(array $fids = NULL, $reset = FALSE) {
73   if ($reset) {
74     \Drupal::entityManager()->getStorage('file')->resetCache($fids);
75   }
76   return File::loadMultiple($fids);
77 }
78
79 /**
80  * Loads a single file entity from the database.
81  *
82  * @param int $fid
83  *   A file ID.
84  * @param bool $reset
85  *   (optional) Whether to reset the internal file_load_multiple() cache.
86  *   Defaults to FALSE.
87  *
88  * @return \Drupal\file\FileInterface|null
89  *   A file entity or NULL if the file was not found.
90  *
91  * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
92  *   Use \Drupal\file\Entity\File::load().
93  *
94  * @see hook_ENTITY_TYPE_load()
95  * @see file_load_multiple()
96  */
97 function file_load($fid, $reset = FALSE) {
98   if ($reset) {
99     \Drupal::entityManager()->getStorage('file')->resetCache([$fid]);
100   }
101   return File::load($fid);
102 }
103
104 /**
105  * Copies a file to a new location and adds a file record to the database.
106  *
107  * This function should be used when manipulating files that have records
108  * stored in the database. This is a powerful function that in many ways
109  * performs like an advanced version of copy().
110  * - Checks if $source and $destination are valid and readable/writable.
111  * - If file already exists in $destination either the call will error out,
112  *   replace the file or rename the file based on the $replace parameter.
113  * - If the $source and $destination are equal, the behavior depends on the
114  *   $replace parameter. FILE_EXISTS_REPLACE will error out. FILE_EXISTS_RENAME
115  *   will rename the file until the $destination is unique.
116  * - Adds the new file to the files database. If the source file is a
117  *   temporary file, the resulting file will also be a temporary file. See
118  *   file_save_upload() for details on temporary files.
119  *
120  * @param \Drupal\file\FileInterface $source
121  *   A file entity.
122  * @param string $destination
123  *   A string containing the destination that $source should be
124  *   copied to. This must be a stream wrapper URI.
125  * @param int $replace
126  *   (optional) Replace behavior when the destination file already exists.
127  *   Possible values include:
128  *   - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
129  *     the destination name exists, then its database entry will be updated. If
130  *     no database entry is found, then a new one will be created.
131  *   - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
132  *     filename is unique.
133  *   - FILE_EXISTS_ERROR: Do nothing and return FALSE.
134  *
135  * @return \Drupal\file\FileInterface|false
136  *   File entity if the copy is successful, or FALSE in the event of an error.
137  *
138  * @see file_unmanaged_copy()
139  * @see hook_file_copy()
140  */
141 function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
142   if (!file_valid_uri($destination)) {
143     if (($realpath = drupal_realpath($source->getFileUri())) !== FALSE) {
144       \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]);
145     }
146     else {
147       \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]);
148     }
149     drupal_set_message(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()]), 'error');
150     return FALSE;
151   }
152
153   if ($uri = file_unmanaged_copy($source->getFileUri(), $destination, $replace)) {
154     $file = $source->createDuplicate();
155     $file->setFileUri($uri);
156     $file->setFilename(drupal_basename($uri));
157     // If we are replacing an existing file re-use its database record.
158     // @todo Do not create a new entity in order to update it. See
159     //   https://www.drupal.org/node/2241865.
160     if ($replace == FILE_EXISTS_REPLACE) {
161       $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
162       if (count($existing_files)) {
163         $existing = reset($existing_files);
164         $file->fid = $existing->id();
165         $file->setOriginalId($existing->id());
166         $file->setFilename($existing->getFilename());
167       }
168     }
169     // If we are renaming around an existing file (rather than a directory),
170     // use its basename for the filename.
171     elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
172       $file->setFilename(drupal_basename($destination));
173     }
174
175     $file->save();
176
177     // Inform modules that the file has been copied.
178     \Drupal::moduleHandler()->invokeAll('file_copy', [$file, $source]);
179
180     return $file;
181   }
182   return FALSE;
183 }
184
185 /**
186  * Moves a file to a new location and update the file's database entry.
187  *
188  * - Checks if $source and $destination are valid and readable/writable.
189  * - Performs a file move if $source is not equal to $destination.
190  * - If file already exists in $destination either the call will error out,
191  *   replace the file or rename the file based on the $replace parameter.
192  * - Adds the new file to the files database.
193  *
194  * @param \Drupal\file\FileInterface $source
195  *   A file entity.
196  * @param string $destination
197  *   A string containing the destination that $source should be moved
198  *   to. This must be a stream wrapper URI.
199  * @param int $replace
200  *   (optional) The replace behavior when the destination file already exists.
201  *   Possible values include:
202  *   - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
203  *     the destination name exists then its database entry will be updated and
204  *     $source->delete() called after invoking hook_file_move(). If no database
205  *     entry is found, then the source files record will be updated.
206  *   - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
207  *     filename is unique.
208  *   - FILE_EXISTS_ERROR: Do nothing and return FALSE.
209  *
210  * @return \Drupal\file\FileInterface|false
211  *   Resulting file entity for success, or FALSE in the event of an error.
212  *
213  * @see file_unmanaged_move()
214  * @see hook_file_move()
215  */
216 function file_move(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
217   if (!file_valid_uri($destination)) {
218     if (($realpath = drupal_realpath($source->getFileUri())) !== FALSE) {
219       \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]);
220     }
221     else {
222       \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]);
223     }
224     drupal_set_message(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()]), 'error');
225     return FALSE;
226   }
227
228   if ($uri = file_unmanaged_move($source->getFileUri(), $destination, $replace)) {
229     $delete_source = FALSE;
230
231     $file = clone $source;
232     $file->setFileUri($uri);
233     // If we are replacing an existing file re-use its database record.
234     if ($replace == FILE_EXISTS_REPLACE) {
235       $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
236       if (count($existing_files)) {
237         $existing = reset($existing_files);
238         $delete_source = TRUE;
239         $file->fid = $existing->id();
240         $file->uuid = $existing->uuid();
241       }
242     }
243     // If we are renaming around an existing file (rather than a directory),
244     // use its basename for the filename.
245     elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
246       $file->setFilename(drupal_basename($destination));
247     }
248
249     $file->save();
250
251     // Inform modules that the file has been moved.
252     \Drupal::moduleHandler()->invokeAll('file_move', [$file, $source]);
253
254     // Delete the original if it's not in use elsewhere.
255     if ($delete_source && !\Drupal::service('file.usage')->listUsage($source)) {
256       $source->delete();
257     }
258
259     return $file;
260   }
261   return FALSE;
262 }
263
264 /**
265  * Checks that a file meets the criteria specified by the validators.
266  *
267  * After executing the validator callbacks specified hook_file_validate() will
268  * also be called to allow other modules to report errors about the file.
269  *
270  * @param \Drupal\file\FileInterface $file
271  *   A file entity.
272  * @param array $validators
273  *   (optional) An associative array of callback functions used to validate
274  *   the file. The keys are function names and the values arrays of callback
275  *   parameters which will be passed in after the file entity. The functions
276  *   should return an array of error messages; an empty array indicates that
277  *   the file passed validation. The callback functions will be called in the
278  *   order specified in the array, then the hook hook_file_validate()
279  *   will be invoked so other modules can validate the new file.
280  *
281  * @return array
282  *   An array containing validation error messages.
283  *
284  * @see hook_file_validate()
285  */
286 function file_validate(FileInterface $file, $validators = []) {
287   // Call the validation functions specified by this function's caller.
288   $errors = [];
289   foreach ($validators as $function => $args) {
290     if (function_exists($function)) {
291       array_unshift($args, $file);
292       $errors = array_merge($errors, call_user_func_array($function, $args));
293     }
294   }
295
296   // Let other modules perform validation on the new file.
297   return array_merge($errors, \Drupal::moduleHandler()->invokeAll('file_validate', [$file]));
298 }
299
300 /**
301  * Checks for files with names longer than can be stored in the database.
302  *
303  * @param \Drupal\file\FileInterface $file
304  *   A file entity.
305  *
306  * @return array
307  *   An empty array if the file name length is smaller than the limit or an
308  *   array containing an error message if it's not or is empty.
309  */
310 function file_validate_name_length(FileInterface $file) {
311   $errors = [];
312
313   if (!$file->getFilename()) {
314     $errors[] = t("The file's name is empty. Please give a name to the file.");
315   }
316   if (strlen($file->getFilename()) > 240) {
317     $errors[] = t("The file's name exceeds the 240 characters limit. Please rename the file and try again.");
318   }
319   return $errors;
320 }
321
322 /**
323  * Checks that the filename ends with an allowed extension.
324  *
325  * @param \Drupal\file\FileInterface $file
326  *   A file entity.
327  * @param string $extensions
328  *   A string with a space separated list of allowed extensions.
329  *
330  * @return array
331  *   An empty array if the file extension is allowed or an array containing an
332  *   error message if it's not.
333  *
334  * @see hook_file_validate()
335  */
336 function file_validate_extensions(FileInterface $file, $extensions) {
337   $errors = [];
338
339   $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
340   if (!preg_match($regex, $file->getFilename())) {
341     $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => $extensions]);
342   }
343   return $errors;
344 }
345
346 /**
347  * Checks that the file's size is below certain limits.
348  *
349  * @param \Drupal\file\FileInterface $file
350  *   A file entity.
351  * @param int $file_limit
352  *   (optional) The maximum file size in bytes. Zero (the default) indicates
353  *   that no limit should be enforced.
354  * @param int $user_limit
355  *   (optional) The maximum number of bytes the user is allowed. Zero (the
356  *   default) indicates that no limit should be enforced.
357  *
358  * @return array
359  *   An empty array if the file size is below limits or an array containing an
360  *   error message if it's not.
361  *
362  * @see hook_file_validate()
363  */
364 function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit = 0) {
365   $user = \Drupal::currentUser();
366   $errors = [];
367
368   if ($file_limit && $file->getSize() > $file_limit) {
369     $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => format_size($file->getSize()), '%maxsize' => format_size($file_limit)]);
370   }
371
372   // Save a query by only calling spaceUsed() when a limit is provided.
373   if ($user_limit && (\Drupal::entityManager()->getStorage('file')->spaceUsed($user->id()) + $file->getSize()) > $user_limit) {
374     $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', ['%filesize' => format_size($file->getSize()), '%quota' => format_size($user_limit)]);
375   }
376
377   return $errors;
378 }
379
380 /**
381  * Checks that the file is recognized as a valid image.
382  *
383  * @param \Drupal\file\FileInterface $file
384  *   A file entity.
385  *
386  * @return array
387  *   An empty array if the file is a valid image or an array containing an error
388  *   message if it's not.
389  *
390  * @see hook_file_validate()
391  */
392 function file_validate_is_image(FileInterface $file) {
393   $errors = [];
394
395   $image_factory = \Drupal::service('image.factory');
396   $image = $image_factory->get($file->getFileUri());
397   if (!$image->isValid()) {
398     $supported_extensions = $image_factory->getSupportedExtensions();
399     $errors[] = t('Image type not supported. Allowed types: %types', ['%types' => implode(' ', $supported_extensions)]);
400   }
401
402   return $errors;
403 }
404
405 /**
406  * Verifies that image dimensions are within the specified maximum and minimum.
407  *
408  * Non-image files will be ignored. If an image toolkit is available the image
409  * will be scaled to fit within the desired maximum dimensions.
410  *
411  * @param \Drupal\file\FileInterface $file
412  *   A file entity. This function may resize the file affecting its size.
413  * @param string|int $maximum_dimensions
414  *   (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or
415  *   '85x85'. If an image toolkit is installed, the image will be resized down
416  *   to these dimensions. A value of zero (the default) indicates no restriction
417  *   on size, so no resizing will be attempted.
418  * @param string|int $minimum_dimensions
419  *   (optional) A string in the form WIDTHxHEIGHT. This will check that the
420  *   image meets a minimum size. A value of zero (the default) indicates that
421  *   there is no restriction on size.
422  *
423  * @return array
424  *   An empty array if the file meets the specified dimensions, was resized
425  *   successfully to meet those requirements or is not an image. If the image
426  *   does not meet the requirements or an attempt to resize it fails, an array
427  *   containing the error message will be returned.
428  *
429  * @see hook_file_validate()
430  */
431 function file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
432   $errors = [];
433
434   // Check first that the file is an image.
435   $image_factory = \Drupal::service('image.factory');
436   $image = $image_factory->get($file->getFileUri());
437   if ($image->isValid()) {
438     if ($maximum_dimensions) {
439       // Check that it is smaller than the given dimensions.
440       list($width, $height) = explode('x', $maximum_dimensions);
441       if ($image->getWidth() > $width || $image->getHeight() > $height) {
442         // Try to resize the image to fit the dimensions.
443         if ($image->scale($width, $height)) {
444           $image->save();
445           if (!empty($width) && !empty($height)) {
446             $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $maximum_dimensions]);
447           }
448           elseif (empty($width)) {
449             $message = t('The image was resized to fit within the maximum allowed height of %height pixels.', ['%height' => $height]);
450           }
451           elseif (empty($height)) {
452             $message = t('The image was resized to fit within the maximum allowed width of %width pixels.', ['%width' => $width]);
453           }
454           drupal_set_message($message);
455         }
456         else {
457           $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.');
458         }
459       }
460     }
461
462     if ($minimum_dimensions) {
463       // Check that it is larger than the given dimensions.
464       list($width, $height) = explode('x', $minimum_dimensions);
465       if ($image->getWidth() < $width || $image->getHeight() < $height) {
466         $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => $minimum_dimensions]);
467       }
468     }
469   }
470
471   return $errors;
472 }
473
474 /**
475  * Saves a file to the specified destination and creates a database entry.
476  *
477  * @param string $data
478  *   A string containing the contents of the file.
479  * @param string|null $destination
480  *   (optional) A string containing the destination URI. This must be a stream
481  *   wrapper URI. If no value or NULL is provided, a randomized name will be
482  *   generated and the file will be saved using Drupal's default files scheme,
483  *   usually "public://".
484  * @param int $replace
485  *   (optional) The replace behavior when the destination file already exists.
486  *   Possible values include:
487  *   - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
488  *     the destination name exists, then its database entry will be updated. If
489  *     no database entry is found, then a new one will be created.
490  *   - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
491  *     filename is unique.
492  *   - FILE_EXISTS_ERROR: Do nothing and return FALSE.
493  *
494  * @return \Drupal\file\FileInterface|false
495  *   A file entity, or FALSE on error.
496  *
497  * @see file_unmanaged_save_data()
498  */
499 function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
500   $user = \Drupal::currentUser();
501
502   if (empty($destination)) {
503     $destination = file_default_scheme() . '://';
504   }
505   if (!file_valid_uri($destination)) {
506     \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]);
507     drupal_set_message(t('The data could not be saved because the destination is invalid. More information is available in the system log.'), 'error');
508     return FALSE;
509   }
510
511   if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
512     // Create a file entity.
513     $file = File::create([
514       'uri' => $uri,
515       'uid' => $user->id(),
516       'status' => FILE_STATUS_PERMANENT,
517     ]);
518     // If we are replacing an existing file re-use its database record.
519     // @todo Do not create a new entity in order to update it. See
520     //   https://www.drupal.org/node/2241865.
521     if ($replace == FILE_EXISTS_REPLACE) {
522       $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
523       if (count($existing_files)) {
524         $existing = reset($existing_files);
525         $file->fid = $existing->id();
526         $file->setOriginalId($existing->id());
527         $file->setFilename($existing->getFilename());
528       }
529     }
530     // If we are renaming around an existing file (rather than a directory),
531     // use its basename for the filename.
532     elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
533       $file->setFilename(drupal_basename($destination));
534     }
535
536     $file->save();
537     return $file;
538   }
539   return FALSE;
540 }
541
542 /**
543  * Examines a file entity and returns appropriate content headers for download.
544  *
545  * @param \Drupal\file\FileInterface $file
546  *   A file entity.
547  *
548  * @return array
549  *   An associative array of headers, as expected by
550  *   \Symfony\Component\HttpFoundation\StreamedResponse.
551  */
552 function file_get_content_headers(FileInterface $file) {
553   $type = Unicode::mimeHeaderEncode($file->getMimeType());
554
555   return [
556     'Content-Type' => $type,
557     'Content-Length' => $file->getSize(),
558     'Cache-Control' => 'private',
559   ];
560 }
561
562 /**
563  * Implements hook_theme().
564  */
565 function file_theme() {
566   return [
567     // From file.module.
568     'file_link' => [
569       'variables' => ['file' => NULL, 'description' => NULL, 'attributes' => []],
570     ],
571     'file_managed_file' => [
572       'render element' => 'element',
573     ],
574
575     // From file.field.inc.
576     'file_widget_multiple' => [
577       'render element' => 'element',
578       'file' => 'file.field.inc',
579     ],
580     'file_upload_help' => [
581       'variables' => ['description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL],
582       'file' => 'file.field.inc',
583     ],
584   ];
585 }
586
587 /**
588  * Implements hook_file_download().
589  */
590 function file_file_download($uri) {
591   // Get the file record based on the URI. If not in the database just return.
592   /** @var \Drupal\file\FileInterface[] $files */
593   $files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
594   if (count($files)) {
595     foreach ($files as $item) {
596       // Since some database servers sometimes use a case-insensitive comparison
597       // by default, double check that the filename is an exact match.
598       if ($item->getFileUri() === $uri) {
599         $file = $item;
600         break;
601       }
602     }
603   }
604   if (!isset($file)) {
605     return;
606   }
607
608   // Find out if a temporary file is still used in the system.
609   if ($file->isTemporary()) {
610     $usage = \Drupal::service('file.usage')->listUsage($file);
611     if (empty($usage) && $file->getOwnerId() != \Drupal::currentUser()->id()) {
612       // Deny access to temporary files without usage that are not owned by the
613       // same user. This prevents the security issue that a private file that
614       // was protected by field permissions becomes available after its usage
615       // was removed and before it is actually deleted from the file system.
616       // Modules that depend on this behavior should make the file permanent
617       // instead.
618       return -1;
619     }
620   }
621
622   // Find out which (if any) fields of this type contain the file.
623   $references = file_get_file_references($file, NULL, EntityStorageInterface::FIELD_LOAD_CURRENT, NULL);
624
625   // Stop processing if there are no references in order to avoid returning
626   // headers for files controlled by other modules. Make an exception for
627   // temporary files where the host entity has not yet been saved (for example,
628   // an image preview on a node/add form) in which case, allow download by the
629   // file's owner.
630   if (empty($references) && ($file->isPermanent() || $file->getOwnerId() != \Drupal::currentUser()->id())) {
631     return;
632   }
633
634   if (!$file->access('download')) {
635     return -1;
636   }
637
638   // Access is granted.
639   $headers = file_get_content_headers($file);
640   return $headers;
641 }
642
643 /**
644  * Implements hook_cron().
645  */
646 function file_cron() {
647   $age = \Drupal::config('system.file')->get('temporary_maximum_age');
648   $file_storage = \Drupal::entityManager()->getStorage('file');
649
650   // Only delete temporary files if older than $age. Note that automatic cleanup
651   // is disabled if $age set to 0.
652   if ($age) {
653     $fids = Drupal::entityQuery('file')
654       ->condition('status', FILE_STATUS_PERMANENT, '<>')
655       ->condition('changed', REQUEST_TIME - $age, '<')
656       ->range(0, 100)
657       ->execute();
658     $files = $file_storage->loadMultiple($fids);
659     foreach ($files as $file) {
660       $references = \Drupal::service('file.usage')->listUsage($file);
661       if (empty($references)) {
662         if (file_exists($file->getFileUri())) {
663           $file->delete();
664         }
665         else {
666           \Drupal::logger('file system')->error('Could not delete temporary file "%path" during garbage collection', ['%path' => $file->getFileUri()]);
667         }
668       }
669       else {
670         \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))]);
671       }
672     }
673   }
674 }
675
676 /**
677  * Saves file uploads to a new location.
678  *
679  * The files will be added to the {file_managed} table as temporary files.
680  * Temporary files are periodically cleaned. Use the 'file.usage' service to
681  * register the usage of the file which will automatically mark it as permanent.
682  *
683  * @param string $form_field_name
684  *   A string that is the associative array key of the upload form element in
685  *   the form array.
686  * @param array $validators
687  *   (optional) An associative array of callback functions used to validate the
688  *   file. See file_validate() for a full discussion of the array format.
689  *   If the array is empty, it will be set up to call file_validate_extensions()
690  *   with a safe list of extensions, as follows: "jpg jpeg gif png txt doc
691  *   xls pdf ppt pps odt ods odp". To allow all extensions, you must explicitly
692  *   set this array to ['file_validate_extensions' => '']. (Beware: this is not
693  *   safe and should only be allowed for trusted users, if at all.)
694  * @param string|false $destination
695  *   (optional) A string containing the URI that the file should be copied to.
696  *   This must be a stream wrapper URI. If this value is omitted or set to
697  *   FALSE, Drupal's temporary files scheme will be used ("temporary://").
698  * @param null|int $delta
699  *   (optional) The delta of the file to return the file entity.
700  *   Defaults to NULL.
701  * @param int $replace
702  *   (optional) The replace behavior when the destination file already exists.
703  *   Possible values include:
704  *   - FILE_EXISTS_REPLACE: Replace the existing file.
705  *   - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
706  *     filename is unique.
707  *   - FILE_EXISTS_ERROR: Do nothing and return FALSE.
708  *
709  * @return array|\Drupal\file\FileInterface|null|false
710  *   An array of file entities or a single file entity if $delta != NULL. Each
711  *   array element contains the file entity if the upload succeeded or FALSE if
712  *   there was an error. Function returns NULL if no file was uploaded.
713  */
714 function file_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
715   $user = \Drupal::currentUser();
716   static $upload_cache;
717
718   $all_files = \Drupal::request()->files->get('files', []);
719   // Make sure there's an upload to process.
720   if (empty($all_files[$form_field_name])) {
721     return NULL;
722   }
723   $file_upload = $all_files[$form_field_name];
724
725   // Return cached objects without processing since the file will have
726   // already been processed and the paths in $_FILES will be invalid.
727   if (isset($upload_cache[$form_field_name])) {
728     if (isset($delta)) {
729       return $upload_cache[$form_field_name][$delta];
730     }
731     return $upload_cache[$form_field_name];
732   }
733
734   // Prepare uploaded files info. Representation is slightly different
735   // for multiple uploads and we fix that here.
736   $uploaded_files = $file_upload;
737   if (!is_array($file_upload)) {
738     $uploaded_files = [$file_upload];
739   }
740
741   $files = [];
742   foreach ($uploaded_files as $i => $file_info) {
743     // Check for file upload errors and return FALSE for this file if a lower
744     // level system error occurred. For a complete list of errors:
745     // See http://php.net/manual/features.file-upload.errors.php.
746     switch ($file_info->getError()) {
747       case UPLOAD_ERR_INI_SIZE:
748       case UPLOAD_ERR_FORM_SIZE:
749         drupal_set_message(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => format_size(file_upload_max_size())]), 'error');
750         $files[$i] = FALSE;
751         continue;
752
753       case UPLOAD_ERR_PARTIAL:
754       case UPLOAD_ERR_NO_FILE:
755         drupal_set_message(t('The file %file could not be saved because the upload did not complete.', ['%file' => $file_info->getFilename()]), 'error');
756         $files[$i] = FALSE;
757         continue;
758
759       case UPLOAD_ERR_OK:
760         // Final check that this is a valid upload, if it isn't, use the
761         // default error handler.
762         if (is_uploaded_file($file_info->getRealPath())) {
763           break;
764         }
765
766         // Unknown error
767       default:
768         drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', ['%file' => $file_info->getFilename()]), 'error');
769         $files[$i] = FALSE;
770         continue;
771
772     }
773     // Begin building file entity.
774     $values = [
775       'uid' => $user->id(),
776       'status' => 0,
777       'filename' => $file_info->getClientOriginalName(),
778       'uri' => $file_info->getRealPath(),
779       'filesize' => $file_info->getSize(),
780     ];
781     $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['filename']);
782     $file = File::create($values);
783
784     $extensions = '';
785     if (isset($validators['file_validate_extensions'])) {
786       if (isset($validators['file_validate_extensions'][0])) {
787         // Build the list of non-munged extensions if the caller provided them.
788         $extensions = $validators['file_validate_extensions'][0];
789       }
790       else {
791         // If 'file_validate_extensions' is set and the list is empty then the
792         // caller wants to allow any extension. In this case we have to remove the
793         // validator or else it will reject all extensions.
794         unset($validators['file_validate_extensions']);
795       }
796     }
797     else {
798       // No validator was provided, so add one using the default list.
799       // Build a default non-munged safe list for file_munge_filename().
800       $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
801       $validators['file_validate_extensions'] = [];
802       $validators['file_validate_extensions'][0] = $extensions;
803     }
804
805     if (!empty($extensions)) {
806       // Munge the filename to protect against possible malicious extension
807       // hiding within an unknown file type (ie: filename.html.foo).
808       $file->setFilename(file_munge_filename($file->getFilename(), $extensions));
809     }
810
811     // Rename potentially executable files, to help prevent exploits (i.e. will
812     // rename filename.php.foo and filename.php to filename.php.foo.txt and
813     // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads'
814     // evaluates to TRUE.
815     if (!\Drupal::config('system.file')->get('allow_insecure_uploads') && preg_match('/\.(php|pl|py|cgi|asp|js)(\.|$)/i', $file->getFilename()) && (substr($file->getFilename(), -4) != '.txt')) {
816       $file->setMimeType('text/plain');
817       // The destination filename will also later be used to create the URI.
818       $file->setFilename($file->getFilename() . '.txt');
819       // The .txt extension may not be in the allowed list of extensions. We have
820       // to add it here or else the file upload will fail.
821       if (!empty($extensions)) {
822         $validators['file_validate_extensions'][0] .= ' txt';
823         drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]));
824       }
825     }
826
827     // If the destination is not provided, use the temporary directory.
828     if (empty($destination)) {
829       $destination = 'temporary://';
830     }
831
832     // Assert that the destination contains a valid stream.
833     $destination_scheme = file_uri_scheme($destination);
834     if (!file_stream_wrapper_valid_scheme($destination_scheme)) {
835       drupal_set_message(t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]), 'error');
836       $files[$i] = FALSE;
837       continue;
838     }
839
840     $file->source = $form_field_name;
841     // A file URI may already have a trailing slash or look like "public://".
842     if (substr($destination, -1) != '/') {
843       $destination .= '/';
844     }
845     $file->destination = file_destination($destination . $file->getFilename(), $replace);
846     // If file_destination() returns FALSE then $replace === FILE_EXISTS_ERROR and
847     // there's an existing file so we need to bail.
848     if ($file->destination === FALSE) {
849       drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', ['%source' => $form_field_name, '%directory' => $destination]), 'error');
850       $files[$i] = FALSE;
851       continue;
852     }
853
854     // Add in our check of the file name length.
855     $validators['file_validate_name_length'] = [];
856
857     // Call the validation functions specified by this function's caller.
858     $errors = file_validate($file, $validators);
859
860     // Check for errors.
861     if (!empty($errors)) {
862       $message = [
863         'error' => [
864           '#markup' => t('The specified file %name could not be uploaded.', ['%name' => $file->getFilename()]),
865         ],
866         'item_list' => [
867           '#theme' => 'item_list',
868           '#items' => $errors,
869         ],
870       ];
871       // @todo Add support for render arrays in drupal_set_message()? See
872       //  https://www.drupal.org/node/2505497.
873       drupal_set_message(\Drupal::service('renderer')->renderPlain($message), 'error');
874       $files[$i] = FALSE;
875       continue;
876     }
877
878     // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
879     // directory. This overcomes open_basedir restrictions for future file
880     // operations.
881     $file->setFileUri($file->destination);
882     if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) {
883       drupal_set_message(t('File upload error. Could not move uploaded file.'), 'error');
884       \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', ['%file' => $file->getFilename(), '%destination' => $file->getFileUri()]);
885       $files[$i] = FALSE;
886       continue;
887     }
888
889     // Set the permissions on the new file.
890     drupal_chmod($file->getFileUri());
891
892     // If we are replacing an existing file re-use its database record.
893     // @todo Do not create a new entity in order to update it. See
894     //   https://www.drupal.org/node/2241865.
895     if ($replace == FILE_EXISTS_REPLACE) {
896       $existing_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]);
897       if (count($existing_files)) {
898         $existing = reset($existing_files);
899         $file->fid = $existing->id();
900         $file->setOriginalId($existing->id());
901       }
902     }
903
904     // If we made it this far it's safe to record this file in the database.
905     $file->save();
906     $files[$i] = $file;
907     // Allow an anonymous user who creates a non-public file to see it. See
908     // \Drupal\file\FileAccessControlHandler::checkAccess().
909     if ($user->isAnonymous() && $destination_scheme !== 'public') {
910       $session = \Drupal::request()->getSession();
911       $allowed_temp_files = $session->get('anonymous_allowed_file_ids', []);
912       $allowed_temp_files[$file->id()] = $file->id();
913       $session->set('anonymous_allowed_file_ids', $allowed_temp_files);
914     }
915   }
916
917   // Add files to the cache.
918   $upload_cache[$form_field_name] = $files;
919
920   return isset($delta) ? $files[$delta] : $files;
921 }
922
923 /**
924  * Determines the preferred upload progress implementation.
925  *
926  * @return string|false
927  *   A string indicating which upload progress system is available. Either "apc"
928  *   or "uploadprogress". If neither are available, returns FALSE.
929  */
930 function file_progress_implementation() {
931   static $implementation;
932   if (!isset($implementation)) {
933     $implementation = FALSE;
934
935     // We prefer the PECL extension uploadprogress because it supports multiple
936     // simultaneous uploads. APCu only supports one at a time.
937     if (extension_loaded('uploadprogress')) {
938       $implementation = 'uploadprogress';
939     }
940     elseif (version_compare(PHP_VERSION, '7', '<') && extension_loaded('apc') && ini_get('apc.rfc1867')) {
941       $implementation = 'apc';
942     }
943   }
944   return $implementation;
945 }
946
947 /**
948  * Implements hook_ENTITY_TYPE_predelete() for file entities.
949  */
950 function file_file_predelete(File $file) {
951   // @todo Remove references to a file that is in-use.
952 }
953
954 /**
955  * Implements hook_tokens().
956  */
957 function file_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
958   $token_service = \Drupal::token();
959
960   $url_options = ['absolute' => TRUE];
961   if (isset($options['langcode'])) {
962     $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
963     $langcode = $options['langcode'];
964   }
965   else {
966     $langcode = NULL;
967   }
968
969   $replacements = [];
970
971   if ($type == 'file' && !empty($data['file'])) {
972     /** @var \Drupal\file\FileInterface $file */
973     $file = $data['file'];
974
975     foreach ($tokens as $name => $original) {
976       switch ($name) {
977         // Basic keys and values.
978         case 'fid':
979           $replacements[$original] = $file->id();
980           break;
981
982         // Essential file data
983         case 'name':
984           $replacements[$original] = $file->getFilename();
985           break;
986
987         case 'path':
988           $replacements[$original] = $file->getFileUri();
989           break;
990
991         case 'mime':
992           $replacements[$original] = $file->getMimeType();
993           break;
994
995         case 'size':
996           $replacements[$original] = format_size($file->getSize());
997           break;
998
999         case 'url':
1000           // Ideally, this would use file_url_transform_relative(), but because
1001           // tokens are also often used in e-mails, it's better to keep absolute
1002           // file URLs. The 'url.site' cache context is associated to ensure the
1003           // correct absolute URL is used in case of a multisite setup.
1004           $replacements[$original] = file_create_url($file->getFileUri());
1005           $bubbleable_metadata->addCacheContexts(['url.site']);
1006           break;
1007
1008         // These tokens are default variations on the chained tokens handled below.
1009         case 'created':
1010           $date_format = DateFormat::load('medium');
1011           $bubbleable_metadata->addCacheableDependency($date_format);
1012           $replacements[$original] = format_date($file->getCreatedTime(), 'medium', '', NULL, $langcode);
1013           break;
1014
1015         case 'changed':
1016           $date_format = DateFormat::load('medium');
1017           $bubbleable_metadata = $bubbleable_metadata->addCacheableDependency($date_format);
1018           $replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode);
1019           break;
1020
1021         case 'owner':
1022           $owner = $file->getOwner();
1023           $bubbleable_metadata->addCacheableDependency($owner);
1024           $name = $owner->label();
1025           $replacements[$original] = $name;
1026           break;
1027       }
1028     }
1029
1030     if ($date_tokens = $token_service->findWithPrefix($tokens, 'created')) {
1031       $replacements += $token_service->generate('date', $date_tokens, ['date' => $file->getCreatedTime()], $options, $bubbleable_metadata);
1032     }
1033
1034     if ($date_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
1035       $replacements += $token_service->generate('date', $date_tokens, ['date' => $file->getChangedTime()], $options, $bubbleable_metadata);
1036     }
1037
1038     if (($owner_tokens = $token_service->findWithPrefix($tokens, 'owner')) && $file->getOwner()) {
1039       $replacements += $token_service->generate('user', $owner_tokens, ['user' => $file->getOwner()], $options, $bubbleable_metadata);
1040     }
1041   }
1042
1043   return $replacements;
1044 }
1045
1046 /**
1047  * Implements hook_token_info().
1048  */
1049 function file_token_info() {
1050   $types['file'] = [
1051     'name' => t("Files"),
1052     'description' => t("Tokens related to uploaded files."),
1053     'needs-data' => 'file',
1054   ];
1055
1056   // File related tokens.
1057   $file['fid'] = [
1058     'name' => t("File ID"),
1059     'description' => t("The unique ID of the uploaded file."),
1060   ];
1061   $file['name'] = [
1062     'name' => t("File name"),
1063     'description' => t("The name of the file on disk."),
1064   ];
1065   $file['path'] = [
1066     'name' => t("Path"),
1067     'description' => t("The location of the file relative to Drupal root."),
1068   ];
1069   $file['mime'] = [
1070     'name' => t("MIME type"),
1071     'description' => t("The MIME type of the file."),
1072   ];
1073   $file['size'] = [
1074     'name' => t("File size"),
1075     'description' => t("The size of the file."),
1076   ];
1077   $file['url'] = [
1078     'name' => t("URL"),
1079     'description' => t("The web-accessible URL for the file."),
1080   ];
1081   $file['created'] = [
1082     'name' => t("Created"),
1083     'description' => t("The date the file created."),
1084     'type' => 'date',
1085   ];
1086   $file['changed'] = [
1087     'name' => t("Changed"),
1088     'description' => t("The date the file was most recently changed."),
1089     'type' => 'date',
1090   ];
1091   $file['owner'] = [
1092     'name' => t("Owner"),
1093     'description' => t("The user who originally uploaded the file."),
1094     'type' => 'user',
1095   ];
1096
1097   return [
1098     'types' => $types,
1099     'tokens' => [
1100       'file' => $file,
1101     ],
1102   ];
1103 }
1104
1105 /**
1106  * Form submission handler for upload / remove buttons of managed_file elements.
1107  *
1108  * @see \Drupal\file\Element\ManagedFile::processManagedFile()
1109  */
1110 function file_managed_file_submit($form, FormStateInterface $form_state) {
1111   // Determine whether it was the upload or the remove button that was clicked,
1112   // and set $element to the managed_file element that contains that button.
1113   $parents = $form_state->getTriggeringElement()['#array_parents'];
1114   $button_key = array_pop($parents);
1115   $element = NestedArray::getValue($form, $parents);
1116
1117   // No action is needed here for the upload button, because all file uploads on
1118   // the form are processed by \Drupal\file\Element\ManagedFile::valueCallback()
1119   // regardless of which button was clicked. Action is needed here for the
1120   // remove button, because we only remove a file in response to its remove
1121   // button being clicked.
1122   if ($button_key == 'remove_button') {
1123     $fids = array_keys($element['#files']);
1124     // Get files that will be removed.
1125     if ($element['#multiple']) {
1126       $remove_fids = [];
1127       foreach (Element::children($element) as $name) {
1128         if (strpos($name, 'file_') === 0 && $element[$name]['selected']['#value']) {
1129           $remove_fids[] = (int) substr($name, 5);
1130         }
1131       }
1132       $fids = array_diff($fids, $remove_fids);
1133     }
1134     else {
1135       // If we deal with single upload element remove the file and set
1136       // element's value to empty array (file could not be removed from
1137       // element if we don't do that).
1138       $remove_fids = $fids;
1139       $fids = [];
1140     }
1141
1142     foreach ($remove_fids as $fid) {
1143       // If it's a temporary file we can safely remove it immediately, otherwise
1144       // it's up to the implementing module to remove usages of files to have them
1145       // removed.
1146       if ($element['#files'][$fid] && $element['#files'][$fid]->isTemporary()) {
1147         $element['#files'][$fid]->delete();
1148       }
1149     }
1150     // Update both $form_state->getValues() and FormState::$input to reflect
1151     // that the file has been removed, so that the form is rebuilt correctly.
1152     // $form_state->getValues() must be updated in case additional submit
1153     // handlers run, and for form building functions that run during the
1154     // rebuild, such as when the managed_file element is part of a field widget.
1155     // FormState::$input must be updated so that
1156     // \Drupal\file\Element\ManagedFile::valueCallback() has correct information
1157     // during the rebuild.
1158     $form_state->setValueForElement($element['fids'], implode(' ', $fids));
1159     NestedArray::setValue($form_state->getUserInput(), $element['fids']['#parents'], implode(' ', $fids));
1160   }
1161
1162   // Set the form to rebuild so that $form is correctly updated in response to
1163   // processing the file removal. Since this function did not change $form_state
1164   // if the upload button was clicked, a rebuild isn't necessary in that
1165   // situation and calling $form_state->disableRedirect() would suffice.
1166   // However, we choose to always rebuild, to keep the form processing workflow
1167   // consistent between the two buttons.
1168   $form_state->setRebuild();
1169 }
1170
1171 /**
1172  * Saves any files that have been uploaded into a managed_file element.
1173  *
1174  * @param array $element
1175  *   The FAPI element whose values are being saved.
1176  * @param \Drupal\Core\Form\FormStateInterface $form_state
1177  *   The current state of the form.
1178  *
1179  * @return array|false
1180  *   An array of file entities for each file that was saved, keyed by its file
1181  *   ID. Each array element contains a file entity. Function returns FALSE if
1182  *   upload directory could not be created or no files were uploaded.
1183  */
1184 function file_managed_file_save_upload($element, FormStateInterface $form_state) {
1185   $upload_name = implode('_', $element['#parents']);
1186   $all_files = \Drupal::request()->files->get('files', []);
1187   if (empty($all_files[$upload_name])) {
1188     return FALSE;
1189   }
1190   $file_upload = $all_files[$upload_name];
1191
1192   $destination = isset($element['#upload_location']) ? $element['#upload_location'] : NULL;
1193   if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
1194     \Drupal::logger('file')->notice('The upload directory %directory for the file field %name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', ['%directory' => $destination, '%name' => $element['#field_name']]);
1195     $form_state->setError($element, t('The file could not be uploaded.'));
1196     return FALSE;
1197   }
1198
1199   // Save attached files to the database.
1200   $files_uploaded = $element['#multiple'] && count(array_filter($file_upload)) > 0;
1201   $files_uploaded |= !$element['#multiple'] && !empty($file_upload);
1202   if ($files_uploaded) {
1203     if (!$files = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
1204       \Drupal::logger('file')->notice('The file upload failed. %upload', ['%upload' => $upload_name]);
1205       $form_state->setError($element, t('Files in the @name field were unable to be uploaded.', ['@name' => $element['#title']]));
1206       return [];
1207     }
1208
1209     // Value callback expects FIDs to be keys.
1210     $files = array_filter($files);
1211     $fids = array_map(function ($file) {
1212       return $file->id();
1213     }, $files);
1214
1215     return empty($files) ? [] : array_combine($fids, $files);
1216   }
1217
1218   return [];
1219 }
1220
1221 /**
1222  * Prepares variables for file form widget templates.
1223  *
1224  * Default template: file-managed-file.html.twig.
1225  *
1226  * @param array $variables
1227  *   An associative array containing:
1228  *   - element: A render element representing the file.
1229  */
1230 function template_preprocess_file_managed_file(&$variables) {
1231   $element = $variables['element'];
1232
1233   $variables['attributes'] = [];
1234   if (isset($element['#id'])) {
1235     $variables['attributes']['id'] = $element['#id'];
1236   }
1237   if (!empty($element['#attributes']['class'])) {
1238     $variables['attributes']['class'] = (array) $element['#attributes']['class'];
1239   }
1240 }
1241
1242 /**
1243  * Prepares variables for file link templates.
1244  *
1245  * Default template: file-link.html.twig.
1246  *
1247  * @param array $variables
1248  *   An associative array containing:
1249  *   - file: A file object to which the link will be created.
1250  *   - icon_directory: (optional) A path to a directory of icons to be used for
1251  *     files. Defaults to the value of the "icon.directory" variable.
1252  *   - description: A description to be displayed instead of the filename.
1253  *   - attributes: An associative array of attributes to be placed in the a tag.
1254  */
1255 function template_preprocess_file_link(&$variables) {
1256   $file = $variables['file'];
1257   $options = [];
1258
1259   $file_entity = ($file instanceof File) ? $file : File::load($file->fid);
1260   // @todo Wrap in file_url_transform_relative(). This is currently
1261   // impossible. As a work-around, we currently add the 'url.site' cache context
1262   // to ensure different file URLs are generated for different sites in a
1263   // multisite setup, including HTTP and HTTPS versions of the same site.
1264   // Fix in https://www.drupal.org/node/2646744.
1265   $url = file_create_url($file_entity->getFileUri());
1266   $variables['#cache']['contexts'][] = 'url.site';
1267
1268   $mime_type = $file->getMimeType();
1269   // Set options as per anchor format described at
1270   // http://microformats.org/wiki/file-format-examples
1271   $options['attributes']['type'] = $mime_type . '; length=' . $file->getSize();
1272
1273   // Use the description as the link text if available.
1274   if (empty($variables['description'])) {
1275     $link_text = $file_entity->getFilename();
1276   }
1277   else {
1278     $link_text = $variables['description'];
1279     $options['attributes']['title'] = $file_entity->getFilename();
1280   }
1281
1282   // Classes to add to the file field for icons.
1283   $classes = [
1284     'file',
1285     // Add a specific class for each and every mime type.
1286     'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
1287     // Add a more general class for groups of well known MIME types.
1288     'file--' . file_icon_class($mime_type),
1289   ];
1290
1291   // Set file classes to the options array.
1292   $variables['attributes'] = new Attribute($variables['attributes']);
1293   $variables['attributes']->addClass($classes);
1294
1295   $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options));
1296 }
1297
1298 /**
1299  * Gets a class for the icon for a MIME type.
1300  *
1301  * @param string $mime_type
1302  *   A MIME type.
1303  *
1304  * @return string
1305  *   A class associated with the file.
1306  */
1307 function file_icon_class($mime_type) {
1308   // Search for a group with the files MIME type.
1309   $generic_mime = (string) file_icon_map($mime_type);
1310   if (!empty($generic_mime)) {
1311     return $generic_mime;
1312   }
1313
1314   // Use generic icons for each category that provides such icons.
1315   foreach (['audio', 'image', 'text', 'video'] as $category) {
1316     if (strpos($mime_type, $category) === 0) {
1317       return $category;
1318     }
1319   }
1320
1321   // If there's no generic icon for the type the general class.
1322   return 'general';
1323 }
1324
1325 /**
1326  * Determines the generic icon MIME package based on a file's MIME type.
1327  *
1328  * @param string $mime_type
1329  *   A MIME type.
1330  *
1331  * @return string|false
1332  *   The generic icon MIME package expected for this file.
1333  */
1334 function file_icon_map($mime_type) {
1335   switch ($mime_type) {
1336     // Word document types.
1337     case 'application/msword':
1338     case 'application/vnd.ms-word.document.macroEnabled.12':
1339     case 'application/vnd.oasis.opendocument.text':
1340     case 'application/vnd.oasis.opendocument.text-template':
1341     case 'application/vnd.oasis.opendocument.text-master':
1342     case 'application/vnd.oasis.opendocument.text-web':
1343     case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
1344     case 'application/vnd.stardivision.writer':
1345     case 'application/vnd.sun.xml.writer':
1346     case 'application/vnd.sun.xml.writer.template':
1347     case 'application/vnd.sun.xml.writer.global':
1348     case 'application/vnd.wordperfect':
1349     case 'application/x-abiword':
1350     case 'application/x-applix-word':
1351     case 'application/x-kword':
1352     case 'application/x-kword-crypt':
1353       return 'x-office-document';
1354
1355     // Spreadsheet document types.
1356     case 'application/vnd.ms-excel':
1357     case 'application/vnd.ms-excel.sheet.macroEnabled.12':
1358     case 'application/vnd.oasis.opendocument.spreadsheet':
1359     case 'application/vnd.oasis.opendocument.spreadsheet-template':
1360     case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
1361     case 'application/vnd.stardivision.calc':
1362     case 'application/vnd.sun.xml.calc':
1363     case 'application/vnd.sun.xml.calc.template':
1364     case 'application/vnd.lotus-1-2-3':
1365     case 'application/x-applix-spreadsheet':
1366     case 'application/x-gnumeric':
1367     case 'application/x-kspread':
1368     case 'application/x-kspread-crypt':
1369       return 'x-office-spreadsheet';
1370
1371     // Presentation document types.
1372     case 'application/vnd.ms-powerpoint':
1373     case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
1374     case 'application/vnd.oasis.opendocument.presentation':
1375     case 'application/vnd.oasis.opendocument.presentation-template':
1376     case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
1377     case 'application/vnd.stardivision.impress':
1378     case 'application/vnd.sun.xml.impress':
1379     case 'application/vnd.sun.xml.impress.template':
1380     case 'application/x-kpresenter':
1381       return 'x-office-presentation';
1382
1383     // Compressed archive types.
1384     case 'application/zip':
1385     case 'application/x-zip':
1386     case 'application/stuffit':
1387     case 'application/x-stuffit':
1388     case 'application/x-7z-compressed':
1389     case 'application/x-ace':
1390     case 'application/x-arj':
1391     case 'application/x-bzip':
1392     case 'application/x-bzip-compressed-tar':
1393     case 'application/x-compress':
1394     case 'application/x-compressed-tar':
1395     case 'application/x-cpio-compressed':
1396     case 'application/x-deb':
1397     case 'application/x-gzip':
1398     case 'application/x-java-archive':
1399     case 'application/x-lha':
1400     case 'application/x-lhz':
1401     case 'application/x-lzop':
1402     case 'application/x-rar':
1403     case 'application/x-rpm':
1404     case 'application/x-tzo':
1405     case 'application/x-tar':
1406     case 'application/x-tarz':
1407     case 'application/x-tgz':
1408       return 'package-x-generic';
1409
1410     // Script file types.
1411     case 'application/ecmascript':
1412     case 'application/javascript':
1413     case 'application/mathematica':
1414     case 'application/vnd.mozilla.xul+xml':
1415     case 'application/x-asp':
1416     case 'application/x-awk':
1417     case 'application/x-cgi':
1418     case 'application/x-csh':
1419     case 'application/x-m4':
1420     case 'application/x-perl':
1421     case 'application/x-php':
1422     case 'application/x-ruby':
1423     case 'application/x-shellscript':
1424     case 'text/vnd.wap.wmlscript':
1425     case 'text/x-emacs-lisp':
1426     case 'text/x-haskell':
1427     case 'text/x-literate-haskell':
1428     case 'text/x-lua':
1429     case 'text/x-makefile':
1430     case 'text/x-matlab':
1431     case 'text/x-python':
1432     case 'text/x-sql':
1433     case 'text/x-tcl':
1434       return 'text-x-script';
1435
1436     // HTML aliases.
1437     case 'application/xhtml+xml':
1438       return 'text-html';
1439
1440     // Executable types.
1441     case 'application/x-macbinary':
1442     case 'application/x-ms-dos-executable':
1443     case 'application/x-pef-executable':
1444       return 'application-x-executable';
1445
1446     // Acrobat types
1447     case 'application/pdf':
1448     case 'application/x-pdf':
1449     case 'applications/vnd.pdf':
1450     case 'text/pdf':
1451     case 'text/x-pdf':
1452       return 'application-pdf';
1453
1454     default:
1455       return FALSE;
1456   }
1457 }
1458
1459 /**
1460  * Retrieves a list of references to a file.
1461  *
1462  * @param \Drupal\file\FileInterface $file
1463  *   A file entity.
1464  * @param \Drupal\Core\Field\FieldDefinitionInterface|null $field
1465  *   (optional) A field definition to be used for this check. If given,
1466  *   limits the reference check to the given field. Defaults to NULL.
1467  * @param int $age
1468  *   (optional) A constant that specifies which references to count. Use
1469  *   EntityStorageInterface::FIELD_LOAD_REVISION (the default) to retrieve all
1470  *   references within all revisions or
1471  *   EntityStorageInterface::FIELD_LOAD_CURRENT to retrieve references only in
1472  *   the current revisions of all entities that have references to this file.
1473  * @param string $field_type
1474  *   (optional) The name of a field type. If given, limits the reference check
1475  *   to fields of the given type. If both $field and $field_type are given but
1476  *   $field is not the same type as $field_type, an empty array will be
1477  *   returned. Defaults to 'file'.
1478  *
1479  * @return array
1480  *   A multidimensional array. The keys are field_name, entity_type,
1481  *   entity_id and the value is an entity referencing this file.
1482  *
1483  * @ingroup file
1484  */
1485 function file_get_file_references(FileInterface $file, FieldDefinitionInterface $field = NULL, $age = EntityStorageInterface::FIELD_LOAD_REVISION, $field_type = 'file') {
1486   $references = &drupal_static(__FUNCTION__, []);
1487   $field_columns = &drupal_static(__FUNCTION__ . ':field_columns', []);
1488
1489   // Fill the static cache, disregard $field and $field_type for now.
1490   if (!isset($references[$file->id()][$age])) {
1491     $references[$file->id()][$age] = [];
1492     $usage_list = \Drupal::service('file.usage')->listUsage($file);
1493     $file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : [];
1494     foreach ($file_usage_list as $entity_type_id => $entity_ids) {
1495       $entities = \Drupal::entityTypeManager()
1496         ->getStorage($entity_type_id)->loadMultiple(array_keys($entity_ids));
1497       foreach ($entities as $entity) {
1498         $bundle = $entity->bundle();
1499         // We need to find file fields for this entity type and bundle.
1500         if (!isset($file_fields[$entity_type_id][$bundle])) {
1501           $file_fields[$entity_type_id][$bundle] = [];
1502           // This contains the possible field names.
1503           foreach ($entity->getFieldDefinitions() as $field_name => $field_definition) {
1504             // If this is the first time this field type is seen, check
1505             // whether it references files.
1506             if (!isset($field_columns[$field_definition->getType()])) {
1507               $field_columns[$field_definition->getType()] = file_field_find_file_reference_column($field_definition);
1508             }
1509             // If the field type does reference files then record it.
1510             if ($field_columns[$field_definition->getType()]) {
1511               $file_fields[$entity_type_id][$bundle][$field_name] = $field_columns[$field_definition->getType()];
1512             }
1513           }
1514         }
1515         foreach ($file_fields[$entity_type_id][$bundle] as $field_name => $field_column) {
1516           // Iterate over the field items to find the referenced file and field
1517           // name. This will fail if the usage checked is in a non-current
1518           // revision because field items are from the current
1519           // revision.
1520           // We also iterate over all translations because a file can be linked
1521           // to a language other than the default.
1522           foreach ($entity->getTranslationLanguages() as $langcode => $language) {
1523             foreach ($entity->getTranslation($langcode)->get($field_name) as $item) {
1524               if ($file->id() == $item->{$field_column}) {
1525                 $references[$file->id()][$age][$field_name][$entity_type_id][$entity->id()] = $entity;
1526                 break;
1527               }
1528             }
1529           }
1530         }
1531       }
1532     }
1533   }
1534   $return = $references[$file->id()][$age];
1535   // Filter the static cache down to the requested entries. The usual static
1536   // cache is very small so this will be very fast.
1537   if ($field || $field_type) {
1538     foreach ($return as $field_name => $data) {
1539       foreach (array_keys($data) as $entity_type_id) {
1540         $field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type_id);
1541         $current_field = $field_storage_definitions[$field_name];
1542         if (($field_type && $current_field->getType() != $field_type) || ($field && $field->uuid() != $current_field->uuid())) {
1543           unset($return[$field_name][$entity_type_id]);
1544         }
1545       }
1546     }
1547   }
1548   return $return;
1549 }
1550
1551 /**
1552  * Formats human-readable version of file status.
1553  *
1554  * @param int|null $choice
1555  *   (optional) An integer status code. If not set, all statuses are returned.
1556  *   Defaults to NULL.
1557  *
1558  * @return \Drupal\Core\StringTranslation\TranslatableMarkup|\Drupal\Core\StringTranslation\TranslatableMarkup[]
1559  *   An array of file statuses or a specified status if $choice is set.
1560  */
1561 function _views_file_status($choice = NULL) {
1562   $status = [
1563     0 => t('Temporary'),
1564     FILE_STATUS_PERMANENT => t('Permanent'),
1565   ];
1566
1567   if (isset($choice)) {
1568     return isset($status[$choice]) ? $status[$choice] : t('Unknown');
1569   }
1570
1571   return $status;
1572 }