file.api.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Hooks related to the File management system.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Control access to private file downloads and specify HTTP headers.
  12. *
  13. * This hook allows modules to enforce permissions on file downloads whenever
  14. * Drupal is handling file download, as opposed to the web server bypassing
  15. * Drupal and returning the file from a public directory. Modules can also
  16. * provide headers to specify information like the file's name or MIME type.
  17. *
  18. * @param $uri
  19. * The URI of the file.
  20. * @return
  21. * If the user does not have permission to access the file, return -1. If the
  22. * user has permission, return an array with the appropriate headers. If the
  23. * file is not controlled by the current module, the return value should be
  24. * NULL.
  25. *
  26. * @see \Drupal\system\FileDownloadController::download()
  27. */
  28. function hook_file_download($uri) {
  29. // Check to see if this is a config download.
  30. $scheme = file_uri_scheme($uri);
  31. $target = file_uri_target($uri);
  32. if ($scheme == 'temporary' && $target == 'config.tar.gz') {
  33. return [
  34. 'Content-disposition' => 'attachment; filename="config.tar.gz"',
  35. ];
  36. }
  37. }
  38. /**
  39. * Alter the URL to a file.
  40. *
  41. * This hook is called from file_create_url(), and is called fairly
  42. * frequently (10+ times per page), depending on how many files there are in a
  43. * given page.
  44. * If CSS and JS aggregation are disabled, this can become very frequently
  45. * (50+ times per page) so performance is critical.
  46. *
  47. * This function should alter the URI, if it wants to rewrite the file URL.
  48. *
  49. * @param $uri
  50. * The URI to a file for which we need an external URL, or the path to a
  51. * shipped file.
  52. */
  53. function hook_file_url_alter(&$uri) {
  54. $user = \Drupal::currentUser();
  55. // User 1 will always see the local file in this example.
  56. if ($user->id() == 1) {
  57. return;
  58. }
  59. $cdn1 = 'http://cdn1.example.com';
  60. $cdn2 = 'http://cdn2.example.com';
  61. $cdn_extensions = ['css', 'js', 'gif', 'jpg', 'jpeg', 'png'];
  62. // Most CDNs don't support private file transfers without a lot of hassle,
  63. // so don't support this in the common case.
  64. $schemes = ['public'];
  65. $scheme = file_uri_scheme($uri);
  66. // Only serve shipped files and public created files from the CDN.
  67. if (!$scheme || in_array($scheme, $schemes)) {
  68. // Shipped files.
  69. if (!$scheme) {
  70. $path = $uri;
  71. }
  72. // Public created files.
  73. else {
  74. $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
  75. $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
  76. }
  77. // Clean up Windows paths.
  78. $path = str_replace('\\', '/', $path);
  79. // Serve files with one of the CDN extensions from CDN 1, all others from
  80. // CDN 2.
  81. $pathinfo = pathinfo($path);
  82. if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], $cdn_extensions)) {
  83. $uri = $cdn1 . '/' . $path;
  84. }
  85. else {
  86. $uri = $cdn2 . '/' . $path;
  87. }
  88. }
  89. }
  90. /**
  91. * Alter MIME type mappings used to determine MIME type from a file extension.
  92. *
  93. * Invoked by \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess(). It
  94. * is used to allow modules to add to or modify the default mapping from
  95. * \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::$defaultMapping.
  96. *
  97. * @param $mapping
  98. * An array of mimetypes correlated to the extensions that relate to them.
  99. * The array has 'mimetypes' and 'extensions' elements, each of which is an
  100. * array.
  101. *
  102. * @see \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess()
  103. * @see \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::$defaultMapping
  104. */
  105. function hook_file_mimetype_mapping_alter(&$mapping) {
  106. // Add new MIME type 'drupal/info'.
  107. $mapping['mimetypes']['example_info'] = 'drupal/info';
  108. // Add new extension '.info.yml' and map it to the 'drupal/info' MIME type.
  109. $mapping['extensions']['info'] = 'example_info';
  110. // Override existing extension mapping for '.ogg' files.
  111. $mapping['extensions']['ogg'] = 189;
  112. }
  113. /**
  114. * Alter archiver information declared by other modules.
  115. *
  116. * See hook_archiver_info() for a description of archivers and the archiver
  117. * information structure.
  118. *
  119. * @param $info
  120. * Archiver information to alter (return values from hook_archiver_info()).
  121. */
  122. function hook_archiver_info_alter(&$info) {
  123. $info['tar']['extensions'][] = 'tgz';
  124. }
  125. /**
  126. * Register information about FileTransfer classes provided by a module.
  127. *
  128. * The FileTransfer class allows transferring files over a specific type of
  129. * connection. Core provides classes for FTP and SSH. Contributed modules are
  130. * free to extend the FileTransfer base class to add other connection types,
  131. * and if these classes are registered via hook_filetransfer_info(), those
  132. * connection types will be available to site administrators using the Update
  133. * manager when they are redirected to the authorize.php script to authorize
  134. * the file operations.
  135. *
  136. * @return array
  137. * Nested array of information about FileTransfer classes. Each key is a
  138. * FileTransfer type (not human readable, used for form elements and
  139. * variable names, etc), and the values are subarrays that define properties
  140. * of that type. The keys in each subarray are:
  141. * - 'title': Required. The human-readable name of the connection type.
  142. * - 'class': Required. The name of the FileTransfer class. The constructor
  143. * will always be passed the full path to the root of the site that should
  144. * be used to restrict where file transfer operations can occur (the $jail)
  145. * and an array of settings values returned by the settings form.
  146. * - 'weight': Optional. Integer weight used for sorting connection types on
  147. * the authorize.php form.
  148. *
  149. * @see \Drupal\Core\FileTransfer\FileTransfer
  150. * @see authorize.php
  151. * @see hook_filetransfer_info_alter()
  152. * @see drupal_get_filetransfer_info()
  153. */
  154. function hook_filetransfer_info() {
  155. $info['sftp'] = [
  156. 'title' => t('SFTP (Secure FTP)'),
  157. 'class' => 'Drupal\Core\FileTransfer\SFTP',
  158. 'weight' => 10,
  159. ];
  160. return $info;
  161. }
  162. /**
  163. * Alter the FileTransfer class registry.
  164. *
  165. * @param array $filetransfer_info
  166. * Reference to a nested array containing information about the FileTransfer
  167. * class registry.
  168. *
  169. * @see hook_filetransfer_info()
  170. */
  171. function hook_filetransfer_info_alter(&$filetransfer_info) {
  172. // Remove the FTP option entirely.
  173. unset($filetransfer_info['ftp']);
  174. // Make sure the SSH option is listed first.
  175. $filetransfer_info['ssh']['weight'] = -10;
  176. }
  177. /**
  178. * @} End of "addtogroup hooks".
  179. */