gotwo.module in Go - url redirects 5
Same filename and directory in other branches
Module that provides easy to use redirection links. A redirection link would be like: http://examples.org/go/a_label http://examples.org/go/123546 http://examples.org/go/or/like/this
It's much like url aliases except these are redirects
File
gotwo.moduleView source
<?php
/**
* @file
* Module that provides easy to use redirection links. A redirection link
* would be like:
* http://examples.org/go/a_label
* http://examples.org/go/123546
* http://examples.org/go/or/like/this
*
* It's much like url aliases except these are redirects
*/
define('GOTWO_NOP', 0);
define('GOTWO_CREATE', 1);
/**
* Implementation of hook_help
*/
function gotwo_help($section) {
switch ($section) {
case 'admin/modules#description':
$output = t("Creation and management of 'go' URL redirection to external links on the web.");
break;
case 'admin/help#gotwo':
$output = gotwo_filter_tips(0, '', true);
}
return $output;
}
/**
* Implementation of hook_filter_tips().
*/
function gotwo_filter_tips($delta, $format, $long = false) {
switch ($delta) {
case 0:
if (!$long) {
return t('You can use the <go> tags just like the <a> for nicer urls.');
}
else {
return t('<p>You can use the <go> tags just like the <a>. 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>For example: <pre><go href="http://example.org/some/page">Some page example</go></pre> produces: <pre><a href="go/example.org/some/page">Some page example</a></pre>Or like this:<pre><go href="http://example.org/some/page" title="Some page">Some page example</go></pre> produces: <pre><a href="go/some_page" title="Some page">Some page example</a></pre></p>');
}
break;
}
}
/**
* Implementation of hook_menu
*/
function gotwo_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'go',
'callback' => '__gotwo_do_redir',
'access' => TRUE,
'type' => MENU_CALLBACK,
);
$items[] = array(
'title' => t('Go redirects'),
'path' => 'admin/build/gotwo',
'callback' => '__gotwo_list',
'access' => user_access('view gotwo entries'),
);
$items[] = array(
'path' => 'admin/settings/gotwo',
'title' => t('Gotwo Settings'),
'description' => t('Configure URL parameters and disclaimer options'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'gotwo_admin_settings',
),
'access' => user_access('administer gotwo'),
);
$items[] = array(
'title' => t('List'),
'path' => 'admin/build/gotwo/list',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access' => user_access('view gotwo entries'),
);
}
else {
$items[] = array(
'title' => t('Add'),
'path' => 'admin/build/gotwo/add',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'__gotwo_manual_add',
),
'type' => MENU_LOCAL_TASK,
'access' => user_access('edit gotwo entries'),
);
$items[] = array(
'path' => 'admin/build/gotwo/reset',
'title' => t('Reset'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'gotwo_reset_confirm',
),
'access' => user_access('edit gotwo entries'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/gotwo/delete',
'callback' => '__gotwo_delete_page',
'access' => user_access('edit gotwo entries'),
'type' => MENU_CALLBACK,
);
}
return $items;
}
function gotwo_perm() {
return array(
'view gotwo entries',
'edit gotwo entries',
'administer gotwo',
);
}
/**
* Redirect the user to the given location
*/
function __gotwo_do_redir() {
$args = func_get_args();
$src = implode("/", $args);
// $src may be an Go ID or a source attribute
$res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s' OR gid = %d", $src, intval($src)));
if (!$res) {
drupal_not_found();
}
else {
db_query(db_rewrite_sql("UPDATE {gotwo} SET cnt = cnt+1 WHERE gid = %d"), $res->gid);
//echo 'updated db '.$res->gid;exit;
if (variable_get('gotwo_disclaimer_boolean', FALSE)) {
// Display the Disclaimer.
$disclaimer_text = variable_get('gotwo_disclaimer_text', '');
$find_array = array(
'%url',
'%seconds',
);
$replace_array = array(
check_url($res->dst),
variable_get('gotwo_disclaimer_time', 0),
);
$page_content = filter_xss_admin(str_replace($find_array, $replace_array, $disclaimer_text));
// Should we refresh?
if (variable_get('gotwo_disclaimer_time', 0) > 0) {
drupal_set_html_head('<meta http-equiv="refresh" content="' . variable_get('gotwo_disclaimer_time', 0) . ';url=' . check_url($res->dst) . '"/>');
}
return $page_content;
}
else {
// Parse the URL.
$uri = parse_url($res->dst);
$scheme = isset($uri['scheme']) ? $uri['scheme'] . '://' : '';
$user = isset($uri['user']) ? $uri['user'] . ($uri['pass'] ? ':' . $uri['pass'] : '') . '@' : '';
$port = isset($uri['port']) ? $uri['port'] : 80;
$host = $uri['host'] . ($port != 80 ? ':' . $port : '');
$path = isset($uri['path']) ? $uri['path'] : '/';
// Glue the URL variables.
$dst_url = $scheme . $user . $host . $path;
$dst_query = isset($uri['query']) ? $uri['query'] : NULL;
$dst_fragment = isset($uri['fragment']) ? $uri['fragment'] : NULL;
drupal_goto($dst_url, $dst_query, $dst_fragment);
}
}
}
/**
* Implementation of hook_filter
*/
function gotwo_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('"Go" redirection filter'),
);
case 'description':
return t('Automatically creates redirection urls. <go href=""></go> tags are nicely translated to <a href=""></a> tags.');
case 'prepare':
return $text;
case "process":
return preg_replace_callback('#<go\\s([^>]*)>(.*?)</go>#is', '_gotwo_filter', $text);
default:
return $text;
}
}
/**
* This function will strip the <go> link into it's parts, save the link with
* the title to the database and returns a HTML based link for the replacement
* in the content.
*
* @param $link
* A raw <go> link to be processed.
* @return
* The HTML representation of a <go> link.
*/
function _gotwo_filter($link) {
// Split all link tag attributes into key and value pairs.
preg_match_all('/\\s?+([^=]*)=["\']([^"\']*)["\']/i', $link[1], $matches);
$go_attributes = array_combine($matches[1], $matches[2]);
// Drop empty attributes and sort by name.
$go_attributes = array_filter($go_attributes);
ksort($go_attributes);
// Verify if the url exists in the {gotwo} table. If the url is missing,
// add the url with link title to the {gotwo} table.
if (!empty($go_attributes['href'])) {
$go_attributes['href'] = gotwo_get_url($go_attributes['href'], isset($go_attributes['title']) ? $go_attributes['title'] : NULL);
}
// Do not return a link if the go link doesn't haven attributes.
return empty($go_attributes) ? $link[2] : '<a ' . drupal_attributes($go_attributes) . '>' . $link[2] . '</a>';
}
/**
* Return the GO url for a given link
*/
function gotwo_get_url($url, $src = false, $flags = gotwo_CREATE) {
$res = gotwo_get($url, $src, $flags);
if (!$res) {
return url($url);
}
if (variable_get('gotwo_numeric', false)) {
return url('go/' . $res->gid);
}
return url('go/' . $res->src);
}
/**
* Return the GO object url for a given link
*/
function gotwo_get($url, $src = NULL, $flags = gotwo_CREATE) {
// Only add valid URLs to the database. Otherwise the disclaimer reload may fail.
if (!valid_url($url)) {
return FALSE;
}
// If there is no title to mangle, use the url instead.
if (!$src) {
$src = preg_replace('#^(http(s)?://)#', '', $url);
}
$src = __gotwo_mangle_src($src);
$maxlength = min(variable_get('gotwo_max_length', 128), 128);
$res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s' AND dst = '%s'", $src, $url));
if ($res === false) {
$res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = gid+'/%s' AND dst = '%s'", $src, $url));
$src_alt = substr($res->gid . '/' . $src, 0, $maxlength);
if ($src_alt != $res->src) {
$res == false;
}
}
if ($res === false) {
if ($flags & gotwo_CREATE) {
// force unique src
$gid = db_next_id('gotwo_gid');
$res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s'", $src));
if ($res) {
$res->src = substr($gid . '/' . $src, 0, $maxlength);
}
else {
$res = new StdClass();
$res->src = $src;
}
$res->gid = $gid;
$res->dst = $url;
$res->cnt = 0;
db_query("INSERT INTO {gotwo} (gid, src, dst) VALUES (%d, '%s', '%s')", $res->gid, $res->src, $res->dst);
}
else {
return false;
}
}
return $res;
}
/**
* Mangle the input for url friendlyness. Based on pathauto_cleanstring from
* pathauto.module
*/
function __gotwo_mangle_src($string) {
static $translations = array(
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Ā' => 'A',
'Ą' => 'A',
'Ă' => 'A',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'ā' => 'a',
'ą' => 'a',
'ă' => 'a',
'Æ' => 'Ae',
'æ' => 'ae',
'Ç' => 'C',
'Ć' => 'C',
'Č' => 'C',
'Ĉ' => 'C',
'Ċ' => 'C',
'ç' => 'c',
'ć' => 'c',
'č' => 'c',
'ĉ' => 'c',
'ċ' => 'c',
'Ď' => 'D',
'Đ' => 'D',
'Ð' => 'D',
'ď' => 'd',
'đ' => 'd',
'ð' => 'd',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ē' => 'E',
'Ę' => 'E',
'Ě' => 'E',
'Ĕ' => 'E',
'Ė' => 'E',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ē' => 'e',
'ę' => 'e',
'ě' => 'e',
'ĕ' => 'e',
'ė' => 'e',
'ƒ' => 'f',
'Ĝ' => 'G',
'Ğ' => 'G',
'Ġ' => 'G',
'Ģ' => 'G',
'ĝ' => 'g',
'ğ' => 'g',
'ġ' => 'g',
'ģ' => 'g',
'Ĥ' => 'H',
'Ħ' => 'H',
'ĥ' => 'h',
'ħ' => 'h',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ī' => 'I',
'Ĩ' => 'I',
'Ĭ' => 'I',
'Į' => 'I',
'İ' => 'I',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ī' => 'i',
'ĩ' => 'i',
'ĭ' => 'i',
'į' => 'i',
'ı' => 'i',
'IJ' => 'Ij',
'ij' => 'ij',
'Ĵ' => 'J',
'ĵ' => 'j',
'Ķ' => 'K',
'ķ' => 'k',
'ĸ' => 'k',
'Ł' => 'L',
'Ľ' => 'L',
'Ĺ' => 'L',
'Ļ' => 'L',
'Ŀ' => 'L',
'ł' => 'l',
'ľ' => 'l',
'ĺ' => 'l',
'ļ' => 'l',
'ŀ' => 'l',
'Ñ' => 'N',
'Ń' => 'N',
'Ň' => 'N',
'Ņ' => 'N',
'Ŋ' => 'N',
'ñ' => 'n',
'ń' => 'n',
'ň' => 'n',
'ņ' => 'n',
'ʼn' => 'n',
'ŋ' => 'n',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'Ø' => 'O',
'Ō' => 'O',
'Ő' => 'O',
'Ŏ' => 'O',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ō' => 'o',
'ő' => 'o',
'ŏ' => 'o',
'Œ' => 'Oe',
'œ' => 'oe',
'Ŕ' => 'R',
'Ř' => 'R',
'Ŗ' => 'R',
'ŕ' => 'r',
'ř' => 'r',
'ŗ' => 'r',
'Ś' => 'S',
'Š' => 'S',
'Ş' => 'S',
'Ŝ' => 'S',
'Ș' => 'S',
'Ť' => 'T',
'Ţ' => 'T',
'Ŧ' => 'T',
'Ț' => 'T',
'Þ' => 'T',
'þ' => 't',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ū' => 'U',
'Ů' => 'U',
'Ű' => 'U',
'Ŭ' => 'U',
'Ũ' => 'U',
'Ų' => 'U',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ū' => 'u',
'ů' => 'u',
'ű' => 'u',
'ŭ' => 'u',
'ũ' => 'u',
'ų' => 'u',
'Ŵ' => 'W',
'ŵ' => 'w',
'Ý' => 'Y',
'Ŷ' => 'Y',
'Ÿ' => 'Y',
'Y' => 'Y',
'ý' => 'y',
'ÿ' => 'y',
'ŷ' => 'y',
'Ź' => 'Z',
'Ž' => 'Z',
'Ż' => 'Z',
'ž' => 'z',
'ż' => 'z',
'ź' => 'z',
'ß' => 'ss',
'ſ' => 'ss',
);
$output = str_replace("'", "", $string);
$output = strtr($output, $translations);
$pattern = '#[^a-zA-Z0-9./]+# ';
$separator = '_';
$output = preg_replace($pattern, $separator, $output);
if ($separator) {
if (ctype_alnum($separator)) {
$seppattern = $separator;
}
else {
$seppattern = '\\' . $separator;
}
$output = preg_replace("/^{$seppattern}+|{$seppattern}+\$/", "", $output);
}
$maxlength = min(variable_get('gotwo_max_length', 128), 128);
$output = substr($output, 0, $maxlength);
return $output;
}
/**
* Implementation of hook_settings
*/
function gotwo_admin_settings() {
$form['gotwo_numeric'] = array(
'#type' => 'checkbox',
'#title' => t('Numerical urls'),
'#description' => t('Use numbers instead of a more friendlier url. "go/1234" instead of "go/some/location".'),
'#default_value' => variable_get('gotwo_numeric', false),
);
$form['gotwo_max_length'] = array(
'#type' => 'textfield',
'#title' => t('Maximum length of target labels'),
'#description' => t('Target labels are the parts after the "go/" part of the shown url. The absolute maximum is 128.'),
'#default_value' => min(variable_get('gotwo_max_length', 128), 128),
'#size' => 10,
);
$form['gotwo_disclaimer_boolean'] = array(
'#type' => 'checkbox',
'#title' => t('Disclaimer'),
'#description' => t('Check to add a disclaimer before redirecting to the Gotwo links'),
'#default_value' => variable_get('gotwo_disclaimer_boolean', false),
);
$gotwo_time = array(
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
10 => 10,
15 => 15,
30 => 30,
45 => 45,
60 => 60,
);
$form['gotwo_disclaimer_time'] = array(
'#type' => 'select',
'#title' => t('# of seconds until refresh'),
'#options' => $gotwo_time,
'#description' => t('Number of seconds until the page will be redirected to the requested URL, 0 means no refresh'),
'#default_value' => variable_get('gotwo_disclaimer_time', 0),
);
$form['gotwo_disclaimer_text'] = array(
'#type' => 'textarea',
'#title' => t('Disclaimer Text'),
'#description' => t('The disclaimer that will be presented to the user before they are redirected<br><strong>Variables available:</strong><br> %url = url to be redirected to <br>%seconds = # of seconds until page redirects'),
'#default_value' => variable_get('gotwo_disclaimer_text', ''),
);
return system_settings_form($form);
}
/**
* shows the list of go entries
*/
function __gotwo_list() {
drupal_set_title(t('Go list'));
$header = array(
array(
'data' => t('ID'),
'field' => 'gid',
),
array(
'data' => t('Label'),
'field' => 'src',
),
array(
'data' => t('Destination'),
'field' => 'dst',
),
array(
'data' => t('Counter'),
'field' => 'cnt',
'sort' => 'desc',
),
);
if (user_access('edit gotwo entries')) {
$header[] = array(
'data' => t('Tools'),
);
}
$sql = "SELECT * FROM {gotwo}";
$tablesort = tablesort_sql($header);
$result = pager_query($sql . $tablesort, 50);
while ($go = db_fetch_object($result)) {
$i = count($rows);
$rows[$i] = array(
'data' => array(
$go->gid,
check_plain($go->src),
check_plain($go->dst),
$go->cnt,
),
);
if (user_access('edit gotwo entries')) {
$rows[$i]['data'][] = l(t('reset'), 'admin/build/gotwo/reset/' . $go->gid, array(
'title' => t('Reset the counter'),
)) . ' ' . l(t('delete'), 'admin/build/gotwo/delete/' . $go->gid);
}
}
if (!$rows) {
$rows[] = array(
array(
'data' => t('No entries available.'),
'colspan' => 4,
),
);
}
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
return $output;
}
/**
* Create confirmation form for click counter item reset.
*/
function gotwo_reset_confirm($gid) {
$res = db_fetch_object(db_query('SELECT * FROM {gotwo} WHERE gid = %d', $gid));
$form['gid'] = array(
'#type' => 'value',
'#value' => $res->gid,
);
return confirm_form($form, t('Reset click counter for %label', array(
'%label' => $res->src,
)), 'admin/build/gotwo', '<p>' . t('Are you sure you want to reset the click counter for %label? This action cannot be undone.', array(
'%label' => $res->src,
)) . '</p>', t('Reset'), t('Cancel'));
}
/**
* Form submit handler for click counter item reset.
*/
function gotwo_reset_confirm_submit($form_id, $form_values) {
db_query("UPDATE {gotwo} SET cnt = 0 WHERE gid = %d", $form_values['gid']);
return drupal_goto('admin/build/gotwo');
}
/**
* Remove a go entry
*/
function __gotwo_delete_page($gid) {
return drupal_get_form('__gotwo_delete', $gid);
}
/**
* Drupal 5.x changed to using Forms as functions.
* All forms are located below here
*/
function __gotwo_delete($gid) {
$res = db_fetch_object(db_query('SELECT * FROM {gotwo} WHERE gid = %d', $gid));
$form['gid'] = array(
'#type' => 'value',
'#value' => $gid,
);
return confirm_form($form, t('Are you sure you want to delete the go entry with the label %label ?', array(
'%label' => $res->src,
)), 'admin/build/gotwo', t('This action cannot be undone. The link will become broken, a new one might be automatically created when a node linking to it is edited.'), t('Delete'), t('Cancel'));
}
/**
* Remove entry form submitted
*/
function __gotwo_delete_submit($form_id, $form_values) {
if ($form_values['confirm']) {
db_query('DELETE FROM {gotwo} WHERE gid = %d', $form_values['gid']);
}
return drupal_goto('admin/build/gotwo');
}
/**
* Manually add a go entry
*/
function __gotwo_manual_add() {
drupal_set_title('Add go entry');
$form['src'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#description' => t('The label used in the go url, this will automatically be made suitable'),
'#required' => true,
);
$form['dst'] = array(
'#type' => 'textfield',
'#title' => t('Destination'),
'#description' => t('The target url. Can be a relative drupal url or an absolute url.'),
'#required' => true,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* Go entry submitted
*/
function __gotwo_manual_add_submit($form_id, $form_values) {
$res = gotwo_get($form_values['dst'], $form_values['src']);
return 'admin/build/gotwo';
}
Functions
Name![]() |
Description |
---|---|
gotwo_admin_settings | Implementation of hook_settings |
gotwo_filter | Implementation of hook_filter |
gotwo_filter_tips | Implementation of hook_filter_tips(). |
gotwo_get | Return the GO object url for a given link |
gotwo_get_url | Return the GO url for a given link |
gotwo_help | Implementation of hook_help |
gotwo_menu | Implementation of hook_menu |
gotwo_perm | |
gotwo_reset_confirm | Create confirmation form for click counter item reset. |
gotwo_reset_confirm_submit | Form submit handler for click counter item reset. |
_gotwo_filter | This function will strip the <go> link into it's parts, save the link with the title to the database and returns a HTML based link for the replacement in the content. |
__gotwo_delete | Drupal 5.x changed to using Forms as functions. All forms are located below here |
__gotwo_delete_page | Remove a go entry |
__gotwo_delete_submit | Remove entry form submitted |
__gotwo_do_redir | Redirect the user to the given location |
__gotwo_list | shows the list of go entries |
__gotwo_mangle_src | Mangle the input for url friendlyness. Based on pathauto_cleanstring from pathauto.module |
__gotwo_manual_add | Manually add a go entry |
__gotwo_manual_add_submit | Go entry submitted |
Constants
Name![]() |
Description |
---|---|
GOTWO_CREATE | |
GOTWO_NOP | @file Module that provides easy to use redirection links. A redirection link would be like: http://examples.org/go/a_label http://examples.org/go/123546 http://examples.org/go/or/like/this |