Index: forward.module
===================================================================
--- forward.module	(revision 1831)
+++ forward.module	(working copy)
@@ -455,22 +455,27 @@
 
 function forward_tracker() {
   global $user;
-  $form_state['values']['path'] = drupal_get_normal_path($_GET['path']);
-  $args = explode('/', $form_state['values']['path']);
 
-  if (($args[0] == 'node') && (!empty($args[1])) && (is_numeric($args[1]))) {
-    $nid = $args[1];
+  $path_parts = parse_url($_GET['path']);
+  $path = $path_parts['path'];
+
+  $norm_path = drupal_get_normal_path($path);
+  $norm_path_args = explode('/', $norm_path);
+  if (($norm_path_args[0] == 'node') && !empty($norm_path_args[1]) && is_numeric($norm_path_args[1])) {
+    $nid = intval($args[1]);
     db_query("UPDATE {forward_statistics} SET clickthrough_count = clickthrough_count+1 WHERE nid = %d", $nid);
   }
 
-  if ($form_state['values']['path'] == variable_get('site_frontpage', 'node')) {
-    $form_state['values']['path'] = '<front>';
+  if ($norm_path == variable_get('site_frontpage', 'node')) {
+    $path = '<front>';
   }
 
-  db_query("INSERT INTO {forward_log} (path, type, timestamp, uid, hostname) VALUES ('%s', '%s', %d, %d, '%s')", $form_state['values']['path'], 'REF', time(), $user->uid, ip_address());
+  db_query("INSERT INTO {forward_log} (path, type, timestamp, uid, hostname) VALUES ('%s', '%s', %d, %d, '%s')", $_GET['path'], 'REF', time(), $user->uid, ip_address());
 
-  if (!_forward_url_is_external($form_state['values']['path'])) {
-    drupal_goto(drupal_get_path_alias($form_state['values']['path']));
+  if (!_forward_url_is_external($path)) {
+    $query = !empty($path_parts['query']) ? $path_parts['query'] : NULL;
+    $fragment = !empty($path_parts['fragment']) ? $path_parts['fragment'] : NULL;
+    drupal_goto(drupal_get_path_alias($path), $query, $fragment);
   }
   else {
     drupal_goto();
@@ -529,10 +534,9 @@
   }
   else {
     $emailtype = 'page';
-    if (!empty($_GET['cid'])) {
-      $cid = '?cid='. $_GET['cid'];
+    if (!empty($form_state['values']['path']) && !empty($_GET['cid'])) {
+      $form_state['values']['path'] .= '#comment-' . intval($_GET['cid']);
     }
-    $form_state['values']['path'] .= $cid;
     drupal_set_title(check_plain(t(variable_get('forward_email_title', 'Forward this page'))));
   }
   
@@ -548,13 +552,23 @@
   $emailtype = ($path == 'epostcard') ? 'epostcard' : 'page';
 
   $form = array();
-  $cid = array();
+  $url_options = array();
 
-  if (preg_match("/\?cid=/i", $path) == 1) {
-    $paths = explode('?cid=', $path);
-    $cid = array('fragment' => 'comment-'. $paths[1]);
-    $path = $paths[0];
+  // Parse out query arguments or fragments from the path
+  $path_len = strlen($path);
+  $query_pos = strpos($path, '?');
+  $fragment_pos = strpos($path, '#', $query_pos);
+  $path_end = $path_len;
+  if ($fragment_pos) {
+    $url_options['fragment'] = substr($path, $fragment_pos);
+    $path_end = $fragment_pos;
   }
+  if ($query_pos) {
+    $query_end = $fragment_pos ? $fragment_pos : $path_len;
+    $url_options['query'] = substr($path, $query_pos + 1, $query_end - $query_pos);
+    $path_end = $query_pos;
+  }
+  $path = substr($path, 0, $path_end);
 
   if ($nodeapi == TRUE) {
     $form['message'] = array(
@@ -596,7 +610,7 @@
     $form['message']['page'] = array(
       '#type' => 'item',
       '#title' => t('You are going to email the following'),
-      '#value' => l($title, $path, $cid),
+      '#value' => l($title, $path, $url_options),
     );
   }
   $form['message']['subject'] = array(
@@ -628,9 +642,9 @@
     '#type' => 'hidden',
     '#value' => $path,
   );
-  $form['message']['path_cid'] = array(
-    '#type' => 'hidden',
-    '#value' => (!empty($cid['fragment'])) ? '#'. $cid['fragment'] : '',
+  $form['message']['path_options'] = array(
+    '#type' => 'value',
+    '#value' => serialize($url_options),
   );
   $form['message']['forward_footer'] = array(
     '#type' => 'hidden',
@@ -687,10 +701,8 @@
 }
 
 function forward_form_validate($form, &$form_state) {
-  global $base_url, $user;
+  global $user;
 
-  $url = $base_url .'/'. $form_state['values']['path'];
-
   // normalize address entries
   $recipients = trim($form_state['values']['recipients']);
   $recipients = str_replace(array("\r\n", "\n", "\r"), ',', $recipients);
@@ -768,15 +780,15 @@
       break;
   }
 
-  if ((!$form_state['values']['path']) || ($form_state['values']['path'] == 'epostcard')) {
+  if (empty($form_state['values']['path']) || ($form_state['values']['path'] == 'epostcard')) {
     $emailtype = 'epostcard';
     $content = '';
     $returnurl = '';
   }
   else {
     $emailtype = 'email';
-    $returnurl = $form_state['values']['path'];
     $path_array = explode('/', $form_state['values']['path']);
+
     if (($path_array[0] == 'node') && (!empty($path_array[1])) && (is_numeric($path_array[1]))) {
       $nid = $path_array[1];
       // we have a node
@@ -814,6 +826,16 @@
       $content->teaser = '';
       $content->body = '';
     }
+
+    $url_options = !empty($form_state['values']['path_options']) ? unserialize($form_state['values']['path_options']) : array();
+    $returnurl = url($form_state['values']['path'], $url_options);
+    // Removing leading slash so it is formatted as a local path
+    if (substr($returnurl, 0, 1) == '/') {
+      $returnurl = substr($returnurl, 1);
+    }
+    $redirecturl = array($form_state['values']['path']);
+    $redirecturl[] = !empty($url_options['query']) ? $url_options['query'] : '';
+    $redirecturl[] = !empty($url_options['fragment']) ? $url_options['fragment'] : '';
   }
 
   if (variable_get('forward_allow_message', TRUE)) {
@@ -836,20 +858,20 @@
     'message' => $message,
     'base_url' => $base_url,
     'content' => $content,
-    'path' => $returnurl . $form_state['values']['path_cid'],
+    'path' => $returnurl,
     'dynamic_content' => $dynamic_content,
     'forward_ad_footer' => variable_get('forward_ad_footer', ''),
     'forward_footer' => variable_get('forward_footer', ''),
 
     // New values for forward.tpl.php
-    'site_url' => url('forward/emailref', array('absolute' => TRUE, 'query' => 'path='. $returnurl . $form_state['values']['path_cid'])),
+    'site_url' => url('forward/emailref', array('absolute' => TRUE, 'query' => 'path='. $returnurl)),
     'width' => variable_get('forward_width', 400),
     'logo' => (!empty($logo)) ? '<img src="'. url($logo, array('absolute' => TRUE)). '" alt="" />' : '',
     'title' => ($emailtype == 'email') ? l($content->title, 'forward/emailref', array('absolute' => TRUE, 'query' => 'path='. $returnurl)) : FALSE,
     'submitted' => (theme_get_setting('toggle_node_info_'. $content->type)) ? t('by %author', array('%author' => $content->name)) : FALSE,
     'teaser' => ($emailtype == 'email') ? $content->teaser : FALSE,
     'node' => ($emailtype == 'email') ? $content : FALSE,
-    'link' => ($emailtype == 'email') ? l(t('Click here to read more on our site'), 'forward/emailref', array('absolute' => TRUE, 'query' => 'path='. $returnurl . $form_state['values']['path_cid'])) : FALSE,
+    'link' => ($emailtype == 'email') ? l(t('Click here to read more on our site'), 'forward/emailref', array('absolute' => TRUE, 'query' => 'path='. $returnurl)) : FALSE,
   );
 
   if (variable_get('forward_theme_template', 0)) {
@@ -902,7 +924,7 @@
     drupal_mail('forward', 'forward_thankyou', trim($form_state['values']['email']), language_default(), $thankyou_params, $thankyou_params['from']);
   }
 
-  $form_state['redirect'] = ($returnurl != '') ? $returnurl : variable_get('forward_epostcard_return', '');
+  $form_state['redirect'] = !empty($redirecturl) ? $redirecturl : variable_get('forward_epostcard_return', '');
 
   // CRMAPI hook - saves data to default enabled CRM
   if (module_exists('crmapi')) {
