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.
For example:
<go href="http://example.org/some/page">Some page example</go>
produces: <a href="go/example.org/some/page">Some page example</a>
Or like this:<go href="http://example.org/some/page" title="Some page">Some page example</go>
produces: <a href="go/some_page" title="Some page">Some page example</a>
');
}
break;
}
}
/**
* Implementation of hook_menu
*/
function gotwo_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'go',
'callback' => '__gotwo_do_redir',
'access' => user_access('gotwo url'),
'type' => MENU_CALLBACK
);
$items[] = array(
'title' => 'go list',
'path' => 'admin/go',
'callback' => '__gotwo_list',
'access' => user_access('view entry list'),
);
$items[] = array(
'title' => 'list',
'path' => 'admin/go/list',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access' => user_access('view entry list'),
);
$items[] = array(
'title' => 'add',
'path' => 'admin/go/add',
'callback' => '__gotwo_manual_add',
'type' => MENU_LOCAL_TASK,
'access' => user_access('view entry list'),
);
}
else {
$items[] = array(
'path' => 'admin/go/reset',
'callback' => '__gotwo_reset',
'access' => user_access('edit entries'),
'type' => MENU_CALLBACK
);
$items[] = array(
'path' => 'admin/go/remove',
'callback' => '__gotwo_remove',
'access' => user_access('edit entries'),
'type' => MENU_CALLBACK
);
}
return $items;
}
function gotwo_perm() {
return array('view entry list', 'edit entries', 'gotwo url');
}
/**
* Redirect the user to the given location
*/
function __gotwo_do_redir() {
//drupal_set_message(print_r($form_values, true));
$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("UPDATE {gotwo} SET cnt = cnt+1 WHERE gid = '%d'", $res->gid);
if (strstr($res->dst, 'http://')) {
header('Location: ' . $res->dst);
} else {
drupal_goto($res->dst);
}
exit();
}
}
/**
* 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('#]*)>([^<]*)#ise', '__gotwo_filter("\1", "\2")', $text);
default:
return $text;
}
}
/**
* Performs the actual tag translation
*/
function __gotwo_filter($args, $text) {
if (preg_match_all('#\s*([^=]*)\s*=\s*("([^"]*)")|(\'([^\']*)\')\s*#', $args, $_args)) {
$lst = array();
for ($i = 0; $i < count($_args[1]); $i++) {
if (!empty($_args[3][$i])) $lst[$_args[1][$i]] = $_args[3][$i];
else if (!empty($_args[5][$i])) $lst[$_args[1][$i]] = $_args[5][$i];
}
if (isset($lst['href'])) $lst['href'] = gotwo_get_url($lst['href'], isset($lst['title'])?$lst['title']:false);
$args = "";
foreach ($lst as $key => $value) {
$args .= $key.'="'.htmlspecialchars($value).'" ';
}
}
return "$text";
}
/**
* 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=false, $flags=gotwo_CREATE) {
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','ī'=>'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'=>'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_settings() {
$output = form_checkbox(t('Numerical urls'), 'gotwo_numeric', 1, variable_get('gotwo_numeric', false), t('Use numbers instead of a more friendlier url. "go/1234" instead of "go/some/location".'));
$output.=form_textfield(t("Maximum length of target labels"), 'gotwo_max_length', min(128, variable_get('gotwo_max_length', 128)), 20, 20, 'Target labels are the parts after the "go/" part of the shown url. The absolute maximum is 128.');
return $output;
}
/**
* 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 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,
$go->src,
$go->dst,
$go->cnt
)
);
if (user_access('edit entries')) $rows[$i]['data'][] = l(t('reset'), 'admin/go/reset/'.$go->gid, array('title' => t('Reset the counter'))).' '.l(t('remove'), 'admin/go/remove/'.$go->gid);
}
if (!$rows) {
$rows[] = array(array('data' => t('No entries available.'), 'colspan' => 4));
}
print $output;
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
print theme('page', $output);
}
/**
* Reset a counter
*/
function __gotwo_reset($gid) {
db_query("UPDATE {gotwo} SET cnt = 0 WHERE gid = '%d'", $gid);
drupal_goto('admin/go');
}
/**
* Remove a go entry
*/
function __gotwo_remove($gid) {
switch ($_POST['op']) {
case ('Delete'):
__gotwo_remove_submit('',$_POST['edit']);
break;
}
drupal_set_title('Remove go entry');
//$form['gid'] = array('#type' => 'value', '#value' => $gid);
$res = db_fetch_object(db_query('SELECT * FROM {gotwo} WHERE gid = %d', $gid));
$output = t('Are you sure you want to remove the go entry with the label %label ?', array('%label' => theme('placeholder', $res->src)));
$output .= form_hidden('gid', $gid);
$output .= ' ' . 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.');
$output .= form_submit(t('Delete'));
$output .= form_submit(t('Cancel'));
print theme('page', form($output));
}
/**
* Remove entry form submitted
*/
function __gotwo_remove_submit($form_id, $form_values) {
db_query('DELETE FROM {gotwo} WHERE gid = %d', $form_values['gid']);
return drupal_goto('admin/go');
}
/**
* Manually add a go entry
*/
function __gotwo_manual_add() {
switch ($_POST['op']) {
case ('Add'):
__gotwo_manual_add_submit('',$_POST['edit']);
break;
}
drupal_set_title('Add go entry');
$form =form_textfield(t("Label"), 'src', '', 20, 128, 'The label used in the go url, this will automatically be made suitable.');
$form.=form_textfield(t("Destination"), 'dst', '', variable_get('gotwo_max_length', 128), 128, 'The target url. Can be a relative drupal url or an absolute url.');
$form .= form_submit(t('Add'));
$output = form($form);
print theme('page', $output);
}
/**
* Go entry submitted
*/
function __gotwo_manual_add_submit($form_id, $form_values) {
$res = gotwo_get($form_values['dst'], $form_values['src']);
drupal_goto('admin/go/list');
}