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

Contents of /contributions/modules/gotwo/gotwo.module

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


Revision 1.10 - (show annotations) (download) (as text)
Tue Oct 20 18:03:46 2009 UTC (5 weeks, 4 days ago) by hass
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +3 -3 lines
File MIME type: text/x-php
#572728 by hass: Image html tags inside <go> links not working
1 <?php
2 // $Id: gotwo.module,v 1.9 2009/08/26 19:13:26 hass Exp $
3
4 /**
5 * @file
6 * Module that provides easy to use redirection links. A redirection link
7 * would be like:
8 * http://examples.org/go/a_label
9 * http://examples.org/go/123546
10 * http://examples.org/go/or/like/this
11 *
12 * It's much like url aliases except these are redirects
13 */
14
15 define('GOTWO_CREATE', 1);
16
17 /**
18 * Implementation of hook_perm
19 */
20 function gotwo_perm() {
21 return array('view gotwo redirects', 'edit gotwo redirects', 'administer gotwo');
22 }
23
24 /**
25 * Implementation of hook_help
26 */
27 function gotwo_help($path, $arg) {
28 switch ($path) {
29 case 'admin/help#gotwo':
30 return gotwo_filter_tips(0, '', TRUE);
31 break;
32 }
33 }
34
35 /**
36 * Implementation of hook_filter_tips().
37 */
38 function gotwo_filter_tips($delta, $format, $long = FALSE) {
39 switch ($delta) {
40 case 0:
41 if (!$long) {
42 return t('You can use the &lt;go&gt; tags just like the &lt;a&gt; for nicer urls.');
43 }
44 else {
45 return '<p>'. t('You can use the &lt;go&gt; tags just like the &lt;a&gt;. The url will be rewritten to in internal URL that will eventually redirect the user to the given url. Depending on the settings the url will contain an identifying label constructed from the provided url. Alternatively you can provide a label by means of the "title" argument.') .'</p><p>'. t('For example: <pre>&lt;go href="http://example.org/some/page"&gt;Some page example&lt;/go&gt;</pre> produces: <pre>&lt;a href="go/example.com/some/page"&gt;Some page example&lt;/a&gt;</pre>Or like this:<pre>&lt;go href="http://example.com/some/page" title="Some page"&gt;Some page example&lt;/go&gt;</pre> produces: <pre>&lt;a href="go/some_page" title="Some page"&gt;Some page example&lt;/a&gt;</pre>') .'</p>';
46 }
47 break;
48 }
49 }
50
51 /**
52 * Implementation of hook_menu
53 */
54 function gotwo_menu() {
55
56 $items['go'] = array(
57 'page callback' => '_gotwo_do_redir',
58 'access callback' => TRUE,
59 'type' => MENU_CALLBACK,
60 );
61 $items['admin/build/gotwo'] = array(
62 'title' => 'Go redirects',
63 'description' => 'You can use the &lt;go&gt; tags just like the &lt;a&gt; for nicer urls.',
64 'page callback' => '_gotwo_list',
65 'access arguments' => array('view gotwo redirects'),
66 'file' => 'gotwo.admin.inc',
67 );
68 $items['admin/build/gotwo/list'] = array(
69 'title' => 'List',
70 'access arguments' => array('view gotwo redirects'),
71 'file' => 'gotwo.admin.inc',
72 'type' => MENU_DEFAULT_LOCAL_TASK,
73 'weight' => -10,
74 );
75 $items['admin/build/gotwo/add'] = array(
76 'title' => 'Add redirect',
77 'description' => 'Add a new Go redirect',
78 'page callback' => 'drupal_get_form',
79 'page arguments' => array('gotwo_add_form'),
80 'access arguments' => array('edit gotwo redirects'),
81 'file' => 'gotwo.admin.inc',
82 'type' => MENU_LOCAL_TASK,
83 );
84 $items['admin/build/gotwo/delete/%gotwo'] = array(
85 'page callback' => 'drupal_get_form',
86 'page arguments' => array('gotwo_delete_form', 4),
87 'access arguments' => array('edit gotwo redirects'),
88 'file' => 'gotwo.admin.inc',
89 'type' => MENU_CALLBACK,
90 );
91 $items['admin/build/gotwo/reset/%gotwo'] = array(
92 'page callback' => 'drupal_get_form',
93 'page arguments' => array('gotwo_reset_form', 4),
94 'access arguments' => array('edit gotwo redirects'),
95 'file' => 'gotwo.admin.inc',
96 'type' => MENU_CALLBACK,
97 );
98 $items['admin/settings/gotwo'] = array(
99 'title' => 'Go settings',
100 'description' => 'Configure URL parameters and disclaimer options',
101 'page callback' => 'drupal_get_form',
102 'page arguments' => array('gotwo_admin_settings_form'),
103 'access arguments' => array('administer gotwo'),
104 'file' => 'gotwo.admin.inc',
105 );
106
107 return $items;
108 }
109
110 /**
111 * Menu helper function to verify if gotwo id exists.
112 */
113 function gotwo_load($gid) {
114 return db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE gid = %d", $gid));
115 }
116
117 /**
118 * Redirect the user to the given location
119 */
120 function _gotwo_do_redir() {
121 $args = func_get_args();
122 $src = implode('/', $args);
123
124 // $src may be an Go ID or a source attribute
125 $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s' OR gid = %d", $src, intval($src)));
126
127 if (!$res) {
128 drupal_not_found();
129 }
130 else {
131 db_query("UPDATE {gotwo} SET cnt = cnt+1 WHERE gid = %d", $res->gid);
132 if (variable_get('gotwo_disclaimer_boolean', FALSE)) {
133 // Display the Disclaimer
134 $disclaimer_text = variable_get('gotwo_disclaimer_text', '');
135 $find_array = array('%url', '%seconds');
136 $replace_array = array(check_url($res->dst), variable_get('gotwo_disclaimer_time', 0));
137 $page_content = filter_xss_admin(str_replace($find_array, $replace_array, $disclaimer_text));
138
139 // Should we refresh?
140 if (variable_get('gotwo_disclaimer_time', 0) > 0 ) {
141 drupal_set_html_head('<meta http-equiv="refresh" content="' . variable_get('gotwo_disclaimer_time', 0) . ';url=' . check_url($res->dst) . '"/>');
142 }
143
144 return $page_content;
145 }
146 else {
147 // Parse the URL.
148 $uri = parse_url($res->dst);
149
150 $scheme = isset($uri['scheme']) ? $uri['scheme'] . '://' : '';
151 $user = isset($uri['user']) ? $uri['user'] . ($uri['pass'] ? ':' . $uri['pass'] : '') . '@' : '';
152 $port = isset($uri['port']) ? $uri['port'] : 80;
153 $host = $uri['host'] . ($port != 80 ? ':'. $port : '');
154 $path = isset($uri['path']) ? $uri['path'] : '/';
155
156 // Glue the URL variables.
157 $dst_url = $scheme . $user . $host . $path;
158 $dst_query = isset($uri['query']) ? $uri['query'] : NULL;
159 $dst_fragment = isset($uri['fragment']) ? $uri['fragment'] : NULL;
160
161 drupal_goto($dst_url, $dst_query, $dst_fragment);
162 }
163 }
164 }
165
166 /**
167 * Implementation of hook_filter
168 */
169 function gotwo_filter($op, $delta = 0, $format = -1, $text = '') {
170 switch ($op) {
171 case 'list':
172 return array(0 => t('"Go" redirection filter'));
173
174 case 'description':
175 return t('Automatically creates redirection urls. &lt;go href=""&gt;&lt;/go&gt; tags are nicely translated to &lt;a href=""&gt;&lt;/a&gt; tags.');
176
177 case 'prepare':
178 return $text;
179
180 case "process":
181 return preg_replace_callback('#<go\s([^>]*)>(.*?)</go>#is', '_gotwo_filter', $text);
182
183 default:
184 return $text;
185 }
186 }
187
188 /**
189 * This function will strip the <go> link into it's parts, save the link with
190 * the title to the database and returns a HTML based link for the replacement
191 * in the content.
192 *
193 * @param $link
194 * A raw <go> link to be processed.
195 * @return
196 * The HTML representation of a <go> link.
197 */
198 function _gotwo_filter($link) {
199 // Split all link tag attributes into key and value pairs.
200 preg_match_all('/\s?+([^=]*)=["\']([^"\']*)["\']/i', $link[1], $matches);
201 $go_attributes = array_combine($matches[1], $matches[2]);
202
203 // Drop empty attributes and sort by name.
204 $go_attributes = array_filter($go_attributes);
205 ksort($go_attributes);
206
207 // Verify if the url exists in the {gotwo} table. If the url is missing,
208 // add the url with link title to the {gotwo} table.
209 if (!empty($go_attributes['href'])) {
210 $go_attributes['href'] = _gotwo_get_url($go_attributes['href'], isset($go_attributes['title']) ? $go_attributes['title'] : NULL);
211 }
212
213 // Do not return a link if the go link doesn't haven attributes.
214 return empty($go_attributes) ? $link[2] : '<a ' . drupal_attributes($go_attributes) . '>' . $link[2] . '</a>';
215 }
216
217 /**
218 * Return the GO url for a given link
219 */
220 function _gotwo_get_url($url, $src = FALSE, $flags = GOTWO_CREATE) {
221 $res = _gotwo_get($url, $src, $flags);
222 if (!$res) {
223 return url($url);
224 }
225 if (variable_get('gotwo_numeric', FALSE)) {
226 return url('go/'. $res->gid);
227 }
228 return url('go/'. $res->src);
229 }
230
231 /**
232 * Return the GO object url for a given link
233 */
234 function _gotwo_get($url, $src = NULL, $flags = GOTWO_CREATE) {
235 // Only add valid URLs to the database. Otherwise the disclaimer reload may fail.
236 if (!valid_url($url)) {
237 return FALSE;
238 }
239 // If there is no title to mangle, use the url instead.
240 if (!$src) {
241 $src = preg_replace('#^(http(s)?://)#', '', $url);
242 }
243 $src = _gotwo_mangle_src($src);
244
245 $maxlength = min(variable_get('gotwo_max_length', 128), 128);
246
247 $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s' AND dst = '%s'", $src, $url));
248 if ($res === FALSE) {
249 $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = gid+'/%s' AND dst = '%s'", $src, $url));
250 if (!empty($res)) {
251 $src_old = substr($res->gid .'/'. $src, 0, $maxlength);
252 if ($src_old != $res->src) {
253 $res == FALSE;
254 }
255 }
256 }
257 if ($res === FALSE) {
258 if ($flags & GOTWO_CREATE) {
259 // Force unique src.
260 $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s'", $src));
261 if (!empty($res)) {
262 // TODO: find a better solution.
263 // Insert a dummy first with an uniqe src value to get the 'gid' value.
264 db_query("INSERT INTO {gotwo} (src, dst) VALUES ('%s', '%s')", uniqid(), $url);
265 $gid = db_last_insert_id('gotwo', 'gid');
266 $src = substr($gid .'/'. $src, 0, $maxlength);
267 db_query("UPDATE {gotwo} SET src = '%s' WHERE gid = %d", $src, $gid);
268 }
269 else {
270 db_query("INSERT INTO {gotwo} (src, dst) VALUES ('%s', '%s')", $src, $url);
271 }
272 }
273 else {
274 return FALSE;
275 }
276 }
277 return $res;
278 }
279
280 /**
281 * Mangle the input for url friendlyness. Based on pathauto_cleanstring from pathauto.module
282 */
283 function _gotwo_mangle_src($string) {
284 static $translations = array(
285 'À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Ä'=>'A','Å'=>'A','Ā'=>'A','Ą'=>'A','Ă'=>'A',
286 'à'=>'a','á'=>'a','â'=>'a','ã'=>'a','ä'=>'a','å'=>'a','ā'=>'a','ą'=>'a','ă'=>'a',
287 'Æ'=>'Ae',
288 'æ'=>'ae',
289 'Ç'=>'C','Ć'=>'C','Č'=>'C','Ĉ'=>'C','Ċ'=>'C',
290 'ç'=>'c','ć'=>'c','č'=>'c','ĉ'=>'c','ċ'=>'c',
291 'Ď'=>'D','Đ'=>'D','Ð'=>'D',
292 'ď'=>'d','đ'=>'d','ð'=>'d',
293 'È'=>'E','É'=>'E','Ê'=>'E','Ë'=>'E','Ē'=>'E','Ę'=>'E','Ě'=>'E','Ĕ'=>'E','Ė'=>'E',
294 'è'=>'e','é'=>'e','ê'=>'e','ë'=>'e','ē'=>'e','ę'=>'e','ě'=>'e','ĕ'=>'e','ė'=>'e',
295 'ƒ'=>'f',
296 'Ĝ'=>'G','Ğ'=>'G','Ġ'=>'G','Ģ'=>'G',
297 'ĝ'=>'g','ğ'=>'g','ġ'=>'g','ģ'=>'g',
298 'Ĥ'=>'H','Ħ'=>'H',
299 'ĥ'=>'h','ħ'=>'h',
300 'Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','Ī'=>'I','Ĩ'=>'I','Ĭ'=>'I','Į'=>'I','İ'=>'I',
301 'ì'=>'i','í'=>'i','î'=>'i','ï'=>'i','ī'=>'i','ĩ'=>'i','ĭ'=>'i','į'=>'i','ı'=>'i',
302 'IJ'=>'Ij',
303 'ij'=>'ij',
304 'Ĵ'=>'J',
305 'ĵ'=>'j',
306 'Ķ'=>'K',
307 'ķ'=>'k','ĸ'=>'k',
308 'Ł'=>'L','Ľ'=>'L','Ĺ'=>'L','Ļ'=>'L','Ŀ'=>'L',
309 'ł'=>'l','ľ'=>'l','ĺ'=>'l','ļ'=>'l','ŀ'=>'l',
310 'Ñ'=>'N','Ń'=>'N','Ň'=>'N','Ņ'=>'N','Ŋ'=>'N',
311 'ñ'=>'n','ń'=>'n','ň'=>'n','ņ'=>'n','ʼn'=>'n','ŋ'=>'n',
312 'Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ö'=>'O','Ø'=>'O','Ō'=>'O','Ő'=>'O','Ŏ'=>'O',
313 'ò'=>'o','ó'=>'o','ô'=>'o','õ'=>'o','ö'=>'o','ø'=>'o','ō'=>'o','ő'=>'o','ŏ'=>'o',
314 'Œ'=>'Oe',
315 'œ'=>'oe',
316 'Ŕ'=>'R','Ř'=>'R','Ŗ'=>'R',
317 'ŕ'=>'r','ř'=>'r','ŗ'=>'r',
318 'Ś'=>'S','Š'=>'S','Ş'=>'S','Ŝ'=>'S','Ș'=>'S',
319 'Ť'=>'T','Ţ'=>'T','Ŧ'=>'T','Ț'=>'T','Þ'=>'T',
320 'þ'=>'t',
321 'Ù'=>'U','Ú'=>'U','Û'=>'U','Ü'=>'U','Ū'=>'U','Ů'=>'U','Ű'=>'U','Ŭ'=>'U','Ũ'=>'U','Ų'=>'U',
322 'ú'=>'u','û'=>'u','ü'=>'u','ū'=>'u','ů'=>'u','ű'=>'u','ŭ'=>'u','ũ'=>'u','ų'=>'u',
323 'Ŵ'=>'W',
324 'ŵ'=>'w',
325 'Ý'=>'Y','Ŷ'=>'Y','Ÿ'=>'Y','Y'=>'Y',
326 'ý'=>'y','ÿ'=>'y','ŷ'=>'y',
327 'Ź'=>'Z','Ž'=>'Z','Ż'=>'Z',
328 'ž'=>'z','ż'=>'z','ź'=>'z',
329 'ß'=>'ss','ſ'=>'ss');
330
331 $output = str_replace("'", "", $string);
332 $output = strtr($output, $translations);
333 $pattern = '#[^a-zA-Z0-9./]+# ';
334 $separator = '_';
335 $output = preg_replace($pattern, $separator, $output);
336 if ($separator) {
337 if (ctype_alnum($separator)) {
338 $seppattern = $separator;
339 }
340 else {
341 $seppattern = '\\'. $separator;
342 }
343 $output = preg_replace("/^$seppattern+|$seppattern+$/", "", $output);
344 }
345 $maxlength = min(variable_get('gotwo_max_length', 128), 128);
346 $output = substr($output, 0, $maxlength);
347
348 return $output;
349 }

  ViewVC Help
Powered by ViewVC 1.1.2