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