' . t('Here you can set up URL redirecting for this site. Any existing or non-existing path within this site can redirect to any internal or external URL.') .'

'; break; case 'admin/config/search/path-redirect/add': case 'admin/config/search/path-redirect/edit/%': $output .= '

' . t('If you need advanced redirection functionality (i.e. wildcards, etc.), you should be using a webserver rewriting engine rather than this module.') . '

'; break; } return $output; } /** * Implements hook_permission(). */ function path_redirect_permission() { return array( 'administer redirects' => array( 'title' => t('Administer URL redirections'), 'description' => t('Add, edit or delete URL redirections.'), ), ); } /** * Implements hook_menu(). */ function path_redirect_menu() { $items['admin/config/search/path-redirect'] = array( 'title' => 'URL redirects', 'description' => 'Redirect users from one URL to another.', 'page callback' => 'drupal_get_form', 'page arguments' => array('path_redirect_admin_redirects'), 'access arguments' => array('administer redirects'), 'file' => 'path_redirect.admin.inc', ); $items['admin/config/search/path-redirect/list'] = array( 'title' => 'List', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items['admin/config/search/path-redirect/add'] = array( 'title' => 'Add redirect', 'description' => 'Add a new URL redirect.', 'page callback' => 'drupal_get_form', 'page arguments' => array('path_redirect_edit_form'), 'access arguments' => array('administer redirects'), 'type' => MENU_LOCAL_TASK, 'file' => 'path_redirect.admin.inc', ); $items['admin/config/search/path-redirect/edit/%path_redirect'] = array( 'title' => 'Edit redirect', 'description' => 'Edit an existing URL redirect.', 'page callback' => 'drupal_get_form', 'page arguments' => array('path_redirect_edit_form', 5), 'access arguments' => array('administer redirects'), 'type' => MENU_CALLBACK, 'file' => 'path_redirect.admin.inc', ); $items['admin/config/search/path-redirect/delete/%path_redirect'] = array( 'title' => 'Delete redirect', 'description' => 'Delete an existing URL redirect.', 'page callback' => 'drupal_get_form', 'page arguments' => array('path_redirect_delete_form', 5), 'access arguments' => array('administer redirects'), 'type' => MENU_CALLBACK, 'file' => 'path_redirect.admin.inc', ); $items['admin/config/search/path-redirect/settings'] = array( 'title' => 'Settings', 'description' => 'Configure behavior for URL redirects.', 'page callback' => 'drupal_get_form', 'page arguments' => array('path_redirect_settings_form'), 'access arguments' => array('administer redirects'), 'type' => MENU_LOCAL_TASK, 'weight' => 10, 'file' => 'path_redirect.admin.inc', ); $items['js/path_redirect/autocomplete_404'] = array( 'page callback' => 'path_redirect_js_autocomplete_404', 'access arguments' => array('administer redirects'), 'type' => MENU_CALLBACK, 'file' => 'path_redirect.admin.inc', ); return $items; } /** * Implements hook_menu_local_tasks_alter(). * * @todo Make sure we get these on 404 pages as well? */ //function path_redirect_menu_local_tasks_alter(&$data, $router_item, $root_path) { // if (!path_is_admin($root_path)) { // $item = menu_get_item('admin/config/search/path-redirect/add'); // if ($item['access']) { // $item['title'] = t('Add redirect to this location'); // $item['localized_options']['query'] = drupal_get_destination() + array('redirect' => $_GET['q']); // $data['actions']['output'][] = array( // '#theme' => 'menu_local_action', // '#link' => $item, // ); // } // } //} /** * Implements hook_init(). */ function path_redirect_init() { if (defined('MAINTENANCE_MODE')) { return; } path_redirect_goto(); } function path_redirect_goto($redirect = NULL) { if (!isset($redirect)) { $path = path_redirect_get_path(); $language = $GLOBALS['language']->language; $query = path_redirect_get_query(); $redirect = path_redirect_load_by_source($path, $language, $query); } elseif (is_numeric($redirect)) { $redirect = path_redirect_load($redirect); } if ($redirect) { // Create the absolute redirection URL. $redirect['redirect_url'] = url($redirect['redirect'], array('query' => $redirect['query'], 'fragment' => $redirect['fragment'], 'absolute' => TRUE)); // Update the last used timestamp so that unused redirects can be purged. db_update('path_redirect') ->fields(array( 'last_used' => REQUEST_TIME, )) ->condition('rid', $redirect['rid']) ->execute(); if (url($redirect['redirect']) == url($_GET['q'])) { // Prevent infinite loop redirection. watchdog('path_redirect', 'Redirect to %redirect is causing an infinite loop; redirect cancelled.', array('%redirect' => $redirect['redirect_url']), WATCHDOG_WARNING, l(t('Edit'), 'admin/build/path-redirect/edit/'. $redirect['rid'])); } elseif (variable_get('path_redirect_allow_bypass', 0) && isset($_GET['redirect']) && $_GET['redirect'] === 'no') { // If the user has requested not to be redirected, show a message. drupal_set_message(t('This page has been moved to @redirect.', array('@redirect' => $redirect['redirect_url']))); } elseif (variable_get('path_redirect_redirect_warning', 0)) { // Show a message and automatically redirect after 10 seconds. drupal_set_message(t('This page has been moved to @redirect. You will be automatically redirected in 10 seconds.', array('@redirect' => $redirect['redirect_url'])), 'error'); $element = array( '#tag' => 'meta', '#attributes' => array( 'http-equiv' => 'refresh', 'content' => '10;url=' . $redirect['redirect_url'], ), ); drupal_add_html_head($element, 'meta_refresh'); } else { // Perform the redirect. unset($_REQUEST['destination']); drupal_goto($redirect['redirect_url'], array(), $redirect['type']); } } // If this is being executed as a menu item, return a not found flag. return MENU_NOT_FOUND; } /** * Implements hook_cron(). */ function path_redirect_cron() { // Purge inactive redirects from the database. if ($purge = variable_get('path_redirect_purge_inactive', 0)) { $rids = db_query("SELECT rid FROM {path_redirect} WHERE last_used < :purge", array(':purge' => REQUEST_TIME - $purge))->fetchCol(); if ($rids) { path_redirect_delete_multiple($rids); watchdog('path_redirect', format_plural(count($rids), 'Removed 1 inactive redirect from the database.', 'Removed @count inactive redirects from the database.')); } } } /** * Implements hook_path_redirect_operations(). */ function path_redirect_path_redirect_operations() { $operations = array( 'delete' => array( 'action' => t('Delete'), 'action_past' => t('Deleted'), 'callback' => 'path_redirect_delete_multiple', 'confirm' => TRUE, ), ); return $operations; } /** * Implements hook_form_alter(). * * Add a summary of redirects pointing to a node on its edit form. */ function path_redirect_form_alter(&$form, $form_state, $form_id) { if (!empty($form['#node_edit_form']) && !empty($form['#node']->nid)) { $redirect = array( 'redirect' => 'node/' . $form['#node']->nid, 'language' => $form['#node']->language, ); $form['path_redirect'] = array( '#type' => 'fieldset', '#title' => t('URL redirects'), '#description' => t('The following are a list of URL redirects that point to this location.'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#access' => user_access('administer redirects'), '#weight' => 30, '#group' => 'additional_settings', '#attached' => array( 'js' => array( 'vertical-tabs' => drupal_get_path('module', 'path_redirect') . '/path_redirect.js', ), ), ); module_load_include('inc', 'path_redirect', 'path_redirect.admin'); $form['path_redirect']['table'] = path_redirect_list_redirects(NULL, array('redirect' => $redirect['redirect'])); $form['path_redirect']['actions'] = path_redirect_local_actions($redirect); } } function path_redirect_local_actions(array $redirect = array()) { $links = array( array( 'title' => $redirect ? t('Add redirect to this location') : t('Add redirect'), 'href' => 'admin/config/search/path-redirect/add', 'query' => drupal_get_destination() + $redirect, ), ); return array( '#markup' => theme('links', array('links' => $links, 'attributes' => array('class' => array('action-links')))), ); } /** * Implements hook_node_presave(). * * Creates automatic redirects when URL aliases are manually changed. */ function path_redirect_node_presave($node) { if (isset($node->path['alias'])) { path_redirect_check_alias_changed('node/' . $node->nid, $node->path['alias'], $node->language); } } /** * Implements hook_node_delete(). */ function path_redirect_node_delete(stdClass $node) { $uri = entity_uri('node', $node); path_redirect_delete_multiple(NULL, array('source' => 'node/'. $node->nid)); path_redirect_delete_multiple(NULL, array('redirect' => 'node/'. $node->nid)); if ($uri['path'] != "node/{$node->nid}") { path_redirect_delete_multiple(NULL, array('source' => "node/{$node->nid}")); path_redirect_delete_multiple(NULL, array('redirect' => "node/{$node->nid}")); } } /** * Implements hook_taxonomy_term_delete(). */ function path_redirect_taxonomy_term_delete(stdClass $term) { $uri = entity_uri('taxonomy_term', $term); path_redirect_delete_multiple(NULL, array('source' => $uri['path'])); path_redirect_delete_multiple(NULL, array('redirect' => $uri['path'])); if ($uri['path'] != "taxonomy/term/{$term->tid}") { path_redirect_delete_multiple(NULL, array('source' => "taxonomy/term/{$term->tid}")); path_redirect_delete_multiple(NULL, array('redirect' => "taxonomy/term/{$term->tid}")); } } /** * Implements hook_user_cancel(). */ function path_redirect_user_cancel($edit, stdClass $account, $method) { switch ($method) { case 'user_cancel_block': case 'user_cancel_block_unpublish': // Don't remove redirects because the user may become active again later. break; case 'user_cancel_reassign': case 'user_cancel_delete': // Do remove redirects since the account will be deleted. $uri = entity_uri('user', $account); path_redirect_delete_multiple(NULL, array('source' => $uri['path'])); path_redirect_delete_multiple(NULL, array('redirect' => $uri['path'])); if ($uri['path'] != "user/{$account->uid}") { path_redirect_delete_multiple(NULL, array('source' => "user/{$account->uid}")); path_redirect_delete_multiple(NULL, array('redirect' => "user/{$account->uid}")); } break; } } /** * Creates a redirect if an URL alias is being changed. * * We cannot use hook_path_update() since the alias has already been saved * and we do not have information about the old alias path. * * @param $path * The base (normal) path. * @param $new_alias * The new alias for the path. * @param $language * The language of the alias being created. * @return * TRUE if a redirect was created, or FALSE otherwise. */ function path_redirect_check_alias_changed($path, $new_alias, $language = LANGUAGE_NONE) { if (!variable_get('path_redirect_auto_redirect', 1) || empty($new_alias)) { return FALSE; } $old_alias = drupal_get_path_alias($path, $language); if ($old_alias != $path && $old_alias != $new_alias) { // If the user is manually changing the path alias, add a redirect from the old alias to the node. $redirect = array( 'source' => $old_alias, 'redirect' => $path, //'language' => $language, ); path_redirect_save($redirect); return $redirect; } } /** * Save an URL redirect to the database. */ function path_redirect_save(&$redirect) { // Merge default values. $redirect += array( 'rid' => NULL, 'query' => '', 'fragment' => '', 'language' => LANGUAGE_NONE, 'type' => variable_get('path_redirect_default_status', 301), 'last_used' => REQUEST_TIME, ); // Allow spaces in "from" path // @todo Move to validation? $redirect['source'] = str_replace('+', ' ', $redirect['source']); // Remove beginning and trailing slashes from path. // @todo Move to validation? $redirect['source'] = trim($redirect['source'], '\/?'); path_redirect_clear_cache(); if (empty($redirect['rid'])) { drupal_write_record('path_redirect', $redirect); } else { drupal_write_record('path_redirect', $redirect, array('rid')); } return $redirect; } /** * Load a redirect by ID. * * @param $rid * An integer with the redirect ID. */ function path_redirect_load($rid) { $redirect = path_redirect_load_multiple(array($rid)); return $redirect ? reset($redirect) : FALSE; } /** * Load a redirect by incoming path and language. * * @param $source * The incoming path. * @param $language * An optional language code. If not provided this will examine all language- * neutral redirects. * @param $query * An optional query string to match. */ function path_redirect_load_by_source($source, $language = LANGUAGE_NONE, array $query = array()) { $where = 'source = :source'; $args = array(':source' => $source, ':language' => $language, ':langneutral' => LANGUAGE_NONE); if ($query) { $where = '(source = :source OR source LIKE :sourcequery)'; $args += array(':sourcequery' => $source . '?%'); } // @todo Remove the '' language condition. $rids = db_query("SELECT rid FROM {path_redirect} WHERE $where AND language IN (:language, :langneutral, '') ORDER BY language DESC, source DESC, rid DESC", $args)->fetchCol(); if ($query) { $redirects = path_redirect_load_multiple($rids); foreach ($redirects as $rid => $redirect) { if (path_redirect_compare_array($redirect['source_query'], $query)) { return $redirect; } } } elseif ($rids) { return path_redirect_load(current($rids)); } return FALSE; } // @todo Use entity_load()? function path_redirect_load_multiple(array $rids = NULL, $conditions = array()) { if (isset($rids) && empty($rids)) { return array(); } $query = db_select('path_redirect'); $query->fields('path_redirect'); _path_redirect_build_conditions($query, $rids, $conditions); $redirects = $query->execute()->fetchAllAssoc('rid', PDO::FETCH_ASSOC); $redirects = array_map('path_redirect_post_load_redirect', $redirects); return $redirects; } function _path_redirect_build_conditions(QueryConditionInterface &$query, $rids, $conditions) { if ($rids) { $conditions += array('rid' => array()); $conditions['rid'] = array_merge($rids, (array) $conditions['rid']); } if ($conditions) { foreach ($conditions as $field => $value) { //if ($field == 'language' && !is_array($value)) { // $value = array($value, LANGUAGE_NONE); //} $query->condition($field, $value); } } } function path_redirect_post_load_redirect(array &$redirect) { if (!isset($redirect['source_query']) || !is_array($redirect['source_query'])) { $redirect['source_query'] = drupal_get_query_array(parse_url($redirect['source'], PHP_URL_QUERY)); $redirect['source'] = parse_url($redirect['source'], PHP_URL_PATH); } $redirect['query'] = drupal_get_query_array($redirect['query']); return $redirect; } function path_redirect_clear_cache() { cache_clear_all(NULL, 'cache_page'); } /** * Delete a redirect. * * @param $rid * The ID of the redirect to delete. */ function path_redirect_delete($rid) { return path_redirect_delete_multiple(array($rid)); } /** * Delete multiple redirects. * * @param $rids * An optional array or redirect IDs. * @param $conditions * An optional array of conditions keyed by field to match. * @return * The number of deleted redirects. */ function path_redirect_delete_multiple(array $rids = NULL, array $conditions = array()) { $query = db_delete('path_redirect'); _path_redirect_build_conditions($query, $rids, $conditions); $deleted = $query->execute(); path_redirect_clear_cache(); return $deleted; } function path_redirect_get_path($path = NULL) { if (!isset($path)) { if (drupal_is_front_page()) { $path = ''; } else { $path = $_GET['q']; } } else { if ($path == drupal_get_normal_path(variable_get('site_frontpage', 'node'))) { $path = ''; } } return $path; } function path_redirect_get_query(array $query = NULL) { if (!isset($query)) { $query = $_GET; } unset($query['q']); return $query; } function path_redirect_build_url($path, $query = '', $fragment = '', $clean_url = NULL) { if (!isset($clean_url)) { $clean_url = variable_get('clean_url', 0); } $url = $path; if ($query) { $url .= $clean_url ? '?' : '&'; if (is_array($query)) { $url .= drupal_http_build_query($query); } else { $url .= $query; } } if ($fragment) { $url .= '#' . $fragment; } return $url; } /** * Compare tha all values and associations in one array match another array. * * We cannot use array_diff_assoc() here because we need to be recursive. * * @param $match * The array that has the values. * @param $haystack * The array that will be searched for values. */ function path_redirect_compare_array($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 (!path_redirect_compare_array($value, $haystack[$key])) { return FALSE; } } elseif ($value != $haystack[$key]) { return FALSE; } } return TRUE; } function path_redirect_variables() { return array( 'path_redirect_redirect_warning' => 0, 'path_redirect_allow_bypass' => 0, 'path_redirect_auto_redirect' => 1, 'path_redirect_purge_inactive' => 0, 'path_redirect_default_status' => 301, 'path_redirect_nodeapi_enabled' => NULL, ); }