file.module 62 KB

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