You are here

function htaccess_deploy in Htaccess 7.2

1 call to htaccess_deploy()
htaccess_confirm_form_submit in ./htaccess.admin.inc
Submit handler for confirm form

File

./htaccess.admin.inc, line 308
Administration pages.

Code

function htaccess_deploy($id) {
  $root_path = DRUPAL_ROOT;
  $htaccess_path = $root_path . '/.htaccess';
  $htaccess_get = db_select('htaccess', 'h')
    ->fields('h')
    ->condition('id', array(
    ':id' => $id,
  ))
    ->execute()
    ->fetchAssoc();
  $htaccess_content = $htaccess_get['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_chmod($htaccess_path, 0644);

    // Get the current htaccess deployed
    $htaccess_current = db_select('htaccess', 'h')
      ->fields('h')
      ->condition('deployed', 1, '=')
      ->execute()
      ->fetchAssoc();

    // If any, set the status to 0
    if ($htaccess_current) {
      db_update('htaccess')
        ->fields(array(
        'deployed' => 0,
      ))
        ->condition('id', $htaccess_current['id'], '=')
        ->execute();
    }

    // Set the status to 1
    db_update('htaccess')
      ->fields(array(
      'deployed' => 1,
    ))
      ->condition('id', $htaccess_get['id'], '=')
      ->execute();
    drupal_set_message(t('Htaccess profile @profile has been deployed.', array(
      '@profile' => $htaccess_get['name'],
    )));
    drupal_goto("admin/config/system/htaccess/deployment");
  }
  else {
    $variables = array(
      '%directory' => $root_path,
      '!htaccess' => '<br />' . nl2br(check_plain($htaccess_content)),
    );
    watchdog('security', "Security warning: Couldn't write .htaccess file.", $variables, WATCHDOG_ERROR);
    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');
    drupal_goto("admin/config/system/htaccess/deployment");
  }
}