/[drupal]/contributions/modules/http_redirect/http_redirect.module
ViewVC logotype

Contents of /contributions/modules/http_redirect/http_redirect.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sun Mar 11 09:43:38 2007 UTC (2 years, 8 months ago) by jondoesdrupal
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Initial release.
1 <?php
2
3 /* $Id$ */
4
5 function http_redirect_help($section='') {
6 $output = '';
7
8 switch ($section) {
9 case "admin/modules#description":
10 $output = t("A module for setting up and managing 301 and 302 http redirects.");
11 break;
12 }
13
14 return $output;
15 }
16
17
18 function http_redirect_perm() {
19 return array('get redirected', 'can administer redirects');
20 }
21
22
23 function http_redirect_menu() {
24 $items = array();
25 $items[] = array('path' => 'http_redirect',
26 'title' => 'http_redirect',
27 'callback' => 'http_redirect_do_redirect',
28 'access' => user_access('get redirected'),
29 'type' => MENU_CALLBACK);
30 $items[] = array('path' => 'admin/http_redirect',
31 'title' => 'http redirect',
32 'callback' => 'http_redirect_admin',
33 'access' => user_access('can administer redirects'),
34 'type' => MENU_NORMAL_ITEM);
35 return $items;
36 }
37
38 function http_redirect_do_redirect() {
39 $rid = arg(1);
40
41 // Based on the redirect ID requested, look up the URL we're going to and the code we're using
42 $result = db_query("SELECT url, code FROM {http_redirect} WHERE rid = %d LIMIT 1", $rid);
43
44 // Check we were given a valid id
45 if (mysql_num_rows($result) == 0) {
46 print theme('page', "Invalid redirect id provided.");
47 }
48 else {
49 $url = mysql_result($result, 0, 0);
50 $code = mysql_result($result, 0, 1);
51 }
52
53 if ($code == '301') {
54 Header( "HTTP/1.1 301 Moved Permanently" );
55 Header( "Location: $url" );
56 }
57 else if ($code == '302') {
58 Header( "HTTP/1.1 302 Moved Permanently" );
59 Header( "Location: $url" );
60 }
61 }
62
63 function http_redirect_admin() {
64 // Warning messages in the header
65 $output = '<strong>' . t('Guidelines for use') . '</strong>';
66 $output .= '<ul><li>';
67 $output .= t('Any URLs supplied will override existing genuine urls (node/1, user/5, etc) however will not replace any existing aliases unless the option to overwrite is checked.');
68 $output .= '</li><li>';
69 $output .= t('Source URLs are in the same format as url aliases, one per line and at this point, no error checking is made.');
70 $output .= '</li></ul>';
71
72 // Preload existing redirects
73 $result = db_query("SELECT rid, url, code FROM {http_redirect}");
74
75 // Set some default values
76 $rid = 0;
77
78 while ($row = db_fetch_object($result)) {
79 // $rids keeps track of the rids we're using
80 $rids[] = $row->rid;
81
82 // Extract and prepare data for the form
83 $rid = $row->rid;
84 $src_default = "";
85 $subresult = db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", 'http_redirect/' . $rid);
86 while ($subrow = db_fetch_object($subresult)) {
87 $src_default .= $subrow->dst . "\n";
88 }
89
90 $form['src_' . $rid] = array('#type' => 'textarea',
91 '#title' => t('Existing Redirect - Source URLs'),
92 '#default_value' => $src_default,
93 '#rows' => 1);
94 $form['dst_' . $rid] = array('#type' => 'textfield',
95 '#title' => t('Existing Redirect - Destination URL'),
96 '#size' => 60,
97 '#default_value' => $row->url,
98 '#maxlength' => 128);
99 $form['code_' . $rid] = array('#type' => 'select',
100 '#title' => t('Existing Redirect - http redirect code'),
101 '#default_value' => $row->code,
102 '#options' => array('301' => t('301 - permanent'),
103 '302' => t('302 - temporary')));
104
105 $form['overwrite_'. $rid] = array('#type' => 'checkbox',
106 '#title' => t('Overwrite existing aliases?'));
107
108 $form['delete_'. $rid] = array('#type' => 'checkbox',
109 '#title' => t('Delete?'));
110 }
111
112 // Now an extra set of fields for a new entry
113 $rid++;
114 $rids[] = $rid;
115
116 $form['src_' . $rid] = array('#type' => 'textarea',
117 '#title' => t('Add New Redirect - Source URLs'),
118 '#rows' => 1);
119 $form['dst_' . $rid] = array('#type' => 'textfield',
120 '#title' => t('Add New Redirect - Destination URL'),
121 '#size' => 60,
122 '#maxlength' => 128);
123 $form['code_' . $rid] = array('#type' => 'select',
124 '#title' => t('http redirect code'),
125 '#default_value' => $row->code,
126 '#options' => array('301' => t('301 - permanent'),
127 '302' => t('302 - temporary')));
128 $form['overwrite_'. $rid] = array('#type' => 'checkbox',
129 '#title' => t('Overwrite existing aliases?'));
130
131 $form['rids'] = array('#type' => 'value', '#value' => $rids);
132 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
133
134 $output .= drupal_get_form('http_redirect_admin', $form);
135
136 return $output;
137 }
138
139 function http_redirect_admin_validate($form_id, $form_values) {
140 return TRUE;
141 }
142
143 function http_redirect_admin_submit($form_id, $form_values) {
144 // Process the form data - the only one that needs anything clever is the source url list
145 foreach ($form_values['rids'] as $rid) {
146 if (($form_values['src_' . $rid] != "") && ($form_values['dst_' . $rid] != "")) {
147 // First update the redircts table - delete any previous ones
148 db_query("DELETE FROM {http_redirect} WHERE rid = %d", $rid);
149 if ($form_values['delete_' . $rid] != 1) {
150 db_query("INSERT INTO {http_redirect} (rid, url, code) VALUES (%d, '%s', %d)", $rid, $form_values['dst_' . $rid], $form_values['code_' . $rid]);
151 }
152
153 // Now iterate through the source URL and make aliases for them - again, delete any previous ones
154 db_query("DELETE FROM {url_alias} WHERE src = '%s'", 'http_redirect/' . $rid);
155
156 if ($form_values['delete_' . $rid] != 1) {
157 $src_urls = split("\n", rtrim($form_values['src_' . $rid]));
158 foreach ($src_urls as $src_url) {
159 // If the overwrite option has been taken, we need to clear url_alias based on the source urls (well, destination in url_alias terms)
160 if ($form_values['overwrite_' . $rid] == 1) {
161 db_query("DELETE FROM {url_alias} WHERE dst = '%s'", rtrim($src_url));
162 }
163 db_query("INSERT INTO {url_alias} (pid, src, dst) VALUES (%d, '%s', '%s')", $pid, 'http_redirect/' . $rid, rtrim($src_url));
164 }
165 }
166 }
167 }
168
169 drupal_set_message(t('http redirects updated'));
170 }

  ViewVC Help
Powered by ViewVC 1.1.2