| 1 |
<?php
|
| 2 |
// $Id: token.module,v 1.15 2009/07/12 04:43:23 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implement hook_menu().
|
| 6 |
*/
|
| 7 |
function token_menu() {
|
| 8 |
$items['admin/settings/token'] = array(
|
| 9 |
'title' => 'Token testing',
|
| 10 |
'description' => "Test replacement tokens in real time.",
|
| 11 |
'page callback' => 'drupal_get_form',
|
| 12 |
'page arguments' => array('token_test_form'),
|
| 13 |
'access arguments' => array('administer site'),
|
| 14 |
'file' => 'token.dev.inc',
|
| 15 |
);
|
| 16 |
return $items;
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Replace all tokens in a given string with appropriate values.
|
| 21 |
*
|
| 22 |
* @param $text
|
| 23 |
* A string potentially containing replacable tokens.
|
| 24 |
* @param $data
|
| 25 |
* (optional) An array of keyed objects. For simple replacement scenarios
|
| 26 |
* 'node', 'user', and others are common keys, with an accompanying node or
|
| 27 |
* user object being the value. Some token types, like 'site', do not require
|
| 28 |
* any explicit information from $data and can be replaced even if it is empty.
|
| 29 |
* @param $options
|
| 30 |
* (optional) A keyed array of settings and flags to control the token
|
| 31 |
* replacement process. Supported options are:
|
| 32 |
* - language: A language object to be used when generating locale-sensitive
|
| 33 |
* tokens.
|
| 34 |
* - callback: A callback function that will be used to post-process the array
|
| 35 |
* of token replaements after they are generated. Can be used when modules
|
| 36 |
* require special formatting of token text, for example URL encoding or
|
| 37 |
* truncation to a specific length.
|
| 38 |
* - sanitize: A boolean flag indicating that tokens should be sanitized for
|
| 39 |
* display to a web browser. Developers who set this option to FALSE assume
|
| 40 |
* responsibility for running filter_xss(), check_plain() or other
|
| 41 |
* appropriate scrubbing functions before displaying data to users.
|
| 42 |
* @return
|
| 43 |
* Text with tokens replaced.
|
| 44 |
*/
|
| 45 |
function token_replace($text, array $data = array(), array $options = array()) {
|
| 46 |
$token_list = token_scan($text);
|
| 47 |
$replacements = token_generate($token_list, $data, $options);
|
| 48 |
|
| 49 |
if (!empty($options['callback']) && drupal_function_exists($options['callback'])) {
|
| 50 |
$function = $options['callback'];
|
| 51 |
$function($replacements, $data, $options);
|
| 52 |
}
|
| 53 |
|
| 54 |
$tokens = array_keys($replacements);
|
| 55 |
$values = array_values($replacements);
|
| 56 |
|
| 57 |
return str_replace($tokens, $values, $text);
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Build a list of all token-like patterns that appear in the text.
|
| 62 |
*
|
| 63 |
* @param $text
|
| 64 |
* The text to be scanned for possible tokens.
|
| 65 |
* @return
|
| 66 |
* An associative array of discovered tokens, grouped by type.
|
| 67 |
*/
|
| 68 |
function token_scan($text) {
|
| 69 |
// Matches tokens with the following pattern: [$type:$token]
|
| 70 |
// $type and $token may not contain white spaces.
|
| 71 |
preg_match_all('/\[([^\s\]:]*):([^\s\]]*)\]/', $text, $matches);
|
| 72 |
|
| 73 |
$types = $matches[1];
|
| 74 |
$tokens = $matches[2];
|
| 75 |
|
| 76 |
// Iterate through the matches, building an associative array containing
|
| 77 |
// $tokens grouped by $types, pointing to the version of the token found in
|
| 78 |
// the source text. For example, $results['node']['title'] = '[node:title]';
|
| 79 |
$results = array();
|
| 80 |
for ($i = 0; $i < count($tokens); $i++) {
|
| 81 |
$results[$types[$i]][$tokens[$i]] = $matches[0][$i];
|
| 82 |
}
|
| 83 |
|
| 84 |
return $results;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Generate replacement values for a list of tokens.
|
| 89 |
*
|
| 90 |
* @param $raw_tokens
|
| 91 |
* A keyed array of tokens, and their original raw form in the source text.
|
| 92 |
* @param $data
|
| 93 |
* (optional) An array of keyed objects. For simple replacement scenarios
|
| 94 |
* 'node', 'user', and others are common keys, with an accompanying node or
|
| 95 |
* user object being the value. Some token types, like 'site', do not require
|
| 96 |
* any explicit information from $data and can be replaced even if it is empty.
|
| 97 |
* @param $options
|
| 98 |
* (optional) A keyed array of settings and flags to control the token
|
| 99 |
* replacement process. Supported options are:
|
| 100 |
* - 'language' A language object to be used when generating locale-sensitive
|
| 101 |
* tokens.
|
| 102 |
* - 'callback' A callback function that will be used to post-process the array
|
| 103 |
* of token replaements after they are generated. Can be used when modules
|
| 104 |
* require special formatting of token text, for example URL encoding or
|
| 105 |
* truncation to a specific length.
|
| 106 |
* - 'sanitize' A boolean flag indicating that tokens should be sanitized for
|
| 107 |
* display to a web browser. Developers who set this option to FALSE assume
|
| 108 |
* responsibility for running filter_xss(), check_plain() or other
|
| 109 |
* appropriate scrubbing functions before displaying data to users.
|
| 110 |
* @return
|
| 111 |
* An associative array of replacement values, keyed by the original 'raw'
|
| 112 |
* tokens that were found in the source text. For example:
|
| 113 |
* $results['[node:title]'] = 'My new node';
|
| 114 |
*/
|
| 115 |
function token_generate(array $raw_tokens, array $data = array(), array $options = array()) {
|
| 116 |
$results = array();
|
| 117 |
$options += array('sanitize' => TRUE);
|
| 118 |
|
| 119 |
foreach ($raw_tokens as $type => $tokens) {
|
| 120 |
foreach (module_invoke_all('tokens', $type, $tokens, $data, $options) as $original => $replacement) {
|
| 121 |
$results[$original] = $replacement;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
return $results;
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Given a list of tokens, return those that begin with a specific prefix.
|
| 129 |
*
|
| 130 |
* Used to extract a group of 'chained' tokens (such as [node:author:name]) from
|
| 131 |
* the full list of tokens found in text. For example:
|
| 132 |
* @code
|
| 133 |
* $data = array(
|
| 134 |
* 'author:name' => '[node:author:name]',
|
| 135 |
* 'title' => '[node:title]',
|
| 136 |
* 'created' => '[node:author:name]',
|
| 137 |
* );
|
| 138 |
* $results = token_find_with_prefix($data, 'author');
|
| 139 |
* $results == array('name' => '[node:author:name]');
|
| 140 |
* @endcode
|
| 141 |
*
|
| 142 |
* @param $tokens
|
| 143 |
* A keyed array of tokens, and their original raw form in the source text.
|
| 144 |
* @param $prefix
|
| 145 |
* A textual string to be matched at the beginning of the token.
|
| 146 |
* @param $delimiter
|
| 147 |
* An optional string containing the character that separates the prefix from
|
| 148 |
* the rest of the token. Defaults to ':'.
|
| 149 |
* @return
|
| 150 |
* An associative array of discovered tokens, with the prefix and delimiter
|
| 151 |
* stripped from the key.
|
| 152 |
*/
|
| 153 |
function token_find_with_prefix(array $tokens, $prefix, $delimiter = ':') {
|
| 154 |
$results = array();
|
| 155 |
foreach ($tokens as $token => $raw) {
|
| 156 |
$parts = split($delimiter, $token, 2);
|
| 157 |
if (count($parts) == 2 && $parts[0] == $prefix) {
|
| 158 |
$results[$parts[1]] = $raw;
|
| 159 |
}
|
| 160 |
}
|
| 161 |
return $results;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Returns metadata describing supported tokens.
|
| 166 |
*
|
| 167 |
* The metadata array contains token type, name, and description data as well as
|
| 168 |
* an optional pointer indicating that the token chains to another set of tokens.
|
| 169 |
* For example:
|
| 170 |
* @code
|
| 171 |
* $data['types']['node'] = array(
|
| 172 |
* 'name' => t('Nodes'),
|
| 173 |
* 'description' => t('Tokens related to node objects.'),
|
| 174 |
* );
|
| 175 |
* $data['tokens']['node']['title'] = array(
|
| 176 |
* 'name' => t('Title'),
|
| 177 |
* 'description' => t('The title of the current node.'),
|
| 178 |
* );
|
| 179 |
* $data['tokens']['node']['author'] = array(
|
| 180 |
* 'name' => t('Author'),
|
| 181 |
* 'description' => t('The author of the current node.'),
|
| 182 |
* 'type' => 'user',
|
| 183 |
* );
|
| 184 |
* @endcode
|
| 185 |
* @return
|
| 186 |
* An associative array of token information, grouped by token type.
|
| 187 |
*/
|
| 188 |
function token_info() {
|
| 189 |
$data = &drupal_static(__FUNCTION__);
|
| 190 |
if (!isset($data)) {
|
| 191 |
$data = module_invoke_all('token_info');
|
| 192 |
drupal_alter('token_info', $data);
|
| 193 |
}
|
| 194 |
return $data;
|
| 195 |
}
|
| 196 |
|
| 197 |
/*
|
| 198 |
* Implement hook_registry_files_alter().
|
| 199 |
*
|
| 200 |
* The files in Token module's /modules directory implement hooks on behalf
|
| 201 |
* of other modules. This hook iterates through them and switches the 'owning'
|
| 202 |
* module from token to the real one.
|
| 203 |
*
|
| 204 |
* We loves us drupal_alter().
|
| 205 |
*/
|
| 206 |
function token_registry_files_alter(&$files, $modules) {
|
| 207 |
$dir = $modules['token']->dir;
|
| 208 |
foreach ($modules['token']->info['files'] as $file) {
|
| 209 |
$path = split('/', $file);
|
| 210 |
$inc_file = array_pop($path);
|
| 211 |
$pieces = explode('.', $inc_file, 2);
|
| 212 |
if ($pieces[1] == 'tokens.inc') {
|
| 213 |
$files["$dir/$file"]['module'] = $pieces[0];
|
| 214 |
}
|
| 215 |
}
|
| 216 |
}
|