update.manager.inc 34 KB

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