Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / media / media.install
1 <?php
2
3 /**
4  * @file
5  * Install, uninstall and update hooks for Media module.
6  */
7
8 use Drupal\Core\Url;
9 use Drupal\media\MediaTypeInterface;
10 use Drupal\media\Plugin\media\Source\OEmbedInterface;
11 use Drupal\user\RoleInterface;
12 use Drupal\user\Entity\Role;
13
14 /**
15  * Implements hook_install().
16  */
17 function media_install() {
18   $source = drupal_get_path('module', 'media') . '/images/icons';
19   $destination = \Drupal::config('media.settings')->get('icon_base_uri');
20   file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
21
22   $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
23   foreach ($files as $file) {
24     // When reinstalling the media module we don't want to copy the icons when
25     // they already exist. The icons could be replaced (by a contrib module or
26     // manually), so we don't want to replace the existing files. Removing the
27     // files when we uninstall could also be a problem if the files are
28     // referenced somewhere else. Since showing an error that it was not
29     // possible to copy the files is also confusing, we silently do nothing.
30     if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
31       file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR);
32     }
33   }
34
35   // Grant the "view media" permission to all users by default.
36   if (\Drupal::moduleHandler()->moduleExists('user')) {
37     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']);
38     user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']);
39   }
40 }
41
42 /**
43  * Implements hook_requirements().
44  */
45 function media_requirements($phase) {
46   $requirements = [];
47   if ($phase == 'install') {
48     $destination = 'public://media-icons/generic';
49     file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
50     $is_writable = is_writable($destination);
51     $is_directory = is_dir($destination);
52     if (!$is_writable || !$is_directory) {
53       if (!$is_directory) {
54         $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
55       }
56       else {
57         $error = t('The directory %directory is not writable.', ['%directory' => $destination]);
58       }
59       $description = t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [':handbook_url' => 'https://www.drupal.org/server-permissions']);
60       if (!empty($error)) {
61         $description = $error . ' ' . $description;
62         $requirements['media']['description'] = $description;
63         $requirements['media']['severity'] = REQUIREMENT_ERROR;
64       }
65     }
66
67     // Prevent installation if the 1.x branch of the contrib module is enabled.
68     if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
69       $info = system_get_info('module', 'media_entity');
70       if (version_compare($info['version'], '8.x-2') < 0) {
71         $requirements['media_module_incompatibility'] = [
72           'title' => t('Media'),
73           'description' => t('The Media module is not compatible with contrib <a href=":url">Media Entity</a> 1.x branch. Please check the 2.x branch of that module for an upgrade path.', [
74             ':url' => 'https://drupal.org/project/media_entity',
75           ]),
76           'severity' => REQUIREMENT_ERROR,
77         ];
78       }
79     }
80   }
81   elseif ($phase === 'runtime') {
82     // Check that oEmbed content is served in an iframe on a different domain,
83     // and complain if it isn't.
84     $domain = \Drupal::config('media.settings')->get('iframe_domain');
85
86     if (!\Drupal::service('media.oembed.iframe_url_helper')->isSecure($domain)) {
87       // Find all media types which use a source plugin that implements
88       // OEmbedInterface.
89       $media_types = \Drupal::entityTypeManager()
90         ->getStorage('media_type')
91         ->loadMultiple();
92
93       $oembed_types = array_filter($media_types, function (MediaTypeInterface $media_type) {
94         return $media_type->getSource() instanceof OEmbedInterface;
95       });
96
97       if ($oembed_types) {
98         // @todo Potentially allow site administrators to suppress this warning
99         // permanently. See https://www.drupal.org/project/drupal/issues/2962753
100         // for more information.
101         $requirements['media_insecure_iframe'] = [
102           'title' => t('Media'),
103           'description' => t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. <a href=":url">You can specify a different domain for serving oEmbed content here</a>.', [
104             ':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
105           ]),
106           'severity' => REQUIREMENT_WARNING,
107         ];
108       }
109     }
110   }
111
112   return $requirements;
113 }
114
115 /**
116  * Introduce per-bundle permissions.
117  */
118 function media_update_8500() {
119   $media_types = \Drupal::entityQuery('media_type')->execute();
120
121   /** @var \Drupal\user\RoleInterface $role */
122   foreach (Role::loadMultiple() as $role) {
123     if ($role->hasPermission('update media')) {
124       foreach ($media_types as $media_type) {
125         $role->grantPermission("edit own $media_type media");
126       }
127     }
128
129     if ($role->hasPermission('update any media')) {
130       foreach ($media_types as $media_type) {
131         $role->grantPermission("edit any $media_type media");
132       }
133     }
134
135     if ($role->hasPermission('delete media')) {
136       foreach ($media_types as $media_type) {
137         $role->grantPermission("delete own $media_type media");
138       }
139     }
140
141     if ($role->hasPermission('delete any media')) {
142       foreach ($media_types as $media_type) {
143         $role->grantPermission("delete any $media_type media");
144       }
145     }
146
147     if ($role->hasPermission('create media')) {
148       foreach ($media_types as $media_type) {
149         $role->grantPermission("create $media_type media");
150       }
151     }
152
153     $role->save();
154   }
155 }
156
157 /**
158  * Updates media.settings to support OEmbed.
159  */
160 function media_update_8600() {
161   \Drupal::configFactory()->getEditable('media.settings')
162     ->set('iframe_domain', '')
163     ->set('oembed_providers_url', 'https://oembed.com/providers.json')
164     ->save(TRUE);
165 }