/[drupal]/drupal/modules/update/update.manager.inc
ViewVC logotype

Contents of /drupal/modules/update/update.manager.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Thu Oct 29 07:11:50 2009 UTC (3 weeks, 6 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.10: +4 -6 lines
File MIME type: text/x-php
#602496 by dww: Clean up the update manager project selector page when updating your site.
1 <?php
2 // $Id: update.manager.inc,v 1.10 2009/10/29 07:08:38 webchick Exp $
3
4 /**
5 * @file
6 * Administrative screens and processing functions for the update manager.
7 * This allows site administrators with the 'administer software updates'
8 * permission to either upgrade existing projects, or download and install new
9 * ones, so long as the killswitch setting ('allow_authorize_operations') is
10 * still TRUE.
11 *
12 * To install new code, the administrator is prompted for either the URL of an
13 * archive file, or to directly upload the archive file. The archive is loaded
14 * into a temporary location, extracted, and verified. If everything is
15 * successful, the user is redirected to authorize.php to type in their file
16 * transfer credentials and authorize the installation to proceed with
17 * elevated privileges, such that the extracted files can be copied out of the
18 * temporary location and into the live web root.
19 *
20 * Updating existing code is a more elaborate process. The first step is a
21 * selection form where the user is presented with a table of installed
22 * projects that are missing newer releases. The user selects which projects
23 * they wish to upgrade, and presses the "Download updates" button to
24 * continue. This sets up a batch to fetch all the selected releases, and
25 * redirects to admin/update/download to display the batch progress bar as it
26 * runs. Each batch operation is responsible for downloading a single file,
27 * extracting the archive, and verifying the contents. If there are any
28 * errors, the user is redirected back to the first page with the error
29 * messages. If all downloads were extacted and verified, the user is instead
30 * redirected to admin/update/confirm, a landing page which reminds them to
31 * backup their database and asks if they want to put the site offline during
32 * the upgrade. Once the user presses the "Install updates" button, they are
33 * redirected to authorize.php to supply their web root file access
34 * credentials. The authorized operation (which lives in update.authorize.inc)
35 * sets up a batch to copy each extracted update from the temporary location
36 * into the live web root.
37 */
38
39 /**
40 * @defgroup update_manager_update Update manager for updating existing code.
41 * @{
42 */
43
44 /**
45 * Build the form for the update manager page to update existing projects.
46 *
47 * This presents a table with all projects that have available updates with
48 * checkboxes to select which ones to upgrade.
49 *
50 * @param $form
51 * @param $form_state
52 * @param $context
53 * String representing the context from which we're trying to update, can be:
54 * 'module', 'theme' or 'report'.
55 * @return
56 * The form array for selecting which projects to update.
57 */
58 function update_manager_update_form($form, $form_state = array(), $context) {
59 $form['#theme'] = 'update_manager_update_form';
60
61 $available = update_get_available(TRUE);
62 if (empty($available)) {
63 $form['message'] = array(
64 '#markup' => t('There was a problem getting update information. Please try again later.'),
65 );
66 return $form;
67 }
68
69 drupal_add_css('misc/ui/ui.all.css');
70 drupal_add_css('misc/ui/ui.dialog.css');
71 drupal_add_js('misc/ui/ui.core.js', array('weight' => JS_LIBRARY + 5));
72 drupal_add_js('misc/ui/ui.dialog.js', array('weight' => JS_LIBRARY + 6));
73 $form['#attached']['js'][] = drupal_get_path('module', 'update') . '/update.manager.js';
74 $form['#attached']['css'][] = drupal_get_path('module', 'update') . '/update.css';
75
76 // This will be a nested array. The first key is the kind of project, which
77 // can be either 'enabled', 'disabled', 'manual' (projects which require
78 // manual updates, such as core). Then, each subarray is an array of
79 // projects of that type, indexed by project short name, and containing an
80 // array of data for cells in that project's row in the appropriate table.
81 $projects = array();
82
83 // This stores the actual download link we're going to update from for each
84 // project in the form, regardless of if it's enabled or disabled.
85 $form['project_downloads'] = array('#tree' => TRUE);
86
87 module_load_include('inc', 'update', 'update.compare');
88 $project_data = update_calculate_project_data($available);
89 foreach ($project_data as $name => $project) {
90 // Filter out projects which are up to date already.
91 if ($project['status'] == UPDATE_CURRENT) {
92 continue;
93 }
94 // The project name to display can vary based on the info we have.
95 if (!empty($project['title'])) {
96 if (!empty($project['link'])) {
97 $project_name = l($project['title'], $project['link']);
98 }
99 else {
100 $project_name = check_plain($project['title']);
101 }
102 }
103 elseif (!empty($project['info']['name'])) {
104 $project_name = check_plain($project['info']['name']);
105 }
106 else {
107 $project_name = check_plain($name);
108 }
109 if ($project['project_type'] == 'theme' || $project['project_type'] == 'theme-disabled') {
110 $project_name .= ' ' . t('(Theme)');
111 }
112
113 if (empty($project['recommended'])) {
114 // If we don't know what to recommend they upgrade to, we should skip
115 // the project entirely.
116 continue;
117 }
118
119 $recommended_release = $project['releases'][$project['recommended']];
120 $recommended_version = $recommended_release['version'] . ' ' . l(t('(Release notes)'), $recommended_release['release_link'], array('attributes' => array('title' => t('Release notes for @project_name', array('@project_name' => $project_name)))));
121 if ($recommended_release['version_major'] != $project['existing_major']) {
122 $recommended_version .= '<div title="Major upgrade warning" class="update-major-version-warning">' . t('This update is a major version update which means that it may not be backwards compatible with your currently running version. It is recommended that you read the release notes and proceed at your own risk.') . '</div>';
123 }
124
125 // Create an entry for this project.
126 $entry = array(
127 'title' => $project_name,
128 'installed_version' => $project['existing_version'],
129 'recommended_version' => $recommended_version,
130 );
131
132 switch ($project['status']) {
133 case UPDATE_NOT_SECURE:
134 case UPDATE_REVOKED:
135 $entry['title'] .= ' ' . t('(Security update)');
136 $entry['#weight'] = -2;
137 $type = 'security';
138 break;
139
140 case UPDATE_NOT_SUPPORTED:
141 $type = 'unsupported';
142 $entry['title'] .= ' ' . t('(Unsupported)');
143 $entry['#weight'] = -1;
144 break;
145
146 case UPDATE_UNKNOWN:
147 case UPDATE_NOT_FETCHED:
148 case UPDATE_NOT_CHECKED:
149 case UPDATE_NOT_CURRENT:
150 $type = 'recommended';
151 break;
152
153 default:
154 // Jump out of the switch and onto the next project in foreach.
155 continue 2;
156 }
157
158 $entry['#attributes'] = array('class' => array('update-' . $type));
159
160 // Drupal core needs to be upgraded manually.
161 $needs_manual = $project['project_type'] == 'core';
162
163 if ($needs_manual) {
164 // Since it won't be tableselect, #weight will add an extra column to the
165 // table if it's defined, so just unset it. The order doesn't matter that
166 // much in the manual updates table, anyway.
167 unset($entry['#weight']);
168 }
169 else {
170 $form['project_downloads'][$name] = array(
171 '#type' => 'value',
172 '#value' => $recommended_release['download_link'],
173 );
174 }
175
176 // Based on what kind of project this is, save the entry into the
177 // appropriate subarray.
178 switch ($project['project_type']) {
179 case 'core':
180 // Core needs manual updates at this time.
181 $projects['manual'][$name] = $entry;
182 break;
183
184 case 'module':
185 case 'theme':
186 $projects['enabled'][$name] = $entry;
187 break;
188
189 case 'module-disabled':
190 case 'theme-disabled':
191 $projects['disabled'][$name] = $entry;
192 break;
193 }
194 }
195
196 if (empty($projects)) {
197 $form['message'] = array(
198 '#markup' => t('All of your projects are up to date.'),
199 );
200 return $form;
201 }
202
203 $headers = array(
204 'title' => array(
205 'data' => t('Name'),
206 'class' => array('update-project-name'),
207 ),
208 'installed_version' => t('Installed version'),
209 'recommended_version' => t('Recommended version'),
210 );
211
212 if (!empty($projects['enabled'])) {
213 $form['projects'] = array(
214 '#type' => 'tableselect',
215 '#header' => $headers,
216 '#options' => $projects['enabled'],
217 );
218 if (!empty($projects['disabled'])) {
219 $form['projects']['#prefix'] = '<h2>' . t('Enabled') . '</h2>';
220 }
221 }
222
223 if (!empty($projects['disabled'])) {
224 $form['disabled_projects'] = array(
225 '#type' => 'tableselect',
226 '#header' => $headers,
227 '#options' => $projects['disabled'],
228 '#weight' => 1,
229 '#prefix' => '<h2>' . t('Disabled') . '</h2>',
230 );
231 }
232
233 // If either table has been printed yet, we need a submit button and to
234 // validate the checkboxes.
235 if (!empty($projects['enabled']) || !empty($projects['disabled'])) {
236 $form['submit'] = array(
237 '#type' => 'submit',
238 '#value' => t('Download these updates'),
239 '#weight' => 10,
240 );
241 $form['#validate'][] = 'update_manager_update_form_validate';
242 }
243
244 if (!empty($projects['manual'])) {
245 $prefix = '<h2>' . t('Manual updates required') . '</h2>';
246 $prefix .= '<p>' . t('Updates of Drupal core are not supported at this time.') . '</p>';
247 $form['manual_updates'] = array(
248 '#type' => 'markup',
249 '#markup' => theme('table', array('header' => $headers, 'rows' => $projects['manual'])),
250 '#prefix' => $prefix,
251 '#weight' => 20,
252 );
253 }
254
255 return $form;
256 }
257
258 /**
259 * Theme the first page in the update manager wizard to select projects.
260 *
261 * @param $variables
262 * form: The form
263 *
264 * @ingroup themeable
265 */
266 function theme_update_manager_update_form($variables) {
267 $form = $variables['form'];
268 $last = variable_get('update_last_check', 0);
269 $output = theme('update_last_check', array('last' => $last));
270 $output .= drupal_render_children($form);
271 return $output;
272 }
273
274 /**
275 * Validation callback to ensure that at least one project is selected.
276 */
277 function update_manager_update_form_validate($form, &$form_state) {
278 if (!empty($form_state['values']['projects'])) {
279 $enabled = array_filter($form_state['values']['projects']);
280 }
281 if (!empty($form_state['values']['disabled_projects'])) {
282 $disabled = array_filter($form_state['values']['disabled_projects']);
283 }
284 if (empty($enabled) && empty($disabled)) {
285 form_set_error('projects', t('You must select at least one project to update.'));
286 }
287 }
288
289 /**
290 * Submit function for the main update form.
291 *
292 * This sets up a batch to download, extract and verify the selected releases
293 *
294 * @see update_manager_update_form()
295 */
296 function update_manager_update_form_submit($form, &$form_state) {
297 $projects = array();
298 foreach (array('projects', 'disabled_projects') as $type) {
299 if (!empty($form_state['values'][$type])) {
300 $projects = array_merge($projects, array_keys(array_filter($form_state['values'][$type])));
301 }
302 }
303 $operations = array();
304 foreach ($projects as $project) {
305 $operations[] = array(
306 'update_manager_batch_project_get',
307 array(
308 $project,
309 $form_state['values']['project_downloads'][$project],
310 ),
311 );
312 }
313 $batch = array(
314 'title' => t('Downloading updates'),
315 'init_message' => t('Preparing to download selected updates'),
316 'operations' => $operations,
317 'finished' => 'update_manager_download_batch_finished',
318 'file' => drupal_get_path('module', 'update') . '/update.manager.inc',
319 );
320 batch_set($batch);
321 }
322
323 /**
324 * Batch callback invoked when the download batch is completed.
325 */
326 function update_manager_download_batch_finished($success, $results) {
327 if (!empty($results['errors'])) {
328 $error_list = array(
329 'title' => t('Downloading updates failed:'),
330 'items' => $results['errors'],
331 );
332 drupal_set_message(theme('item_list', $error_list), 'error');
333 }
334 elseif ($success) {
335 $_SESSION['update_manager_update_projects'] = $results['projects'];
336 drupal_goto('admin/update/confirm');
337 }
338 else {
339 // Ideally we're catching all Exceptions, so they should never see this,
340 // but just in case, we have to tell them something.
341 drupal_set_message(t('Fatal error trying to download.'), 'error');
342 }
343 }
344
345 /**
346 * Build the form to confirm that an update should proceed (after downloading).
347 *
348 * This form is an intermediary step in the automated update workflow. It is
349 * presented to the site administrator after all the required updates have
350 * been downloaded and verified. The point of this page is to encourage the
351 * user to backup their site, gives them the opportunity to put the site
352 * offline, and then asks them to confirm that the update should continue.
353 * After this step, the user is redirected to authorize.php to enter their
354 * file transfer credentials and attempt to complete the update.
355 */
356 function update_manager_confirm_update_form($form, &$form_state) {
357 $form['information']['#weight'] = -100;
358 $form['information']['backup_header'] = array(
359 '#prefix' => '<h3>',
360 '#markup' => t('Step 1: Backup your site'),
361 '#suffix' => '</h3>',
362 );
363
364 $form['information']['backup_message'] = array(
365 '#prefix' => '<p>',
366 '#markup' => t('We do not currently have a web based backup tool. <a href="@backup_url">Learn more about how to take a backup</a>.', array('@backup_url' => url('http://drupal.org/node/22281'))),
367 '#suffix' => '</p>',
368 );
369
370 $form['information']['maint_header'] = array(
371 '#prefix' => '<h3>',
372 '#markup' => t('Step 2: Enter maintenance mode'),
373 '#suffix' => '</h3>',
374 );
375
376 $form['information']['maint_message'] = array(
377 '#prefix' => '<p>',
378 '#markup' => t('It is strongly recommended that you put your site into maintenance mode while performing an update.'),
379 '#suffix' => '</p>',
380 );
381
382 $form['information']['site_offline'] = array(
383 '#title' => t('Perform updates with site in maintenance mode'),
384 '#type' => 'checkbox',
385 '#default_value' => TRUE,
386 );
387
388 $form['submit'] = array(
389 '#type' => 'submit',
390 '#value' => t('Install updates'),
391 '#weight' => 100,
392 );
393
394 return $form;
395 }
396
397 /**
398 * Submit handler for the form to confirm that an update should continue.
399 *
400 * If the site administrator requested that the site is put offline during the
401 * update, do so now. Otherwise, pull information about all the required
402 * updates out of the SESSION, figure out what Updater class is needed for
403 * each one, generate an array of update operations to perform, and hand it
404 * all off to system_authorized_init(), then redirect to authorize.php.
405 *
406 * @see update_authorize_run_update()
407 * @see system_authorized_init()
408 * @see system_authorized_get_url()
409 */
410 function update_manager_confirm_update_form_submit($form, &$form_state) {
411 if ($form_state['values']['site_offline'] == TRUE) {
412 variable_set('site_offline', TRUE);
413 }
414
415 if (!empty($_SESSION['update_manager_update_projects'])) {
416 // Make sure the Updater registry is loaded.
417 drupal_get_updaters();
418
419 $updates = array();
420 $directory = _update_manager_extract_directory();
421
422 $projects = $_SESSION['update_manager_update_projects'];
423 unset($_SESSION['update_manager_update_projects']);
424
425 foreach ($projects as $project => $url) {
426 $project_location = $directory . '/' . $project;
427 $updater = Updater::factory($project_location);
428 $project_real_location = drupal_realpath($project_location);
429 $updates[] = array(
430 'project' => $project,
431 'updater_name' => get_class($updater),
432 'local_url' => $project_real_location,
433 );
434 }
435
436 // If the owner of the last directory we extracted is the same as the
437 // owner of our configuration directory (e.g. sites/default) where we're
438 // trying to install the code, there's no need to prompt for FTP/SSH
439 // credentials. Instead, we instantiate a FileTransferLocal and invoke
440 // update_authorize_run_update() directly.
441 if (fileowner($project_real_location) == fileowner(conf_path())) {
442 module_load_include('inc', 'update', 'update.authorize');
443 $filetransfer = new FileTransferLocal(DRUPAL_ROOT);
444 update_authorize_run_update($filetransfer, $updates);
445 }
446 // Otherwise, go through the regular workflow to prompt for FTP/SSH
447 // credentials and invoke update_authorize_run_update() indirectly with
448 // whatever FileTransfer object authorize.php creates for us.
449 else {
450 system_authorized_init('update_authorize_run_update', drupal_get_path('module', 'update') . '/update.authorize.inc', array($updates));
451 $form_state['redirect'] = system_authorized_get_url();
452 }
453 }
454 }
455
456 /**
457 * @} End of "defgroup update_manager_update".
458 */
459
460 /**
461 * @defgroup update_manager_install Update manager for installing new code.
462 * @{
463 */
464
465 /**
466 * Build the form for the update manager page to install new projects.
467 *
468 * This presents a place to enter a URL or upload an archive file to use to
469 * install a new module or theme.
470 *
471 * @param $form
472 * @param $form_state
473 * @param $context
474 * String representing the context from which we're trying to install, can
475 * be: 'module', 'theme' or 'report'.
476 * @return
477 * The form array for selecting which project to install.
478 */
479 function update_manager_install_form($form, &$form_state, $context) {
480 $form = array();
481
482 // Collect all the supported archive file extensions for the UI text.
483 $extensions = array();
484 $archiver_info = archiver_get_info();
485 foreach ($archiver_info as $info) {
486 if (!empty($info['extensions'])) {
487 $extensions += $info['extensions'];
488 }
489 }
490 $form['help_text'] = array(
491 '#prefix' => '<p>',
492 '#markup' => t('To install a new module or theme, either paste the URL of an archive file you wish to install, or upload the archive file that you have downloaded. You can find <a href="@module_url">modules</a> and <a href="@theme_url">themes</a> at <a href="@drupal_org_url">http://drupal.org</a>. The following archive extensions are supported: %extensions', array('@module_url' => 'http://drupal.org/project/modules', '@theme_url' => 'http://drupal.org/project/themes', '@drupal_org_url' => 'http://drupal.org', '%extensions' => implode(', ', $extensions))),
493 '#suffix' => '</p>',
494 );
495
496 $form['project_url'] = array(
497 '#type' => 'textfield',
498 '#title' => t('URL'),
499 '#description' => t('Paste the URL to a Drupal module or theme archive to install it (e.g http://ftp.drupal.org/files/projects/projectname.tar.gz).'),
500 );
501
502 $form['information'] = array(
503 '#prefix' => '<strong>',
504 '#markup' => t('Or'),
505 '#suffix' => '</strong>',
506 );
507
508 $form['project_upload'] = array(
509 '#type' => 'file',
510 '#title' => t('Upload a module or theme'),
511 '#description' => t('Upload a Drupal module or theme archive to install it.'),
512 );
513
514 $form['submit'] = array(
515 '#type' => 'submit',
516 '#value' => t('Install'),
517 );
518
519 return $form;
520 }
521
522 /**
523 * Validate the form for installing a new project via the update manager.
524 */
525 function update_manager_install_form_validate($form, &$form_state) {
526 if (!($form_state['values']['project_url'] XOR !empty($_FILES['files']['name']['project_upload']))) {
527 form_set_error('project_url', t('You must either provide a URL or upload an archive file to install.'));
528 }
529
530 if ($form_state['values']['project_url']) {
531 if (!valid_url($form_state['values']['project_url'], TRUE)) {
532 form_set_error('project_url', t('The provided URL is invalid.'));
533 }
534 }
535 }
536
537 /**
538 * Handle form submission when installing new projects via the update manager.
539 *
540 * Either downloads the file specified in the URL to a temporary cache, or
541 * uploads the file attached to the form, then attempts to extract the archive
542 * into a temporary location and verify it. Instantiate the appropriate
543 * Updater class for this project and make sure it is not already installed in
544 * the live webroot. If everything is successful, setup an operation to run
545 * via authorize.php which will copy the extracted files from the temporary
546 * location into the live site.
547 *
548 * @see update_authorize_run_install()
549 * @see system_authorized_init()
550 * @see system_authorized_get_url()
551 */
552 function update_manager_install_form_submit($form, &$form_state) {
553 if ($form_state['values']['project_url']) {
554 $field = 'project_url';
555 $local_cache = update_manager_file_get($form_state['values']['project_url']);
556 if (!$local_cache) {
557 form_set_error($field, t('Unable to retreive Drupal project from %url.', array('%url' => $form_state['values']['project_url'])));
558 return;
559 }
560 }
561 elseif ($_FILES['files']['name']['project_upload']) {
562 $field = 'project_upload';
563 // @todo: add some validators here.
564 $finfo = file_save_upload($field, array(), NULL, FILE_EXISTS_REPLACE);
565 // @todo: find out if the module is already instealled, if so, throw an error.
566 $local_cache = $finfo->uri;
567 }
568
569 $directory = _update_manager_extract_directory();
570 try {
571 $archive = update_manager_archive_extract($local_cache, $directory);
572 }
573 catch (Exception $e) {
574 form_set_error($field, $e->getMessage());
575 return;
576 }
577
578 $files = $archive->listContents();
579 if (!$files) {
580 form_set_error($field, t('Provided archive contains no files.'));
581 return;
582 }
583 // Unfortunately, we can only use the directory name for this. :(
584 $project = drupal_substr($files[0]['filename'], 0, -1);
585
586 try {
587 update_manager_archive_verify($project, $local_cache, $directory);
588 }
589 catch (Exception $e) {
590 form_set_error($field, $e->getMessage());
591 return;
592 }
593
594 // Make sure the Updater registry is loaded.
595 drupal_get_updaters();
596
597 $project_location = $directory . '/' . $project;
598 $updater = Updater::factory($project_location);
599 $project_title = Updater::getProjectTitle($project_location);
600
601 if (!$project_title) {
602 form_set_error($field, t('Unable to determine %project name.', array('%project' => $project)));
603 }
604
605 if ($updater->isInstalled()) {
606 form_set_error($field, t('%project is already installed.', array('%project' => $project_title)));
607 return;
608 }
609
610 $project_real_location = drupal_realpath($project_location);
611 $arguments = array(
612 'project' => $project,
613 'updater_name' => get_class($updater),
614 'local_url' => $project_real_location,
615 );
616
617 // If the owner of the directory we extracted is the same as the
618 // owner of our configuration directory (e.g. sites/default) where we're
619 // trying to install the code, there's no need to prompt for FTP/SSH
620 // credentials. Instead, we instantiate a FileTransferLocal and invoke
621 // update_authorize_run_install() directly.
622 if (fileowner($project_real_location) == fileowner(conf_path())) {
623 module_load_include('inc', 'update', 'update.authorize');
624 $filetransfer = new FileTransferLocal(DRUPAL_ROOT);
625 call_user_func_array('update_authorize_run_install', array_merge(array($filetransfer), $arguments));
626 }
627 // Otherwise, go through the regular workflow to prompt for FTP/SSH
628 // credentials and invoke update_authorize_run_install() indirectly with
629 // whatever FileTransfer object authorize.php creates for us.
630 else {
631 system_authorized_init('update_authorize_run_install', drupal_get_path('module', 'update') . '/update.authorize.inc', $arguments);
632 $form_state['redirect'] = system_authorized_get_url();
633 }
634 }
635
636 /**
637 * @} End of "defgroup update_manager_install".
638 */
639
640 /**
641 * @defgroup update_manager_file Update manager file management functions.
642 * @{
643 */
644
645 /**
646 * Return the directory where update archive files should be extracted.
647 *
648 * If the directory does not already exist, attempt to create it.
649 *
650 * @return
651 * The full path to the temporary directory where update file archives
652 * should be extracted.
653 */
654 function _update_manager_extract_directory() {
655 $directory = &drupal_static(__FUNCTION__, '');
656 if (empty($directory)) {
657 $directory = DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-extraction';
658 if (!file_exists($directory)) {
659 mkdir($directory);
660 }
661 }
662 return $directory;
663 }
664
665 /**
666 * Unpack a downloaded archive file.
667 *
668 * @param string $project
669 * The short name of the project to download.
670 * @param string $file
671 * The filename of the archive you wish to extract.
672 * @param string $directory
673 * The directory you wish to extract the archive into.
674 * @return Archiver
675 * The Archiver object used to extract the archive.
676 * @throws Exception on failure.
677 */
678 function update_manager_archive_extract($file, $directory) {
679 $archiver = archiver_get_archiver($file);
680 if (!$archiver) {
681 throw new Exception(t('Cannot extract %file, not a valid archive.', array ('%file' => $file)));
682 }
683 $archiver->extract($directory);
684 return $archiver;
685 }
686
687 /**
688 * Verify an archive after it has been downloaded and extracted.
689 *
690 * This function is responsible for invoking hook_verify_update_archive().
691 *
692 * @param string $project
693 * The short name of the project to download.
694 * @param string $archive_file
695 * The filename of the unextracted archive.
696 * @param string $directory
697 * The directory that the archive was extracted into.
698 *
699 * @return void
700 * @throws Exception on failure.
701 *
702 */
703 function update_manager_archive_verify($project, $archive_file, $directory) {
704 $failures = module_invoke_all('verify_update_archive', $project, $archive_file, $directory);
705 if (!empty($failures)) {
706 throw new Exception(t('Unable to extact %file', array('%file' => $file)));
707 }
708 }
709
710 /**
711 * Copies a file from $url to the temporary directory for updates.
712 *
713 * If the file has already been downloaded, returns the the local path.
714 *
715 * @param $url
716 * The URL of the file on the server.
717 *
718 * @return string
719 * Path to local file.
720 */
721 function update_manager_file_get($url) {
722 $parsed_url = parse_url($url);
723 $remote_schemes = array('http', 'https', 'ftp', 'ftps', 'smb', 'nfs');
724 if (!in_array($parsed_url['scheme'], $remote_schemes)) {
725 // This is a local file, just return the path.
726 return drupal_realpath($url);
727 }
728
729 // Check the cache and download the file if needed.
730 $local = 'temporary://update-cache/' . basename($parsed_url['path']);
731 $cache_directory = DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-cache/';
732
733 if (!file_exists($cache_directory)) {
734 mkdir($cache_directory);
735 }
736
737 if (!file_exists($local)) {
738 return system_retrieve_file($url, $local);
739 }
740 else {
741 return $local;
742 }
743 }
744
745 /**
746 * Batch operation: download, unpack, and verify a project.
747 *
748 * This function assumes that the provided URL points to a file archive of
749 * some sort. The URL can have any scheme that we have a file stream wrapper
750 * to support. The file is downloaded to a local cache.
751 *
752 * @param string $project
753 * The short name of the project to download.
754 * @param string $url
755 * The URL to download a specific project release archive file.
756 * @param array &$context
757 * Reference to an array used for BatchAPI storage.
758 *
759 * @see update_manager_download_page()
760 */
761 function update_manager_batch_project_get($project, $url, &$context) {
762 // This is here to show the user that we are in the process of downloading.
763 if (!isset($context['sandbox']['started'])) {
764 $context['sandbox']['started'] = TRUE;
765 $context['message'] = t('Downloading %project', array('%project' => $project));
766 $context['finished'] = 0;
767 return;
768 }
769
770 // Actually try to download the file.
771 if (!($local_cache = update_manager_file_get($url))) {
772 $context['results']['errors'][$project] = t('Failed to download %project from %url', array('%project' => $project, '%url' => $url));
773 return;
774 }
775
776 // Extract it.
777 $extract_directory = _update_manager_extract_directory();
778 try {
779 update_manager_archive_extract($local_cache, $extract_directory);
780 }
781 catch (Exception $e) {
782 $context['results']['errors'][$project] = $e->getMessage();
783 return;
784 }
785
786 // Verify it.
787 try {
788 update_manager_archive_verify($project, $local_cache, $extract_directory);
789 }
790 catch (Exception $e) {
791 $context['results']['errors'][$project] = $e->getMessage();
792 return;
793 }
794
795 // Yay, success.
796 $context['results']['projects'][$project] = $url;
797 $context['finished'] = 1;
798 }
799
800 /**
801 * @} End of "defgroup update_manager_file".
802 */

  ViewVC Help
Powered by ViewVC 1.1.2