Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.836 diff -u -p -r1.836 common.inc --- includes/common.inc 9 Dec 2008 11:09:26 -0000 1.836 +++ includes/common.inc 9 Dec 2008 23:19:11 -0000 @@ -2878,6 +2878,7 @@ function _drupal_bootstrap_full() { require_once DRUPAL_ROOT . '/includes/form.inc'; require_once DRUPAL_ROOT . '/includes/mail.inc'; require_once DRUPAL_ROOT . '/includes/actions.inc'; + require_once DRUPAL_ROOT . '/includes/token.inc'; // Set the Drupal custom error handler. set_error_handler('_drupal_error_handler'); set_exception_handler('_drupal_exception_handler'); Index: includes/module.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/module.inc,v retrieving revision 1.134 diff -u -p -r1.134 module.inc --- includes/module.inc 24 Nov 2008 10:41:39 -0000 1.134 +++ includes/module.inc 9 Dec 2008 23:19:12 -0000 @@ -280,6 +280,7 @@ function module_enable($module_list) { module_list(TRUE); // Force to regenerate the stored list of hook implementations. registry_rebuild(); + token_refresh(); } foreach ($invoke_modules as $module) { @@ -330,6 +331,7 @@ function module_disable($module_list) { module_list(TRUE); // Force to regenerate the stored list of hook implementations. registry_rebuild(); + token_refresh(); } // If there remains no more node_access module, rebuilding will be Index: includes/token.inc =================================================================== RCS file: includes/token.inc diff -N includes/token.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/token.inc 9 Dec 2008 23:19:12 -0000 @@ -0,0 +1,274 @@ + $category) { + $rows[] = array(array('data' => drupal_ucfirst($key) .' '. t('tokens'), 'class' => 'region', 'colspan' => 2)); + foreach ($category as $token => $description) { + $row = array(); + $row[] = $prefix . $token . $suffix; + $row[] = $description; + $rows[] = $row; + } + } + + $output = theme('table', $headers, $rows, array('class' => 'description')); + return $output; +} + +/** + * A helper function that retrieves all currently exposed tokens, + * and merges them recursively. This is only necessary when building + * the token listing. + * + * @param type + * A flag indicating the class of substitution tokens to use. If an + * object is passed in the second param, 'type' should contain the + * object's type. For example, 'node', 'comment', or 'user'. If no + * type is specified, only 'global' site-wide substitution tokens are + * built. + * @return + * The array of usable tokens and their descriptions, organized by + * token type. + */ +function token_get_list($type = 'all') { + $return = array(); + foreach (module_implements('token_list') as $module) { + $function = $module .'_token_list'; + $result = $function($type); + if (is_array($result)) { + foreach ($result as $category => $tokens) { + foreach ($tokens as $token => $title) { + $return[$category][$token] = $title; + } + } + } + } + return $return; +} + +/** + * Return the value of $original, with all instances of placeholder + * tokens replaced by their proper values. + * + * @param $original + * A string, or an array of strings, to perform token substitutions + * on. + * @param $object + * Optionally, the object to use for building substitution values. + * A node, comment, user, etc. + * @return + * The modified version of $original, with all substitutions made. + */ +function token_replace($original, $object = NULL) { // TODO: $original could be an array of strings. + $tokens = _token_find_tokens($original); + $values = token_get_values(array_unique(array_values($tokens)), $object); + $map = _token_create_substitution_map($tokens, $values); + return str_replace(array_keys($map), array_values($map), $original); +} + +/** + * This function clears and repopulates the {token} database table which stores + * the mapping between token names and associated value functions. + */ +function token_refresh() { + $table = "token"; + db_delete($table)->execute(); + + // Repopulate the table. + $result = module_invoke_all("tokens"); + foreach ($result as $name => $function) { + $fields = array( + 'name' => $name, + 'function' => $function, + ); + db_insert("token")->fields($fields)->execute(); + } +} + +/** + * Creates an associative array that serves as a substitution map. This + * map has the full token string (eg "[token]") as the key and the + * replacement value as the value in the associative array. + * + * @param $tokens + * An associative array that maps the original token text to the base + * token name. Example "[token]" => "token". + * @param $values + * An associative array that maps the base token name to the unfiltered + * value that the token represents. + * @return + * An associative array of tokens in which the key is the full token + * and the value is the associated value. + */ +function _token_create_substitution_map($tokens, $values) { + $result = array(); + foreach($tokens as $token => $name) { + if (isset($result[$token])) { + continue; + } + $value = _token_get_value($token, $values); + if (isset($value)) { + $result[$token] = $value; + } + } + return $result; +} + +/** + * Inspects the specified string and returns an associative array in which + * the key is the token text and the value is the token name. + * + * @param $original + * The original string that may contain tokens that should be replaced. + * @return + * An associative array identifying all of the tokens found in the + * specified text. + */ +function _token_find_tokens($original) { + $result = array(); + $matches = array(); + preg_match_all("/\[[\S]+\]/", $original, $matches); + $len = sizeof($matches[0]); + for ($i = 0; $i < $len; $i++) { + $result[$matches[0][$i]] = substr($matches[0][$i], 1, -1); + } + return $result; +} + +/** + * Returns a mapping between the token name and the unfiltered value. + * + * @param $token_names + * An array of token names for which values are being requested. + * @param $object + * An optional object that will be passed to the function responsible for + * returning the value for a specified token. + * @return + * An associative array that maps token names to their associated raw + * values. + */ +function token_get_values($token_names, $object = NULL) { + $result = array(); + $function_map = _token_get_token_functions($token_names); + foreach($function_map as $name => $function) { + if (isset($name) && drupal_function_exists($function)) { + $result[$name] = call_user_func($function, $object); + } + } + return $result; +} + +/** + * Returns an associative array mapping the token name + * to the function which is responsible for returning the + * token value. + * + * A simple caching scheme is employed to reduce the number of database + * queries performed per request in cases where the same tokens are used more + * than once. + * + * @param $token_names + * An array representing all of the token names to be replaced in the + * original string that was passed to token_replace(). + * @return + * An associative array mapping token names to the functions responsible + * for returning the respective values. + */ +function _token_get_token_functions($token_names) { + static $token_function_map = array(); + $result = array(); + + // If the token name=>function mapping has already been discovered + // avoid additional db queries. + $len = sizeof($token_names); + for ($i = 0; $i < $len; $i++) { + $token_name = $token_names[$i]; + if (isset($token_function_map[$token_name])) { + unset($token_names[$i]); + $result[$token_name] = $token_function_map[$token_name]; + } + } + + // Get all token name=>function associations in a single database + // query. + $query = _token_get_token_query($token_names); + if ($query == NULL) { + // There are no tokens to query for. + return $result; + } + $query_result = db_query($query); + while ($obj = db_fetch_object($query_result)) { + $result[$obj->name] = $obj->function; + $token_function_map[$obj->name] = $obj->function; + } + return $result; +} + +/** + * Returns a database query string that can be used to discover which functions + * should be called to get values for each of the token names in the specified + * array. + * + * @param $token_names + * An array of token names. This array should not have duplicate entries. + * @return + * A string representing a database query that will provide the token name/ + * value function mapping. + */ +function _token_get_token_query($token_names) { + $functions = array(); + $query = "SELECT name, function FROM {token} WHERE"; + $first = TRUE; + foreach (array_values($token_names) as $token_name) { + if (!isset($token_name)) { + continue; + } + if ($first) { + $first = FALSE; + } + else { + $query .= " OR"; + } + $query .= " name = '" .$token_name ."'"; + } + if ($first == TRUE) { + // There are no token names to check. + return NULL; + } + return db_rewrite_sql($query); +} + +/** + * Returns the value for the specified token. + * + * @param $token + * The token from the original string to be replaced + * @param $values + * An associative array of values. This array holds the token name as the + * key and the unchecked value as the value. + * @return + * The value. + */ +function _token_get_value($token, $values) { + // Grab the token name from the token string. + $token_name = substr($token, 1, -1); + $value = NULL; + if (isset($values[$token_name])) { + $value = $values[$token_name]; + } + return $value; +} Index: modules/book/book.module =================================================================== RCS file: /cvs/drupal/drupal/modules/book/book.module,v retrieving revision 1.477 diff -u -p -r1.477 book.module --- modules/book/book.module 9 Dec 2008 11:30:24 -0000 1.477 +++ modules/book/book.module 9 Dec 2008 23:19:12 -0000 @@ -1194,3 +1194,103 @@ function book_menu_subtree_data($item) { return $tree[$cid]; } + +/** + * Implementation of hook_token_list(). + */ +function book_token_list($type = 'all') { + if ($type == 'node' || $type == 'all') { + $tokens['node']['book-id'] = t('Book id'); + $tokens['node']['book-title'] = t('Book title'); + $tokens['node']['book-path'] = t('Book path'); + + return $tokens; + } +} + +/** + * Implementation of hook_tokens(). + */ +function book_tokens() { + $tokens = array(); + $tokens['book_id'] = 'book_get_id'; + $tokens['book-title'] = 'book_get_title'; + $tokens['bookpath'] = 'book_get_path'; + return $tokens; +} + +/** + * Returns true if the specified object is a node. + * + * @param $object + * The object + * @return + * True if the object is a node. + */ +function _book_is_node($object) { + return (isset($object) && isset($object->type) && $object->type == 'node'); +} + +/** + * Returns the id of the book. + * + * @param $object + * The object + * @return + * The book id. + */ +function book_get_id($object = NULL) { + if (_book_is_node($object)) { + $node = $object; + if (isset($node->parent)) { + return $node->parent; + } + } + return ''; +} + +/** + * Returns the title of the book. + * + * @param $object + * The book. + * @return + * The book title. + */ +function book_get_title($object = NULL) { + if (_book_is_node($object)) { + $node = $object; + if (isset($node->parent)) { + $path = book_location($node); + return $path[0]->title; + } + } + return ''; +} + +/** + * Returns the path of the book. + * + * @param $object + * The book. + * @return + * The book path. + */ +function book_get_path($object = NULL) { + if (_book_is_node($object)) { + $node = $object; + if (isset($node->parent)) { + $bookhierarchy = book_location($node); + $bookpath = ''; + foreach ($bookhierarchy as $bookelement) { + if ($bookpath == '') { + $bookpath = $bookelement->title; + } + else { + $bookpath = $bookpath .'/'. $bookelement->title; + } + } + } + } + return ''; +} Index: modules/comment/comment.module =================================================================== RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v retrieving revision 1.670 diff -u -p -r1.670 comment.module --- modules/comment/comment.module 9 Dec 2008 11:30:24 -0000 1.670 +++ modules/comment/comment.module 9 Dec 2008 23:19:13 -0000 @@ -2143,3 +2143,136 @@ function comment_ranking() { ), ); } + +/** + * Implementation of hook_token_list(). + */ +function comment_token_list($type = 'all') { + if ($type == 'comment' || $type == 'all') { + $tokens['comment']['comment-id'] = t('Comment ID'); + $tokens['comment']['comment-title'] = t('Comment title'); + $tokens['comment']['comment-body'] = t('Comment body'); + $tokens['comment']['comment-author-id'] = t("Comment author's user id"); + $tokens['comment']['comment-author-name'] = t("Comment author's user name"); + $tokens['comment']['comment-creation_date'] = t("Comment creation year (four digit)"); + + return $tokens; + } +} + +/** + * Implementation of hook_tokens(). + */ +function comment_tokens() { + $tokens = array(); + $tokens['comment-id'] = "comment_get_id"; + $tokens['comment-title'] = "comment_get_title"; + $tokens['comment-body'] = "comment_get_body"; + $tokens['comment-author-id'] = "comment_get_author_id"; + $tokens['comment-author-name'] = "comment_get_author_name"; + $tokens['comment-creation-date'] = "comment_get_creation_date"; + return $tokens; +} + +/** + * Returns true if the specified object is a comment; + * false otherwise. + * + * @param $object + * The object. + * @return + * True if the object represent a comment. + */ +function _comment_is_comment($object) { + return (isset($object) && isset($object->comment)); +} + +/** + * Returns the comment id. + * + * @param $object + * The comment. + * @return + * The comment id. + */ +function comment_get_id($object = NULL) { + if (_comment_is_comment($object)) { + return $object->nid; + } + return ''; +} + +/** + * Returns the comment title. + * + * @param $object + * The comment + * @return + * The title. + */ +function comment_get_title($object = NULL) { + if (_comment_is_comment($object)) { + return $object->subject; + } + return ''; +} + +/** + * Returns the comment body. + * + * @param $object + * The comment + * @return + * The comment body. + */ +function comment_get_body($object = NULL) { + if (_comment_is_comment($object)) { + return $object->comment; + } + return ''; +} + +/** + * Returns the id of the comment author. + * + * @param $object + * The comment. + * @return + * The author id. + */ +function comment_get_author_id($object = NULL) { + if (_comment_is_comment($object)) { + return $object->uid; + } + return ''; +} + +/** + * Returns the name of the comment author. + * + * @param $object + * The comment. + * @return + * The name. + */ +function comment_get_author_name($object = NULL) { + if (_comment_is_comment($object)) { + return $object->name; + } + return ''; +} + +/** + * Returns the comment creation date. + * + * @param $object + * The comment. + * @return + * The creation date. + */ +function comment_get_creation_date($object = NULL) { + if (_comment_is_comment($object)) { + return format_date($object->timestamp, 'short', ''); + } + return ''; +} Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.1001 diff -u -p -r1.1001 node.module --- modules/node/node.module 5 Dec 2008 22:18:45 -0000 1.1001 +++ modules/node/node.module 9 Dec 2008 23:19:15 -0000 @@ -3000,3 +3000,172 @@ function node_list_permissions($type) { return $perms; } + +/** + * Implementation of hook_token_list(). + */ +function node_token_list($type = 'all') { + if ($type == 'node' || $type == 'all') { + $tokens['node']['node-id'] = t('Node ID'); + $tokens['node']['node-type'] = t('Node type'); + $tokens['node']['node-type-name'] = t('Node type (user-friendly version)'); + $tokens['node']['node-title'] = t('Node title'); + + $tokens['node']['node-author-id'] = t("Node author's user id"); + $tokens['node']['node-author-name'] = t("Node author's user name"); + + $tokens['node']['node-created'] = t("Node creation date"); + $tokens['node']['node-modified'] = t('All tokens for node creation dates can also be used with with the "mod-" prefix; doing so will use the modification date rather than the creation date.'); + + return $tokens; + } +} + +/** + * Implementation of hook_tokens(). + */ +function node_tokens() { + $tokens = array(); + $tokens['node-id'] = "node_get_id"; + $tokens['node-type'] = "node_get_type"; + $tokens['node-type-name'] = "node_get_type_name"; + $tokens['node-title'] = "node_get_title"; + $tokens['node-author-id'] = "node_get_author_id"; + $tokens['node-author-name'] = "node_get_author_name"; + $tokens['node-created'] = "node_get_creation_date"; + $tokens['node-modified'] = "node_get_modification_date"; + return $tokens; +} + +/** + * Returns true if the specified object represents a node; false + * otherwise. + * + * @param $object + * The object. + * @return + * True if the object is a node. + */ +function _node_is_node($object) { + return (isset($object) && isset($object->nid) && isset($object->type)); +} + +/** + * Returns the node id. + * + * @param $object + * The node object. + * @return + * The node id. + */ +function node_get_id($object = NULL) { + if (_node_is_node($object)) { + return $object->nid; + } + return ''; +} + +/** + * Returns the node type. + * + * @param $object + * The node object. + * @return + * The node type. + */ +function node_get_type($object = NULL) { + if (_node_is_node($object)) { + return $object->type; + } + return ''; +} + +/** + * Returns the name of the node type. + * + * @param $object + * The node. + * @return + * The name of the node type. + */ +function node_get_type_name($object = NULL) { + if (_node_is_node($object)) { + return node_get_types('name', $object->type); + } + return ''; +} + +/** + * Returns the title of the node. + * + * @param $object + * The node. + * @return + * The node title. + */ +function node_get_title($object = NULL) { + if (_node_is_node($object)) { + return $object->title; + } + return ''; +} + +/** + * Returns the id of the node author. + * + * @param $object + * The node. + * @return + * The node author id. + */ +function node_get_author_id($object = NULL) { + if (_node_is_node($object)) { + return $object->uid; + } + return ''; +} + +/** + * Returns the name of the node author. + * + * @param $object + * The node. + * @return + * The node author. + */ +function node_get_author_name($object = NULL) { + if (_node_is_node($object)) { + return $object->name; + } + return ''; +} + +/** + * Returns the creation date of the node. + * + * @param $object + * The node. + * @return + * The creation date. + */ +function node_get_creation_date($object = NULL) { + if (_node_is_node($object)) { + return format_date($object->created, 'short', ''); + } + return ''; +} + +/** + * Returns the modification date of the node. + * + * @param $object + * The node. + * @return + * The modification date. + */ +function node_get_modification_date($object = NULL) { + if (_node_is_node($object)) { + return format_date($object->changed, 'short', ''); + } + return ''; +} Index: modules/system/system.install =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.install,v retrieving revision 1.291 diff -u -p -r1.291 system.install --- modules/system/system.install 3 Dec 2008 16:32:22 -0000 1.291 +++ modules/system/system.install 9 Dec 2008 23:19:16 -0000 @@ -1272,7 +1272,34 @@ function system_schema() { 'src' => array('src'), ), ); - + + $schema['token'] = array( + 'description' => 'Stores a token name/function mapping.', + 'fields' => array( + 'tid' => array( + 'description' => 'Primary Key: unique ID for token mapping.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'name' => array( + 'description' => 'Token name', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + 'function' => array( + 'description' => 'Function that retrieves the token value.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + ), + 'indexes' => array( + 'name' => array('name'), + ), + 'primary key' => array('tid'), + ); return $schema; } @@ -3143,6 +3170,48 @@ function system_update_7015() { } /** + * Add the token table. + */ +function system_update_7016() { + $ret = array(); + $schema['token'] = array( + 'description' => 'Stores a token name/function mapping.', + 'fields' => array( + 'tid' => array( + 'description' => 'Primary Key: unique ID for token mapping.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'name' => array( + 'description' => 'Token name', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + 'function' => array( + 'description' => 'Function that retrieves the token value.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + ), + 'indexes' => array( + 'name' => array('name'), + ), + 'primary key' => array('tid'), + ); + + db_create_table($ret, 'token', $schema['token']); + + // Populate the token table that maps a token to the function that returns + // the associated value. + registry_rebuild(); + token_refresh(); + return $ret; +} + +/** * @} End of "defgroup updates-6.x-to-7.x" * The next series of updates should start at 8000. */ Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.650 diff -u -p -r1.650 system.module --- modules/system/system.module 28 Nov 2008 09:25:59 -0000 1.650 +++ modules/system/system.module 9 Dec 2008 23:19:17 -0000 @@ -2252,3 +2252,98 @@ function theme_meta_generator_header($ve function system_image_toolkits() { return array('gd'); } + +/** + * Implementation of hook_token_values(). + */ +function system_token_values($type, $object = NULL) { + $values = array(); + switch ($type) { + case 'global': + global $base_url; + $values['site-url'] = $base_url; + $values['site-name'] = variable_get('site_name', t('Drupal')); + $values['site-slogan'] = variable_get('site_slogan', ''); + $values['site-mail'] = variable_get('site_mail', ''); + $values['site-date'] = format_date(time(), 'short', '', variable_get('date_default_timezone', 0)); + break; + } + return $values; +} + +/** + * Implementation of hook_token_list(). + */ +function system_token_list($type = 'all') { + $tokens = array(); + $tokens['global']['site-url'] = t('The url of the current Drupal website.'); + $tokens['global']['site-name'] = t('The name of the current Drupal website.'); + $tokens['global']['site-slogan'] = t('The slogan of the current Drupal website.'); + $tokens['global']['site-mail'] = t('The contact email address for the current Drupal website.'); + $tokens['global']['site-date'] = t("The current date on the site's server."); + return $tokens; +} + +/** + * Implementation of hook_tokens(). + */ +function system_tokens() { + $tokens = array(); + $tokens['site-url'] = "system_get_site_url"; + $tokens['site-name'] = "system_get_site_name"; + $tokens['site-slogan'] = "system_get_site_slogan"; + $tokens['site-mail'] = "system_get_site_mail"; + $tokens['site-date'] = "system_get_site_date"; + return $tokens; +} + +/** + * Returns the url for this site. + * + * @return + * The url. + */ +function system_get_site_url() { + global $base_url; + return $base_url; +} + +/** + * Returns the name of this site. + * + * @return + * The site name. + */ +function system_get_site_name() { + return variable_get('site_name', t('Drupal')); +} + +/** + * Returns the site slogan for this site. + * + * @return + * The site slogan. + */ +function system_get_site_slogan() { + return variable_get('site_slogan', ''); +} + +/** + * Returns the site email address. + * + * @return + * The email address. + */ +function system_get_site_mail() { + return variable_get('site_mail', ''); +} + +/** + * Returns a string representation of the current time on the server. + * + * @return + * The current time. + */ +function system_get_site_date() { + return format_date(time(), 'short', '', variable_get('date_default_timezone', 0)); +} Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.944 diff -u -p -r1.944 user.module --- modules/user/user.module 29 Nov 2008 09:33:51 -0000 1.944 +++ modules/user/user.module 9 Dec 2008 23:19:20 -0000 @@ -2419,3 +2419,188 @@ function _user_forms(&$edit, $account, $ return empty($groups) ? FALSE : $groups; } +/** + * Implementation of hook_token_list() + */ +function user_token_list($type = 'all') { + $tokens = array(); + $tokens['global']['user-name'] = t('The name of the currently logged in user.'); + $tokens['global']['user-id'] = t('The user ID of the currently logged in user.'); + $tokens['global']['user-mail'] = t('The email address of the currently logged in user.'); + $tokens['global']['user-reg-date'] = t("User's registration date"); + $tokens['global']['user-reg-since'] = t("Days since the user registered"); + $tokens['global']['user-log-date'] = t("User's last login date"); + $tokens['global']['user-log-since'] = t("Days since the user's last login"); + $tokens['global']['user-date-in-tz'] = t("The current date in the user's timezone"); + $tokens['global']['user-account-url'] = t("The URL of the user's profile page."); + $tokens['global']['user-account-edit'] = t("The URL the user's account editing page."); + return $tokens; +} + +/** + * Implementation of hook_tokens(). + */ +function user_tokens() { + $tokens = array(); + $tokens['user-name'] = "user_get_name"; + $tokens['user-id'] = "user_get_id"; + $tokens['user-mail'] = "user_get_mail"; + $tokens['user-reg-date'] = "user_get_registration_date"; + $tokens['user-reg-since'] = "user_get_registered_since"; + $tokens['user-log-date'] = "user_get_login_date"; + $tokens['user-log-since'] = "user_get_login_since"; + $tokens['user-date-in-tz'] = "user_get_user_time"; + $tokens['user-account-url'] = "user_get_account_url"; + $tokens['user-account-edit'] = "user_get_account_edit_url"; + return $tokens; +} + +/** + * Returns the user account either from the specified user + * or from the global user reference. + * + * @param $object (Optional) + * The user account. + * @return + * The user account. + */ +function _user_get_account($object = NULL) { + if (isset($object) && isset($object->uid) && isset($object->mail)) { + $account = $object; + } + else { + global $user; + $account = user_load(array('uid' => $user->uid)); + } + return $account; +} + +/** + * Returns the login name of the user. + * + * @param $object (Optional) + * The user. + * @return + * The login name. + */ +function user_get_name($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? $account->name : variable_get('anonymous', 'Anonymous'); +} + +/** + * Returns the id of the user. + * + * @param $object (Optional) + * The user. + * @return + * The user's id. + */ +function user_get_id($object = NULL) { + $account = _user_get_account($object); + return $account->uid; +} + +/** + * Returns the user's email address. + * + * @param $object (Optional) + * The user. + * @return + * The user's email address. + */ +function user_get_mail($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? $account->mail : ''; +} + +/** + * Returns the user's registration date. + * + * @param $object (Optional) + * The user. + * @return + * The registration date of the user. + */ +function user_get_registration_date($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? format_date($account->created, 'short') : ''; +} + +/** + * Returns the elapsed time since the user registered. + * + * @param $object (Optional) + * The user. + * @return + * The elapsed time since the user registered. + */ +function user_get_registered_since($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? format_interval(time() - $account->created) : ''; +} + +/** + * Returns the time when the user logged in. + * + * @param $object (Optional) + * The user. + * @return + * A string representation of the login time. + */ +function user_get_login_date($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? format_date($account->access, 'short') : ''; +} + +/** + * Returns the elapsed time since the user logged in. + * + * @param $object (Optional) + * The user. + * @return + * A string representation of the time elapsed since the user logged in. + */ +function user_get_login_since($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? format_interval(time() - $account->access) : ''; +} + +/** + * Returns the current time, displayed with respect to the user's timezone. + * + * @param $object (Optional) + * The user. + * @return + * A string representation of the current time in the user's timezone. + */ +function user_get_user_time($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? format_date(time(), 'short', '', $account->timezone) : ''; +} + +/** + * Returns a url that points to the user's account. + * + * @param $object (Optional) + * The user. + * @return + * The url of the user's account page. + */ +function user_get_account_url($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? url("user/$account->uid") : ''; +} + +/** + * Returns a url that points to the edit page for the user's account. + * + * @param $object (Optional) + * The user. + * @return + * The url for the user's account edit page. + */ +function user_get_account_edit_url($object = NULL) { + $account = _user_get_account($object); + return $account->uid ? url("user/$account->uid/edit") : ''; +}