Go module
This module allows you to create links to external sites in the form:
http://example.com/go/your_alias
Where your_alias is whatever you choose. These addresses are intercepted and redirected to the external site.
Go module can be useful for monitoring the number of people on your mailing list who follow any links you include.
');
}
}
/**
* Implementation of hook_menu().
*/
function go_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/build/go',
'title' => t('External redirects'),
'callback' => 'go_admin',
'access' => user_access('administer external redirects')
);
$items[] = array(
'path' => 'admin/build/go/edit',
'title' => t('Edit redirect'),
'callback' => 'drupal_get_form',
'callback arguments' => array('go_entry_form'),
'access' => user_access('administer external redirects'),
'type' => MENU_CALLBACK
);
$items[] = array(
'path' => 'admin/build/go/delete',
'title' => t('Delete redirect'),
'callback' => 'go_admin_delete',
'access' => user_access('administer external redirects'),
'type' => MENU_CALLBACK
);
$items[] = array(
'path' => 'admin/build/go/list',
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10);
$items[] = array(
'path' => 'admin/build/go/add',
'title' => t('Add'),
'callback' => 'drupal_get_form',
'callback arguments' => array('go_entry_form'),
'access' => user_access('administer external redirects'),
'type' => MENU_LOCAL_TASK
);
}
return $items;
}
/**
* Menu callback; presents an overview of all URL aliases.
*/
function go_admin() {
print theme('page', go_overview());
}
/**
* Menu callback; handles page for editing URL aliases.
*/
function go_entry_form($redirect = NULL) {
$form['redirect'] = array(
'#type' => 'fieldset',
'#title' => t('External link'),
);
$form['redirect']['alias'] = array(
'#type' => 'textfield',
'#title' => t('Alias'),
'#description' => t('Specify the alias you wish to use. This will appear in the redirect address like: %url.',
array('%url' => 'http://'. $_SERVER['HTTP_HOST'] . base_path() .'go/alias')),
'#size' => 50,
'#maxlength' => 64,
);
$form['redirect']['dest'] = array(
'#type' => 'textfield',
'#title' => t('Target'),
'#description' => t('Specify the external website to which you want this address to redirect.'),
'#size' => 50,
'#maxlength' => 64,
);
$form['redirect']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
if ($redirect) {
$redirect = go_load($redirect);
$form['redirect']['alias']['#default_value'] = $redirect['alias'];
$form['redirect']['alias']['#disabled'] = TRUE;
$form['redirect']['dest']['#default_value'] = $redirect['dest'];
$form['redirect']['submit']['#value'] = t('Update');
$form['go_edit'] = array(
'#type' => 'hidden',
'#value' => 1,
);
}
$form['#validate'] = array(
'go_entry_form_validate' => array(),
);
return $form;
}
function go_entry_form_validate($form_id, $form_values) {
if (empty($_POST['go_edit']) && db_result(db_query("SELECT COUNT(dest) FROM {go} WHERE alias = '%s'", $form_values['alias']))) {
form_set_error('alias', t('The alias %alias has already been used.', array('%alias' => $form_values['alias'])));
}
if (!valid_url($form_values['dest'], TRUE)) {
form_set_error('dest', t('The url %dest is invalid.', array('%dest' => $form_values['dest'])));
}
}
/**
* Verify that a new URL alias is valid, and save it to the database.
*/
function go_entry_form_submit($form_id, $form_values) {
if (db_result(db_query("SELECT COUNT(dest) FROM {go} WHERE alias = '%s'", $form_values['alias']))) {
db_query("UPDATE {go} SET dest = '%s', type = '%d' WHERE alias = '%s'",
$form_values['dest'], $form_values['type'], $form_values['alias']);
}
else {
db_query("INSERT INTO {go} (alias, dest, type) VALUES ('%s', '%s', '%d')",
$form_values['alias'], $form_values['dest'], $form_values['type']);
}
drupal_set_message(t('External redirect has been saved.'));
drupal_goto('admin/build/go');
}
function go_confirm_delete($alias) {
return confirm_form(array(),
t('Are you sure you want to delete the external redirect %alias?', array('%alias' => $alias)),
'admin/build/go/edit/'. $alias,
t('This action cannot be undone.'),
t('Delete'), t('Cancel'));
}
/**
* Delete an external redirect.
*
* @param $alias The alias of the external redirect to be deleted.
*/
function go_delete($alias) {
db_query("DELETE FROM {go} WHERE alias = '%s'", $alias);
watchdog('go', t('Deleted external redirect: %alias.', array('%alias' => $alias)), WATCHDOG_NOTICE);
drupal_set_message(t('%alias has been deleted.', array('%alias' => $alias)));
}
/**
* Menu callback; handles deletion of an URL alias.
*/
function go_admin_delete($alias) {
if (!empty($_POST['confirm'])) {
go_delete($alias);
drupal_goto('admin/build/go');
}
else {
return drupal_get_form('go_confirm_delete', $alias);
}
}
/**
* Return a listing of all defined external redirects
*/
function go_overview() {
$sql = 'SELECT * FROM {go}';
$header = array(
array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'),
array('data' => t('Target'), 'field' => 'dest'),
//array('data' => t('Type'), 'field' => 'type'), - Only one type atm
array('data' => t('Count'), 'field' => 'count'),
array('data' => t('Operations'), 'colspan' => 2)
);
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
while ($data = db_fetch_object($result)) {
$rows[] = array(l($data->alias, "go/$data->alias", array('target' => '_blank')), $data->dest, intval($data->count), l(t('edit'), "admin/build/go/edit/$data->alias"), l(t('delete'), "admin/build/go/delete/$data->alias"));
}
if ($pager = theme('pager', NULL, 50, 0)) {
$rows[] = array(array('data' => $pager, 'colspan' => '5'));
}
if (!$rows) {
$rows[] = array(array('data' => t('No redirects set.'), 'colspan' => '5'));
}
return theme('table', $header, $rows);
}
/**
* Fetch a specific URL alias from the database.
*/
function go_load($alias) {
return db_fetch_array(db_query_range("SELECT * FROM {go} WHERE alias = '%s'", $alias, 0, 1));
}
function go_init() {
// Is this a 'go' type node
if (substr($_GET['q'], 0, 3) == 'go/') {
$alias = substr($_GET['q'], 3); // Get the alias
$result = go_load($alias);
// Update the count
db_query("UPDATE {go} SET count = count + 1 WHERE alias = '%s'", $alias);
// Redirect
switch ($result['type']) {
case '0':
// direct: Straight redirect
header('Location: '.$result['dest']);
die();
break;
case '1':
// frame: Inside a frameset
require_once 'frameset.inc';
die();
break;
case '2':
// splash: Exit splashscreen, with meta refresh after n seconds
die();
break;
case '3':
// manual: Exit splash with manual click through
die();
break;
}
}
}
function go_perm() {
return array('administer external redirects');
}
?>