View source
<?php
define('REDIRECT_ACCESS_ALLOW', 'allow');
define('REDIRECT_ACCESS_DENY', 'deny');
define('REDIRECT_ACCESS_IGNORE', NULL);
function redirect_entity_info() {
$info['redirect'] = array(
'label' => t('Redirect'),
'base table' => 'redirect',
'controller class' => 'RedirectController',
'entity keys' => array(
'id' => 'rid',
'bundle' => 'type',
'language' => 'language',
),
'fieldable' => FALSE,
'uuid' => FALSE,
'redirect' => FALSE,
);
return $info;
}
function redirect_hook_info() {
$hooks = array(
'redirect_load',
'redirect_load_by_source_alter',
'redirect_access',
'redirect_prepare',
'redirect_validate',
'redirect_presave',
'redirect_insert',
'redirect_update',
'redirect_delete',
'redirect_alter',
);
return array_fill_keys($hooks, array(
'group' => 'redirect',
));
}
function redirect_permission() {
return array(
'administer redirects' => array(
'title' => t('Administer URL redirections'),
),
'administer own redirects' => array(
'title' => t('Administer own redirections'),
),
);
}
function redirect_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/config/search/redirect/404':
$output = '<p>' . t('This page lists all paths that have resulted in 404 errors and do not yet have any redirects assigned to them.') . '</p>';
break;
case 'admin/reports/page-not-found':
break;
}
return $output;
}
function redirect_menu() {
$items['admin/config/search/redirect'] = array(
'title' => 'URL redirects',
'description' => 'Redirect users from one URL to another.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redirect_list_form',
),
'access callback' => 'redirect_access',
'access arguments' => array(
'list',
'redirect',
),
'file' => 'redirect.admin.inc',
);
$items['admin/config/search/redirect/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/search/redirect/add'] = array(
'title' => 'Add redirect',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redirect_edit_form',
),
'access callback' => 'redirect_access',
'access arguments' => array(
'create',
'redirect',
),
'file' => 'redirect.admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/config/search/redirect/edit/%redirect'] = array(
'title' => 'Edit redirect',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redirect_edit_form',
5,
),
'access callback' => 'redirect_access',
'access arguments' => array(
'update',
5,
),
'file' => 'redirect.admin.inc',
);
$items['admin/config/search/redirect/delete/%redirect'] = array(
'title' => 'Delete redirect',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redirect_delete_form',
5,
),
'access callback' => 'redirect_access',
'access arguments' => array(
'delete',
5,
),
'file' => 'redirect.admin.inc',
);
$items['admin/config/search/redirect/settings'] = array(
'title' => 'Settings',
'description' => 'Configure behavior for URL redirects.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redirect_settings_form',
),
'access arguments' => array(
'administer redirects',
),
'file' => 'redirect.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 50,
);
if (module_exists('dblog')) {
$items['admin/config/search/redirect/404'] = array(
'title' => 'Add redirects for 404 pages.',
'description' => 'Add redirects for 404 pages.',
'page callback' => 'redirect_404_list',
'access arguments' => array(
'administer redirects',
),
'file' => 'redirect.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
$items['admin/reports/page-not-found/redirect'] = array(
'title' => 'Fix file not found (404) errors with URL redirects',
'page callback' => 'drupal_goto',
'page arguments' => array(
'admin/config/search/redirect/404',
),
'access arguments' => array(
'administer redirects',
),
'type' => MENU_LOCAL_ACTION,
);
}
if (module_exists('devel_generate')) {
$items['admin/config/development/generate/redirects'] = array(
'title' => 'Generate redirects',
'description' => 'Generate a given number of redirects. Optionally delete current redirects.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redirect_generate_form',
),
'access arguments' => array(
'administer redirects',
),
'file' => 'redirect.generate.inc',
);
$items['admin/config/search/redirect/generate'] = $items['admin/config/development/generate/redirects'];
$items['admin/config/search/redirect/generate']['type'] = MENU_LOCAL_ACTION;
}
return $items;
}
function redirect_set_current_redirect($redirect) {
$static =& drupal_static(__FUNCTION__);
$static = $redirect;
}
function redirect_get_current_redirect() {
$redirect = drupal_static('redirect_set_current_redirect', NULL);
if (!isset($redirect)) {
$redirect = redirect_load_by_source(current_path(), $GLOBALS['language_url']->language, drupal_get_query_parameters());
}
return $redirect;
}
function redirect_url_inbound_alter(&$path, $original_path, $path_language) {
if (empty($_GET['q'])) {
$_GET['q'] = variable_get('site_frontpage', 'node');
return;
}
if ($path != $original_path && $original_path == current_path()) {
$current_langcode = !empty($path_language) ? $path_language : $GLOBALS['language_url']->language;
$current_query = drupal_get_query_parameters();
if ($redirect = redirect_load_by_source($original_path, $current_langcode, $current_query)) {
redirect_set_current_redirect($redirect);
}
}
}
function redirect_entity_info_alter(&$info) {
$default_paths = array(
'node' => 'node/%node',
'user' => 'user/%user',
'taxonomy_term' => 'taxonomy/term/%taxonomy_term',
);
foreach ($default_paths as $entity_type => $default_path) {
if (isset($info[$entity_type]) && !isset($info[$entity_type]['default path'])) {
$info[$entity_type]['default path'] = $default_path;
}
}
$unsupported_entity_types = array(
'comment',
'media',
);
foreach ($unsupported_entity_types as $unsupported_entity_type) {
if (isset($info[$unsupported_entity_type])) {
$info[$unsupported_entity_type]['redirect'] = FALSE;
}
}
}
function redirect_entity_type_supports_redirects($entity_type) {
$types =& drupal_static(__FUNCTION__);
if (!isset($types)) {
$types = array();
foreach (entity_get_info() as $type => $entity_info) {
$types[$type] = !empty($entity_info['uri callback']) && (!isset($entity_info['redirect']) || !empty($entity_info['redirect']));
}
}
return isset($types[$entity_type]) ? $types[$entity_type] : FALSE;
}
function redirect_init() {
if (!redirect_can_redirect()) {
return;
}
if ($redirect = redirect_get_current_redirect()) {
redirect_redirect($redirect);
}
$redirect_global = FALSE;
$request_uri = $original_uri = ltrim(request_uri(), '/');
if (variable_get('redirect_global_clean', 1) && variable_get('clean_url', 0) && strpos($request_uri, '?q=') !== FALSE) {
}
if (strpos($request_uri, 'index.php') !== FALSE) {
}
if ($redirect_global && $request_uri != $original_uri) {
redirect_redirect(array(
'type' => 'global',
));
}
}
function redirect_cron() {
redirect_purge_inactive_redirects();
}
function redirect_exit($destination = NULL) {
if (drupal_get_http_header('Location') && ($rid = drupal_get_http_header('X-Redirect-ID'))) {
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
db_update('redirect')
->fields(array(
'access' => REQUEST_TIME,
))
->expression('count', 'count + 1')
->condition('rid', $rid)
->execute();
}
}
function redirect_entity_delete($entity, $entity_type) {
if (redirect_entity_type_supports_redirects($entity_type)) {
redirect_delete_by_entity_path($entity_type, $entity);
}
}
function redirect_path_update(array $path) {
if (!variable_get('redirect_auto_redirect', TRUE)) {
return;
}
elseif (isset($path['redirect']) && !$path['redirect']) {
return;
}
if (!empty($path['original']['pid']) && $path['original']['pid'] == $path['pid'] && $path['original']['alias'] != $path['alias']) {
redirect_disable_by_path($path['alias'], $path['language']);
$redirect = new stdClass();
redirect_object_prepare($redirect);
$redirect->source = $path['original']['alias'];
$redirect->redirect = $path['source'];
$redirect->language = $path['original']['language'];
$hash = redirect_hash($redirect);
$existing = redirect_load_by_hash($hash);
if (!$existing) {
redirect_save($redirect);
}
elseif (isset($existing->status) && $existing->status == 0) {
$existing->status = 1;
redirect_save($existing);
}
}
}
function redirect_path_insert(array $path) {
if (!empty($path['alias'])) {
redirect_disable_by_path($path['alias'], $path['language']);
}
}
function redirect_path_delete($path) {
if (!variable_get('redirect_auto_redirect_delete', FALSE)) {
return;
}
elseif (isset($path['redirect']) && !$path['redirect']) {
return;
}
elseif (empty($path)) {
return;
}
if (!redirect_load_by_source($path['alias'], $path['language']) && drupal_valid_path($path['source'])) {
$redirect = new stdClass();
redirect_object_prepare($redirect);
$redirect->source = $path['alias'];
$redirect->redirect = $path['source'];
$redirect->language = $path['language'];
$hash = redirect_hash($redirect);
$existing = redirect_load_by_hash($hash);
if (!$existing) {
redirect_save($redirect);
}
elseif (property_exists($existing, 'status') && $existing->status == 0) {
$existing->status = 1;
redirect_save($existing);
}
}
}
function redirect_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'redirect') . '/views',
);
}
function redirect_page_build(&$page) {
if (redirect_is_current_page_404() && user_access('administer redirects')) {
if (!isset($page['content']['system_main']['actions'])) {
$page['content']['system_main']['actions'] = array(
'#theme' => 'links',
'#links' => array(),
'#attributes' => array(
'class' => array(
'action-links',
),
),
'#weight' => -100,
);
}
$destination = drupal_get_destination();
$page['content']['system_main']['actions']['#links']['add_redirect'] = array(
'title' => t('Add URL redirect from this page to another location'),
'href' => 'admin/config/search/redirect/add',
'query' => array(
'source' => $destination['destination'],
) + drupal_get_destination(),
);
}
}
function redirect_form_node_form_alter(&$form, $form_state) {
if (!empty($form['path']['pid']['#value']) && !isset($form['path']['original'])) {
$form['path']['original'] = array(
'#type' => 'value',
'#value' => path_load($form['path']['pid']['#value']),
);
}
}
function redirect_form_taxonomy_form_term_alter(&$form, $form_state) {
if (!empty($form['path']['pid']['#value']) && !isset($form['path']['original'])) {
$form['path']['original'] = array(
'#type' => 'value',
'#value' => path_load($form['path']['pid']['#value']),
);
}
}
function redirect_form_path_admin_form_alter(&$form, $form_state) {
if (!empty($form['pid']['#value']) && !isset($form['original'])) {
$form['original'] = array(
'#type' => 'value',
'#value' => path_load($form['pid']['#value']),
);
}
}
function redirect_load($rid, $reset = FALSE) {
$redirects = entity_load('redirect', array(
$rid,
), array(), $reset);
if (!empty($redirects)) {
drupal_alter('redirect_load', $redirects);
return !empty($redirects) ? reset($redirects) : FALSE;
}
return FALSE;
}
function redirect_load_by_hash($hash, $reset = FALSE) {
$redirects = entity_load('redirect', FALSE, array(
'hash' => $hash,
), $reset);
return !empty($redirects) ? reset($redirects) : FALSE;
}
function redirect_fetch_rids_by_path($source, $language, $enabled_only = FALSE) {
static $status_field_exists = NULL;
if (!isset($status_field_exists)) {
$status_field_exists = db_field_exists('redirect', 'status');
}
$rid_query = db_select('redirect');
$rid_query
->addField('redirect', 'rid');
if ($enabled_only && $status_field_exists) {
$rid_query
->condition('status', 1);
}
if ($source != variable_get('site_frontpage', 'node')) {
$rid_query
->condition('source', db_like($source), 'LIKE');
}
else {
$source_condition = db_or();
$source_condition
->condition('source', db_like($source), 'LIKE');
$source_condition
->condition('source', '');
$rid_query
->condition($source_condition);
}
$rid_query
->condition('language', array(
$language,
LANGUAGE_NONE,
));
$rid_query
->addTag('redirect_fetch');
$rids = $rid_query
->execute()
->fetchCol();
return $rids;
}
function redirect_load_by_source($source, $language = LANGUAGE_NONE, array $query = array(), $enabled_only = TRUE) {
$rids = redirect_fetch_rids_by_path($source, $language, $enabled_only);
if ($rids && ($redirects = redirect_load_multiple($rids))) {
foreach ($redirects as $rid => $redirect) {
if (!empty($redirect->source_options['query'])) {
if (empty($query) || !redirect_compare_array_recursive($redirect->source_options['query'], $query)) {
unset($redirects[$rid]);
continue;
}
}
if ($source !== $redirect->source) {
$redirects[$rid]->weight = 1;
}
}
if (!empty($redirects)) {
uasort($redirects, '_redirect_uasort');
$context = array(
'language' => $language,
'query' => $query,
);
drupal_alter('redirect_load_by_source', $redirects, $source, $context);
return !empty($redirects) ? reset($redirects) : FALSE;
}
}
return FALSE;
}
function redirect_load_multiple($rids = array(), array $conditions = array(), $reset = FALSE) {
return entity_load('redirect', $rids, $conditions, $reset);
}
function redirect_access($op, $redirect, $account = NULL) {
global $user;
$rights =& drupal_static(__FUNCTION__, array());
if (!$redirect || !in_array($op, array(
'create',
'update',
'delete',
'list',
), TRUE)) {
return FALSE;
}
if (empty($account)) {
$account = $user;
}
$cid = isset($redirect->rid) ? $redirect->rid : $redirect;
if (isset($rights[$account->uid][$cid][$op])) {
return $rights[$account->uid][$cid][$op];
}
$rights[$account->uid][$cid][$op] = FALSE;
if (user_access('administer redirects', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
}
elseif (user_access('administer own redirects')) {
$rights[$account->uid][$cid][$op] = $op == 'create' || $op == 'list' || $redirect->uid == $account->uid;
}
$access = module_invoke_all('redirect_access', $op, $redirect, $account);
if (in_array(REDIRECT_ACCESS_DENY, $access, TRUE)) {
$rights[$account->uid][$cid][$op] = FALSE;
}
elseif (in_array(REDIRECT_ACCESS_ALLOW, $access, TRUE)) {
$rights[$account->uid][$cid][$op] = TRUE;
}
return $rights[$account->uid][$cid][$op];
}
function redirect_validate($redirect, $form, &$form_state) {
$redirect = (object) $redirect;
if (url($redirect->source) == url($redirect->redirect)) {
form_set_error('redirect', t('You are attempting to redirect the page to itself. This will result in an infinite loop.'));
}
redirect_hash($redirect);
if ($existing = redirect_load_by_hash($redirect->hash)) {
if ($redirect->rid != $existing->rid) {
form_set_error('source', t('A redirect already exists for the source path %source. Do you want to <a href="@edit-page">edit the existing redirect</a>?', array(
'%source' => redirect_url($redirect->source, $redirect->source_options),
'@edit-page' => url('admin/config/search/redirect/edit/' . $existing->rid),
)));
}
}
foreach (module_implements('redirect_validate') as $module) {
$function = $module . '_redirect_validate';
$function($redirect, $form, $form_state);
}
}
function redirect_object_prepare($redirect, $defaults = array()) {
$defaults += array(
'rid' => NULL,
'type' => 'redirect',
'uid' => $GLOBALS['user']->uid,
'source_options' => array(),
'redirect_options' => array(),
'language' => LANGUAGE_NONE,
'status_code' => 0,
'count' => 0,
'access' => 0,
'hash' => '',
'status' => 1,
);
foreach ($defaults as $key => $default) {
if (!isset($redirect->{$key})) {
$redirect->{$key} = $default;
}
}
module_invoke_all('redirect_prepare', $redirect);
}
function redirect_save($redirect) {
$transaction = db_transaction();
try {
if (!empty($redirect->rid) && !isset($redirect->original)) {
$redirect->original = entity_load_unchanged('redirect', $redirect->rid);
}
if (!isset($redirect->is_new)) {
$redirect->is_new = empty($redirect->rid);
}
redirect_hash($redirect);
if ($redirect->is_new || $redirect->hash != $redirect->original->hash) {
$redirect->count = 0;
$redirect->access = 0;
}
module_invoke_all('redirect_presave', $redirect);
module_invoke_all('entity_presave', $redirect, 'redirect');
if ($exists = redirect_load_by_hash($redirect->hash)) {
$redirect->rid = $exists->rid;
$redirect->is_new = FALSE;
}
if ($redirect->is_new) {
drupal_write_record('redirect', $redirect);
module_invoke_all('redirect_insert', $redirect);
module_invoke_all('entity_insert', $redirect, 'redirect');
}
else {
drupal_write_record('redirect', $redirect, array(
'rid',
));
module_invoke_all('redirect_update', $redirect);
module_invoke_all('entity_update', $redirect, 'redirect');
}
unset($redirect->is_new);
unset($redirect->original);
entity_get_controller('redirect')
->resetCache(array(
$redirect->rid,
));
db_ignore_slave();
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception('redirect', $e);
throw $e;
}
}
function redirect_redirect_insert($redirect) {
redirect_page_cache_clear($redirect);
}
function redirect_redirect_update($redirect) {
redirect_page_cache_clear($redirect);
if (!empty($redirect->original) && $redirect->original->source != $redirect->source) {
redirect_page_cache_clear($redirect->original);
}
}
function redirect_redirect_delete($redirect) {
redirect_page_cache_clear($redirect);
}
function redirect_delete($rid) {
return redirect_delete_multiple(array(
$rid,
));
}
function redirect_delete_by_path($path) {
$query = db_select('redirect');
$query
->addField('redirect', 'rid');
$query_or = db_or();
if (variable_get('redirect_delete_by_source_path', TRUE)) {
$query_or
->condition('source', db_like($path), 'LIKE');
$query_or
->condition('source', db_like($path . '/') . '%', 'LIKE');
}
$query_or
->condition('redirect', db_like($path), 'LIKE');
$query_or
->condition('redirect', db_like($path . '/') . '%', 'LIKE');
$query
->condition($query_or);
$rids = $query
->execute()
->fetchCol();
if ($rids) {
return redirect_delete_multiple($rids);
}
}
function redirect_disable_by_path($path, $language) {
$rids = redirect_fetch_rids_by_path($path, $language, FALSE);
if ($rids) {
return redirect_change_status_multiple($rids, 0);
}
}
function redirect_delete_by_entity_path($entity_type, $entity) {
$uri = entity_uri($entity_type, $entity);
if (!empty($uri['path'])) {
redirect_delete_by_path($uri['path']);
}
}
function redirect_change_status_multiple(array $rids, $status) {
if ($status !== 0 && $status !== 1 && $status !== '0' && $status !== '1') {
watchdog('Cannot change redirect status to %status', array(
'%status' => $status,
));
drupal_set_message(t('Cannot change redirect status to %status', array(
'%status' => $status,
)));
return;
}
if (!empty($rids)) {
$redirects = redirect_load_multiple($rids, array(), TRUE);
foreach ($redirects as $rid => $redirect) {
if (isset($redirect->status) && $redirect->status == $status) {
continue;
}
$redirect->status = $status;
redirect_save($redirect);
if ($status) {
$redirect_link = l($redirect->redirect, $redirect->redirect) . '=> ' . l($redirect->source, $redirect->source);
watchdog('redirect', 'Enabled redirect: !redirect_link', array(
'!redirect_link' => $redirect_link,
));
}
else {
$redirect_link = l($redirect->redirect, $redirect->redirect) . '=> ' . l($redirect->source, $redirect->source);
watchdog('redirect', 'Disabled redirect: !redirect_link', array(
'!redirect_link' => $redirect_link,
));
}
}
}
}
function redirect_delete_multiple(array $rids) {
$transaction = db_transaction();
if (!empty($rids)) {
$redirects = redirect_load_multiple($rids);
try {
foreach ($redirects as $rid => $redirect) {
module_invoke_all('redirect_delete', $redirect);
module_invoke_all('entity_delete', $redirect, 'redirect');
}
db_delete('redirect')
->condition('rid', $rids, 'IN')
->execute();
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception('redirect', $e);
throw $e;
}
entity_get_controller('redirect')
->resetCache();
}
}
function redirect_purge_inactive_redirects(array $types = array(
'redirect',
), $interval = NULL) {
if (!isset($interval)) {
$interval = variable_get('redirect_purge_inactive', 0);
}
if (!$interval) {
return FALSE;
}
if (variable_get('redirect_page_cache', 0) && !variable_get('page_cache_invoke_hooks', TRUE)) {
watchdog('redirect', 'Due to existing settings, could not track when a redirect is used, so could not remove unused redirects.');
return FALSE;
}
$query = db_select('redirect');
$query
->addField('redirect', 'rid');
if (!empty($types)) {
$query
->condition('type', $types);
}
$query
->condition('access', REQUEST_TIME - $interval, '<');
$query
->range(0, variable_get('redirect_purge_amount', 100));
$query
->addTag('redirect_purge');
$rids = $query
->execute()
->fetchCol();
if (count($rids)) {
redirect_delete_multiple($rids);
watchdog('redirect', format_plural(count($rids), 'Removed 1 inactive redirect from the database.', 'Removed @count inactive redirects from the database.'));
return $rids;
}
}
function redirect_redirect($redirect = NULL) {
if (!isset($redirect)) {
$redirect = new stdClass();
}
redirect_object_prepare($redirect, array(
'redirect' => current_path(),
'type' => 'manual',
'callback' => 'redirect_goto',
'cache' => TRUE,
));
if ($scheme = file_uri_scheme($redirect->redirect)) {
if (file_stream_wrapper_valid_scheme($scheme)) {
if ($external_path = file_create_url($redirect->redirect)) {
$redirect->redirect = $external_path;
}
}
}
if (empty($redirect->status_code)) {
$redirect->status_code = variable_get('redirect_default_status_code', 301);
}
$passthrough_querystring = variable_get('redirect_passthrough_querystring', 1);
if (isset($redirect->redirect_options['passthrough_querystring'])) {
$passthrough_querystring = $redirect->redirect_options['passthrough_querystring'];
}
if ($passthrough_querystring) {
$redirect->redirect_options += array(
'query' => array(),
);
$redirect->redirect_options['query'] += drupal_get_query_parameters();
}
drupal_alter('redirect', $redirect);
if (isset($redirect->redirect) && isset($redirect->callback) && $redirect->redirect !== FALSE && function_exists($redirect->callback)) {
if ($GLOBALS['base_root'] . request_uri() == url($redirect->redirect, array(
'absolute' => TRUE,
) + $redirect->redirect_options)) {
return FALSE;
}
$callback = $redirect->callback;
$callback($redirect);
}
}
function redirect_goto($redirect) {
$redirect->redirect_options['absolute'] = TRUE;
if (file_exists(DRUPAL_ROOT . '/' . $redirect->redirect)) {
$url = url(file_create_url($redirect->redirect), $redirect->redirect_options);
}
else {
$url = url($redirect->redirect, $redirect->redirect_options);
}
drupal_add_http_header('Location', $url);
drupal_add_http_header('Status', redirect_status_code_options($redirect->status_code));
if (!empty($redirect->rid)) {
drupal_add_http_header('X-Redirect-ID', $redirect->rid);
}
if (!variable_get('redirect_page_cache', 0) || !variable_get('cache', 0) || !drupal_page_is_cacheable() || empty($redirect->cache)) {
drupal_exit($url);
}
if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) {
if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
module_invoke_all('exit', $url);
}
drupal_session_commit();
if (variable_get('cache', 0)) {
echo ' ';
if ($cache = drupal_page_set_cache()) {
drupal_serve_page_from_cache($cache);
}
}
}
exit;
}
function redirect_hash($redirect) {
$hash = array(
'source' => $redirect->source,
'language' => $redirect->language,
);
if (!empty($redirect->source_options['query'])) {
$hash['source_query'] = $redirect->source_options['query'];
}
drupal_alter('redirect_hash', $hash, $redirect);
redirect_sort_recursive($hash, 'ksort');
$redirect->hash = drupal_hash_base64(serialize($hash));
return $redirect->hash;
}
function redirect_page_cache_clear($redirect = NULL) {
if (!variable_get('redirect_page_cache', 0)) {
return;
}
if (isset($redirect)) {
$path = url($redirect->source, array(
'absolute' => TRUE,
));
cache_clear_all($path, 'cache_page', TRUE);
}
else {
cache_clear_all('*', 'cache_page', TRUE);
}
}
function redirect_load_entity_from_path($path) {
$entity_paths =& drupal_static(__FUNCTION__);
if (!isset($entity_paths)) {
$entity_paths = array();
foreach (entity_get_info() as $entity_type => $entity_info) {
if (isset($entity_info['default path'])) {
$default_path = $entity_info['default path'];
$default_path = preg_quote($default_path, '/');
$default_path = str_replace(preg_quote('%' . $entity_type, '/'), '(\\d+)', $default_path);
$entity_paths[$entity_type] = $default_path;
}
}
}
foreach ($entity_paths as $entity_type => $default_path) {
if (preg_match("/^{$default_path}\$/", $path, $matches)) {
if ($entity = entity_load($entity_type, array(
$matches[1],
))) {
return array(
'entity_type' => $entity_type,
'entity' => reset($entity),
);
}
break;
}
}
}
function redirect_can_redirect() {
$can_redirect =& drupal_static(__FUNCTION__);
if (!isset($can_redirect)) {
$path = current_path();
$can_redirect = TRUE;
if (!preg_match('/index\\.php$/', $_SERVER['SCRIPT_NAME'])) {
$can_redirect = FALSE;
}
elseif (!empty($_POST)) {
$can_redirect = FALSE;
}
elseif (drupal_is_cli()) {
$can_redirect = FALSE;
}
elseif ((variable_get('maintenance_mode', 0) || defined('MAINTENANCE_MODE')) && !user_access('access site in maintenance mode')) {
$can_redirect = FALSE;
}
elseif (!variable_get('redirect_global_admin_paths', 0) && path_is_admin($path)) {
$can_redirect = FALSE;
}
}
return $can_redirect;
}
function redirect_compare_array_recursive($match, $haystack) {
foreach ($match as $key => $value) {
if (!array_key_exists($key, $haystack)) {
return FALSE;
}
elseif (is_array($value)) {
if (!is_array($haystack[$key])) {
return FALSE;
}
elseif (!redirect_compare_array_recursive($value, $haystack[$key])) {
return FALSE;
}
}
elseif ($value != $haystack[$key]) {
return FALSE;
}
}
return TRUE;
}
function redirect_sort_recursive(&$array, $callback = 'sort') {
$result = $callback($array);
foreach ($array as $key => $value) {
if (is_array($value)) {
$result &= redirect_sort_recursive($array[$key], $callback);
}
}
return $result;
}
function redirect_language_load($language = LANGUAGE_NONE) {
$languages =& drupal_static(__FUNCTION__);
if (!isset($languages)) {
$languages = language_list();
$languages[LANGUAGE_NONE] = NULL;
}
return isset($languages[$language]) ? $languages[$language] : NULL;
}
function redirect_url($path, array $options = array(), $clean_url = NULL) {
if (!isset($clean_url)) {
$clean_url = variable_get('clean_url', 0);
}
if ($path == '') {
$path = '<front>';
}
if (!isset($options['alter']) || !empty($options['alter'])) {
drupal_alter('redirect_url', $path, $options);
}
if (!isset($options['base_url'])) {
if (isset($options['https']) && variable_get('https', FALSE)) {
if ($options['https'] === TRUE) {
$options['base_url'] = $GLOBALS['base_secure_url'];
$options['absolute'] = TRUE;
}
elseif ($options['https'] === FALSE) {
$options['base_url'] = $GLOBALS['base_insecure_url'];
$options['absolute'] = TRUE;
}
}
else {
$options['base_url'] = $GLOBALS['base_url'];
}
}
if (empty($options['absolute']) || url_is_external($path)) {
$url = $path;
}
else {
$url = $options['base_url'] . base_path() . $path;
}
if (isset($options['query'])) {
$url .= $clean_url ? '?' : '&';
$url .= drupal_http_build_query($options['query']);
}
if (isset($options['fragment'])) {
$url .= '#' . $options['fragment'];
}
$url = str_replace($GLOBALS['base_url'] . '/', '', $url);
return $url;
}
function redirect_variables() {
return array(
'redirect_default_status_code' => 301,
'redirect_auto_redirect' => TRUE,
'redirect_auto_redirect_delete' => FALSE,
'redirect_warning' => FALSE,
'redirect_passthrough_querystring' => 1,
'redirect_page_cache' => 0,
'redirect_purge_inactive' => 0,
'redirect_purge_amount' => 100,
'redirect_global_home' => 1,
'redirect_global_clean' => 1,
'redirect_global_canonical' => 1,
'redirect_global_admin_paths' => 0,
);
}
function redirect_parse_url($url) {
$original_url = $url;
$is_external = url_is_external($url);
if (!$is_external) {
$url = trim($url, " \t\n\r\0\v\\/");
}
else {
$url = trim($url);
}
$parsed = parse_url($url);
if (isset($parsed['fragment'])) {
$url = substr($url, 0, -strlen($parsed['fragment']));
$url = trim($url, '#');
}
if (variable_get('clean_url', 0) == 0 && strpos($url, '&')) {
$parsed['path'] = substr($url, 0, strpos($url, '&'));
$parsed['query'] = substr($url, strpos($url, '&') + 1);
}
if (isset($parsed['query'])) {
$url = substr($url, 0, -strlen($parsed['query']));
$url = trim($url, '?&');
parse_str($parsed['query'], $parsed['query']);
}
if (isset($parsed['scheme']) && isset($parsed['host'])) {
$base_secure_url = rtrim($GLOBALS['base_secure_url'] . base_path(), '/');
$base_insecure_url = rtrim($GLOBALS['base_insecure_url'] . base_path(), '/');
if (strpos($url, $base_secure_url) === 0) {
$url = str_replace($base_secure_url, '', $url);
$parsed['https'] = TRUE;
}
elseif (strpos($url, $base_insecure_url) === 0) {
$url = str_replace($base_insecure_url, '', $url);
}
}
if (!$is_external) {
$url = trim($url, '/');
}
if ($url == '<front>') {
$url = '';
}
$parsed['url'] = $url;
drupal_alter('redirect_parse_url', $parsed, $original_url);
return $parsed;
}
function redirect_status_code_options($code = NULL) {
$codes = array(
300 => '300 ' . t('Multiple Choices'),
301 => '301 ' . t('Moved Permanently'),
302 => '302 ' . t('Found'),
303 => '303 ' . t('See Other'),
304 => '304 ' . t('Not Modified'),
305 => '305 ' . t('Use Proxy'),
307 => '307 ' . t('Temporary Redirect'),
);
return isset($codes[$code]) ? $codes[$code] : $codes;
}
function redirect_is_current_page_404() {
return drupal_get_http_header('Status') == '404 Not Found';
}
function _redirect_uasort($a, $b) {
$a_weight = isset($a->weight) ? $a->weight : 0;
$b_weight = isset($b->weight) ? $b->weight : 0;
if ($a_weight != $b_weight) {
return $a_weight > $b_weight ? -1 : 1;
}
if ($a->language != $b->language) {
return $a->language != LANGUAGE_NONE ? -1 : 1;
}
$a_query_count = isset($a->source_options['query']) ? count($a->source_options['query']) : 0;
$b_query_count = isset($b->source_options['query']) ? count($b->source_options['query']) : 0;
if ($a_query_count != $b_query_count) {
return $a_query_count > $b_query_count ? -1 : 1;
}
return $a->rid > $b->rid ? -1 : 1;
}
function locale_form_redirect_edit_form_alter(&$form, &$form_state) {
$form['language'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#options' => array(
LANGUAGE_NONE => t('All languages'),
) + locale_language_list('name'),
'#default_value' => $form['language']['#value'],
'#description' => t('A redirect set for a specific language will always be used when requesting this page in that language, and takes precedence over redirects set for <em>All languages</em>.'),
);
}
function redirect_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
list($id) = entity_extract_ids($entity_type, $entity);
if (!empty($form['redirect']) || empty($id)) {
return;
}
if (!redirect_entity_type_supports_redirects($entity_type)) {
return;
}
$uri = entity_uri($entity_type, $entity);
if (empty($uri['path'])) {
return;
}
$info = entity_get_info($entity_type);
$form['redirect'] = array(
'#type' => 'fieldset',
'#title' => t('URL redirects'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => redirect_access('list', 'redirect'),
'#weight' => 30,
'#attributes' => array(
'class' => array(
'redirect-list',
),
),
);
foreach (element_children($form) as $key) {
if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
$form['redirect']['#group'] = $key;
$form['redirect']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'redirect') . '/redirect.js';
}
}
$redirect = array(
'redirect' => $uri['path'],
'redirect_options' => array_diff_key($uri['options'], array(
'entity_type' => '',
'entity' => '',
)),
'language' => $langcode,
);
$form['redirect']['actions'] = array(
'#theme' => 'links',
'#links' => array(),
'#attributes' => array(
'class' => array(
'action-links',
),
),
);
if (redirect_access('create', 'redirect')) {
$form['redirect']['actions']['#links']['add'] = array(
'title' => t('Add URL redirect'),
'href' => 'admin/config/search/redirect/add',
'query' => array_filter($redirect) + drupal_get_destination(),
);
}
module_load_include('inc', 'redirect', 'redirect.admin');
$redirects = redirect_load_multiple(FALSE, array(
'redirect' => $uri['path'],
));
$header = array(
'source',
'status',
'status_code',
'language',
'count',
'access',
'operations',
);
$form['redirect'] += redirect_list_table($redirects, $header);
}
function redirect_field_extra_fields() {
$entity_info = entity_get_info();
$info = array();
foreach (array_keys($entity_info) as $entity_type) {
if (!redirect_entity_type_supports_redirects($entity_type)) {
continue;
}
if (!empty($entity_info[$entity_type]['bundles'])) {
foreach (array_keys($entity_info[$entity_type]['bundles']) as $bundle) {
if (!isset($entity_info[$entity_type]['bundles'][$bundle]['uri callback']) && !isset($entity_info[$entity_type]['uri callback'])) {
continue;
}
$info[$entity_type][$bundle]['form']['redirect'] = array(
'label' => t('URL redirects'),
'description' => t('Redirect module form elements'),
'weight' => 30,
);
}
}
}
return $info;
}
function redirect_get_redirect_operations() {
$operations =& drupal_static(__FUNCTION__);
if (!isset($operations)) {
$operations = module_invoke_all('redirect_operations');
drupal_alter('redirect_operations', $operations);
}
return $operations;
}
function redirect_redirect_operations() {
$operations['delete'] = array(
'action' => t('Delete'),
'action_past' => t('Deleted'),
'callback' => 'redirect_delete_multiple',
'confirm' => TRUE,
);
$operations['disable'] = array(
'action' => t('Disable'),
'action_past' => t('Disabled'),
'callback' => 'redirect_change_status_multiple',
'callback arguments' => array(
0,
),
'confirm' => TRUE,
);
$operations['enable'] = array(
'action' => t('Enable'),
'action_past' => t('Enabled'),
'callback' => 'redirect_change_status_multiple',
'callback arguments' => array(
1,
),
'confirm' => TRUE,
);
return $operations;
}
function mpac_form_redirect_edit_form_alter(&$form, &$form_state) {
if ($form['redirect']['#type'] == 'textfield') {
$form['redirect']['#autocomplete_path'] = 'mpac/autocomplete/menu';
$form['redirect']['#description'] .= '<br />' . t("You may enter the title of the node you'd like to link to to get a list of all possible matches.");
if (module_exists('path')) {
$form['redirect']['#description'] .= ' ' . t('Matches marked with %marker are URL aliases.', array(
'%marker' => t('*'),
));
}
}
}