From a5dba069fb404bc0f2999bf2732154c75d5e4f2c Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Fri, 9 Oct 2009 15:39:12 +0000
Subject: [PATCH] - #440876 by Dave Reid, sun: reuse comment.module's anonymous
 cookie information for contact forms.

---
 modules/comment/comment.js        | 23 +----------------------
 modules/comment/comment.module    | 18 +++++++-----------
 modules/contact/contact.js        | 15 +++++++++++++++
 modules/contact/contact.pages.inc | 12 +++++++++++-
 modules/user/user.module          | 17 +++++++++++++++++
 5 files changed, 51 insertions(+), 34 deletions(-)
 create mode 100644 modules/contact/contact.js

diff --git a/modules/comment/comment.js b/modules/comment/comment.js
index 58f00dc6b526..753e9925203f 100644
--- a/modules/comment/comment.js
+++ b/modules/comment/comment.js
@@ -4,7 +4,7 @@
 Drupal.behaviors.comment = {
   attach: function (context, settings) {
     $.each(['name', 'homepage', 'mail'], function () {
-      var cookie = Drupal.comment.getCookie('comment_info_' + this);
+      var cookie = $.cookie('Drupal.visitor.' + this);
       if (cookie) {
         $('#comment-form input[name=' + this + ']', context).once('comment').val(cookie);
       }
@@ -12,25 +12,4 @@ Drupal.behaviors.comment = {
   }
 };
 
-Drupal.comment = {};
-
-Drupal.comment.getCookie = function (name) {
-  var search = name + '=';
-  var returnValue = '';
-
-  if (document.cookie.length > 0) {
-    offset = document.cookie.indexOf(search);
-    if (offset != -1) {
-      offset += search.length;
-      var end = document.cookie.indexOf(';', offset);
-      if (end == -1) {
-        end = document.cookie.length;
-      }
-      returnValue = decodeURIComponent(document.cookie.substring(offset, end).replace(/\+/g, '%20'));
-    }
-  }
-
-  return returnValue;
-};
-
 })(jQuery);
diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index b9b7cd18c466..d3facf4c6b4f 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -1637,7 +1637,8 @@ function comment_form($form, &$form_state, $comment) {
   $node = node_load($comment->nid);
 
   if (!$user->uid && variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
-    $form_state['#attached']['js'][] = drupal_get_path('module', 'comment') . '/comment.js';
+    $form['#attached']['library'][] = array('system', 'cookie');
+    $form['#attached']['js'][] = drupal_get_path('module', 'comment') . '/comment.js';
   }
 
   $comment = (array) $comment;
@@ -1957,15 +1958,6 @@ function comment_form_validate($form, &$form_state) {
   $comment = (object) $form_state['values'];
   field_attach_form_validate('comment', $comment, $form, $form_state);
 
-  if ($user->uid === 0) {
-    foreach (array('name', 'homepage', 'mail') as $field) {
-      // Set cookie for 365 days.
-      if (isset($form_state['values'][$field])) {
-        setcookie('comment_info_' . $field, $form_state['values'][$field], REQUEST_TIME + 31536000, '/');
-      }
-    }
-  }
-
   if (isset($form_state['values']['date'])) {
     if (strtotime($form_state['values']['date']) === FALSE) {
       form_set_error('date', t('You have to specify a valid date.'));
@@ -2068,6 +2060,11 @@ function comment_form_submit($form, &$form_state) {
   $node = node_load($form_state['values']['nid']);
   $comment = comment_form_submit_build_comment($form, $form_state);
   if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) {
+    // Save the anonymous user information to a cookie for reuse.
+    if (!$comment->uid) {
+      user_cookie_save($form_state['values']);
+    }
+
     comment_save($comment);
     // Explain the approval queue if necessary.
     if ($comment->status == COMMENT_NOT_PUBLISHED) {
@@ -2425,4 +2422,3 @@ function comment_filter_format_delete($format, $fallback) {
     ->condition('format', $format->format)
     ->execute();
 }
-
diff --git a/modules/contact/contact.js b/modules/contact/contact.js
new file mode 100644
index 000000000000..effac8145879
--- /dev/null
+++ b/modules/contact/contact.js
@@ -0,0 +1,15 @@
+// $Id$
+(function ($) {
+
+Drupal.behaviors.contact = {
+  attach: function(context) {
+    $.each(['name', 'mail'], function () {
+      var cookie = $.cookie('Drupal.user.' + this);
+      if (cookie) {
+        $('#contact-site-form input[name=' + this + ']', context).once('comment').val(cookie);
+      }
+    });
+  }
+};
+
+})(jQuery);
diff --git a/modules/contact/contact.pages.inc b/modules/contact/contact.pages.inc
index d4a15bea5711..5ad41d6f130e 100644
--- a/modules/contact/contact.pages.inc
+++ b/modules/contact/contact.pages.inc
@@ -52,6 +52,11 @@ function contact_site_form($form, &$form_state) {
     }
   }
 
+  if (!$user->uid) {
+    $form['#attached']['library'][] = array('system', 'cookie');
+    $form['#attached']['js'][] = drupal_get_path('module', 'contact') . '/contact.js';
+  }
+
   $form['#token'] = $user->uid ? $user->name . $user->mail : '';
   $form['name'] = array(
     '#type' => 'textfield',
@@ -117,10 +122,15 @@ function contact_site_form_validate($form, &$form_state) {
  * Form submission handler for contact_site_form().
  */
 function contact_site_form_submit($form, &$form_state) {
-  global $language;
+  global $user, $language;
 
   $values = $form_state['values'];
 
+  // Save the anonymous user information to a cookie for reuse.
+  if (!$user->uid) {
+    user_cookie_save($values);
+  }
+
   // E-mail address of the sender: as the form field is a text field,
   // all instances of \r and \n have been automatically stripped from it.
   $from = $values['mail'];
diff --git a/modules/user/user.module b/modules/user/user.module
index 6ca3039dbdc0..5fcb39e93b0b 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -3226,3 +3226,20 @@ function user_login_destination() {
   return $destination;
 }
 
+/**
+ * Saves visitor information as a cookie so it can be reused.
+ *
+ * @param $values
+ *   An array of submitted form values with identifying information about the
+ *   current user, typically $form_state['values'] from a submit handler.
+ * @param $fields
+ *   An array of key values from $values to be saved into a cookie.
+ */
+function user_cookie_save(array $values, array $fields = array('name', 'mail', 'homepage')) {
+  foreach ($fields as $field) {
+    if (isset($values[$field])) {
+      // Set cookie for 365 days.
+      setrawcookie('Drupal.visitor.' . $field, rawurlencode($values[$field]), REQUEST_TIME + 31536000, '/');
+    }
+  }
+}
-- 
GitLab