View source
<?php
define('HTACCESS_TEMPLATE_PATH', drupal_get_path('module', 'htaccess') . '/includes/htaccess.txt');
function htaccess_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#htaccess":
$output = '<p>' . t("Auto generate htaccess. ") . '</p>';
break;
}
return $output;
}
function htaccess_permission() {
return array(
'administer htaccess module' => array(
'title' => t('Administer htaccess module'),
'description' => t('Ability to generate htaccess.'),
),
);
}
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() {
$root_path = DRUPAL_ROOT;
$htaccess_filepath = $root_path . '/.htaccess';
$htaccess_current = file_get_contents($htaccess_filepath);
if ($htaccess_current) {
$htaccess_deployed = db_select('htaccess', 'h')
->fields('h')
->condition('deployed', 1, '=')
->execute()
->fetchAssoc();
if ($htaccess_deployed) {
$htaccess_deployed = $htaccess_deployed['htaccess'];
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);
}
}
}
}