Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Handles updates of Drupal core and contributed projects.
|
Chris@0
|
6 *
|
Chris@0
|
7 * The module checks for available updates of Drupal core and any installed
|
Chris@0
|
8 * contributed modules and themes. It warns site administrators if newer
|
Chris@0
|
9 * releases are available via the system status report (admin/reports/status),
|
Chris@0
|
10 * the module and theme pages, and optionally via email. It also provides the
|
Chris@0
|
11 * ability to install contributed modules and themes via an user interface.
|
Chris@0
|
12 */
|
Chris@0
|
13
|
Chris@18
|
14 use Drupal\Core\File\Exception\FileException;
|
Chris@0
|
15 use Drupal\Core\Url;
|
Chris@0
|
16 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
17 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
18 use Drupal\Core\Site\Settings;
|
Chris@0
|
19
|
Chris@0
|
20 // These are internally used constants for this code, do not modify.
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Project is missing security update(s).
|
Chris@0
|
24 *
|
Chris@0
|
25 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
26 * Use \Drupal\update\UpdateManagerInterface::NOT_SECURE instead.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
29 */
|
Chris@0
|
30 const UPDATE_NOT_SECURE = 1;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Current release has been unpublished and is no longer available.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
36 * Use \Drupal\update\UpdateManagerInterface::REVOKED instead.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
39 */
|
Chris@0
|
40 const UPDATE_REVOKED = 2;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Current release is no longer supported by the project maintainer.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
46 * Use \Drupal\update\UpdateManagerInterface::NOT_SUPPORTED instead.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
49 */
|
Chris@0
|
50 const UPDATE_NOT_SUPPORTED = 3;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Project has a new release available, but it is not a security release.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
56 * Use \Drupal\update\UpdateManagerInterface::NOT_CURRENT instead.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
59 */
|
Chris@0
|
60 const UPDATE_NOT_CURRENT = 4;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Project is up to date.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
66 * Use \Drupal\update\UpdateManagerInterface::CURRENT instead.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
69 */
|
Chris@0
|
70 const UPDATE_CURRENT = 5;
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Project's status cannot be checked.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
76 * Use \Drupal\update\UpdateFetcherInterface::NOT_CHECKED instead.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
79 */
|
Chris@0
|
80 const UPDATE_NOT_CHECKED = -1;
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * No available update data was found for project.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
86 * Use \Drupal\update\UpdateFetcherInterface::UNKNOWN instead.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
89 */
|
Chris@0
|
90 const UPDATE_UNKNOWN = -2;
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * There was a failure fetching available update data for this project.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
96 * Use \Drupal\update\UpdateFetcherInterface::NOT_FETCHED instead.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
99 */
|
Chris@0
|
100 const UPDATE_NOT_FETCHED = -3;
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * We need to (re)fetch available update data for this project.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
|
Chris@0
|
106 * Use \Drupal\update\UpdateFetcherInterface::FETCH_PENDING instead.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @see https://www.drupal.org/node/2831620
|
Chris@0
|
109 */
|
Chris@0
|
110 const UPDATE_FETCH_PENDING = -4;
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Implements hook_help().
|
Chris@0
|
114 */
|
Chris@0
|
115 function update_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
116 switch ($route_name) {
|
Chris@0
|
117 case 'help.page.update':
|
Chris@0
|
118 $output = '';
|
Chris@0
|
119 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@18
|
120 $output .= '<p>' . t('The Update Manager module periodically checks for new versions of your site\'s software (including contributed modules and themes), and alerts administrators to available updates. The Update Manager system is also used by some other modules to manage updates and downloads; for example, the Interface Translation module uses the Update Manager to download translations from the localization server. Note that whenever the Update Manager system is used, anonymous usage statistics are sent to Drupal.org. If desired, you may disable the Update Manager module from the <a href=":modules">Extend page</a>; if you do so, functionality that depends on the Update Manager system will not work. For more information, see the <a href=":update">online documentation for the Update Manager module</a>.', [':update' => 'https://www.drupal.org/documentation/modules/update', ':modules' => Url::fromRoute('system.modules_list')->toString()]) . '</p>';
|
Chris@0
|
121 // Only explain the Update manager if it has not been disabled.
|
Chris@0
|
122 if (_update_manager_access()) {
|
Chris@0
|
123 $output .= '<p>' . t('The Update Manager also allows administrators to update and install modules and themes through the administration interface.') . '</p>';
|
Chris@0
|
124 }
|
Chris@0
|
125 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
126 $output .= '<dl>';
|
Chris@0
|
127 $output .= '<dt>' . t('Checking for available updates') . '</dt>';
|
Chris@18
|
128 $output .= '<dd>' . t('The <a href=":update-report">Available updates report</a> displays core, contributed modules, and themes for which there are new releases available for download. On the report page, you can also check manually for updates. You can configure the frequency of update checks, which are performed during cron runs, and whether notifications are sent on the <a href=":update-settings">Update Manager settings page</a>.', [':update-report' => Url::fromRoute('update.status')->toString(), ':update-settings' => Url::fromRoute('update.settings')->toString()]) . '</dd>';
|
Chris@0
|
129 // Only explain the Update manager if it has not been disabled.
|
Chris@0
|
130 if (_update_manager_access()) {
|
Chris@0
|
131 $output .= '<dt>' . t('Performing updates through the Update page') . '</dt>';
|
Chris@18
|
132 $output .= '<dd>' . t('The Update Manager module allows administrators to perform updates directly from the <a href=":update-page">Update page</a>. It lists all available updates, and you can confirm whether you want to download them. If you don\'t have sufficient access rights to your web server, you could be prompted for your FTP/SSH password. Afterwards the files are transferred into your site installation, overwriting your old files. Direct links to the Update page are also displayed on the <a href=":modules_page">Extend page</a> and the <a href=":themes_page">Appearance page</a>.', [':modules_page' => Url::fromRoute('system.modules_list')->toString(), ':themes_page' => Url::fromRoute('system.themes_page')->toString(), ':update-page' => Url::fromRoute('update.report_update')->toString()]) . '</dd>';
|
Chris@0
|
133 $output .= '<dt>' . t('Installing new modules and themes through the Install page') . '</dt>';
|
Chris@18
|
134 $output .= '<dd>' . t('You can also install new modules and themes in the same fashion, through the <a href=":install">Install page</a>, or by clicking the <em>Install new module/theme</em> links at the top of the <a href=":modules_page">Extend page</a> and the <a href=":themes_page">Appearance page</a>. In this case, you are prompted to provide either the URL to the download, or to upload a packaged release file from your local computer.', [':modules_page' => Url::fromRoute('system.modules_list')->toString(), ':themes_page' => Url::fromRoute('system.themes_page')->toString(), ':install' => Url::fromRoute('update.report_install')->toString()]) . '</dd>';
|
Chris@0
|
135 }
|
Chris@0
|
136 $output .= '</dl>';
|
Chris@0
|
137 return $output;
|
Chris@0
|
138
|
Chris@0
|
139 case 'update.status':
|
Chris@0
|
140 return '<p>' . t('Here you can find information about available updates for your installed modules and themes. Note that each module or theme is part of a "project", which may or may not have the same name, and might include multiple modules or themes within it.') . '</p>';
|
Chris@0
|
141
|
Chris@0
|
142 case 'system.modules_list':
|
Chris@0
|
143 if (_update_manager_access()) {
|
Chris@18
|
144 $output = '<p>' . t('Regularly review and install <a href=":updates">available updates</a> to maintain a secure and current site. Always run the <a href=":update-php">update script</a> each time a module is updated.', [':update-php' => Url::fromRoute('system.db_update')->toString(), ':updates' => Url::fromRoute('update.status')->toString()]) . '</p>';
|
Chris@0
|
145 }
|
Chris@0
|
146 else {
|
Chris@18
|
147 $output = '<p>' . t('Regularly review <a href=":updates">available updates</a> to maintain a secure and current site. Always run the <a href=":update-php">update script</a> each time a module is updated.', [':update-php' => Url::fromRoute('system.db_update')->toString(), ':updates' => Url::fromRoute('update.status')->toString()]) . '</p>';
|
Chris@0
|
148 }
|
Chris@0
|
149 return $output;
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * Implements hook_page_top().
|
Chris@0
|
155 */
|
Chris@0
|
156 function update_page_top() {
|
Chris@0
|
157 /** @var \Drupal\Core\Routing\AdminContext $admin_context */
|
Chris@0
|
158 $admin_context = \Drupal::service('router.admin_context');
|
Chris@0
|
159 $route_match = \Drupal::routeMatch();
|
Chris@0
|
160 if ($admin_context->isAdminRoute($route_match->getRouteObject()) && \Drupal::currentUser()->hasPermission('administer site configuration')) {
|
Chris@0
|
161 $route_name = \Drupal::routeMatch()->getRouteName();
|
Chris@0
|
162 switch ($route_name) {
|
Chris@0
|
163 // These pages don't need additional nagging.
|
Chris@0
|
164 case 'update.theme_update':
|
Chris@0
|
165 case 'system.theme_install':
|
Chris@0
|
166 case 'update.module_update':
|
Chris@0
|
167 case 'update.module_install':
|
Chris@0
|
168 case 'update.status':
|
Chris@0
|
169 case 'update.report_update':
|
Chris@0
|
170 case 'update.report_install':
|
Chris@0
|
171 case 'update.settings':
|
Chris@0
|
172 case 'system.status':
|
Chris@0
|
173 case 'update.confirmation_page':
|
Chris@0
|
174 return;
|
Chris@0
|
175
|
Chris@0
|
176 // If we are on the appearance or modules list, display a detailed report
|
Chris@0
|
177 // of the update status.
|
Chris@0
|
178 case 'system.themes_page':
|
Chris@0
|
179 case 'system.modules_list':
|
Chris@0
|
180 $verbose = TRUE;
|
Chris@0
|
181 break;
|
Chris@0
|
182
|
Chris@0
|
183 }
|
Chris@0
|
184 module_load_install('update');
|
Chris@0
|
185 $status = update_requirements('runtime');
|
Chris@0
|
186 foreach (['core', 'contrib'] as $report_type) {
|
Chris@0
|
187 $type = 'update_' . $report_type;
|
Chris@0
|
188 // hook_requirements() supports render arrays therefore we need to render
|
Chris@17
|
189 // them before using
|
Chris@17
|
190 // \Drupal\Core\Messenger\MessengerInterface::addStatus().
|
Chris@0
|
191 if (isset($status[$type]['description']) && is_array($status[$type]['description'])) {
|
Chris@0
|
192 $status[$type]['description'] = \Drupal::service('renderer')->renderPlain($status[$type]['description']);
|
Chris@0
|
193 }
|
Chris@0
|
194 if (!empty($verbose)) {
|
Chris@0
|
195 if (isset($status[$type]['severity'])) {
|
Chris@0
|
196 if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
|
Chris@17
|
197 \Drupal::messenger()->addError($status[$type]['description']);
|
Chris@0
|
198 }
|
Chris@0
|
199 elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
|
Chris@17
|
200 \Drupal::messenger()->addWarning($status[$type]['description']);
|
Chris@0
|
201 }
|
Chris@0
|
202 }
|
Chris@0
|
203 }
|
Chris@0
|
204 // Otherwise, if we're on *any* admin page and there's a security
|
Chris@0
|
205 // update missing, print an error message about it.
|
Chris@0
|
206 else {
|
Chris@0
|
207 if (isset($status[$type])
|
Chris@0
|
208 && isset($status[$type]['reason'])
|
Chris@0
|
209 && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
|
Chris@17
|
210 \Drupal::messenger()->addError($status[$type]['description']);
|
Chris@0
|
211 }
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * Resolves if the current user can access updater menu items.
|
Chris@0
|
219 *
|
Chris@0
|
220 * It both enforces the 'administer software updates' permission and the global
|
Chris@0
|
221 * kill switch for the authorize.php script.
|
Chris@0
|
222 *
|
Chris@0
|
223 * @return
|
Chris@0
|
224 * TRUE if the current user can access the updater menu items; FALSE
|
Chris@0
|
225 * otherwise.
|
Chris@0
|
226 */
|
Chris@0
|
227 function _update_manager_access() {
|
Chris@0
|
228 return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 /**
|
Chris@0
|
232 * Implements hook_theme().
|
Chris@0
|
233 */
|
Chris@0
|
234 function update_theme() {
|
Chris@0
|
235 return [
|
Chris@0
|
236 'update_last_check' => [
|
Chris@0
|
237 'variables' => ['last' => 0],
|
Chris@0
|
238 ],
|
Chris@0
|
239 'update_report' => [
|
Chris@0
|
240 'variables' => ['data' => NULL],
|
Chris@0
|
241 'file' => 'update.report.inc',
|
Chris@0
|
242 ],
|
Chris@0
|
243 'update_project_status' => [
|
Chris@0
|
244 'variables' => ['project' => []],
|
Chris@0
|
245 'file' => 'update.report.inc',
|
Chris@0
|
246 ],
|
Chris@0
|
247 // We are using template instead of '#type' => 'table' here to keep markup
|
Chris@0
|
248 // out of preprocess and allow for easier changes to markup.
|
Chris@0
|
249 'update_version' => [
|
Chris@0
|
250 'variables' => ['version' => NULL, 'title' => NULL, 'attributes' => []],
|
Chris@0
|
251 'file' => 'update.report.inc',
|
Chris@0
|
252 ],
|
Chris@0
|
253 ];
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 /**
|
Chris@0
|
257 * Implements hook_cron().
|
Chris@0
|
258 */
|
Chris@0
|
259 function update_cron() {
|
Chris@0
|
260 $update_config = \Drupal::config('update.settings');
|
Chris@0
|
261 $frequency = $update_config->get('check.interval_days');
|
Chris@0
|
262 $interval = 60 * 60 * 24 * $frequency;
|
Chris@0
|
263 $last_check = \Drupal::state()->get('update.last_check') ?: 0;
|
Chris@0
|
264 if ((REQUEST_TIME - $last_check) > $interval) {
|
Chris@0
|
265 // If the configured update interval has elapsed, we want to invalidate
|
Chris@0
|
266 // the data for all projects, attempt to re-fetch, and trigger any
|
Chris@0
|
267 // configured notifications about the new status.
|
Chris@0
|
268 update_refresh();
|
Chris@0
|
269 update_fetch_data();
|
Chris@0
|
270 }
|
Chris@0
|
271 else {
|
Chris@0
|
272 // Otherwise, see if any individual projects are now stale or still
|
Chris@0
|
273 // missing data, and if so, try to fetch the data.
|
Chris@0
|
274 update_get_available(TRUE);
|
Chris@0
|
275 }
|
Chris@0
|
276 $last_email_notice = \Drupal::state()->get('update.last_email_notification') ?: 0;
|
Chris@0
|
277 if ((REQUEST_TIME - $last_email_notice) > $interval) {
|
Chris@0
|
278 // If configured time between notifications elapsed, send email about
|
Chris@0
|
279 // updates possibly available.
|
Chris@0
|
280 module_load_include('inc', 'update', 'update.fetch');
|
Chris@0
|
281 _update_cron_notify();
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 // Clear garbage from disk.
|
Chris@0
|
285 update_clear_update_disk_cache();
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 /**
|
Chris@0
|
289 * Implements hook_themes_installed().
|
Chris@0
|
290 *
|
Chris@0
|
291 * If themes are installed, we invalidate the information of available updates.
|
Chris@0
|
292 */
|
Chris@0
|
293 function update_themes_installed($themes) {
|
Chris@0
|
294 // Clear all update module data.
|
Chris@0
|
295 update_storage_clear();
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 /**
|
Chris@0
|
299 * Implements hook_themes_uninstalled().
|
Chris@0
|
300 *
|
Chris@0
|
301 * If themes are uninstalled, we invalidate the information of available updates.
|
Chris@0
|
302 */
|
Chris@0
|
303 function update_themes_uninstalled($themes) {
|
Chris@0
|
304 // Clear all update module data.
|
Chris@0
|
305 update_storage_clear();
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 /**
|
Chris@0
|
309 * Implements hook_form_FORM_ID_alter() for system_modules().
|
Chris@0
|
310 *
|
Chris@0
|
311 * Adds a form submission handler to the system modules form, so that if a site
|
Chris@0
|
312 * admin saves the form, we invalidate the information of available updates.
|
Chris@0
|
313 *
|
Chris@0
|
314 * @see _update_cache_clear()
|
Chris@0
|
315 */
|
Chris@0
|
316 function update_form_system_modules_alter(&$form, FormStateInterface $form_state) {
|
Chris@0
|
317 $form['#submit'][] = 'update_storage_clear_submit';
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 /**
|
Chris@0
|
321 * Form submission handler for system_modules().
|
Chris@0
|
322 *
|
Chris@0
|
323 * @see update_form_system_modules_alter()
|
Chris@0
|
324 */
|
Chris@0
|
325 function update_storage_clear_submit($form, FormStateInterface $form_state) {
|
Chris@0
|
326 // Clear all update module data.
|
Chris@0
|
327 update_storage_clear();
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 /**
|
Chris@0
|
331 * Returns a warning message when there is no data about available updates.
|
Chris@0
|
332 */
|
Chris@0
|
333 function _update_no_data() {
|
Chris@0
|
334 $destination = \Drupal::destination()->getAsArray();
|
Chris@0
|
335 return t('No update information available. <a href=":run_cron">Run cron</a> or <a href=":check_manually">check manually</a>.', [
|
Chris@18
|
336 ':run_cron' => Url::fromRoute('system.run_cron', [], ['query' => $destination])->toString(),
|
Chris@18
|
337 ':check_manually' => Url::fromRoute('update.manual_status', [], ['query' => $destination])->toString(),
|
Chris@0
|
338 ]);
|
Chris@0
|
339 }
|
Chris@0
|
340
|
Chris@0
|
341 /**
|
Chris@0
|
342 * Tries to get update information and refreshes it when necessary.
|
Chris@0
|
343 *
|
Chris@0
|
344 * In addition to checking the lifetime, this function also ensures that
|
Chris@0
|
345 * there are no .info.yml files for enabled modules or themes that have a newer
|
Chris@0
|
346 * modification timestamp than the last time we checked for available update
|
Chris@0
|
347 * data. If any .info.yml file was modified, it almost certainly means a new
|
Chris@0
|
348 * version of something was installed. Without fresh available update data, the
|
Chris@0
|
349 * logic in update_calculate_project_data() will be wrong and produce confusing,
|
Chris@0
|
350 * bogus results.
|
Chris@0
|
351 *
|
Chris@0
|
352 * @param $refresh
|
Chris@0
|
353 * (optional) Boolean to indicate if this method should refresh automatically
|
Chris@0
|
354 * if there's no data. Defaults to FALSE.
|
Chris@0
|
355 *
|
Chris@0
|
356 * @return
|
Chris@0
|
357 * Array of data about available releases, keyed by project shortname.
|
Chris@0
|
358 *
|
Chris@0
|
359 * @see update_refresh()
|
Chris@0
|
360 * @see \Drupal\Update\UpdateManager::getProjects()
|
Chris@0
|
361 */
|
Chris@0
|
362 function update_get_available($refresh = FALSE) {
|
Chris@0
|
363 module_load_include('inc', 'update', 'update.compare');
|
Chris@0
|
364 $needs_refresh = FALSE;
|
Chris@0
|
365
|
Chris@0
|
366 // Grab whatever data we currently have.
|
Chris@0
|
367 $available = \Drupal::keyValueExpirable('update_available_releases')->getAll();
|
Chris@0
|
368 $projects = \Drupal::service('update.manager')->getProjects();
|
Chris@0
|
369 foreach ($projects as $key => $project) {
|
Chris@0
|
370 // If there's no data at all, we clearly need to fetch some.
|
Chris@0
|
371 if (empty($available[$key])) {
|
Chris@0
|
372 // update_create_fetch_task($project);
|
Chris@0
|
373 \Drupal::service('update.processor')->createFetchTask($project);
|
Chris@0
|
374 $needs_refresh = TRUE;
|
Chris@0
|
375 continue;
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378 // See if the .info.yml file is newer than the last time we checked for
|
Chris@0
|
379 // data, and if so, mark this project's data as needing to be re-fetched.
|
Chris@0
|
380 // Any time an admin upgrades their local installation, the .info.yml file
|
Chris@0
|
381 // will be changed, so this is the only way we can be sure we're not showing
|
Chris@0
|
382 // bogus information right after they upgrade.
|
Chris@0
|
383 if ($project['info']['_info_file_ctime'] > $available[$key]['last_fetch']) {
|
Chris@0
|
384 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 // If we have project data but no release data, we need to fetch. This
|
Chris@0
|
388 // can be triggered when we fail to contact a release history server.
|
Chris@0
|
389 if (empty($available[$key]['releases']) && !$available[$key]['last_fetch']) {
|
Chris@0
|
390 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393 // If we think this project needs to fetch, actually create the task now
|
Chris@0
|
394 // and remember that we think we're missing some data.
|
Chris@0
|
395 if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) {
|
Chris@0
|
396 \Drupal::service('update.processor')->createFetchTask($project);
|
Chris@0
|
397 $needs_refresh = TRUE;
|
Chris@0
|
398 }
|
Chris@0
|
399 }
|
Chris@0
|
400
|
Chris@0
|
401 if ($needs_refresh && $refresh) {
|
Chris@0
|
402 // Attempt to drain the queue of fetch tasks.
|
Chris@0
|
403 update_fetch_data();
|
Chris@0
|
404 // After processing the queue, we've (hopefully) got better data, so pull
|
Chris@0
|
405 // the latest data again and use that directly.
|
Chris@0
|
406 $available = \Drupal::keyValueExpirable('update_available_releases')->getAll();
|
Chris@0
|
407 }
|
Chris@0
|
408
|
Chris@0
|
409 return $available;
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 /**
|
Chris@17
|
413 * Identifies equivalent security releases with a hardcoded list.
|
Chris@17
|
414 *
|
Chris@17
|
415 * Generally, only the latest minor version of Drupal 8 is supported. However,
|
Chris@17
|
416 * when security fixes are backported to an old branch, and the site owner
|
Chris@17
|
417 * updates to the release containing the backported fix, they should not
|
Chris@17
|
418 * see "Security update required!" again if the only other security releases
|
Chris@17
|
419 * are releases for the same advisories.
|
Chris@17
|
420 *
|
Chris@17
|
421 * @return string[]
|
Chris@17
|
422 * A list of security release numbers that are equivalent to this release
|
Chris@17
|
423 * (i.e. covered by the same advisory), for backported security fixes only.
|
Chris@17
|
424 *
|
Chris@17
|
425 * @internal
|
Chris@17
|
426 *
|
Chris@17
|
427 * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use the
|
Chris@17
|
428 * 'Insecure' release type tag in update XML provided by Drupal.org to
|
Chris@17
|
429 * determine if releases are insecure.
|
Chris@17
|
430 */
|
Chris@17
|
431 function _update_equivalent_security_releases() {
|
Chris@17
|
432 trigger_error("_update_equivalent_security_releases() was a temporary fix and will be removed before 9.0.0. Use the 'Insecure' release type tag in update XML provided by Drupal.org to determine if releases are insecure.", E_USER_DEPRECATED);
|
Chris@17
|
433 switch (\Drupal::VERSION) {
|
Chris@17
|
434 case '8.3.8':
|
Chris@17
|
435 return ['8.4.5', '8.5.0-rc1'];
|
Chris@17
|
436 case '8.3.9':
|
Chris@17
|
437 return ['8.4.6', '8.5.1'];
|
Chris@17
|
438 case '8.4.5':
|
Chris@17
|
439 return ['8.5.0-rc1'];
|
Chris@17
|
440 case '8.4.6':
|
Chris@17
|
441 return ['8.5.1'];
|
Chris@17
|
442 case '8.4.7':
|
Chris@17
|
443 return ['8.5.2'];
|
Chris@17
|
444 case '8.4.8':
|
Chris@17
|
445 return ['8.5.3'];
|
Chris@17
|
446 }
|
Chris@17
|
447
|
Chris@17
|
448 return [];
|
Chris@17
|
449 }
|
Chris@17
|
450
|
Chris@17
|
451 /**
|
Chris@0
|
452 * Adds a task to the queue for fetching release history data for a project.
|
Chris@0
|
453 *
|
Chris@0
|
454 * We only create a new fetch task if there's no task already in the queue for
|
Chris@0
|
455 * this particular project (based on 'update_fetch_task' key-value collection).
|
Chris@0
|
456 *
|
Chris@0
|
457 * @param $project
|
Chris@0
|
458 * Associative array of information about a project as created by
|
Chris@0
|
459 * \Drupal\Update\UpdateManager::getProjects(), including keys such as 'name'
|
Chris@0
|
460 * (short name), and the 'info' array with data from a .info.yml file for the
|
Chris@0
|
461 * project.
|
Chris@0
|
462 *
|
Chris@0
|
463 * @see \Drupal\update\UpdateFetcher::createFetchTask()
|
Chris@0
|
464 */
|
Chris@0
|
465 function update_create_fetch_task($project) {
|
Chris@0
|
466 \Drupal::service('update.processor')->createFetchTask($project);
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 /**
|
Chris@0
|
470 * Refreshes the release data after loading the necessary include file.
|
Chris@0
|
471 */
|
Chris@0
|
472 function update_refresh() {
|
Chris@0
|
473 \Drupal::service('update.manager')->refreshUpdateData();
|
Chris@0
|
474 }
|
Chris@0
|
475
|
Chris@0
|
476 /**
|
Chris@0
|
477 * Attempts to fetch update data after loading the necessary include file.
|
Chris@0
|
478 *
|
Chris@0
|
479 * @see \Drupal\update\UpdateProcessor::fetchData()
|
Chris@0
|
480 */
|
Chris@0
|
481 function update_fetch_data() {
|
Chris@0
|
482 \Drupal::service('update.processor')->fetchData();
|
Chris@0
|
483 }
|
Chris@0
|
484
|
Chris@0
|
485 /**
|
Chris@0
|
486 * Batch callback: Performs actions when all fetch tasks have been completed.
|
Chris@0
|
487 *
|
Chris@0
|
488 * @param $success
|
Chris@0
|
489 * TRUE if the batch operation was successful; FALSE if there were errors.
|
Chris@0
|
490 * @param $results
|
Chris@0
|
491 * An associative array of results from the batch operation, including the key
|
Chris@0
|
492 * 'updated' which holds the total number of projects we fetched available
|
Chris@0
|
493 * update data for.
|
Chris@0
|
494 */
|
Chris@0
|
495 function update_fetch_data_finished($success, $results) {
|
Chris@0
|
496 if ($success) {
|
Chris@0
|
497 if (!empty($results)) {
|
Chris@0
|
498 if (!empty($results['updated'])) {
|
Chris@17
|
499 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.'));
|
Chris@0
|
500 }
|
Chris@0
|
501 if (!empty($results['failures'])) {
|
Chris@17
|
502 \Drupal::messenger()->addError(\Drupal::translation()->formatPlural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.'));
|
Chris@0
|
503 }
|
Chris@0
|
504 }
|
Chris@0
|
505 }
|
Chris@0
|
506 else {
|
Chris@17
|
507 \Drupal::messenger()->addError(t('An error occurred trying to get available update data.'), 'error');
|
Chris@0
|
508 }
|
Chris@0
|
509 }
|
Chris@0
|
510
|
Chris@0
|
511 /**
|
Chris@0
|
512 * Implements hook_mail().
|
Chris@0
|
513 *
|
Chris@0
|
514 * Constructs the email notification message when the site is out of date.
|
Chris@0
|
515 *
|
Chris@0
|
516 * @param $key
|
Chris@0
|
517 * Unique key to indicate what message to build, always 'status_notify'.
|
Chris@0
|
518 * @param $message
|
Chris@0
|
519 * Reference to the message array being built.
|
Chris@0
|
520 * @param $params
|
Chris@0
|
521 * Array of parameters to indicate what kind of text to include in the message
|
Chris@0
|
522 * body. This is a keyed array of message type ('core' or 'contrib') as the
|
Chris@0
|
523 * keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for the
|
Chris@0
|
524 * values.
|
Chris@0
|
525 *
|
Chris@0
|
526 * @see \Drupal\Core\Mail\MailManagerInterface::mail()
|
Chris@0
|
527 * @see _update_cron_notify()
|
Chris@0
|
528 * @see _update_message_text()
|
Chris@0
|
529 */
|
Chris@0
|
530 function update_mail($key, &$message, $params) {
|
Chris@0
|
531 $langcode = $message['langcode'];
|
Chris@0
|
532 $language = \Drupal::languageManager()->getLanguage($langcode);
|
Chris@0
|
533 $message['subject'] .= t('New release(s) available for @site_name', ['@site_name' => \Drupal::config('system.site')->get('name')], ['langcode' => $langcode]);
|
Chris@0
|
534 foreach ($params as $msg_type => $msg_reason) {
|
Chris@0
|
535 $message['body'][] = _update_message_text($msg_type, $msg_reason, $langcode);
|
Chris@0
|
536 }
|
Chris@18
|
537 $message['body'][] = t('See the available updates page for more information:', [], ['langcode' => $langcode]) . "\n" . Url::fromRoute('update.status', [], ['absolute' => TRUE, 'language' => $language])->toString();
|
Chris@0
|
538 if (_update_manager_access()) {
|
Chris@18
|
539 $message['body'][] = t('You can automatically install your missing updates using the Update manager:', [], ['langcode' => $langcode]) . "\n" . Url::fromRoute('update.report_update', [], ['absolute' => TRUE, 'language' => $language])->toString();
|
Chris@0
|
540 }
|
Chris@18
|
541 $settings_url = Url::fromRoute('update.settings', [], ['absolute' => TRUE])->toString();
|
Chris@0
|
542 if (\Drupal::config('update.settings')->get('notification.threshold') == 'all') {
|
Chris@0
|
543 $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, @url.', ['@url' => $settings_url]);
|
Chris@0
|
544 }
|
Chris@0
|
545 else {
|
Chris@0
|
546 $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, @url.', ['@url' => $settings_url]);
|
Chris@0
|
547 }
|
Chris@0
|
548 }
|
Chris@0
|
549
|
Chris@0
|
550 /**
|
Chris@0
|
551 * Returns the appropriate message text when site is out of date or not secure.
|
Chris@0
|
552 *
|
Chris@0
|
553 * These error messages are shared by both update_requirements() for the
|
Chris@0
|
554 * site-wide status report at admin/reports/status and in the body of the
|
Chris@0
|
555 * notification email messages generated by update_cron().
|
Chris@0
|
556 *
|
Chris@0
|
557 * @param $msg_type
|
Chris@0
|
558 * String to indicate what kind of message to generate. Can be either 'core'
|
Chris@0
|
559 * or 'contrib'.
|
Chris@0
|
560 * @param $msg_reason
|
Chris@0
|
561 * Integer constant specifying why message is generated.
|
Chris@0
|
562 * @param $langcode
|
Chris@0
|
563 * (optional) A language code to use. Defaults to NULL.
|
Chris@0
|
564 *
|
Chris@0
|
565 * @return
|
Chris@0
|
566 * The properly translated error message for the given key.
|
Chris@0
|
567 */
|
Chris@0
|
568 function _update_message_text($msg_type, $msg_reason, $langcode = NULL) {
|
Chris@0
|
569 $text = '';
|
Chris@0
|
570 switch ($msg_reason) {
|
Chris@0
|
571 case UPDATE_NOT_SECURE:
|
Chris@0
|
572 if ($msg_type == 'core') {
|
Chris@0
|
573 $text = t('There is a security update available for your version of Drupal. To ensure the security of your server, you should update immediately!', [], ['langcode' => $langcode]);
|
Chris@0
|
574 }
|
Chris@0
|
575 else {
|
Chris@0
|
576 $text = t('There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately!', [], ['langcode' => $langcode]);
|
Chris@0
|
577 }
|
Chris@0
|
578 break;
|
Chris@0
|
579
|
Chris@0
|
580 case UPDATE_REVOKED:
|
Chris@0
|
581 if ($msg_type == 'core') {
|
Chris@0
|
582 $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', [], ['langcode' => $langcode]);
|
Chris@0
|
583 }
|
Chris@0
|
584 else {
|
Chris@0
|
585 $text = t('The installed version of at least one of your modules or themes has been revoked and is no longer available for download. Upgrading or disabling is strongly recommended!', [], ['langcode' => $langcode]);
|
Chris@0
|
586 }
|
Chris@0
|
587 break;
|
Chris@0
|
588
|
Chris@0
|
589 case UPDATE_NOT_SUPPORTED:
|
Chris@0
|
590 if ($msg_type == 'core') {
|
Chris@0
|
591 $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', [], ['langcode' => $langcode]);
|
Chris@0
|
592 }
|
Chris@0
|
593 else {
|
Chris@0
|
594 $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or disabling is strongly recommended. See the project homepage for more details.', [], ['langcode' => $langcode]);
|
Chris@0
|
595 }
|
Chris@0
|
596 break;
|
Chris@0
|
597
|
Chris@0
|
598 case UPDATE_NOT_CURRENT:
|
Chris@0
|
599 if ($msg_type == 'core') {
|
Chris@0
|
600 $text = t('There are updates available for your version of Drupal. To ensure the proper functioning of your site, you should update as soon as possible.', [], ['langcode' => $langcode]);
|
Chris@0
|
601 }
|
Chris@0
|
602 else {
|
Chris@0
|
603 $text = t('There are updates available for one or more of your modules or themes. To ensure the proper functioning of your site, you should update as soon as possible.', [], ['langcode' => $langcode]);
|
Chris@0
|
604 }
|
Chris@0
|
605 break;
|
Chris@0
|
606
|
Chris@0
|
607 case UPDATE_UNKNOWN:
|
Chris@0
|
608 case UPDATE_NOT_CHECKED:
|
Chris@0
|
609 case UPDATE_NOT_FETCHED:
|
Chris@0
|
610 case UPDATE_FETCH_PENDING:
|
Chris@0
|
611 if ($msg_type == 'core') {
|
Chris@18
|
612 $text = t('There was a problem checking <a href=":update-report">available updates</a> for Drupal.', [':update-report' => Url::fromRoute('update.status')->toString()], ['langcode' => $langcode]);
|
Chris@0
|
613 }
|
Chris@0
|
614 else {
|
Chris@18
|
615 $text = t('There was a problem checking <a href=":update-report">available updates</a> for your modules or themes.', [':update-report' => Url::fromRoute('update.status')->toString()], ['langcode' => $langcode]);
|
Chris@0
|
616 }
|
Chris@0
|
617 break;
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 return $text;
|
Chris@0
|
621 }
|
Chris@0
|
622
|
Chris@0
|
623 /**
|
Chris@0
|
624 * Orders projects based on their status.
|
Chris@0
|
625 *
|
Chris@0
|
626 * Callback for uasort() within update_requirements().
|
Chris@0
|
627 */
|
Chris@0
|
628 function _update_project_status_sort($a, $b) {
|
Chris@0
|
629 // The status constants are numerically in the right order, so we can
|
Chris@0
|
630 // usually subtract the two to compare in the order we want. However,
|
Chris@0
|
631 // negative status values should be treated as if they are huge, since we
|
Chris@0
|
632 // always want them at the bottom of the list.
|
Chris@0
|
633 $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
|
Chris@0
|
634 $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
|
Chris@0
|
635 return $a_status - $b_status;
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 /**
|
Chris@0
|
639 * Prepares variables for last time update data was checked templates.
|
Chris@0
|
640 *
|
Chris@0
|
641 * Default template: update-last-check.html.twig.
|
Chris@0
|
642 *
|
Chris@0
|
643 * In addition to properly formatting the given timestamp, this function also
|
Chris@0
|
644 * provides a "Check manually" link that refreshes the available update and
|
Chris@0
|
645 * redirects back to the same page.
|
Chris@0
|
646 *
|
Chris@0
|
647 * @param $variables
|
Chris@0
|
648 * An associative array containing:
|
Chris@0
|
649 * - last: The timestamp when the site last checked for available updates.
|
Chris@0
|
650 *
|
Chris@0
|
651 * @see theme_update_report()
|
Chris@0
|
652 */
|
Chris@0
|
653 function template_preprocess_update_last_check(&$variables) {
|
Chris@0
|
654 $variables['time'] = \Drupal::service('date.formatter')->formatTimeDiffSince($variables['last']);
|
Chris@0
|
655 $variables['link'] = \Drupal::l(t('Check manually'), new Url('update.manual_status', [], ['query' => \Drupal::destination()->getAsArray()]));
|
Chris@0
|
656 }
|
Chris@0
|
657
|
Chris@0
|
658 /**
|
Chris@0
|
659 * Implements hook_verify_update_archive().
|
Chris@0
|
660 *
|
Chris@0
|
661 * First, we ensure that the archive isn't a copy of Drupal core, which the
|
Chris@0
|
662 * update manager does not yet support. See https://www.drupal.org/node/606592.
|
Chris@0
|
663 *
|
Chris@0
|
664 * Then, we make sure that at least one module included in the archive file has
|
Chris@0
|
665 * an .info.yml file which claims that the code is compatible with the current
|
Chris@0
|
666 * version of Drupal core.
|
Chris@0
|
667 *
|
Chris@0
|
668 * @see \Drupal\Core\Extension\ExtensionDiscovery
|
Chris@0
|
669 */
|
Chris@0
|
670 function update_verify_update_archive($project, $archive_file, $directory) {
|
Chris@0
|
671 $errors = [];
|
Chris@0
|
672
|
Chris@0
|
673 // Make sure this isn't a tarball of Drupal core.
|
Chris@0
|
674 if (
|
Chris@0
|
675 file_exists("$directory/$project/index.php")
|
Chris@0
|
676 && file_exists("$directory/$project/core/install.php")
|
Chris@0
|
677 && file_exists("$directory/$project/core/includes/bootstrap.inc")
|
Chris@0
|
678 && file_exists("$directory/$project/core/modules/node/node.module")
|
Chris@0
|
679 && file_exists("$directory/$project/core/modules/system/system.module")
|
Chris@0
|
680 ) {
|
Chris@0
|
681 return [
|
Chris@0
|
682 'no-core' => t('Automatic updating of Drupal core is not supported. See the <a href=":upgrade-guide">upgrade guide</a> for information on how to update Drupal core manually.', [':upgrade-guide' => 'https://www.drupal.org/upgrade']),
|
Chris@0
|
683 ];
|
Chris@0
|
684 }
|
Chris@0
|
685
|
Chris@0
|
686 // Parse all the .info.yml files and make sure at least one is compatible with
|
Chris@0
|
687 // this version of Drupal core. If one is compatible, then the project as a
|
Chris@0
|
688 // whole is considered compatible (since, for example, the project may ship
|
Chris@0
|
689 // with some out-of-date modules that are not necessary for its overall
|
Chris@0
|
690 // functionality).
|
Chris@0
|
691 $compatible_project = FALSE;
|
Chris@0
|
692 $incompatible = [];
|
Chris@0
|
693 $files = file_scan_directory("$directory/$project", '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', ['key' => 'name', 'min_depth' => 0]);
|
Chris@0
|
694 foreach ($files as $file) {
|
Chris@0
|
695 // Get the .info.yml file for the module or theme this file belongs to.
|
Chris@0
|
696 $info = \Drupal::service('info_parser')->parse($file->uri);
|
Chris@0
|
697
|
Chris@0
|
698 // If the module or theme is incompatible with Drupal core, set an error.
|
Chris@0
|
699 if (empty($info['core']) || $info['core'] != \Drupal::CORE_COMPATIBILITY) {
|
Chris@0
|
700 $incompatible[] = !empty($info['name']) ? $info['name'] : t('Unknown');
|
Chris@0
|
701 }
|
Chris@0
|
702 else {
|
Chris@0
|
703 $compatible_project = TRUE;
|
Chris@0
|
704 break;
|
Chris@0
|
705 }
|
Chris@0
|
706 }
|
Chris@0
|
707
|
Chris@18
|
708 /** @var \Drupal\Core\File\FileSystemInterface $file_system */
|
Chris@18
|
709 $file_system = \Drupal::service('file_system');
|
Chris@0
|
710 if (empty($files)) {
|
Chris@18
|
711 $errors[] = t('%archive_file does not contain any .info.yml files.', ['%archive_file' => $file_system->basename($archive_file)]);
|
Chris@0
|
712 }
|
Chris@0
|
713 elseif (!$compatible_project) {
|
Chris@0
|
714 $errors[] = \Drupal::translation()->formatPlural(
|
Chris@0
|
715 count($incompatible),
|
Chris@0
|
716 '%archive_file contains a version of %names that is not compatible with Drupal @version.',
|
Chris@0
|
717 '%archive_file contains versions of modules or themes that are not compatible with Drupal @version: %names',
|
Chris@18
|
718 [
|
Chris@18
|
719 '@version' => \Drupal::CORE_COMPATIBILITY,
|
Chris@18
|
720 '%archive_file' => $file_system->basename($archive_file),
|
Chris@18
|
721 '%names' => implode(', ', $incompatible),
|
Chris@18
|
722 ]
|
Chris@0
|
723 );
|
Chris@0
|
724 }
|
Chris@0
|
725
|
Chris@0
|
726 return $errors;
|
Chris@0
|
727 }
|
Chris@0
|
728
|
Chris@0
|
729 /**
|
Chris@0
|
730 * Invalidates stored data relating to update status.
|
Chris@0
|
731 */
|
Chris@0
|
732 function update_storage_clear() {
|
Chris@0
|
733 \Drupal::keyValueExpirable('update')->deleteAll();
|
Chris@0
|
734 \Drupal::keyValueExpirable('update_available_release')->deleteAll();
|
Chris@0
|
735 }
|
Chris@0
|
736
|
Chris@0
|
737 /**
|
Chris@0
|
738 * Returns a short unique identifier for this Drupal installation.
|
Chris@0
|
739 *
|
Chris@0
|
740 * @return
|
Chris@0
|
741 * An eight character string uniquely identifying this Drupal installation.
|
Chris@0
|
742 */
|
Chris@0
|
743 function _update_manager_unique_identifier() {
|
Chris@0
|
744 $id = &drupal_static(__FUNCTION__, '');
|
Chris@0
|
745 if (empty($id)) {
|
Chris@0
|
746 $id = substr(hash('sha256', Settings::getHashSalt()), 0, 8);
|
Chris@0
|
747 }
|
Chris@0
|
748 return $id;
|
Chris@0
|
749 }
|
Chris@0
|
750
|
Chris@0
|
751 /**
|
Chris@0
|
752 * Returns the directory where update archive files should be extracted.
|
Chris@0
|
753 *
|
Chris@0
|
754 * @param $create
|
Chris@0
|
755 * (optional) Whether to attempt to create the directory if it does not
|
Chris@0
|
756 * already exist. Defaults to TRUE.
|
Chris@0
|
757 *
|
Chris@0
|
758 * @return
|
Chris@0
|
759 * The full path to the temporary directory where update file archives should
|
Chris@0
|
760 * be extracted.
|
Chris@0
|
761 */
|
Chris@0
|
762 function _update_manager_extract_directory($create = TRUE) {
|
Chris@0
|
763 $directory = &drupal_static(__FUNCTION__, '');
|
Chris@0
|
764 if (empty($directory)) {
|
Chris@0
|
765 $directory = 'temporary://update-extraction-' . _update_manager_unique_identifier();
|
Chris@0
|
766 if ($create && !file_exists($directory)) {
|
Chris@0
|
767 mkdir($directory);
|
Chris@0
|
768 }
|
Chris@0
|
769 }
|
Chris@0
|
770 return $directory;
|
Chris@0
|
771 }
|
Chris@0
|
772
|
Chris@0
|
773 /**
|
Chris@0
|
774 * Returns the directory where update archive files should be cached.
|
Chris@0
|
775 *
|
Chris@0
|
776 * @param $create
|
Chris@0
|
777 * (optional) Whether to attempt to create the directory if it does not
|
Chris@0
|
778 * already exist. Defaults to TRUE.
|
Chris@0
|
779 *
|
Chris@0
|
780 * @return
|
Chris@0
|
781 * The full path to the temporary directory where update file archives should
|
Chris@0
|
782 * be cached.
|
Chris@0
|
783 */
|
Chris@0
|
784 function _update_manager_cache_directory($create = TRUE) {
|
Chris@0
|
785 $directory = &drupal_static(__FUNCTION__, '');
|
Chris@0
|
786 if (empty($directory)) {
|
Chris@0
|
787 $directory = 'temporary://update-cache-' . _update_manager_unique_identifier();
|
Chris@0
|
788 if ($create && !file_exists($directory)) {
|
Chris@0
|
789 mkdir($directory);
|
Chris@0
|
790 }
|
Chris@0
|
791 }
|
Chris@0
|
792 return $directory;
|
Chris@0
|
793 }
|
Chris@0
|
794
|
Chris@0
|
795 /**
|
Chris@0
|
796 * Clears the temporary files and directories based on file age from disk.
|
Chris@0
|
797 */
|
Chris@0
|
798 function update_clear_update_disk_cache() {
|
Chris@0
|
799 // List of update module cache directories. Do not create the directories if
|
Chris@0
|
800 // they do not exist.
|
Chris@0
|
801 $directories = [
|
Chris@0
|
802 _update_manager_cache_directory(FALSE),
|
Chris@0
|
803 _update_manager_extract_directory(FALSE),
|
Chris@0
|
804 ];
|
Chris@0
|
805
|
Chris@0
|
806 // Search for files and directories in base folder only without recursion.
|
Chris@0
|
807 foreach ($directories as $directory) {
|
Chris@0
|
808 file_scan_directory($directory, '/.*/', ['callback' => 'update_delete_file_if_stale', 'recurse' => FALSE]);
|
Chris@0
|
809 }
|
Chris@0
|
810 }
|
Chris@0
|
811
|
Chris@0
|
812 /**
|
Chris@0
|
813 * Deletes stale files and directories from the update manager disk cache.
|
Chris@0
|
814 *
|
Chris@0
|
815 * Files and directories older than 6 hours and development snapshots older than
|
Chris@0
|
816 * 5 minutes are considered stale. We only cache development snapshots for 5
|
Chris@0
|
817 * minutes since otherwise updated snapshots might not be downloaded as
|
Chris@0
|
818 * expected.
|
Chris@0
|
819 *
|
Chris@0
|
820 * When checking file ages, we need to use the ctime, not the mtime
|
Chris@0
|
821 * (modification time) since many (all?) tar implementations go out of their way
|
Chris@0
|
822 * to set the mtime on the files they create to the timestamps recorded in the
|
Chris@0
|
823 * tarball. We want to see the last time the file was changed on disk, which is
|
Chris@0
|
824 * left alone by tar and correctly set to the time the archive file was
|
Chris@0
|
825 * unpacked.
|
Chris@0
|
826 *
|
Chris@0
|
827 * @param $path
|
Chris@0
|
828 * A string containing a file path or (streamwrapper) URI.
|
Chris@0
|
829 */
|
Chris@0
|
830 function update_delete_file_if_stale($path) {
|
Chris@0
|
831 if (file_exists($path)) {
|
Chris@0
|
832 $filectime = filectime($path);
|
Chris@0
|
833 $max_age = \Drupal::config('system.file')->get('temporary_maximum_age');
|
Chris@0
|
834
|
Chris@0
|
835 if (REQUEST_TIME - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) {
|
Chris@18
|
836 try {
|
Chris@18
|
837 \Drupal::service('file_system')->deleteRecursive($path);
|
Chris@18
|
838 }
|
Chris@18
|
839 catch (FileException $e) {
|
Chris@18
|
840 // Ignore failed deletes.
|
Chris@18
|
841 }
|
Chris@0
|
842 }
|
Chris@0
|
843 }
|
Chris@0
|
844 }
|