Index: token.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/token/token.module,v
retrieving revision 1.7.4.9
diff -u -r1.7.4.9 token.module
--- token.module	29 Aug 2008 15:21:40 -0000	1.7.4.9
+++ token.module	21 Oct 2008 16:00:05 -0000
@@ -158,7 +158,9 @@
   if (module_exists('comment')) {
     require_once("$path/token_comment.inc");
   }
-
+  if (module_exists('profile')) {
+    include_once("$path/token_profile.inc");
+  }
 }
 
 /**
Index: token_profile.inc
===================================================================
RCS file: token_profile.inc
diff -N token_profile.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ token_profile.inc	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,92 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_token_values().
+ */
+function profile_token_values($type, $object = NULL) {
+  $values = array();
+  if ($type == 'user') {
+    if (isset($object)) {
+      $account = $object;
+    }
+    else {
+      global $user;
+      $account = $user;
+    }
+
+    $fields = _profile_token_profile_fields();
+
+    foreach ($fields as $name => $field) {
+      $field->value = $account->$name;
+
+      if (_profile_field_serialize($field->type)) {
+        $field->value = unserialize($field->value);
+      }
+
+      $values[$field->name] = _profile_token_format_field($field);
+    }
+  }
+
+  return $values;
+}
+
+/**
+ * Implementation of hook_token_list().
+ */
+function profile_token_list($type = 'all') {
+  if ($type == 'user' || $type == 'all') {
+    $fields = _profile_token_profile_fields();
+
+    foreach ($fields as $name => $field) {
+      $tokens['user'][$field->name] = $field->title;
+    }
+
+    return $tokens;
+  }
+}
+
+function _profile_token_profile_fields() {
+  static $fields;
+
+  if (!isset($fields)) {
+    $fields = array();
+    $result = db_query('SELECT name, title, type FROM {profile_fields}');
+    while ($field = db_fetch_object($result)) {
+      $fields[$field->name] = $field;
+    }
+  }
+
+  return $fields;
+}
+
+function _profile_token_format_field($field) {
+  switch ($field->type) {
+    case 'checkbox':
+      return check_plain($field->title);
+    case 'date':
+      $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
+      // Note: Avoid PHP's date() because it does not handle dates before
+      // 1970 on Windows. This would make the date field useless for e.g.
+      // birthdays.
+      $replace = array('d' => sprintf('%02d', $field->value['day']),
+                       'j' => $field->value['day'],
+                       'm' => sprintf('%02d', $field->value['month']),
+                       'M' => map_month($field->value['month']),
+                       'Y' => $field->value['year'],
+                       'H:i' => NULL,
+                       'g:ia' => NULL);
+      return strtr($format, $replace);
+    case 'list':
+      $values = split("[,\n\r]", $field->value);
+      $fields = array();
+      foreach ($values as $value) {
+        if ($value = trim($value)) {
+          $fields[] = check_plain($value);
+        }
+      }
+      return implode(', ', $fields);
+    default:
+      return check_plain($field->value);
+  }
+}
