You are here

htaccess.module in Htaccess 7.2

Same filename and directory in other branches
  1. 8.2 htaccess.module
  2. 7 htaccess.module

Htaccess is a module which autogenerates a Drupal root htaccess file based on your settings.

File

htaccess.module
View source
<?php

/**
 * @file
 * Htaccess is a module which autogenerates a Drupal root htaccess
 * file based on your settings.
 */
define('HTACCESS_TEMPLATE_PATH', drupal_get_path('module', 'htaccess') . '/includes/htaccess.txt');

/**
 * Implementation of hook_help()
 */
function htaccess_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#htaccess":
      $output = '<p>' . t("Auto generate htaccess. ") . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_permission()
 */
function htaccess_permission() {
  return array(
    'administer htaccess module' => array(
      'title' => t('Administer htaccess module'),
      'description' => t('Ability to generate htaccess.'),
    ),
  );
}

/**
 * Implementation of hook_menu()
 */
function htaccess_menu() {
  $items = array();
  $items['admin/config/system/htaccess'] = array(
    'title' => t('Htaccess'),
    'description' => t('Configure htaccess.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htaccess_settings',
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/system/htaccess/main'] = array(
    'title' => t('Settings'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/config/system/htaccess/generate'] = array(
    'title' => t('Generate'),
    'description' => t('Generate htaccess.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htaccess_generate',
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 20,
  );
  $items['admin/config/system/htaccess/deployment'] = array(
    'title' => t('Deployment'),
    'description' => t('Deploy htaccess.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htaccess_deployment',
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 30,
  );
  $items['admin/config/system/htaccess/deployment/view/%'] = array(
    'page callback' => 'htaccess_display',
    'page arguments' => array(
      6,
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_CALLBACK,
    'weight' => 30,
  );
  $items['admin/config/system/htaccess/deployment/deploy/%'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htaccess_confirm_form',
      'deploy',
      6,
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_CALLBACK,
    'weight' => 30,
  );
  $items['admin/config/system/htaccess/deployment/download/%'] = array(
    'page callback' => 'htaccess_download',
    'page arguments' => array(
      6,
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_CALLBACK,
    'weight' => 30,
  );
  $items['admin/config/system/htaccess/deployment/delete/%'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htaccess_confirm_form',
      'delete',
      6,
    ),
    'access arguments' => array(
      'administer htaccess module',
    ),
    'file' => 'htaccess.admin.inc',
    'type' => MENU_CALLBACK,
    'weight' => 30,
  );
  return $items;
}
function htaccess_generate_default() {
  $htaccess_template = file_get_contents(HTACCESS_TEMPLATE_PATH);
  $with_www_default = "#RewriteCond %{HTTP_HOST} .\n";
  $with_www_default .= "#RewriteCond %{HTTP_HOST} !^www\\. [NC]\n";
  $with_www_default .= "#RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n";
  $without_www_default = "#RewriteCond %{HTTP_HOST} ^www\\.(.+)\$ [NC]\n";
  $without_www_default .= "#RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]\n";
  $search = array(
    "%%%rules_before%%%",
    "%%%prevent_txt%%%",
    "%%%symbolic_links%%%",
    "%%%ssl_force_redirect%%%",
    "%%%with_www%%%",
    "%%%without_www%%%",
    "%%%boost_rules%%%",
  );
  $replace = array(
    "",
    "",
    "+FollowSymLinks",
    "",
    $with_www_default,
    $without_www_default,
    "",
  );
  $htaccess_update = str_replace($search, $replace, $htaccess_template);
  db_update('htaccess')
    ->fields(array(
    'htaccess' => $htaccess_update,
  ))
    ->execute();
}
function htaccess_cron() {

  // try to get current htaccess content
  $root_path = DRUPAL_ROOT;
  $htaccess_filepath = $root_path . '/.htaccess';
  $htaccess_current = file_get_contents($htaccess_filepath);
  if ($htaccess_current) {

    // Get the current htaccess deployed
    $htaccess_deployed = db_select('htaccess', 'h')
      ->fields('h')
      ->condition('deployed', 1, '=')
      ->execute()
      ->fetchAssoc();
    if ($htaccess_deployed) {
      $htaccess_deployed = $htaccess_deployed['htaccess'];

      // Compare the two results
      if ($htaccess_current === $htaccess_deployed) {
        variable_del('htaccess_altered');
      }
      else {
        drupal_set_message(t('The htaccess seems to be altered.'), 'warning');
        variable_set('htaccess_altered', true);
      }
    }
  }
}

Functions

Namesort descending Description
htaccess_cron
htaccess_generate_default
htaccess_help Implementation of hook_help()
htaccess_menu Implementation of hook_menu()
htaccess_permission Implementation of hook_permission()

Constants

Namesort descending Description
HTACCESS_TEMPLATE_PATH @file Htaccess is a module which autogenerates a Drupal root htaccess file based on your settings.