public function HtaccessConfirmForm::submitForm in Htaccess 8.2
Submit handler for confirm form
Overrides FormInterface::submitForm
File
- src/
Form/ HtaccessConfirmForm.php, line 119 - Administration pages.
Class
- HtaccessConfirmForm
- Defines a form to confirm Htaccess deployment or deletion
Namespace
Drupal\htaccess\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
switch ($this->action) {
case 'Deploy':
$root_path = \Drupal::root();
$htaccess_path = $root_path . '/.htaccess';
$select = Database::getConnection()
->select('htaccess', 'h');
$select
->fields('h');
$select
->condition('id', $this->id);
$results = $select
->execute();
$result = $results
->fetch();
$htaccess_content = $result->htaccess;
// Remove utf8-BOM
$htaccess_content = str_replace("", '', $htaccess_content);
// Standardize the EOL.
$htaccess_content = str_replace("\r\n", PHP_EOL, $htaccess_content);
// Try to write to the .htaccess file
if (file_put_contents($htaccess_path, $htaccess_content)) {
\Drupal::service("file_system")
->chmod($htaccess_path, 0644);
// Get the current htaccess deployed
$htaccess_current = Database::getConnection()
->select('htaccess', 'h');
$htaccess_current
->fields('h');
$htaccess_current
->condition('deployed', 1);
$results = $htaccess_current
->execute();
$current = $results
->fetch();
// If any, set the status to 0
if ($current) {
$disable = Database::getConnection()
->update('htaccess');
$disable
->fields(array(
'deployed' => 0,
));
$disable
->condition('id', $current->id);
$disable
->execute();
}
// Set the status to 1
$deploy = Database::getConnection()
->update('htaccess');
$deploy
->fields(array(
'deployed' => 1,
));
$deploy
->condition('id', $result->id);
$deploy
->execute();
drupal_set_message(t('Htaccess profile @profile has been deployed.', array(
'@profile' => $result->name,
)));
}
else {
$variables = array(
'%directory' => $root_path,
'!htaccess' => '<br />' . nl2br(\Drupal\Component\Utility\SafeMarkup::checkPlain($htaccess_content)),
);
\Drupal::logger('security')
->error("Security warning: Couldn't write .htaccess file.", []);
drupal_set_message(t('Error during deployment: couldn\'t write .htaccess file. You have to download it and manually put it in the root of your Drupal installation.'), 'error');
}
break;
case 'Delete':
// Check that the profile is not in use
$htaccess_check = Database::getConnection()
->select('htaccess', 'h');
$htaccess_check
->fields('h');
$htaccess_check
->condition('deployed', 1);
$htaccess_check
->condition('id', $this->id);
$results = $htaccess_check
->execute();
if (!empty($results
->fetchCol())) {
drupal_set_message(t('This htaccess\'s profile is currently in use'), 'error');
}
else {
$htaccess_get = Database::getConnection()
->delete('htaccess');
$htaccess_get
->condition('id', $this->id);
$htaccess_get
->execute();
drupal_set_message(t('Htaccess profile has been removed.'));
}
break;
}
$form_state
->setRedirect('htaccess.admin_deployment');
}