~stefanor/drupal/sr

« back to all changes in this revision

Viewing changes to modules/path_redirect/path_redirect.module

  • Committer: Stefano Rivera
  • Date: 2009-02-03 13:46:19 UTC
  • Revision ID: stefano@rivera.za.net-20090203134619-qtah2mm7178s3gla
Updated:
* swfobject to 2.0 (jquery_media)
* path_redirect beta1
* xmlsitemap update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
// $Id: path_redirect.module,v 1.3.2.7.2.13 2008/03/08 01:10:17 horsepunchkid Exp $
3
 
 
4
 
// The default HTTP status code for redirects is "moved permanently"
5
 
define('PATH_REDIRECT_DEFAULT_TYPE', 301);
 
2
// $Id: path_redirect.module,v 1.3.2.7.2.20 2009/01/27 19:17:00 davereid Exp $
6
3
 
7
4
/**
8
 
 * Implementation of hook_help
 
5
 * Implementation of hook_help().
9
6
 */
10
7
function path_redirect_help($path, $arg) {
11
8
  switch ($path) {
13
10
      return '<p>'. 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.') .'</p>';
14
11
    case 'admin/build/path-redirect/'. $arg[2]:
15
12
    case 'admin/build/path-redirect/edit/'. $arg[3]:
16
 
      return '<p>'. t("The <strong>from</strong> path must be an internal Drupal path in the form of 'node/123', 'admin/logs', or 'taxonomy/term/123'. The <strong>to</strong> path can be either an internal Drupal path as above or a complete external URL such as http://www.example.com/. Furthermore, the <strong>to</strong> path may contain query arguments (such as 'page=2') and fragment anchors, to make it possible to redirect to 'admin/user?page=1#help'. Most redirects will not contain queries or anchors.") .'</p>';
 
13
      return '<p>'. t("The <strong>from</strong> path must be an internal Drupal path in the form of 'node/123', 'admin/reports', or 'taxonomy/term/123'. The <strong>to</strong> path can be either an internal Drupal path as above or a complete external URL such as http://www.example.com/. Furthermore, the <strong>to</strong> path may contain query arguments (such as 'page=2') and fragment anchors, to make it possible to redirect to 'admin/user?page=1#help'. Most redirects will not contain queries or anchors.") .'</p>';
17
14
  }
18
15
}
19
16
 
20
17
/**
21
 
 * Implementation of hook_init
 
18
 * Implementation of hook_init().
22
19
 *
23
20
 * Early checking of URL requested.
24
21
 * If a match is found, user is redirected using drupal_goto()
 
22
 *
 
23
 * @todo Massive revision needed. Split into sub-functions if necessary.
25
24
 */
26
25
function path_redirect_init() {
27
26
  // see if this page has a redirect path
28
 
  $path = substr(request_uri(), strlen($GLOBALS['base_path']));
 
27
  $path = substr(request_uri(), strlen(base_path()));
29
28
  if (preg_match('/^\?q=/', $path)) {
30
29
    $path = preg_replace(array('/^\?q=/', '/&/'), array('', '?'), $path, 1);
31
30
  }
 
31
 
 
32
  // Remove trailing slash from path.
 
33
  $path = preg_replace('/\/\?|\/$/', '', $path);
 
34
 
32
35
  $r = db_fetch_object(db_query("SELECT rid, redirect, query, fragment, type FROM {path_redirect} WHERE path = '%s' OR path = '%s'", $path, utf8_encode($path)));
33
36
  if (!$r) {
34
37
    $path = preg_replace('/\?.*/', '', $path);
35
38
    $r = db_fetch_object(db_query("SELECT rid, redirect, query, fragment, type FROM {path_redirect} WHERE path = '%s' OR path = '%s'", $path, utf8_encode($path)));
36
39
  }
37
40
 
38
 
  // only redirect if allow_bypass is off or bypass is not requested
39
 
  if ($r && !(variable_get('path_redirect_allow_bypass', 0) && !empty($_GET['redirect']) && $_GET['redirect'] == 'no') && url($r->redirect) != url($path)) {
40
 
    if (variable_get('path_redirect_redirect_warning', 0)) {
41
 
      drupal_set_message(t('This page has been moved. You may want to update your bookmarks.'));
42
 
    }
43
 
    if (function_exists('drupal_goto')) {
44
 
      // if there's a result found, do the redirect
45
 
      unset($_REQUEST['destination']);
46
 
      drupal_goto($r->redirect, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL), $r->type);
 
41
  if ($r) {
 
42
    $redirect = url($r->redirect, array('query' => $r->query, 'fragment' => $r->fragment, 'absolute' => TRUE));
 
43
    if (url($r->redirect) == url($path)) {
 
44
      // Prevent infinite loop redirection.
 
45
      watchdog('path_redirect', 'Redirect to <code>%redirect</code> is causing an infinite loop; redirect cancelled.', array('%redirect' => $r->redirect), WATCHDOG_WARNING, l(t('edit'), 'admin/build/path-redirect/edit/'. $r->rid));
 
46
    }
 
47
    elseif (variable_get('path_redirect_allow_bypass', 0) && isset($_GET['redirect']) && $_GET['redirect'] === 'no') {
 
48
      // If the user has requested not to be redirected, show a message.
 
49
      drupal_set_message(t('This page has been redirected to <a href="@redirect">@redirect</a>.', array('@redirect' => $redirect)));
 
50
    }
 
51
    elseif (variable_get('path_redirect_redirect_warning', 0)) {
 
52
      drupal_set_message(t('This page has been moved to <a href="@redirect">@redirect</a> and will redirect in 5 seconds. You may want to update your bookmarks.', array('@redirect' => $redirect)), 'error');
 
53
      drupal_set_html_head("<meta http-equiv=\"refresh\" content=\"5;url=$redirect\" />");
47
54
    }
48
55
    else {
49
 
      // page caching is turned on so drupal_goto() (common.inc) hasn't been loaded
50
 
      path_redirect_goto($r->redirect, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL), $r->type);
 
56
      // @todo Clean the redirection code up. Do we need to duplicate code?
 
57
      if (function_exists('drupal_goto')) {
 
58
        // if there's a result found, do the redirect
 
59
        unset($_REQUEST['destination']);
 
60
        drupal_goto($r->redirect, $r->query, $r->fragment, $r->type);
 
61
      }
 
62
      else {
 
63
        // page caching is turned on so drupal_goto() (common.inc) hasn't been loaded
 
64
        path_redirect_goto($r->redirect, $r->query, $r->fragment, $r->type);
 
65
      }
51
66
    }
52
67
  }
53
 
  else if ($r && url($r->redirect) == url($path)) {
54
 
    watchdog('path_redirect', 'Redirect to <code>%redirect</code> is causing an infinite loop; redirect cancelled.', array('%redirect' => $r->redirect), WATCHDOG_WARNING, l(t('edit'), 'admin/build/path-redirect/edit/'. $r->rid));
55
 
  }
56
 
  else if ($r && variable_get('path_redirect_allow_bypass', 0) && !empty($_GET['redirect']) && $_GET['redirect'] === 'no') {
57
 
    drupal_set_message(t('This page is redirected to:') .' <code>'. l($r->redirect, $r->redirect, array('query' => ($r->query ? $r->query: NULL), 'fragment' => ($r->fragment ? $r->fragment : NULL))) .'</code>');
58
 
  }
59
68
}
60
69
 
61
70
/**
62
 
 * Implementation of hook_menu
 
71
 * Implementation of hook_menu().
63
72
 */
64
73
function path_redirect_menu() {
65
74
  $items['admin/build/path-redirect'] = array(
105
114
  );
106
115
  $items['admin/settings/path-redirect'] = array(
107
116
    'title' => 'URL redirects',
108
 
    'description' => t('Configure behavior for URL redirects'),
 
117
    'description' => 'Configure behavior for URL redirects',
109
118
    'page callback' => 'drupal_get_form',
110
119
    'page arguments' => array('path_redirect_settings'),
111
120
    'access arguments' => array('administer redirects'),
115
124
}
116
125
 
117
126
/**
118
 
 * Implementation of hook_perm
 
127
 * Implementation of hook_perm().
119
128
 */
120
129
function path_redirect_perm() {
121
130
  return array('administer redirects');
122
131
}
123
132
 
124
133
function path_redirect_save($edit) {
125
 
  if (empty($edit['type'])) {
126
 
    $edit['type'] = PATH_REDIRECT_DEFAULT_TYPE;
127
 
  }
128
 
  if (empty($edit['query'])) {
129
 
    $edit['query'] = '';
130
 
  }
131
 
  if (empty($edit['fragment'])) {
132
 
    $edit['fragment'] = '';
133
 
  }
 
134
  $edit += array(
 
135
    'type' => variable_get('path_redirect_default_status', 301),
 
136
    'query' => '',
 
137
    'fragment' => '',
 
138
  );
134
139
 
135
140
  if (!empty($edit['rid'])) {
136
141
    $return = db_query("UPDATE {path_redirect} SET path = '%s', redirect = '%s', query = '%s', fragment = '%s', type = %d WHERE rid = %d", $edit['path'], $edit['redirect'], $edit['query'], $edit['fragment'], $edit['type'], $edit['rid']);
148
153
  if (!empty($rid)) {
149
154
    $result = db_fetch_array(db_query("SELECT rid, path, redirect, query, fragment, type FROM {path_redirect} WHERE rid = %d", $rid));
150
155
  }
151
 
  else if (!empty($from)) {
 
156
  elseif (!empty($from)) {
152
157
    $result = db_fetch_array(db_query("SELECT rid, path, redirect, query, fragment, type FROM {path_redirect} WHERE path = '%s'", $from));
153
158
  }
154
159
  return $result;
175
180
  if (!empty($rid)) {
176
181
    $result = db_query("DELETE FROM {path_redirect} WHERE rid = %d", $rid);
177
182
  }
178
 
  else if (!empty($from)) {
 
183
  elseif (!empty($from)) {
179
184
    if (!empty($to)) {
180
185
      $result = db_query("DELETE FROM {path_redirect} WHERE path = '%s' AND redirect = '%s'", $from, $to);
181
186
    }
183
188
      $result = db_query("DELETE FROM {path_redirect} WHERE path = '%s'", $from);
184
189
    }
185
190
  }
186
 
  else if (!empty($to)) {
 
191
  elseif (!empty($to)) {
187
192
    $result = db_query("DELETE FROM {path_redirect} WHERE redirect = '%s'", $to);
188
193
  }
189
194
  return $result;
208
213
  // Remove newlines from the URL to avoid header injection attacks.
209
214
  $url = str_replace(array("\n", "\r"), '', $url);
210
215
 
211
 
  // Before the redirect, allow modules to react to the end of the page request.
212
 
  bootstrap_invoke_all('exit');
 
216
  // Allow modules to react to the end of the page request before redirecting.
 
217
  // We do not want this while running update.php.
 
218
  if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
 
219
    module_invoke_all('exit', $url);
 
220
  }
213
221
 
214
222
  // Even though session_write_close() is registered as a shutdown function, we
215
223
  // need all session data written to the database before redirecting.
217
225
 
218
226
  header('Location: '. $url, TRUE, $http_response_code);
219
227
 
220
 
  // The "Location" header sends a REDIRECT status code to the http
221
 
  // daemon. In some cases this can go wrong, so we make sure none
222
 
  // of the code below the drupal_goto() call gets executed when we redirect.
 
228
  // The "Location" header sends a redirect status code to the HTTP daemon. In
 
229
  // some cases this can be wrong, so we make sure none of the code below the
 
230
  // drupal_goto() call gets executed upon redirection.
223
231
  exit();
 
232
 
224
233
}