Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\update\Form;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
6 use Drupal\Core\FileTransfer\Local;
|
Chris@0
|
7 use Drupal\Core\Form\FormBase;
|
Chris@0
|
8 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
9 use Drupal\Core\State\StateInterface;
|
Chris@0
|
10 use Drupal\Core\Updater\Updater;
|
Chris@0
|
11 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
12 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Configure update settings for this site.
|
Chris@0
|
16 */
|
Chris@0
|
17 class UpdateReady extends FormBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The root location under which updated projects will be saved.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var string
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $root;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * The module handler.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $moduleHandler;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * The state key value store.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var \Drupal\Core\State\StateInterface
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $state;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * The Site path.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var string
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $sitePath;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Constructs a new UpdateReady object.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param string $root
|
Chris@0
|
51 * The root location under which updated projects will be saved.
|
Chris@0
|
52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
53 * The object that manages enabled modules in a Drupal installation.
|
Chris@0
|
54 * @param \Drupal\Core\State\StateInterface $state
|
Chris@0
|
55 * The state key value store.
|
Chris@0
|
56 * @param string $site_path
|
Chris@0
|
57 * The site path.
|
Chris@0
|
58 */
|
Chris@0
|
59 public function __construct($root, ModuleHandlerInterface $module_handler, StateInterface $state, $site_path) {
|
Chris@0
|
60 $this->root = $root;
|
Chris@0
|
61 $this->moduleHandler = $module_handler;
|
Chris@0
|
62 $this->state = $state;
|
Chris@0
|
63 $this->sitePath = $site_path;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public function getFormId() {
|
Chris@0
|
70 return 'update_manager_update_ready_form';
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public static function create(ContainerInterface $container) {
|
Chris@0
|
77 return new static(
|
Chris@0
|
78 $container->get('update.root'),
|
Chris@0
|
79 $container->get('module_handler'),
|
Chris@0
|
80 $container->get('state'),
|
Chris@0
|
81 $container->get('site.path')
|
Chris@0
|
82 );
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * {@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public function buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
89 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
|
Chris@0
|
90 if (!_update_manager_check_backends($form, 'update')) {
|
Chris@0
|
91 return $form;
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 $form['backup'] = [
|
Chris@0
|
95 '#prefix' => '<strong>',
|
Chris@0
|
96 '#markup' => $this->t('Back up your database and site before you continue. <a href=":backup_url">Learn how</a>.', [':backup_url' => 'https://www.drupal.org/node/22281']),
|
Chris@0
|
97 '#suffix' => '</strong>',
|
Chris@0
|
98 ];
|
Chris@0
|
99
|
Chris@0
|
100 $form['maintenance_mode'] = [
|
Chris@0
|
101 '#title' => $this->t('Perform updates with site in maintenance mode (strongly recommended)'),
|
Chris@0
|
102 '#type' => 'checkbox',
|
Chris@0
|
103 '#default_value' => TRUE,
|
Chris@0
|
104 ];
|
Chris@0
|
105
|
Chris@0
|
106 $form['actions'] = ['#type' => 'actions'];
|
Chris@0
|
107 $form['actions']['submit'] = [
|
Chris@0
|
108 '#type' => 'submit',
|
Chris@0
|
109 '#value' => $this->t('Continue'),
|
Chris@0
|
110 ];
|
Chris@0
|
111
|
Chris@0
|
112 return $form;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
119 // Store maintenance_mode setting so we can restore it when done.
|
Chris@0
|
120 $_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
|
Chris@0
|
121 if ($form_state->getValue('maintenance_mode') == TRUE) {
|
Chris@0
|
122 $this->state->set('system.maintenance_mode', TRUE);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 if (!empty($_SESSION['update_manager_update_projects'])) {
|
Chris@0
|
126 // Make sure the Updater registry is loaded.
|
Chris@0
|
127 drupal_get_updaters();
|
Chris@0
|
128
|
Chris@0
|
129 $updates = [];
|
Chris@0
|
130 $directory = _update_manager_extract_directory();
|
Chris@0
|
131
|
Chris@0
|
132 $projects = $_SESSION['update_manager_update_projects'];
|
Chris@0
|
133 unset($_SESSION['update_manager_update_projects']);
|
Chris@0
|
134
|
Chris@0
|
135 $project_real_location = NULL;
|
Chris@0
|
136 foreach ($projects as $project => $url) {
|
Chris@0
|
137 $project_location = $directory . '/' . $project;
|
Chris@0
|
138 $updater = Updater::factory($project_location, $this->root);
|
Chris@0
|
139 $project_real_location = drupal_realpath($project_location);
|
Chris@0
|
140 $updates[] = [
|
Chris@0
|
141 'project' => $project,
|
Chris@0
|
142 'updater_name' => get_class($updater),
|
Chris@0
|
143 'local_url' => $project_real_location,
|
Chris@0
|
144 ];
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 // If the owner of the last directory we extracted is the same as the
|
Chris@0
|
148 // owner of our configuration directory (e.g. sites/default) where we're
|
Chris@0
|
149 // trying to install the code, there's no need to prompt for FTP/SSH
|
Chris@0
|
150 // credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
|
Chris@0
|
151 // and invoke update_authorize_run_update() directly.
|
Chris@0
|
152 if (fileowner($project_real_location) == fileowner($this->sitePath)) {
|
Chris@0
|
153 $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
|
Chris@0
|
154 $filetransfer = new Local($this->root);
|
Chris@0
|
155 $response = update_authorize_run_update($filetransfer, $updates);
|
Chris@0
|
156 if ($response instanceof Response) {
|
Chris@0
|
157 $form_state->setResponse($response);
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 // Otherwise, go through the regular workflow to prompt for FTP/SSH
|
Chris@0
|
161 // credentials and invoke update_authorize_run_update() indirectly with
|
Chris@0
|
162 // whatever FileTransfer object authorize.php creates for us.
|
Chris@0
|
163 else {
|
Chris@0
|
164 // The page title must be passed here to ensure it is initially used
|
Chris@0
|
165 // when authorize.php loads for the first time with the FTP/SSH
|
Chris@0
|
166 // credentials form.
|
Chris@0
|
167 system_authorized_init('update_authorize_run_update', __DIR__ . '/../../update.authorize.inc', [$updates], $this->t('Update manager'));
|
Chris@0
|
168 $form_state->setRedirectUrl(system_authorized_get_url());
|
Chris@0
|
169 }
|
Chris@0
|
170 }
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 }
|