--- forward.module.dist	2007-04-26 12:56:57.000000000 -0400
+++ forward.module	2007-04-26 12:56:57.000000000 -0400
@@ -61,7 +61,7 @@
       'path'     => 'forward',
       'title'    => t('Forward this page'),
       'callback' => 'drupal_get_form',
-      'callback arguments' => array('forward_form', arg(1)),
+      'callback arguments' => array('forward_form'),
       'access'   => (user_access('access content')),
       'type'     => MENU_CALLBACK
     );
@@ -69,7 +69,6 @@
       'path'     => 'forward/emailref',
       'title'    => t('Track email clickthrus'),
       'callback' => 'forward_tracker',
-      'callback arguments' => array(arg(2)),
       'access'   => (user_access('access content')),
       'type'     => MENU_CALLBACK
     );
@@ -177,34 +176,52 @@
     '#description' => t('Enter the URL of the image to as a logo at the top of forwarded pages.'),
     '#attributes' => FALSE,
   );
-  $form['forward_default_values']['forward_emailsubject'] = array(
+  // configure strings for forwarding node urls
+  $form['forward_default_values']['forward_node_subject'] = array(
     '#type' => 'textfield',
-    '#title' => t('Forward Message Subject'),
-    '#default_value' => variable_get('forward_emailsubject', t('!name has forwarded a page to you from !site')),
+    '#title' => t('Forward Node Message Subject'),
+    '#default_value' => variable_get('forward_node_subject', t('!name has forwarded a page to you from !site')),
     '#size' => 40,
     '#maxlength' => 256,
     '#description' => t('Email subject line.  The sender\'s name will appear in place of !name in the subject.  The web site name will be inserted in place of !site.'),
   );
-  $form['forward_default_values']['forward_emailmessage'] = array(
+  $form['forward_default_values']['forward_node_message'] = array(
     '#type' => 'textarea',
-    '#title' => t('Forward Message Body'),
-    '#default_value' => variable_get('forward_emailmessage', t('!name thought you would like to see this page from the !site web site.')),
+    '#title' => t('Forward Node Message Body'),
+    '#default_value' => variable_get('forward_node_message', t('!name thought you would like to see this page from the !site web site.')),
     '#cols' => 40,
     '#rows' => 10,
     '#description' => t('Email message body.  The sender\'s name will appear in place of !name in the message body.  The web site name will be inserted in place of !site.  The sender will be able to add their own message after this.'),
   );
-  $form['forward_default_values']['forward_postcardsubject'] = array(
+  // configure strings for forwarding non-node urls
+  $form['forward_default_values']['forward_forward_subject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Forward Page Message Subject'),
+    '#default_value' => variable_get('forward_forward_subject', t('!name has forwarded a page to your from !site')),
+    '#size' => 40,
+    '#maxlength' => 256,
+    '#description' => t('Email subject line.  The sender\'s name will appear in place of !name in the subject.  The web site name will be inserted in place of !site.'),
+  );
+  $form['forward_default_values']['forward_forward_message'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Forward Page Message Body'),
+    '#default_value' => variable_get('forward_forward_message', t('!name thought you would like to see this page from the !site web site.')),
+    '#cols' => 40,
+    '#rows' => 10,
+    '#description' => t('Email message body.  The sender\'s name will appear in place of !name in the message body.  The web site name will be inserted in place of !site.  The sender will be able to add their own message after this.'),
+  );
+  $form['forward_default_values']['forward_epostcard_subject'] = array(
     '#type' => 'textfield',
     '#title' => t('e-Postcard Message Subject'),
-    '#default_value' => variable_get('forward_postcardsubject', t('!name has sent you an e-postcard from !site')),
+    '#default_value' => variable_get('forward_epostcard_subject', t('!name has sent you an e-postcard from !site')),
     '#size' => 40,
     '#maxlength' => 256,
     '#description' => t('Postcard subject line.  The sender\'s name will appear in place of !name in the subject.  The web site name will be inserted in place of !site.'),
   );
-  $form['forward_default_values']['forward_postcardmessage'] = array(
+  $form['forward_default_values']['forward_epostcard_message'] = array(
     '#type' => 'textarea',
     '#title' => t('e-Postcard Message Body'),
-    '#default_value' => variable_get('forward_postcardmessage', t('!name has sent you an e-postcard from the !site web site.  Please take a moment to visit our web site.')),
+    '#default_value' => variable_get('forward_epostcard_message', t('!name has sent you an e-postcard from the !site web site.  Please take a moment to visit our web site.')),
     '#cols' => 40,
     '#rows' => 10,
     '#description' => t('Postcard message body.  The sender\'s name will appear in place of !name in the message body.  The web site name will be inserted in place of !site.  The sender will be able to add their own message after this.'),
@@ -245,34 +262,62 @@
 }
 
 /**
+ * Callback function
+ *
  * Email Tracker
  */
-function forward_tracker($nid) {
-  db_query("INSERT INTO {forward_log} (nid, type, timestamp) VALUES ('%d', '%s', %d)", $nid, "REF", time());
-  if ($nid == 'postcard'){
-    drupal_goto(drupal_get_path_alias(''));
-  }
-  else {
-    drupal_goto(drupal_get_path_alias('node/'.$nid));
+function forward_tracker() {
+  $args = explode('/',$_GET['q']);
+  array_shift($args);
+  array_shift($args);
+  $return_url = implode('/',$args);
+  $nid = '';
+  if ('node' == $args[0] && is_numeric($args[1]) && empty($args[2])) {
+    $nid = $args[1];
+  }
+  elseif (is_numeric($args[0]) && empty($args[1])) { // legacy format
+    $nid = $args[0];
+    $return_url = 'node/'.$nid;
+  }
+  // for now, we're only tracking node page forwards
+  if ($nid) {
+    db_query("INSERT INTO {forward_log} (nid, type, timestamp) VALUES ('%d', '%s', %d)", $nid, "REF", time());
   }
+  drupal_goto($return_url);
 }
 
 /**
- * Form
+ * function forward_form()
+ * 
+ * Callback to generate the forwarding form.
+ * This can be called from either /forward/<forwarding url>
+ * or from /epostcard/
  */
-function forward_form($nid) {
+
+function forward_form() {
   global $base_url, $user;
 
+  // figure out the url to forward, and the nid if it's a node
+  // and the context of the calling url
+  $forward_url = '';
+  $nid = '';
+  $emailtype = 'forward'; // default, non-node page forward
   $form = array();
-
-  if ($nid != '') {
-    $emailtype = 'email';
-    $form['node'] = array('#type' => 'value', '#value' => node_load((int)$nid));
-  }
-  else {
-    $emailtype = 'postcard';
+  if ($_GET['q']) {
+    $q = explode('/', $_GET['q']);
+    // the emailtype is the first arg, which should be 'forward', or 'epostcard'
+    $emailtype = array_shift($q);
+    // test for special case if forwarding a node url
+    if ('node' == $q[0] && is_numeric($q[1])) {
+      if ($nid = $q[1]) {
+        $emailtype = 'node';
+        $form['node'] = array('#type' => 'value', '#value' => node_load((int)$nid));
+      }
+    }
+    $forward_url = implode('/',$q);
   }
-  
+
+  // build the form
   $form['message']['yemail'] = array(
     '#type' => 'textfield',
     '#title' => t('Your Email'),
@@ -303,7 +348,7 @@
     '#attributes' => NULL,
     '#required' => TRUE,
   );
-  if ($emailtype == 'email') {
+  if ('node' == $emailtype) {
     $form['message']['page'] = array(
       '#type' => 'item',
       '#title' => t('You are going to email the following'),
@@ -313,13 +358,13 @@
   $form['message']['subject'] = array(
     '#type' => 'item',
     '#title' => t('Message Subject'),
-    '#value' => t(variable_get('forward_'.$emailtype.'subject', '!name has sent you a message from !site'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
+    '#value' => t(variable_get('forward_'.$emailtype.'_subject', '!name has sent you a message from !site'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
     '#description' => '',
   );
   $form['message']['body'] = array(
     '#type' => 'item',
     '#title' => t('Message Body'),
-    '#value' => t(variable_get('forward_'.$emailtype.'message', '!name thought you would like to see the !site web site.'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
+    '#value' => t(variable_get('forward_'.$emailtype.'_message', '!name thought you would like to see the !site web site.'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
     '#description' => '',
   );
   $form['message']['message'] = array(
@@ -332,10 +377,18 @@
     '#attributes' => NULL,
     '#required' => TRUE,
   );
+  $form['message']['emailtype'] = array(
+    '#type' => 'hidden',
+    '#value' => $emailtype,
+  );
   $form['message']['nid'] = array(
     '#type' => 'hidden',
     '#value' => $nid,
   );
+  $form['message']['forward_url'] = array(
+    '#type' => 'hidden',
+    '#value' => $forward_url,
+  );
   $form['message']['forward_footer'] = array(
     '#type' => 'hidden',
     '#value' => variable_get('forward_footer', ''),
@@ -403,13 +456,13 @@
   $form['message']['subject'] = array(
     '#type' => 'item',
     '#title' => t('Message Subject'),
-    '#value' => t(variable_get('forward_'.$emailtype.'subject', '!name has sent you a message from !site'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
+    '#value' => t(variable_get('forward_'.$emailtype.'_subject', '!name has sent you a message from !site'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
     '#description' => '',
   );
   $form['message']['body'] = array(
     '#type' => 'item',
     '#title' => t('Message Body'),
-    '#value' => t(variable_get('forward_'.$emailtype.'message', '!name thought you would like to see the !site web site.'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
+    '#value' => t(variable_get('forward_'.$emailtype.'_message', '!name thought you would like to see the !site web site.'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
     '#description' => '',
   );
   $form['message']['message'] = array(
@@ -455,7 +508,7 @@
   $recipients = $form['edit']['recipients'];
   $message    = $form['edit']['message'];
   $nid        = $form['edit']['nid'];
-  $url        = $base_url.'/node/'.$nid;
+  $url        = url($form['edit']['forward_url'], NULL, NULL, TRUE);
 
   // normalize address entries
   $recipients = trim($form['recipients']);
@@ -526,30 +579,38 @@
       $dynamic_content .= forward_top5_list($query, $base_url,'blog');
       break;
   }
+  $emailtype = $edit['emailtype'];
+  $forward_url = $edit['forward_url'];
+  // set additional variables according to email type
+  switch($emailtype) {
+    case 'epostcard':
+      $content->title = '';
+      $content->name = '';
+      $content->teaser = '';
+      $returnurl = drupal_get_path_alias('');
+      break;
 
-  if (!$edit['nid']) {
-    $emailtype = 'postcard';
-    $nid = '';
-    $content_title = '';
-    $content_name = '';
-    $content_teaser = '';
-    $returnurl = drupal_get_path_alias('');
-  }
-  else {
-    $nid = $edit['nid'];
-    $emailtype = 'email';
-    //Get article information.
-    $content = node_load($nid);
-    $content->teaser = check_markup($content->teaser, $content->format, FALSE);
-    $returnurl = drupal_get_path_alias('node/'.$nid);
+    case 'node':
+      $nid = $edit['nid'];
+      //Get article information.
+      $content = node_load($nid);
+      $content->teaser = check_markup($content->teaser, $content->format, FALSE);
+      $returnurl = drupal_get_path_alias('node/'.$nid);
+      break;
+
+    default:
+    case 'forward':
+      $returnurl = drupal_get_path_alias($forward_url);
+      break;
   }
   $vars = array(
     'forward_header_image' => variable_get('forward_header_image', ''),
     'site_name' => variable_get('site_name', 'Drupal'),
     'yemail' => $edit['yemail'],
-    'forward_message' => t(variable_get('forward_'.$emailtype.'message', '!name thought you would like to see the !site web site.'), array('!name' => l($edit['yname'], 'mailto:'.$edit['yemail'],NULL,NULL,NULL,TRUE), '!site' => variable_get('site_name', 'drupal'))),
+    'forward_message' => t(variable_get('forward_'.$emailtype.'_message', '!name thought you would like to see the !site web site.'), array('!name' => l($edit['yname'], 'mailto:'.$edit['yemail'],NULL,NULL,NULL,TRUE), '!site' => variable_get('site_name', 'drupal'))),
     'message' => $edit['message'],
     'base_url' => $base_url,
+    'forward_url' => $forward_url,
     'content' => $content,
     'dynamic_content' => $dynamic_content,
     'forward_ad_footer' => variable_get('forward_ad_footer', ''),
@@ -557,7 +618,7 @@
   );
 
   $body = theme('forward_'.$emailtype, $vars);
-  $subject = t(variable_get('forward_'.$emailtype.'subject', '!name has sent you a message from !site'), array('!name' => $edit['yname'], '!site' => variable_get('site_name', 'drupal')));
+  $subject = t(variable_get('forward_'.$emailtype.'_subject', '!name has sent you a message from !site'), array('!name' => $edit['yname'], '!site' => variable_get('site_name', 'drupal')));
 
   $from = $edit['yemail'];
   $headers['MIME-Version'] = '1.0';
@@ -585,8 +646,11 @@
 }
 
 /**
- * Generate links for pages
+ * Implementation of hook_link().
+ *
+ * Generate links for node pages.
  */
+
 function forward_link($type, $node=0, $main=0) {
   if ('comment' == $type && !variable_get('forward_display_comment', '1')) {
     return;
@@ -620,7 +684,7 @@
     if ($node_url == 'node' && (!$main || variable_get('forward_show_on_main', 0))) {
       $links['forward_links'] = array(
         'title'      => t('Email this !type', array('!type' => $forward_link_type)),
-        'href'       => "forward/$node->nid",
+        'href'       => "forward/node/$node->nid",
         'attributes' => array('title' => t('Forward this page to a friend'), 'class' => 'forward-page'));
       return $links;
     }
@@ -768,4 +832,4 @@
 
 function _forward_column_width($column, $width = 35) {
   return (strlen($column) > $width ? substr($column, 0, $width) . '...' : $column);
-}
\ No newline at end of file
+}
