From 9eca2a0c375aefca2719e60993f8968bd1215a48 Mon Sep 17 00:00:00 2001
From: GoZ <goz@226961.no-reply.drupal.org>
Date: Wed, 29 May 2013 15:35:01 +0200
Subject: [PATCH 1/2] Issue #722368 by GoZ: fix path with query another patch

---
 forward.api.php |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
 create mode 100644 forward.api.php

diff --git a/forward.api.php b/forward.api.php
new file mode 100644
index 0000000..b8e32a9
--- /dev/null
+++ b/forward.api.php
@@ -0,0 +1,15 @@
+<?php
+
+/**
+ * Allows alteration of path.
+ *
+ * @param $path
+ *   The path to redirect after email upcoming.
+ */
+function hook_forward_path_alter(&$path) {
+  // Take care of language negociation.
+  if (!variable_get('locale_language_negotiation_url_part', 0)) {
+    global $language;
+    $path = preg_replace('/\/?' . $language->prefix . '\//', '', $path);
+  }
+}
\ No newline at end of file
-- 
1.7.5.4


From be296a567bb81161619d2b416b10e94b58b4c8f3 Mon Sep 17 00:00:00 2001
From: GoZ <goz@226961.no-reply.drupal.org>
Date: Wed, 29 May 2013 15:40:40 +0200
Subject: [PATCH 2/2] Issue #722368 by GoZ: fix path with query another patch

---
 forward.module |   85 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/forward.module b/forward.module
index d418ba9..e84062c 100644
--- a/forward.module
+++ b/forward.module
@@ -502,7 +502,26 @@ function forward_tracker() {
   flood_register_event('forward_tracker', 60);
 
   if (!url_is_external($form_state['values']['path'])) {
-    drupal_goto(drupal_get_path_alias($form_state['values']['path']));
+
+    // Get path, query and fragment.
+    $url = parse_url($form_state['values']['path']);
+    $path = $url['path'];
+    // If path is absolute, make it relative.
+    if (substr($path, 0, 1) == '/') {
+      $path = substr($path, 1);
+    }
+
+    // Get query in an array
+    $query_string = (isset($url['query'])) ? $url['query'] : array();
+    parse_str($query_string, $query);
+
+    // Get fragment from url
+    $fragment = (isset($url['fragment'])) ? $url['fragment'] : array();
+
+    // Clean path deleting prefix (default language prefix).
+    _forward_clean_prefix($path);
+
+    drupal_goto(drupal_get_path_alias($path), array('absolute' => TRUE, 'query' => $query, 'fragment' => $fragment));
   }
   else {
     drupal_goto();
@@ -538,7 +557,7 @@ function forward_page() {
 
   if (!empty($_GET['path'])) {
     $form_state['values']['path'] = drupal_get_normal_path($_GET['path']);
-    $ret = preg_match("/^node\/(.*)/i", $form_state['values']['path'], $matches);
+    $ret = preg_match("/^node\/([0-9]+)/i", $form_state['values']['path'], $matches);
     if ($ret == 1) {
       $nid = $matches[1];
     }
@@ -552,7 +571,10 @@ function forward_page() {
 
       return drupal_access_denied();
     }
-    $form_state['values']['path'] = 'node/' . $node->nid;
+    // Get end of url (so parameters if there is or anything else)
+    $end_path = str_replace('node/' . $node->nid, '', $form_state['values']['path']);
+
+    $form_state['values']['path'] = 'node/' . $node->nid . $end_path;
   }
   else {
     $args = explode('/', $form_state['values']['path']);
@@ -586,6 +608,8 @@ function forward_page() {
 function forward_form($form, &$form_state, $path = NULL, $title = NULL, $nodeapi = FALSE) {
   global $base_url, $user;
 
+  $query = array();
+
   $emailtype = ($path == 'epostcard') ? 'epostcard' : 'email';
 
   $form = array();
@@ -597,6 +621,14 @@ function forward_form($form, &$form_state, $path = NULL, $title = NULL, $nodeapi
     $path = $paths[0];
   }
 
+  // Extract query from path
+  $url = parse_url($path);
+  if (isset($url['query'])) {
+    $path = $url['path'];
+    parse_str(rawurldecode($url['query']), $query);
+    $cid['query'] = $query;
+  }
+
   if ($nodeapi == TRUE) {
     $form['message'] = array(
       '#type' => 'fieldset',
@@ -665,13 +697,14 @@ function forward_form($form, &$form_state, $path = NULL, $title = NULL, $nodeapi
       '#required' => (variable_get('forward_message', 1) == 2) ? TRUE : FALSE,
     );
   }
+
   $form['message']['path'] = array(
     '#type' => 'hidden',
-    '#value' => $path,
+    '#value' => url($path, $cid),
   );
   $form['message']['path_cid'] = array(
     '#type' => 'hidden',
-    '#value' => (!empty($cid['fragment'])) ? '#' . $cid['fragment'] : '',
+    '#value' => (!empty($cid['fragment'])) ? $cid['fragment'] : '',
   );
   $form['message']['forward_footer'] = array(
     '#type' => 'hidden',
@@ -737,9 +770,6 @@ function forward_node_delete($node) {
  * @see http://drupal.org/node/1354
  */
 function forward_form_validate($form, &$form_state) {
-  global $base_url, $user;
-
-  $url = $base_url . '/' . $form_state['values']['path'];
 
   // normalize address entries
 
@@ -893,6 +923,23 @@ function forward_form_submit($form, &$form_state) {
     drupal_save_session($old_state);
   }
 
+  $path = $form_state['values']['path'];
+  // Delete first / so make this path relative instead of absolute.
+  if (substr($path, 0, 1) == '/') {
+    $path = substr($path, 1);
+  }
+
+  // Get path AND query.
+  $url = parse_url($path);
+  $path = $url['path'];
+  $query_string = (isset($url['query'])) ? $url['query'] : array();
+  parse_str($query_string, $query);
+
+  // Clean prefix
+  _forward_clean_prefix($path);
+
+  $path = drupal_get_normal_path($path);
+
   // Send email of appropruate type based on module configuration
   if ((!$form_state['values']['path']) || ($form_state['values']['path'] == 'epostcard')) {
     $emailtype = 'epostcard';
@@ -902,7 +949,8 @@ function forward_form_submit($form, &$form_state) {
   else {
     $emailtype = 'email';
     $returnurl = $form_state['values']['path'];
-    $path_array = explode('/', $form_state['values']['path']);
+
+    $path_array = explode('/', $path);
     if (($path_array[0] == 'node') && (!empty($path_array[1])) && (is_numeric($path_array[1]))) {
       $nid = $path_array[1];
       // we have a node
@@ -913,7 +961,7 @@ function forward_form_submit($form, &$form_state) {
 
         return drupal_access_denied();
       }
-      //dsm($content);
+
       $node = $content;
       $node->content = array();
       $langcode = $GLOBALS['language_content']->language;
@@ -981,6 +1029,8 @@ function forward_form_submit($form, &$form_state) {
   $theme_key = variable_get('theme_default', '');
   $logo = (variable_get('forward_header_image', '') == '') ? theme_get_setting('logo') : variable_get('forward_header_image', '');
 
+  $url = url($path, array('fragment' => $form_state['values']['path_cid'], 'query' => $query));
+
   $vars = array(
     'type' => $emailtype,
     'site_name' => check_plain(variable_get('site_name', 'Drupal')),
@@ -990,7 +1040,7 @@ function forward_form_submit($form, &$form_state) {
     'message' => $message,
     'base_url' => $base_url,
     'content' => $content,
-    'path' => $returnurl . $form_state['values']['path_cid'],
+    'path' => $url,
     'dynamic_content' => $dynamic_content,
     'forward_ad_footer' => variable_get('forward_ad_footer', ''),
     'forward_footer' => variable_get('forward_footer', ''),
@@ -1614,6 +1664,17 @@ function forward_views_api() {
   );
 }
 
+function _forward_clean_prefix(&$path) {
+  // Take care of language negociation.
+  if (!variable_get('locale_language_negotiation_url_part', 0)) {
+    global $language;
+    $path = preg_replace('/\/?' . $language->prefix . '\//', '', $path);
+  }
+
+  // Let other modules to clean prefix (so modules like og could hook this).
+  module_invoke_all('forward_path_alter', $path);
+}
+
 
 /**
  * Modify the drupal mail system to send HTML emails for the forward module.
@@ -1663,4 +1724,4 @@ class ForwardMailSystem implements MailSystemInterface {
       join("\n", $mimeheaders)
     );
   }
-}
\ No newline at end of file
+}
-- 
1.7.5.4

